# [SciPy-user] Sparse with fast element-wise multiply?

David Warde-Farley dwf@cs.toronto....
Sun Dec 16 13:50:12 CST 2007

```On 16-Dec-07, at 2:03 PM, lorenzo bolla wrote:

> converting the matrix from lil_matrix to csc or csr format gives a
> 140x speed improvement on multiplication.
>
> In [4]: A = scipy.sparse.lil_matrix((1000, 1000))
>
> In [5]: A[:,100] = scipy.rand(1000)
>
> In [6]: A[99, :] = scipy.rand(1000)
>
> In [7]: A.setdiag(scipy.rand(1000))

Hi Lorenzo,

multiply; I know CSR/CSC is faster for that.

I'm looking for elementwise multiply, i.e. take two matrices of the
same size, multiply each element in matrix 1 with the corresponding
element in matrix 2, and put the result in the same position in the
result matrix (as with matlab's a .* b syntax). It seems lil_matrix is
the only sparse type that implements .multiply(), and converting to
lil_matrix is slow, so I've written my own function for multiplying
two coo_matrix's together. It's a LITTLE slower than .multiply() but
faster than converting both matrices to lil_matrix and then multiplying.

I'm not entirely sure how CSR and CSC work so it might be possible to
implement faster elementwise multiplication on top of them.

David

```