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.Mon, 02 Dec 2013 04:19:14 +0100Yet another linear combinationhttps://ask.sagemath.org/question/10791/yet-another-linear-combination/Hi, I'm really new to Sage and to programming in general. I have 20 linear independent vectors of length 20, and a linear dependent vector. I would like to write this one as a linear combination of the others: I looked up on the internet all day, but nothing I found worked. The ways I tried are:
- define a vector space of dim 20 on the field I'm using, impose my vectors as a base, and use the method .coodinates(). PROBLEM: I couldn't impose the basis, I didn't find a command to do so.
- use G.solve_right(s), where G is a 20x20 matrix and s is my linear dependent vector. I thought I would have a vector as an output but instead I get something of dimension 20x8. Weird.
- I tried to do a linear system directly using equations with the vectors, in order to solve them with respect to some variables, but I got as an output that you can't do it using vectors.
So, what shall I try?
Thank you very much Fri, 29 Nov 2013 18:49:42 +0100https://ask.sagemath.org/question/10791/yet-another-linear-combination/Comment by Luca for <p>Hi, I'm really new to Sage and to programming in general. I have 20 linear independent vectors of length 20, and a linear dependent vector. I would like to write this one as a linear combination of the others: I looked up on the internet all day, but nothing I found worked. The ways I tried are:</p>
<ul>
<li><p>define a vector space of dim 20 on the field I'm using, impose my vectors as a base, and use the method .coodinates(). PROBLEM: I couldn't impose the basis, I didn't find a command to do so.</p></li>
<li><p>use G.solve_right(s), where G is a 20x20 matrix and s is my linear dependent vector. I thought I would have a vector as an output but instead I get something of dimension 20x8. Weird. </p></li>
<li><p>I tried to do a linear system directly using equations with the vectors, in order to solve them with respect to some variables, but I got as an output that you can't do it using vectors. </p></li>
</ul>
<p>So, what shall I try?
Thank you very much </p>
https://ask.sagemath.org/question/10791/yet-another-linear-combination/?comment=16592#post-id-16592I don't see why `G.solve_right(s)` shouldn't work. Double check your code, maybe you did some programming mistake.Sat, 30 Nov 2013 10:02:27 +0100https://ask.sagemath.org/question/10791/yet-another-linear-combination/?comment=16592#post-id-16592Answer by Bubusettete for <p>Hi, I'm really new to Sage and to programming in general. I have 20 linear independent vectors of length 20, and a linear dependent vector. I would like to write this one as a linear combination of the others: I looked up on the internet all day, but nothing I found worked. The ways I tried are:</p>
<ul>
<li><p>define a vector space of dim 20 on the field I'm using, impose my vectors as a base, and use the method .coodinates(). PROBLEM: I couldn't impose the basis, I didn't find a command to do so.</p></li>
<li><p>use G.solve_right(s), where G is a 20x20 matrix and s is my linear dependent vector. I thought I would have a vector as an output but instead I get something of dimension 20x8. Weird. </p></li>
<li><p>I tried to do a linear system directly using equations with the vectors, in order to solve them with respect to some variables, but I got as an output that you can't do it using vectors. </p></li>
</ul>
<p>So, what shall I try?
Thank you very much </p>
https://ask.sagemath.org/question/10791/yet-another-linear-combination/?answer=15746#post-id-15746Ok, it works, sorry for the silly question, I got a bit lost and panicked with the fact of not knowing Sage commands. But it was easy :) thank you very much!Mon, 02 Dec 2013 04:19:14 +0100https://ask.sagemath.org/question/10791/yet-another-linear-combination/?answer=15746#post-id-15746Answer by tmonteil for <p>Hi, I'm really new to Sage and to programming in general. I have 20 linear independent vectors of length 20, and a linear dependent vector. I would like to write this one as a linear combination of the others: I looked up on the internet all day, but nothing I found worked. The ways I tried are:</p>
<ul>
<li><p>define a vector space of dim 20 on the field I'm using, impose my vectors as a base, and use the method .coodinates(). PROBLEM: I couldn't impose the basis, I didn't find a command to do so.</p></li>
<li><p>use G.solve_right(s), where G is a 20x20 matrix and s is my linear dependent vector. I thought I would have a vector as an output but instead I get something of dimension 20x8. Weird. </p></li>
<li><p>I tried to do a linear system directly using equations with the vectors, in order to solve them with respect to some variables, but I got as an output that you can't do it using vectors. </p></li>
</ul>
<p>So, what shall I try?
Thank you very much </p>
https://ask.sagemath.org/question/10791/yet-another-linear-combination/?answer=15745#post-id-15745Basic linear algebra is easily done with matrices. Let me replace 20 by 3 (in my example) and assume that your set of 3 linearly independent vectors are stored in a list L:
sage: L = []
sage: L.append(vector(QQ,[1,2,3]))
sage: L.append(vector(QQ,[0,3,1]))
sage: L.append(vector(QQ,[0,0,2]))
sage: L
[(1, 2, 3), (0, 3, 1), (0, 0, 2)]
Then, you define the matrix M whose columns are the vectors of L:
sage: M = matrix(L).transpose()
sage: M
[1 0 0]
[2 3 0]
[3 1 2]
Let V be your additional linear dependent vector:
sage: V = vector(QQ,[1,1,2]) ; V
(1, 1, 2)
The linear combination you are looking for is given by $M^{-1}V$:
sage: W = M.inverse()*V ; W
(1, -1/3, -1/3)
Which you can check with:
sage: sum(W[i]*L[i] for i in range(3))
(1, 1, 2)
sage: sum(W[i]*L[i] for i in range(3)) == V
True
Sat, 30 Nov 2013 06:38:40 +0100https://ask.sagemath.org/question/10791/yet-another-linear-combination/?answer=15745#post-id-15745Answer by Francis Clarke for <p>Hi, I'm really new to Sage and to programming in general. I have 20 linear independent vectors of length 20, and a linear dependent vector. I would like to write this one as a linear combination of the others: I looked up on the internet all day, but nothing I found worked. The ways I tried are:</p>
<ul>
<li><p>define a vector space of dim 20 on the field I'm using, impose my vectors as a base, and use the method .coodinates(). PROBLEM: I couldn't impose the basis, I didn't find a command to do so.</p></li>
<li><p>use G.solve_right(s), where G is a 20x20 matrix and s is my linear dependent vector. I thought I would have a vector as an output but instead I get something of dimension 20x8. Weird. </p></li>
<li><p>I tried to do a linear system directly using equations with the vectors, in order to solve them with respect to some variables, but I got as an output that you can't do it using vectors. </p></li>
</ul>
<p>So, what shall I try?
Thank you very much </p>
https://ask.sagemath.org/question/10791/yet-another-linear-combination/?answer=15744#post-id-15744This is just standard linear algebra: form a 21 by 20 matrix whose rows consist of the linearly dependent vectors and the 20 linearly independent vectors, and find the one-dimensional left kernel (nullspace); alternatively transpose and find the right kernel. The coefficients of the single element in a basis of the kernel provide a linear combination of all 21 vectors which is zero. Hence the dependent vector can be written as a linear combination of the 20 independent vectors.
A small scale version in Sage:
sage: V = VectorSpace(QQ, 6)
sage: vectors = [V.random_element() for _ in range(6)]
sage: V.subspace(vectors).rank()
6
Confirming that the vectors are linearly independent, otherwise repeat. Then form a random linear combination, and create the matrix:
sage: w = sum(randint(-4, 4)*v for v in vectors)
sage: A = matrix([w] + vectors)
Now take the first (and only) generator of the kernel, and verify the result:
sage: c = A.kernel().basis()[0]
sage: w == -sum(c[i+1]*vectors[i] for i in range(6))
True
Sat, 30 Nov 2013 06:23:09 +0100https://ask.sagemath.org/question/10791/yet-another-linear-combination/?answer=15744#post-id-15744