linearly

Lecture 05

The Algebra of Matrices

Four readings of AB on one small example, why order matters and grouping does not, and what it takes to undo a matrix at all.

104 slides / MIT 18.06, Lecture 3 / Strang §2.4–2.5

Slide 1 of 104
1 / 104

The idea in one sentence

There are four ways to read the product ABAB, they always agree on the numbers, and the three you were not taught in school are the ones you will think with.

One product, four readings

Everything in this chapter runs on one pair:

A=[123456],B=[102103],AB=[5111423].A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix}, \qquad B = \begin{bmatrix} 1 & 0 \\ 2 & 1 \\ 0 & 3 \end{bmatrix}, \qquad AB = \begin{bmatrix} 5 & 11 \\ 14 & 23 \end{bmatrix}.

Four times now you will get (5,11,14,23)(5, 11, 14, 23) back. What changes each time is which piece of AA meets which piece of BB, and therefore what the product is telling you.

row × column123×120=51×1 + 2×2 + 3×0 = 5column × row14×10=1040column 1 × row 1, one layer of threematrix × column123456×120=5141(1,4) + 2(2,5) + 0(3,6) = (5,14)row × matrix123×102103=5111(1,0) + 2(2,1) + 3(0,3) = (5,11)
Fig. 1 

Four cameras on one product. Orange is what you take from AA and BB, green is what comes out, and that pairing holds for the rest of the chapter. Each reading hands back a different shape: one entry, one whole layer, one column, one row. Three of them are pieces of [[5,11],[14,23]][[5, 11], [14, 23]] you can read straight off; the second is a layer, and three layers add to the same answer.

Row times column. The school rule. Entry (i,j)(i,j) of ABAB is row ii of AA dotted with column jj of BB:

(AB)11=11+22+30=5,(AB)22=40+51+63=23.(AB)_{11} = 1 \cdot 1 + 2 \cdot 2 + 3 \cdot 0 = 5, \qquad (AB)_{22} = 4 \cdot 0 + 5 \cdot 1 + 6 \cdot 3 = 23.

Four dot products, four numbers. This is how you compute by hand and how the innermost loop of a matrix library is written. It says nothing about what the product means.

Draw it once and the mechanics stop being a rule to remember. Put BB above the answer and AA beside it, and the row and the column you are pairing run straight into the cell they build.

10210312345651114231 × 1k = 12 × 2k = 23 × 0k = 3= 5ABAB
Fig. 2 

Where one entry comes from. The orange row of AA runs right, the orange column of BB runs down, and the green cell is where they meet. The three products are stacked on the right, one for each row of BB, so the index kk that vanishes in the sum is the one running down the page.

Column times row. Take column kk of AA and row kk of BB, multiply them the wide way, and add up over kk:

AB=[14][10]+[25][21]+[36][03]=[1040]+[42105]+[09018].AB = \begin{bmatrix} 1 \\ 4 \end{bmatrix}\begin{bmatrix} 1 & 0 \end{bmatrix} + \begin{bmatrix} 2 \\ 5 \end{bmatrix}\begin{bmatrix} 2 & 1 \end{bmatrix} + \begin{bmatrix} 3 \\ 6 \end{bmatrix}\begin{bmatrix} 0 & 3 \end{bmatrix} = \begin{bmatrix} 1 & 0 \\ 4 & 0 \end{bmatrix} + \begin{bmatrix} 4 & 2 \\ 10 & 5 \end{bmatrix} + \begin{bmatrix} 0 & 9 \\ 0 & 18 \end{bmatrix}.

Add the three and you get ABAB again. Each piece is a whole matrix built from one column and one row, so both of its columns are multiples of (1,4)(1,4), or of (2,5)(2,5), or of (3,6)(3,6). A matrix like that is called rank one: it has one direction in it, repeated. This reading says ABAB is a sum of three rank-one layers. It is the least obvious of the four and by far the most valuable later; the whole of Part VI is about which layers of a matrix carry the information and which can be thrown away.

104042105090185111423++=col 1 × row 1col 2 × row 2col 3 × row 3ABthree rank-one layers
Fig. 3 

The same product as a sum of three flat pieces. Each orange layer is one column of AA meeting one row of BB, so it carries a single direction and nothing more. Part VI lives here.

Matrix times column. Column jj of ABAB is AA acting on column jj of BB, and Lecture 2 already told you what that is: a combination of the columns of AA, with the entries of that column as the weights. So column 1 of ABAB is

