In order to successfully complete this assignment, you must do the required reading, watch the provided videos, and complete all instructions. The embedded survey form must be entirely filled out and submitted on or before 11:59pm on the day before class. Students must come to class the next day prepared to discuss the material covered in this assignment.

Pre-Class Assignment: Transformation Matrix


1. Elementary Matrices

NOTE: A detailed description of elementary matrices can be found here in the Beezer text Subsection EM 340-345 if you find the following confusing.

There exist a cool set of matrices that can be used to implement Elementary Row Operations. Recall our elementary row operations include:

  1. Swap two rows
  2. Multiply a row by a constant ($c$)
  3. Multiply a row by a constant ($c$) and add it to another row.

You can create these elementary matrices by applying the desired elementary row operations to the identity matrix.

If you multiply your matrix from the left using the elementary matrix, you will get the desired operation.

For example, here is the elementary row operation to swap the first and second rows of a $3\times 3$ matrix:

$$ E_{12}= \left[ \begin{matrix} 0 & 1 & 0\\ 1 & 0 & 0 \\ 0 & 0 & 1 \end{matrix} \right] $$
import numpy as np
import sympy as sym
sym.init_printing(use_unicode=True)
A = np.matrix([[3, -3,9], [2, -2, 7], [-1, 2, -4]])
sym.Matrix(A)
$$\left[\begin{matrix}3 & -3 & 9\\2 & -2 & 7\\-1 & 2 & -4\end{matrix}\right]$$
E1 = np.matrix([[0,1,0], [1,0,0], [0,0,1]])
sym.Matrix(E1)
$$\left[\begin{matrix}0 & 1 & 0\\1 & 0 & 0\\0 & 0 & 1\end{matrix}\right]$$
A1 = E1*A
sym.Matrix(A1)
$$\left[\begin{matrix}2 & -2 & 7\\3 & -3 & 9\\-1 & 2 & -4\end{matrix}\right]$$

**DO THIS**: Give a $3\times 3$ elementary matrix named E2 that swaps row 3 with row 1 and apply it to the $A$ Matrix. Replace the matrix $A$ with the new matrix.

# Put your answer here.  
# Feel free to swich this cell to markdown if you want to try writing your answer in latex.
from answercheck import checkanswer

checkanswer.matrix(E2,'2c2d2e407389eabeb6d90894565c830f');
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-2e6ff2c01205> in <module>
      1 from answercheck import checkanswer
      2 
----> 3 checkanswer.matrix(E2,'2c2d2e407389eabeb6d90894565c830f');

NameError: name 'E2' is not defined

**DO THIS**: Give a $3\times 3$ elementary matrix named E3 that multiplies the first row by $c=3$ and adds it to the third row. Apply the elementary matrix to the $A$ matrix. Replace the matrix $A$ with the new matrix.

# Put your answer here.  
# Feel free to swich this cell to markdown if you want to try writing your answer in latex.
from answercheck import checkanswer

checkanswer.matrix(E3,'55ae1f9eb21df00c59dad623b9471506');
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-c71cb23dc7c6> in <module>
      1 from answercheck import checkanswer
      2 
----> 3 checkanswer.matrix(E3,'55ae1f9eb21df00c59dad623b9471506');

NameError: name 'E3' is not defined

**DO THIS**: Give a $3\times 3$ elementary matrix named E4 that multiplies the second row by a constant $c=1/2$ applies this to matrix $A$.

# Put your answer here.  
# Feel free to swich this cell to markdown if you want to try writing your answer in latex.
from answercheck import checkanswer

checkanswer.matrix(E3,'55ae1f9eb21df00c59dad623b9471506');
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-9-c71cb23dc7c6> in <module>
      1 from answercheck import checkanswer
      2 
----> 3 checkanswer.matrix(E3,'55ae1f9eb21df00c59dad623b9471506');

NameError: name 'E3' is not defined

If the above are correct then we can combine the three operators on the original matrix $A$ as follows.

A = np.matrix([[3, -3,9], [2, -2, 7], [-1, 2, -4]])

sym.Matrix(E4*E3*E2*A)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-10-0801f8d0e63d> in <module>
      1 A = np.matrix([[3, -3,9], [2, -2, 7], [-1, 2, -4]])
      2 
----> 3 sym.Matrix(E4*E3*E2*A)

NameError: name 'E4' is not defined

2. The Inverse Matrix (aka $A^{-1}$)

For some (not all) square matrices $A$, there exists a special matrix called the Inverse Matrix, which is typically written as $A^{-1}$ and when multiplied by $A$ results in the identity matrix $I$:

$$ A^{-1}A = AA^{-1} = I $$

