Skip to main content

Section 4.6 Sage Computations

In this section, we demonstrate how Sage can be utilized to define vector spaces and perform computations involving vectors. We shall explore various concepts related to vector spaces that have been introduced in this chapter. The majority of the computations will be carried out in the vector spaces \(\mathbb{Q}^n\text{,}\) \(\mathbb{R}^n\) and \(\mathbb{C}^n\text{.}\) Additionally, we shall examine certain concepts pertaining to the vector space \({\mathcal{P}}_n(\mathbb{R})\text{.}\)
To define a vector space \(\mathbb{Q}^n\) in Sage, one may use QQ^n or VectorSpace(QQ, n). Similarly, the vector spaces \(\mathbb{Q}^n\) and \(\mathbb{C}^n\) can be defined using RR^n and CC^n, respectively. Sage provides built-in functions for generating the linear span of a set of vectors, checking their linear dependence, and determining the coordinates of a vector with respect to a given basis. Nevertheless, we will also employ the Reduced Row Echelon Form (RREF) to explore various concepts such as subspaces, linear dependence and independence, and the computation of coordinates.

Example 4.6.1. Vector space \(\mathbb{Q}^6\).

Let us define the vector space \(\mathbb{Q}^6\) and explore the various concepts in Sage. Define 5 vectors \(v_1=\left(1,\,-1,\,2,\,3,\,1,\,4\right) \text{,}\) \(v_2=(\left(2,\,1,\,0,\,2,\,-3,\,1\right),) \text{,}\) \(v_3=(\left(-4,\,-5,\,4,\,0,\,11,\,5\right),) \text{,}\) \(v_4=(\left(-1,\,0,\,2,\,1,\,3,\,2\right),)\) and \(v_5=(\left(-2,\,-2,\,4,\,2,\,7,\,5\right),) \text{.}\) Find the linear span of \(B=\{v_1,v_2,v_3,v_4,v_5\}\text{.}\) Check of \(B\) is a linearly independent set.
We can use the following sage commands to define the \(\mathbb{Q}^6\text{.}\)
V
Try to explore more methods on the object V.
Now let us define vectors in \(V\text{.}\)
Next, let us define the linear span of \(B=\{v_1,\ldots, v_5\}\text{.}\)
We can check if W is a subspace of V.
Note that a basis of the \(L(B)\) can be also obtained by applying RREF on the matrix whose rows are vectors, \(v_1,\ldots, v_5\text{.}\)
Notice that the 1st three rows of the above output consitute the basis of the \(L(B\text{.}\)
Next let us check whether the set of vecors \(B\) is linearly dependent using the following Sage syntax.
If the set is linearly indepensent then S = V.linear_dependence(B);S returns an empty list. However, if \(B\) is linearly dependent then it returs a list of scalars (possibly more than on1), say \(\alpha_1,\cdots,\alpha_5\) such that \(\sum v_i\alpha_i=0\text{.}\) Let us verift the same.

Checkpoint 4.6.2.

Repeat the above example on the vector space \(\mathbb{R}^6\text{.}\)

Example 4.6.3.

Let \(V=\mathbb{Q}^4\) be a vector space. Let \({\cal B}=\{(1,0,0,0),(1,1,0,0,),(1,0,-1,0),(0,0,0,1)\}\text{.}\) Check if \({\cal B}\) is linearly independent. Consider a vector \(v=(-1,2,3,-2)\text{.}\) Find the coordinates of \(v\) with respect to \({\cal B}\text{.}\)
Solution 1.
Let us solve this problem in Sage.
Since \(S\) is an empty list, \(B\) is a linearly independent set.
Next, we define a subspace with \(B\) as a basis and then find the coordinates of the vector \(v\) with respect to \(B\text{.}\)
We can also find the coordinate vector of \(v\text{.}\)
We cal also find the coordinates of \(v\) using RREF.
The last columns of the above output is the coordinate vector.

Example 4.6.4.

Let \(V=\mathbb{Q}^4\) be a vector space. Let
\begin{equation*} {\cal B}=\{(1,0,0,0),(1,1,0,0,),(1,0,-1,0),(0,0,0,1)\} \end{equation*}
and
\begin{equation*} {\cal C}=\{(1,1,1,0),(1,1,0,1),(1,0,1,1),(1,1,1,-1)\} \end{equation*}
be two bases of \(V\text{.}\) Find the change of basis matrix \(M_{\cal B}^{\cal C}\) and the change of basis matrix \(M_{\cal C}^{\cal B}\text{.}\) Consider a vector \(v=(-1,2,3,-2)\text{.}\) Suppose \(c_{\cal B}\) be the coordinate vector of \(v\) with respect to the basis \({\cal B}\) and \(v_{\cal C}\) be the coordinate vector of \(v\) with respect to the basis \({\cal C}\text{.}\) Verify that \(v_{\cal C}=M_{\cal B}^{\cal C}v_{\cal B}\text{.}\)
Solution 2.
Let us solve the above problem in Sage. Note that the matrix \(M_{\cal B}^{\cal C}\) can be obtained in many ways. One way, is to find this by writing the matrix of whose columns are the coordinate vectors of each vector in \({\cal B}\) with respect to \({\cal B}\text{.}\) Alternatively, we can use RREF on \([{\cal C}~ {\cal B}\) and take the last four columns of the same.
Next we find the change of basis matrix using RREF.
Let us verify that \(v_{\cal C}=M_{\cal B}^{\cal C}v_{\cal B}\text{.}\)
The change of basis matrix \(M_{\cal B}^{\cal C}\text{,}\) can be also otained by finding the matrix of the identity linear transformation \(Id: \mathbb{Q}^4\to \mathbb{Q}^4\) and find its matrix with respect to the basis \({\cal B}\) of the domain and \({\cal C}\) on the codomain.

Example 4.6.5. The Dimesnion Formula.

Let \(\text{.}\)  Let  \(W_1\) be a subspace spanned by \(v_1=(-3, 1, -1, 2), v_2=(1, -1, 11, 3),  v_3=(-5, 1, 9, 7), v_4=(-1, 12, 5, 4)\) and \(W_2\) be the subspace spanned by vectors \(u_1=(3, -1, 5,  7), u_2 =(21, 11, 10, -13), u_3=(-2, 3, -51, 7), u_4=(-17, -10, -51, 34)\text{.}\) Find bases and dimensions of  subspace \(W_1+W_2\) and \($W_1\cap W_2\text{.}\)
Solution 3.
Let us solve the above problem using Sage.
W1+W2 returns the space \(W_1+W_2\text{.}\)
Intersection of W1 and W2 using the inbuilt sage function intersection
Finally, we can verify the dimension formula.