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.Thu, 16 Jul 2020 01:04:39 +0200Checking what the span is for a vectorhttps://ask.sagemath.org/question/52485/checking-what-the-span-is-for-a-vector/Let's assume I have a vector called v1 and I have a matrix called Matrix.
Let us assume that the vector is in the span of the rows of Matrix. How would I know what the linear combination is? Here is what I do know. Let's assume this is a m by n matrix. I do know there is the span function. So
I can do something like make a list of vectors out of the Matrix. Say something like make an empty list ListofVectors=[].
i=0
while i< m:
ListofVectors.append(M[i])
i+=1
Now doing
v1 in span(ListofVectors)
will give me true assuming v1 is in the span. However, is there a function that tells me what the coefficients are for each term. For example, the vector v1=[3,2,1] for the ListofVectors being [1,1,1], [1,0,0], and [0,1,0] should give me coefficients 1,2,1 respectively as 1*[1,1,1] + 2*[1,0,0] + 1*[0,1,0] gives [3,2,1].
Thu, 16 Jul 2020 00:20:50 +0200https://ask.sagemath.org/question/52485/checking-what-the-span-is-for-a-vector/Answer by John Palmieri for <p>Let's assume I have a vector called v1 and I have a matrix called Matrix.</p>
<p>Let us assume that the vector is in the span of the rows of Matrix. How would I know what the linear combination is? Here is what I do know. Let's assume this is a m by n matrix. I do know there is the span function. So</p>
<p>I can do something like make a list of vectors out of the Matrix. Say something like make an empty list ListofVectors=[].</p>
<pre><code>i=0
while i< m:
ListofVectors.append(M[i])
i+=1
</code></pre>
<p>Now doing </p>
<pre><code>v1 in span(ListofVectors)
</code></pre>
<p>will give me true assuming v1 is in the span. However, is there a function that tells me what the coefficients are for each term. For example, the vector v1=[3,2,1] for the ListofVectors being [1,1,1], [1,0,0], and [0,1,0] should give me coefficients 1,2,1 respectively as 1<em>[1,1,1] + 2</em>[1,0,0] + 1*[0,1,0] gives [3,2,1].</p>
https://ask.sagemath.org/question/52485/checking-what-the-span-is-for-a-vector/?answer=52486#post-id-52486Looks like you are trying to solve the matrix equation `x Matrix = v1` for `x`. Use `Matrix.solve_left(v1)`.
sage: m = random_matrix(QQ, 2, 4)
sage: m
[ 0 1 0 -1/2]
[ -1 0 0 0]
sage: v = 2*m[0] + m[1]
sage: v
(-1, 2, 0, -1)
sage: m.solve_left(v)
(2, 1)
Of course this need not have any solutions, and if it does have a solution, it may not be unique.Thu, 16 Jul 2020 01:04:39 +0200https://ask.sagemath.org/question/52485/checking-what-the-span-is-for-a-vector/?answer=52486#post-id-52486