linearly

Lecture 09

The Four Subspaces & the Rank Theorem

One small matrix, four subspaces, four bases read straight off a single elimination, and the reason row rank always equals column rank.

102 slides / MIT 18.06, Lectures 8 to 10 / Strang §3.5

Slide 1 of 102
1 / 102

The idea in one sentence

Every matrix carries four subspaces, two in the space it reads from and two in the space it writes to, and a single run of elimination hands you a basis for all four.

One matrix, carried the whole way

Here is the matrix that will show you all four:

A=[122438616410820].A = \begin{bmatrix} 1 & 2 & 2 & 4 \\ 3 & 8 & 6 & 16 \\ 4 & 10 & 8 & 20 \end{bmatrix}.

It is the doubled-columns matrix from Lecture 7.5 with one row added, and it earns its place by repeating itself in both directions. Read it down the columns: column 3 is twice column 1, and column 4 is twice column 2. Read it across the rows: row 3 is row 1 plus row 2. Nothing below uses any other matrix.

122438616410820read downcol 3 = 2 × col 1col 4 = 2 × col 2122438616410820read acrossrow 3 = row 1 + row 2only two rows are new
Fig. 1 

The same matrix, read twice. Two of the four columns are copies, and one of the three rows is the sum of the others. Both facts point at the same number, 2. Cyan marks the columns that will hand you the null space, purple the row that will hand you the left null space, and elimination is about to confirm both.

AA has 3 rows and 4 columns. It takes a list of four numbers and returns three, so it maps R4\R^4 to R3\R^3. Four subspaces hang off it, two on each side.

On the input side, in R4\R^4, the row space C(AT)C(A\T) is every combination of the rows of AA. The transpose sits in the name because the rows of AA are the columns of ATA\T, and C()C(\cdot) always means the space some columns span. Next to it lives the null space N(A)N(A), every xx with Ax=0Ax = 0. These are the directions AA is blind to, and the ring road machine from Lecture 2 was blind to (1,1,1)(1, 1, 1).

On the output side, in R3\R^3, the column space C(A)C(A) is every combination of the columns, which by the column reading of AxAx is exactly the set of outputs AA can produce. Next to it lives the left null space N(AT)N(A\T), every yy with ATy=0A\T y = 0. Transpose that equation and it reads yTA=0y\T A = 0: the vector yy sits on the left of AA and kills it. In words, yy holds the coefficients of a combination of the rows of AA that comes out zero, and this matrix has one in plain sight, because row 1 plus row 2 minus row 3 is the zero row.

Four definitions, four sets of vectors to hunt down. One elimination finds them all.

Elimination, run once, paid four times

Reduce AA to its row reduced form RR, and carry the identity along so the record of what you did comes out on the right. Subtract 3 times row 1 from row 2 and 4 times row 1 from row 3, subtract the new row 2 from the new row 3, halve row 2, then subtract twice row 2 from row 1:

R=[102001020000],E=[41032120111],EA=R.R = \begin{bmatrix} 1 & 0 & 2 & 0 \\ 0 & 1 & 0 & 2 \\ 0 & 0 & 0 & 0 \end{bmatrix}, \qquad E = \begin{bmatrix} 4 & -1 & 0 \\ -\tfrac{3}{2} & \tfrac{1}{2} & 0 \\ -1 & -1 & 1 \end{bmatrix}, \qquad EA = R.

Two pivots, so the rank is r=2r = 2. Columns 1 and 2 are the pivot columns, columns 3 and 4 are free. The third row of AA was never new information, and RR says so with a row of zeros.

122438616410820EA = R102001020000ARthe matrix you were giventwo pivots, two free columnsthe dead row records N(Aᵀ)
Fig. 2 

One reduction, four answers. The yellow rings are the pivots, in columns 1 and 2. The cyan columns 3 and 4 are free. The row of zeros at the bottom is the left null space announcing itself.

Where the four bases come from

Each subspace takes its basis from a different place, and three of the four places are already on the page.

Row space: the pivot rows of RR

Every elimination step replaces a row by a combination of rows, so each row of RR is a combination of rows of AA. Every step is reversible, since EE has an inverse, so each row of AA is a combination of rows of RR. Two matrices whose rows are combinations of each other span the same space. So AA and RR have the same row space, and the zero row of RR contributes nothing. The rr pivot rows are a basis:

(1,0,2,0)and(0,1,0,2),dimC(AT)=2.(1, 0, 2, 0) \quad\text{and}\quad (0, 1, 0, 2), \qquad \dim C(A\T) = 2.

Both directions of that argument are on the page already. EE says how to build RR‘s rows out of AA‘s, and E1E^{-1}, which for this matrix comes out in whole numbers, says how to build AA‘s rows back out of RR‘s.

