ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 26 Nov 2013 06:17:44 -0600Using sage to check if a vector is in a matrix's null spacehttps://ask.sagemath.org/question/10781/using-sage-to-check-if-a-vector-is-in-a-matrixs-null-space/I want to check if a given vector is inside a matrix's null space.
Here is what I have done:
#Define a matrix
A=Matrix([[1,-1,2,-1],[2,1,-2,-2],[-1,2,-4,1],[3,0,0,-3]],ring=QQ)
#Free variables to be used in the given vector
var('s t')
#The null space of the matrix A
ns=A.kernel()
s=2
t=1
#Create a vector
ans=s*vector([1,0,0,1])+t*vector([0,2,1,0])
#If ans is in the null space of A, then A*ans is the 0 vector
print A*ans
#If A*ans is the 0 vector, then the following should be true too
print ans in ns
I had expected the last print statement to be true since A*ans results in a zero vector, hence ans is in the null space of A.
Why isn't this the case?
Tue, 26 Nov 2013 06:04:34 -0600https://ask.sagemath.org/question/10781/using-sage-to-check-if-a-vector-is-in-a-matrixs-null-space/Answer by John Palmieri for <p>I want to check if a given vector is inside a matrix's null space.</p>
<p>Here is what I have done:</p>
<pre><code>#Define a matrix
A=Matrix([[1,-1,2,-1],[2,1,-2,-2],[-1,2,-4,1],[3,0,0,-3]],ring=QQ)
#Free variables to be used in the given vector
var('s t')
#The null space of the matrix A
ns=A.kernel()
s=2
t=1
#Create a vector
ans=s*vector([1,0,0,1])+t*vector([0,2,1,0])
#If ans is in the null space of A, then A*ans is the 0 vector
print A*ans
#If A*ans is the 0 vector, then the following should be true too
print ans in ns
</code></pre>
<p>I had expected the last print statement to be true since A*ans results in a zero vector, hence ans is in the null space of A.</p>
<p>Why isn't this the case?</p>
https://ask.sagemath.org/question/10781/using-sage-to-check-if-a-vector-is-in-a-matrixs-null-space/?answer=15730#post-id-15730Your vector `ans` is not in `A.kernel()` because of how `A.kernel()` is defined. From its documentation:
Returns the left kernel of this matrix, as a vector space or free
module. This is the set of vectors "x" such that "x*self = 0".
Note: For the right kernel, use "right_kernel()". The method
"kernel()" is exactly equal to "left_kernel()".
So since `ans * A` is not zero, `ans` is not in `A.kernel()`.
sage: ans in A.kernel()
False
sage: ans in A.right_kernel()
True
`A.right_kernel()` is what you want.Tue, 26 Nov 2013 06:17:44 -0600https://ask.sagemath.org/question/10781/using-sage-to-check-if-a-vector-is-in-a-matrixs-null-space/?answer=15730#post-id-15730Answer by tmonteil for <p>I want to check if a given vector is inside a matrix's null space.</p>
<p>Here is what I have done:</p>
<pre><code>#Define a matrix
A=Matrix([[1,-1,2,-1],[2,1,-2,-2],[-1,2,-4,1],[3,0,0,-3]],ring=QQ)
#Free variables to be used in the given vector
var('s t')
#The null space of the matrix A
ns=A.kernel()
s=2
t=1
#Create a vector
ans=s*vector([1,0,0,1])+t*vector([0,2,1,0])
#If ans is in the null space of A, then A*ans is the 0 vector
print A*ans
#If A*ans is the 0 vector, then the following should be true too
print ans in ns
</code></pre>
<p>I had expected the last print statement to be true since A*ans results in a zero vector, hence ans is in the null space of A.</p>
<p>Why isn't this the case?</p>
https://ask.sagemath.org/question/10781/using-sage-to-check-if-a-vector-is-in-a-matrixs-null-space/?answer=15729#post-id-15729Actually, the `.kernel()` method corresponds to the left kernel. What you are looking for is the right kernel:
sage: ns = A.right_kernel()
sage: ans in ns
True
Tue, 26 Nov 2013 06:14:35 -0600https://ask.sagemath.org/question/10781/using-sage-to-check-if-a-vector-is-in-a-matrixs-null-space/?answer=15729#post-id-15729