Matrix Operations & Linear Transformations

Volume I, Chapter 1 — Part II. Matrices as linear transformations, composition, the four fundamental subspaces, rank-nullity theorem, determinants, and affine maps. Full derivations connecting matrix algebra to neural network layers and attention.

Beginner

Table of Contents

  1. Learning Objectives
  2. Prerequisites
  3. Notation
  4. Core Intuition
  5. Linear Transformations
  6. Matrix Representation of Linear Maps
  7. Matrix Multiplication as Composition
  8. The Four Fundamental Subspaces
  9. The Rank-Nullity Theorem
  10. Determinants and Invertibility
  11. Systems of Linear Equations
  12. Transpose, Symmetry, and Orthogonality
  13. Affine Transformations and Homogeneous Coordinates
  14. Special Matrix Structures in Deep Learning
  15. Attention as Composed Linear Maps
  16. Common Pitfalls and Misconceptions
  17. Research Perspective
  18. Summary of Takeaways
  19. Exercises

Learning Objectives

After reading this chapter, you should be able to:

  1. Define linear transformations and prove basic properties (e.g., T(0)=0T(\mathbf{0}) = \mathbf{0}).
  2. Construct the matrix representation of a linear map relative to the standard basis and prove uniqueness.
  3. Derive matrix multiplication from composition of linear maps and interpret products via column, row, and outer-product expansions.
  4. State and prove the rank-nullity theorem, and apply it to analyze information loss in neural network layers.
  5. Define the determinant, prove its multiplicative property, and characterize invertibility.
  6. Solve linear systems Ax=b\mathbf{A}\mathbf{x} = \mathbf{b} and derive the least-squares normal equations.
  7. Distinguish linear from affine maps and explain how homogeneous coordinates restore linearity.
  8. Decompose attention into a sequence of matrix operations.

Prerequisites


Notation

  • ARm×n\mathbf{A} \in \mathbb{R}^{m \times n} — Matrix with mm rows and nn columns
  • C(A),N(A)\mathcal{C}(\mathbf{A}), \mathcal{N}(\mathbf{A}) — Column space and null space
  • rank(A)\text{rank}(\mathbf{A}) — Column rank
  • T:VW\mathbf{T}: V \to W — Linear transformation between vector spaces
  • In\mathbf{I}_nn×nn \times n identity matrix
  • A1,AT\mathbf{A}^{-1}, \mathbf{A}^T — Inverse and transpose

Core Intuition

A matrix is not merely a table of numbers — it is a concrete representation of a linear transformation. When a neural network layer computes h=Wx\mathbf{h} = \mathbf{W}\mathbf{x}, the matrix W\mathbf{W} specifies how each input coordinate contributes to each output coordinate. Composing layers corresponds to multiplying matrices. Understanding which outputs are reachable (column space), which inputs are collapsed to zero (null space), and how much information is preserved (rank) is essential for analyzing model capacity, bottlenecks, and low-rank adaptation.

This chapter develops matrix algebra from the transformation viewpoint, proves the rank-nullity theorem in full, introduces determinants as volume-scaling factors, and connects the theory to attention mechanisms and affine neural network layers.

Series context. This is Part II of Chapter 1 (Linear Algebra). We build on Part I and prepare for Eigenvalues & Eigenvectors and the SVD.

Interactive: Linear Transformations

Ae₁Ae₂
A = [0.71, -0.71; 0.71, 0.71]det(A) = 1.000

Transform type:

Observe: The blue/green arrows show where the standard basis vectors e₁=(1,0) and e₂=(0,1) land after transformation. The shaded area is the transformed unit square — its area equals |det(A)|. When det < 0, orientation flips.

Linear Transformations

Definition 1 (Linear Transformation). A function T:VWT: V \to W between vector spaces over F\mathbb{F} is a linear transformation (linear map) if for all u,vV\mathbf{u}, \mathbf{v} \in V and α,βF\alpha, \beta \in \mathbb{F}:

T(αu+βv)=αT(u)+βT(v).(1)T(\alpha \mathbf{u} + \beta \mathbf{v}) = \alpha T(\mathbf{u}) + \beta T(\mathbf{v}). \tag{1}

Equivalently, TT satisfies additivity T(u+v)=T(u)+T(v)T(\mathbf{u}+\mathbf{v}) = T(\mathbf{u}) + T(\mathbf{v}) and homogeneity T(αv)=αT(v)T(\alpha\mathbf{v}) = \alpha T(\mathbf{v}).

