# Finding basis of image of Matrix

Is there a command that explicitly gives me the vectors for the image given a matrix M? If not, lets suppose I have the matrix in row echelon, N. So I create an empty list. Basically, I want to say while i< # of rows, if row i of row echelon form matrix N has a 1, put M[i] in list. But I don't know how to say if a row has a 1( a pivot).

P.s., yes I need the rows. My matrix is like transposed.

edit retag close merge delete

Please provide code for a matrix you care about, that can be copy-pasted in a fresh Sage session.

( 2020-06-02 00:56:22 +0100 )edit

Sort by » oldest newest most voted

In Sage, matrices have an image method.

The image is given as a vector space with basis.

Using random coefficients in a square matrix we are likely to get an invertible matrix whose image is the whole space.

sage: a = matrix(QQ, 4, [randint(-5, 5) for _ in range(16)])
sage: a
[-5 -1 -1  0]
[ 4 -1  1  5]
[-5 -4  3  3]
[ 1  1 -1  2]
sage: a.image()
Vector space of degree 4 and dimension 4 over Rational Field
Basis matrix:
[1 0 0 0]
[0 1 0 0]
[0 0 1 0]
[0 0 0 1]


Now change the last line to be a combination of the first three lines.

sage: b = copy(a)
sage: b
[-5 -1 -1  0]
[ 4 -1  1  5]
[-5 -4  3  3]
[ 1  1 -1  2]
sage: b[3] = 3*b[2] + b[1] - b[0]
sage: b
[ -5  -1  -1   0]
[  4  -1   1   5]
[ -5  -4   3   3]
[ -6 -12  11  14]
sage: b.image()
Vector space of degree 4 and dimension 3 over Rational Field
Basis matrix:
[     1      0      0  29/33]
[     0      1      0 -97/33]
[     0      0      1 -16/11]

more

( 2020-06-04 18:20:40 +0100 )edit

As explained by @slelievre, you can use the image method to get the row space of a matrix, that is, the vector space spanned by its rows. Then, the basis method allows to explicitly get a basis as a list of vectors. For example, consider the matrix $$A=\left(\begin{array}{rrrrrr} 3 & 6 & 3 & 3 & -6 & -3 \\ 4 & 8 & 4 & 4 & -8 & -4 \\ 3 & 6 & 0 & 0 & -6 & -9 \\ -3 & -6 & -3 & -4 & 7 & 3 \end{array}\right).$$ We first define this matrix and get its RREF:

sage: A = matrix(QQ,[[3, 6, 3, 3, -6, -3], [4, 8, 4, 4, -8, -4],
....:                [3, 6, 0, 0, -6, -9], [-3, -6, -3, -4, 7, 3]])
sage: A.rref()
[ 1  2  0  0 -2 -3]
[ 0  0  1  0  1  2]
[ 0  0  0  1 -1  0]
[ 0  0  0  0  0  0]


The first three rows form a basis of $\operatorname{row} A$. This is what the image method also points out:

sage: A.image()
Vector space of degree 6 and dimension 3 over Rational Field
Basis matrix:
[ 1  2  0  0 -2 -3]
[ 0  0  1  0  1  2]
[ 0  0  0  1 -1  0]


Let us extract the basis:

sage: A.image().basis()
[
(1, 2, 0, 0, -2, -3),
(0, 0, 1, 0, 1, 2),
(0, 0, 0, 1, -1, 0)
]


As an alternative, the pivots_row method provides the rows in the matrix that are linearly independent (please, note, in the original matrix, not in the rref):

sage: p = A.pivot_rows(); p
(0, 2, 3)


Now you can extract these rows and get a basis of $\operatorname{row} A$:

sage: A[p,:].rows()
[(3, 6, 3, 3, -6, -3), (3, 6, 0, 0, -6, -9), (-3, -6, -3, -4, 7, 3)]


For the sake of completeness, let us check that these rows really span $\operatorname{row} A$:

sage: A[p,:].image() == A.image()
True

more

( 2020-06-04 18:20:56 +0100 )edit