Skip to main content

Section 3.5 Sage Computations

In this section we explain how Sage can be used to explore linear transformations and deal with various concepts related to linear transformations that we have learned in this chapter.
Sage has a construction linear_transformation() to define a linear transformation. We can define a linear transformation in Sage using the following four different ways.
  1. linear_transformation(A, side='left') defines a linear transformation as a matrix transformation defined by the matrix \(A\text{.}\) Domain and the codomain and the base space are inferred by the dimension of the matrix and base ring (the ring from on matrix is defined). Side is the keyword that can be taken as ’left’ or ’right’, the default value is ’left’.
  2. linear_transformation(D, C, A, side='left') , this is same as before, however, here domain is taken as \(D\) and codomain is taken as \(C\text{.}\)
  3. linear_transformation(D, C, f), in this case \(f\) is the function that is defined on on a basis element of the domain and it produces the elements of the codomain. The linear tranformation is the unique linear transformation that is extended linearly on the domain \(D\text{.}\)
  4. linear_transformation(D, C, images), where ’images’ is a list or a touple of vectors of the codmain, whihc is equal to the size of the basis. Each element of th basis is mapped to the corresponding element of the image.

Example 3.5.1. Linear transformaation defined by a matrix.

Let us define a linear tarnsformatrion defined by a matrix \(A\) over a rational numbers.
\begin{equation*} A = \begin{pmatrix} 2\amp -1\amp3\\-2\amp 3\amp 5\end{pmatrix} \end{equation*}
from \(\Q^3\to \Q^2\text{.}\)
Clearly, here \(T\) is the matrix tranformatrion defined from \(\Q^3\) to \(\Q^2\text{.}\) We can find, the domain, codmain, parent, images, kernel, that is null space etc using the dot methods. Let us explore some of them.
T.matrix() returns a basis matrix of the image along with the dimension of the image space. By default, it takes option side="left". In our case, we need to give option side="right".
We can find image of any vector using the following Sage syntax.
The kernel of a linear transoformation can be obtained using T.kernel().
If we take a vector \(w\) in the range space of \(T\text{,}\) we can find a representative of the \(T^{-1}(w)\text{.}\)
Note that the same linear transformation, we can also define as follows.

Activity 3.5.1.

Define a matrix \(A=\left(\begin{array}{rrr} -1 \amp 1 \amp 0 \\ 2 \amp 0 \amp -1 \\ 2 \amp -1 \amp 1 \\ 0 \amp 2 \amp 1 \end{array}\right)\) and define a linear transformation \(T:\Q^3\to \Q^4\) by \(T(x)=Ax\text{.}\) Now define two vectors \(w_1=(-3, -1, 0, -31/3)\) and \(w_2=(1, -1/2, -1, 11/6)\) . Check that \(w_1,w_2\) lie in the image space of \(T\text{.}\) Let \(v_1\in T^{-1}(w_1)\) and \(v_2\in T^{-1}(w_1)\text{.}\) Show that \(v_1, v_2\) are linearly independent in \(\Q^3\text{.}\) Can you generalize this result and prove the same?

Example 3.5.2.

Let us define a linear transformation \(T:\Q^4\to\Q^4\) given by
\begin{equation*} T(x_1,x_2,x_3,x_4)=\left(\begin{array}{r} -x_{1} + 2 \, x_{3} - x_{4} \\ x_{1} + 3 \, x_{2} + 7 \, x_{3} - 2 \, x_{4} \\ x_{1} - 3 \, x_{2} - 21 \, x_{3} + 4 \, x_{4} \\ x_{1} + 6 \, x_{2} + 16 \, x_{3} - 5 \, x_{4} \end{array}\right) \end{equation*}
and explore this in Sage to find various concept associated with \(T\text{.}\)
Next we find the matrix of \(T\) with respect to a bases \(\beta\) on the domain and a basis \(\gamma\) on the codomain.
Now we define a subspace \(V_1\) of \(V\) with B as a basis and a subspace \(W_1\) of \(W\) with C as a basis. After that we rectrict T on to \(V_1\) on its domain and \(W_1\) on its codomain. We call this restriction as \(T_1\text{.}\) The matrix of \(T_1\) is the matrix of \(T\) with respect to \(\beta\) on domain and \(\gamma\) on codmain, that is \([T]_\beta^\gamma\text{.}\)
Note that the matrix \([T]_\beta^\gamma\) can also be obtained by defining a column matrix whose columns are \(w_1,w_2,w_3,w_4, T(v_1), T(v_2), T(v_3), T(v_4)\text{,}\) then applying RREF and extracting the last four columns.
The image of \(T\) can also be obtained by using RREF of the matrix whose rows are vectors \(T(u1),T(u2),T(u3),T(u4)\text{.}\)
Note that the first three rows of this is sage as what we get using T.image().
Reshufling the basis elements.
Let us see what happens to the matrix of a linear transformation when we reshuffle the elemnts of a basis on domain and codomain. Let us find the matrix of the abobe linear transformation with resepect the basis \(\beta_1 =\{u_1,u_3,u_2,u_4\}\) of the domain and \(\gamma\) of the codomain. Here we have interchanged 2nd and 3rd basis elements of \(\beta\text{.}\) You may also explore with other interchanges.
Clearly in this case 2nd and 3rd columns of \([T]_\beta^\gamma\) are interchanged.
Clearly in this case 2nd and 3rd rows of \([T]_\beta^\gamma\) are interchanged.
Thus we have the following result.

