[SciPy-user] __eq__ for scipy.sparse not working?

Joseph Turian turian at gmail.com
Mon Apr 14 17:45:25 EDT 2008


I am having trouble determining equality of sparse matrices.
Consider this code snippet. Although the sparse matrices appear to be equal,
z==y returns false (until I convert the matrices to dense matrices).
What is the problem with the equality test here?

Thanks,

  Joseph

=================

import scipy.sparse
import numpy

x = scipy.sparse.csc_matrix((500,3))
x[(10, 1)] = 1
x[(20, 2)] = 2
y = numpy.asarray([[1., 2], [3, 4], [2, 1]])

z = x.dot(y)
assert(z.shape == (500,2))

w = scipy.sparse.csc_matrix((500,2))
w[(10, 0)] = 3.
w[(20, 0)] = 4
w[(10, 1)] = 4
w[(20, 1)] = 2
assert(z.shape == w.shape)
assert(type(z) == type(w))
assert(z.dtype == w.dtype)
print z
print w
print "z:", z
print "w:", w
print "type(z) == type(w):", type(z) == type(w)
print "dtype(z) == dtype(w):", z.dtype == w.dtype

print cmp(z, w)
print "Sparse z and w are equal:", z == w
z = z.todense()
w = w.todense()
print "Dense z and w are equal:", (z == w).all() == True
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20080414/43d90e28/attachment.html>


More information about the SciPy-User mailing list