Unit 1, lesson 5

Matrix Multiplication and Composition

How composing two linear maps leads to the matrix product, when sizes are compatible, why order matters, and the rules for the identity matrix, associativity, the transpose of a product and blocks.

  • Video 5
  • Matrices and Linear Transformations

Lean a drawing to the right with a shear, then stretch it to twice its width. If the stretch comes first instead, does the drawing end up the same? Applying one linear transformation after another gives a single linear transformation, and its matrix is the product of the two matrices. This lesson builds that product column by column, says when it is defined and why the order of the factors matters, and collects the rules for regrouping, transposing and splitting a product into blocks.

Key terms

Composition of linear maps

Definition Vectors are columns, and on this page a tuple such as (x,y)(x, y) written inline stands for the column with those entries. Let TT be a linear transformation from Rp\mathbb{R}^p to Rn\mathbb{R}^n and UU a linear transformation from Rn\mathbb{R}^n to Rm\mathbb{R}^m. The composition U∘TU \circ T, read as UU after TT, is the map from Rp\mathbb{R}^p to Rm\mathbb{R}^m given by

(U∘T)(x)=U(T(x)),(U \circ T)(\mathbf{x}) = U(T(\mathbf{x})),

so TT acts first and UU acts on its output. The composition makes sense because every output of TT lies in Rn\mathbb{R}^n, the domain of UU. It is linear, since for all u,v\mathbf{u}, \mathbf{v} in Rp\mathbb{R}^p and all real a,ba, b

U(T(au+bv))=U(aT(u)+bT(v))=aU(T(u))+bU(T(v)).\begin{aligned} &U(T(a\mathbf{u} + b\mathbf{v})) \\[0.4em] &\quad = U(aT(\mathbf{u}) + bT(\mathbf{v})) \\[0.4em] &\quad = aU(T(\mathbf{u})) + bU(T(\mathbf{v})). \end{aligned}

If BB is the standard matrix of TT and AA is the standard matrix of UU, then column jj of the standard matrix of U∘TU \circ T is U(T(ej))=AbjU(T(\mathbf{e}_j)) = A\mathbf{b}_j, where bj=T(ej)\mathbf{b}_j = T(\mathbf{e}_j) is column jj of BB. The matrix with these columns is the product ABAB, defined in the next card.

In plain words The output of the first map becomes the input of the second. In the matrix ABAB the map that acts first stands on the right, next to the vector it acts on.

Example Let SS be the matrix of the shear (x,y)↦(x+y,y)(x, y) \mapsto (x + y, y) and DD the matrix of the horizontal stretch (x,y)↦(2x,y)(x, y) \mapsto (2x, y),

S=[1101],D=[2001].S = \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix}, \qquad D = \begin{bmatrix} 2 & 0 \\ 0 & 1 \end{bmatrix}.

To shear first and then stretch, follow the standard unit vectors. The shear leaves e1\mathbf{e}_1 at (1,0)(1, 0) and the stretch moves it to (2,0)(2, 0). The shear moves e2\mathbf{e}_2 to (1,1)(1, 1) and the stretch moves that to (2,1)(2, 1). These images are the columns of

DS=[2201].DS = \begin{bmatrix} 2 & 2 \\ 0 & 1 \end{bmatrix}.

As a check, the shear sends (3,−1)(3, -1) to (2,−1)(2, -1) and the stretch sends that to (4,−1)(4, -1), while the rows of DSDS give 2⋅3+2⋅(−1)=42 \cdot 3 + 2 \cdot (-1) = 4 and 0⋅3+1⋅(−1)=−10 \cdot 3 + 1 \cdot (-1) = -1.

Matrix product and compatible sizes

Definition Let AA be an m×nm \times n matrix and BB an n×pn \times p matrix with columns b1,…,bp\mathbf{b}_1, \dots, \mathbf{b}_p. The product ABAB is the m×pm \times p matrix

AB=[Ab1Ab2⋯Abp].AB = \begin{bmatrix} A\mathbf{b}_1 & A\mathbf{b}_2 & \cdots & A\mathbf{b}_p \end{bmatrix}.

