Skip to main content

Section 2.3 Matrix Transformations

Matrix-vector multiplication can fundamentally be interpreted as linear transformations of a vector space. We can directly graph these transformations in \(\mathbb{R}^2\) and \(\mathbb{R}^3\text{.}\) We can usually generalize our understanding to \(\mathbb{R}^n\) even though we can’t graph the higher dimensions.
Since we will graph in Matplotlib, which uses NumPy arrays, we will default to NumPy (instead of SymPy) for our matrix computations.
In this section we will learn how to

Subsection Computing Matrix Transformations

We can compute the matrix transformation of a vector simply by using matrix-vector multiplication. Recall that in order to multiply a vector by a matrix, you need a column vector. You can use NumPy’s transpose or .T function to convert if needed.
 3 
Similarly you can use SymPy’s Transpose() or .T to convert.

You Try 2.20.

Note the above code throws an error. Use NumPy’s transpose() function to make \(\vec{x}\) a column vector to fix the error.
Hint.
You can use transpose(arrayname), or arrayname.T

You Try 2.21.

Recall that NumPy has an alternate command for matrix multiplication. Alter the above code to use the alternate command.
Because matrix multiplication extends matrix-vector multiplication linearly, we can find the image of multiple input vectors all at once. We simply use the vectors we want to transform as the columns of a matrix, then we can extract their image from the matrix product as the corresponding column.
\begin{equation*} A \left[ \vec{x}_1, \vec{x}_2, \cdots, \vec{x}_n \right] = \left[ A\vec{x}_1, A\vec{x}_2, \cdots, A\vec{x}_n \right] \end{equation*}

You Try 2.22.

Change the above code to select a different input vector and compare to the corresponding column of the matrix product.

Subsection Visualizing a Matrix Transformation

We can visualize the effect of a transformation by considering the transformed basis vectors. That is, the image of basis vectors under the transformation \(A\text{.}\)
 4 
We say \(A\vec{e}_1\) is the image of the standard basis vector under the transformation \(A\text{.}\) That is, the output of \(A\vec{e}_1\) under the transformation, i.e. where \(\vec{e}_1\) gets mapped via \(A\text{.}\)
We can then picture the image of any linear combinations of basis vectors using scaling and adding tail to tip. Since every other vector is a linear combination of the basis vectors, that allows us to visualize the image of every vector in the space.
Note that for a square matrix, the image can be visualized in the same space as the domain. We will demonstrate for a transformation \(A:\mathbb{R}^2 \to \mathbb{R}^2\) using the standard basis.

Example 2.23. Transformed Basis Vectors.

First we will visualize just the transformed basis vectors.

You Try 2.24.

Edit the code in Example 2.23 to consolidate the basis vectors into columns of one matrix instead. Then extract their images under \(A\) from the resulting matrix product. What do you notice?
To visualize how the rest of the space is transformed, we can use scalar multiples of the standard basis to grid out the space. Since the vector \(2\vec{e}_1 -\vec{e}_2\) gets mapped to \(2A\vec{e}_1-A\vec{e_2}\text{,}\) we can then visualize the result of the transformation using the transformed grid.

Example 2.25. Transformed Point Grid.

We will first apply \(A\) to a grid of coordinate dots. While NumPy’s meshgrid() works great for plotting a grid of points, the arrays are not the right size to act as input vectors for the matrix-vector multiplication.
For matrix-vector multiplication we need column vectors, and if we want to compute the image of several vectors all at once we need a matrix where each column is one of our vectors. Here we want each column to be a coordinate, having top entry an x-value and bottom entry a corresponding y-value. In other words, we need a matrix with first row the x-values and second row the corresponding y-values. NumPy has a array.reshape(size) function that allows us to reshape each array of x-values and y-values from meshgrid() into rows. We can then row_stack() those two rows to form the desired input matrix.

You Try 2.26.

Edit the code in Example 2.25 to print every array to see how the same arrays are reshaped, recombined, and transformed.
Hint.
Print arrays so you can compare X to X1 and Y to Y1. Also compare xygrid to xygrid[i] and xygrid to uvgrid to see the difference.

You Try 2.27.

Edit the code in Example 2.25 to make a \(9\times 9\) point grid instead.
 5 
We used a \(3\times 3\) grid at first so you could print the arrays to follow along. Now we want to generalize to more points.

You Try 2.28.

Note: the argument c= inside scatter tells Matplotlib how to assign colors to our scatterplot. Here we assign colors according to the first array in xygrid. What happens if you assign colors according to the second array inside xygrid?
It can be easier to visualize a matrix transformation using a line grid (or coordinate grid) rather than a collection of points. To use our points to create a grid of lines instead, we need at least two points on each line and we will use plot instead of scatter. Note: horizontal lines have incrementing x-values and constant y-values, while vertical lines have incrementing y-values and constant x-values. We will need to think carefully about which arrays we pass into plot.

