Skip to main content

MATH 345: Linear Algebra and Optimization

Section 1.6 Exercises

Exercise tags. The tags below identify the primary outcomes and competency types for each exercise group. Individual problems may also involve earlier outcomes.

Subsection Matrices

Primary outcomes. U1-LO3. Competencies. P.
  • Nicholson 2.1.3(f)
     1 
    math.libretexts.org/Bookshelves/Linear_Algebra/Linear_Algebra_with_Applications_(Nicholson)/02%3A_Matrix_Algebra/2.01%3A_Matrix_Addition_Scalar_Multiplication_and_Transposition/2.1E%3A_Matrix_Addition_Scalar_Multiplication_and_Transposition_Exercises
  • Nicholson 2.1.8(b)
     2 
    math.libretexts.org/Bookshelves/Linear_Algebra/Linear_Algebra_with_Applications_(Nicholson)/02%3A_Matrix_Algebra/2.01%3A_Matrix_Addition_Scalar_Multiplication_and_Transposition/2.1E%3A_Matrix_Addition_Scalar_Multiplication_and_Transposition_Exercises
  • Nicholson 2.1.15(b)
     3 
    math.libretexts.org/Bookshelves/Linear_Algebra/Linear_Algebra_with_Applications_(Nicholson)/02%3A_Matrix_Algebra/2.01%3A_Matrix_Addition_Scalar_Multiplication_and_Transposition/2.1E%3A_Matrix_Addition_Scalar_Multiplication_and_Transposition_Exercises

Subsection Matrix-vector multiplication

Primary outcomes. U1-LO4. Competencies. P+R.
  • Nicholson 2.2.2(b)
     4 
    math.libretexts.org/Bookshelves/Linear_Algebra/Linear_Algebra_with_Applications_(Nicholson)/02%3A_Matrix_Algebra/2.02%3A_Equations_Matricies_and_Transformations/2.2E%3A_Matrix-Vector_Multiplication_Exercises
  • Nicholson 2.2.3(b)
     5 
    math.libretexts.org/Bookshelves/Linear_Algebra/Linear_Algebra_with_Applications_(Nicholson)/02%3A_Matrix_Algebra/2.02%3A_Equations_Matricies_and_Transformations/2.2E%3A_Matrix-Vector_Multiplication_Exercises
  • Nicholson 2.2.11(d)
     6 
    math.libretexts.org/Bookshelves/Linear_Algebra/Linear_Algebra_with_Applications_(Nicholson)/02%3A_Matrix_Algebra/2.02%3A_Equations_Matricies_and_Transformations/2.2E%3A_Matrix-Vector_Multiplication_Exercises

Subsection Linear and affine maps

Primary outcomes. U1-LO5, U1-LO7. Competencies. P+C+R.
  • Nicholson 2.6.2(b)
     7 
    math.libretexts.org/Bookshelves/Linear_Algebra/Linear_Algebra_with_Applications_(Nicholson)/02%3A_Matrix_Algebra/2.06%3A_Linear_Transformations/2.6E%3A_Linear_Transformations_Exercises
  • Nicholson 2.6.13(b)
     8 
    math.libretexts.org/Bookshelves/Linear_Algebra/Linear_Algebra_with_Applications_(Nicholson)/02%3A_Matrix_Algebra/2.06%3A_Linear_Transformations/2.6E%3A_Linear_Transformations_Exercises

Subsection Matrix multiplication

Primary outcomes. U1-LO6. Competencies. P+R.
  • Nicholson 2.3.1(b)
     9 
    math.libretexts.org/Bookshelves/Linear_Algebra/Linear_Algebra_with_Applications_(Nicholson)/02%3A_Matrix_Algebra/2.03%3A_Matrix_Multiplication/2.3E%3A_Matrix_Multiplication_Exercises
  • Nicholson 2.3.3(b)
     10 
    math.libretexts.org/Bookshelves/Linear_Algebra/Linear_Algebra_with_Applications_(Nicholson)/02%3A_Matrix_Algebra/2.03%3A_Matrix_Multiplication/2.3E%3A_Matrix_Multiplication_Exercises
  • Nicholson 2.3.5(b)
     11 
    math.libretexts.org/Bookshelves/Linear_Algebra/Linear_Algebra_with_Applications_(Nicholson)/02%3A_Matrix_Algebra/2.03%3A_Matrix_Multiplication/2.3E%3A_Matrix_Multiplication_Exercises
  • Nicholson 2.3.16(b)
     12 
    math.libretexts.org/Bookshelves/Linear_Algebra/Linear_Algebra_with_Applications_(Nicholson)/02%3A_Matrix_Algebra/2.03%3A_Matrix_Multiplication/2.3E%3A_Matrix_Multiplication_Exercises

Subsection Additional applied and computational problems

These exercises connect the main Unit 1 ideas: vectors as data, cosine similarity, weighted averages, matrix-vector products, geometric matrix actions, composition, affine maps, and code interpretation. Solutions are collected in Appendix C.1.

