[Numpy-discussion] Puzzling numpy results?

Tim Churches tchur at optushome.com.au
Mon Apr 8 13:34:16 CDT 2002


Andrew McNamara wrote:
> 
> The behavior I'm seeing with zero length Numeric arrays is not what I
> would have expected:
> 
>     >>> from Numeric import *
>     >>> array([5]) != array([])
>     zeros((0,), 'l')
>     >>> array([]) == array([])
>     zeros((0,), 'l')
>     >>> allclose(array([5]), array([]))
>     1

The Numpy docs point out that == and != are implemented via the logical
ufuncs, and that:

 "The ``logical'' ufuncs also perform their operations on arrays in
elementwise fashion, just like the ``mathematical'' ones."

I think this explains the results you are seeing: if you do an
element-wise comparison of a length-one array with a zero-length array,
the Numpy
recycling rule means that you should always get a zero-length result.
Note that zeros((0,),'l') is not zero, it is zero zeros. So although the
results are surprising (at least to me, and you), I think the observed
results are logically correct, although surprising.

But, if that is the case, why does this hold (which I suspect reflects
what you originally expected)?:

>>> from Numeric import *
>>> array([5,6]) != array([])
1
>>> array([5,6]) == array([])
0

Tim C




More information about the Numpy-discussion mailing list