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

Pierre GM pgmdevlist@gmail....
Fri Jan 4 15:26:00 CST 2008


On Friday 04 January 2008 16:08:54 Stuart Brorson wrote:
> 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_)


More information about the Numpy-discussion mailing list