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.Sun, 11 Mar 2018 10:52:56 +0100Cant find matrix kernelhttps://ask.sagemath.org/question/41456/cant-find-matrix-kernel/ I have a symbolic matrix at the start and I use the determinant to find roots, ok. But when I input some solution of the determinant equation in the matrix again, sage is not able to find the kernel (null space).
Here's my atempt:
A=matrix([[x^2,-1.75*x,0],[-1.75*x,x^2,2.07*i*x^2+4.65*i],[0,2.07*i*x^2+-4.65*i,x^2]])
sol=solve(A.det(),x,solution_dict=True)
A.subs(x=sol[0][x]).right_kernel_matrix()
My output is simple:
[]Fri, 09 Mar 2018 21:33:07 +0100https://ask.sagemath.org/question/41456/cant-find-matrix-kernel/Comment by vdelecroix for <p>I have a symbolic matrix at the start and I use the determinant to find roots, ok. But when I input some solution of the determinant equation in the matrix again, sage is not able to find the kernel (null space).
Here's my atempt:</p>
<pre><code>A=matrix([[x^2,-1.75*x,0],[-1.75*x,x^2,2.07*i*x^2+4.65*i],[0,2.07*i*x^2+-4.65*i,x^2]])
sol=solve(A.det(),x,solution_dict=True)
A.subs(x=sol[0][x]).right_kernel_matrix()
</code></pre>
<p>My output is simple:</p>
<pre><code>[]
</code></pre>
https://ask.sagemath.org/question/41456/cant-find-matrix-kernel/?comment=41464#post-id-41464I believe that the function `solve` is wrong by giving you exact symbolic expressions for the solutions
sage: A=matrix([[x^2,-1.75*x,0],[-1.75*x,x^2,2.07*i*x^2+4.65*i],[0,2.07*i*x^2+-4.65*i,x^2]])
sage: solve(A.det(),x)
[x == -sqrt(5/105698*sqrt(1865879629) + 30625/105698),
x == sqrt(5/105698*sqrt(1865879629) + 30625/105698),
x == -sqrt(-5/105698*sqrt(1865879629) + 30625/105698),
x == sqrt(-5/105698*sqrt(1865879629) + 30625/105698), x == 0]
And this is part of the confusion here (it make no sense to substitute these "exact solutions" in your floating point matrix). I opened the [trac ticket #24939](https://trac.sagemath.org/ticket/24939) for this issue.Sat, 10 Mar 2018 10:59:35 +0100https://ask.sagemath.org/question/41456/cant-find-matrix-kernel/?comment=41464#post-id-41464Answer by vdelecroix for <p>I have a symbolic matrix at the start and I use the determinant to find roots, ok. But when I input some solution of the determinant equation in the matrix again, sage is not able to find the kernel (null space).
Here's my atempt:</p>
<pre><code>A=matrix([[x^2,-1.75*x,0],[-1.75*x,x^2,2.07*i*x^2+4.65*i],[0,2.07*i*x^2+-4.65*i,x^2]])
sol=solve(A.det(),x,solution_dict=True)
A.subs(x=sol[0][x]).right_kernel_matrix()
</code></pre>
<p>My output is simple:</p>
<pre><code>[]
</code></pre>
https://ask.sagemath.org/question/41456/cant-find-matrix-kernel/?answer=41463#post-id-41463If you are mixing exact values with unexact ones it is unlikely that you get your answer. You have two solutions:
- use floating point numbers everywhere. In this situation, you have to be careful by what you call a kernel since arithmetic operations are not exact (eg look below that the determinant is not exactly zero)
- replace your floating point numbers `1.75`, `2.07`, etc by rational values `7/4`, `207/100`
To illustrate the first option using scipy (from the method at http://scipy-cookbook.readthedocs.io/items/RankNullspace.html)
sage: As = A.subs(x=sol[0][x]).change_ring(CDF)
sage: As
[ 2.333101328217149 2.673036254461398 0.0]
[ 2.673036254461398 2.333101328217149 9.479519749409498*I]
[ 0.0 0.179519749409498*I 2.333101328217149]
sage: As.det() # small but not zero
-3.1648558730042146e-14
sage: import scipy.linalg as linalg
sage: u, s, vh = linalg.svd(As.numpy()) # computing singular values decomposition
sage: s
array([ 1.04215568e+01, 3.45036344e+00, 8.81146174e-16])
From above you see that the small singular value is the last one (that is correspond to the "approximate kernel"). Hence to get the corresponding vector into Sage you can just do
sage: k = vector(vh[2])
sage: k
(-0.7524244881834203, 0.6567372851130923, -0.050532444272992305*I)
And you can check that it is in the kernel
sage: As * k
(6.661338147750939e-16, 1.27675647831893e-15, -1.0547118733938987e-15*I)
Sat, 10 Mar 2018 10:53:05 +0100https://ask.sagemath.org/question/41456/cant-find-matrix-kernel/?answer=41463#post-id-41463Comment by ianncunha for <p>If you are mixing exact values with unexact ones it is unlikely that you get your answer. You have two solutions:</p>
<ul>
<li>use floating point numbers everywhere. In this situation, you have to be careful by what you call a kernel since arithmetic operations are not exact (eg look below that the determinant is not exactly zero)</li>
<li>replace your floating point numbers <code>1.75</code>, <code>2.07</code>, etc by rational values <code>7/4</code>, <code>207/100</code></li>
</ul>
<p>To illustrate the first option using scipy (from the method at <a href="http://scipy-cookbook.readthedocs.io/items/RankNullspace.html">http://scipy-cookbook.readthedocs.io/...</a>)</p>
<pre><code>sage: As = A.subs(x=sol[0][x]).change_ring(CDF)
sage: As
[ 2.333101328217149 2.673036254461398 0.0]
[ 2.673036254461398 2.333101328217149 9.479519749409498*I]
[ 0.0 0.179519749409498*I 2.333101328217149]
sage: As.det() # small but not zero
-3.1648558730042146e-14
sage: import scipy.linalg as linalg
sage: u, s, vh = linalg.svd(As.numpy()) # computing singular values decomposition
sage: s
array([ 1.04215568e+01, 3.45036344e+00, 8.81146174e-16])
</code></pre>
<p>From above you see that the small singular value is the last one (that is correspond to the "approximate kernel"). Hence to get the corresponding vector into Sage you can just do</p>
<pre><code>sage: k = vector(vh[2])
sage: k
(-0.7524244881834203, 0.6567372851130923, -0.050532444272992305*I)
</code></pre>
<p>And you can check that it is in the kernel</p>
<pre><code>sage: As * k
(6.661338147750939e-16, 1.27675647831893e-15, -1.0547118733938987e-15*I)
</code></pre>
https://ask.sagemath.org/question/41456/cant-find-matrix-kernel/?comment=41474#post-id-41474Nice! Thak you!Sat, 10 Mar 2018 19:06:19 +0100https://ask.sagemath.org/question/41456/cant-find-matrix-kernel/?comment=41474#post-id-41474Comment by slelievre for <p>If you are mixing exact values with unexact ones it is unlikely that you get your answer. You have two solutions:</p>
<ul>
<li>use floating point numbers everywhere. In this situation, you have to be careful by what you call a kernel since arithmetic operations are not exact (eg look below that the determinant is not exactly zero)</li>
<li>replace your floating point numbers <code>1.75</code>, <code>2.07</code>, etc by rational values <code>7/4</code>, <code>207/100</code></li>
</ul>
<p>To illustrate the first option using scipy (from the method at <a href="http://scipy-cookbook.readthedocs.io/items/RankNullspace.html">http://scipy-cookbook.readthedocs.io/...</a>)</p>
<pre><code>sage: As = A.subs(x=sol[0][x]).change_ring(CDF)
sage: As
[ 2.333101328217149 2.673036254461398 0.0]
[ 2.673036254461398 2.333101328217149 9.479519749409498*I]
[ 0.0 0.179519749409498*I 2.333101328217149]
sage: As.det() # small but not zero
-3.1648558730042146e-14
sage: import scipy.linalg as linalg
sage: u, s, vh = linalg.svd(As.numpy()) # computing singular values decomposition
sage: s
array([ 1.04215568e+01, 3.45036344e+00, 8.81146174e-16])
</code></pre>
<p>From above you see that the small singular value is the last one (that is correspond to the "approximate kernel"). Hence to get the corresponding vector into Sage you can just do</p>
<pre><code>sage: k = vector(vh[2])
sage: k
(-0.7524244881834203, 0.6567372851130923, -0.050532444272992305*I)
</code></pre>
<p>And you can check that it is in the kernel</p>
<pre><code>sage: As * k
(6.661338147750939e-16, 1.27675647831893e-15, -1.0547118733938987e-15*I)
</code></pre>
https://ask.sagemath.org/question/41456/cant-find-matrix-kernel/?comment=41499#post-id-41499If this solves your question, you can mark this answer as accepted by clicking the "tick" sign at the top left of the answer. This way your question will appear as solved in the list of questions.Sun, 11 Mar 2018 10:52:56 +0100https://ask.sagemath.org/question/41456/cant-find-matrix-kernel/?comment=41499#post-id-41499