linearly

Lecture 08

The Complete Solution & Rank

One anchor plus the whole null space. Whether b fits at all, and how a single number, the rank, tells you how many answers to expect before you compute one.

103 slides / MIT 18.06, Lecture 8 / Strang §3.3–3.5

Slide 1 of 103
1 / 103

The idea in one sentence

Find one solution of Ax=bAx = b, add every vector in the null space to it, and you have found them all.

One anchor, and the whole null space

Lecture 7.5 ended with a shape and no formula. The solutions of Ax=bAx = b form an affine set, a flat sheet pushed off the origin. That left one question open: what does the pushing?

The answer takes one line. Suppose xpx_p is any single vector with Axp=bA x_p = b. Call it the particular solution. Suppose xnx_n is any vector in the null space, so Axn=0A x_n = 0. Then

A(xp+xn)=Axp+Axn=b+0=b.A(x_p + x_n) = A x_p + A x_n = b + 0 = b .

So xp+xnx_p + x_n solves the system too. Every choice of xnx_n gives a solution, and there are no others: if xx and xpx_p both solve the system then A(xxp)=bb=0A(x - x_p) = b - b = 0, which puts xxpx - x_p in the null space. Write xn=xxpx_n = x - x_p and you are back where you started.

x=xp+xn,xnN(A).x = x_p + x_n , \qquad x_n \in N(A) .

One anchor, and a whole subspace of drift. Watch it in a case small enough to draw. Take

A=[1224],b=(3,6).A = \begin{bmatrix} 1 & 2 \\ 2 & 4 \end{bmatrix}, \qquad b = (3, 6) .

The second row is twice the first, so both equations say the same thing, x1+2x2=3x_1 + 2x_2 = 3. One solution is xp=(3,0)x_p = (3, 0). The null space is the line through xn=(2,1)x_n = (-2, 1), since 2+2=0-2 + 2 = 0. The complete solution is x=(3,0)+t(2,1)x = (3, 0) + t(-2, 1) for every real tt.

xpxnxp + xn0N(A)xp + N(A)
Fig. 1 

The dashed line is the null space, which passes through the origin. The blue line is the whole solution set, the same line pushed over so it passes through xpx_p. Orange is the anchor, green is the drift you add to it. Land on the blue line once and you can slide along it forever.

Notice what changes when bb changes and what does not. The direction of the drift belongs to AA alone, so every solvable bb produces a line parallel to that dashed one. Only the anchor moves.

b = (−2, −4)b = (0, 0)b = (3, 6)0same direction every time
Fig. 2 

Three right-hand sides, three parallel lines. The dashed one runs through the origin and is N(A)N(A) itself, the case b=0b = 0. Orange and blue are the other two, colored apart only so you can follow them. They are the same line slid sideways, and no choice of bb can ever tilt it.

Try it. Slide along the solution line
N(A)xₚxₚ + N(A)

x = xₚ + t xₙ = (0.6, 1.2)

Ax = (3.0, 6.0)
b = (3.0, 6.0)

‖Ax − b‖ = 0.00

Every t on the blue line solves the system.

Does b even fit?

The recipe assumed a particular solution exists. Often it does not, and elimination tells you which case you are in without any extra work. Put bb next to AA as one more column and reduce the whole thing:

A=[130200141316],b=(1,6,7).A = \begin{bmatrix} 1 & 3 & 0 & 2 \\ 0 & 0 & 1 & 4 \\ 1 & 3 & 1 & 6 \end{bmatrix}, \qquad b = (1, 6, 7) .

Row 3 of AA is row 1 plus row 2. Whatever the third equation claims, it is the sum of the first two, so the only value of b3b_3 that can possibly work is b1+b2=7b_1 + b_2 = 7. It is 7. Subtract rows 1 and 2 from row 3 of the augmented matrix and the last row goes completely flat, right side included.

130210014600000130210014600001b = (1, 6, 7)b = (1, 6, 8)0 = 00 = 1true, so solutions existfalse, so there are none
Fig. 3 

The same matrix, two right-hand sides. Elimination flattens the third row either way, and what sits to the right of the divider decides everything. A zero there is the harmless statement 0=00 = 0. Anything else is a contradiction, which is what the pink row is.

That is the whole consistency test. Reduce [Ab][A \mid b], look at every row of the reduced matrix that is all zeros on the left, and check that its right-hand entry is zero too. If one of them is not, some combination of your equations says 0=10 = 1 and no xx on earth will fix it.

When the test passes, the particular solution comes out almost for free. Set every free variable to zero. In the reduced matrix above the pivots sit in columns 1 and 3, so x2x_2 and x4x_4 are free. Setting both to zero leaves x1=1x_1 = 1 and x3=6x_3 = 6, which is exactly the right-hand column:

