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: Matrix Spaces

%matplotlib inline
import matplotlib.pylab as plt
import numpy as np
import sympy as sym
sym.init_printing(use_unicode=True)

1. Review the Properties of Invertible Matrices

Let $A$ be an $n \times n$ matrix. The following statements are equivalent.

  • The column vectors of $A$ form a basis for $R^n$
  • $|A| \ne 0$
  • $A$ is invertible.
  • $A$ is row equivalent to $I_n$ (i.e. it's reduced row echelon form is $I_n$)
  • The system of equations $Ax = b$ has a unique solution.
  • $rank(A) = n$

Consider the following example. We claim that the following set of vectors form a baiss for $R^3$:

$$B = \{(2,1, 4), (-1,6, 0), (2, 4, -3) \}$$

Remember for these two vectors to be a basis they need to obay the following two properties:

  1. They must span $R^3$.
  2. They must be linearly independent.

Using the above statements we can show this is true in multiple ways.

The column vectors of $A$ form a basis for $R^n$

**DO THIS:** Define a numpy matrix A consisting of the vectors $B$ as columns:

#Put your answer to the above question here
from answercheck import checkanswer

checkanswer.matrix(A,'94827a40ec59c7d767afe6841e1723ce');
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-8f6986dd642d> in <module>
      1 from answercheck import checkanswer
      2 
----> 3 checkanswer.matrix(A,'94827a40ec59c7d767afe6841e1723ce');

NameError: name 'A' is not defined

$|A| \ne 0$

**DO THIS:** The first in the above properties tell us that if the vectors in $B$ are truly a basis of $R^3$ then $|A|=0$. Calculate the determinate of $A$ and store the value in det.

#Put your answer to the above question here
#Verify that the determinate is in fact zero
if np.isclose(det,0):
    print("Since the Determinate is zero the column vectors do NOT form a Basis")
else:
    print("Since the Determinate is non-zero then the column vectors form a Basis.")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-81ab7081fa8d> in <module>
      1 #Verify that the determinate is in fact zero
----> 2 if np.isclose(det,0):
      3     print("Since the Determinate is zero the column vectors do NOT form a Basis")
      4 else:
      5     print("Since the Determinate is non-zero then the column vectors form a Basis.")

NameError: name 'det' is not defined

$A$ is invertible.

**DO THIS:** Since the determinate is non-zero we know that there is an inverse to A. Use python to calculate that inverse and store it in a matrix called A_inv

#put your answer to the above question here
from answercheck import checkanswer

checkanswer.matrix(A_inv,'001aaddd4824f42ad9d2ccde21cf9d24');
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-856f71941f85> in <module>
      1 from answercheck import checkanswer
      2 
----> 3 checkanswer.matrix(A_inv,'001aaddd4824f42ad9d2ccde21cf9d24');

NameError: name 'A_inv' is not defined

$A$ is row equivalent to $I_n$ (i.e. it's reduced row echelon form is $I_n$)

**DO THIS:** According to the property above the reduced row echelon form of an invertable matrix is the Identiy matrix. Verify using the python sympy library and store the reduced row echelone matrix in a variable called rref if you really need to check it.

#put your answer to the above question here
from answercheck import checkanswer

checkanswer.matrix(rref,'cde432847c1c4b6d17cd7bfacc457ed1');
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-9-185d34243ad9> in <module>
      1 from answercheck import checkanswer
      2 
----> 3 checkanswer.matrix(rref,'cde432847c1c4b6d17cd7bfacc457ed1');

NameError: name 'rref' is not defined

The system of equations $Ax = b$ has a unique solution.

Let us assume some arbitrary vector $b \in R^n$. According to the above properties it should only have one solution.

**DO THIS:** Find the solution to $Ax=b$ for the vector $b=(-10,200,3)$. Store the solution in a variable called x

from answercheck import checkanswer

checkanswer.vector(x,'161cfd16545b1b5fb13e35d2800f13df');
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-10-41845754cd02> in <module>
      1 from answercheck import checkanswer
      2 
----> 3 checkanswer.vector(x,'161cfd16545b1b5fb13e35d2800f13df');

NameError: name 'x' is not defined

$rank(A) = n$

The final property says that the Rank should equal the dimension of $R^n$. In our example $n=3$. Find a python function to calculate the rank of $A$. Store the value in a variable named rank to check your answer.

#Put your answer to the above quesion here
#Verify that the determinate is in fact zero
if np.isclose(rank,3):
    print("Rank is 3")
else:
    print("Rank is not 3. Did we do something wrong?")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-12-4bcfc6c36d41> in <module>
      1 #Verify that the determinate is in fact zero
----> 2 if np.isclose(rank,3):
      3     print("Rank is 3")
      4 else:
      5     print("Rank is not 3. Did we do something wrong?")

NameError: name 'rank' is not defined

**QUESTION:** Without doing any calculations (i.e. only using the above properties), how many solutions are there to $Ax=0$? What is(are) the solution(s)?

Put your answer to the above question here.


2. The Basis of a Vector Space

Let $U$ be a vector space with basis $B=\{u_1, \ldots, u_n\}$, and let $u$ be a vector in $U$. Because a basis "spans" the vector space, we know that there exists unique scalars $a_1, \dots a_n$ such that:

$$ u = a_1u_1 + \dots + a_nu_n$$

The values $a_1, \dots, a_n$ are called the coordinates of $u$ relative to the basis ($B$) and is typically written as a column vector:

$$ u_B = \left[ \begin{matrix} a_1 \\ \vdots \\ a_n \end{matrix} \right] $$

We can create a transition matrix $P$ using the inverse of the matrix with the basis vectors being columns.

$$P = [ u_1 \ldots u_n ]^{-1}$$

Now we will show that matrix $P$ will transition vector $u$ in the standard coordinate system to the coordinates relative to the basis $B$:

$$ u_B = Pu$$

EXAMPLE: Consider the vector $u = \left[ \begin{matrix} 5 \\ 3 \end{matrix} \right]$ and the basis vectors $B = \{(1,2), (3,-1)\}$. The following code calculate the $P$ transition matrix from $B$ and then use $P$ to calculate the values of $u_B$ ($a_1$ and $a_2$):

u = np.matrix([[5],[3]])
sym.Matrix(u)
$$\left[\begin{matrix}5\\3\end{matrix}\right]$$
B = np.matrix([[1,2],[3,-1]]).T
sym.Matrix(B)
$$\left[\begin{matrix}1 & 3\\2 & -1\end{matrix}\right]$$
P = np.linalg.inv(B)
ub = P*u

sym.Matrix(ub)
$$\left[\begin{matrix}2.0\\1.0\end{matrix}\right]$$

Here we would like to view this from $R^n$. Let $$B=[u_1 \dots u_n],$$ then the values of $u_B$ can be found by solving the linear system $$u = B u_B.$$ Because the columns of $B$ is a basis, therefore, the matrix $B$ is a $n\times n$ square matrix and it has an inverse. Therefore, we can solve the linear system and obtain $$u_B = B^{-1} u = Pu.$$

Let's try to visualize this with a plot:

ax = plt.axes();


#Blue arrow representing first Basis Vector
ax.arrow(0, 0, B[0,0],B[1,0], head_width=.2, head_length=.2, fc='blue', ec='blue');


#Green arrow representing Second Basis Vector
plt.plot([0,B[0,1]],[0,B[1,1]],color='green'); #Need this line to make the figure work. Not sure why.
ax.arrow(0, 0, B[0,1],B[1,1], head_width=.2, head_length=.2, fc='green', ec='green');

#Original point u as a red dot
ax.scatter(u[0,0],u[1,0], color='red');

plt.show()
#plt.axis('equal');

Notice that the blue arrow represents the first basis vector and the green arrow is the second basis vector in $B$. The solution to $u_B$ shows 2 units along the blue vector and 1 units along the green vector, which puts us at the point (5,3).

This is also called a change in coordinate systems.

**QUESTION**: What is the coordinate vector of $u$ relative to the given basis $B$ in $R^3$?

$$u = (9,-3,21)$$$$B = \{(2,0,-1), (0,1,3), (1,1,1)\}$$

Store this coordinate in a variable ub for checking:

#Put your answer here
from answercheck import checkanswer

checkanswer.vector(ub,'f72f62c739096030e0074c4e1dfca159');
CheckWarning: numpy.matrix is row vector...
    Trying to convert to a column vector using ```A = A.T```.

Testing [[2. 1.]]
Answer seems to be incorrect

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-18-66e556a15463> in <module>
      1 from answercheck import checkanswer
      2 
----> 3 checkanswer.vector(ub,'f72f62c739096030e0074c4e1dfca159');

~/_CMSE314_F20/CMSE314/answercheck.py in vector(A, hashtag, decimal_accuracy)
    104     def vector(A, hashtag=None, decimal_accuracy = 5):
    105         A = checkanswer.make_vector(A, decimal_accuracy)
--> 106         return checkanswer.basic(A, hashtag)
    107 
    108     def eq_vector(A, hashtag=None, decimal_accuracy = 5):

~/_CMSE314_F20/CMSE314/answercheck.py in basic(var, hashtag)
     48             else:
     49                 print("Answer seems to be incorrect\n")
---> 50                 assert checktag == hashtag, f"Answer is incorrect {checktag}"
     51         else:
     52             raise TypeError(f"No answer hastag provided: {checktag}")

AssertionError: Answer is incorrect 6c5899f6950363d955d8d27da3ffec82

Let's look more closely into the matrix $P$, what is the meaning of the columns of the matrix $P$?

We know that $P$ is the inverse of $B$, therefore, we have $$BP=I.$$ Then we can look at the first column of the $P$, say $p_{1}$, we have that $Bp_1$ is the column vector $(1,0,0)^\top$, which is exactly the first component from the standard basis. This is true for other columns.

It means that if we want to change an old basis $B$ to a new basis $B'$, we need to find out all the coordinates in the new basis for the old basis, and the transition matrix is by putting all the coordinates as columns.

Here is the matrix $B$ again:

B = np.matrix([[2,0,-1],[0,1,3],[1,1,1]]).T
sym.Matrix(B)
$$\left[\begin{matrix}2 & 0 & 1\\0 & 1 & 1\\-1 & 3 & 1\end{matrix}\right]$$

The first column of P should be the solution to $Bx=\left[ \begin{matrix} 1 \\ 0 \\ 0 \end{matrix} \right]$. We can use the numpy.linalg.solve function to find this solution:

# The first column of P should be 
u1 = np.matrix([1,0,0]).T
p1 = np.linalg.solve(B,u1)
p1
matrix([[ 0.66666667],
        [ 0.33333333],
        [-0.33333333]])

We can find a similar answer for columns $p_2$ and $p_3$:

# The second column of P should be 
u2 = np.matrix([0,1,0]).T
p2 = np.linalg.solve(B,u2)
p2
matrix([[-1.],
        [-1.],
        [ 2.]])
# The third column of P should be 
u3 = np.matrix([0,0,1]).T
p3 = np.linalg.solve(B,u3)
p3
matrix([[ 0.33333333],
        [ 0.66666667],
        [-0.66666667]])
# concatenate three column together into a 3x3 matrix
P = np.concatenate((p1, p2, p3), axis=1)
sym.Matrix(P)
$$\left[\begin{matrix}0.666666666666667 & -1.0 & 0.333333333333333\\0.333333333333333 & -1.0 & 0.666666666666667\\-0.333333333333333 & 2.0 & -0.666666666666667\end{matrix}\right]$$
# Find the new coordinate in the new basis
u = np.matrix([9,-3,21]).T
UB = P*u
print(UB)
[[ 16.]
 [ 20.]
 [-23.]]

This should be basically the same answer as you got above.


3. Change of Basis

Now consider the following two bases in $R^2$:

$$B_1 = \{(1,2), (3,-1)\}$$$$B_2 = \{(3,1), (5,2)\}$$

The transformation from the "standard basis" to $B_1$ and $B_2$ can be defined as the column vectors $P_1$ and $P_2$ as follows:

B1 = np.matrix([[1,2],[3,-1]]).T
P1 = np.linalg.inv(B1)

sym.Matrix(P1)
$$\left[\begin{matrix}0.142857142857143 & 0.428571428571429\\0.285714285714286 & -0.142857142857143\end{matrix}\right]$$
B2 = np.matrix([[3,1],[5,2]]).T
P2 = np.linalg.inv(B2)

sym.Matrix(P2)
$$\left[\begin{matrix}2.0 & -5.0\\-1.0 & 3.0\end{matrix}\right]$$

**DO THIS**: Find the transition matrix $T$ that will take points in the $B_1$ coordinate representation and put them into $B_2$ coordinates. NOTE this is analogous to the robot kinematics problem. We want to represent points in a different coordinate system.

# Put your answer to the above question here.
from answercheck import checkanswer

checkanswer.matrix(T,'dcc03ddff982e29eea6dd52ec9088986')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-28-419ab6be81a1> in <module>
      1 from answercheck import checkanswer
      2 
----> 3 checkanswer.matrix(T,'dcc03ddff982e29eea6dd52ec9088986')

NameError: name 'T' is not defined

**QUESTION 6**: Given $u_{B_1} = \left[ \begin{matrix} 2 \\ 1 \end{matrix} \right]$ (a point named $u$ in the $B_1$ coordinate system) and your calculated transition matrix $T$, what is the same point expressed in the $B_2$ basis (i.e. what is $u_{B2}$)? Store your answer in a variable named ub2 for checking.

ub1 = np.matrix([[2],[1]])
sym.Matrix(ub1)
$$\left[\begin{matrix}2\\1\end{matrix}\right]$$
##Put your code here
from answercheck import checkanswer

checkanswer.vector(ub2,'9a5fe29254c07cf59ebdffcaba679917')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-31-2a5a9c3bf4dd> in <module>
      1 from answercheck import checkanswer
      2 
----> 3 checkanswer.vector(ub2,'9a5fe29254c07cf59ebdffcaba679917')

NameError: name 'ub2' is not defined

There are three bases $B_1$, $B_2$, and $B_3$. We have the transition matrix $P_{12}$ from $B_1$ to $B_2$ and the transition matrix $P_{23}$ from $B_2$ to $B_3$. In $R^n$, we can compute the transition matrix as $$P_{12}=B_2^{-1}B_1,\quad P_{23}=B_3^{-1}B_2$$

Then we can find all other transition matrices. $$P_{13} = B_3^{-1}B_1=B_3^{-1}B_2*B_2^{-1}B_1= P_{23}P_{12}$$ $$P_{21} = B_1^{-1}B_2 = (B_2^{-1}B_1)^{-1}=P_{12}^{-1}$$ $$P_{32} = B_2^{-1}B_3 = (B_3^{-1}B_2)^{-1}=P_{23}^{-1}$$ $$P_{31} = B_1^{-1}B_3 = (B_3^{-1}B_1)^{-1}=P_{13}^{-1}=(P_{23}P_{12})^{-1}=P_{12}^{-1}P_{23}^{-1}$$

The result is true for general vector spaces and can be extended to many bases.


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:** There is no Assignment specific question for this notebook. You can just say "none".

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 Dirk Colbry, Michigan State University Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.