[Numpy-discussion] equality of empty arrays

Skipper Seabold jsseabold at gmail.com
Tue Oct 12 09:15:11 EDT 2010


On Tue, Oct 12, 2010 at 7:40 AM, Friedrich Romstedt
<friedrichromstedt at gmail.com> wrote:
> 2010/10/12 Ian Goodfellow <goodfellow.ian at gmail.com>:
>> If the arrays are the same size or can be broadcasted to the same
>> size, it returns true or false on an elementwise basis.
>> If the arrays are not the same size and can't be broadcasted to the
>> same size, it returns False, which was a surprise to me too.
>>
>>  >>> import numpy as N
>>  >>> N.asarray([[0,1],[2,3]]) == N.asarray([[1,1],[3,3]])
>> array([[False,  True],
>>        [False,  True]], dtype=bool)
>>  >>> N.asarray([[0,1],[2,3]]) == N.asarray([[1,1]])
>> array([[False,  True],
>>        [False, False]], dtype=bool)
>>  >>> N.asarray([[0,1],[2,3]]) == N.asarray([[1,1],[3,3],[5,5]])
>> False
>
> This behaviour should maybe be explained in the docs, maybe here:
> http://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html#arithmetic-and-comparison-operations
> .
>
> It relies on the following:
>
> Python first asks the objects using their operator overloads, if
> existent.  If the first one returns NotImplemented or does not exist,
> Python falls back to the second operand.  If this does not work
> either, Python compares objects equal if they are the *same* object, I
> believe based on id() (maybe this has just the same effect).
>
> Since numpy cannot compare in the numpy-sense if arrays are not
> broadcastable to the same shape, both __eq__() methods of both
> operands will return NotImplemented, like in the last case in the
> cited section above, where "id() comparison" takes place, yielding
> False.
>
> Btw, this issue occurs on some Poisson basis with non-negligible q :-)
> on the list ... I could start improving the docs in this respect using
> the responses of others and me from the list, but I have no idea how
> they would make in into the official docs?
>

Some elaboration here?

http://www.scipy.org/FAQ#head-9448031cbb9760d0a44db0eceda47393e56e8270

Skipper



More information about the NumPy-Discussion mailing list