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

Nathan Bell wnbell@gmail....
Mon Dec 17 22:41:32 CST 2007

```David Warde-Farley <dwf <at> cs.toronto.edu> writes:

> Thanks for your reply. Actually, though, I'm not looking for matrix-
> 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
>

Currently elementwise multiplication is exposed through A**B where A and B are
csr_matrix or csc_matrix objects.  You can expect similar performance to A+B.

I don't know why ** was chosen, it was that way before I started working on
scipy.sparse.  I've added a .multiply() method to the sparse matrix base class
that goes through csr_matrix:
http://projects.scipy.org/scipy/scipy/changeset/3682

```