Proposition 1. Every linear transformation maps the zero vector to the zero vector: T(0)=0T(\mathbf{0}) = \mathbf{0}.

Proof. T(0)=T(0v)=0T(v)=0T(\mathbf{0}) = T(0 \cdot \mathbf{v}) = 0 \cdot T(\mathbf{v}) = \mathbf{0}. \blacksquare

Corollary 1. An affine map f(x)=Wx+bf(\mathbf{x}) = \mathbf{W}\mathbf{x} + \mathbf{b} with b0\mathbf{b} \neq \mathbf{0} is not linear.

Proof. f(0)=b0f(\mathbf{0}) = \mathbf{b} \neq \mathbf{0}, contradicting Proposition 1. \blacksquare

Neural network layers apply affine maps followed by nonlinear activations: h=σ(Wx+b)\mathbf{h} = \sigma(\mathbf{W}\mathbf{x} + \mathbf{b}). The linear part is W\mathbf{W}; the bias b\mathbf{b} shifts the origin.

Definition 2 (Kernel and Image). For T:VWT: V \to W:

ker(T)={vV:T(v)=0},im(T)={T(v):vV}.(2)\text{ker}(T) = \{\mathbf{v} \in V : T(\mathbf{v}) = \mathbf{0}\}, \qquad \text{im}(T) = \{T(\mathbf{v}) : \mathbf{v} \in V\}. \tag{2}

Both are subspaces of VV and WW respectively.


Matrix Representation of Linear Maps

Theorem 1 (Matrix Representation). Every linear transformation T:RnRmT: \mathbb{R}^n \to \mathbb{R}^m can be represented as multiplication by a unique matrix ARm×n\mathbf{A} \in \mathbb{R}^{m \times n}:

T(x)=AxxRn.(3)T(\mathbf{x}) = \mathbf{A}\mathbf{x} \quad \forall \mathbf{x} \in \mathbb{R}^n. \tag{3}

Proof. Let {e1,,en}\{\mathbf{e}_1, \ldots, \mathbf{e}_n\} be the standard basis of Rn\mathbb{R}^n. Any x=j=1nxjej\mathbf{x} = \sum_{j=1}^n x_j \mathbf{e}_j. By linearity:

T(x)=j=1nxjT(ej).(4)T(\mathbf{x}) = \sum_{j=1}^n x_j T(\mathbf{e}_j). \tag{4}

Define A=[T(e1)T(e2)T(en)]Rm×n\mathbf{A} = [T(\mathbf{e}_1) \mid T(\mathbf{e}_2) \mid \cdots \mid T(\mathbf{e}_n)] \in \mathbb{R}^{m \times n}, so the jj-th column is the image of the jj-th basis vector. Then (4) becomes T(x)=AxT(\mathbf{x}) = \mathbf{A}\mathbf{x}.

For uniqueness, suppose Ax=Bx\mathbf{A}\mathbf{x} = \mathbf{B}\mathbf{x} for all x\mathbf{x}. Taking x=ej\mathbf{x} = \mathbf{e}_j gives the jj-th columns of A\mathbf{A} and B\mathbf{B} equal for all jj, so A=B\mathbf{A} = \mathbf{B}. \blacksquare

Important equation. Equation (3) identifies the matrix column jj with T(ej)T(\mathbf{e}_j). In a neural network, column jj of W\mathbf{W} tells us how input feature jj contributes to every output dimension.

Proposition 2. Under the identification TAT \leftrightarrow \mathbf{A}, ker(T)=null(A)\text{ker}(T) = \text{null}(\mathbf{A}) and im(T)=col(A)\text{im}(T) = \text{col}(\mathbf{A}).


Matrix Multiplication as Composition

Let T1:RnRmT_1: \mathbb{R}^n \to \mathbb{R}^m with matrix ARm×n\mathbf{A} \in \mathbb{R}^{m \times n} and T2:RmRpT_2: \mathbb{R}^m \to \mathbb{R}^p with matrix BRp×m\mathbf{B} \in \mathbb{R}^{p \times m}.

Definition 3 (Composition). The composition T2T1:RnRpT_2 \circ T_1: \mathbb{R}^n \to \mathbb{R}^p satisfies

