# [SciPy-user] multiplying sparse matrices in scipy-0.4.8?

afraser afraser at lanl.gov
Thu Mar 23 11:09:16 CST 2006

```I'm using numpy-0.9.6 and scipy-0.4.8.  When I multiply sparse
matrices, I often get the error: "ValueError: nzmax must not be less
than nnz".  The error happens when the matrices are only sort of

Here is a sample program and Traceback:
============================================================
import numpy, scipy, scipy.sparse, random

L = 30
frac = .3

random.seed(0) # make runs repeatable
A = scipy.sparse.csc_matrix((L,2))
#A = numpy.asmatrix(numpy.zeros((L,2),numpy.Float))

for i in xrange(L):
for j in xrange(2):
r = random.random()
if r < frac:
A[i,j] = r/frac
B = A*A.T
print B
============================================================
Traceback (most recent call last):
File "<stdin>", line 15, in ?
File "/usr/lib/python2.3/site-packages/scipy/sparse/sparse.py", line 658, in __mul__
return self.dot(other)
File "/usr/lib/python2.3/site-packages/scipy/sparse/sparse.py", line 306, in dot
result = self.matmat(other)
File "/usr/lib/python2.3/site-packages/scipy/sparse/sparse.py", line 824, in matmat
return csc_matrix((c, rowc, ptrc), dims=(M, N))
File "/usr/lib/python2.3/site-packages/scipy/sparse/sparse.py", line 558, in __init__
self._check()
File "/usr/lib/python2.3/site-packages/scipy/sparse/sparse.py", line 574, in _check
raise ValueError, "nzmax must not be less than nnz"
ValueError: nzmax must not be less than nnz
============================================================

Andy

```