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

Charles R Harris charlesr.harris@gmail....
Fri Mar 6 19:28:03 CST 2009


On Fri, Mar 6, 2009 at 6:18 PM, Stéfan van der Walt <stefan@sun.ac.za>wrote:

> Hi all,
>
> The following code succeeds, while I thought it should fail:
>
> 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.])

The question is whether such usage is likely in error, calling for an
exception, or a useful convenience to avoid a cast. I tend to think that
casts should be made explicit in such cases but it's a fine line. What about
this?

In [5]: a = zeros(2)

In [6]: a[0] = 1

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

Should a cast be required? What if the lhs is a float32 and the rhs is a
python float?

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090306/f3130a29/attachment.html 


More information about the Numpy-discussion mailing list