Ask Your Question

Revision history [back]

A vector V is in the span of the colomns of a matrix M if, and only if, the equation $MX=V$ has a solution. Here is an example:

sage: M = matrix([[1,2,3],[3,4,5],[5,6,7],[6,7,8]]) ; M
[1 2 3]
[3 4 5]
[5 6 7]
[6 7 8]

sage: V = vector([3,7,11,13])
sage: M.solve_right(V)
(1, 1, 0)

So in this case, V is the sum of the first and second column of M:

sage: M.column(0)+M.column(1) == V
True

However:

sage: V = vector([1,2,3,4])
sage: M.solve_right(V)
ValueError: matrix equation has no solutions

So, in this case V is not in the span of the columns of M.

To deal with such exception, you can define:

sage: def is_in_span(M,V):
....:     try:
....:         M.solve_right(V)
....:         return True
....:     except ValueError:
....:         return False

You have:

sage: V = vector([3,7,11,13])
sage: is_in_span(M,V)
True
sage: V = vector([1,2,3,4])
sage: is_in_span(M,V)
False

A vector V is in the span of the colomns of a matrix M if, and only if, the equation $MX=V$ has a solution. Here is an example:

sage: M = matrix([[1,2,3],[3,4,5],[5,6,7],[6,7,8]]) ; M
[1 2 3]
[3 4 5]
[5 6 7]
[6 7 8]

sage: V = vector([3,7,11,13])
sage: M.solve_right(V)
(1, 1, 0)

So in this case, V is the sum of the first and second column of M:

sage: M.column(0)+M.column(1) == V
True

You can see which columns contribute to the solution with:

 sage: M.solve_right(V).nonzero_positions()
 [0, 1]

However:

sage: V = vector([1,2,3,4])
sage: M.solve_right(V)
ValueError: matrix equation has no solutions

So, in this case V is not in the span of the columns of M.

To deal with such exception, you can define:

sage: def is_in_span(M,V):
....:     try:
....:         M.solve_right(V)
....:         return True
....:     except ValueError:
....:         return False

You have:

sage: V = vector([3,7,11,13])
sage: is_in_span(M,V)
True
sage: V = vector([1,2,3,4])
sage: is_in_span(M,V)
False