(T2T1)(x)=T2(T1(x))=B(Ax)=(BA)x.(5)(T_2 \circ T_1)(\mathbf{x}) = T_2(T_1(\mathbf{x})) = \mathbf{B}(\mathbf{A}\mathbf{x}) = (\mathbf{B}\mathbf{A})\mathbf{x}. \tag{5}

Thus composition corresponds to matrix multiplication, defined by (BA)ij=k=1mBikAkj(\mathbf{B}\mathbf{A})_{ij} = \sum_{k=1}^m B_{ik} A_{kj}.

Theorem 2 (Four Interpretations of C=AB\mathbf{C} = \mathbf{A}\mathbf{B}). Let ARm×n\mathbf{A} \in \mathbb{R}^{m \times n}, BRn×p\mathbf{B} \in \mathbb{R}^{n \times p}, C=ABRm×p\mathbf{C} = \mathbf{A}\mathbf{B} \in \mathbb{R}^{m \times p}.

  1. Entrywise (dot product): Cij=aibjC_{ij} = \mathbf{a}_{i*} \cdot \mathbf{b}_{*j} (row ii of A\mathbf{A} dotted with column jj of B\mathbf{B}).
  2. Column view: cj=Abj=k=1nBkjak\mathbf{c}_{*j} = \mathbf{A}\mathbf{b}_{*j} = \sum_{k=1}^n B_{kj}\, \mathbf{a}_{*k}.
  3. Row view: ci=aiB=k=1nAikbk\mathbf{c}_{i*} = \mathbf{a}_{i*} \mathbf{B} = \sum_{k=1}^n A_{ik}\, \mathbf{b}_{k*}.
  4. Outer-product (rank-1 sum): C=k=1nakbkT\mathbf{C} = \sum_{k=1}^n \mathbf{a}_{*k}\, \mathbf{b}_{k*}^T.

Proof of (4). (akbkT)ij=AikBkj(\mathbf{a}_{*k}\mathbf{b}_{k*}^T)_{ij} = A_{ik} B_{kj}, and summing over kk gives CijC_{ij}. \blacksquare

The outer-product view is foundational for low-rank matrix factorization and LoRA fine-tuning: a rank-rr update is a sum of rr rank-1 terms.

Proposition 3 (Non-commutativity). Matrix multiplication is associative: (AB)C=A(BC)(\mathbf{A}\mathbf{B})\mathbf{C} = \mathbf{A}(\mathbf{B}\mathbf{C}), but not commutative in general.

Deep networks compute WLW2W1x\mathbf{W}_L \cdots \mathbf{W}_2 \mathbf{W}_1 \mathbf{x} — rightmost factor applied first.


The Four Fundamental Subspaces

For ARm×n\mathbf{A} \in \mathbb{R}^{m \times n}, define:

col(A)={Ax:xRn}Rm(column space / range)null(A)={xRn:Ax=0}Rn(null space / kernel)row(A)=col(AT)Rn(row space)null(AT)={yRm:ATy=0}Rm(left null space)(6)\begin{aligned} \text{col}(\mathbf{A}) &= \{\mathbf{A}\mathbf{x} : \mathbf{x} \in \mathbb{R}^n\} \subseteq \mathbb{R}^m && \text{(column space / range)} \\ \text{null}(\mathbf{A}) &= \{\mathbf{x} \in \mathbb{R}^n : \mathbf{A}\mathbf{x} = \mathbf{0}\} \subseteq \mathbb{R}^n && \text{(null space / kernel)} \\ \text{row}(\mathbf{A}) &= \text{col}(\mathbf{A}^T) \subseteq \mathbb{R}^n && \text{(row space)} \\ \text{null}(\mathbf{A}^T) &= \{\mathbf{y} \in \mathbb{R}^m : \mathbf{A}^T \mathbf{y} = \mathbf{0}\} \subseteq \mathbb{R}^m && \text{(left null space)} \end{aligned} \tag{6}

Theorem 3 (Orthogonality Relations).

row(A)null(A)(both in Rn)col(A)null(AT)(both in Rm)(7)\begin{aligned} \text{row}(\mathbf{A}) &\perp \text{null}(\mathbf{A}) & \text{(both in } \mathbb{R}^n\text{)} \\ \text{col}(\mathbf{A}) &\perp \text{null}(\mathbf{A}^T) & \text{(both in } \mathbb{R}^m\text{)} \end{aligned} \tag{7}