row₁ = (1, 2, 2, 4)(1, 0, 2, 0)row₂ = (3, 8, 6, 16)(0, 1, 0, 2)row₃ = (4, 10, 8, 20)(0, 0, 0, 0)rows of Arows of RE: 4 row₁ − row₂ = (1, 0, 2, 0)E⁻¹: 1 (1, 0, 2, 0) + 2 (0, 1, 0, 2) = row₁Each side is a mix of the other, so the two row spaces are one space.The dashed row is the one that died: −row₁ − row₂ + row₃ = 0.
Fig. 3 

Elimination is a two-way street. The top arrow builds a row of RR from rows of AA, using the first row of EE. The bottom arrow builds a row of AA back from rows of RR, using the first row of E1E^{-1}, which here is (1,2,0)(1, 2, 0). Neither trip leaves the row space.

Column space: the pivot columns of AA itself

Here the pattern breaks, and this is the one place readers get burned. Elimination does not preserve the column space. What it preserves is which columns depend on which, because Ax=0Ax = 0 and Rx=0Rx = 0 have the same solutions, and a dependence among columns is exactly a solution of Ax=0Ax = 0. Column 3 of RR is twice column 1 of RR, so column 3 of AA is twice column 1 of AA. The recipe carries over. The vectors do not.

So take the columns in the pivot positions from the original matrix:

(1,3,4)and(2,8,10),dimC(A)=2.(1, 3, 4) \quad\text{and}\quad (2, 8, 10), \qquad \dim C(A) = 2.

The columns of RR in those same positions are (1,0,0)(1, 0, 0) and (0,1,0)(0, 1, 0), and they span a different plane in R3\R^3. Figure 4 shows how different.

C(R)pivot columns of RC(A)pivot columns of A(1, 0, 0)(1, 0, 1)0v₁v₂v₃
Fig. 4 

Elimination moves the column space. The pink plane is spanned by the pivot columns of RR, the green one by the pivot columns of AA. The vector (1,0,0)(1, 0, 0) lies in the first and misses the second by one full unit, which is why a column space basis has to be read off AA.

Null space: the special solutions

Lecture 7.5 built these. Set one free variable to 1, the rest to 0, and solve Rx=0Rx = 0 upward. With x3=1x_3 = 1 the first row gives x1+2=0x_1 + 2 = 0, so x1=2x_1 = -2. With x4=1x_4 = 1 the second row gives x2+2=0x_2 + 2 = 0, so x2=2x_2 = -2:

s1=(2,0,1,0)ands2=(0,2,0,1),dimN(A)=nr=2.s_1 = (-2, 0, 1, 0) \quad\text{and}\quad s_2 = (0, -2, 0, 1), \qquad \dim N(A) = n - r = 2.

One special solution per free column, so the count is forced to be nrn - r.

Left null space: the last mrm - r rows of EE

This is the one people never remember, and it is the easiest of the four once you know where to look. EA=REA = R says that row ii of EE times AA gives row ii of RR. The bottom mrm - r rows of RR are zero. So the bottom mrm - r rows of EE, applied to AA, produce zero rows. Those rows are precisely the combinations of AA‘s rows that vanish, which is what the left null space is.

Row 3 of EE is (1,1,1)(-1, -1, 1), and sure enough row1row2+row3=0-\text{row}_1 - \text{row}_2 + \text{row}_3 = 0:

(1,1,1),dimN(AT)=mr=1.(-1, -1, 1), \qquad \dim N(A\T) = m - r = 1.
−1−11122438616410820=0000last row of EAa row of zeros−1 (1, 2, 2, 4) − 1 (3, 8, 6, 16) + 1 (4, 10, 8, 20) = (0, 0, 0, 0)the combination of rows that vanishes, which is what N(Aᵀ) collects
Fig. 5 

Why the left null space sits in EE. A row of EE times AA gives that row of RR, and RR‘s last row is zero, so EE‘s last row is a recipe for cancelling the rows of AA against each other. Reading it out loud gives the dependence you spotted in Figure 1.

Athe matrix you were givenRrow reduced formEthe record, EA = Rcolumn space C(A)(1, 3, 4) (2, 8, 10)dim 2, in R³row space C(Aᵀ)(1, 0, 2, 0) (0, 1, 0, 2)dim 2, in R⁴null space N(A)(−2, 0, 1, 0) (0, −2, 0, 1)dim 2, in R⁴left null space N(Aᵀ)(−1, −1, 1)dim 1, in R³pivot columnspivot rowsspecial solutionslast m − r rows
Fig. 6 

The whole chapter on one card, and the color key for the rest of the course: green is the column space, orange the row space, cyan the null space, purple the left null space, yellow the pivots. Three matrices come out of one elimination, and each hands over a different basis. Only the column space is read off AA; the other three are read off the leftovers.

The big picture

