Contact Us!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

| Download

Math 208 Interactive Notebooks © 2024 by Soham Bhosale, Sara Billey, Herman Chau, Zihan Chen, Isaac Hartin Pasco, Jennifer Huang, Snigdha Mahankali, Clare Minerath, and Anna Willis is licensed under CC BY-ND 4.0

Views: 846
License: OTHER
Image: ubuntu2204
Kernel: SageMath 10.3

© 2024 By Soham Bhosale, Sara Billey, Herman Chau, Zihan Chen, Isaac Hartin Pasco, Jennifer Huang, Snigdha Mahankali, Clare Minerath, and Anna Willis is licensed under CC BY-ND 4.0

Welcome to Chapter 3: The Paradise of Matrices

Originally created by ZihanChenZihan Chen | zchen84@uw.eduzchen84@uw.edu

Preface:

This Sage Worksheet is a part of a LinearLinear AlgebraAlgebra tutorial created by Prof.SaraSara BilleyBilley's 2024 WXML group. Referred to LinearLinear AlgebraAlgebra withwith ApplicationsApplications byby HoltHolt, 2nd2^{nd} editionedition, this Worksheet is intended to help future UWUW Math208Math208 students with some tools and software to present more visual linear algebra content. This Worksheet will revolve around Section3.1Section 3.1 of HoltHolt's Book.

Get Strart:

I am delighted to share with you the basic knowledge of matrices, an important part of linearlinear algebraalgebra. But allow me to start with linearlinear transformationstransformations. You might wonder what the connection between linearlinear transformationstransformations and matricesmatrices is, but I believe after this section, you will have a better understanding of linearlinear transformationstransformations. Let's begin with Section3.1Section 3.1: LinearLinear TransformationsTransformations!

3.13.1— LinearLinear TransformationsTransformations

You might be familiar with what a transformation is, which is the operation of changing the position or appearance of a figure. But what exactly is 'linearlinear'? Why use 'linearlinear' to describe a transformationtransformation?

In textbooks, we have a clear definition of linear transformation.

Theorem1:Theorem1: A function T:RmRn T: \mathbb{R}^m \rightarrow \mathbb{R}^n is a linear transformation if for all vectors u u and v v in Rm \mathbb{R}^m and all scalars r r , we have:

  • T(u+v)=T(u)+T(v) T(u + v) = T(u) + T(v)
  • T(ru)=rT(u) T(ru) = rT(u)

Let's consider a simple example:

Example1:Example1: Let T([xy])=[x2x+y] T\left(\begin{bmatrix} x \\ y \end{bmatrix}\right) = \begin{bmatrix} x^2 \\ x + y \end{bmatrix} . We will try to determine whether T T is a linear transformation using Theorem1Theorem1.

""" This function is for checking 2 conditions of the linear transformation 1. closed under addition 2. closed under scalar multiplication Inputs: ------- variables named x1,x2,y1,y2, in symbolic form Outputs: -------- (additive_check,homogeneity_check), that is, (satisfy condition 1? , satisfy condition 2?) eg. (satisfy condition 1, not satisfy condition 2)=(True,False) """ # define unknown symbols(skip choosing concrete numbers here just for demonstrate more representative examples) var('x_1 x_2 y_1 y_2 c') # define x and y, note here SR: Symbolic Ring # we use unknown variables to make our line changes more representative:) x = vector(SR, [x_1, x_2]) y = vector(SR, [y_1, y_2]) # define transformation T def T(v): x, y = v return vector([x^2, x + y]) # you can change the "return vector" if you want) # check first condition: T(x+y) ?=? Tx+Ty additive_check = T(x + y) == T(x) + T(y) # check second condition: cT(x) ?=? T(c*x) homogeneity_check = c * T(x) == T(c * x) # output results (additive_check, homogeneity_check)
(False, False)

This does not seem to be a linear transformation because we checked whether A(x+y)=Ax+AyA(x + y) = Ax + Ay and whether cA(x)=A(cx)cA(x) = A(c \cdot x). Both of these conditions are false in our example, so we can conclude that this is not a linear transformation.

Similarly, I encourage you to modify this expression and think about when it would be a linear transformation. (HintHint: What should be the highest power of the unknowns in a linear transformation?)

Let's consider Example2Example2 and try to determine whether it is a linear transformation by observing the equation.

Example2:Example2: Let T2([xy])=[x+yx+y+1] T_2\left(\begin{bmatrix} x \\ y \end{bmatrix}\right) = \begin{bmatrix} x+y \\ x + y+1 \end{bmatrix} . We will try to determine whether T2 T_2 is a linear transformation using Theorem1Theorem1.

