[SciPy-user] Sparse matrix operations

Travis Oliphant oliphant at ee.byu.edu
Fri Nov 5 11:11:14 CST 2004

```Nils Wagner wrote:

> Travis Oliphant wrote:
>
>> Nils Wagner wrote:
>>
>>> Hi all,
>>>
>>> How can I compute the Rayleigh quotient
>>>
>>> R = x' A x/(x' B x),
>>>
>>> where A and B are sparse matrices.
>>>
>>> How do I compute the matrix vector product of a sparse matrix with a
>>> vector ?
>>> Can I use dot(A,x) ?
>>
>>
>>
>> No, This won't work.
>>
>> dot(x,A*x ) should work
>>
>> -Travis
>>
> Now, assume that x is a rectangular (dense) matrix and A is a sparse
> matrix
>
>   ared = dot(transpose(x0),ma*x0)
>  File "/usr/lib/python2.3/site-packages/scipy/sparse/Sparse.py", line
> 179, in __mul__
>    res = csc * other
>  File "/usr/lib/python2.3/site-packages/scipy/sparse/Sparse.py", line
> 403, in __mul__
>    return self.matvec(other)
>  File "/usr/lib/python2.3/site-packages/scipy/sparse/Sparse.py", line
> 492, in matvec
>    raise ValueError, "Dimension mismatch"
> ValueError: Dimension mismatch
> >>> shape(x0)
> (715, 10)
> >>> shape(transpose(x0))
> (10, 715)
> >>> shape(ma)
> (715, 715)
>
> Any suggestion ?

sparse matrix * dense matrix is not yet supported.  You will have to
loop over the columns of the dense matrix.

-Travis

```