Unit 6, lesson 81

Matrix Multiplication

When two matrices can be multiplied, how each entry of the product is a row-column dot product, why a product means doing one linear map after another, and why the order of the factors matters.

  • Theory part 161
  • Practice part 162
  • Matrices

Two bakery shops sell muffins and loaves. One table records how many of each item each shop sold today, and another gives each item’s price and its cost to make. To get a shop’s revenue you multiply matching numbers and add, a row of one table against a column of the other. That rule is matrix multiplication, and the same rule describes what happens when you move the points of the plane with one matrix and then with another.

Key terms

Compatible dimensions

Definition Let AA be an m×nm \times n matrix and BB a p×qp \times q matrix. The product ABAB is defined exactly when n=pn = p, that is, when AA has as many columns as BB has rows. In that case ABAB is an m×qm \times q matrix,

A⏟m×n B⏟n×q=AB⏟m×q.\underbrace{A}_{m \times n} \, \underbrace{B}_{n \times q} = \underbrace{AB}_{m \times q}.

In plain words Write the two sizes side by side. The two inside numbers must match, and the two outside numbers give the size of the answer.

Example Let AA be 2×32 \times 3 and BB be 3×43 \times 4. The inside numbers are 33 and 33, so ABAB exists and is 2×42 \times 4. In the other order the sizes read 3×43 \times 4 and 2×32 \times 3, the inside numbers 44 and 22 differ, and BABA is not defined.

Order changes sizes too. A 1×31 \times 3 row times a 3×13 \times 1 column is a 1×11 \times 1 matrix, a single number, while the column times the row is a 3×33 \times 3 matrix.

Row-column dot product

Definition Let A=[aij]A = [a_{ij}] be m×nm \times n and B=[bij]B = [b_{ij}] be n×qn \times q. The product ABAB is the m×qm \times q matrix whose entry in row ii and column jj is

(AB)ij=ai1b1j+ai2b2j+⋯+ainbnj=∑k=1naikbkj.\begin{aligned} (AB)_{ij} &= a_{i1}b_{1j} + a_{i2}b_{2j} \\[0.4em] &\quad + \cdots + a_{in}b_{nj} \\[0.4em] &= \sum_{k=1}^{n} a_{ik}b_{kj}. \end{aligned}

This is the dot product of row ii of AA with column jj of BB, both read as vectors with nn components.

In plain words To fill the spot in row ii and column jj of the answer, run across row ii of the first matrix and down column jj of the second, multiply the pairs, and add.

Example In the matrix SS the rows are the two shops and the columns are muffins and loaves sold. In PP the rows are muffins and loaves and the columns are price and cost in dollars.

S=[30121820],P=[3152].S = \begin{bmatrix} 30 & 12 \\ 18 & 20 \end{bmatrix}, \qquad P = \begin{bmatrix} 3 & 1 \\ 5 & 2 \end{bmatrix}.

Row 1 of SS against column 1 of PP gives shop 1’s revenue, 30⋅3+12⋅5=15030 \cdot 3 + 12 \cdot 5 = 150 dollars. All four spots together give

SP=[30⋅3+12⋅530⋅1+12⋅218⋅3+20⋅518⋅1+20⋅2]=[1505415458].\begin{gathered} SP = \\[0.4em] \begin{bmatrix} 30 \cdot 3 + 12 \cdot 5 & 30 \cdot 1 + 12 \cdot 2 \\ 18 \cdot 3 + 20 \cdot 5 & 18 \cdot 1 + 20 \cdot 2 \end{bmatrix} \\[0.8em] = \begin{bmatrix} 150 & 54 \\ 154 & 58 \end{bmatrix}. \end{gathered}

Shop 1 took in 150 dollars for goods that cost 54 dollars to make, and shop 2 took in 154 dollars for goods that cost 58.

A matrix as a map

Definition Write a vector with nn real components as an n×1n \times 1 column, so ⟨a,b⟩\langle a, b \rangle becomes the column with entries aa and bb. The set of all such vectors is Rn\mathbb{R}^n, and a point stands for its position vector. An m×nm \times n matrix AA defines the function x↦Ax\mathbf{x} \mapsto A\mathbf{x} from Rn\mathbb{R}^n to Rm\mathbb{R}^m, where AxA\mathbf{x} is the matrix product. This function is linear, meaning

A(u+v)=Au+AvandA(cu)=c Au\begin{aligned} A(\mathbf{u} + \mathbf{v}) &= A\mathbf{u} + A\mathbf{v} \quad \text{and} \\[0.4em] A(c\mathbf{u}) &= c\,A\mathbf{u} \end{aligned}