xp=(1,0,6,0).x_p = (1, 0, 6, 0) .

Check it: the first equation gives 1+0+0+0=11 + 0 + 0 + 0 = 1, the second gives 0+0+6+0=60 + 0 + 6 + 0 = 6, and the third gives 1+0+6+0=71 + 0 + 6 + 0 = 7. Now add the null space. The free columns give two special solutions by the method of Lecture 7.5, s1=(3,1,0,0)s_1 = (-3, 1, 0, 0) and s2=(2,0,4,1)s_2 = (-2, 0, -4, 1), so the complete solution is

x=(1,0,6,0)+c(3,1,0,0)+d(2,0,4,1).x = (1, 0, 6, 0) + c\,(-3, 1, 0, 0) + d\,(-2, 0, -4, 1) .

An anchor in R4\R^4, plus a two-dimensional sheet of drift, for any cc and dd you like.

python
import numpy as np

A = np.array([[1., 3., 0., 2.],
              [0., 0., 1., 4.],
              [1., 3., 1., 6.]])       # row 3 = row 1 + row 2
b = np.array([1., 6., 7.])             # and 7 = 1 + 6, so it fits

M = np.hstack([A, b.reshape(-1, 1)])   # the augmented matrix
M[2] -= M[0] + M[1]                    # the only step elimination needs
print(M)                    # [[1. 3. 0. 2. 1.]
                            #  [0. 0. 1. 4. 6.]
                            #  [0. 0. 0. 0. 0.]]   <- 0 = 0, consistent

xp = np.array([1., 0., 6., 0.])        # free variables set to zero
s1 = np.array([-3., 1., 0., 0.])       # switch on x2
s2 = np.array([-2., 0., -4., 1.])      # switch on x4
print(A @ xp, A @ s1, A @ s2)          # [1. 6. 7.] [0. 0. 0.] [0. 0. 0.]
for c, d in [(0., 0.), (2., -1.), (-0.5, 3.)]:
    print(A @ (xp + c * s1 + d * s2))  # [1. 6. 7.] every time

bad = np.array([1., 6., 8.])           # 8 is not 1 + 6
Mbad = np.hstack([A, bad.reshape(-1, 1)])
Mbad[2] -= Mbad[0] + Mbad[1]
print(Mbad[2])                         # [0. 0. 0. 0. 1.]  <- 0 = 1, no good
print(np.linalg.matrix_rank(A), np.linalg.matrix_rank(Mbad))   # 2 3

Rank

Everything above turned on two counts: how many pivots the matrix has, and how many columns are left over. The first count has a name.

The rank rr of a matrix is the number of pivots elimination produces. Equivalently, it is the number of columns that carry information no earlier column already carried, which makes it the dimension of the column space C(A)C(A). The rows tell the same story, so rr is also the dimension of the row space C(AT)C(A\T). That last claim is the one surprise in this chapter, and Lecture 9 proves it properly.

Two consequences you already used. The null space has dimension nrn - r, one direction per free column. And a system has a solution exactly when adding bb as an extra column leaves the rank alone, since a column that raises the rank is a column the old ones could not reach. So Ax=bAx = b is solvable precisely when

rank[Ab]=rankA.\rank\begin{bmatrix} A & b \end{bmatrix} = \rank A .

Rank is bounded by both dimensions, so rmr \le m and rnr \le n. Those two comparisons, each either tight or slack, sort every matrix into four cases.

Four shapes, four answers

r = nr < nr = mr < msquare and invertibleexactly one solutionshort and wide, m < nalways infinitely manytall and thin, n < mnone, or exactly onenot full ranknone, or infinitely many
Fig. 4 

Two questions, four answers. Cyan marks a free column, which gives the answer set room to move. Pink marks a zero row, which is where bb can fail. Case 1 has neither and case 4 has both, and you can read every verdict off which colors are present.

Case 1, r=m=nr = m = n. Square with a pivot in every row and column. No free columns means N(A)={0}N(A) = \lbrace 0 \rbrace and no drift, and no zero rows means bb always fits. Exactly one solution, for every bb. Take

A=[1238],b=(5,17).A = \begin{bmatrix} 1 & 2 \\ 3 & 8 \end{bmatrix}, \qquad b = (5, 17) .

Eliminate and back-substitute to get x=(3,1)x = (3, 1), and check: 3+2=53 + 2 = 5 and 9+8=179 + 8 = 17. There is nothing to add, because the null space holds only the zero vector. This is the invertible case from Lecture 5, seen from the rank side.