Example 3.5.4. Reflection about a line \(y=mx\).

Find the reflection of \((2,4)\) about the line \(y=\frac{1}{2} x\text{.}\) Also plot the point and the line and the point point obtained by reflection.

Example 3.5.5.

Find the projection of \((4,-1)\) about the line \(y=\frac12 x\text{.}\) Plot the point, the line and the point of proection.

Activity 3.5.2.

Write a Sage routine to find the matrix of reflection about the plane \(ax+by+cz=0\text{.}\) Hence use this routine to find the reflection of a point \((2,5,7)\) about the plane \(2x-3y+z=0\text{.}\) Also plot the point, the plane and the point of reflection.

Example 3.5.6. Linear transformation defined by its action on a basis.

Suppose \(T\) is a linear transformation from \(V=\mathbb{Q}^4\) to \(W=\mathbb{Q}^3\text{.}\)
Fix a basis \(\beta=\{v_1,v_2,v_3,v_4\}\) of \(V\) and four vectors \(w_1,w_2,w_3,w_4\) in \(W\) such that \(T(v_i)=w_i\) for \(1\le i\le 4\text{.}\)
Assume
\begin{equation*} v_1=(1,-3,2,-1), v_2=(0,1,0,1), v_3=(-1,2,-1,-1), v_4=(2,-8,4,-3), \end{equation*}
and
\begin{equation*} w_1=(-2,-2,16), w_2=(1,4,-4), w_3=(-3,-4,-9), w_4=(-4,-9,39). \end{equation*}
Find the linear transformation \(T\) explicitly.
Solution.
First we define the vector spaces \(V\) and \(W\) in Sage.
Next we define the basis elements of \(V\) and the images in \(W\text{.}\)
We define the the subspace \(V_1\) of \(V\) with basis \(\beta\text{.}\) Also the standard basis of \(V\)
Let us also define a vector \(X=(x,y,z,w)\) and the matrix \(A\) whose columns the coordinates of \(e_i\) with respect to the basis \(\beta\text{.}\)
Using the ExampleΒ 3.2.11, the map \(T\) can be defined as follows.
The same can be obtained as follows (justification is left to the reader).
Let us verify that \(T(v_i)=w_i\) for \(1\le i\le 4\text{.}\)

Activity 3.5.3.

Let \(A = \begin{pmatrix} 3 \amp -2 \amp 1 \amp -1 \\ 1 \amp 5 \amp 2 \amp 1 \\ -3 \amp 1 \amp 3 \amp 1 \end{pmatrix}\) be the matrix of the linear transformation \(T: \mathbb{R}^4 \to \mathbb{R}^3\) with respect to the basis \(B=\{v_1, v_2, v_3, v_4\}\) of \(\mathbb{R}^4\) and \(B'=\{w_1, w_2, w_3\}\) of \(\mathbb{R}^3\text{,}\) where
\begin{equation*} v_1 = \begin{pmatrix}0 \\ 1 \\ 1 \\ 1\end{pmatrix},\quad v_2 = \begin{pmatrix}2 \\ 1 \\ -1 \\ -1\end{pmatrix},\quad v_3 = \begin{pmatrix}1 \\ 4 \\ -1 \\ 2\end{pmatrix},\quad v_4 = \begin{pmatrix}6 \\ 9 \\ 4 \\ 2\end{pmatrix} \end{equation*}
and
\begin{equation*} w_1 = \begin{pmatrix}0 \\ 8 \\ 8\end{pmatrix},\quad w_2 = \begin{pmatrix}-7 \\ 8 \ 1\end{pmatrix},\quad w_3 = \begin{pmatrix}-6 \\ 9 \\ 1\end{pmatrix}. \end{equation*}
  1. Find \([T(v_1)]_{B'}, [T(v_2)]_{B'}, [T(v_3)]_{B'}, [T(v_4)]_{B'}\text{.}\)
  2. Find \(T(v_1), T(v_2), T(v_3), T(v_4)\text{.}\)
  3. Find the formula for \(T(x_1, x_2, x_3, x_4)\text{.}\)

Activity 3.5.4.

Write a Sage routine to find the matrix of projection of a point on to the plane \(ax+by+cz=0\text{.}\) Hence use this routine to find the projection of point \((2,5,7)\) on to the plane \(2x-3y+z=0\text{.}\) Also plot the point, the plane and the point projected.