1[14]+2[25]+0[36]=[514].1\begin{bmatrix} 1 \\ 4 \end{bmatrix} + 2\begin{bmatrix} 2 \\ 5 \end{bmatrix} + 0\begin{bmatrix} 3 \\ 6 \end{bmatrix} = \begin{bmatrix} 5 \\ 14 \end{bmatrix}.

That has a consequence you get for free: every column of ABAB is a mix of columns of AA, so ABAB can never reach anywhere AA could not reach on its own. Multiplying on the right can only narrow the set of reachable outputs.

Row times matrix. The mirror image. Row ii of ABAB is row ii of AA used as weights on the rows of BB, so row 1 of ABAB is

1[10]+2[21]+3[03]=[511].1\begin{bmatrix} 1 & 0 \end{bmatrix} + 2\begin{bmatrix} 2 & 1 \end{bmatrix} + 3\begin{bmatrix} 0 & 3 \end{bmatrix} = \begin{bmatrix} 5 & 11 \end{bmatrix}.

This is the reading Lecture 4 leaned on the whole way through. An elimination matrix EE is the identity with one entry changed, and row 2 of EAEA is row 2 of EE used as weights on the rows of AA. Change one weight and you change one row of the answer. That is why a matrix on the left moves rows.

Move between the readings until they stop feeling like four rules and start feeling like one object seen from four sides:

Try it. One product, four readings
ABAB·=1234561021035111423Entry (1,1) is row 1 of A dotted with column 1 of B.

Orange is what you take in, green is what comes out. The numbers never change.

Order matters

Take two matrices that both do something simple to the rows of whatever they touch. SS adds row 2 into row 1. TT adds row 1 into row 2:

S=[1101],T=[1011],ST=[2111],TS=[1112].S = \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix}, \qquad T = \begin{bmatrix} 1 & 0 \\ 1 & 1 \end{bmatrix}, \qquad ST = \begin{bmatrix} 2 & 1 \\ 1 & 1 \end{bmatrix}, \qquad TS = \begin{bmatrix} 1 & 1 \\ 1 & 2 \end{bmatrix}.

Different matrices, from the same two factors. The reason is easy to say out loud. In STST the matrix TT is closer to whatever comes next, so TT acts first: row 1 gets added into row 2, and then the already-changed row 2 gets added into row 1. In TSTS the first step is the other one, so the second step works on a row nobody has touched yet. Operations that interfere cannot be reordered.

(2, 1)(1, 1)(1, 1)(1, 2)STTST acts firstS acts first
Fig. 4 

The yellow square is where both products start, and the blue arrows show where its two sides land. The green regions have the same area and different shapes. Two shears in two orders are two different machines.

Sometimes the failure is louder than that. With the AA and BB from the top of the chapter, ABAB is 2 by 2 and BABA is 3 by 3, so the question of whether they are equal never gets asked. The one pair that always commutes is a matrix and the identity: AI=IA=AAI = IA = A, because doing nothing before and doing nothing after are the same nothing.

Grouping does not matter

Order is fixed. Bracketing is free:

A(BC)=(AB)C.A(BC) = (AB)C.

The reason is that both sides compute the same double sum. Entry (i,j)(i,j) is klaikbklclj\sum_k \sum_l a_{ik} b_{kl} c_{lj} either way; the brackets only decide which sum you do first. This law looks like bookkeeping and pays like an investment.

It is what let Lecture 4 collapse the chain E32E31E21AE_{32}E_{31}E_{21}A into one matrix EE acting on AA alone. It is also the entire reason an inverse is unique, in a single line: if BA=IBA = I and AC=IAC = I, then B=BI=B(AC)=(BA)C=IC=CB = BI = B(AC) = (BA)C = IC = C. Two candidate inverses have no room to differ.

And it decides how fast your code runs.

ABvABvfirstfirst(AB)va matrix times a matrix,then a vectorA(Bv)a matrix times a vector,twiceabout 1 000 000 000 multiplicationsabout 2 000 000 multiplications
Fig. 5 

Both groupings give the same answer for 1000×10001000 \times 1000 matrices. Pink is the pair to group first if you enjoy waiting; green is the one to group first otherwise. Never build a matrix you are only going to hit with a vector.

Cutting a matrix into blocks

Nothing in the four readings cared that the pieces were single rows and columns. Cut AA into rectangular blocks instead, cut BB to match, and the same rule holds with numbers replaced by blocks:

[A11A12A21A22][B11B21]=[A11B11+A12B21A21B11+A22B21].\begin{bmatrix} A_{11} & A_{12} \\ A_{21} & A_{22} \end{bmatrix} \begin{bmatrix} B_{11} \\ B_{21} \end{bmatrix} = \begin{bmatrix} A_{11}B_{11} + A_{12}B_{21} \\ A_{21}B_{11} + A_{22}B_{21} \end{bmatrix}.

The one condition is that the vertical cuts in AA line up with the horizontal cuts in BB. If AA is split after column 2, then BB has to be split after row 2, so that A11A_{11} has as many columns as B11B_{11} has rows.

×ABcut after column 2cut after row 2the two cuts must agree
Fig. 6 

Block multiplication is the ordinary rule with numbers replaced by blocks. Orange meets orange and green meets green, which is what the blue cuts have to guarantee.

You have already used this. The augmented matrix [A    b][\,A \;|\; b\,] from Lecture 4 is a block matrix with one skinny block glued on the right, and E[A    b]=[EA    Eb]E[\,A \;|\; b\,] = [\,EA \;|\; Eb\,] is block multiplication written out. Deep learning uses the same idea at scale: a batch of inputs is stacked into one wide block so that one call to the hardware does the work of a thousand.

The shape the hardware actually wants

Blocks are not only a proof device. A library never multiplies two matrices entry by entry: it cuts the answer into tiles and builds one tile at a time, because a tile is small enough to sit in fast memory while the panels stream past it.

ABC = ABmkknrows of Acolumns of Bone tile of Cgreen is done, the loop takes the next tile
Fig. 7 

How the machine multiplies. Orange is what you take in, green is what comes out, and the yellow edge is the shared dimension kk that meets and vanishes. One tile of CC needs a panel of rows from AA and a panel of columns from BB; the loop then takes the next tile, and the next, until CC is full. This loop has a name, GEMM, and it is where the time goes.

Now put a real layer through it. A batch of 32 images, each flattened to 784 numbers, meets a weight matrix that turns 784 numbers into 128.

X, a batch of 32 imagesW, one layerY, what the layer answers3278478412812832 × 784 × 128= 3 211 264 multiply-addsin one call
Fig. 8 

One layer, one call. The 784 that both operands share is the yellow edge, and it is gone from the answer, exactly as kk was. Every forward pass in every network you will meet is this picture, repeated once per layer.

The four readings from the top of this chapter are four ways to describe what happens inside one tile. The machine picks whichever one its memory prefers, and the answer is the same either way.

Undoing a matrix

A matrix takes xx to AxAx. The natural question is whether anything takes AxAx back to xx. If some matrix A1A^{-1} does, for every xx, then

A1A=AA1=I,A^{-1}A = AA^{-1} = I,

and AA is called invertible. For square matrices either equation implies the other, so you only ever have to check one.

When is there no such matrix? Suppose Ax=0Ax = 0 for some xx that is not the zero vector. AA also sends 00 to 00. So two different inputs produce the same output, and no machine on earth can send that one output back to both places. Lecture 2 met this already: the ring road machine sent (1,1,1)(1, 1, 1) to zero because raising all three cities by a meter changed no climb, and that is exactly why it could not be undone. Singular and “kills a nonzero vector” are the same condition wearing different clothes.

xx + vAxx₁x₂Ax₁Ax₂singularinvertibletwo inputs, one outputevery arrow reverses
Fig. 9 

What singular costs you. Blue is the map. On the left two different inputs land on the same pink point, so an arrow back would have to choose between them. On the right nothing was lost, and every blue arrow has a green one returning beside it.

At size 2 you can write the answer down. For

A=[abcd],A1=1adbc[dbca].A = \begin{bmatrix} a & b \\ c & d \end{bmatrix}, \qquad A^{-1} = \frac{1}{ad - bc}\begin{bmatrix} d & -b \\ -c & a \end{bmatrix}.

Try it on A=[[3,1],[4,2]]A = [[3, 1], [4, 2]], where adbc=3214=2ad - bc = 3 \cdot 2 - 1 \cdot 4 = 2:

A1=12[2143]=[10.521.5],[3142][10.521.5]=[1001].A^{-1} = \frac{1}{2}\begin{bmatrix} 2 & -1 \\ -4 & 3 \end{bmatrix} = \begin{bmatrix} 1 & -0.5 \\ -2 & 1.5 \end{bmatrix}, \qquad \begin{bmatrix} 3 & 1 \\ 4 & 2 \end{bmatrix} \begin{bmatrix} 1 & -0.5 \\ -2 & 1.5 \end{bmatrix} = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}.

