Skip to main content

Section 2.5 Sage Computations

In this section, we explain, how Sage can be used to deal with all the concepts that we have learned in this chapter. We use reduced row echelon form (RREF) as tool to deal with all the comoutations.
Vectors in Sage
In keyword ’vector’ to define vectors by mentioning the domain from which entries are taken and giving the entries in a list. We can plot vectors in 2 and 3 dimensions. We can define vectors with entries from rational numbers (’QQ’), real numbers (’RR’), complex numbers (’CC’) and even finite fields ’GF(p^k)’ where \(p\) is a prime number.
Vectors in \(\R^2\)
Vectors in \(\R^3\)
Vector in higher dimension in Sage
Vectors in hiher dimensions can be defined in a similar manner, we just need to increase the number of coordinates/components. Let us look at an example.

Example 2.5.1.

Genarate five random vectors in \(\mathbb{R}^8\) whose entries are random integers between -10 and 10. Also generate 5 random numbers (scalars) whihc are integers between -10 and 10. Find the linear combinations of these vectors with five scalars.

Example 2.5.2.

(i) Let us plot the linear span of a single vector \((3,2, -1)\) in \(\R^3\) in Sage.
Let us also plot the linear span of two vectors \((3,2, -1), (-1,1,2)\) in \(\R^3\) in Sage.
This can be achieved by sage method ’parametric_plot3d’ in sage.
Generating matrix from vectors:
We can generate matrix from a given set of vectors its columns and rows. Let us look at an example.
Vector Spaces over rational or reals:
In sage a vector space, \(V\) has two methods V.random_element() and V.an_element() that displays a random element and an element (fixed) of \(V\text{.}\)
Linearly dependence and indepndence in Sage

Example 2.5.3.

Let us generate some random 5 vectors, \(v_1,\ldots, v_5\) in the vector space QQ^5. Sage has a method .are_linearly_dependent() to check if a set of vectors are linearly dependent. Sage also has a method linear_dependence() whihc return emply list in case the set is linearly independent and returns a list of scalars \(\alpha_i\) such that \(\sum \alpha_i v_i=0\text{.}\)
Now we can check if the scalar linear combination of the output shown above with sect of vectors in \(B\) is zero.

Example 2.5.4. Linear Span of vectors in Sage.

Sage can generate a linear span of a set of vectors in a vector space using the vector space method .span(). Let us explore this for set of 5 vectors in a vector space V=QQ^4.
It returns the dimension of the linear span along with a basis as a matrix. We can check if any linear combinations of vectors in B lies in W.
We can also check if W is a subspave of V using the following sage syntaxes.

Example 2.5.5. Using RREF to find a basis of a linear span.

Let us use the sage method rref to find a basis of the set of vectors define earlies in Sage.
Clearly the first three rows constitutes a basis of linear span. This is what we obtained using .span().

Example 2.5.6. Coordinates of a vector w.r.t. a basis.

Consider a set of vectors \(B = \{(1,-1,3,2), (-2,1,3,-1), (5,3,1,4),(3,2,1,2)\}\text{.}\)Check if this forms a basis of the vector space \(V=\mathbb{Q}^5\text{.}\) Find the coordinates of the vector \(v=(5,7,11,-13)\) with respect to this basis.
We can also print this basis as a matrix using V.basis_matrix()
Now we define the set of vectors in Sage.
We can also check the same by finding the rank of the martrix \(M\) whose columns and the given vectors. If the rank is 4, then the given set of vectors are linearly independet.
Since it returns an empty list. The given set of vectors \(B\) is linearly independet. We can find the coordinates of the vector using sage method coordinate_vector() or using .coordinates() using the following codes. For this first we need to create a subspace say V1 with B as a basis using the method V.subspace_with_basis(B)
We can also find the same manually using RREF as follows.
The same can be achieved using by solving the system \(Bx=v\text{.}\)

Example 2.5.7. Fundamental subscapes of a matrix.

