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

Ed Schofield schofield at ftw.at
Sun Mar 26 11:47:12 CST 2006

```On 23/03/2006, at 6:09 PM, afraser wrote:

> 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
> sparse.  Any advice appreciated.
>
> 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
> ============================================================

Yes, this is a bug.

Travis, could you please take a look at this?  The FORTRAN functions
dcscmucsc and dcscmucsr both seem to be returning incorrect values in
indptr.  In fact, could you please explain the Python code in matmat
() that calls these functions?  I'd like to understand what the
"while 1" loop is for, and in particular why we have
c, rowc, ptrc, irow, kcol, ierr = func(*args)
when c, rowc, etc are part of *args anyway.

-- Ed

```