# Sparse matrix - matrix multiplication

How can a sparse matrix - matrix product be calculated? I know the 'classic' / mathematical way of doing it, but it seems pretty inefficient.

I thought about storing the first matrix in CSR form and the second one in CSC form, so since the row and column vectors are sorted I won't have to search for a specific row / column I need, but I guess that doesn't help much.

• Note that $\mathrm{CSR}\times\mathrm{CSC}$ is not generally a very good idea and actually $\mathrm{CSR}\times\mathrm{CRS}$ performs much better. As far as I know, there's a whole chapter in this book related to sparse matrix-matrix products. Apr 4, 2014 at 16:51

It is almost always inefficient to do a sparse-sparse matrix product $C=AB$. It is almost always more efficient to simply think of $C$ as an operator that you can apply to vectors, i.e., to compute $y=Cx$. Whenever such an operation is required, you compute it in two steps: $y=A(Bx)$, i.e., you simply do two matrix-vector products in a row to get the result. For this, it is not necessary to actually form the product matrix $AB$.