Its entry in row ii and column jj is row ii of AA times column jj of BB,

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

The sizes are compatible when the number of columns of AA equals the number of rows of BB, and only then is ABAB defined. An m×nm \times n matrix times an n×pn \times p matrix is an m×pm \times p matrix. The shared inner size nn counts the coordinates handed from the first map to the second, and the outer sizes are the output size mm and the input size pp. For every x\mathbf{x} in Rp\mathbb{R}^p the product satisfies (AB)x=A(Bx)(AB)\mathbf{x} = A(B\mathbf{x}), so ABAB is the matrix of the composition that applies BB first and then AA. The product is not formed by multiplying entries in matching positions.

In plain words Multiply the left matrix into each column of the right matrix. The inside sizes must agree, and the outside sizes give the size of the answer.

Example Take

A=[123−1],B=[2011−13].\begin{gathered} A = \begin{bmatrix} 1 & 2 \\ 3 & -1 \end{bmatrix}, \\[0.4em] B = \begin{bmatrix} 2 & 0 & 1 \\ 1 & -1 & 3 \end{bmatrix}. \end{gathered}

The sizes 2×22 \times 2 and 2×32 \times 3 are compatible, so ABAB is defined and is 2×32 \times 3. Multiplying AA into the columns (2,1)(2, 1), (0,−1)(0, -1) and (1,3)(1, 3) of BB gives (4,5)(4, 5), (−2,1)(-2, 1) and (7,0)(7, 0), so

AB=[4−27510].AB = \begin{bmatrix} 4 & -2 & 7 \\ 5 & 1 & 0 \end{bmatrix}.

The row rule agrees. For example, row 22 of AA times column 33 of BB is 3⋅1+(−1)⋅3=03 \cdot 1 + (-1) \cdot 3 = 0. The product BABA is not defined, because BB has 33 columns and AA has only 22 rows.

Noncommutativity of matrix multiplication

Definition Matrix multiplication is not commutative, which means ABAB and BABA need not be equal. If ABAB is defined, then BABA may be undefined. If AA is m×nm \times n and BB is n×mn \times m, both products are defined but have sizes m×mm \times m and n×nn \times n, which differ when m≠nm \ne n. When AA and BB are both n×nn \times n with n≥2n \ge 2, the two products have the same size and can still differ. Matrices of size 1×11 \times 1 always commute, because they multiply like numbers. Some pairs do commute, for example two diagonal matrices of the same size, a square matrix and the identity matrix of its size, or a square matrix and itself. Before exchanging two factors, check that AB=BAAB = BA for those particular matrices.

In plain words Applying two maps in the opposite order usually gives a different map, so the factors of a product cannot be swapped freely.

Example With the shear SS and the stretch DD from the first card,

DS=[2201],SD=[2101].DS = \begin{bmatrix} 2 & 2 \\ 0 & 1 \end{bmatrix}, \qquad SD = \begin{bmatrix} 2 & 1 \\ 0 & 1 \end{bmatrix}.

Follow the input (1,1)(1, 1). Shearing first gives (2,1)(2, 1), and stretching then gives (4,1)(4, 1), which is DSDS times (1,1)(1, 1). Stretching first gives (2,1)(2, 1), and shearing then gives (3,1)(3, 1), which is SDSD times (1,1)(1, 1). In the first order the stretch also doubles the sideways shift made by the shear. In the second order the shift is added after the stretch and is not doubled.

Associativity and the identity matrix

Definition If AA is m×nm \times n, BB is n×pn \times p and CC is p×qp \times q, then

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

To prove it, apply both sides to any x\mathbf{x} in Rq\mathbb{R}^q and use the rule (XY)x=X(Yx)(XY)\mathbf{x} = X(Y\mathbf{x}) for products,

((AB)C)x=(AB)(Cx)=A(B(Cx)),(A(BC))x=A((BC)x)=A(B(Cx)).\begin{aligned} ((AB)C)\mathbf{x} &= (AB)(C\mathbf{x}) \\[0.4em] &= A(B(C\mathbf{x})), \\[0.4em] (A(BC))\mathbf{x} &= A((BC)\mathbf{x}) \\[0.4em] &= A(B(C\mathbf{x})). \end{aligned}