Checkpoint 1.6.1. Word-count vectors and cosine similarity.

Use the dictionary linear, matrix, data. Let
\begin{equation*} \mathbf{q}=\begin{bmatrix}1\\0\\1\end{bmatrix}, \end{equation*}
\begin{equation*} \mathbf{D}_1=\begin{bmatrix}2\\0\\2\end{bmatrix},\qquad \mathbf{D}_2=\begin{bmatrix}1\\1\\0\end{bmatrix},\qquad \mathbf{D}_3=\begin{bmatrix}0\\2\\0\end{bmatrix}. \end{equation*}
  1. Compute the cosine similarity between \(\mathbf{q}\) and each document vector.
  2. Rank the documents by cosine similarity.
  3. Explain why \(\mathbf{D}_1\) has cosine similarity \(1\) with \(\mathbf{q}\text{.}\)
  4. Which document is orthogonal to \(\mathbf{q}\text{?}\)
  5. What does that orthogonality mean in terms of the dictionary words?
  6. Compute the Euclidean distance from \(\mathbf{q}\) to each \(\mathbf{D}_i\text{.}\) Does the distance ranking match the cosine ranking?
Tags. [U1-LO1, U1-LO2 | C+R | Core]

Checkpoint 1.6.2. Weighted average of value vectors.

Let
\begin{equation*} \mathbf{v}_1=\begin{bmatrix}10\\0\end{bmatrix},\qquad \mathbf{v}_2=\begin{bmatrix}0\\10\end{bmatrix},\qquad \mathbf{v}_3=\begin{bmatrix}10\\10\end{bmatrix}, \end{equation*}
and let
\begin{equation*} \boldsymbol{\alpha}=\begin{bmatrix}\frac14\\\frac14\\\frac12\end{bmatrix}. \end{equation*}
  1. Compute \(\alpha_1\mathbf{v}_1+\alpha_2\mathbf{v}_2+\alpha_3\mathbf{v}_3\text{.}\)
  2. Explain why this is a weighted average.
  3. Which value vector receives the largest weight?
Tags. [U1-LO8 | P+C | Core]

Checkpoint 1.6.3. Attention bridge: hand scores versus \(K\mathbf{q}\).

Use the token attention activity with tokens β€œsmall”, β€œred”, β€œbird”. Let
\begin{equation*} \mathbf{q}=\begin{bmatrix}1\\1\end{bmatrix}, \end{equation*}
\begin{equation*} \mathbf{k}_{\mathrm{small}}=\begin{bmatrix}1\\0\end{bmatrix},\qquad \mathbf{k}_{\mathrm{red}}=\begin{bmatrix}0\\1\end{bmatrix},\qquad \mathbf{k}_{\mathrm{bird}}=\begin{bmatrix}1\\1\end{bmatrix}. \end{equation*}
  1. Compute the three dot products
    \begin{equation*} \mathbf{q}\cdot \mathbf{k}_{\mathrm{small}},\qquad \mathbf{q}\cdot \mathbf{k}_{\mathrm{red}},\qquad \mathbf{q}\cdot \mathbf{k}_{\mathrm{bird}}. \end{equation*}
  2. Put the key vectors as rows of the matrix
    \begin{equation*} K=\begin{bmatrix}1\amp 0\\ 0\amp 1\\ 1\amp 1\end{bmatrix}. \end{equation*}
    Compute \(K\mathbf{q}\text{.}\)
  3. Explain why the entries of \(K\mathbf{q}\) are the same scores from part (a).
  4. Normalize the scores by dividing by their sum. What is the weight vector \(\boldsymbol{\alpha}\text{?}\)
Tags. [U1-LO1, U1-LO4, U1-LO8 | C+R | Core]

Checkpoint 1.6.4. Rows measure; columns contribute.

Let
\begin{equation*} A=\begin{bmatrix}-1\amp 4\amp -5\\ 3\amp 1\amp -2\end{bmatrix}, \qquad \mathbf{x}=\begin{bmatrix}2\\ -3\\ 4\end{bmatrix}. \end{equation*}
  1. Compute \(A\mathbf{x}\) using row dot products.
  2. Compute \(A\mathbf{x}\) as a linear combination of the columns of \(A\text{.}\)
  3. In this example, what does the first row of \(A\) measure?
  4. In this example, what do the coordinates \(2,-3,4\) tell you in the column-combination view?
Tags. [U1-LO4 | P+R | Core]

Checkpoint 1.6.5. The unit-square visualization.

