[SciPy-User] Help: Multiplying two sparse matrices

Pranava Swaroop Madhyastha pranava@lsi.upc....
Tue Dec 25 12:34:11 CST 2012


I have a problem multiplying two sparse matrices. Here is a sample:

import scipy.sparse as ss

D = ss.rand(49837, 1000, format='csr', density=0.1)

E = ss.rand(1000, 49837, format='csr', density=0.1)

D * E

This is the error that I get:

ValueError                                Traceback (most recent call last)
<ipython-input-4-c94d439b5bf5> in <module>()
----> 1 D * E

/usr/local/lib/python2.7/dist-packages/scipy/sparse/base.pyc in
__mul__(self, other)
    254             if self.shape[1] != other.shape[0]:
    255                 raise ValueError('dimension mismatch')
--> 256             return self._mul_sparse_matrix(other)
    258         try:

/usr/local/lib/python2.7/dist-packages/scipy/sparse/compressed.py in
_mul_sparse_matrix(self, other)
    297         nnz = indptr[-1]
--> 298         indices = np.empty(nnz, dtype=np.intc)
    299         data    = np.empty(nnz, dtype=upcast(self.dtype,other.dtype))

ValueError: negative dimensions are not allowed

Apparently, the error seems to be because of dtype=np.intc. Is there a
work around for this?

Thanks and Regards,

More information about the SciPy-User mailing list