[Numpy-discussion] bug in numpy.ndarray?
Robert Kern
robert.kern at gmail.com
Sun May 8 20:31:47 EDT 2011
On Sun, May 8, 2011 at 19:23, 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.
Well, not quite. It looks like we do handle (1,) arrays on the RHS just fine:
[~/scratch]
|46> A = np.arange(10)
[~/scratch]
|47> A[0] = np.array([1])
[~/scratch]
|48> A[0]
1
It works for a variety of dtypes except for the complexes:
[~/scratch]
|53> for dt in [bool, np.uint8, np.int8, int, float, np.float32,
np.complex64, np.complex128, object]:
...> A = np.zeros(10, dtype=dt)
...> try:
...> A[0] = np.array([1], dtype=dt)
...> except Exception, e:
...> print '%s failed: %s: %s' % (dt.__name__, type(e).__name__, e)
...> else:
...> print '%s succeeded' % dt.__name__
...>
bool succeeded
uint8 succeeded
int8 succeeded
int succeeded
float succeeded
float32 succeeded
complex64 failed: TypeError: can't convert complex to float
complex128 failed: TypeError: can't convert complex to float
object succeeded
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the NumPy-Discussion
mailing list