Let \(A=\begin{bmatrix}\mathbf{a}_1 \amp \mathbf{a}_2 \amp \cdots \amp \mathbf{a}_n\end{bmatrix}\) be an \(m\times n\) matrix and let \(\mathbf{x}=(x_1,\ldots,x_n)\) be a column vector in \(\mathbb R^n\text{.}\) The matrix-vector product is
A matrix-vector product has two complementary readings. In the row view, each row measures the input by taking a dot product with \(\mathbf{x}\text{.}\) In the column view,
The same product \(A\mathbf{x}\) can represent different operations, depending on what the rows and columns of \(A\) mean. The next activities show three common patterns: scoring, selecting, and measuring changes.
In ActivityΒ 1.1.6, we ranked documents by cosine similarity one score at a time. The following code repeats that ranking using a data matrix whose rows are the document vectors.
The rows of X are \(\mathbf{D}_1\text{,}\)\(\mathbf{D}_2\text{,}\) and \(\mathbf{D}_3\text{.}\) The product X @ q computes the three dot products with the query. The expression np.argsort(scores)[::-1] gives the indices of the scores from largest to smallest. The output agrees with ActivityΒ 1.1.6: \(1,\frac12,0\text{.}\)
The line s = K @ q computes the three dot products. The token βbirdβ receives the largest weight. The line out = alpha @ V forms the weighted average of the rows of V. The entries of alpha add to \(1\) because the scores were divided by their sum.
If \(A=\begin{bmatrix}\mathbf{a}_1 \amp \cdots \amp \mathbf{a}_n\end{bmatrix}\text{,}\) then \(A\mathbf{e}_j=\mathbf{a}_j\text{.}\) Applying a matrix to a standard basis vector picks out a column.
In \(\mathbb R^2\text{,}\) a matrix \(A=\begin{bmatrix}\mathbf{a}_1 \amp \mathbf{a}_2\end{bmatrix}\) sends \(\mathbf{e}_1\) to \(\mathbf{a}_1\) and \(\mathbf{e}_2\) to \(\mathbf{a}_2\text{.}\) Since every vector \(\mathbf{x}=(x_1,x_2)\) can be written as \(x_1\mathbf{e}_1+x_2\mathbf{e}_2\text{,}\) we have
To understand a \(2\times 2\) matrix \(A\text{,}\) apply it to the four corners of the unit square. The image is usually a parallelogram. The two edges leaving the origin are the columns of \(A\text{.}\) We will refer to this as the unit-square visualization.
Three panels appear from left to right: the original unit square, the two transformed basis vectors, and the image parallelogram. In the middle panel, a red arrow marks \(A\mathbf e_1\) and a green arrow marks \(A\mathbf e_2\text{;}\) in the right panel, those two arrows form adjacent edges of the sheared parallelogram.
Figure1.3.10.A \(2\times 2\) matrix sends the unit square to the parallelogram determined by its two columns. This is the geometric version of columns contribute.
For each matrix in the compact transformation gallery, compare \(A\mathbf{e}_1\) and \(A\mathbf{e}_2\) with \(\mathbf{e}_1\) and \(\mathbf{e}_2\text{.}\) Which column changed most visibly from the identity matrix? What does that change do to the unit square?
The columns \(A\mathbf{e}_1\) and \(A\mathbf{e}_2\) are the transformed coordinate directions. A stretch changes the length of a column. A reflection reverses the direction of a column. A shear changes one column by adding part of the other direction. A projection sends one column to zero, so one direction is forgotten.
Let \(A=\begin{bmatrix}-1\amp0\\0\amp1\end{bmatrix}\text{.}\) Compute \(A\mathbf{e}_1\text{,}\)\(A\mathbf{e}_2\text{,}\)\(A\mathbf{x}\text{,}\) and \(A\mathbf{y}\text{,}\) where \(\mathbf{x}=(1,1)\) and \(\mathbf{y}=(0,1)\text{.}\) Which coordinate changes?
Let \(A=\begin{bmatrix}1\amp1\\0\amp1\end{bmatrix}\text{.}\) Compute \(A\mathbf{e}_1\text{,}\)\(A\mathbf{e}_2\text{,}\) and \(A\mathbf{x}\text{,}\) where \(\mathbf{x}=(1,1)\text{.}\) Explain why the bottom edge stays fixed and the top edge shifts right.
Every linear map \(T:\mathbb R^n\to\mathbb R^m\) is given by multiplication by a unique matrix. The columns of that matrix are \(T(\mathbf{e}_1),\ldots,T(\mathbf{e}_n)\text{.}\)
Activity1.3.9.Images of basis vectors determine the matrix.
Suppose a linear map \(T:\mathbb R^2\to\mathbb R^2\) satisfies \(T(\mathbf{e}_1)=(2,1)\) and \(T(\mathbf{e}_2)=(-1,3)\text{.}\) Find the matrix \(A\) such that \(T(\mathbf{x})=A\mathbf{x}\text{.}\)
An affine map is linear only when \(\mathbf{b}=\mathbf{0}\text{.}\) A quick test is the zero vector: every linear map sends \(\mathbf{0}\) to \(\mathbf{0}\text{,}\) but