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

Stuart Brorson sdb@cloud9....
Fri Jan 4 16:03:10 CST 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?


Stuart Brorson
Interactive Supercomputing, inc.
135 Beaver Street | Waltham | MA | 02452 | USA

More information about the Numpy-discussion mailing list