Example 2.29. Transformed Line Grid.

We will build up to the transformed grid a step at a time.
Step 1: First we will learn how to graph a basic grid of lines.
Step 2: Graph a transformed grid of lines.
Now we add in the matrix transformation. Note: in order to find the transformed line grid, we again .reshape() the X,Y grid into a row of x-values and a row of y-values so we can matrix multiply each point as a column of a matrix. We will then .reshape() back into a transformed meshgrid in order to graph our grid of transformed lines.

You Try 2.30.

In the first step of Example 2.29, graphing the basic grid of lines, change the number of lines to 3. Now print each array to better see which array is used for each plot. Why do we need the transpose for our vertical lines?

You Try 2.31.

In the second step of Example 2.29, graphing the transformed grid of lines, change the number of lines to 3. Now print each array to better see which array is used for each plot. What happens if you only increment through the X or the Y and not both?

Example 2.32. Coloring the Transformed Line Grid.

Unlike scatter which has an argument c=array that allows us to color code based on an x-coordinate or y-coordinate, plot does not. To better see the effects of a reflection, we will use a sequential color map to color our lines.
Step 1: Color a basic grid
Step 2: Color a transformed grid using corresponding coloring.
Below is the corresponding transformed grid with a sequential color map.
If instead the transformation maps higher dimensional vectors to lower dimensional vectors, or vice versa, we can still visualize the transformation.
  • One approach is to plot two graphs side-by-side color-coded to match: (1) the standard basis (or grid) in the domain and (2)the corresponding transformed basis (or transformed grid) in the co-domain.
  • Another approach is to embed everything in the higher dimensional space and animate the transformation there.
     6 
    Note: every \(m\times n\) matrix transformation, say \(m\geq n\) can be extended to a \(m \times m \) matrix transformation by adding an zero column. (Respectively if \(m \leq n\) we extend to \(n\times n\) and add a zero row).

Subsection Animating Matrix Transformations

What if we wanted to visualize the transformation as a gradual change?
We can think of where we start as applying the identity matrix \(I_n.\) Meanwhile we can think of where we end as applying the final transformation matrix \(A\text{.}\) We can create intermediate transformation matrices \(IA_p \) between \(I_n\) and \(A\) by adding complimentary percentages \(p\) of the identity transformation and our matrix transformation,
\begin{equation*} IA_p = (1-p)*I_n +p*A \text{.} \end{equation*}
We obtain a sliding scale between the two by incrementing through our percentage \(p\) from 0 to 1, starting with 100% of our identity transformation and ending with 100% of our matrix transformation.

Example 2.33. Transformed Basis Vectors with Interactive Slider.

First we create a slider that starts with the identity transformation on two basis vectors and gradually gives us the transformation by \(A.\)
Note that in this interactive, we only want the current frame, and not all the frames up to the current frame. Do you know why?
We can animate the gradual transformation using what we learned in Section 1.5. Using PillowWriter we will start with a somewhat simple .gif of the transformation, then add features.

Example 2.34. Transformed Basis Vectors with PillowWriter .gif.

You Try 2.35.

Comment out plt.cla() in Example 2.34. What happens to your animation?

You Try 2.36.

Add a title, grid, and legend to the animation in Example 2.34. Where do these need to be added in the code to show up in every frame?

Example 2.37. Buffering a .gif.

Note that the animation would look better visually if we started with the standard basis plot and lingered for a few seconds before beginning the transformation. We can add that by grabbing frames of the standard basis plot first.
Note that the .gif would also look better if the animation lingered for a few more seconds at the end where the transformation is complete.

You Try 2.38.

Edit the code in Example 2.37 so the animation pauses at the completed transformation. Where do you need to add the loop to grab the correct frames?
Animating a gradual transformation of our collection of points from the identity to the matrix transformation can be done similarly. We will include one example using a .gif animating the transformation of a grid of points and leave the slider and animating a grid of lines as an exercise for the reader.

Example 2.39. Animated Point Grid Transformation .gif.

You Try 2.40.

Animate a gradual matrix transformation using the line grid.
Because linear transformations can be fully determined by a basis, we can visualize the transformation based on how a basis is transformed. We can visualize matrix transformations on vectors in \(\mathbb{R}^2\) using basis grids (points or lines) and in \(\mathbb{R}^3\) using basis vectors (and perhaps a point grid).

Summary.

  • TBD
  • TBD

Exercises Exercises

1.

Visualize a linear transformation using similar methods on a point grid in \(\mathbb{R}^3\) in Matplotlib.

2.

Animate a linear transformation on a line grid in Matplotlib.

3.

Animate a linear transformation that goes up or down a dimension in Matplotlib. E.g. \(\mathbb{R}^3 \rightarrow \mathbb{R}^2\) or \(\mathbb{R}^2 \rightarrow \mathbb{R}^3 \) all visualized or embedded inside \(\mathbb{R}^3\text{.}\)