[SciPy-dev] test matrix equality

Robert Kern rkern at ucsd.edu
Fri Nov 26 16:04:26 CST 2004


Alan G Isaac wrote:
> Newbie question:
> 
> What is the right/efficient way to test
> for equality between two matrices?
> While alltrue(ravel(A==B)) works,
> I am guessing alltrue(alltrue(A==B))
> is better.

What you want is the function all(A==B).

Also, note that strict equality testing is probably not what you want 
for float arrays. allclose(A, B) would be more appropriate in the 
floating point case (and doesn't require ravel() shenanigans).

> If I import MA it seems I can use
> allequal, but this does not seem
> to be available otherwise.  (Why??)

Because allequal is defined by MA. all() is defined by scipy.

> Also, although this behaves like
> I want, it is oddly inconsistent
> with the other 'all' functions.

Not really. Only alltrue takes an axis argument (because it is sometimes 
useful to be able to do an axis-wise operation like this).

> I'd like to be able to just look at
> A.eq(B) for any matrices A and B.

Just use all().

> Thank you,
> Alan Isaac
> 
> PS Suggestion: allow setting index=None
> for alltrue to test all individual
> elements.

Just use all().

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter




More information about the Scipy-dev mailing list