Two matrices that agree on every vector agree on each ej\mathbf{e}_j, so they have the same columns and are equal. This is why a product such as ABCABC needs no parentheses. The identity matrix InI_n is the n×nn \times n matrix with columns e1,…,en\mathbf{e}_1, \dots, \mathbf{e}_n, that is, with 11 in every diagonal position and 00 elsewhere. It satisfies Inx=xI_n\mathbf{x} = \mathbf{x} for every x\mathbf{x} in Rn\mathbb{R}^n, and for every m×nm \times n matrix AA

ImA=A=AIn.I_m A = A = A I_n.

In plain words In a chain of maps the order of application is fixed, but you may choose which neighboring pair to combine first. Regrouping is allowed and reordering is not. The identity matrix represents the map that leaves every vector where it is.

Example Let u=(1,2)\mathbf{u} = (1, 2), v=(3,−1)\mathbf{v} = (3, -1) and w=(2,5)\mathbf{w} = (2, 5). Grouping on the left first forms a 2×22 \times 2 matrix,

uv⊤=[3−16−2],\mathbf{u}\mathbf{v}^{\top} = \begin{bmatrix} 3 & -1 \\ 6 & -2 \end{bmatrix},

and then (uv⊤)w=(6−5,12−10)=(1,2)(\mathbf{u}\mathbf{v}^{\top})\mathbf{w} = (6 - 5, 12 - 10) = (1, 2). Grouping on the right first gives the 1×11 \times 1 matrix v⊤w=6−5=1\mathbf{v}^{\top}\mathbf{w} = 6 - 5 = 1, so u(v⊤w)=(1,2)\mathbf{u}(\mathbf{v}^{\top}\mathbf{w}) = (1, 2) with far less arithmetic. For the identity, take the 2×32 \times 3 matrix EE with columns (1,0)(1, 0), (0,1)(0, 1) and (1,1)(1, 1). Then I2E=EI_2E = E and EI3=EEI_3 = E, while the product I3EI_3E is not even defined.

Transpose of a product

Definition If AA is m×nm \times n and BB is n×pn \times p, then

(AB)⊤=B⊤A⊤.(AB)^{\top} = B^{\top}A^{\top}.

Both sides are p×mp \times m. To prove it, compare the entries in row ii and column jj,

((AB)⊤)ij=(AB)ji=∑k=1najkbki=∑k=1n(B⊤)ik(A⊤)kj=(B⊤A⊤)ij,\begin{aligned} ((AB)^{\top})_{ij} &= (AB)_{ji} = \sum_{k=1}^{n} a_{jk}b_{ki} \\[0.4em] &= \sum_{k=1}^{n} (B^{\top})_{ik}(A^{\top})_{kj} \\[0.4em] &= (B^{\top}A^{\top})_{ij}, \end{aligned}

where the middle step uses ajkbki=bkiajka_{jk}b_{ki} = b_{ki}a_{jk} for real numbers. The order of the factors reverses. The product A⊤B⊤A^{\top}B^{\top} in the original order is in general a different matrix, and it may not be defined at all.

In plain words To transpose a product, transpose each factor and reverse their order.

Example With AA and BB from the matrix product card, (AB)⊤(AB)^{\top} is the 3×23 \times 2 matrix whose columns (4,−2,7)(4, -2, 7) and (5,1,0)(5, 1, 0) are the rows of ABAB. The reversed product of transposes gives the same matrix,

B⊤A⊤=[210−113][132−1]=[45−2170].\begin{aligned} B^{\top}A^{\top} &= \begin{bmatrix} 2 & 1 \\ 0 & -1 \\ 1 & 3 \end{bmatrix} \begin{bmatrix} 1 & 3 \\ 2 & -1 \end{bmatrix} \\[0.4em] &= \begin{bmatrix} 4 & 5 \\ -2 & 1 \\ 7 & 0 \end{bmatrix}. \end{aligned}