Proof of first relation. Let xnull(A)\mathbf{x} \in \text{null}(\mathbf{A}) and rrow(A)\mathbf{r} \in \text{row}(\mathbf{A}). Then rTx=0\mathbf{r}^T \mathbf{x} = 0 because each row of A\mathbf{A} is orthogonal to x\mathbf{x} (since Ax=0\mathbf{A}\mathbf{x} = \mathbf{0}). Any vector in row(A)\text{row}(\mathbf{A}) is a linear combination of rows, hence also orthogonal to x\mathbf{x}. \blacksquare

Visual description. In Rn\mathbb{R}^n, the row space and null space are complementary orthogonal subspaces: every vector decomposes uniquely into a component in row(A)\text{row}(\mathbf{A}) and a component in null(A)\text{null}(\mathbf{A}). The column space describes reachable outputs; the null space describes input directions "invisible" to the transformation.

Definition 4 (Rank). rank(A)=dim(col(A))=dim(row(A))\text{rank}(\mathbf{A}) = \dim(\text{col}(\mathbf{A})) = \dim(\text{row}(\mathbf{A})).


The Rank-Nullity Theorem

Theorem 4 (Rank-Nullity). For any ARm×n\mathbf{A} \in \mathbb{R}^{m \times n}:

rank(A)+nullity(A)=n,(8)\text{rank}(\mathbf{A}) + \text{nullity}(\mathbf{A}) = n, \tag{8}

where nullity(A)=dim(null(A))\text{nullity}(\mathbf{A}) = \dim(\text{null}(\mathbf{A})).

Proof. Let r=rank(A)r = \text{rank}(\mathbf{A}) and k=nullity(A)k = \text{nullity}(\mathbf{A}). Choose a basis {u1,,uk}\{\mathbf{u}_1, \ldots, \mathbf{u}_k\} for null(A)\text{null}(\mathbf{A}) and extend to a basis {u1,,uk,w1,,wr}\{\mathbf{u}_1, \ldots, \mathbf{u}_k, \mathbf{w}_1, \ldots, \mathbf{w}_r\} for Rn\mathbb{R}^n (possible since row(A)null(A)=Rn\text{row}(\mathbf{A}) \oplus \text{null}(\mathbf{A}) = \mathbb{R}^n and dim(row(A))=r\dim(\text{row}(\mathbf{A})) = r).

Claim: {Aw1,,Awr}\{\mathbf{A}\mathbf{w}_1, \ldots, \mathbf{A}\mathbf{w}_r\} is a basis for col(A)\text{col}(\mathbf{A}).

Spanning: Any Ax\mathbf{A}\mathbf{x} with x=iαiui+jβjwj\mathbf{x} = \sum_i \alpha_i \mathbf{u}_i + \sum_j \beta_j \mathbf{w}_j equals jβjAwj\sum_j \beta_j \mathbf{A}\mathbf{w}_j since Aui=0\mathbf{A}\mathbf{u}_i = \mathbf{0}.

Independence: If jβjAwj=0\sum_j \beta_j \mathbf{A}\mathbf{w}_j = \mathbf{0}, then A(jβjwj)=0\mathbf{A}(\sum_j \beta_j \mathbf{w}_j) = \mathbf{0}, so jβjwjnull(A)\sum_j \beta_j \mathbf{w}_j \in \text{null}(\mathbf{A}). But jβjwj\sum_j \beta_j \mathbf{w}_j is also in span(wj)\text{span}(\mathbf{w}_j), which intersects null(A)\text{null}(\mathbf{A}) only at 0\mathbf{0} (by construction of the extended basis). Hence all βj=0\beta_j = 0. \blacksquare

Important equation. Equation (8) quantifies the trade-off between information preserved (rank) and information destroyed (nullity). A layer with rank(W)<din\text{rank}(\mathbf{W}) < d_{\text{in}} has a nontrivial null space — distinct inputs can produce identical outputs.

Corollary 2. For ARn×n\mathbf{A} \in \mathbb{R}^{n \times n}, A\mathbf{A} is invertible if and only if rank(A)=n\text{rank}(\mathbf{A}) = n if and only if null(A)={0}\text{null}(\mathbf{A}) = \{\mathbf{0}\}.


Determinants and Invertibility

Definition 5 (Determinant — axiomatic). The determinant is the unique function det:Rn×nR\det: \mathbb{R}^{n \times n} \to \mathbb{R} satisfying:

  1. Multilinearity in rows (or columns).
  2. Alternating: swapping two rows changes sign.
  3. Normalization: det(I)=1\det(\mathbf{I}) = 1.

