[SciPy-User] np.array_equal for sparse arrays

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

[SciPy-User] np.array_equal for sparse arrays

Michael Lamparski
Hello everyone,

I feel like I am missing something.  It seems that sparse matrices have no 'any' method or anything useful regarding equality.  Is there not a better way to write this?

def sparse_equal(a, b):
    Equality test for sparse matrices which performs standard floating point equality,
    allows mismatched types, considers missing elements equal to zero, and
    requires identical shape (no broadcasting)
    return a.shape == b.shape and not (a != b).data.any()

(I suppose I could also write 'not (a != b).nnz', since the implementation of != appears to eliminate zeros, but I'm not sure if I should be relying on that)


SciPy-User mailing list
[hidden email]