""" Same function as before Inputs: ------- variables named x1,x2,y1,y2, in symbolic form Outputs: -------- (additive_check,homogeneity_check) """ var('x_1 x_2 y_1 y_2 c') x = vector(SR, [x_1, x_2]) y = vector(SR, [y_1, y_2]) def T_2(v): x, y = v return vector([x+y, x+y+1]) additive_check = T_2(x + y) == T_2(x) + T_2(y) homogeneity_check = c * T_2(x) == T_2(c * x) (additive_check, homogeneity_check)
(False, False)

I assume you've already understood how to simply observe an expression to conclude whether it's a linear transformation. Now, I want to present another example to help you understand better.

Example3:Example3: Suppose we have a transformation T3([xy])=[2x2y] T_3\left(\begin{bmatrix} x \\ y \end{bmatrix}\right) = \begin{bmatrix} 2x \\ 2y \end{bmatrix} . I'd like you to determine whether this is a linear transformation, but this time, I have a request:

  1. I hope you can represent this linear transformation in the form: T3([xy])=A[xy] T_3\left(\begin{bmatrix} x \\ y \end{bmatrix}\right) = A\begin{bmatrix} x \\ y \end{bmatrix} .
  2. Insert any coordinates you like for xx and yy.
  3. Determine whether this is a linear transformation based on the Theorem1Theorem1.

Of course, I will also provide my answer, let's try it together!

  1. Represent this linear transformation: T([x,y])=[2002]([x,y]) T([x, y]) = \begin{bmatrix} 2 & 0 \\ 0 & 2 \end{bmatrix}([x, y]) , where A=[2002].A=\begin{bmatrix} 2 & 0 \\ 0 & 2 \end{bmatrix}. In response, we defined matrix AA in sage:
#step1 define a matrix A = matrix([[2,0],[0,2]]) print("A=") A
A=
[2 0] [0 2]

2. Insert my favorite coordinates: u=[1,2]T,v=[2,3]Tu=[1,2]^T, v=[2,3]^T.

3. Check whether it is a linear transformation.

""""" Inputs: ------- vectors you like, u & v Outputs: -------- (additive_check,homogeneity_check) """"" #step2 input the vector you want u = vector([1,2]) v = vector([2,3]) r = 5 #step3 determine whether it is linear transformation? check1 = A*u+A*v == A*(u+v) check2 = r*A*u == A*(r*u) (check1, check2)
(True, True)

If your attempt concluded that this is a linear transformation, congratulations, you are correct. But is this a coincidence or a necessity? Let's see what the textbook says.

Theorem2:Theorem2: Let AA be an n×mn×m matrix, and define T(x)=Ax T(x) = Ax . Then T:RmRn T: \mathbb{R}^m \rightarrow \mathbb{R}^n is a linear transformation.

So, we now know that any matrix can represent a linear transformation. In other words, if a transformation is linear, we can always write it in the form of a matrix. This is indeed a remarkable conclusion! I encourage you to explore more possibilities!

Also, I would like to introduce a more abstract concept for you to understand, which is also an effective way to judge linear transformations. This theorem is also in the book.

Theorem3:Theorem3: Let [a1a2...am][a_1 a_2 ... a_m] be an n×mn×m matrix, and let T:RmRn T: \mathbb{R}^m \rightarrow \mathbb{R}^n with T(x)=Ax T(x) = Ax be a linear transformation. Then

  1. The vector w w is in the range of T T if and only if Ax=w Ax = w is a consistent linear system.
  2. rangerange(T T ) = spanspan{a1,,am a_1, …, a_m }.

The proofs are all in the textbook, but I want to give a simple example to illustrate this theorem.

Example4Example4 Suppose we have a simple R1R1 R^1 \rightarrow R^1 transformation, T(x)=x2 T(x) = x^2 . Is TT a linear transformation?

Hint: Can you determine the domain and codomain through this linear transformation?

Let's look at a standard example of a linear transformation, but this time, we'll use a graphical representation.

""" This function is for illustrating the rotation transformation. """ # Define a linear transformation, e.g., a rotation def linear_transform(v): # Define the rotation matrix, e.g., rotation by 45 degrees theta = pi / 2 # I suggest modifying this parameter rotation_matrix = matrix([[cos(theta), -sin(theta)], [sin(theta), cos(theta)]]) return rotation_matrix * v # Create vertices of the initial shape, for example, a unit square square_vertices = [(0,0), (1,0), (1,1), (0,1)] # Apply the linear transformation to each vertex transformed_vertices = [linear_transform(vector(v)) for v in square_vertices] # Create the original shape and the transformed shape square = polygon(square_vertices, color='blue') transformed_square = polygon(transformed_vertices, color='red') # Display the original and the transformed shapes show(square + transformed_square)
Image in a Jupyter notebook

