Computational Science Stack Exchange is a question and answer site for scientists using computers to solve scientific problems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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
up vote 3 down vote accepted

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 and search for the mmult() function.

share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.