[Numpy-discussion] Assigning complex values to a real array

Robert Kern robert.kern at gmail.com
Sat Mar 7 04:35:35 EST 2009


On Sat, Mar 7, 2009 at 03:30, Stéfan van der Walt <stefan at sun.ac.za> wrote:
> 2009/3/7 Charles R Harris <charlesr.harris at gmail.com>:
>>> a = np.zeros(6) # real
>>> b= np.arange(6)*(2+3j) # complex
>>> a[1] = b[1] # shouldn't this break?
>>>
>>> What is the rationale behind this behaviour?
>>
>> The same as this:
>>
>> In [1]: a = zeros(2)
>>
>> In [2]: a[0] = '1'
>>
>> In [3]: a
>> Out[3]: array([ 1.,  0.])
>
> This difference is that, in your example, no information is lost.
> When assigning a complex value to a real array, you are probably doing
> something wrong.

In [5]: z = zeros(3, int)

In [6]: z[1] = 1.5

In [7]: z
Out[7]: array([0, 1, 0])

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco



More information about the NumPy-Discussion mailing list