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.

Subsection 4.6.1 Vector Spaces in Sage

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{.}\)
\(v\)

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.

Subsection 4.6.2 Polynomial Space in Sage

In this subsection we shall explore, how to work with the polynomial space \({\mathcal{P}}_n(\mathbb{R})\) in Sage. The main idea is to consider a polynomial \(p(x) =\sum_i^n a_i x^i\) as a vecror \(\left(a_0,a_1,\ldots,a_n\right)\in \mathbb{R}^{n+1}.\)
Sage provides ways to extract coefficents of a polynomail. If \(p(x)\) is a polynomial then p(x).coefficients() returns the list of coefficints and the corresponding degree in increasing order of degree.
There is a way to get only the coeffiencts as well as follows.
Note that if \(p\) is a polynomial of degree \(k\text{,}\) then it can be also thought of as a vector in \({\mathcal{P}}_n(\mathbb{R})\) for \(m\geq k\text{.}\) In order take care of this, let us create s sage routine as follow.
Next we write a Sage routine to find the degree of a polynomial.
Now we write a Sage routine to check if a set of polynomial is linearly independent or linearly dependent. How do we do this? Suppose \(p_1(x), \ldots, p_k(x)\) be polynomials of degrees \(n_1, n_2, \ldots, n_k\) respectively. Let \(n = \max\{deg(p_i)\} \text{.}\) Then \(p_i(x)\in {\mathcal{P}}_n(\mathbb{R})\) for each \(i\) and each \(p_i\) corresponds to a vector \(v_i \in \mathbb{R}^{n+1}\text{.}\) Thus to check linearly dependency of \(p_1(x),\ldots, p_k(x)\) amount to checking linearly dependency of \(v_1,\ldots, v_k \in \mathbb{R}^{n+1}\text{.}\)
Next we write a Sage routine to find the coordinates of a polynomial with respect to a basis of a polynomial space.
Finally let us define a Sage routine to find the linear span of set of polynomials.

Remark 4.6.6.

Once we get the basis matrix of the subspace we can write the corresponding polynomials as basis.

Subsection 4.6.3 Matrix Spaces in Sage

How to we work with matrix space in Sage? Note that if \(A\) is an \(m\times n\) real matrix, then we can think of this as a vector in \(\mathbb{R}^{mn}\) defined by stacking the entries row-by-row (or column-by-column). In particular, the matrix space \(M(m,n,\mathbb{R})\) is ismorphic to \(\mathbb{R}^{mn}\) via flattening map
\begin{equation*} \varphi \colon M(m,n,\mathbb{R})\to \mathbb{R}^{mn} \end{equation*}
defined by stacking the entries row-by-row (or column-by-column).
\(\{A_1,\ldots, A_k\}\text{,}\)\(\{\varphi(A_1),\ldots, \varphi(A_k)\}\)\(\varphi^{-1}\)\(M(m,n,\mathbb{R})\text{.}\)
Let us first see how to convert a given matrix to \(A\) to the associated vector \(v=\varphi(A)\) and conversely, how to convert a given vector \(v\) to the associated matrix \(A=\varphi^{-1}(v)\) in sage. We define two Sage rountines.

Example 4.6.7.

Check if the set of matrices \(\{A_1,A_2,A_3,A_4,A_5\}\) where
\begin{equation*} A_1=\left(\begin{array}{rrrr} 0 \amp -1 \amp 0 \amp 1 \\ 2 \amp -2 \amp -2 \amp -1 \\ \frac{1}{2} \amp -2 \amp 0 \amp 1 \end{array}\right), A_2=\left(\begin{array}{rrrr} 0 \amp \frac{1}{2} \amp 0 \amp -2 \\ 0 \amp 0 \amp 0 \amp -1 \\ 0 \amp 1 \amp 0 \amp -1 \end{array}\right),A_3 = \left(\begin{array}{rrrr} 0 \amp -1 \amp -2 \amp 0 \\ 0 \amp -\frac{1}{2} \amp -1 \amp -1 \\ -2 \amp 0 \amp 1 \amp 2 \end{array}\right) \end{equation*}
\begin{equation*} A_4 = \left(\begin{array}{rrrr} 1 \amp \frac{1}{2} \amp 2 \amp -1 \\ -2 \amp 1 \amp 0 \amp 1 \\ -\frac{1}{2} \amp 0 \amp 0 \amp -1 \end{array}\right), A_5=\left(\begin{array}{rrrr} -1 \amp -5 \amp -4 \amp 9 \\ 6 \amp -\frac{11}{2} \amp -5 \amp -1 \\ -\frac{1}{2} \amp -7 \amp 1 \amp 8 \end{array}\right) \end{equation*}
is linearly depedent in \(M(3,r,\mathbb{Q})\text{.}\)
Clearly the above set of matrices are not linearly independent. In fact,
\begin{equation*} A_1-3/2A_2+1/2A_3-1/2A_4 -1/2A_5=0. \end{equation*}

Activity 4.6.1.

Write Sage rountines to find coordinates of a matrix with repect to a basis of a matrix space and also the to find the linear span of set of matrices.