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, 19 Apr 2016 14:55:52 +0200Bug in matrix multiplication and inversion?https://ask.sagemath.org/question/33133/bug-in-matrix-multiplication-and-inversion/I'm using sage 7.1 and this code here returns `false`.
M = matrix ([[sqrt(1-1/2),1/2],[1,-sqrt(1-1/2)]])
N = matrix ([[sqrt(1-1/3),1],[1/3,-sqrt(1-1/3)]])
(M*N).inverse() == N.inverse()*M.inverse()
What is going on?
Mon, 18 Apr 2016 16:06:07 +0200https://ask.sagemath.org/question/33133/bug-in-matrix-multiplication-and-inversion/Answer by tmonteil for <p>I'm using sage 7.1 and this code here returns <code>false</code>.</p>
<pre><code>M = matrix ([[sqrt(1-1/2),1/2],[1,-sqrt(1-1/2)]])
N = matrix ([[sqrt(1-1/3),1],[1/3,-sqrt(1-1/3)]])
(M*N).inverse() == N.inverse()*M.inverse()
</code></pre>
<p>What is going on?</p>
https://ask.sagemath.org/question/33133/bug-in-matrix-multiplication-and-inversion/?answer=33134#post-id-33134If `E` denotes the difference;
sage: E = (M*N).inverse() - N.inverse()*M.inverse()
sage: E
[-sqrt(2/3)*sqrt(1/2) + 6/(6*sqrt(2/3)*sqrt(1/2) + 1) - 6*(3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/((6*sqrt(2/3)*sqrt(1/2) + 1)^2*(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1)) - 1 -1/2*sqrt(2/3) + sqrt(1/2) + 3*(sqrt(2/3) - 2*sqrt(1/2))/((6*sqrt(2/3)*sqrt(1/2) + 1)*(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1))]
[ sqrt(2/3) - 1/3*sqrt(1/2) - 2*(3*sqrt(2/3) - sqrt(1/2))/((6*sqrt(2/3)*sqrt(1/2) + 1)*(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1)) -sqrt(2/3)*sqrt(1/2) + 1/(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1) - 1/6]
You get a matrix whose entries are symbolic expressions. If we write:
sage: E == 0
False
We get false because the entries of `E` are not all zero, as symbolic expressions, but they are actually zero if we simplify all entries one by one:
sage: E[0][0]
-sqrt(2/3)*sqrt(1/2) + 6/(6*sqrt(2/3)*sqrt(1/2) + 1) - 6*(3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/((6*sqrt(2/3)*sqrt(1/2) + 1)^2*(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1)) - 1
sage: E[0][0].full_simplify()
0
sage: E[0][1].full_simplify()
0
sage: E[1][1].full_simplify()
0
sage: E[1][0].full_simplify()
0
Note that checking whether a symbolic expression is equal to zero is undecidable in general (though we should admit that in the present case, this is just because symbolics is weak in Sage).
Since your symbolic expressions represent algebraic numbers, you can change the ring over which `E` is defined to be the field of algebraic numbers. In this field, the equalty is decidable, and Sage knows how to handle it well:
sage: E.change_ring(QQbar) == 0
True
Mon, 18 Apr 2016 17:16:10 +0200https://ask.sagemath.org/question/33133/bug-in-matrix-multiplication-and-inversion/?answer=33134#post-id-33134Comment by B r u n o for <p>If <code>E</code> denotes the difference;</p>
<pre><code>sage: E = (M*N).inverse() - N.inverse()*M.inverse()
sage: E
[-sqrt(2/3)*sqrt(1/2) + 6/(6*sqrt(2/3)*sqrt(1/2) + 1) - 6*(3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/((6*sqrt(2/3)*sqrt(1/2) + 1)^2*(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1)) - 1 -1/2*sqrt(2/3) + sqrt(1/2) + 3*(sqrt(2/3) - 2*sqrt(1/2))/((6*sqrt(2/3)*sqrt(1/2) + 1)*(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1))]
[ sqrt(2/3) - 1/3*sqrt(1/2) - 2*(3*sqrt(2/3) - sqrt(1/2))/((6*sqrt(2/3)*sqrt(1/2) + 1)*(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1)) -sqrt(2/3)*sqrt(1/2) + 1/(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1) - 1/6]
</code></pre>
<p>You get a matrix whose entries are symbolic expressions. If we write:</p>
<pre><code>sage: E == 0
False
</code></pre>
<p>We get false because the entries of <code>E</code> are not all zero, as symbolic expressions, but they are actually zero if we simplify all entries one by one:</p>
<pre><code>sage: E[0][0]
-sqrt(2/3)*sqrt(1/2) + 6/(6*sqrt(2/3)*sqrt(1/2) + 1) - 6*(3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/((6*sqrt(2/3)*sqrt(1/2) + 1)^2*(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1)) - 1
sage: E[0][0].full_simplify()
0
sage: E[0][1].full_simplify()
0
sage: E[1][1].full_simplify()
0
sage: E[1][0].full_simplify()
0
</code></pre>
<p>Note that checking whether a symbolic expression is equal to zero is undecidable in general (though we should admit that in the present case, this is just because symbolics is weak in Sage).
Since your symbolic expressions represent algebraic numbers, you can change the ring over which <code>E</code> is defined to be the field of algebraic numbers. In this field, the equalty is decidable, and Sage knows how to handle it well:</p>
<pre><code>sage: E.change_ring(QQbar) == 0
True
</code></pre>
https://ask.sagemath.org/question/33133/bug-in-matrix-multiplication-and-inversion/?comment=33135#post-id-33135Not that one can also simplify the matrix `E` directly:
sage: E.simplify_full()
[0 0]
[0 0]
In terms of weakness of symbolics in Sage, is there a reason for not trying to apply `simplify_full` when one tests an expression for zero? (Note that I am very much illiterate regarding Sage's symbolics!)Mon, 18 Apr 2016 18:17:27 +0200https://ask.sagemath.org/question/33133/bug-in-matrix-multiplication-and-inversion/?comment=33135#post-id-33135Comment by tmonteil for <p>If <code>E</code> denotes the difference;</p>
<pre><code>sage: E = (M*N).inverse() - N.inverse()*M.inverse()
sage: E
[-sqrt(2/3)*sqrt(1/2) + 6/(6*sqrt(2/3)*sqrt(1/2) + 1) - 6*(3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/((6*sqrt(2/3)*sqrt(1/2) + 1)^2*(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1)) - 1 -1/2*sqrt(2/3) + sqrt(1/2) + 3*(sqrt(2/3) - 2*sqrt(1/2))/((6*sqrt(2/3)*sqrt(1/2) + 1)*(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1))]
[ sqrt(2/3) - 1/3*sqrt(1/2) - 2*(3*sqrt(2/3) - sqrt(1/2))/((6*sqrt(2/3)*sqrt(1/2) + 1)*(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1)) -sqrt(2/3)*sqrt(1/2) + 1/(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1) - 1/6]
</code></pre>
<p>You get a matrix whose entries are symbolic expressions. If we write:</p>
<pre><code>sage: E == 0
False
</code></pre>
<p>We get false because the entries of <code>E</code> are not all zero, as symbolic expressions, but they are actually zero if we simplify all entries one by one:</p>
<pre><code>sage: E[0][0]
-sqrt(2/3)*sqrt(1/2) + 6/(6*sqrt(2/3)*sqrt(1/2) + 1) - 6*(3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/((6*sqrt(2/3)*sqrt(1/2) + 1)^2*(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1)) - 1
sage: E[0][0].full_simplify()
0
sage: E[0][1].full_simplify()
0
sage: E[1][1].full_simplify()
0
sage: E[1][0].full_simplify()
0
</code></pre>
<p>Note that checking whether a symbolic expression is equal to zero is undecidable in general (though we should admit that in the present case, this is just because symbolics is weak in Sage).
Since your symbolic expressions represent algebraic numbers, you can change the ring over which <code>E</code> is defined to be the field of algebraic numbers. In this field, the equalty is decidable, and Sage knows how to handle it well:</p>
<pre><code>sage: E.change_ring(QQbar) == 0
True
</code></pre>
https://ask.sagemath.org/question/33133/bug-in-matrix-multiplication-and-inversion/?comment=33137#post-id-33137Damn, i was looking for `full_simplify()` but tab completion did not gave anything !Mon, 18 Apr 2016 21:41:53 +0200https://ask.sagemath.org/question/33133/bug-in-matrix-multiplication-and-inversion/?comment=33137#post-id-33137Comment by Thomas for <p>If <code>E</code> denotes the difference;</p>
<pre><code>sage: E = (M*N).inverse() - N.inverse()*M.inverse()
sage: E
[-sqrt(2/3)*sqrt(1/2) + 6/(6*sqrt(2/3)*sqrt(1/2) + 1) - 6*(3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/((6*sqrt(2/3)*sqrt(1/2) + 1)^2*(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1)) - 1 -1/2*sqrt(2/3) + sqrt(1/2) + 3*(sqrt(2/3) - 2*sqrt(1/2))/((6*sqrt(2/3)*sqrt(1/2) + 1)*(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1))]
[ sqrt(2/3) - 1/3*sqrt(1/2) - 2*(3*sqrt(2/3) - sqrt(1/2))/((6*sqrt(2/3)*sqrt(1/2) + 1)*(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1)) -sqrt(2/3)*sqrt(1/2) + 1/(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1) - 1/6]
</code></pre>
<p>You get a matrix whose entries are symbolic expressions. If we write:</p>
<pre><code>sage: E == 0
False
</code></pre>
<p>We get false because the entries of <code>E</code> are not all zero, as symbolic expressions, but they are actually zero if we simplify all entries one by one:</p>
<pre><code>sage: E[0][0]
-sqrt(2/3)*sqrt(1/2) + 6/(6*sqrt(2/3)*sqrt(1/2) + 1) - 6*(3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/((6*sqrt(2/3)*sqrt(1/2) + 1)^2*(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1)) - 1
sage: E[0][0].full_simplify()
0
sage: E[0][1].full_simplify()
0
sage: E[1][1].full_simplify()
0
sage: E[1][0].full_simplify()
0
</code></pre>
<p>Note that checking whether a symbolic expression is equal to zero is undecidable in general (though we should admit that in the present case, this is just because symbolics is weak in Sage).
Since your symbolic expressions represent algebraic numbers, you can change the ring over which <code>E</code> is defined to be the field of algebraic numbers. In this field, the equalty is decidable, and Sage knows how to handle it well:</p>
<pre><code>sage: E.change_ring(QQbar) == 0
True
</code></pre>
https://ask.sagemath.org/question/33133/bug-in-matrix-multiplication-and-inversion/?comment=33142#post-id-33142OK thanks. I did actually try `simplify` on the difference, but not `simplify_full()`Tue, 19 Apr 2016 12:57:15 +0200https://ask.sagemath.org/question/33133/bug-in-matrix-multiplication-and-inversion/?comment=33142#post-id-33142Comment by Thomas for <p>If <code>E</code> denotes the difference;</p>
<pre><code>sage: E = (M*N).inverse() - N.inverse()*M.inverse()
sage: E
[-sqrt(2/3)*sqrt(1/2) + 6/(6*sqrt(2/3)*sqrt(1/2) + 1) - 6*(3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/((6*sqrt(2/3)*sqrt(1/2) + 1)^2*(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1)) - 1 -1/2*sqrt(2/3) + sqrt(1/2) + 3*(sqrt(2/3) - 2*sqrt(1/2))/((6*sqrt(2/3)*sqrt(1/2) + 1)*(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1))]
[ sqrt(2/3) - 1/3*sqrt(1/2) - 2*(3*sqrt(2/3) - sqrt(1/2))/((6*sqrt(2/3)*sqrt(1/2) + 1)*(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1)) -sqrt(2/3)*sqrt(1/2) + 1/(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1) - 1/6]
</code></pre>
<p>You get a matrix whose entries are symbolic expressions. If we write:</p>
<pre><code>sage: E == 0
False
</code></pre>
<p>We get false because the entries of <code>E</code> are not all zero, as symbolic expressions, but they are actually zero if we simplify all entries one by one:</p>
<pre><code>sage: E[0][0]
-sqrt(2/3)*sqrt(1/2) + 6/(6*sqrt(2/3)*sqrt(1/2) + 1) - 6*(3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/((6*sqrt(2/3)*sqrt(1/2) + 1)^2*(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1)) - 1
sage: E[0][0].full_simplify()
0
sage: E[0][1].full_simplify()
0
sage: E[1][1].full_simplify()
0
sage: E[1][0].full_simplify()
0
</code></pre>
<p>Note that checking whether a symbolic expression is equal to zero is undecidable in general (though we should admit that in the present case, this is just because symbolics is weak in Sage).
Since your symbolic expressions represent algebraic numbers, you can change the ring over which <code>E</code> is defined to be the field of algebraic numbers. In this field, the equalty is decidable, and Sage knows how to handle it well:</p>
<pre><code>sage: E.change_ring(QQbar) == 0
True
</code></pre>
https://ask.sagemath.org/question/33133/bug-in-matrix-multiplication-and-inversion/?comment=33143#post-id-33143... and by the way, Mathematica just does it.Tue, 19 Apr 2016 12:57:51 +0200https://ask.sagemath.org/question/33133/bug-in-matrix-multiplication-and-inversion/?comment=33143#post-id-33143Comment by B r u n o for <p>If <code>E</code> denotes the difference;</p>
<pre><code>sage: E = (M*N).inverse() - N.inverse()*M.inverse()
sage: E
[-sqrt(2/3)*sqrt(1/2) + 6/(6*sqrt(2/3)*sqrt(1/2) + 1) - 6*(3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/((6*sqrt(2/3)*sqrt(1/2) + 1)^2*(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1)) - 1 -1/2*sqrt(2/3) + sqrt(1/2) + 3*(sqrt(2/3) - 2*sqrt(1/2))/((6*sqrt(2/3)*sqrt(1/2) + 1)*(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1))]
[ sqrt(2/3) - 1/3*sqrt(1/2) - 2*(3*sqrt(2/3) - sqrt(1/2))/((6*sqrt(2/3)*sqrt(1/2) + 1)*(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1)) -sqrt(2/3)*sqrt(1/2) + 1/(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1) - 1/6]
</code></pre>
<p>You get a matrix whose entries are symbolic expressions. If we write:</p>
<pre><code>sage: E == 0
False
</code></pre>
<p>We get false because the entries of <code>E</code> are not all zero, as symbolic expressions, but they are actually zero if we simplify all entries one by one:</p>
<pre><code>sage: E[0][0]
-sqrt(2/3)*sqrt(1/2) + 6/(6*sqrt(2/3)*sqrt(1/2) + 1) - 6*(3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/((6*sqrt(2/3)*sqrt(1/2) + 1)^2*(sqrt(2/3)*sqrt(1/2) + (3*sqrt(2/3) - sqrt(1/2))*(sqrt(2/3) - 2*sqrt(1/2))/(6*sqrt(2/3)*sqrt(1/2) + 1) + 1)) - 1
sage: E[0][0].full_simplify()
0
sage: E[0][1].full_simplify()
0
sage: E[1][1].full_simplify()
0
sage: E[1][0].full_simplify()
0
</code></pre>
<p>Note that checking whether a symbolic expression is equal to zero is undecidable in general (though we should admit that in the present case, this is just because symbolics is weak in Sage).
Since your symbolic expressions represent algebraic numbers, you can change the ring over which <code>E</code> is defined to be the field of algebraic numbers. In this field, the equalty is decidable, and Sage knows how to handle it well:</p>
<pre><code>sage: E.change_ring(QQbar) == 0
True
</code></pre>
https://ask.sagemath.org/question/33133/bug-in-matrix-multiplication-and-inversion/?comment=33145#post-id-33145As a general rule, when you are doing purely algebraic computation, an advice is to tell Sage that you're doing algebra, and not deal with symbolics. Here, you can do:
sage: M = matrix (QQbar,[[sqrt(1-1/2),1/2],[1,-sqrt(1-1/2)]])
sage: N = matrix (QQbar,[[sqrt(1-1/3),1],[1/3,-sqrt(1-1/3)]])
sage: (M*N).inverse() == N.inverse()*M.inverse()
True
And another solution is not to work on `QQbar` but staying in the symbolic world and using the much more powerful method `is_zero`:
sage: M = matrix ([[sqrt(1-1/2),1/2],[1,-sqrt(1-1/2)]])
sage: N = matrix ([[sqrt(1-1/3),1],[1/3,-sqrt(1-1/3)]])
sage: ((M*N).inverse() - N.inverse()*M.inverse()).is_zero()
TrueTue, 19 Apr 2016 14:55:52 +0200https://ask.sagemath.org/question/33133/bug-in-matrix-multiplication-and-inversion/?comment=33145#post-id-33145