Lecture 14 · Week 4· 13:48

Matrix multiplication

Product as composition, rules, non-commutativity.

Play: Lecture 14: Matrix multiplication
Lecture · 13:48 · Open on YouTube · Report a problem with this video
TranscriptThe full text of the lecture. Click any sentence to play the video from there.

Today: one machine after another

Welcome back. Last time, we saw that a matrix is more than a table of numbers. An m by n matrix A is a machine: it takes a vector x in F n, and returns the vector A x in F m. And this machine is linear. Today we ask what happens when we run two machines, one after the other. First a matrix B acts on x, and gives B x. Then A acts on the result, and gives A, times B x. For example, we could first rotate the plane by a quarter turn, and then shear it. Doing one after the other is again a way of moving the plane. Is it also given by a matrix? And if so, which one?

The answer is yes, and that matrix is called the product A B. Today we define it, learn two ways to compute it, prove the rules it obeys, and meet its big surprise: the order matters.

The product as composition

Let's set up the sizes. Let B be an n by p matrix, so it takes vectors in F p to vectors in F n. And let A be an m by n matrix, which takes vectors in F n to vectors in F m. The output of B has n entries, exactly what A needs as its input. So we can follow x through both machines: from F p, through B, into F n, then through A, into F m. The combined machine sends x to A, times B x. To find its matrix, we write B x by the column rule. If B has columns b one up to b p, then B x is x one b one, plus and so on, up to x p b p.

The next step is the heart of this lecture. It is short, but take it slowly, and feel free to pause or rewatch it. Now apply A. By linearity, A respects every linear combination. So A, times B x, equals x one times A b one, plus and so on, up to x p times A b p. Look at what this says. It is a linear combination of the vectors A b one up to A b p, with the entries of x as weights. By the column rule again, it is the matrix with columns A b one up to A b p, times x.

So the combined machine is multiplication by a single matrix: the matrix whose column j is A times column j of B. This is how we define the product. Definition. Let A be an m by n matrix, and let B be an n by p matrix with columns b one up to b p. The product A B is the m by p matrix with columns A b one, A b two, up to A b p. And what we just proved is the key theorem. For every x in F p, A B, times x, equals A, times B x. Multiplying by A B is the same as multiplying by B first, and then by A.

Notice the order. In A B, the matrix on the right acts first, just as f of g of x means: apply g first, then f. In the language of lecture thirteen: if the matrix B is followed by the matrix A, the combined linear transformation has matrix A B.

Sizes

The sizes follow from the definition. A B is defined only when A has as many columns as B has rows, since every column of B must be a valid input for A. The product has one column for each column of B, and each column A b j has one entry for each row of A. So m by n, times n by p, gives m by p. As before, the inner sizes must match, and the outer sizes give the size of the result. When p is one, B is a single column, and the product is just the matrix vector product from lecture twelve. So the new product extends the old one.

Rotate, then shear

Let's compute one product, and watch it. Let B be the quarter turn, with columns (zero, one) and (minus one, zero). And let A be the shear, with columns (one, zero) and (one, one). The first column of A B is A times the first column of B, that is, A times the vector (zero, one). That is zero times the first column of A, plus one times the second, which is the vector (one, one). The second column of A B is A times the vector (minus one, zero). That is minus one times the first column of A, plus zero times the second, which is the vector (minus one, zero).

So A B is the matrix with columns (one, one) and (minus one, zero). Now let's watch it on the plane. Here is the unit square, with the two standard basis vectors as its sides. First, B turns it a quarter turn. Then, A shears it sideways. The square lands on the parallelogram whose sides are the vectors (one, one) and (minus one, zero): exactly the columns of A B. And if we start again and apply the single matrix A B, the square moves there in one step.

The row-column rule

Column by column is how the product is defined. But by hand, it is usually quicker to find one entry at a time. Entry i j of A B sits in column j, which is A b j. By the row rule from lecture twelve, entry i of A b j is row i of A, times b j, entry by entry and added up. This is the row column rule. Entry i j of A B is row i of A, times column j of B: a i one b one j, plus a i two b two j, and so on, up to a i n b n j. Each row of A meets each column of B.

Let's use it on a product of different sizes. A is two by three, with rows (one, two, zero), and (minus one, one, three). B is three by two, with first row (two, one), second row (zero, minus one), and third row (one, two). Two by three, times three by two, gives two by two. Entry one one: row one of A, times column one of B. One times two, plus two times zero, plus zero times one. That is two. Entry one two: row one, times column two. One times one, plus two times minus one, plus zero times two. That is one minus two, which is minus one.

Entry two one: row two, times column one. Minus one times two, plus one times zero, plus three times one. That is minus two plus three, which is one. Entry two two: row two, times column two. Minus one times one, plus one times minus one, plus three times two. That is minus one minus one plus six, which is four. So A B is the two by two matrix with rows (two, minus one) and (one, four).

And what about B A? B is three by two and A is two by three, so B A is defined too, but it is three by three. It cannot equal A B: the two products do not even have the same size.

The rules of matrix algebra