Some properties of an Inverse Matrix include:

  1. $(A^{-1})^{-1} = A$
  2. $(cA)^{-1} = \frac{1}{c}A^{-1}$
  3. $(AB)^{-1} = B^{-1}A^{-1}$
  4. $(A^n)^{-1} = (A^{-1})^n$
  5. $(A^t)^{-1} = (A^{-1})^t$ here $A^t$ is the tranpose of the matrix $A$.

If you know that $A^{-1}$ is an inverse matrix to $A$, then solving $Ax=b$ is simple, just multiply both sides of the equation by $A^{-1}$ and you get:

$$A^{-1}Ax = A^{-1}b$$

If we apply the definition of the inverse matrix from above we can reduce the equation to:

$$Ix = A^{-1}b$$

We know $I$ times $x$ is just $x$ (definition of the identity matrix), so this further reduces to:

$$x = A^{-1}b$$

To conclude, solving $Ax=b$ when you know $A^{-1}$ is really simple. All you need to do is multiply $A^{-1}$ by $b$ and you know $x$.

**DO THIS:** Find a Python numpy command that will calculate the inverse of a matrix and use it invert the following matrix A. Store the inverse in a new matirx named A_inv

import numpy as np
import sympy as sym
sym.init_printing(use_unicode=True) # Trick to make matrixes look nice in jupyter

A = np.matrix([[1, 2, 3], [4, 5, 6], [7,8,7]])

sym.Matrix(A)
$$\left[\begin{matrix}1 & 2 & 3\\4 & 5 & 6\\7 & 8 & 7\end{matrix}\right]$$
#put your answer to the above question here.

Lets check your answer by multiplying A by A_inv.

A * A_inv
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-13-11907386186c> in <module>
----> 1 A * A_inv

NameError: name 'A_inv' is not defined
np.allclose(A*A_inv, [[1,0,0],[0,1,0],[0,0,1]])
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-14-e45a7742ee13> in <module>
----> 1 np.allclose(A*A_inv, [[1,0,0],[0,1,0],[0,0,1]])

NameError: name 'A_inv' is not defined

**QUESTION:** What function did you use to find the inverse of matrix $A$?

put your answer to the above question here

How do we create an inverse matrix?

From previous assignments, we learned that we could string together a bunch of Elementary Row Operations to get matrix ($A$) into it's Reduced Row Eshelon form. We now know that we can represent Elementary Row Operations as a sequence of Elementaary Matrices as follows:

$$ E_n \dots E_3 E_2 E_1 A = RREF $$

If $A$ reduces to an identity matrix (i.e. $A$ is row equivalent to $I$), then $A$ has an inverse and its inverse is just all of the Elementary Matrices multiplied together:

$$ A^{-1} = E_n \dots E_3 E_2 E_1 $$

Consider the following matrix.
$$ A = \left[ \begin{matrix} 1 & 2 \\ 4 & 6 \end{matrix} \right] $$

A = np.matrix([[1, 2], [4,6]])

It can be reduced into an identity matrix using the following elementary operators $$ E_1 = \left[ \begin{matrix} 1 & 0 \\ -4 & 1 \end{matrix} \right] $$

$$ E_2 = \left[ \begin{matrix} 1 & 1 \\ 0 & 1 \end{matrix} \right] $$$$ E_3 = \left[ \begin{matrix} 1 & 0 \\ 0 & -\frac{1}{2} \end{matrix} \right] $$
E1 = np.matrix([[1,0], [-4,1]])
E2 = np.matrix([[1,1], [0,1]])
E3 = np.matrix([[1,0],[0,-1/2]])

We can just check that the statment seems to be true by multiplying everything out.

E3*E2*E1*A
matrix([[1., 0.],
        [0., 1.]])

**DO THIS:** Combine the above elementary Matrices to make an inverse matrix named A_inv

# Put your answer to the above question here.

**DO THIS:** Verify that A_inv is an actual inverse and chech that $AA^{-1} = I$.

# Put your code here.

**QUESTION:** Is an invertible matrix is always square? Why or why not?

Put your answer to the above question here.

**QUESTION:** Is a square matrix is always invertible? Why or why not?

Put your answer to the above question here.

**QUESTION:** Describe the Reduced Row Echelon form of a square, invertible matrix.

Put your answer to the above question here.

**QUESTION:** Is the following matrix in the Reduced Row Echelon form?

$$ \left[ \begin{matrix} 1 & 2 & 0 & 3 & 0 & 4 \\ 0 & 0 & 1 & 3 & 0 & 7 \\ 0 & 0 & 0 & 0 & 1 & 6 \\ 0 & 0 & 0 & 0 & 0 & 0 \end{matrix} \right] $$

Put your answer to the above question here.

**QUESTION:** If the matrix shown above is not in Reduced Row Echelon form. What rule is violated?

Put your answer to the above question here.

