Skip to content
BIASED

Matrices and linear systems

The matrix product as a composition of layers, the row and column pictures of Ax = b, rank, null space and rank–nullity, when a system is solvable, Gaussian elimination, the inverse, and over- and underdetermined systems.

The matrix product is composition

The topic Vectors, spaces and linear maps showed that every linear map Rn→Rm\mathbb R^n \to \mathbb R^m is an m×nm \times n matrix and that a linear layer computes x↦Wx+bx \mapsto Wx + b. Two questions were left open: how to compute with such maps, and when an equation Ax=bAx = b can be solved for xx. This topic answers both.

Start with composition. Apply B∈Rn×pB \in \mathbb R^{n \times p} first and A∈Rm×nA \in \mathbb R^{m \times n} second. The result x↦A(Bx)x \mapsto A(Bx) is linear again, so it is a matrix too. We call that matrix the product ABAB and define it by

(AB) x=A(Bx)for every x.(AB)\,x = A(Bx) \quad \text{for every } x.

Column jj of ABAB is AA applied to column jj of BB. Entry by entry, row ii of AA meets column jj of BB:

(AB)ij=∑k=1nAikBkj.(AB)_{ij} = \sum_{k=1}^{n} A_{ik} B_{kj}.

The inner sizes must match: an m×nm \times n matrix times an n×pn \times p matrix is m×pm \times p.

Order matters. Take the matrix that will run through this topic and one more:

A=(2−111),B=(1011).A = \begin{pmatrix} 2 & -1 \\ 1 & 1 \end{pmatrix}, \qquad B = \begin{pmatrix} 1 & 0 \\ 1 & 1 \end{pmatrix}.

Then AB=(1−121)AB = \begin{pmatrix} 1 & -1 \\ 2 & 1 \end{pmatrix}, but BA=(2−130)BA = \begin{pmatrix} 2 & -1 \\ 3 & 0 \end{pmatrix}. "First BB, then AA" is a different map from "first AA, then BB". Grouping, on the other hand, is free: (AB)C=A(BC)(AB)C = A(BC), because composition is associative. Transposing reverses the order: (AB)⊤=B⊤A⊤(AB)^\top = B^\top A^\top.

One consequence for networks: two linear layers with no activation between them are no deeper than one. Without biases, W2(W1x)=(W2W1) xW_2(W_1 x) = (W_2 W_1)\,x is a single layer with the matrix W2W1W_2 W_1. That is why every hidden layer is followed by a nonlinearity.

Ax = b: the row and column pictures

A system of linear equations is a single matrix equation. The two equations

2x1−x2=1,x1+x2=52x_1 - x_2 = 1, \qquad x_1 + x_2 = 5

are Ax=bAx = b with the running matrix AA and b=(1,5)b = (1, 5). There are two ways to read them.

The row picture. Each equation is a line in the plane; in Rn\mathbb R^n it is a hyperplane. A solution is a point that lies on all of them. Here the two lines cross at x=(2,3)x = (2, 3). Lines can also be parallel or coincide, and the row picture alone does not say in advance which case you are in.

The column picture. The product AxAx mixes the columns of AA with weights taken from xx:

Ax=x1a1+x2a2,a1=(21),a2=(−11).Ax = x_1 a_1 + x_2 a_2, \qquad a_1 = \begin{pmatrix} 2 \\ 1 \end{pmatrix}, \quad a_2 = \begin{pmatrix} -1 \\ 1 \end{pmatrix}.

Solving means finding the weights that mix the columns into bb. Indeed, 2a1+3a2=(4−3, 2+3)=(1,5)2a_1 + 3a_2 = (4 - 3,\ 2 + 3) = (1, 5).

The column picture is the one that scales. In linear regression the data matrix XX has one row per example and one column per feature, and we look for weights ww with Xw=yXw = y. The row picture says that every example is one constraint on ww. The column picture says that the predictions XwXw are a mix of feature columns, so the question becomes: can the targets yy be mixed from the features at all?

Column space, null space and rank