Consider a matrix \(A= \left(\begin{array}{rrrrrr} -351 \amp -1053 \amp -1053 \amp 160 \amp 382 \amp 1657 \\ -32 \amp -96 \amp -96 \amp 15 \amp 35 \amp 152 \\ -36 \amp -108 \amp -108 \amp 16 \amp 39 \amp 169 \\ 11 \amp 33 \amp 33 \amp -5 \amp -12 \amp -52 \\ -11 \amp -33 \amp -33 \amp 5 \amp 12 \amp 52 \end{array}\right)\text{.}\) Find the fundamental subspaces defined in the Example Example 2.2.4
Solution.
First of all let us find the row space using the method .row_space(). This returns the dimension and the basis matrix of the row space. This we can verify using rref applied to the matrix \(A\text{.}\)
Clearly the first three row of rref(A) is same as basis matrix obtained uising the inbuilt sage method.
Next we find the columns space using the sage method A.column_space() and verify the same using the rref applied to \(A^T\text{.}\)
Clearly, the 1st three rows of rref(A) are same as basis matrix of A.column_space()
The column space of \(A\) is also known as image space of \(A\text{.}\) Sage has inbuilt method A.image() to find the image space.
The null space of \(A\) also known as kernel of \(A\) can be obtained by the sage method A.right_kernel().
Clearly, the kernel is of dimension 3.
The left null space can be obtained by the sage method A.left_kernel() or using A.T.right_kernel().

Observation 2.5.8.

Consider the matrix \(A\) defined in the Example Example 2.5.7. Observe the following:
(i) Let \(u\) be a random element in \(Row(A)\) and \(v\) be a random element in \(null(A)\text{.}\) Then \(u\) and \(v\) are orthogoal. That is, \(u\cdot v=0\text{.}\)
(ii) Let \(u\) be a random element in \(Col(A)\) and \(v\) be a random element in \(leftnull(A)\text{.}\) Then \(u\) and \(v\) are orthogoal. That is, \(u\cdot v=0\text{.}\)
Solution.

Example 2.5.9. Change of coordinates.

Consider the vector space \(V=\mathbb{Q}^4\text{.}\) Defines two subsets
\begin{equation*} \beta=\{(-5, 8, 0, 4),(-3, 9, -2, 4),(-1, 4, -1, 2),(-1, 2, 0, 1)\} \end{equation*}
and
\begin{equation*} \gamma = \{(0, 2, -7, 5),(-1, 2, -1, 4),(1, -3, 5, -7),(1, 1, -8, 3)\}\text{.} \end{equation*}
Check that \(\beta\) and \(\gamma\) are linearly independent hence they form bases of \(V\text{.}\) Let \(x=(3,5,7,9)\in V\text{.}\) Find the coordinates of \(x\) with respect to \(\beta\) and \(\gamma\) and denote them by \(x_\beta\) and \(x_\gamma\) respectively. Find the change of basis matrices \([I]_\beta^\gamma\) and \([I]_\gamma^\beta\text{.}\) Show that \(\left([I]_\beta^\gamma\right)^{-1}=[I]_\gamma^\beta\text{.}\) Also verify that \([I]_\beta^\gamma x_\beta=x_\gamma\) and \([I]_\gamma^\beta x_\gamma=x_\beta\text{.}\)
Solution.
Clearly B and C are linearly indendent and hence they are bases of \(V\text{.}\)
Next we define two subspaces V1 and V2 with B and C as bases respectively and define the vector \(x\) in Sage.
Next we find the coordinates \(x_\beta\) and \(x_\gamma\text{.}\)
Next we define the change of basis matrix \([I]_\beta^\gamma\text{.}\) This can be achieved by taking a matrix \(M=[C~|~B]\) applying rref and extracting the last four columns.
One can verify that this is same as \(C^{-1}B\text{.}\)
Next we define the change of basis matrix \([I]_\gamma^\beta\text{.}\) This can be achieved by taking a matrix \(M=[B~|~C]\) applying rref and extracting the last four columns. One can verify that this is same as \(C^{-1}B\text{.}\)
Let us check that \(\left([I]_\beta^\gamma\right)^{-1}=[I]_\gamma^\beta\text{.}\)
Finally we show that \([I]_\beta^\gamma x_\beta=x_\gamma\) and \([I]_\gamma^\beta x_\gamma=x_\beta\text{.}\)