**QUESTION:** What is the size of the matrix described in the previous QUESTION?

  • $4 \times 6$
  • $6 \times 4$
  • $3 \times 6$
  • $5 \times 3$

Put your answer to the above question here.

**QUESTION:** Describe the elementary row operation that is implemented by the following matrix

$$ \left[ \begin{matrix} 0 & 1 & 0 \\ 1 & 0 & 0 \\ 0 & 0 & 1 \end{matrix} \right] $$

Put your answer to the above question here.


3. Transformation Matrix

Consider the following set of points:

%matplotlib inline
import matplotlib.pylab as plt

x = [0.0,  0.0,  2.0,  8.0, 10.0, 10.0, 8.0, 4.0, 3.0, 3.0, 4.0, 6.0, 7.0, 7.0, 10.0, 
     10.0,  8.0,  2.0, 0.0, 0.0, 2.0, 6.0, 7.0,  7.0,  6.0,  4.0,  3.0, 3.0, 0.0]
y = [0.0, -2.0, -4.0, -4.0, -2.0,  2.0, 4.0, 4.0, 5.0, 7.0, 8.0, 8.0, 7.0, 6.0,  6.0,
     8.0, 10.0, 10.0, 8.0, 4.0, 2.0, 2.0, 1.0, -1.0, -2.0, -2.0, -1.0, 0.0, 0.0]

plt.plot(x,y, color='green');
plt.axis('equal');

We can rotate these points around the origin by using the following simple set of equations:

$$ x \cos(\theta) - y \sin(\theta) = x_{rotated} $$$$ x \sin(\theta) + y \cos(\theta) = y_{rotated} $$

This can be rewritten as the following system of matrices:

$$ \left[ \begin{matrix} \cos(\theta) & -\sin(\theta) \\ \sin(\theta) & \cos(\theta) \end{matrix} \right] \left[ \begin{matrix} x \\ y \end{matrix} \right] = \left[ \begin{matrix} x_{rotated}\\ y_{rotated} \end{matrix} \right] $$

We can rotate the points around the origin by $\pi/4$ (i.e. $45^o$} as follows:

import numpy as np
import sympy as sym
sym.init_printing(use_unicode=True) # Trick to make matrixes look nice in jupyter

points = np.matrix([x,y])
angle = np.pi/4
R = np.matrix([[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.cos(angle)]]);
sym.Matrix(R)
$$\left[\begin{matrix}0.707106781186548 & -0.707106781186547\\0.707106781186547 & 0.707106781186548\end{matrix}\right]$$
p=R*points

plt.plot(p[0].T,p[1].T);
plt.axis('equal');

#print(p[0].T)

We can even have a little fun and keep applying the same rotation over and over again.

# Apply R and plot 8 times
for i in range(0,8):
    p = R * p
    plt.plot(p[0].T,p[1].T);

plt.axis('equal');

**QUESTION:** In the above code what does the T call in p[0].T do?

Put your answer here

**DO THIS:** Modify the above code to move the data and place the center of the S at a different point and then do the rotation loop.


4. Assignment wrap-up

Please fill out the form that appears when you run the code below. You must completely fill this out in order to receive credit for the assignment!

Direct Link to Google Form

If you have trouble with the embedded form, please make sure you log on with your MSU google account at googleapps.msu.edu and then click on the direct link above.

**Assignment-Specific QUESTION:** If the matrix shown above is not in Reduced Row Echelon form. What rule is violated?

Put your answer to the above question here

**QUESTION:** Summarize what you did in this assignment.

Put your answer to the above question here

**QUESTION:** What questions do you have, if any, about any of the topics discussed in this assignment after working through the jupyter notebook?

Put your answer to the above question here

**QUESTION:** How well do you feel this assignment helped you to achieve a better understanding of the above mentioned topic(s)?

Put your answer to the above question here

**QUESTION:** What was the most challenging part of this assignment for you?

Put your answer to the above question here

**QUESTION:** What was the least challenging part of this assignment for you?

Put your answer to the above question here

**QUESTION:** What kind of additional questions or support, if any, do you feel you need to have a better understanding of the content in this assignment?

Put your answer to the above question here

**QUESTION:** Do you have any further questions or comments about this material, or anything else that's going on in class?

Put your answer to the above question here

**QUESTION:** Approximately how long did this pre-class assignment take?

Put your answer to the above question here

from IPython.display import HTML
HTML(
"""
<iframe 
	src="https://cmse.msu.edu/cmse314-pc-survey" 
	width="100%" 
	height="1000px" 
	frameborder="0" 
	marginheight="0" 
	marginwidth="0">
	Loading...
</iframe>
"""
)

Congratulations, we're done!

To get credits for this assignment, you must fill out and submit the above survey form on or before the assignment due date.

Course Resources:

Written by Dr. Dirk Colbry, Michigan State University Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.