The other order A⊤B⊤A^{\top}B^{\top} would be a 2×22 \times 2 matrix times a 3×23 \times 2 matrix, which is not defined.

Block multiplication

Definition Cut the rows and the columns of a matrix AA into consecutive groups, which splits AA into blocks AikA_{ik}, and cut the rows and the columns of BB into groups as well, giving blocks BkjB_{kj}. The partitions conform when the column groups of AA have the same sizes, in the same order, as the row groups of BB. Then ABAB can be computed block by block. Its block in block row ii and block column jj is

∑kAikBkj,\sum_{k} A_{ik}B_{kj},

the entry formula with blocks in place of numbers. With two block columns in AA and two block rows in BB, for instance, the top left block of ABAB is A11B11+A12B21A_{11}B_{11} + A_{12}B_{21}. Conforming partitions make every product AikBkjA_{ik}B_{kj} defined and give the terms of each sum the same size. Each product must keep its order, because blocks are matrices and need not commute. The row groups of AA and the column groups of BB may be chosen freely, and they become the row and column groups of ABAB.

In plain words Treat the blocks like entries and keep each left factor on the left. This works only when the inner size is cut the same way in both matrices.

Example Cut the 2×32 \times 3 matrix MM after its second column, so that M=[I2c]M = \begin{bmatrix} I_2 & \mathbf{c} \end{bmatrix} with c=(2,−1)\mathbf{c} = (2, -1), and cut x=(3,4,2)\mathbf{x} = (3, 4, 2) after its second entry to match,

Mx=[10201−1][342].M\mathbf{x} = \left[\begin{array}{cc|c} 1 & 0 & 2 \\ 0 & 1 & -1 \end{array}\right] \left[\begin{array}{c} 3 \\ 4 \\ \hline 2 \end{array}\right].

The block rule gives

Mx=I2[34]+[2−1]⋅2=[34]+[4−2]=[72],\begin{aligned} M\mathbf{x} &= I_2\begin{bmatrix} 3 \\ 4 \end{bmatrix} + \begin{bmatrix} 2 \\ -1 \end{bmatrix} \cdot 2 \\[0.4em] &= \begin{bmatrix} 3 \\ 4 \end{bmatrix} + \begin{bmatrix} 4 \\ -2 \end{bmatrix} = \begin{bmatrix} 7 \\ 2 \end{bmatrix}, \end{aligned}

and the rows of MM confirm it, since 3+0+4=73 + 0 + 4 = 7 and 0+4−2=20 + 4 - 2 = 2. Cutting x\mathbf{x} after its first entry instead would pair a block of two columns with a block of one entry, and the block products would not be defined.

Check yourself

  1. Let AA be a 3×23 \times 2 matrix and BB a 2×42 \times 4 matrix. Which of the products ABAB, BABA, B⊤A⊤B^{\top}A^{\top} and A⊤B⊤A^{\top}B^{\top} are defined, and what are their sizes?
Show the answer

The product ABAB is defined, since AA has 22 columns and BB has 22 rows, and it is 3×43 \times 4. The product BABA is not defined, because BB has 44 columns but AA has 33 rows. The transposes are B⊤B^{\top}, of size 4×24 \times 2, and A⊤A^{\top}, of size 2×32 \times 3, so B⊤A⊤B^{\top}A^{\top} is defined, is 4×34 \times 3 and equals (AB)⊤(AB)^{\top}. The product A⊤B⊤A^{\top}B^{\top} would need the 33 columns of A⊤A^{\top} to match the 44 rows of B⊤B^{\top}, so it is not defined. Keeping the original order when transposing a product is a common mistake.

  1. Let RR be the counterclockwise quarter turn and PP the projection onto the horizontal axis,
R=[0−110],P=[1000].R = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix}, \qquad P = \begin{bmatrix} 1 & 0 \\ 0 & 0 \end{bmatrix}.

Compute RPRP and PRPR, apply both to e1\mathbf{e}_1, and explain the difference.

Show the answer

