# 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. – Algebraic Pavel Apr 4 '14 at 16:51

## 1 Answer

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$.

If you absolutely must form the product, take a look at https://svn.dealii.org/trunk/deal.II/include/deal.II/lac/sparse_matrix.templates.h and search for the mmult() function.

• Downvoting because of the last paragraph: simply saying "look at this code" does not seem very helpful for the reader. – Federico Poloni Mar 29 '14 at 23:40