The number adbcad - bc is the determinant. The formula divides by it, so a zero determinant is a matrix with no inverse. At size 2 that happens exactly when the two columns point along the same line and the machine flattens the plane. Lecture 14 builds the determinant properly, for every size, out of that one idea.

Inverses come in reverse order

Put on socks, then shoes. To undo that you take off the shoes first. Matrices are the same:

(AB)1=B1A1.(AB)^{-1} = B^{-1}A^{-1}.
ABxABxB⁻¹A⁻¹ABxxsocks, then shoesshoes off, then socks
Fig. 10 

Orange is doing, green is undoing. Walking a chain backwards means the last thing done is the first thing undone, so the bottom row is the top row reversed.

The proof is one line of associativity used twice:

(AB)(B1A1)=A(BB1)A1=AIA1=AA1=I.(AB)(B^{-1}A^{-1}) = A(BB^{-1})A^{-1} = AIA^{-1} = AA^{-1} = I.

Check it on the two shears. (ST)1(ST)^{-1} and T1S1T^{-1}S^{-1} both come out as [[1,1],[1,2]][[1, -1], [-1, 2]], while S1T1S^{-1}T^{-1} comes out as [[2,1],[1,1]][[2, -1], [-1, 1]], a different matrix. The order in the answer really is the reverse of the order in the question.

Computing an inverse, once

Here is the honest way to find A1A^{-1} by hand. Elimination does all the work; what changes is where you point it. AA1=IAA^{-1} = I says that AA times column jj of A1A^{-1} equals column jj of II. So the columns of the inverse are the solutions of nn systems that all share the same matrix. Solve them together: glue the whole identity on as extra columns and run elimination on the block [A    I][\,A \;|\; I\,]. When the left half becomes II, the right half has become A1A^{-1}. Elimination downward makes the staircase, elimination upward clears above the pivots, and one division per row finishes it. That last upward sweep is the only new move, and it is why the method carries Jordan’s name alongside Gauss’s.

Run it on the matrix this course will keep coming back to, the second-difference matrix:

K=[210121012].K = \begin{bmatrix} 2 & -1 & 0 \\ -1 & 2 & -1 \\ 0 & -1 & 2 \end{bmatrix}.

Downward, the pivots are 22, then 32\tfrac{3}{2}, then 43\tfrac{4}{3}. Upward and scaled, the right half settles at

K1=14[321242123].K^{-1} = \frac{1}{4}\begin{bmatrix} 3 & 2 & 1 \\ 2 & 4 & 2 \\ 1 & 2 & 3 \end{bmatrix}.

That answer has more in it than the nine numbers.

The pivots multiply to 23243=42 \cdot \tfrac{3}{2} \cdot \tfrac{4}{3} = 4, and 4 is the determinant of KK. Every entry of the inverse is then divided by that 4. A matrix with determinant zero has nothing to divide by, which is the same sentence as “no inverse”, arrived at from a third direction.

KK is symmetric and so is K1K^{-1}. That always happens, and it will matter a great deal from Lecture 15 onward.

And KK is as sparse as a matrix can be while still being connected: seven nonzeros out of nine, and every nonzero sits on or beside the diagonal. Its inverse has nine out of nine. Grow the matrix and the contrast grows with it.

K, size 8its inverse22 nonzeros of 6464 nonzeros of 64invert
Fig. 11 

Sparse in, dense out. Every entry of the green inverse depends on every entry of the orange original, so the band spreads to fill the whole square. Depth of colour is size.

Why nobody calls inv

Start with the picture above. A sparse matrix of size a million has a few million nonzeros and fits in memory. Its inverse has a trillion entries and does not exist on any machine you own.

Then there is speed. The inverse costs a full elimination plus nn back substitutions, and then you still have to multiply by bb. Solving directly costs one elimination and one back substitution. On a 3000×30003000 \times 3000 system, measured on the machine that wrote this chapter, inverting and multiplying took 0.20 seconds while solving took 0.067.

The reason that bites silently is accuracy. The code below solves a 12×1212 \times 12 system whose true answer is all ones, using a matrix chosen to be nasty. Going through the inverse gives an answer wrong by 12.6 in its worst coordinate. Solving directly is wrong by 0.22. Both are bad, because the matrix is genuinely awful, and one of them is nearly sixty times worse for no benefit.