This is what I consider to be the quintessential example of a linear transformation. What did you observe from it? First and foremost, the two shapes appear identical. By calculating the area, we can determine that they are exactly the same. If we take the blue square as the initial state, can you describe the process of transformation from blue to red?

What do you notice when you focus your attention on the basis vectors [10] \begin{bmatrix} 1 \\ 0 \end{bmatrix} and [01] \begin{bmatrix} 0 \\ 1 \end{bmatrix} represented?

""" This function is for illustrating the rotation transformation as well. """ # Define vectors and their transformations vectors = {'original': [(1, 0), (0, 1)], 'transformed': [(0, 1), (-1, 0)]} # Create a plot object p = plot([]) # Plot original and transformed vectors for (ox, oy), (tx, ty) in zip(vectors['original'], vectors['transformed']): # Plot original vector (in blue) p += arrow((0, 0), (ox, oy), color='blue', linestyle='dashed') # Plot transformed vector (in red) p += arrow((0, 0), (tx, ty), color='red') # Set properties of the plot p.show(xmin=-1.5, xmax=1.5, ymin=-1.5, ymax=1.5, aspect_ratio=1)
Image in a Jupyter notebook

As can be seen in the above figure, the vector [10] \begin{bmatrix} 1 \\ 0 \end{bmatrix} is transformed to [01] \begin{bmatrix} 0 \\ 1 \end{bmatrix} , and [01] \begin{bmatrix} 0 \\ 1 \end{bmatrix} is transformed to [10] \begin{bmatrix} -1 \\ 0 \end{bmatrix} ! We have found the trick: by observing the transformation of the basis vectors e1,e2,e3,e_1, e_2, e_3, we can determine the nature of this linear transformation. I am eager to share more interesting examples of linear transformations with you.

""" This function is for illustrating the shear transformation. """ # Define the shear transformation matrix k = 1 # Shear factor shear_matrix = matrix([[1, k], [0, 1]]) # Define the vertices of a square square = matrix([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]) # Apply the shear transformation transformed_square = square * shear_matrix # Plot the original square and the sheared square original_plot = polygon(list(square), color='blue', fill=False) transformed_plot = polygon(list(transformed_square), color='red', fill=False, linestyle='--') # Display the plot show(original_plot + transformed_plot, xmin=-0.5, xmax=2.5, ymin=-0.5, ymax=2.5, aspect_ratio=1)
Image in a Jupyter notebook

Observe this; it's an interesting shear transformation. Can you tell where each of the basis vectors has been transformed to?

""" This function is for illustrating the scaling transformation. """ # Define scale factors s_x = 2 # Scale factor for the X-axis s_y = 0.5 # Scale factor for the Y-axis scale_matrix = matrix([[s_x, 0], [0, s_y]]) # Define the vertices of a square square = matrix([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]) # Apply scaling transformation transformed_square = square * scale_matrix # Plot the original square and the scaled square original_plot = polygon(list(square), color='blue', fill=False) transformed_plot = polygon(list(transformed_square), color='red', fill=False, linestyle='--') # Display the plot show(original_plot + transformed_plot, xmin=-0.5, xmax=3, ymin=-0.5, ymax=1.5, aspect_ratio=1)
Image in a Jupyter notebook

I find the method of observing linear transformations through basebase vectorsvectors particularly useful, especially when dealing with abstract three-dimensional linear transformations. For instance, consider an abstract matrix where its column vectors are given by E1=[101]E_1 = \begin{bmatrix} 1 \\ 0 \\ 1 \end{bmatrix}, E2=[31.52]E_2 = \begin{bmatrix} 3 \\ 1.5 \\ 2 \end{bmatrix}, and E3=[023]E_3 = \begin{bmatrix} 0 \\ 2 \\ 3 \end{bmatrix}. It might be challenging to visualize this linear transformation at first. So, how can we understand this linear transformation? I suggest we deduce the transformation process by observing the changes in the basebase vectorsvectors. Let's focus on the three basebase vectorsvectors, labeled in greengreen, redred, and blueblue, respectively. Based on the diagram below, can you identify the characteristics of this linear transformation?

