3
$\begingroup$

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.

$\endgroup$
1
  • 4
    $\begingroup$ 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. $\endgroup$ Commented Apr 4, 2014 at 16:51

1 Answer 1

5
$\begingroup$

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.

$\endgroup$
1
  • 2
    $\begingroup$ Downvoting because of the last paragraph: simply saying "look at this code" does not seem very helpful for the reader. $\endgroup$ Commented Mar 29, 2014 at 23:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.