Theorem 5 (Multiplicativity). For A,BRn×n\mathbf{A}, \mathbf{B} \in \mathbb{R}^{n \times n}:

det(AB)=det(A)det(B).(9)\det(\mathbf{A}\mathbf{B}) = \det(\mathbf{A}) \det(\mathbf{B}). \tag{9}

Theorem 6. ARn×n\mathbf{A} \in \mathbb{R}^{n \times n} is invertible if and only if det(A)0\det(\mathbf{A}) \neq 0.

Geometric interpretation. det(A)|\det(\mathbf{A})| is the factor by which A\mathbf{A} scales nn-dimensional volume. det(A)=0\det(\mathbf{A}) = 0 means the transformation collapses space to lower dimension.

Proposition 4 (Characteristic connection). det(A)=i=1nλi\det(\mathbf{A}) = \prod_{i=1}^n \lambda_i where λi\lambda_i are eigenvalues (with multiplicity).

This links determinants to eigendecomposition, developed in Eigenvalues & Eigenvectors.

2×2 formula. For A=[abcd]\mathbf{A} = \begin{bmatrix} a & b \\ c & d \end{bmatrix}:

det(A)=adbc.(10)\det(\mathbf{A}) = ad - bc. \tag{10}

Systems of Linear Equations

Problem. Given ARm×n\mathbf{A} \in \mathbb{R}^{m \times n} and bRm\mathbf{b} \in \mathbb{R}^m, find xRn\mathbf{x} \in \mathbb{R}^n such that

Ax=b.(11)\mathbf{A}\mathbf{x} = \mathbf{b}. \tag{11}

Theorem 7 (Existence and Uniqueness).

  • Existence: Ax=b\mathbf{A}\mathbf{x} = \mathbf{b} has a solution iff bcol(A)\mathbf{b} \in \text{col}(\mathbf{A}).
  • Uniqueness: If a solution exists, it is unique iff null(A)={0}\text{null}(\mathbf{A}) = \{\mathbf{0}\}.

Proof. Existence: Ax=b\mathbf{A}\mathbf{x} = \mathbf{b} means b\mathbf{b} is a linear combination of columns of A\mathbf{A}. Uniqueness: if x1,x2\mathbf{x}_1, \mathbf{x}_2 are solutions, then A(x1x2)=0\mathbf{A}(\mathbf{x}_1 - \mathbf{x}_2) = \mathbf{0}, so x1x2null(A)\mathbf{x}_1 - \mathbf{x}_2 \in \text{null}(\mathbf{A}). \blacksquare

Least squares. When bcol(A)\mathbf{b} \notin \text{col}(\mathbf{A}) (overdetermined system), the least-squares solution minimizes Axb2\|\mathbf{A}\mathbf{x} - \mathbf{b}\|^2.

Theorem 8 (Normal Equations). If ATA\mathbf{A}^T\mathbf{A} is invertible, the unique least-squares solution satisfies

ATAx^=ATb.(12)\mathbf{A}^T \mathbf{A}\, \hat{\mathbf{x}} = \mathbf{A}^T \mathbf{b}. \tag{12}

Derivation. Define L(x)=Axb2=(Axb)T(Axb)L(\mathbf{x}) = \|\mathbf{A}\mathbf{x} - \mathbf{b}\|^2 = (\mathbf{A}\mathbf{x} - \mathbf{b})^T(\mathbf{A}\mathbf{x} - \mathbf{b}). Expanding:

L(x)=xTATAx2bTAx+bTb.(13)L(\mathbf{x}) = \mathbf{x}^T \mathbf{A}^T \mathbf{A}\, \mathbf{x} - 2\mathbf{b}^T \mathbf{A}\mathbf{x} + \mathbf{b}^T \mathbf{b}. \tag{13}

Setting xL=0\nabla_{\mathbf{x}} L = \mathbf{0}:

2ATAx2ATb=0    ATAx=ATb.(14)2\mathbf{A}^T \mathbf{A}\, \mathbf{x} - 2\mathbf{A}^T \mathbf{b} = \mathbf{0} \implies \mathbf{A}^T \mathbf{A}\, \mathbf{x} = \mathbf{A}^T \mathbf{b}. \tag{14}

