thouis / numpy-trac-migration

numpy Trac to github issues migration
2 stars 3 forks source link

numpy.any ignores imaginary part of arrays (Trac #2218) #6007

Closed numpy-gitbot closed 11 years ago

numpy-gitbot commented 11 years ago

Original ticket http://projects.scipy.org/numpy/ticket/2218 on 2012-09-25 by trac user mike.wimmer, assigned to unknown.

In numpy versions 1.6.1, 1.6.2 and the latest git version (those I tried), numpy.any ignores the imaginary part of arrays completely:

>>> numpy.version.version
'1.8.0.dev-6a847ef'
>>> numpy.any([1j])
False
>>> numpy.any(numpy.array([1j]))
False
>>> numpy.any(numpy.array([0.1j]))
False

whereas

>>> numpy.any(numpy.array([1+1j]))
True
>>> numpy.any(numpy.array([1+0j]))
True

Now, since

>>> bool(1j)
True

I think this is a genuine bug.

Note that in numpy 1.5.1 all of the above returned True, i.e. the imaginary part was taken into account then.

numpy-gitbot commented 11 years ago

atmention:charris wrote on 2012-09-25

Definitely a bug.

numpy-gitbot commented 11 years ago

Milestone changed to NumPy 1.7 by atmention:charris on 2012-09-25

numpy-gitbot commented 11 years ago

atmention:charris wrote on 2012-09-25

Looks to be a problem in the ufunc reduce method for complex:

In [15]: a = ones(5)*1j

In [16]: logical_or(a,a)
Out[16]: array([ True,  True,  True,  True,  True], dtype=bool)

In [17]: logical_or.reduce(a)
Out[17]: False
numpy-gitbot commented 11 years ago

atmention:charris wrote on 2012-09-25

I expect the problem arises from the fact that the output type is not the same as the input type.

One way to fix this would be to use a special functions for the any and all methods instead of the ufunc.

numpy-gitbot commented 11 years ago

trac user seberg wrote on 2012-10-01

Puh... I checked this a bit, and this seems to fix it, but i have really no idea whatsoever if this is the correct way to fix it or if there is more to fix... already puzzles me when those conversion functions from array_types.c.src get actually called. (Note that .astype(bool) also failed here.)

--- a/numpy/core/src/multiarray/lowlevel_strided_loops.c.src +++ b/numpy/core/src/multiarray/lowlevel_strided_loops.c.src atmention:atmention: -811,9 +811,17 atmention:atmention: static void dst_value[0] = _CONVERT_FN(src_value[0]); dst_value[1] = _CONVERT_FN(src_value[1]);

elif !atmention:alignedatmention:

numpy-gitbot commented 11 years ago

trac user seberg wrote on 2012-10-01

Sorry forgot formatting :/...

--- a/numpy/core/src/multiarray/lowlevel_strided_loops.c.src
+++ b/numpy/core/src/multiarray/lowlevel_strided_loops.c.src
atmention:atmention: -811,9 +811,17 atmention:atmention: static void
     dst_value[0] = _CONVERT_FN(src_value[0]);
     dst_value[1] = _CONVERT_FN(src_value[1]);
 #  elif !atmention:alignedatmention:
-    dst_value = _CONVERT_FN(src_value[0]);
+#    if atmention:is_bool2atmention:
+       dst_value = _CONVERT_FN(src_value[0]) || _CONVERT_FN(src_value[1]);
+#    else
+       dst_value = _CONVERT_FN(src_value[0]);
+#    endif
 #  else
-    *(_TYPE2 *)dst = _CONVERT_FN(src_value[0]);
+#    if atmention:is_bool2atmention:
+       *(_TYPE2 *)dst = _CONVERT_FN(src_value[0]) || _CONVERT_FN(src_value[1]);
+#    else
+       *(_TYPE2 *)dst = _CONVERT_FN(src_value[0]);
+#    endif
 #  endif
 #else
 #  if atmention:is_complex2atmention:
numpy-gitbot commented 11 years ago

atmention:charris wrote on 2012-10-04

Should be fixed in mainline.