Chapter 8 Exercises — Matrix Operations

Work the starred tiers in order; each builds on the last. ⭐ are conceptual warm-ups, ⭐⭐ are hand computations, ⭐⭐⭐ are proofs (mark [proof]) or code (mark [code]), and ⭐⭐⭐⭐ are open-ended applications. Every numeric answer can be checked against numpy with @, .T, and np.linalg.matrix_power; do the hand work first, then verify.

Throughout, let $$A = \begin{bmatrix} 2 & 1 \\ 0 & 3 \end{bmatrix}, \quad B = \begin{bmatrix} 1 & 4 \\ 2 & 5 \end{bmatrix}, \quad R = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix}\ (\text{rotate } 90°), \quad S = \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix}\ (\text{shear}).$$


Tier 1 — Conceptual (⭐)

8.1 ⭐ In one sentence each, explain what $A + B$ and what $AB$ mean as combinations of transformations. Which one is "parallel" and which is "serial"?

8.2 ⭐ True or false, with a one-line reason: matrix multiplication is commutative.

8.3 ⭐ The product $AB$ means "do $B$, then do $A$." Which matrix acts first? Explain by analogy to the function notation $f(g(x))$.

8.4 ⭐ You are told $A$ is $3\times 4$ and $B$ is $4\times 2$. Is $AB$ defined? Is $BA$ defined? Give the shape of each product that exists, and state the inner-dimension rule you used.

8.5 ⭐ Your classmate computes a "matrix product" by multiplying entries in matching positions: the $(1,1)$ entry of the answer is $a_{11}b_{11}$. What operation did they actually compute, and why is it not matrix multiplication?

8.6 ⭐ What does the identity matrix $I$ do as a transformation, and why does $AI = IA = A$ follow immediately from that description?

8.7 ⭐ Without computing, what is the shape of $A^{\mathsf{T}}$ if $A$ is $2\times 5$? Name one class of matrices for which $A^{\mathsf{T}} = A^{-1}$.


Tier 2 — Hand computation (⭐⭐)

8.8 ⭐⭐ Compute $A + B$ and $3A - 2B$ by hand.