This is the foundation of linear regression, treated in Volume II, Chapter 4.

Connection to independence. ATA\mathbf{A}^T\mathbf{A} is invertible iff columns of A\mathbf{A} are linearly independent (Proposition 5, Part I). Dependent columns make the normal equations singular.


Transpose, Symmetry, and Orthogonality

Definition 6 (Transpose). (AT)ij=Aji(\mathbf{A}^T)_{ij} = A_{ji}.

Proposition 5.

(AT)T=A,(A+B)T=AT+BT,(αA)T=αAT,(AB)T=BTAT.(15)(\mathbf{A}^T)^T = \mathbf{A}, \quad (\mathbf{A}+\mathbf{B})^T = \mathbf{A}^T + \mathbf{B}^T, \quad (\alpha\mathbf{A})^T = \alpha\mathbf{A}^T, \quad (\mathbf{A}\mathbf{B})^T = \mathbf{B}^T \mathbf{A}^T. \tag{15}

Proof of reversal. ((AB)T)ij=(AB)ji=kAjkBki=k(BT)ik(AT)kj=(BTAT)ij((\mathbf{AB})^T)_{ij} = (\mathbf{AB})_{ji} = \sum_k A_{jk} B_{ki} = \sum_k (\mathbf{B}^T)_{ik} (\mathbf{A}^T)_{kj} = (\mathbf{B}^T \mathbf{A}^T)_{ij}. \blacksquare

Definition 7. A\mathbf{A} is symmetric if A=AT\mathbf{A} = \mathbf{A}^T. It is orthogonal if ATA=I\mathbf{A}^T \mathbf{A} = \mathbf{I}.

Orthogonal matrices preserve Euclidean norms: Qx=x\|\mathbf{Q}\mathbf{x}\| = \|\mathbf{x}\|. They appear in SVD, QR decomposition, and orthogonal initialization.

Definition 8 (Gram matrix). G=ATA\mathbf{G} = \mathbf{A}^T \mathbf{A} encodes inner products between columns of A\mathbf{A}: Gij=ai,ajG_{ij} = \langle \mathbf{a}_i, \mathbf{a}_j \rangle.


Affine Transformations and Homogeneous Coordinates

An affine transformation is f(x)=Ax+bf(\mathbf{x}) = \mathbf{A}\mathbf{x} + \mathbf{b}.

Homogeneous coordinates. Augment x\mathbf{x} with a 1:

x~=[x1]Rn+1,A~=[Ab0T1]R(m+1)×(n+1).(16)\tilde{\mathbf{x}} = \begin{bmatrix} \mathbf{x} \\ 1 \end{bmatrix} \in \mathbb{R}^{n+1}, \qquad \tilde{\mathbf{A}} = \begin{bmatrix} \mathbf{A} & \mathbf{b} \\ \mathbf{0}^T & 1 \end{bmatrix} \in \mathbb{R}^{(m+1) \times (n+1)}. \tag{16}

Then A~x~=[Ax+b1]\tilde{\mathbf{A}}\tilde{\mathbf{x}} = \begin{bmatrix} \mathbf{A}\mathbf{x} + \mathbf{b} \\ 1 \end{bmatrix}, and composition of affine maps becomes matrix multiplication in the augmented space.

This trick allows us to treat biases as part of a larger linear system — useful in backpropagation (Chapter 8) and in projective geometry for computer vision.


Special Matrix Structures in Deep Learning

Diagonal matrices

D=diag(d1,,dn)\mathbf{D} = \text{diag}(d_1, \ldots, d_n) scales coordinate ii by did_i. Layer normalization applies a learned diagonal scaling after centering.

Block-diagonal matrices

Multi-head attention uses block structure:

WO[W1Wh](17)\mathbf{W}_O \begin{bmatrix} \mathbf{W}_1 & & \\ & \ddots & \\ & & \mathbf{W}_h \end{bmatrix} \tag{17}

Each block operates on a head-specific subspace.

Low-rank matrices

A=UVT\mathbf{A} = \mathbf{U}\mathbf{V}^T with URm×r\mathbf{U} \in \mathbb{R}^{m \times r}, VRn×r\mathbf{V} \in \mathbb{R}^{n \times r}, rmin(m,n)r \ll \min(m,n). Storage O(r(m+n))O(r(m+n)) vs O(mn)O(mn). LoRA fine-tuning learns ΔW=BA\Delta \mathbf{W} = \mathbf{B}\mathbf{A} with small rank rr.

