Skip to main content

Linear Algebra with SageMath Learn math with open-source software

Section 10.3 Bases

A basis for the vector space \(V\) is a set of vectors \(\{v_1, v_2,\dots ,v_k\}\) that is linearly independent and spans \(V\text{.}\) We say that the size of this set is the dimension of \(V\text{.}\)
Sage implicitly computes the basis and dimension of any subspace generated with the span command.
Observe that the basis is given in a matrix form. We can explicitly ask for the same result by applying the method basis_matrix.
With the method basis we obtain the basis as a list of vectors. Whereas the method gens returns the same basis as tuple.
The dimension command simply returns the number of vectors in the basis.
Alternatively, we can create the same subspace using the subspace method:
We can check that we obtain the same vector space.
Sage row-reduces your input vectors to produce the basis in echelon form. If we want to create a subspace with a specific basis, we can use the subspace_with_basis method. This method takes as input a list of vectors that form a basis of the subspace.
This method returns an error if we input a list that is not a basis for \(S\text{.}\)
Observe that we still obtain the same vector space, only the specified basis changes:

Subsection 10.3.1 Extracting a Basis from a Generator

Every spanning set in a vector space can be reduced to a basis of that vector space. We can manually extract a basis from a given generating set \(\{v_1, v_2, \dots ,v_n\}\) as follows:
  1. Construct a matrix with \(v_1, v_2, \dots ,v_n\) as its columns.
  2. Find the pivot columns.
The columns corresponding to the pivots form a basis of the subspace spanned by the vectors.
For example, let’s extract a basis of \(S\) from the generator \(v_1,v_2,v_3,v_4\text{.}\)
We obtain that the pivot columns are the first and the second. Then the vectors \(v_1\) and \(v_2\) forms a basis of \(S\text{.}\) Since we already know that the dimension of \(S\) is 2, this result is consistent. We can also verify that these vectors are linearly independent:

Subsection 10.3.2 Coordinates in a Basis

If \(B = \{b_1,b_2, \dots, b_n\}\) is a basis of the vector space \(V\text{,}\) then every \(v \in V\) can be expressed uniquely as a linear combination:
\begin{gather*} v = c_1 b_1 + c_2 b_2 + \dots + c_n b_n \end{gather*}
The scalars \(c_1, c_2, \dots, c_n\) are the coordinates of \(v\) with respect to the basis \(B\) and we write \([v]_B = (c_1, c_2, \dots , c_n)\text{.}\)
Sage provides dedicated methods to calculate the coordinates of a given vector in any basis. To calculate the coordinates in the canonical echelonized basis of the subspace \(S\text{,}\) we simply use the method coordinates and Sage will return the coordinates as a list. For instance, let’s calculate the coordinates of \(v_1\) in the canonical echelonized basis \(B\) of the subspace \(S\text{.}\) Recall that the canonical basis is given as a list:
\begin{equation*} B = \{b_1, b_2\} = \{(1,0,1,-1), (0,1,-1/2,1)\} \end{equation*}
Then the coordinates of \(v_1\) are:
We obtained that the vector \(v_1 = b_1 + 2 \cdot b_2\text{.}\) We can check that this is correct by manually calculating this linear combination and comparing it with the given vector:
By using the method coordinate_vector we obtain the same coordinates as a vector:
Note that Sage stores the basis vectors of \(S\) as rows in the basis matrix. To use the usual column-vector convention, we transpose this matrix so that its columns are the basis vectors of \(S\text{.}\) This results in \(M_B \cdot [v_1]_B = v_1\text{,}\) where \(M_B\) is the canonical basis matrix of \(S\text{.}\)

Subsection 10.3.3 Change of Basis

To calculate the coordinates in any other user defined basis, we use a combination of the methods subspace_with_basis and coordinates. For instance, let’s calculate the coordinates of \(v_1\) in the basis \(C = \{u_1,u_2\}\) of \(S\text{:}\)
We obtained that the vector \(v_1 = 1/3 \cdot u_1 + 1/3 \cdot u_2\text{.}\) Once again, we can verify it by manually calculating this linear combination and comparing it with the given vector:
As expected, when we compute the coordinates of the basis vectors themselves, we obtain the standard unit vector coordinates.

Subsection 10.3.4 Change of Basis Matrix

Often we need to convert the coordinates of a vector from one basis \(B\) to another basis \(C\text{.}\) This is done using a change-of-basis matrix, \(P_{C \leftarrow B}\text{.}\) The conversion formula is:
\begin{equation*} [v]_C = P_{C \leftarrow B} \cdot [v]_B \end{equation*}
where \([v]_B\) and \([v]_C\) are column vectors of coordinates. The columns of the matrix \(P_{C \leftarrow B}\) are the coordinate vectors of the vectors in basis \(B\) expressed in terms of basis \(C\text{.}\)
Let’s calculate the transition matrix from the canonical basis \(B\) of \(S\) to the basis \(C = \{u_1, u_2\}\) of \(T\text{.}\)
We will first define our initial basis \(B\text{.}\)
We will now produce our change of basis matrix by using the coordinates of the vectors in \(B\) with respect to the basis \(C\text{.}\)
Now we can use this matrix to convert the coordinates of \(v_1\) from basis \(B\) to basis \(C\text{.}\)
We can verify that this matches the coordinates we calculated earlier using the coordinates method: