06 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.

Goals for today’s pre-class assignment#

  1. Dot Product Review

  2. Matrix Multiply

  3. Identity Matrix

  4. Elementary Matrices

  5. Solving Many Systems (at the same time)


1. Dot Product Review#

We covered dot (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

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])

QUESTION: What is the dot product of any n-vector and the zero n-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 Section 10.1 of the Stephen Boyd and Lieven Vandenberghe Applied Linear algebra book which covers Matrix 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:

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

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:

\[\begin{split} \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] \end{split}\]

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)
B = np.matrix([[3,4], [3,4]])
sym.Matrix(B)
sym.Matrix(A*B)

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:

\[\begin{split} \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] \end{split}\]

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)
b = np.matrix([[24], [17], [-11]])
sym.Matrix(b)
#Calculate answer to x using numpy
x = np.linalg.solve(A,b)
sym.Matrix(x)

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 the \(3\times 3\) identity matrix:

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

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}\) (either on the left or on the right) results in itself \(A_{nxn}\).

DO THIS: Pick a random \(3\times 3\) matrix (you can define it using np.matrix(np.random.random([3,3]))), \(A\), and multiply it by the \(3\times 3\) Identity matrix (once on the left and once on the right) and show you get \(A\) in either case, i.e. demonstrate that \(A I_3 =A\) and \(I_3 A=A\).

#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 (different from the one above) where \(AB = BA\) is true. Explain why the equality works for your example.

Put your answer here


4. 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:

\[\begin{split} E_{12}= \left[ \begin{matrix} 0 & 1 & 0\\ 1 & 0 & 0 \\ 0 & 0 & 1 \end{matrix} \right] \end{split}\]

DO THIS: Run the three cells below. You should observe that applying the matrix E1 to A resulted in a matrix A1 that looks just like A, except the first and second rows were swapped.

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)
E1 = np.matrix([[0,1,0], [1,0,0], [0,0,1]])
sym.Matrix(E1)
A1 = E1*A
sym.Matrix(A1)

DO THIS: Define a \(3\times 3\) elementary matrix named E2 that swaps row 3 with row 1. Apply it to the matrix A, and store the result as a new variable named A2.

# Put your answer here.  
E2 = 
A2 = E2*A
sym.Matrix(A2)
from answercheck import checkanswer
checkanswer.matrix(E2,'2c2d2e407389eabeb6d90894565c830f');

DO THIS: Give a \(3\times 3\) elementary matrix named E3 that adds \(3\) times the first row to the third row. Apply this elementary matrix to the matrix A2, and store the result as a new variable A3.

# Put your answer here.  
E3 = 
A3 = E3*A2
sym.Matrix(A3)
from answercheck import checkanswer
checkanswer.matrix(E3,'55ae1f9eb21df00c59dad623b9471506');

DO THIS: Give a \(3\times 3\) elementary matrix named E4 that multiplies the second row by \(1/2\). Apply this to matrix A3, and store the result as a new variable A4.

# Put your answer here.  
E4 = 
A4 = E4*A3
sym.Matrix(A4)
from answercheck import checkanswer
checkanswer.matrix(E4,'3a5256840ef907a1b73ebba4471ac26d');

If the above are correct then we can combine the three operators E2, E3, and E4 on the original matrix A to get A4 as follows.

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

sym.Matrix(E4*E3*E2*A)

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.

###STARTFOOTER###


Congratulations, we’re done!#