[Numpy-discussion] all elements equal

Keith Goodman kwgoodman@gmail....
Mon Mar 5 14:30:05 CST 2012


On Mon, Mar 5, 2012 at 12:12 PM, Keith Goodman <kwgoodman@gmail.com> wrote:
> On Mon, Mar 5, 2012 at 12:06 PM, Neal Becker <ndbecker2@gmail.com> wrote:
>> But doesn't this one fail on empty array?
>
> Yes. I'm optimizing for fun, not for corner cases. This should work
> for size zero and NaNs:
>
> @cython.boundscheck(False)
> @cython.wraparound(False)
> def allequal(np.ndarray[np.float64_t, ndim=1] a):
>    cdef:
>        np.float64_t a0
>        Py_ssize_t i, n=a.size
>    if n == 0:
>        return False # Or would you like True?
>    a0 = a[0]
>    for i in range(n):
>        if a[i] != a0:
>            return False
>    return True

Sorry for all the posts. I'll go back to being quiet. Seems like
np.allclose returns True for empty arrays:

I[2] a = np.array([])
I[3] np.allclose(np.array([]), np.array([]))
O[3] True

The original allequal cython code did the same:

I[4] allequal(a)
O[4] True


More information about the NumPy-Discussion mailing list