for all u\mathbf{u} and v\mathbf{v} in Rn\mathbb{R}^n and every real number cc. Conversely, every linear map from Rn\mathbb{R}^n to Rm\mathbb{R}^m has this form for exactly one m×nm \times n matrix, the one whose column kk is the image of ek\mathbf{e}_k, the vector with 11 in place kk and 00 everywhere else. For maps the word linear has this stricter meaning. With one variable, x↦mxx \mapsto mx is a linear map, but a linear function mx+bmx + b with b≠0b \ne 0 is not, since it sends 00 to bb.

In plain words A matrix is a rule that moves vectors. In the plane e1\mathbf{e}_1 and e2\mathbf{e}_2 are the standard unit vectors i\mathbf{i} and j\mathbf{j}, so the first column says where i\mathbf{i} lands and the second column says where j\mathbf{j} lands, and every other vector follows along.

Example The matrix below stretches the plane sideways by a factor of 2,

A=[2001],A[3−1]=[2⋅3+0⋅(−1)0⋅3+1⋅(−1)]=[6−1].\begin{aligned} A &= \begin{bmatrix} 2 & 0 \\ 0 & 1 \end{bmatrix}, \\[0.8em] A \begin{bmatrix} 3 \\ -1 \end{bmatrix} &= \begin{bmatrix} 2 \cdot 3 + 0 \cdot (-1) \\ 0 \cdot 3 + 1 \cdot (-1) \end{bmatrix} \\[0.8em] &= \begin{bmatrix} 6 \\ -1 \end{bmatrix}. \end{aligned}

Its columns show that i\mathbf{i} goes to ⟨2,0⟩\langle 2, 0 \rangle and j\mathbf{j} stays put, so each unit square of the grid becomes a rectangle 2 wide and 1 tall.

Composition of linear maps

Definition Let AA be m×nm \times n and BB be n×pn \times p. For every x\mathbf{x} in Rp\mathbb{R}^p

A(Bx)=(AB)x.A(B\mathbf{x}) = (AB)\mathbf{x}.

So applying BB first and then AA is the same linear map as applying the single matrix ABAB. The matrix that acts first stands on the right. The same reasoning shows that multiplication is associative, (AB)C=A(BC)(AB)C = A(BC) whenever the sizes fit.

In plain words Multiplying two matrices combines two moves into one. Read a product from right to left, because the right factor acts first.

Example Take the quarter turn counterclockwise BB and the sideways stretch AA from above,

B=[0−110],A=[2001],AB=[0−210].\begin{aligned} B &= \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix}, \\[0.8em] A &= \begin{bmatrix} 2 & 0 \\ 0 & 1 \end{bmatrix}, \\[0.8em] AB &= \begin{bmatrix} 0 & -2 \\ 1 & 0 \end{bmatrix}. \end{aligned}

Follow the point (1,1)(1, 1) in two stages. The turn sends it to (−1,1)(-1, 1), and the stretch sends that to (−2,1)(-2, 1). One step with the product gives the same point,

AB[11]=[0⋅1+(−2)⋅11⋅1+0⋅1]=[−21].\begin{aligned} AB \begin{bmatrix} 1 \\ 1 \end{bmatrix} &= \begin{bmatrix} 0 \cdot 1 + (-2) \cdot 1 \\ 1 \cdot 1 + 0 \cdot 1 \end{bmatrix} \\[0.8em] &= \begin{bmatrix} -2 \\ 1 \end{bmatrix}. \end{aligned}

Noncommutativity

Definition Matrix multiplication is not commutative. The product ABAB can be defined while BABA is not. When both are defined they can have different sizes, and for every n≥2n \ge 2 there are n×nn \times n matrices with AB≠BAAB \ne BA. Some pairs do commute, but that has to be checked, never assumed. Rules for numbers that rely on ab=baab = ba can fail. For square matrices AA and BB of the same size

(A+B)2=A2+AB+BA+B2,(A + B)^2 = A^2 + AB + BA + B^2,

which equals A2+2AB+B2A^2 + 2AB + B^2 only when AB=BAAB = BA.

In plain words The order of the factors is part of the instruction. Turning and then stretching is usually not the same as stretching and then turning.

Example With the same AA and BB, stretch first and turn second,

BA=[0⋅2+(−1)⋅00⋅0+(−1)⋅11⋅2+0⋅01⋅0+0⋅1]=[0−120].\begin{gathered} BA = \\[0.4em] \begin{bmatrix} 0 \cdot 2 + (-1) \cdot 0 & 0 \cdot 0 + (-1) \cdot 1 \\ 1 \cdot 2 + 0 \cdot 0 & 1 \cdot 0 + 0 \cdot 1 \end{bmatrix} \\[0.8em] = \begin{bmatrix} 0 & -1 \\ 2 & 0 \end{bmatrix}. \end{gathered}

This is not ABAB. The point (1,1)(1, 1) is stretched to (2,1)(2, 1) and then turned to (−1,2)(-1, 2), while the other order gave (−2,1)(-2, 1).

Identity matrix