Two subspaces of a matrix A∈Rm×nA \in \mathbb R^{m \times n} answer two questions: which outputs can the map reach, and which inputs does it lose?

The column space is the set of all outputs:

col⁡A={Ax:x∈Rn}⊆Rm.\operatorname{col} A = \{Ax : x \in \mathbb R^n\} \subseteq \mathbb R^m.

By the column picture, it is the span of the columns, the image of the map. Its dimension is the rank rank⁡A\operatorname{rank} A, the number of linearly independent columns.

The null space, or kernel, is the set of inputs that the map sends to zero:

ker⁡A={x∈Rn:Ax=0}⊆Rn.\ker A = \{x \in \mathbb R^n : Ax = 0\} \subseteq \mathbb R^n.

It is a subspace: if Ax=0Ax = 0 and Ay=0Ay = 0, then A(αx+βy)=0A(\alpha x + \beta y) = 0 too.

Here is an example from practice. Weekly sales are modeled from three features: spend on search ads, spend on social ads, and total ad spend, which someone helpfully added to the table. Four weeks of data, in thousands:

X=(101011112213).X = \begin{pmatrix} 1 & 0 & 1 \\ 0 & 1 & 1 \\ 1 & 1 & 2 \\ 2 & 1 & 3 \end{pmatrix}.

The third column is the sum of the first two, so only two columns are independent: rank⁡X=2\operatorname{rank} X = 2, and col⁡X\operatorname{col} X is a plane inside R4\mathbb R^4. The null space contains (1,1,−1)(1, 1, -1), because X(1,1,−1)=c1+c2−c3=0X(1, 1, -1) = c_1 + c_2 - c_3 = 0 for the columns c1,c2,c3c_1, c_2, c_3.

This matters for training. If weights ww fit the data, then so do w+t (1,1,−1)w + t\,(1, 1, -1) for every tt: the predictions do not change at all. The data cannot tell these weights apart, so no single coefficient measures "the effect of total spend". Dropping the redundant column or adding regularization picks one answer.

The rank–nullity theorem

Rank and null space are tied together. Each of the nn input dimensions either survives into the column space or collapses into the kernel:

rank⁡A+dim⁡ker⁡A=n.\operatorname{rank} A + \dim \ker A = n.

This is the rank–nullity theorem. For the ad-spend matrix it reads 2+1=32 + 1 = 3.

One more fact, explained in the elimination block below: a matrix has as many independent rows as independent columns. So the rank is at most the smaller side, rank⁡A≤min⁡(m,n)\operatorname{rank} A \le \min(m, n). A composition is no richer than its narrowest part: rank⁡(AB)≤min⁡(rank⁡A,rank⁡B)\operatorname{rank}(AB) \le \min(\operatorname{rank} A, \operatorname{rank} B). Every output ABx=A(Bx)ABx = A(Bx) is an output of AA, so col⁡(AB)⊆col⁡A\operatorname{col}(AB) \subseteq \operatorname{col} A. And every xx with Bx=0Bx = 0 also has ABx=0ABx = 0, so ker⁡B⊆ker⁡(AB)\ker B \subseteq \ker(AB); by rank–nullity, a kernel at least as large leaves a rank no larger than rank⁡B\operatorname{rank} B.

Two consequences for models:

  • Bottlenecks. A linear autoencoder squeezes a 784784-pixel image into a 3232-number code and back: x↦WdecWenc xx \mapsto W_{\text{dec}} W_{\text{enc}}\, x with Wenc∈R32×784W_{\text{enc}} \in \mathbb R^{32 \times 784}. The 784×784784 \times 784 product has rank at most 3232, so by rank–nullity its kernel has dimension at least 752752. Those input directions are lost and cannot be reconstructed.
  • Low-rank updates. LoRA fine-tunes a d×dd \times d weight matrix by adding ΔW=BC\Delta W = BC with B∈Rd×rB \in \mathbb R^{d \times r}, C∈Rr×dC \in \mathbb R^{r \times d} and r≪dr \ll d. The update has rank at most rr and costs 2dr2dr parameters instead of d2d^2. For d=4096d = 4096 and r=8r = 8 that is 65,53665{,}536 numbers instead of 16,777,21616{,}777{,}216.

