[Numpy-discussion] equality of empty arrays
Friedrich Romstedt
friedrichromstedt@gmail....
Tue Oct 12 06:40:28 CDT 2010
2010/10/12 Ian Goodfellow <goodfellow.ian@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?
Friedrich
More information about the NumPy-Discussion
mailing list