Toeplitz and convolution

A 1D convolution corresponds to multiplication by a Toeplitz matrix — each row is a shifted copy of the filter. CNN theory (Chapter 9) builds on this equivalence.


Attention as Composed Linear Maps

Self-attention on input XRT×d\mathbf{X} \in \mathbb{R}^{T \times d} ( TT tokens, dimension dd) proceeds as:

Step 1 — Linear projections:

Q=XWQ,K=XWK,V=XWV,(18)\mathbf{Q} = \mathbf{X}\mathbf{W}_Q, \quad \mathbf{K} = \mathbf{X}\mathbf{W}_K, \quad \mathbf{V} = \mathbf{X}\mathbf{W}_V, \tag{18}

where WQ,WK,WVRd×dk\mathbf{W}_Q, \mathbf{W}_K, \mathbf{W}_V \in \mathbb{R}^{d \times d_k}.

Step 2 — Similarity matrix:

S=QKTdkRT×T.(19)\mathbf{S} = \frac{\mathbf{Q}\mathbf{K}^T}{\sqrt{d_k}} \in \mathbb{R}^{T \times T}. \tag{19}

Entry Sij=qi,kj/dkS_{ij} = \langle \mathbf{q}_i, \mathbf{k}_j \rangle / \sqrt{d_k} is a scaled inner product (cosine similarity when vectors are normalized).

Step 3 — Softmax (row-wise, nonlinear):

A=softmax(S)RT×T,Aij=exp(Sij)kexp(Sik).(20)\mathbf{A} = \text{softmax}(\mathbf{S}) \in \mathbb{R}^{T \times T}, \quad A_{ij} = \frac{\exp(S_{ij})}{\sum_k \exp(S_{ik})}. \tag{20}

Step 4 — Weighted combination of values:

Output=AVRT×dk.(21)\text{Output} = \mathbf{A}\mathbf{V} \in \mathbb{R}^{T \times d_k}. \tag{21}

Steps 1, 2, and 4 are purely linear (matrix multiplications). Softmax introduces nonlinearity. The overall map is not linear, but each linear sub-step inherits the subspace structure developed in this chapter: col(V)\text{col}(\mathbf{V}) determines reachable outputs; rank constraints on WQ,WK\mathbf{W}_Q, \mathbf{W}_K limit expressivity.

Visual description. Think of QKT\mathbf{Q}\mathbf{K}^T as a T×TT \times T table of pairwise affinities between tokens. Softmax converts each row into a probability vector. Multiplying by V\mathbf{V} blends value vectors according to these probabilities — a data-dependent linear combination.


Common Pitfalls and Misconceptions

Pitfall 1: Treating matrix multiplication as element-wise multiplication. The Hadamard product AB\mathbf{A} \odot \mathbf{B} is different from AB\mathbf{A}\mathbf{B}. Composition vs local gating are distinct operations.

Pitfall 2: Assuming AB=BA\mathbf{AB} = \mathbf{BA}. Order of transformations matters. Reversing layer order generally changes the network function.

Pitfall 3: Confusing rank with number of nonzero entries. A sparse matrix can be full rank; a dense matrix can be rank-deficient.

Pitfall 4: Ignoring the null space in dimensionality reduction. Projecting onto col(A)\text{col}(\mathbf{A}) loses all information in null(A)\text{null}(\mathbf{A}). PCA (Chapter 1, Part V) chooses directions to preserve variance, not arbitrary projections.

Pitfall 5: Inverting nearly singular matrices. When det(A)0\det(\mathbf{A}) \approx 0, A1\mathbf{A}^{-1} is numerically unstable. The condition number κ(A)=AA1\kappa(\mathbf{A}) = \|\mathbf{A}\|\|\mathbf{A}^{-1}\| measures this sensitivity.


Research Perspective

The matrix formulation of linear algebra crystallized in the nineteenth century (Cayley, Sylvester). The four-subspace framework was popularized by Gilbert Strang and provides the language for modern numerical linear algebra.

In machine learning, matrix factorization underpins recommender systems (Koren et al., 2009), where low-rank structure in rating matrices is exploited via SVD-like methods. The rank-nullity theorem explains bottleneck layers in autoencoders: an encoder with rank rr can pass at most rr independent features to the decoder. Low-rank adaptation (LoRA, Hu et al., 2021) exploits the empirical observation that fine-tuning updates often have low intrinsic rank.