Case 2, r=m<nr = m < n. Short and wide with a pivot in every row, so elimination never produces a zero row and bb always fits. But nrn - r free columns remain, so the answer is never unique. Infinitely many solutions, for every bb. Take

A=[123014],b=(6,5).A = \begin{bmatrix} 1 & 2 & 3 \\ 0 & 1 & 4 \end{bmatrix}, \qquad b = (6, 5) .

Subtract twice row 2 from row 1 to reach R=[105014]R = \begin{bmatrix} 1 & 0 & -5 \\ 0 & 1 & 4 \end{bmatrix} and d=(4,5)d = (-4, 5). Column 3 is free, so xp=(4,5,0)x_p = (-4, 5, 0) and s=(5,4,1)s = (5, -4, 1). Check the special solution: 58+3=05 - 8 + 3 = 0 and 04+4=00 - 4 + 4 = 0. The complete solution is (4,5,0)+t(5,4,1)(-4, 5, 0) + t\,(5, -4, 1).

Case 3, r=n<mr = n < m. Tall and thin with a pivot in every column. No free columns means at most one solution, and the leftover rows mean bb has to cooperate. No solution or exactly one. Take

A=[100111].A = \begin{bmatrix} 1 & 0 \\ 0 & 1 \\ 1 & 1 \end{bmatrix} .

Its column space is the plane of all (u,v,u+v)(u, v, u+v), which is the set of vectors satisfying b1+b2b3=0b_1 + b_2 - b_3 = 0. So b=(2,3,5)b = (2, 3, 5) works, with the unique answer x=(2,3)x = (2, 3), and b=(2,3,6)b = (2, 3, 6) does not, because 2+36=12 + 3 - 6 = -1. One extra unit in the third slot and the target lifts clean off the plane.

a₁a₂b = (2, 3, 5)b = (2, 3, 6)0off the plane by (0, 0, 1)C(A): every mix of the two columns
Fig. 5 

Two columns in R3\R^3 span a plane, never all of space. The green target sits on the plane and has exactly one recipe, since the columns are independent. The orange one sits above it and has none, no matter how you mix.

Case 4, r<mr < m and r<nr < n. Neither full. Free columns bring back the drift, leftover rows bring back the consistency test, and you get the worst of both. No solution, or infinitely many. This case is usually left symbolic. Here it is with real numbers:

A=[123246124].A = \begin{bmatrix} 1 & 2 & 3 \\ 2 & 4 & 6 \\ 1 & 2 & 4 \end{bmatrix} .

Row 2 is twice row 1, so r=2r = 2 while m=n=3m = n = 3. With b=(1,2,3)b = (1, 2, 3) the second equation is twice the first, which is consistent, and subtracting the first equation from the third gives x3=2x_3 = 2, leaving x1+2x2=5x_1 + 2x_2 = -5. One answer is xp=(5,0,2)x_p = (-5, 0, 2), the null space is the line through (2,1,0)(-2, 1, 0), and the complete solution is (5,0,2)+t(2,1,0)(-5, 0, 2) + t\,(-2, 1, 0). Check the anchor: 5+0+6=1-5 + 0 + 6 = 1, then 10+0+12=2-10 + 0 + 12 = 2, then 5+0+8=3-5 + 0 + 8 = 3. With b=(1,3,3)b = (1, 3, 3) instead, the second equation demands 3 where the first forces 2. Nothing works.

The measurements table

Here is a table you will meet again. Five people, two measurements each, and one target column:

weight wwheight hhtarget
523
348
287
159
472

The question is the one from Lecture 1, asked with real data. Is there a pair of numbers x=(x1,x2)x = (x_1, x_2) with x1w+x2hx_1 w + x_2 h equal to the target column? That is Ax=bAx = b with AA the 5 by 2 matrix of measurements, so m=5m = 5 and n=2n = 2. The two columns are independent, so r=2r = 2 and this is case 3: tall and thin, no solution or exactly one.

It is the no-solution branch. The rank of AA is 2 and the rank of AA with the target attached is 3, so the target column adds something the measurements never had. In geometry: C(A)C(A) is a two-dimensional plane sitting inside R5\R^5, and the target sits off it.

whthe targetthe miss0C(A), a plane in R⁵
Fig. 6 

Two columns of five numbers each span the green plane inside R5\R^5. The orange target is a vector in R5\R^5 that does not lie on that plane, so Ax=bAx = b has no solution and the dashed gap can never be closed to zero.

That is the normal situation for every real dataset. Five equations and two unknowns almost never agree. Lecture 11 measures the gap, and Lecture 12 makes it as small as possible, which is what fitting a model means. Until then it is enough to know that the answer is honestly “none”, and that “none” has a size.

python
import numpy as np