When Ax = b has a solution

Now the main question. The system Ax=bAx = b has a solution exactly when b∈col⁡Ab \in \operatorname{col} A: the target must be a mix of the columns. If xpx_p is one solution (a particular solution), then all solutions are

x=xp+z,z∈ker⁡A.x = x_p + z, \qquad z \in \ker A.

The reason is one line: Ax=bAx = b and Axp=bAx_p = b give A(x−xp)=0A(x - x_p) = 0. So existence is decided by the column space and uniqueness by the null space. The solution is unique exactly when ker⁡A={0}\ker A = \{0\}, that is, when rank⁡A=n\operatorname{rank} A = n.

An equivalent test is rank⁡ [A∣b]=rank⁡A\operatorname{rank}\,[A \mid b] = \operatorname{rank} A: appending bb as an extra column must not add a new independent direction.

Back to the ad-spend data. Every output of XX has the form

Xw=(s, u, s+u, 2s+u),s=w1+w3,u=w2+w3.Xw = (s,\ u,\ s + u,\ 2s + u), \qquad s = w_1 + w_3, \quad u = w_2 + w_3.

The sales vector y=(1,1,2,3)y = (1, 1, 2, 3) has this form with s=u=1s = u = 1. One solution is wp=(1,1,0)w_p = (1, 1, 0), and all of them are

w=(1,1,0)+t (1,1,−1),t∈R.w = (1, 1, 0) + t\,(1, 1, -1), \qquad t \in \mathbb R.

The vector y=(1,1,2,4)y = (1, 1, 2, 4) does not have this form. Its first two coordinates force s=u=1s = u = 1, and then the last one would have to be 33. No weights fit those four weeks exactly.

Gaussian elimination

How do we find ranks, solutions and kernels in practice? With Gaussian elimination. Its one operation is to subtract a multiple of one equation from another, which does not change the set of solutions. The goal is zeros below the diagonal.

For the running system, write the augmented matrix [A∣b][A \mid b] and subtract half of row 1 from row 2:

(2−11115)→  R2−12R1  (2−1101.54.5).\left(\begin{array}{cc|c} 2 & -1 & 1 \\ 1 & 1 & 5 \end{array}\right) \xrightarrow{\;R_2 - \frac12 R_1\;} \left(\begin{array}{cc|c} 2 & -1 & 1 \\ 0 & 1.5 & 4.5 \end{array}\right).

Now back substitution: the last row gives x2=4.5/1.5=3x_2 = 4.5 / 1.5 = 3, and the first gives 2x1=1+32x_1 = 1 + 3, so x1=2x_1 = 2. The leading entries 22 and 1.51.5 are the pivots.

The final staircase shape tells you the structure of the system:

  • the rank is the number of pivots;
  • a column without a pivot is a free variable, and each free variable gives one direction of the kernel;
  • a row that reads 0=c0 = c with c≠0c \ne 0 means there is no solution.

On the ad-spend matrix XX, elimination leaves pivots only in columns 1 and 2. Column 3 is free, and its kernel direction is (1,1,−1)(1, 1, -1).

The staircase also explains why a matrix has as many independent rows as independent columns. Every step can be undone, so elimination changes neither the span of the rows nor which combinations of columns vanish: Ax=0Ax = 0 and the eliminated system have the same solutions. In the staircase form the nonzero rows are independent, and so are the pivot columns. All columns have zeros below the last pivot row, so they lie in a space whose dimension is the number of pivots, and the pivot columns span it. Both counts equal the number of pivots.