Attention's formulation as matrix multiplication (Vaswani et al., 2017) enabled GPU-optimized implementations and subsequent algorithmic improvements (FlashAttention, linear attention). The linear-algebraic decomposition in equations (18)–(21) remains the reference point for all variants.


Summary of Takeaways

  • Linear mapT(αu+βv)=αT(u)+βT(v)T(\alpha\mathbf{u}+\beta\mathbf{v}) = \alpha T(\mathbf{u}) + \beta T(\mathbf{v}) — Layers without bias
  • Matrix columnsjj-th column = T(ej)T(\mathbf{e}_j) — Weight interpretation
  • Composition(T2T1)BA(T_2 \circ T_1) \leftrightarrow \mathbf{B}\mathbf{A} — Deep network depth
  • Column spacecol(A)\text{col}(\mathbf{A}) — Reachable outputs
  • Null spacenull(A)\text{null}(\mathbf{A}) — Lost information
  • Rank-nullityrank+nullity=n\text{rank} + \text{nullity} = n — Capacity vs compression
  • Normal equationsATAx^=ATb\mathbf{A}^T\mathbf{A}\hat{\mathbf{x}} = \mathbf{A}^T\mathbf{b} — Linear regression
  • Attentionsoftmax(QKT/dk)V\text{softmax}(\mathbf{QK}^T/\sqrt{d_k})\mathbf{V} — Transformer core

Next article: Eigenvalues & Eigenvectors → — intrinsic directions of linear transformations and spectral analysis of symmetric matrices.


Exercises

Exercise 1. Prove that rank(A)=rank(AT)\text{rank}(\mathbf{A}) = \text{rank}(\mathbf{A}^T) using the rank-nullity theorem applied to A\mathbf{A} and AT\mathbf{A}^T and the dimension identity n=dim(row)+dim(null)n = \dim(\text{row}) + \dim(\text{null}).

Exercise 2. Let ARm×n\mathbf{A} \in \mathbb{R}^{m \times n} with linearly independent columns. Derive x^=(ATA)1ATb\hat{\mathbf{x}} = (\mathbf{A}^T\mathbf{A})^{-1}\mathbf{A}^T\mathbf{b} from the normal equations (12) and interpret P=A(ATA)1AT\mathbf{P} = \mathbf{A}(\mathbf{A}^T\mathbf{A})^{-1}\mathbf{A}^T as an orthogonal projection onto col(A)\text{col}(\mathbf{A}).

Exercise 3. For AR2×2\mathbf{A} \in \mathbb{R}^{2 \times 2}, prove det(A)0\det(\mathbf{A}) \neq 0 iff columns of A\mathbf{A} are linearly independent.

Exercise 4. Show that orthogonal matrices satisfy Qx=x\|\mathbf{Q}\mathbf{x}\| = \|\mathbf{x}\| and that eigenvalues of Q\mathbf{Q} have modulus 1.

Exercise 5 (Attention). For T=3T = 3, dk=2d_k = 2, suppose S\mathbf{S} (before softmax) has rows with equal entries. Prove that A=softmax(S)\mathbf{A} = \text{softmax}(\mathbf{S}) has Aij=1/3A_{ij} = 1/3 for all i,ji, j, and hence the attention output for each token is the arithmetic mean of all value vectors.

Exercise 6 (Composition). A network has layers W1Rd2×d1\mathbf{W}_1 \in \mathbb{R}^{d_2 \times d_1}, W2Rd3×d2\mathbf{W}_2 \in \mathbb{R}^{d_3 \times d_2}. Express rank(W2W1)\text{rank}(\mathbf{W}_2 \mathbf{W}_1) in terms of ranks of W1,W2\mathbf{W}_1, \mathbf{W}_2. Give an example where rank(W2W1)<min(rank(W1),rank(W2))\text{rank}(\mathbf{W}_2 \mathbf{W}_1) < \min(\text{rank}(\mathbf{W}_1), \text{rank}(\mathbf{W}_2)).

Exercise 7 (Affine maps). Prove that the composition of two affine maps f(x)=Ax+af(\mathbf{x}) = \mathbf{A}\mathbf{x} + \mathbf{a} and g(x)=Bx+bg(\mathbf{x}) = \mathbf{B}\mathbf{x} + \mathbf{b} is affine, and compute its homogeneous matrix (16).