Apply the left matrix to each column of the right one. The columns of PP are (1,0)(1, 0) and (0,0)(0, 0), which RR sends to (0,1)(0, 1) and (0,0)(0, 0). The columns of RR are (0,1)(0, 1) and (−1,0)(-1, 0), which PP sends to (0,0)(0, 0) and (−1,0)(-1, 0). So

RP=[0010],PR=[0−100].RP = \begin{bmatrix} 0 & 0 \\ 1 & 0 \end{bmatrix}, \qquad PR = \begin{bmatrix} 0 & -1 \\ 0 & 0 \end{bmatrix}.

The product RPRP projects first, which leaves e1\mathbf{e}_1 at (1,0)(1, 0), and then turns it to (0,1)(0, 1). The product PRPR turns first, which sends e1\mathbf{e}_1 to (0,1)(0, 1), and the projection then sends that vertical vector to 0\mathbf{0}. So RPe1=(0,1)RP\mathbf{e}_1 = (0, 1) while PRe1=0PR\mathbf{e}_1 = \mathbf{0}.

  1. The 3×33 \times 3 matrices below are cut after the second row and after the second column, with c=(2,−1)\mathbf{c} = (2, -1) and d=(1,3)\mathbf{d} = (1, 3) in their top right blocks.
M=[10201−1001],N=[101013001].\begin{gathered} M = \left[\begin{array}{cc|c} 1 & 0 & 2 \\ 0 & 1 & -1 \\ \hline 0 & 0 & 1 \end{array}\right], \\[0.4em] N = \left[\begin{array}{cc|c} 1 & 0 & 1 \\ 0 & 1 & 3 \\ \hline 0 & 0 & 1 \end{array}\right]. \end{gathered}

Use block multiplication to find MNMN, check one entry directly, and decide whether MN=NMMN = NM.

Show the answer

Both matrices are cut into groups of sizes 22 and 11, for rows and for columns, so the partitions conform. In block form

M=[I2c0⊤1],N=[I2d0⊤1],M = \begin{bmatrix} I_2 & \mathbf{c} \\ \mathbf{0}^{\top} & 1 \end{bmatrix}, \qquad N = \begin{bmatrix} I_2 & \mathbf{d} \\ \mathbf{0}^{\top} & 1 \end{bmatrix},

where 0⊤\mathbf{0}^{\top} is the 1×21 \times 2 zero row. The four blocks of MNMN are

I2I2+c 0⊤=I2,I2d+c⋅1=c+d,0⊤I2+1⋅0⊤=0⊤,0⊤d+1⋅1=1.\begin{aligned} I_2 I_2 + \mathbf{c}\,\mathbf{0}^{\top} &= I_2, \\[0.4em] I_2\mathbf{d} + \mathbf{c} \cdot 1 &= \mathbf{c} + \mathbf{d}, \\[0.4em] \mathbf{0}^{\top} I_2 + 1 \cdot \mathbf{0}^{\top} &= \mathbf{0}^{\top}, \\[0.4em] \mathbf{0}^{\top}\mathbf{d} + 1 \cdot 1 &= 1. \end{aligned}

Here c 0⊤\mathbf{c}\,\mathbf{0}^{\top} is a 2×12 \times 1 matrix times a 1×21 \times 2 matrix, which is the 2×22 \times 2 zero matrix. Since c+d=(3,2)\mathbf{c} + \mathbf{d} = (3, 2),

MN=[103012001].MN = \begin{bmatrix} 1 & 0 & 3 \\ 0 & 1 & 2 \\ 0 & 0 & 1 \end{bmatrix}.

Directly, row 11 of MM times column 33 of NN is 1⋅1+0⋅3+2⋅1=31 \cdot 1 + 0 \cdot 3 + 2 \cdot 1 = 3, which matches. The same block computation for NMNM gives the top right block d+c\mathbf{d} + \mathbf{c}, which equals c+d\mathbf{c} + \mathbf{d}, and the other blocks are unchanged, so NM=MNNM = MN. These two matrices commute, as does every pair of matrices of this block form, but that is a fact about this special form, not a general rule.

How this lesson connects