Definition The n×nn \times n identity matrix InI_n has every entry on the main diagonal, the entries in row ii and column ii, equal to 11 and every other entry equal to 00. For every m×nm \times n matrix AA

ImA=AandAIn=A.I_m A = A \quad \text{and} \quad A I_n = A.

As a map, Inx=xI_n \mathbf{x} = \mathbf{x} for every x\mathbf{x} in Rn\mathbb{R}^n. In particular InI_n commutes with every n×nn \times n matrix.

In plain words The identity matrix does for matrices what the number 1 does for numbers. It leaves every vector where it is.

Example Here are the two smallest identity matrices,

I2=[1001],I3=[100010001].\begin{aligned} I_2 &= \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}, \\[0.8em] I_3 &= \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix}. \end{aligned}

For a 2×32 \times 3 matrix AA, row 1 of I2I_2 has entries 11 and 00, and its dot product with any column of AA simply picks out that column’s top entry. Row 2 picks out the bottom entry. So I2A=AI_2 A = A, and in the same way AI3=AA I_3 = A. The product I2AI_2 A needs I2I_2 and AI3A I_3 needs I3I_3, because the sizes have to fit.

Zero matrix

Definition The m×nm \times n zero matrix OO has every entry equal to 00. For every m×nm \times n matrix AA we have A+O=AA + O = A, and whenever the sizes fit, a product with a zero factor is a zero matrix, AO=OAO = O and OB=OOB = O. Unlike numbers, two nonzero matrices can have product OO. So AB=OAB = O does not force A=OA = O or B=OB = O, and AB=ACAB = AC does not force B=CB = C.

In plain words The zero matrix sends every vector to the zero vector. Be careful, because a product can be zero even when neither factor is.

Example Neither factor below is zero, yet

[1111][1−1−11]=[1−1−1+11−1−1+1]=[0000].\begin{aligned} &\begin{bmatrix} 1 & 1 \\ 1 & 1 \end{bmatrix} \begin{bmatrix} 1 & -1 \\ -1 & 1 \end{bmatrix} \\[0.8em] &\quad = \begin{bmatrix} 1 - 1 & -1 + 1 \\ 1 - 1 & -1 + 1 \end{bmatrix} \\[0.8em] &\quad = \begin{bmatrix} 0 & 0 \\ 0 & 0 \end{bmatrix}. \end{aligned}

The picture explains it. The right factor sends ⟨x,y⟩\langle x, y \rangle to (x−y)⟨1,−1⟩(x - y)\langle 1, -1 \rangle, a multiple of ⟨1,−1⟩\langle 1, -1 \rangle, and the left factor sends ⟨1,−1⟩\langle 1, -1 \rangle to 0\mathbf{0}.

Check yourself

  1. The matrix AA is 3×23 \times 2 and BB is 2×42 \times 4. Which of ABAB and BABA is defined, and what is its size?
Show the answer

The product ABAB is defined because the inside numbers 22 and 22 match, and it is 3×43 \times 4. The product BABA would pair 2×42 \times 4 with 3×23 \times 2, and 4≠34 \ne 3, so it is not defined.

  1. Compute ABAB and BABA for the matrices below.
A=[1201],B=[1031]A = \begin{bmatrix} 1 & 2 \\ 0 & 1 \end{bmatrix}, \qquad B = \begin{bmatrix} 1 & 0 \\ 3 & 1 \end{bmatrix}
Show the answer

Row by column,

AB=[7231],BA=[1237].\begin{aligned} AB &= \begin{bmatrix} 7 & 2 \\ 3 & 1 \end{bmatrix}, \\[0.8em] BA &= \begin{bmatrix} 1 & 2 \\ 3 & 7 \end{bmatrix}. \end{aligned}

For instance the top left entry of ABAB is 1⋅1+2⋅3=71 \cdot 1 + 2 \cdot 3 = 7, while the top left entry of BABA is 1⋅1+0⋅0=11 \cdot 1 + 0 \cdot 0 = 1. The two products differ.

  1. The matrix RR below turns the plane a quarter turn counterclockwise. Compute R2=RRR^2 = RR and describe the map it gives.
R=[0−110]R = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix}
Show the answer

The product is

R2=[0⋅0+(−1)⋅10⋅(−1)+(−1)⋅01⋅0+0⋅11⋅(−1)+0⋅0]=[−100−1],\begin{gathered} R^2 = \\[0.4em] \begin{bmatrix} 0 \cdot 0 + (-1) \cdot 1 & 0 \cdot (-1) + (-1) \cdot 0 \\ 1 \cdot 0 + 0 \cdot 1 & 1 \cdot (-1) + 0 \cdot 0 \end{bmatrix} \\[0.8em] = \begin{bmatrix} -1 & 0 \\ 0 & -1 \end{bmatrix}, \end{gathered}

which sends (x,y)(x, y) to (−x,−y)(-x, -y). Two quarter turns make a half turn about the origin.

How this lesson connects