[Numpy-discussion] Nasty bug using pre-initialized arrays

Stuart Brorson sdb at cloud9.net
Fri Jan 4 17:03:10 EST 2008


>> Sometimes you need to initialize an array using zeros() before doing
>> an assignment to it in a loop.  If you assign a complex value to the
>> initialized array, the imaginary part of the array is dropped.  Does
>> NumPy do a silent type-cast which causes this behavior?  Is this
>> typecast a feature?
>
> By default, empty arrays are initialized as float. If you try to force a
> complex into it, yes, the imaginary part will be dropped. Use the dtype
> argument of zeros or ones to specify the type of your array, for example:
>
> B =  numpy.zeros((4,), dtype=numpy.complex_)

Hmmmm, I'll try this.  Thanks!

I still think that dumb users like me are more likely to assume that
the destination array will become complex when you assign complex to
it.  Is there an articulated philosophy of how assignments (and any
accompanying typecasting) should work in Numpy?

Cheers,

Stuart Brorson
Interactive Supercomputing, inc.
135 Beaver Street | Waltham | MA | 02452 | USA
http://www.interactivesupercomputing.com/




More information about the NumPy-Discussion mailing list