Let
\begin{equation*} A=\begin{bmatrix}2\amp -1\\ 1\amp 1\end{bmatrix}. \end{equation*}
  1. Compute \(A\mathbf{e}_1\) and \(A\mathbf{e}_2\text{.}\)
  2. Compute \(A\begin{bmatrix}1\\1\end{bmatrix}\text{.}\)
  3. Apply \(A\) to the four corners of the unit square:
    \begin{equation*} \begin{bmatrix}0\\0\end{bmatrix},\qquad \begin{bmatrix}1\\0\end{bmatrix},\qquad \begin{bmatrix}1\\1\end{bmatrix},\qquad \begin{bmatrix}0\\1\end{bmatrix}. \end{equation*}
  4. Sketch the image of the unit square.
  5. Which two transformed edges leaving the origin are the columns of \(A\text{?}\)
Tags. [U1-LO5, U1-LO8 | C+R | Core]

Checkpoint 1.6.6. Transformation matching.

Match each matrix with the correct geometric description.
\begin{equation*} A_1=\begin{bmatrix}1\amp 1\\ 0\amp 1\end{bmatrix},\qquad A_2=\begin{bmatrix}0\amp -1\\ 1\amp 0\end{bmatrix},\qquad A_3=\begin{bmatrix}1\amp 0\\ 0\amp 0\end{bmatrix}, \end{equation*}
\begin{equation*} A_4=\begin{bmatrix}-1\amp 0\\ 0\amp 1\end{bmatrix},\qquad A_5=\begin{bmatrix}2\amp 0\\ 0\amp 1\end{bmatrix}. \end{equation*}
  1. Reflection across the \(y\)-axis.
  2. Projection onto the \(x\)-axis.
  3. Horizontal stretch.
  4. Horizontal shear.
  5. \(90^\circ\) counterclockwise rotation.
For each match, briefly justify your answer by describing what happens to \(\mathbf{e}_1\) and \(\mathbf{e}_2\text{.}\)
Tags. [U1-LO5 | C+R | Core]

Checkpoint 1.6.7. Composition order: \(RS\) versus \(SR\).

Let
\begin{equation*} S=\begin{bmatrix}2\amp 0\\ 0\amp 1\end{bmatrix}, \qquad R=\begin{bmatrix}0\amp -1\\ 1\amp 0\end{bmatrix}. \end{equation*}
  1. Compute \(RS\text{.}\)
  2. Compute \(SR\text{.}\)
  3. Are \(RS\) and \(SR\) equal?
  4. Which product represents β€œapply \(S\) first, then apply \(R\)”?
  5. Explain geometrically why the two orders produce different results.
Tags. [U1-LO6 | P+R | Core]

Checkpoint 1.6.8. Affine but not linear.

Consider the transformation
\begin{equation*} T\begin{bmatrix}x\\y\end{bmatrix}=\begin{bmatrix}x+1\\y\end{bmatrix}. \end{equation*}
  1. Compute \(T(0,0)\text{.}\)
  2. Explain why \(T\) cannot be written in the form \(T(\mathbf{x})=A\mathbf{x}\) for any matrix \(A\text{.}\)
  3. Write \(T\) in the affine form
    \begin{equation*} T(\mathbf{x})=A\mathbf{x}+\mathbf{b}. \end{equation*}
  4. Is \(T\) linear, affine but not linear, or neither?
Tags. [U1-LO7 | C+R | Core]

Checkpoint 1.6.9. Code interpretation: sorting similarity scores.

Consider the code:
import numpy as np

doc_names = np.array(["D1", "D2", "D3"])
scores = np.array([1.0, 0.5, 0.0])
ranking = doc_names[np.argsort(scores)[::-1]]
ranking
  1. What is the value of ranking?
  2. What does np.argsort(scores) return?
  3. What does [::-1] do?
  4. What is being ranked?
  5. Is this ranking based on direction, distance, or alphabetical order?
Tags. [U1-LO2, U1-LO8 | R+T | Core]

Checkpoint 1.6.10. Transformer shape check.

Suppose a sequence of token vectors is stored in a matrix
\begin{equation*} X\in \mathbb{R}^{6\times 5}. \end{equation*}
Let
\begin{equation*} W_Q,W_K,W_V\in \mathbb{R}^{5\times 2}, \end{equation*}
and define
\begin{equation*} Q=XW_Q,\qquad K=XW_K,\qquad V=XW_V,\qquad S=QK^T. \end{equation*}
A later weighting step produces an attention-weight matrix \(A_{\mathrm{att}}\text{,}\) and the attention output is
\begin{equation*} H=A_{\mathrm{att}}V. \end{equation*}
  1. What are the shapes of \(Q\text{,}\) \(K\text{,}\) and \(V\text{?}\)
  2. What is the shape of \(K^T\text{?}\)
  3. What is the shape of \(S=QK^T\text{?}\)
  4. What is the shape of \(A_{\mathrm{att}}\text{?}\)
  5. What is the shape of \(H\text{?}\)
  6. What does the \((i,j)\)-entry of \(S\) compute?
  7. Which product forms the weighted averages of value vectors?
  8. In this convention, are token vectors stored as rows or columns of \(X\text{?}\)
Tags. [U1-LO3, U1-LO6, U1-LO8 | R+T | Core]