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].

edit retag close merge delete

Sort by » oldest newest most voted

Looks 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.

more