# Import required libraries for 3D visualization from sage.all import line3d, vector, Graphics, point3d # Define base vectors e1 = vector([1, 0, 0]) e2 = vector([0, 1, 0]) e3 = vector([0, 0, 1]) # Define transformed vectors E1 = vector([1, 0, 1]) E2 = vector([3, 1.5, 2]) E3 = vector([0, 2, 3]) # Create lines representing base vectors and transformed vectors lines = line3d([vector([0,0,0]), e1], color='darkgreen', thickness=2) + \ line3d([vector([0,0,0]), e2], color='darkred', thickness=2) + \ line3d([vector([0,0,0]), e3], color='darkblue', thickness=2) + \ line3d([vector([0,0,0]), E1], color='green', thickness=4) + \ line3d([vector([0,0,0]), E2], color='red', thickness=4) + \ line3d([vector([0,0,0]), E3], color='blue', thickness=4) # Place distinct markers at the tips of each vector marks = point3d([e1, e2, e3, E1, E2, E3], color='grey', size=1) # Create a dashed 3D grid (upper half) def create_dashed_3d_grid(size, color, thickness=1, linestyle='--'): grid = Graphics() for i in range(0, size + 1): for j in range(0, size + 1): grid += line3d([(i, j, 0), (i, j, size)], color=color, thickness=thickness, linestyle=linestyle) # Z-axis lines grid += line3d([(i, 0, j), (i, size, j)], color=color, thickness=thickness, linestyle=linestyle) # Y-axis lines grid += line3d([(0, i, j), (size, i, j)], color=color, thickness=thickness, linestyle=linestyle) # X-axis lines return grid # Create the dashed grid for visualization grid = create_dashed_3d_grid(5, 'lightgrey', thickness=1, linestyle='--') # Display the 3D plot plot = lines + marks + grid plot.show(frame=False, aspect_ratio=1)

Now let's look at one-to-one and onto linear transformations. As usual, let's start with the textbook definitions.

First, let's consider onetooneone-to-one:

Let AA be an n×mn×m matrix and define T:RmRn T: \mathbb{R}^m \rightarrow \mathbb{R}^n by T(x)=Ax T(\mathbf{x}) = A\mathbf{x} . Then
  1. T T is one-to-one if and only if the columns of A A are linearly independent.
  2. If AB A \sim B and B B is in echelon form, then T T is one-to-one if and only if B B has a pivot position in every column.
  3. If n<m n < m , then T T is not one-to-one.

Next, the definition of ontoonto:

Let A be an n × m matrix and define T:RmRn T: \mathbb{R}^m \rightarrow \mathbb{R}^n by T(x)=Ax T(\mathbf{x}) = A\mathbf{x} . Then
  1. T T is onto if and only if the columns of A A span the codomain Rn \mathbb{R}^n .
  2. If AB A \sim B and B B is in echelon form, then T T is onto if and only if B B has a pivot position in every row.
  3. If n>m n > m , then T T is not onto.

Can you find commonalities between these two definitions and summarize them? I think a good way to check is to see if there is a pivot in every row and column after reducing to echelon form. In other words, we can check if a linear transformation is one-to-one by checking for pivots in every column, and check if it's onto by checking for pivots in every row, which will be more helpful for our judgment! Let's start with an example.

Example5Example5 Suppose we have a 2×32×3 matrix BB=[123456] \begin{bmatrix} 1 & 2 \\ 3 & 4 \\ 5 & 6 \end{bmatrix} , and determine whether it is one-to-one and onto.

B=matrix(3,2,[1,2,3,4,5,6]) B_2=B.rref() print("Matrix B is") print(B) print("RREF of B is") print(B_2)
Matrix B is [1 2] [3 4] [5 6] RREF of B is [1 0] [0 1] [0 0]

Let's examine this example! We observe that there is a pivot in each column, so it is one-to-one, but not in every row, which leads us to conclude that this linear transformation is not onto. Think about why it is not onto. Can we see this conclusion at a glance? Let's go back to the definition: the primary definition of onto is that the columns of matrix AA span the entire R3 \mathbb{R}^3 . Is this true in our case? The answer is no, because we know that any two column vectors cannot span R3 \mathbb{R}^3 . In other words, we need at least three column vectors for the transformation to be onto. Therefore, we conclude that a linear transformation from Rm \mathbb{R}^m to Rn \mathbb{R}^n is never onto when m>n m > n . Now, let's look at another example.

Example6Example6 Suppose we have a new matrix C=[211120130] C = \begin{bmatrix} 2 & 1 & 1 \\ 1 & 2 & 0 \\ 1 & 3 & 0 \end{bmatrix} . Determine whether it is one-to-one and onto.

C=matrix([[2,1,1],[1,2,0],[1,3,0]]) C_2=C.rref() print("Matrix C is") print(C) print("RREF of C is") print(C_2)
Matrix C is [2 1 1] [1 2 0] [1 3 0] RREF of C is [1 0 0] [0 1 0] [0 0 1]

I'm sure you've noticed that this is both a one-to-one and onto linear transformation! Take note that this is a 3×33×3 matrix. I encourage you to try out several n×nn×n matrices and see if there is a certain correlation between being one-to-one and onto for n×nn×n matrices.

© 2024 By Soham Bhosale, Sara Billey, Herman Chau, Zihan Chen, Isaac Hartin Pasco, Jennifer Huang, Snigdha Mahankali, Clare Minerath, and Anna Willis is licensed under CC BY-ND 4.0