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 Mechanics

In this assignment, we will explore the mechanics of vectors and matrices. These mechanics will be needed in future assignments. Make sure you understand and come to class with any questions.


1. Dot Product Review

We covered inner products a while ago. This assignment will extend the idea of inner products to matrix multiplication. As a reminder, Sections 1.4 of the Stephen Boyd and Lieven Vandenberghe Applied Linear algebra book covers the dot product. Here is a quick review:

from IPython.display import YouTubeVideo
YouTubeVideo("ZZjWqxKqJwQ",width=640,height=360, cc_load_policy=True)

Given two vectors $u$ and $v$ in $R^n$ (i.e. they have the same length), the "dot" product operation multiplies all of the corresponding elements and then adds them together. Ex:

$$u = [u_1, u_2, \dots, u_n]$$$$v = [v_1, v_2, \dots, v_n]$$$$u \cdot v = u_1 v_1 + u_2 v_2 + \dots + u_nv_n$$

or:

$$ u \cdot v = \sum^n_{i=1} u_i v_i$$

This can easily be written as python code as follows:

u = [1,2,3]
v = [3,2,1]
solution = 0
for i in range(len(u)):
    solution += u[i]*v[i]
    
solution
10

In numpy the dot product between two vectors can be calculated using the following built in function:

import numpy as np
np.dot([1,2,3], [3,2,1])
10

**QUESTION:** What is the dot product of any vector and the zero vector?

Put your answer here

**QUESTION:** What happens to the numpy.dot function if the two input vectors are not the same size?

Put your answer here


2. Matrix Multiply

Read sections Sections 10.1 of the Stephen Boyd and Lieven Vandenberghe Applied Linear algebra book covers Maatirx Multiplication. Here is a quick review:

Two matrices $A$ and $B$ can be multiplied together if and only if their "inner dimensions" are the same, i.e. $A$ is $n\times d$ and $B$ is $d\times m$ (note that the columns of $A$ and the rows of $B$ are both $d$). Multiplication of these two matrices results in a third matrix $C$ with the dimension of $n\times m$. Note that $C$ has the same first dimension as $A$ and the same second dimension as $B$. i.e $n\times m$.

The $(i,j)$ element in $C$ is the dot product of the $i$th row of $A$ and the $j$th column of $B$.

The $i$th row of $A$ is:

$$ [ a_{i1}, a_{i2}, \dots , a_{id} ],$$

and the $j$th column of $B$ is:

$$ \left[ \begin{matrix} b_{1j}\\ b_{2j}\\ \vdots \\ b_{dj} \end{matrix} \right] $$

So, the dot product of these two vectors is:

$$c_{ij} = a_{i1}b_{1j} + a_{i2}b_{2j} + \dots + a_{id}b_{dj}$$

Consider the simple $2\times 2$ example below:

$$ \left[ \begin{matrix} a & b\\ c & d \end{matrix} \right] \left[ \begin{matrix} w & x\\ y & z \end{matrix} \right] = \left[ \begin{matrix} aw+by & ax+bz\\ cw + dy & cx + dz \end{matrix} \right] $$

Let's do an example using numpy and show the results using sympy:

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,1], [2,2]])
sym.Matrix(A)
$$\left[\begin{matrix}1 & 1\\2 & 2\end{matrix}\right]$$
B = np.matrix([[3,4], [3,4]])
sym.Matrix(B)
$$\left[\begin{matrix}3 & 4\\3 & 4\end{matrix}\right]$$
sym.Matrix(A*B)
$$\left[\begin{matrix}6 & 8\\12 & 16\end{matrix}\right]$$

**DO THIS:** Given two matrices; $A$ and $B$, show that order matters when doing a matrix multiply. That is, in general, $AB \neq BA$. Show this with an example using two $3\times 3$ matrices and numpy.

# Put your code here.

Now consider the following set of linear equations:

$$ 3x_1-3x_2+9x_3 =~24$$$$ 2x_1-2x_2+7x_3 =~17$$$$ -x_1+2x_2-4x_3 = -11$$

We typically write this in the following form:

$$ \left[ \begin{matrix} 3 & -3 & 9\\ 2 & -2 & 7 \\ -1 & 2 & -4 \end{matrix} \right] \left[ \begin{matrix} x_1 \\ x_2 \\ x_3 \end{matrix} \right] = \left[ \begin{matrix} 24\\ 17 \\ -11 \end{matrix} \right] $$

Notice how doing the matrix multiplication results back into the original system of equations. If we rename the three matrices from above to $A$, $x$, and $b$ (note $x$ and $b$ are lowercase because they are column vectors) then we get the main equation for this class, which is:

$$Ax=b$$

Note the information about the equation doesn't change when you change formats. For example, the equation format, the augmented format and the $Ax=b$ format contain the same information. However, we use the different formats for different applications. Consider the numpy.linalg.solve function which assumes the format $Ax=b$

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]$$
b = np.matrix([[24], [17], [-11]])
sym.Matrix(b)
$$\left[\begin{matrix}24\\17\\-11\end{matrix}\right]$$
#Calculate answer to x using numpy
x = np.linalg.solve(A,b)
sym.Matrix(x)
$$\left[\begin{matrix}3.0\\-2.0\\1.0\end{matrix}\right]$$

