Each lab worksheet will contain several activities, most of which will involve writing math on paper, and some of which will involve running code in a Jupyter Notebook. Lab activities are meant to last an hour, and the second hour of lab is dedicated to starting the homework assignment. To receive credit for a lab, you must show your lab TA your work on both the lab worksheet and homework assignment.
While you must get checked off by your lab TA individually, we encourage you to form groups with 1-2 other students to complete the activities together.
This content is covered in Chapter 3.1 and Chapter 3.2 of the course notes, which are now posted. Have these open while working on the worksheet. Homework 5 will be released on Friday.
Activity 1: Is the expression valid?
Suppose:
\(A\in\mathbb R^{5\times3}\).
\(B\in\mathbb R^{3\times4}\).
\(\vec u\in\mathbb R^4\).
\(\vec v\in\mathbb R^3\).
Draw a circle around each valid expression and cross out each invalid expression. An expression is valid if the shapes allow the operation shown. Since you donβt actually know \(A\), \(B\), \(\vec u\), and \(\vec v\), you canβt actually do the calculation.
\(A\vec v\)
\(\vec v A\)
\(B\vec u\)
\(B\vec v\)
\(B\vec u+\vec v\)
\(A\vec v+\vec u\)
\(\vec u\cdot\vec v\)
\(\vec v\cdot\vec v\)
\((B\vec u)\cdot\vec v\)
\((A\vec v)\cdot(B\vec u)\)
\(\|A\vec v\|^2\)
\(\|\vec u+\vec v\|\)
\(A(B\vec u)\)
\(B\vec u-3\vec v\)
\(\vec u+2\vec u\)
\(\|B\vec u\|^2+\|\vec v\|^2\)
Compare with your group. Explain any disagreements using the shapes.
Solution
Here \(A\) has shape \(5\times3\), \(B\) has shape \(3\times4\), \(\vec u\) has shape \(4\times1\) (it is a vector in \(\mathbb R^4\)), and \(\vec v\) has shape \(3\times1\) (it is a vector in \(\mathbb R^3\)). Thus \(A\vec v\) has shape \(5\times1\) (it is a vector in \(\mathbb R^5\)) and \(B\vec u\) has shape \(3\times1\) (it is a vector in \(\mathbb R^3\)).
Expression
Valid?
Reason or shape of the result
\(A\vec v\)
Valid
\(A\) has 3 columns and \(\vec v\) has 3 entries; result \(5\times1\) (it is a vector in \(\mathbb R^5\)).
\(\vec v A\)
Invalid
\(\vec v\) has shape \(3\times1\) (it is a vector in \(\mathbb R^3\)). The inner numbers in \((3\times1)(5\times3)\) do not match: \(1\ne5\).
\(B\vec u\)
Valid
\(B\) has 4 columns and \(\vec u\) has 4 entries; result \(3\times1\) (it is a vector in \(\mathbb R^3\)).
\(B\vec v\)
Invalid
\(B\) needs 4 entries, but \(\vec v\) has 3.
\(B\vec u+\vec v\)
Valid
Each vector has shape \(3\times1\) (it is a vector in \(\mathbb R^3\)).
\(A\vec v+\vec u\)
Invalid
The shapes \(5\times1\) (it is a vector in \(\mathbb R^5\)) and \(4\times1\) (it is a vector in \(\mathbb R^4\)) differ.
\(\vec u\cdot\vec v\)
Invalid
A dot product needs the same number of entries: \(4\ne3\).
\(\vec v\cdot\vec v\)
Valid
Both have 3 entries; the result is a scalar.
\((B\vec u)\cdot\vec v\)
Valid
Both have 3 entries; the result is a scalar.
\((A\vec v)\cdot(B\vec u)\)
Invalid
The vectors have 5 and 3 entries.
\(\|A\vec v\|^2\)
Valid
\(A\vec v\) exists; its squared length is a scalar.
\(\|\vec u+\vec v\|\)
Invalid
The sum inside is not defined.
\(A(B\vec u)\)
Valid
\(B\vec u\) has 3 entries, as \(A\) needs; result \(5\times1\) (it is a vector in \(\mathbb R^5\)).
\(B\vec u-3\vec v\)
Valid
Each vector has shape \(3\times1\) (it is a vector in \(\mathbb R^3\)).
\(\vec u+2\vec u\)
Valid
Each vector has shape \(4\times1\) (it is a vector in \(\mathbb R^4\)).
\(\|B\vec u\|^2+\|\vec v\|^2\)
Valid
Both squared lengths are scalars, so they can be added.
Compute \(A\vec v\). What are the shapes of \(\vec v\) and \(A\vec v\)?
Solution
Using a dot product with each row,
$$ A\vec v =\begin{bmatrix}4(-2)+(-3)(3)+6(4)\\(-2)(-2)+5(3)+1(4)\end{bmatrix} =\boxed{\begin{bmatrix}7\\23\end{bmatrix}}. $$
The shape of \(\vec v\) is \(3\times1\) (it is a vector in \(\mathbb R^3\)); the shape of \(A\vec v\) is \(2\times1\) (it is a vector in \(\mathbb R^2\)).
Its shape is \(3\times1\) (it is a vector in \(\mathbb R^3\)).
c)
Is \(B\vec v\) defined? Explain. Explain why \(B(A\vec v)\) can be defined even when \(B\vec v\) is not.
Solution
\(B\vec v\) is not defined: \(B\) has 2 columns, but \(\vec v\) has 3 entries. However, \(A\vec v\) has 2 entries. Therefore it has the shape needed to multiply by \(B\). We compute \(A\vec v\) first and then apply \(B\).
\(S\) moves the first entry to the bottom and moves the other two up one position. \(D\) multiplies the first entry by \(5\), the second by \(-2\), and the third by \(1/3\).
Why are \(S(D\vec x)\) and \(D(S\vec x)\) different? Explain using what \(S\) and \(D\) do to the entries of a vector.
Solution
The scaling factor depends on an entryβs position. In \(S(D\vec x)\), the original second entry, \(7\), is first multiplied by \(-2\) and then moved to the first position, giving \(-14\). In \(D(S\vec x)\), it is first moved to the first position and then multiplied by \(5\), giving \(35\). The same change of position changes which factor is applied to the other entries, too.
Activity 4: Mystery matrices
There is a matrix \(A\), but we have not been given its entries.
Multiplication by \(\begin{bmatrix}1\\0\end{bmatrix}\) takes one copy of the first column and zero copies of the second column. Therefore the first column of \(A\) is \(\begin{bmatrix}5\\-2\\12\end{bmatrix}\). The given input and output also show that \(A\) has shape \(3\times2\). Its second column is not yet determined.
The coefficients \(-2\), \(1\), and \(3\) are the entries of the input vector. Thus the three known outputs give exactly the column vectors needed for this calculation.
$$ \operatorname{proj}_{\ell}(\vec v) =(\vec v\cdot\vec w)\vec w =5\begin{bmatrix}3/5\\4/5\end{bmatrix} =\boxed{\begin{bmatrix}3\\4\end{bmatrix}}. $$
b)
We can also project onto \(\ell\) by multiplying by a matrix. A projection matrix\(P\) satisfies \(P\vec v=\operatorname{proj}_{\ell}(\vec v)\) for every input \(\vec v\). If a unit vector \(\vec w=\begin{bmatrix}w_1\\w_2\end{bmatrix}\) spans \(\ell\), then
$$ P\vec v =\begin{bmatrix}(9/25)7+(12/25)1\\(12/25)7+(16/25)1\end{bmatrix} =\begin{bmatrix}75/25\\100/25\end{bmatrix} =\boxed{\begin{bmatrix}3\\4\end{bmatrix}}. $$
This agrees with part (a). Both columns of \(P\) lie on \(\ell\):
$$ \begin{bmatrix}9/25\\12/25\end{bmatrix}=\frac35\vec w, \qquad \begin{bmatrix}12/25\\16/25\end{bmatrix}=\frac45\vec w. $$
Every product \(P\vec z\) is a combination of these columns, so it is also a multiple of \(\vec w\) and lies on \(\ell\).
Activity 5: Drawing projections (continued)
c)
Use \(P\) to find the projection onto \(\ell\) of \(\vec u=\begin{bmatrix}-4\\5\end{bmatrix}\). On the coordinate axes below, draw \(\ell\) and the four vectors \(\vec u\), \(\vec v\), \(P\vec u\), and \(P\vec v\), all starting at the origin. What do you notice?
In the sketch, both \(P\vec u\) and \(P\vec v\) lie on \(\ell\). The dashed segments from each original tip to its projected tip are perpendicular to \(\ell\).
This lab has a programming notebook to help you visualize various matrix-vector operations, and to show you how matrix-vector multiplication works in Python.
To open the notebook for Lab 6, click the Google Colab link under βCodeβ on the course website for Lab 6. Instructions on how to use Google Colab are at math124.org/running-code. If you are viewing the lab worksheet after it has been posted, this direct link will work, too. To receive credit for the programming component of the lab, work through the entire notebook and show your lab TA your work.