square = (np.array([[1., 2.], [3., 8.]]), np.array([5., 17.]))
wide   = (np.array([[1., 2., 3.], [0., 1., 4.]]), np.array([6., 5.]))
tall   = (np.array([[1., 0.], [0., 1.], [1., 1.]]), np.array([2., 3., 5.]))
flat   = (np.array([[1., 2., 3.], [2., 4., 6.], [1., 2., 4.]]),
          np.array([1., 2., 3.]))
table  = (np.array([[5., 2.], [3., 4.], [2., 8.], [1., 5.], [4., 7.]]),
          np.array([3., 8., 7., 9., 2.]))

cases = {"square, invertible": square, "short and wide": wide,
         "tall and thin": tall, "not full rank": flat,
         "measurements table": table}

for name, (A, b) in cases.items():
    m, n = A.shape
    r = np.linalg.matrix_rank(A)
    fits = np.linalg.matrix_rank(np.hstack([A, b.reshape(-1, 1)])) == r
    how = "none" if not fits else ("one" if r == n else "infinitely many")
    print(f"{name:20s} m={m} n={n} r={r} fits:{str(fits):6s}{how}")

# square, invertible   m=2 n=2 r=2 fits:True  one
# short and wide       m=2 n=3 r=2 fits:True  infinitely many
# tall and thin        m=3 n=2 r=2 fits:True  one
# not full rank        m=3 n=3 r=2 fits:True  infinitely many
# measurements table   m=5 n=2 r=2 fits:False none

A, b = table
best = np.linalg.pinv(A) @ b     # the closest attempt, Lecture 12's answer
print(A @ best - b)
# [-0.84187449 -4.06166073  0.66072897 -4.22554124  4.82460948]
print(np.linalg.norm(A @ best - b))   # 7.666450205657343

Basis, independence, dimension

Three words have been doing quiet work throughout, and they deserve one paragraph each.

Vectors v1,,vkv_1, \dots, v_k are independent when the only combination of them equal to zero is the one with all coefficients zero. Stack them as the columns of a matrix and that sentence becomes a null space statement: the columns are independent exactly when N(A)={0}N(A) = \lbrace 0 \rbrace. Free columns are the dependent ones, and each free column hands you a nonzero vector in the null space that proves the dependence.

A set spans a space when every vector in that space is some combination of the set. A basis is a set that does both jobs, independent and spanning. Independence keeps it from being wasteful and spanning keeps it from being short, and together they buy you something worth having: in terms of a basis, every vector in the space has exactly one recipe. Two different recipes for the same vector would subtract to a nonzero combination equal to zero.

v₁v₂v₁ againv₂ww = 2v₁ + v₂, and nothing else works
Fig. 7 

The two orange vectors are the basis, the blue steps are the walk, and green is where it lands: w=(7,4)w = (7, 4) from two steps of v1=(3,1)v_1 = (3, 1) and one of v2=(1,2)v_2 = (1, 2). Independence is what makes the walk unique. Throw in a third vector like v1+v2v_1 + v_2 and the same ww suddenly has many walks, which is exactly what dependence costs you.

Every basis of a given space has the same number of vectors, and that number is the dimension. For C(A)C(A) the pivot columns of AA form a basis, so the dimension is rr. For N(A)N(A) the special solutions form a basis, so the dimension is nrn - r. The space {0}\lbrace 0 \rbrace has dimension 0 and its basis is the empty set, which is the only sensible answer: the zero vector can never belong to a basis, since 11 times it is already a nonzero combination equal to zero.

Where this is going

Count the dimensions on both sides of AA. On the input side Rn\R^n holds the null space with nrn - r dimensions and the row space C(AT)C(A\T) with rr. On the output side Rm\R^m holds the column space C(A)C(A) with rr, and one more space: the vectors yy with ATy=0A\T y = 0. That one is the left null space N(AT)N(A\T), and it has mrm - r dimensions. Four spaces, two on each side, and the counts add up exactly.

RⁿRᵐAC(Aᵀ), the row spacedimension rN(A), the null spacedimension n − rC(A), the columnsdimension rN(Aᵀ), the left null spacedimension m − rr + (n − r) = n on the left, r + (m − r) = m on the right
Fig. 8 

A thumbnail of the four fundamental subspaces. The four colors are fixed for the whole course: orange row space, cyan null space, green column space, purple left null space. Lecture 9 finds a basis for each one and explains why both rr counts are the same number. It also draws the version of this picture that everything later refers back to.

Two of those four spaces you have already met and can compute. The other two arrive next, along with the reason the two rr counts match. After that comes the question this chapter left open: when bb misses the column space, as it did for the measurements table, what is the closest thing to an answer?