Two practical notes. A zero pivot calls for a row swap. A tiny pivot is nearly as bad: it makes a huge multiplier, and rounding wipes out the other equations. So in floating point one always swaps in the entry of largest absolute value in the column, which is called partial pivoting. Elimination on an n×nn \times n matrix costs about n3/3n^3/3 multiply–adds, roughly 3.3⋅1083.3 \cdot 10^8 for n=1000n = 1000. The multipliers can be stored as the factorization A=LUA = LU, or PA=LUPA = LU when rows were swapped, after which every new right-hand side bb costs only about n2n^2. This is what np.linalg.solve does.

The inverse, and why it is rarely formed

Let AA be square, n×nn \times n, with rank nn. Then col⁡A=Rn\operatorname{col} A = \mathbb R^n and ker⁡A={0}\ker A = \{0\}, so Ax=bAx = b has exactly one solution for every bb. The map can be undone by the inverse matrix A−1A^{-1}, with A−1A=AA−1=IA^{-1}A = AA^{-1} = I and x=A−1bx = A^{-1}b. Read AA−1=IAA^{-1} = I column by column: column jj of A−1A^{-1} is the solution of Ax=ejAx = e_j.

For a 2×22 \times 2 matrix the inverse has a closed form:

(pqrs)−1=1ps−qr(s−q−rp),ps−qr≠0.\begin{pmatrix} p & q \\ r & s \end{pmatrix}^{-1} = \frac{1}{ps - qr}\begin{pmatrix} s & -q \\ -r & p \end{pmatrix}, \qquad ps - qr \ne 0.

The number ps−qrps - qr is the determinant, the subject of the topic Determinants and eigenvalues. For the running matrix it is 2⋅1−(−1)⋅1=32 \cdot 1 - (-1) \cdot 1 = 3, so

A−1=13(11−12),A−1(15)=(23).A^{-1} = \frac13\begin{pmatrix} 1 & 1 \\ -1 & 2 \end{pmatrix}, \qquad A^{-1}\begin{pmatrix} 1 \\ 5 \end{pmatrix} = \begin{pmatrix} 2 \\ 3 \end{pmatrix}.

Two rules follow from composition. (AB)−1=B−1A−1(AB)^{-1} = B^{-1}A^{-1}: to undo "first BB, then AA", undo AA first. And (A⊤)−1=(A−1)⊤(A^\top)^{-1} = (A^{-1})^\top.

In formulas, A−1bA^{-1}b is everywhere. In code, it is almost never computed that way. Forming A−1A^{-1} takes nn solves, one per column, about n3n^3 multiply–adds in all, three times as much as elimination. It is also less accurate and turns a sparse matrix into a dense one. The covariance term Σ−1(x−μ)\Sigma^{-1}(x - \mu) of a Gaussian density and the ridge regression weights (X⊤X+λI)−1X⊤y(X^\top X + \lambda I)^{-1}X^\top y are both computed as solves, and so is every step of Newton's method later in the course. Normalizing flows go further: Glow learns its invertible 1×11 \times 1 convolutions directly in the factored form W=PLUW = PLU, so that undoing them is cheap.

Overdetermined and underdetermined systems

Put existence and uniqueness together for A∈Rm×nA \in \mathbb R^{m \times n} with rank rr:

Shape Rank Solutions of Ax=bAx = b
square, m=nm = n r=nr = n exactly one, for every bb
tall, m>nm > n r=nr = n none or exactly one
wide, m<nm < n r=mr = m infinitely many, for every bb
any r<mr < m and r<nr < n none or infinitely many

A tall system has more equations than unknowns and is called overdetermined. This is ordinary regression: a thousand examples, a handful of features, and targets with noise. The column space is a thin subspace of Rm\mathbb R^m, and noisy yy almost never lies in it exactly.

A wide system has more unknowns than equations and is called underdetermined. This is an overparameterized model with more weights than training examples. It fits the data in infinitely many ways, which differ by vectors from a kernel of dimension at least n−mn - m.

Both cases ask for something this topic cannot yet give. In the overdetermined case we want the ww for which XwXw is closest to yy. In the underdetermined case we want the shortest of all exact solutions. Closest and shortest need lengths and angles, and those come from inner products: the topic Inner products, norms and orthogonality.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.