Darkowic
Darkowic

Reputation: 690

Numpy C-Api array_equal

I've tried to find function comparing two PyArrayObject - something like numpy array_equal But I haven't found anything. Do you know function like this?

If not - How to import this numpy array_equal to my C code?

Upvotes: 1

Views: 182

Answers (1)

hpaulj
hpaulj

Reputation: 231530

Here's the code for array_equal:

def array_equal(a1, a2):
    try:
        a1, a2 = asarray(a1), asarray(a2)
    except:
        return False
    if a1.shape != a2.shape:
        return False
    return bool(asarray(a1 == a2).all())

As you can see it is not a c-api level function. After making sure both inputs are arrays, and that shape match it performs a element == test, followed by all.

This does not work reliably with floats. It's ok with ints and booleans.

There probably is some sort of equality function in the c-api, but a clone of this probably isn't what you need.


PyArray_CountNonzero(PyArrayObject* self)

might be a good function. I remember from digging into the code earlier that PyArray_Nonzero uses it to determine how big of an array to allocate and return. You could give it an object that compares the elements of your 2 arrays (in what ever way is appropriate given the dtype), and then test for a nonzero count.

Or you could construct your own iterator that bails out as soon as it gets a not-equal pair of elements. Use nditer to get the full array broadcasting power.

Upvotes: 3

Related Questions