A1A^{-1} is how you think. solve is how you compute. The code below shows the four readings agreeing, then the inverse story end to end.

python
import numpy as np

A = np.array([[1., 2., 3.], [4., 5., 6.]])
B = np.array([[1., 0.], [2., 1.], [0., 3.]])

print(A @ B)                                                  # [[ 5. 11.] [14. 23.]]
print(np.array([[A[i] @ B[:, j] for j in range(2)]            # rows dot columns
                for i in range(2)]))                          # [[ 5. 11.] [14. 23.]]
print(sum(np.outer(A[:, k], B[k]) for k in range(3)))         # rank-one layers
                                                              # [[ 5. 11.] [14. 23.]]
print(np.column_stack([A @ B[:, j] for j in range(2)]))       # columns of A, mixed
                                                              # [[ 5. 11.] [14. 23.]]
print(np.vstack([A[i] @ B for i in range(2)]))                # rows of B, mixed
                                                              # [[ 5. 11.] [14. 23.]]

S = np.array([[1., 1.], [0., 1.]])
T = np.array([[1., 0.], [1., 1.]])
print(S @ T)                    # [[2. 1.] [1. 1.]]
print(T @ S)                    # [[1. 1.] [1. 2.]]   order is not negotiable

The second block is Gauss-Jordan written out, then the measurements behind the paragraphs above. Only NumPy is timed, because a stopwatch reading is a fact about one machine.

python
import numpy as np

K = np.array([[2., -1., 0.], [-1., 2., -1.], [0., -1., 2.]])
M = np.hstack([K, np.eye(3)])          # the block [K | I], three rows of six

for k in range(3):                     # down: clear below every pivot
    for i in range(k + 1, 3):
        M[i] -= (M[i, k] / M[k, k]) * M[k]
pivots = np.diag(M[:, :3]).copy()
print(pivots)                          # [2.  1.5  1.33333333]
print(np.prod(pivots), np.linalg.det(K))   # 4.0 4.0   pivots multiply to det

for k in (2, 1):                       # up: clear above every pivot
    for i in range(k):
        M[i] -= (M[i, k] / M[k, k]) * M[k]
M /= np.diag(M[:, :3])[:, None]        # one division per row

print(M[:, :3])                        # the identity, so the left half is done
print(4 * M[:, 3:])                    # [[3. 2. 1.] [2. 4. 2.] [1. 2. 3.]]
print(np.allclose(M[:, 3:], np.linalg.inv(K)))   # True

K8 = (np.diag(2 * np.ones(8)) - np.diag(np.ones(7), 1) - np.diag(np.ones(7), -1))
print(np.count_nonzero(K8))                             # 22   sparse going in
print(np.sum(np.abs(np.linalg.inv(K8)) > 1e-9))          # 64   dense coming out
python
import numpy as np
from time import perf_counter

def best(f, reps=3):
    out = float("inf")
    for _ in range(reps):
        t0 = perf_counter(); f(); out = min(out, perf_counter() - t0)
    return out

n, rng = 3000, np.random.default_rng(0)
A = rng.standard_normal((n, n)) + np.sqrt(n) * np.eye(n)
b = rng.standard_normal(n)

print(best(lambda: np.linalg.inv(A) @ b))   # 0.20  seconds, on the machine that wrote this
print(best(lambda: np.linalg.solve(A, b)))  # 0.067 seconds

n = 12                                      # a matrix chosen to be numerically awful
H = 1.0 / (np.arange(1, n + 1)[:, None] + np.arange(1, n + 1) - 1)
x_true = np.ones(n)
c = H @ x_true
print(np.abs(np.linalg.inv(H) @ c - x_true).max())    # 12.59375
print(np.abs(np.linalg.solve(H, c) - x_true).max())   # 0.22008972067760946

Where this is going

Elimination gave you a way to solve one system. This chapter gave you the algebra to talk about what elimination did: a product of simple matrices sitting to the left of AA, regrouped into one matrix, and inverted by walking the chain backwards. Put those together and a question appears. If the expensive part of solving Ax=bAx = b is making the staircase, and the staircase is a product, why not compute the product once, write it down, and reuse it for every bb that ever arrives?

That is a factorization, and the next chapter delivers two of them. A=LUA = LU is elimination’s receipt: UU is the staircase from Lecture 4 and LL holds the multipliers, unmixed, for the reason this chapter half revealed. A=CRA = CR goes the other way, building AA out of its independent columns, and it answers a question you have not been able to ask yet: how much genuinely different information is in a matrix at all?