**QUESTION:** What is the size of the matrix resulting from multiplying a $10 \times 40$ matrix with a $40 \times 3$ matrix?

Put your answer here


3. Identity Matrix

Read sections Sections 6.2 and 6.3 of the Stephen Boyd and Lieven Vandenberghe Applied Linear algebra book covers more about matrixes.

An identity matrix is a special square matrix (i.e. $n=m$) that has ones in the diagonal and zeros other places. For example the following is a $3\times 3$ identity matrix:

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

We always denote the identity matrix with a capital $I$. Often a subscript is used to denote the value of $n$. The notations $I_{nxn}$ and $I_n$ are both acceptable.

An identity matrix is similar to the number 1 for scalar values. I.e. multiplying a square matrix $A_{nxn}$ by its corresponding identity matrix $I_{nxn}$ results in itself $A_{nxn}$.

**DO THIS:** Pick a random $3\times 3$ matrix and multiply it by the $3\times 3$ Identity matrix and show you get the same answer.

#Put your code here

**QUESTION:** Consider two square matrices $A$ and $B$ of size $n \times n$. $AB = BA$ is NOT true for many $A$ and $B$. Describe an example where $AB = BA$ is true? Explain why the equality works for your example.

Put your answer here

**QUESTION:** The following matrix is symmetric. What are the values for $a$, $b$, and $c$? (HINT you may want to look online or in the Boyd book for a definition of matrix symmetry)

$$ \left[ \begin{matrix} 3 & 5 & a\\ b & 8 & 4 \\ -3 & c & 3 \end{matrix} \right] $$

Put your answer here:

a =

b =

c =


4. Vector spaces in $R^n$

There are two properties that define a vector space these are:

  • Closed under addition
  • Closed under scalar multiplication

For now we will consider vector spaces in $R^n$ which are just vectors of real numbers (ex: [10,20,3.2], [5,8,32], [8,-0.7], etc) where $n$ is just the length of the vector (ex: 3, 3, and 2 in the earlier example). In the general case a vector does not have to be composed of real numbers but can be almost any type of object as long as it maintains the two above properties, we will get into this concept later in the semester. In the case of real number the above concepts can be described as follows:

  • Closed under addition means that if we add any two Real vectors vectors (i.e. $u,v \in R^n$) then the result is also in $R^n$). This is easy to understand if you think about adding any two real vectors there is no way to get a result that is not also a real vector. A way to say this mathematically is as follows:
$$\text{if } u,v \in R^n$$$$\text{then } u+v \in R^n$$
  • Closed under scalar multiplication means that if we have any scalar number ($s \in R$) and we multiply it by a real vector ($v \in R^n$) then the result is also a vector in $R^n$. Since multiplying a real number by a real number results in a real number this one is also true. Or we can say it as follows:
$$\text{if } s \in R \text{ and } v \in R^n$$$$\text{then } sv \in R^n$$

The following are some properties of vector addition and multiplication for vectors $u$ and $v$:

  1. $u + v = v + u$ Commutative property
  2. $u + (v + w) = (u + v) + w$ Associative property
  3. $u+0 = 0 + u = u$ Property of zero vector
  4. $u + (-u) = 0$ Property of the negative vector
  5. $c(u+v) = cu + cv$ Distributive properties
  6. $(c+d)u = cu+du$ Distributive Properties
  7. $c(du) = (cd)u$ Distributed Properties
  8. $1u = u$ Scalar multiplication by 1

**QUESTION:** Compute the following linear combinations for $u = (1,2), v = (4,-1)$, and $w = (-3,5)$.

**(a)** $a = u+w$

Put your answer here

from answercheck import checkanswer

checkanswer.vector(a,'af464d466ae982f2cd4461af494e86d6');
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-13-316ffc36ab02> in <module>
      1 from answercheck import checkanswer
      2 
----> 3 checkanswer.vector(a,'af464d466ae982f2cd4461af494e86d6');

NameError: name 'a' is not defined

**(b)** $a = 2u+v$

Put your answer here

from answercheck import checkanswer

checkanswer.vector(a,'393468eff8c6ba5d27b7d0aa1b18f929');
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-14-039185e8e2ef> in <module>
      1 from answercheck import checkanswer
      2 
----> 3 checkanswer.vector(a,'393468eff8c6ba5d27b7d0aa1b18f929');

NameError: name 'a' is not defined

**(c)** $a = u+3w$

Put your answer here

from answercheck import checkanswer

checkanswer.vector(a,'d5e5ca43a86501bcde09b1cbc0ba49b5');
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-15-7e0a200341d4> in <module>
      1 from answercheck import checkanswer
      2 
----> 3 checkanswer.vector(a,'d5e5ca43a86501bcde09b1cbc0ba49b5');

NameError: name 'a' is not defined

5. 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:** In the symmetric matrix shown above, what are the values for a , b , and c ?

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