ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 23 Oct 2018 12:35:12 +0200Multiplying sparse matriceshttps://ask.sagemath.org/question/43979/multiplying-sparse-matrices/Can Sage multiply sparse matrices? If so, does it require some special syntax? I've been trying to run the following code:
M = Matrix(QQ, 1000001, 1000001, {(3,2): 27, (2,98): 71})
M2 = M*M
and it hasn't been working. If M is being converted to a dense matrix for multiplication then of course I wouldn't expect this to run. But it shouldn't be hard to do as sparse matrices, right?Thu, 18 Oct 2018 20:49:55 +0200https://ask.sagemath.org/question/43979/multiplying-sparse-matrices/Answer by nbruin for <p>Can Sage multiply sparse matrices? If so, does it require some special syntax? I've been trying to run the following code:</p>
<pre><code>M = Matrix(QQ, 1000001, 1000001, {(3,2): 27, (2,98): 71})
M2 = M*M
</code></pre>
<p>and it hasn't been working. If M is being converted to a dense matrix for multiplication then of course I wouldn't expect this to run. But it shouldn't be hard to do as sparse matrices, right?</p>
https://ask.sagemath.org/question/43979/multiplying-sparse-matrices/?answer=43980#post-id-43980I think the relevant code is here: https://github.com/sagemath/sage/blob/6187d261eca3c980e575b53d1a31f580ba8cfdfd/src/sage/matrix/matrix_rational_sparse.pyx#L161
The algorithm does take into account the sparse nature, but it does have a triple-nested for-loop, enumerating all the indices. It may be that for moderate sparsity this is faster than just enumerating the non-zero entries, but especially for your example it's pretty clear a much more efficient implementation is possible.Thu, 18 Oct 2018 21:13:44 +0200https://ask.sagemath.org/question/43979/multiplying-sparse-matrices/?answer=43980#post-id-43980Answer by tmonteil for <p>Can Sage multiply sparse matrices? If so, does it require some special syntax? I've been trying to run the following code:</p>
<pre><code>M = Matrix(QQ, 1000001, 1000001, {(3,2): 27, (2,98): 71})
M2 = M*M
</code></pre>
<p>and it hasn't been working. If M is being converted to a dense matrix for multiplication then of course I wouldn't expect this to run. But it shouldn't be hard to do as sparse matrices, right?</p>
https://ask.sagemath.org/question/43979/multiplying-sparse-matrices/?answer=44039#post-id-44039Thanks for reporting, this is now [trac ticket 26532](https://trac.sagemath.org/ticket/26532).Tue, 23 Oct 2018 12:35:12 +0200https://ask.sagemath.org/question/43979/multiplying-sparse-matrices/?answer=44039#post-id-44039