8.9 ⭐⭐ Compute $AB$ two ways: (a) by the columns reading (apply $A$ to each column of $B$ as a weighted sum of $A$'s columns), and (b) by the entry rule (row $i$ of $A$ dotted with column $j$ of $B$). Confirm both give $\begin{bmatrix}4 & 13 \\ 6 & 15\end{bmatrix}$.

8.10 ⭐⭐ Compute $BA$ and verify that $BA \ne AB$. Write down the two different matrices side by side.

8.11 ⭐⭐ Compute both products of the rotation and shear: $RS$ (shear first, then rotate) and $SR$ (rotate first, then shear). Then track east $\mathbf{e}_1 = (1,0)$ through each pipeline by hand and confirm the first columns you get match the products.

8.12 ⭐⭐ Compute $A^{\mathsf{T}}$, $B^{\mathsf{T}}$, and verify the order-reversal rule $(AB)^{\mathsf{T}} = B^{\mathsf{T}}A^{\mathsf{T}}$ by computing both sides. Then show $A^{\mathsf{T}}B^{\mathsf{T}}$ is a different matrix.

8.13 ⭐⭐ Compute $S^2$, $S^3$, and conjecture a formula for $S^n$. Verify your conjecture by computing $S^4$.

8.14 ⭐⭐ For the $2\times 3$ matrix $M = \begin{bmatrix}1 & 0 & 2 \\ 3 & 1 & 0\end{bmatrix}$ and the $3\times 2$ matrix $N = \begin{bmatrix}1 & 1 \\ 0 & 2 \\ 1 & 0\end{bmatrix}$, compute $MN$ and $NM$. Note their shapes are different — this is a case where both products exist but cannot possibly be equal.

8.15 ⭐⭐ Let $D_1 = \begin{bmatrix}2 & 0 \\ 0 & 3\end{bmatrix}$ and $D_2 = \begin{bmatrix}5 & 0 \\ 0 & 7\end{bmatrix}$ be diagonal. Compute $D_1 D_2$ and $D_2 D_1$. Do they commute? State the general rule for multiplying two diagonal matrices.

8.16 ⭐⭐ Verify associativity by hand for $A$, $B$, and $C = R$: compute $(AB)C$ and $A(BC)$ and confirm they agree.

8.17 ⭐⭐ Find a $2\times 2$ matrix $X$ (other than $I$ or a scalar multiple of $I$) that commutes with $A = \begin{bmatrix}2 & 1 \\ 0 & 3\end{bmatrix}$, i.e. $AX = XA$. (Hint: try $X = A$ itself, or a polynomial in $A$.)


Tier 3 — Proof and code (⭐⭐⭐)

8.18 ⭐⭐⭐ [proof] Prove that the composition of two linear transformations is linear. That is, if $S$ and $T$ are linear, show $T\circ S$ (apply $S$, then $T$) satisfies additivity and homogeneity. (This is the fact that guarantees $AB$ represents a transformation, hence is itself a matrix.)

8.19 ⭐⭐⭐ [proof] Prove the order-reversal rule for the transpose: $(AB)^{\mathsf{T}} = B^{\mathsf{T}}A^{\mathsf{T}}$ for conformable $A, B$. (Use the entry formula $(AB)_{ij} = \sum_k a_{ik}b_{kj}$ and compare entries of both sides.)

8.20 ⭐⭐⭐ [proof] Prove the left-distributive law $A(B + C) = AB + AC$ for conformable matrices, using the entry formula and the distributivity of ordinary numbers.

8.21 ⭐⭐⭐ [proof] Show that if $A$ and $B$ are both symmetric ($A = A^{\mathsf{T}}$, $B = B^{\mathsf{T}}$), then $AB$ is symmetric if and only if $A$ and $B$ commute. (Use the order-reversal rule from 8.19.)

8.22 ⭐⭐⭐ [code] Implement matmul(A, B) from scratch in toolkit/matrices.py as composition (the $j$-th column of $AB$ is $A$ applied to the $j$-th column of $B$), reusing your Chapter 7 apply. Raise a clear ValueError when the inner dimensions disagree. Verify with np.allclose(np.array(matmul(A, B)), np.array(A) @ np.array(B)) on at least one square and one non-square conformable pair.

8.23 ⭐⭐⭐ [code] Write a function commute(A, B) that returns True when $AB = BA$. Test it on (a) two diagonal matrices, (b) the rotation $R$ and shear $S$, and (c) a matrix and a scalar multiple of the identity. Report which pairs commute and explain the pattern.

8.24 ⭐⭐⭐ [code] Using the recurring visualizer visualize_2d, render $RS$ and $SR$ in two side-by-side panels (plt.subplots(1, 2)). Confirm visually that the two parallelograms differ, and print the two product matrices to confirm $RS \ne SR$.


Tier 4 — Applications (⭐⭐⭐⭐)

8.25 ⭐⭐⭐⭐ [code] Graphics composition order. Build a non-uniform scale $G = \begin{bmatrix}3 & 0 \\ 0 & 1\end{bmatrix}$ and a $45°$ rotation $R_{45}$. Compute and visualize both $R_{45}G$ ("scale then rotate") and $G R_{45}$ ("rotate then scale"). Describe in words how the two final shapes differ, and explain why a game engine that applies these in the wrong order produces a sheared, distorted object.

8.26 ⭐⭐⭐⭐ [code] Markov transition step. Let $P = \begin{bmatrix}0.9 & 0.2 \\ 0.1 & 0.8\end{bmatrix}$ be a monthly transition matrix whose columns sum to 1 (employed/unemployed). Starting from $\mathbf{x}_0 = (0, 1)$ (everyone unemployed), compute $\mathbf{x}_1, \mathbf{x}_2, \mathbf{x}_3$ by repeated multiplication, then compute $P^{12}$ and apply it to $\mathbf{x}_0$ in one shot. Confirm the one-shot answer matches stepping twelve times. What appears to be the long-run distribution?

8.27 ⭐⭐⭐⭐ [code] Path counting. Take the adjacency matrix of a 4-node directed cycle ($0\to1\to2\to3\to0$). Compute $A^2$, $A^3$, $A^4$. Interpret each entry as a count of $n$-step paths, and explain why $A^4$ equals the identity. What does that say about walks on a cycle?

8.28 ⭐⭐⭐⭐ [proof/code] Rotation powers and trig. Show algebraically that $R(\theta)R(\phi) = R(\theta + \phi)$ by multiplying the two rotation matrices and using the angle-sum identities for sine and cosine. Then verify numerically that $R(30°)R(45°) = R(75°)$ to four decimal places, and that $R(30°)^{12} = I$.