<br><br><div class="gmail_quote">On Sun, May 8, 2011 at 7:22 PM, Paul Anton Letnes <span dir="ltr"><<a href="mailto:paul.anton.letnes@gmail.com">paul.anton.letnes@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div><div></div><div class="h5"><br>
On 8. mai 2011, at 17.32, Warren Weckesser wrote:<br>
<br>
><br>
><br>
> On Sun, May 8, 2011 at 7:23 PM, Charles R Harris <<a href="mailto:charlesr.harris@gmail.com">charlesr.harris@gmail.com</a>> wrote:<br>
><br>
><br>
> On Sun, May 8, 2011 at 3:15 PM, Paul Anton Letnes <<a href="mailto:paul.anton.letnes@gmail.com">paul.anton.letnes@gmail.com</a>> wrote:<br>
> Hi,<br>
><br>
> it seems that I have found a bug in numpy.ndarray. numpy 1.5.1, python 2.7.1 from macports on mac os x 10.6.7. I got the same error on Fedora 14 with numpy 1.4.1 and python 2.7. Appending a [0] to the last line solves the problem.<br>

><br>
> % python testcrash.py                                                                                                                                          [14:13:27 on 11-05-08]<br>
> <type 'numpy.ndarray'> [ 12.+0.1j]<br>
> <type 'numpy.ndarray'> [ 1.+0.1j]<br>
> complex128<br>
> Traceback (most recent call last):<br>
>  File "testcrash.py", line 11, in <module><br>
>    A[0] = A[0] + (eps1 - eps2)<br>
> TypeError: can't convert complex to float<br>
><br>
>  % cat testcrash.py<br>
> #!/usr/bin/env python<br>
><br>
> import numpy<br>
><br>
> A = numpy.zeros(10, dtype=numpy.complex128)<br>
> eps1 = numpy.complex128([12.0 + 0.1j])<br>
> eps2 = numpy.complex128([1.0 + 0.1j])<br>
><br>
> It's the brackets, numpy.complex128([1.0 + 0.1j]) is a 1d array, not a scalar. The error message is less than helpful though.<br>
><br>
><br>
><br>
> But the same pattern works fine with float64:<br>
><br>
> In [2]: x = array([1.0, 2.0])<br>
><br>
> In [3]: y = array([10.0])<br>
><br>
> In [4]: x[0] = y   # Works<br>
><br>
> In [5]: a = array([1.0, 2.0], dtype=complex128)<br>
><br>
> In [6]: b = array([10.0 + 1j])<br>
><br>
> In [7]: a[0] = b   # Error<br>
> ---------------------------------------------------------------------------<br>
> TypeError                                 Traceback (most recent call last)<br>
><br>
> /Users/warren/<ipython console> in <module>()<br>
><br>
> TypeError: can't convert complex to float<br>
><br>
><br>
> Something is fishy about that.<br>
><br>
</div></div>I agree. One thing is if arrays don't support this kind of assignment, but behavior should be the same for complex and float. IMHO.<br>
<font color="#888888"><br></font></blockquote><div><br>Seems that it works for most types as long as there is just one element:<br><br>In [1]: a = ones(10)<br><br>In [2]: b = ones((1,1,1,1))<br><br>In [3]: a[0] = b<br><br>
<br>So that seems to be the standard. IMHO, most uses of this sort are likely to be programming errors, but there it is.<br><br>Chuck <br></div></div>