[Numpy-discussion] bug in numpy.ndarray?

Charles R Harris charlesr.harris at gmail.com
Mon May 9 15:26:05 EDT 2011


On Sun, May 8, 2011 at 7:22 PM, Paul Anton Letnes <
paul.anton.letnes at gmail.com> wrote:

>
> On 8. mai 2011, at 17.32, Warren Weckesser wrote:
>
> >
> >
> > On Sun, May 8, 2011 at 7:23 PM, Charles R Harris <
> charlesr.harris at gmail.com> wrote:
> >
> >
> > On Sun, May 8, 2011 at 3:15 PM, Paul Anton Letnes <
> paul.anton.letnes at gmail.com> wrote:
> > Hi,
> >
> > 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.
> >
> > % python testcrash.py
>
>          [14:13:27 on 11-05-08]
> > <type 'numpy.ndarray'> [ 12.+0.1j]
> > <type 'numpy.ndarray'> [ 1.+0.1j]
> > complex128
> > Traceback (most recent call last):
> >  File "testcrash.py", line 11, in <module>
> >    A[0] = A[0] + (eps1 - eps2)
> > TypeError: can't convert complex to float
> >
> >  % cat testcrash.py
> > #!/usr/bin/env python
> >
> > import numpy
> >
> > A = numpy.zeros(10, dtype=numpy.complex128)
> > eps1 = numpy.complex128([12.0 + 0.1j])
> > eps2 = numpy.complex128([1.0 + 0.1j])
> >
> > 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.
> >
> >
> >
> > But the same pattern works fine with float64:
> >
> > In [2]: x = array([1.0, 2.0])
> >
> > In [3]: y = array([10.0])
> >
> > In [4]: x[0] = y   # Works
> >
> > In [5]: a = array([1.0, 2.0], dtype=complex128)
> >
> > In [6]: b = array([10.0 + 1j])
> >
> > In [7]: a[0] = b   # Error
> >
> ---------------------------------------------------------------------------
> > TypeError                                 Traceback (most recent call
> last)
> >
> > /Users/warren/<ipython console> in <module>()
> >
> > TypeError: can't convert complex to float
> >
> >
> > Something is fishy about that.
> >
> 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.
>
>
Seems that it works for most types as long as there is just one element:

In [1]: a = ones(10)

In [2]: b = ones((1,1,1,1))

In [3]: a[0] = b


So that seems to be the standard. IMHO, most uses of this sort are likely to
be programming errors, but there it is.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110509/8c09c804/attachment.html>


More information about the NumPy-Discussion mailing list