ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 26 Aug 2019 01:41:33 -0500Inverse of real sparse matrixhttp://ask.sagemath.org/question/47587/inverse-of-real-sparse-matrix/Dear sagemath community,
I'm a bit surprised in the result that I obtain when I compute the inverse of a real, sparse matrix. The problem occurs in Sagemath 8.7, (on windows).
In my case the matrix is
B=matrix(RR, [[1/6, -1/24, -1/30, 1/120,1/12, 0, 0, 0, 0],
[-1/24,1/60,1/60, 1/420, -1/24, 0, 0, 0, 0],
[-1/30,1/60, 2/105, 1/140, -1/20, 0, 0, 0, 0],
[1/120, 1/420, 1/140, 13/1260, -1/40, 0, 0, 0, 0],
[1/12, -1/24, -1/20, -1/40, 1/3, -1/24, -1/30, 1/120,1/12],
[0, 0, 0, 0, -1/24,1/60,1/60, 1/420, -1/24],
[0, 0, 0, 0, -1/30,1/60, 2/105, 1/140, -1/20],
[0, 0, 0, 0, 1/120, 1/420, 1/140, 13/1260, -1/40],
[0, 0, 0, 0,1/12, -1/24, -1/20, -1/40, 1/6]],sparse=true)
(B.inverse()*B).norm(1)
The result is 138.4999999999923, which is very far from the expected value: 1. If one replaces sparse=true by sparse=false, the result becomes reasonable.The problem also does not occur when I replace RR by QQ.
I suspect that this will not happen in all versions of Sagemath, since I stumbled upon this strange behaviour when I was executing a code that used to work fine years ago. Sadly I do not recall which version I used to have back then. I wanted to ask: what is currently the right way to compute the inverse of a sparse real matrix?
cheers,
IVSun, 25 Aug 2019 13:39:38 -0500http://ask.sagemath.org/question/47587/inverse-of-real-sparse-matrix/Answer by tmonteil for <p>Dear sagemath community,</p>
<p>I'm a bit surprised in the result that I obtain when I compute the inverse of a real, sparse matrix. The problem occurs in Sagemath 8.7, (on windows). </p>
<p>In my case the matrix is </p>
<pre><code>B=matrix(RR, [[1/6, -1/24, -1/30, 1/120,1/12, 0, 0, 0, 0],
[-1/24,1/60,1/60, 1/420, -1/24, 0, 0, 0, 0],
[-1/30,1/60, 2/105, 1/140, -1/20, 0, 0, 0, 0],
[1/120, 1/420, 1/140, 13/1260, -1/40, 0, 0, 0, 0],
[1/12, -1/24, -1/20, -1/40, 1/3, -1/24, -1/30, 1/120,1/12],
[0, 0, 0, 0, -1/24,1/60,1/60, 1/420, -1/24],
[0, 0, 0, 0, -1/30,1/60, 2/105, 1/140, -1/20],
[0, 0, 0, 0, 1/120, 1/420, 1/140, 13/1260, -1/40],
[0, 0, 0, 0,1/12, -1/24, -1/20, -1/40, 1/6]],sparse=true)
(B.inverse()*B).norm(1)
</code></pre>
<p>The result is 138.4999999999923, which is very far from the expected value: 1. If one replaces sparse=true by sparse=false, the result becomes reasonable.The problem also does not occur when I replace RR by QQ. </p>
<p>I suspect that this will not happen in all versions of Sagemath, since I stumbled upon this strange behaviour when I was executing a code that used to work fine years ago. Sadly I do not recall which version I used to have back then. I wanted to ask: what is currently the right way to compute the inverse of a sparse real matrix?</p>
<p>cheers,</p>
<p>IV</p>
http://ask.sagemath.org/question/47587/inverse-of-real-sparse-matrix/?answer=47589#post-id-47589We have to inspect further for the reasons of that behaviour. It might "only" be the naiveness of the algorithm, since the matrix `B` is ill-conditionned: its inverse has a huge norm.
I bet there is something wrong in the formulas, since when i do the computation on certified fields such as `RBF` and `RIF`, the correct answer does not belong to the possible values provided by Sage.
Meanwhile, you can always turn your matrix into a dense one by doing:
sage: B.dense_matrix()
Thanks for reporting anyway.
**EDIT** The culprit seems to be [trac ticket 24122](https://trac.sagemath.org/ticket/24122) if you revert it, you get something close to 1 again.
This bug is tracked at [trac ticket 28402](https://trac.sagemath.org/ticket/28402)Sun, 25 Aug 2019 16:04:19 -0500http://ask.sagemath.org/question/47587/inverse-of-real-sparse-matrix/?answer=47589#post-id-47589Comment by Emmanuel Charpentier for <p>We have to inspect further for the reasons of that behaviour. It might "only" be the naiveness of the algorithm, since the matrix <code>B</code> is ill-conditionned: its inverse has a huge norm.</p>
<p>I bet there is something wrong in the formulas, since when i do the computation on certified fields such as <code>RBF</code> and <code>RIF</code>, the correct answer does not belong to the possible values provided by Sage.</p>
<p>Meanwhile, you can always turn your matrix into a dense one by doing:</p>
<pre><code>sage: B.dense_matrix()
</code></pre>
<p>Thanks for reporting anyway.</p>
<p><strong>EDIT</strong> The culprit seems to be <a href="https://trac.sagemath.org/ticket/24122">trac ticket 24122</a> if you revert it, you get something close to 1 again.</p>
<p>This bug is tracked at <a href="https://trac.sagemath.org/ticket/28402">trac ticket 28402</a></p>
http://ask.sagemath.org/question/47587/inverse-of-real-sparse-matrix/?comment=47592#post-id-47592Can't reproduce:
sage: sage.version.version
'8.9.beta7'
sage: B=matrix(RR, [[1/6, -1/24, -1/30, 1/120,1/12, 0, 0, 0, 0], [-1/24,1/60,1/60
....: , 1/420, -1/24, 0, 0, 0, 0], [-1/30,1/60, 2/105, 1/140, -1/20, 0, 0, 0, 0],
....: [1/120, 1/420, 1/140, 13/1260, -1/40, 0, 0, 0, 0], [1/12, -1/24, -1/20, -1
....: /40, 1/3, -1/24, -1/30, 1/120,1/12], [0, 0, 0, 0, -1/24,1/60,1/60, 1/420, -
....: 1/24], [0, 0, 0, 0, -1/30,1/60, 2/105, 1/140, -1/20], [0, 0, 0, 0, 1/120, 1
....: /420, 1/140, 13/1260, -1/40], [0, 0, 0, 0,1/12, -1/24, -1/20, -1/40, 1/6]],
....: sparse=true)
sage: (B.inverse()*B).norm(1)
1.0000000000019988
And ticket [28402](https://trac.sagemath.org/ticket/28402) isn't yet merged.Mon, 26 Aug 2019 01:41:33 -0500http://ask.sagemath.org/question/47587/inverse-of-real-sparse-matrix/?comment=47592#post-id-47592