Example 2.5.10. Sum and intersection of subspaces.

Let \(u_1=\left(2,-1,0,3,1,2\right)\text{,}\) \(u_2=\left(-1,2,0,-6,-2,-1\right)\text{,}\) \(u_3=\left(5,-6,-2,18,3,6\right)\) and \(u_4=\left(-12,18,6,-54,-9,-15\right)\text{.}\) Let \(W_1=L(\{u_1,u_2,u_3,u_4\})\text{.}\)
Let \(v_1=\left(-1,4,2,-12,-1,-2\right)\text{,}\) \(v_2=\left(1,2,-1,3,2,1\right)\text{,}\) \(v_3=\left(22,-30,-10,90,15,27\right)\text{,}\) \(v_4=\left(19,-24,-5,63,11,22\right)\) and \(v_5=\left(-1,2,0,-1,3,0\right)\text{.}\) Let \(W_2=L(\{v_1,v_2,v_3,v_4,v_5\})\text{.}\)
Find the subspaces \(W_1\cap W_2\) and \(W_1+W_2\text{.}\)
Solution.
Sage has inbulit method to find sum and intersection of two subspaces. However, shall also see how these can be obtained manually.
First of all we define \(W_1=L(S_1)\text{.}\)
This show that \(W_1\) is a 3-dimensional subspace of \(V\text{.}\)
Next, we define \(W_2=L(S_2)\)
Here \(W_2\) is a 4 dimensional subspace of \(V\text{.}\)
Now we find \(W_1\cap W_2\) using the Sage method W1.intersection(W2).
Next we can find \(W_1+W_2\) as follows.
From the above computations, it is clear that
\begin{equation*} \dim(W_1+W_2)=\dim(W_1)+\dim(W_2)-\dim(W_1\cap W_2)\text{.} \end{equation*}
In fact, this is true in general and we shall prove this later.
How to find \(W_1+W_2\) manually?
This can be obtained as \(L(S_1\cup S_2)\text{.}\) Thus we can construct a matrix \(M\) whose rows are \(S_1\cup S_2\) and apply RREF. Let us see this.
Clearly the first 5 rows of RREF(M) is the basis matrix obtained for \(W_1+W_2\text{.}\)
Finding \(W_1\cap W_2\) manually.
Let \(c_1,c_2, c_3\) be a basis of \(W_1\)   and \(d_1,d_2, d_3,d_4\) be a  basis of \(W_2\) obtined above. If  \(v\in W_1\cap W_2\text{.}\) Then
\begin{equation*} v=\alpha_1c_1+\alpha_2c_2+\alpha_3c_3=\beta_1d_1+\beta_2d_2+\beta_3d_3+\beta_4 d_4\text{.} \end{equation*}
In particular, \(\alpha_1c_1+\alpha_2c_2+\alpha_3c_3-\beta_1d_1-\beta_2d_2-\beta_3d_3-\beta_4 d_4=0\text{.}\)  We can obtain \(\alpha_i\)’s and \(\beta_i\)’s by solving
\begin{equation*} \begin{bmatrix} c_1 \amp c_2\amp c_3\amp d_1\amp d_2\amp d_3 \amp d_4\end{bmatrix} \begin{pmatrix} \alpha_1 \\ \alpha_2\\ \alpha_3\\ \beta_1\\ \beta_2\\ \beta_3\\\beta_4\end{pmatrix}=0 \end{equation*}
Thus we can find the null space of \(\begin{bmatrix} c_1 \amp c_2\amp c_3\amp d_1\amp d_2\amp d_3 \amp d_4\end{bmatrix}\) and find the vectors in the basis of \(W_1\cap W_2\text{.}\) Let see how to do this in Sage.
Once we have the basis of \(null(P)\text{,}\) we can obained the basis vector in \(W_1\cap W_2\) as follows.
Clearly these are the basis vectors for \(W_1\cap W_2\) obtained using the inbulit Sage method.