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

Pierre GM pgmdevlist at gmail.com
Fri Jan 4 16:26:00 EST 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