[Numpy-discussion] all elements equal

Keith Goodman kwgoodman@gmail....
Mon Mar 5 14:12:41 CST 2012


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


More information about the NumPy-Discussion mailing list