Ask Your Question

Yet another linear combination

asked 2013-11-29 18:49:42 +0200

Bubusettete gravatar image

updated 2015-01-14 16:21:39 +0200

FrédéricC gravatar image

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

edit retag flag offensive close merge delete


I don't see why `G.solve_right(s)` shouldn't work. Double check your code, maybe you did some programming mistake.

Luca gravatar imageLuca ( 2013-11-30 10:02:27 +0200 )edit

3 Answers

Sort by » oldest newest most voted

answered 2013-11-30 06:38:40 +0200

tmonteil gravatar image

Basic 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
edit flag offensive delete link more

answered 2013-12-02 04:19:14 +0200

Bubusettete gravatar image

Ok, 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!

edit flag offensive delete link more

answered 2013-11-30 06:23:09 +0200

Francis Clarke gravatar image

This 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()

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))
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2013-11-29 18:49:42 +0200

Seen: 3,287 times

Last updated: Dec 02 '13