Now put the four in one frame. On the input side, R4\R^4 holds the row space of dimension r=2r = 2 and the null space of dimension nr=2n - r = 2, and 2+2=42 + 2 = 4. On the output side, R3\R^3 holds the column space of dimension r=2r = 2 and the left null space of dimension mr=1m - r = 1, and 2+1=32 + 1 = 3. The counts close on both sides, which is the first sign that nothing has been left out.

Every vector xx in R4\R^4 splits into a row space piece xrx_r and a null space piece xnx_n:

x=xr+xn,Ax=Axr+Axn=Axr.x = x_r + x_n, \qquad A x = A x_r + A x_n = A x_r .

The null piece dies on the way through. Whatever AA produces, it produces from the row space alone. Take x=(1,1,1,1)x = (1, 1, 1, 1). Its two pieces are

xr=(35,35,65,65),xn=(25,25,15,15),x_r = \left(\tfrac{3}{5}, \tfrac{3}{5}, \tfrac{6}{5}, \tfrac{6}{5}\right), \qquad x_n = \left(\tfrac{2}{5}, \tfrac{2}{5}, -\tfrac{1}{5}, -\tfrac{1}{5}\right),

and you can check by hand that xrx_r is 35\tfrac{3}{5} of the first row space basis vector plus 35\tfrac{3}{5} of the second, that xnx_n is 15s115s2-\tfrac{1}{5}s_1 - \tfrac{1}{5}s_2, and that Ax=Axr=(9,33,42)Ax = Ax_r = (9, 33, 42) while Axn=0Ax_n = 0.

row space C(Aᵀ)dim r = 2pivot rows of Rnull space N(A)dim n − r = 2special solutionscolumn space C(A)dim r = 2pivot columns of Aleft null space N(Aᵀ)dim m − r = 1last rows of EA xᵣ = A x = bA xₙ = 0xᵣxₙx = xᵣ + xₙb00R⁴
Fig. 7 

The big picture. Green is the column space, orange the row space, cyan the null space, purple the left null space, and blue is what AA does. Solid regions have dimension rr. On the left 2+2=42 + 2 = 4, on the right 2+1=32 + 1 = 3. A vector xx splits into xrx_r and xnx_n, the null piece dies, and the row piece carries the whole answer, so AxAx and AxrAx_r land on the same bb.

Every later chapter of this course points back at this frame. Projections land inside the column space. Least squares takes a bb that is out of reach and moves it onto that space. Eigenvectors are found by asking which null space they sit in. Learn the four rooms once and the rest of the course has an address system.

Try it. Click a room
R⁴AArow spacedim 2null spacedim 2column spacedim 2left null spacedim 100

Row space C(Aᵀ)

Lives in R⁴, dimension 2.

(1, 0, 2, 0)
(0, 1, 0, 2)

Basis read off the pivot rows of R.

Every attention head in a transformer carries these four spaces, and the numbers are lopsided enough to change how you picture a layer.

the residual stream, 4,096 dimsC(W_Qᵀ), dim 128, what this head readsN(W_Q)dim 3,9683,968 of the 4,096 directions are invisible to itW_Q128 × 4096the head’s query space, 128 dimsC(W_Q), dim 128, all of itN(W_Qᵀ) = 0, nothing is wasted128 + 3,968 = 4,096: every input direction is accounted for
Fig. 8 

One attention head, drawn to scale: a unit of height is 16 dimensions. WQW_Q carries the 4,096 numbers of the residual stream down to the head’s own 128, so 3,968 input directions land on zero and never reach this head. The orange cap is the row space, the cyan below it is the null space, and the two add to 4,096. On the output side the head fills all 128 dimensions it has, so its left null space is a single point.

Why the two dimensions match

Look at the two dimension labels in Figure 7 again. Both say 2, and that is no accident of this matrix. For every matrix of every shape, the row space and the column space have the same dimension, and that shared number is what the word rank means.

It sounds like it should be hard to prove. A 3×43 \times 4 matrix has rows of length 4 and columns of length 3, so the two spaces do not even live in the same place. The proof takes two paragraphs and one factorization.

Start with the column space, since a basis for it is already sitting inside AA. Call its dimension kk. Put a basis for C(A)C(A) into the columns of a matrix BB. Every column of AA is some combination of those kk vectors, and if you write the coefficients for column jj down the jj-th column of a new matrix CC, the column reading of matrix multiplication says exactly A=BCA = BC. For our matrix, k=2k = 2 and both factors are already computed:

A=BC=[1238410][10200102].A = BC = \begin{bmatrix} 1 & 2 \\ 3 & 8 \\ 4 & 10 \end{bmatrix} \begin{bmatrix} 1 & 0 & 2 & 0 \\ 0 & 1 & 0 & 2 \end{bmatrix}.

BB is 3×23 \times 2 and holds the pivot columns of AA. CC is 2×42 \times 4 and holds the pivot rows of RR, which is the same thing as the recipe for rebuilding every column. Multiply them and AA comes back.