To state the rules, we need two simpler operations. Matrices of the same size are added entry by entry, and a matrix is scaled by multiplying every entry by the scalar. The zero matrix, written O, has every entry zero. These work just like the operations on vectors, since an m by n matrix is a list of m n numbers arranged in a rectangle. And by the column rule, A plus B, times x, equals A x plus B x. Now the rules of the product, whenever the sizes allow. First, it is associative: A B, times C, equals A, times B C. Second, it distributes over addition: A, times B plus C, equals A B plus A C, and A plus B, times C, equals A C plus B C.

Third, scalars move freely: c times A B equals c A, times B, which equals A, times c B. And fourth, the identity changes nothing: I m times A equals A, and A times I n equals A. All of these are proved the same way: we compare the two sides one column at a time. Let's start with associativity, the most important rule. Let C have columns c one up to c q. Column j of A B, times C, is A B times c j. By the key theorem, that is A, times B c j.

On the other side, column j of B C is B c j. So column j of A, times B C, is A, times B c j. It is the same vector. Every column agrees, so the two matrices are equal. In words: doing C, then B, then A, gives the same result however we group the steps. So we may write A B C without brackets. And for a square matrix we can define powers: A squared is A A, A cubed is A A A, and so on. The first distributive law is just as quick. Column j of A, times B plus C, is A, times b j plus c j. By linearity, that is A b j plus A c j, which is column j of A B plus A C.

And the identity: A times I n has columns A times the standard basis vectors, which are exactly the columns of A. The other rules are proved in the same way, and the book writes out every one.

The order matters

Now the surprise. For numbers, two times three is three times two. For matrices, A B and B A are usually different, even when both are square and of the same size. Go back to our quarter turn B and our shear A. We found that A B, rotate first and then shear, has columns (one, one) and (minus one, zero). Now the other order, B A: shear first, then rotate. Its first column is B times the vector (one, zero), which is the first column of B, the vector (zero, one). Its second column is B times the vector (one, one), which is one times the first column of B, plus one times the second: the vector (minus one, one).

So B A has columns (zero, one) and (minus one, one). That is not A B. On the plane, the difference is easy to see. Rotating and then shearing sends the square to one parallelogram. Shearing and then rotating sends it to another. So matrix multiplication is not commutative, and we must always keep track of the order. Some pairs do commute, for example any square matrix and the identity, or two rotations of the plane. But that is the exception, not the rule.

Do not confuse this with associativity. Associativity says that the grouping does not matter: A B, times C, equals A, times B C. The three matrices keep their order, A, then B, then C. Only the brackets move. Commutativity would let two matrices swap places, and that is what fails: A B is usually not B A. So in a product, we may regroup freely, but we may never reorder. A second surprise. For numbers, if a product is zero, then one of the factors is zero. Not for matrices. Take the matrix N with first row (zero, one), and second row (zero, zero). N is not the zero matrix, but N squared is. The first column of N squared is N times the first column of N, which is the zero vector. And N times the zero vector is the zero vector. The second column is N times the vector (one, zero). That is one times the first column of N, plus zero times the second: again the zero vector. So N squared is the zero matrix.

Geometrically, N sends the second basis vector to the first, and the first basis vector to zero. Apply it twice, and everything ends at zero. One consequence: we cannot cancel. N times N equals N times the zero matrix, since both are zero, but N is not the zero matrix. So from A B equals A C, we may not conclude that B equals C. When we can cancel is the subject of lecture sixteen, on inverse matrices.

Summary

Let's summarise. When the output of B fits the input of A, the product A B is the matrix with columns A b one up to A b p. Multiplying by A B means: first B, then A. An m by n matrix times an n by p matrix gives an m by p matrix, and entry i j is row i of A, times column j of B. The product is associative, it distributes over addition, scalars move freely, and the identity changes nothing.

But the order matters: A B and B A are usually different, and a product can be zero even when neither factor is zero. Before the next lecture, read section three point three of the course book, try the problems on sheet four about matrix multiplication, and do the lecture check on this lecture's page at Open Semester dot com. Next time, we continue with matrix algebra: the transpose, special kinds of matrices, and the trace. See you there.

Check yourself

3 questions, from warm-up to stretch

Work each one out on paper, type your answer and press Check. Stuck? Open a hint; the worked solution is there when you need it.

  1. Question 1 of 3 Warm-up

    Let and . Compute one column at a time: column of is times column of .

    Hint 1 of 2

    . The columns of are and .

    Hint 2 of 2

    : the entries of are the weights on the columns of . Do the same with .

    Worked solution
    1. The columns of are and .
    2. .
    3. .
    4. So .
  2. Question 2 of 3 Core

    Let and . Use the row-column rule to find two entries of . Then: how many rows does have?

    Hint 1 of 2

    is row of times column of , entry by entry, added up. For sizes: .

    Hint 2 of 2

    uses row of , , and column of , : . For use row of , .

    Worked solution
    1. .
    2. .
    3. is , so it has rows. is : the two products do not even have the same size.
    4. For reference, .
  3. Question 3 of 3 Stretch

    Let and . For which number is the zero matrix? With that , is also the zero matrix? Compute .

    Is too?
    Hint 1 of 2

    Column of is , which is already . Column is : choose so that it is as well.

    Hint 2 of 2

    . Then is row of times column of .

    Worked solution
    1. Column of is , for every .
    2. Column of is . It is exactly when .
    3. With : , so . In full, .
    4. So although neither nor is zero, and : a product can be zero, and the order matters.

Found a mistake, or something not working on this page? Report it and we'll fix it. See what we've corrected.