Now read the same product the other way. The row reading of BCBC says row ii of AA is a combination of the rows of CC, with row ii of BB supplying the coefficients. Row 3 of BB is (4,10)(4, 10), so

row3(A)  =  4(1,0,2,0)+10(0,1,0,2)  =  (4,10,8,20).\text{row}_3(A) \;=\; 4\,(1, 0, 2, 0) + 10\,(0, 1, 0, 2) \;=\; (4, 10, 8, 20).

Every row of AA is a combination of the kk rows of CC. So the row space sits inside the span of kk vectors, and such a space has dimension at most kk:

dimC(AT)    k  =  dimC(A).\dim C(A\T) \;\le\; k \;=\; \dim C(A).
122438616410820=123841010200102A (3 × 4)B (3 × 2)C (2 × 4)read by columns: col 3 of A = 2 (col 1 of B) + 0 (col 2 of B)every column of A is a mix of B’s 2 columns, so dim C(A) ≤ 2read by rows: row 3 of A = 4 (row 1 of C) + 10 (row 2 of C)every row of A is a mix of C’s 2 rows, so dim C(Aᵀ) ≤ 2
Fig. 9 

One product, two readings, and the rank theorem falls out. The green boxes are the column reading, the orange boxes the row reading. Because BB has only 2 columns and CC has only 2 rows, both spaces are trapped at dimension 2.

That is one inequality. For the other, run the identical argument on ATA\T. A basis for its column space sits in its first two columns, so

AT=[132826416][101011],A\T = \begin{bmatrix} 1 & 3 \\ 2 & 8 \\ 2 & 6 \\ 4 & 16 \end{bmatrix} \begin{bmatrix} 1 & 0 & 1 \\ 0 & 1 & 1 \end{bmatrix},

and the same reading gives dimC(A)dimC(AT)\dim C(A) \le \dim C(A\T), because the row space of ATA\T is the column space of AA and the column space of ATA\T is the row space of AA. Two inequalities pointing at each other leave one possibility:

dimC(AT)  =  dimC(A)  =  r.\dim C(A\T) \;=\; \dim C(A) \;=\; r .

Checking it in code

Everything above is small enough to check by hand and small enough to check in four lines. The first block confirms the four bases. Watch EE do two jobs at once: it certifies the reduction, and its last row is the left null space.

python
import numpy as np

A = np.array([[1., 2., 2., 4.],
              [3., 8., 6., 16.],
              [4., 10., 8., 20.]])
R = np.array([[1., 0., 2., 0.],
              [0., 1., 0., 2.],
              [0., 0., 0., 0.]])
E = np.array([[4., -1., 0.],
              [-1.5, 0.5, 0.],
              [-1., -1., 1.]])

print(np.linalg.matrix_rank(A))     # 2
print(np.allclose(E @ A, R))        # True   the record checks out

s1 = np.array([-2., 0., 1., 0.])    # special solutions -> null space
s2 = np.array([0., -2., 0., 1.])
print(A @ s1, A @ s2)               # [0. 0. 0.] [0. 0. 0.]

y = np.array([-1., -1., 1.])        # last row of E -> left null space
print(A.T @ y)                      # [0. 0. 0. 0.]

The second block is the rank theorem itself. There is almost nothing to it once A=BCA = BC is on the table, because the shapes of BB and CC are the whole argument.

python
import numpy as np

A = np.array([[1., 2., 2., 4.], [3., 8., 6., 16.], [4., 10., 8., 20.]])

B = A[:, :2]                                        # pivot columns of A
C = np.array([[1., 0., 2., 0.], [0., 1., 0., 2.]])  # pivot rows of R
print(np.allclose(B @ C, A))                        # True
print(B.shape, C.shape)                             # (3, 2) (2, 4)

print(2 * B[:, 0] + 0 * B[:, 1])   # [2. 6. 8.]        = column 3 of A
print(4 * C[0] + 10 * C[1])        # [ 4. 10.  8. 20.] = row 3 of A

print(np.linalg.matrix_rank(A), np.linalg.matrix_rank(A.T))   # 2 2
print(A.shape[1] - np.linalg.matrix_rank(A))        # 2 = dim N(A)
print(A.shape[0] - np.linalg.matrix_rank(A))        # 1 = dim N(A.T)

Where this is going

You now have the four subspaces, their dimensions, and a basis for each, all out of a single elimination. What you do not have is the geometry. Figure 7 drew the row space above the null space as though they were two halves of R4\R^4, and it split xx into two pieces as though there were only one way to do it. Both of those are true, and both are true for the same reason: the row space and the null space meet at a right angle, and so do the column space and the left null space. That right angle is the next chapter, and it turns this filing cabinet into the fundamental theorem of linear algebra.