Composition & Matrix Multiplication

You've multiplied matrices on Rank & Inverses, rows times columns, and seen that $AB \neq BA$. This page answers the "why". Matrix multiplication is built to do one job: keep track of what happens when you do one linear map and then another. Once that's clear, the rules of matrix algebra stop being rules to memorize and become facts about doing things in order.

You've multiplied matrices on Rank & Inverses and chained moves on the Geometry page. This page puts the two together. Doing one linear map and then another is again a linear map, and its matrix is the product of the two matrices. That's the whole reason the product is defined the way it is.

Composing Linear Maps

Doing one map and then another has a name, and a notation that reads backward the first few times you see it.

Let $T\colon \V \to \W$ and $U\colon \W \to \Z$ be functions. The composition $UT\colon \V \to \Z$ is the function $UT(x) = U(T(x))$: first $T$, then $U$.

If $T$ is a function whose outputs $U$ can take in, the composition $UT$ is the function $UT(x) = U(T(x))$: first $T$, then $U$.

Read $UT$ right to left. The map written next to $x$ is the one that touches it first. (Other books write $U \circ T$.)

Let $T, U\colon \R^2 \to \R^2$ be $T(a_1, a_2) = (a_1 + a_2, \ a_2)$ and $U(a_1, a_2) = (2a_1, \ a_1 - a_2)$. Find $UT$ and $TU$.


For $UT$, apply $T$ first, then feed its output into $U$:

$$UT(a_1, a_2) = U(a_1 + a_2, \ a_2) = \big(2(a_1 + a_2), \ (a_1 + a_2) - a_2\big) = (2a_1 + 2a_2, \ a_1).$$

For $TU$, the other way around:

$$TU(a_1, a_2) = T(2a_1, \ a_1 - a_2) = \big(2a_1 + (a_1 - a_2), \ a_1 - a_2\big) = (3a_1 - a_2, \ a_1 - a_2).$$

Different maps. Order matters.

Socks then shoes: fine. Shoes then socks: also technically a composition. Just not the same one.

Both answers came out linear again. That's no accident.

Let $\V$, $\W$, and $\Z$ be vector spaces, and let $T\colon \V \to \W$ and $U\colon \W \to \Z$ be linear. Then $UT\colon \V \to \Z$ is linear.

Show proof

Let $x, y \in \V$ and $a \in \R$. Then

$$UT(ax + y) = U(aT(x) + T(y)) = aU(T(x)) + U(T(y)) = a\,UT(x) + UT(y),$$

using that $T$ is linear and then that $U$ is. So $UT$ is linear (the one-line test). $\blacksquare$

If $T$ and $U$ are linear and $UT$ makes sense, then $UT$ is linear.

Why it's true (picture)

This is a visual argument, not a formal proof. A linear map sends the parallelogram on $x$ and $y$ to the parallelogram on their images, with the image of $x + y$ still at the far corner (and a stretched arrow to a stretched image). Do that twice and it's still true.

The full proof is in the n version.

When everything lives in one space $\V$, composition behaves almost like multiplication of numbers. Almost.

Let $\V$ be a vector space, let $T, U_1, U_2 \in \Lin(\V)$, and let $a \in \R$. Then

(a) $T(U_1 + U_2) = TU_1 + TU_2$ and $(U_1 + U_2)T = U_1T + U_2T$;

(b) $T(U_1U_2) = (TU_1)U_2$;

(c) $TI = IT = T$;

(d) $a(U_1U_2) = (aU_1)U_2 = U_1(aU_2)$.

Show proof

Each part is checked by applying both sides to a vector $x$. For the first half of (a): $T(U_1 + U_2)(x) = T(U_1(x) + U_2(x)) = TU_1(x) + TU_2(x)$, using that $T$ is linear. The second half doesn't even need linearity: $(U_1 + U_2)T(x) = U_1(T(x)) + U_2(T(x))$. Part (b) holds for any functions, since both sides send $x$ to $T(U_1(U_2(x)))$, and (c) and (d) are one line each the same way. $\blacksquare$

What's missing from the list is the interesting part: there's no $TU = UT$. The example above already broke it.

Composition follows most of the rules multiplication does (it distributes over sums, and doing nothing first or last changes nothing), but there's no $TU = UT$. The example above already broke it.

The Matrix of a Composition

On Rank & Inverses, $AB$ was defined column by column: column $j$ of $AB$ is $A$ times column $j$ of $B$. Here's where that definition comes from.

If $T$ and $U$ have matrices, what's the matrix of $UT$? Follow a basis vector through both maps. $T$ sends $e_j$ to column $j$ of $[T]$, and then $U$ multiplies that by $[U]$. So column $j$ of $[UT]$ is $[U]$ times column $j$ of $[T]$, which is exactly column $j$ of $[U][T]$.

Let $\V$, $\W$, and $\Z$ be finite-dimensional vector spaces with ordered bases $\alpha$, $\beta$, and $\gamma$, and let $T\colon \V \to \W$ and $U\colon \W \to \Z$ be linear. Then

$$[UT]_\alpha^\gamma = [U]_\beta^\gamma\,[T]_\alpha^\beta.$$

In particular, $[UT] = [U][T]$ for standard matrices, and $[UT]_\beta = [U]_\beta[T]_\beta$ when $\V = \W = \Z$ with one basis $\beta$.

Show proof

Let $\alpha = \{v_1, \ldots, v_n\}$. Column $j$ of $[UT]_\alpha^\gamma$ is $[UT(v_j)]_\gamma = [U(T(v_j))]_\gamma$. The matrix of a map turns coordinates of the input into coordinates of the output (The Matrix of a Linear Map), so

$$[U(T(v_j))]_\gamma = [U]_\beta^\gamma\,[T(v_j)]_\beta = [U]_\beta^\gamma \cdot (\text{column } j \text{ of } [T]_\alpha^\beta).$$

That is column $j$ of $[U]_\beta^\gamma[T]_\alpha^\beta$, by the column-by-column definition of the product. Every column agrees, so the matrices agree. $\blacksquare$

In entries: if $A = [U]_\beta^\gamma$ and $B = [T]_\alpha^\beta$, the coefficient of $z_i$ in $UT(v_j)$ is $\sum_k A_{ik}B_{kj}$, which is the row-times-column formula for $(AB)_{ij}$. The product was defined to make this theorem true.

If $T$ and $U$ are linear maps of $\R^2$ or $\R^3$ and $UT$ makes sense, then $[UT] = [U][T]$: the matrix of "do $T$, then $U$" is the product, with the first map on the right.

Why it's true (picture)

This is a visual argument, not a formal proof. Follow each $e_j$ through both maps. $T$ sends it to column $j$ of $[T]$. Then $U$ sends that arrow to $[U]$ times it, and that's column $j$ of $[U][T]$ (the column-by-column definition from Rank & Inverses). The columns of $[UT]$ are where $UT$ sends $e_1$, $e_2$, so they match.

The full proof is in the n version.

Check it on the example from the top of the page.

With $T(a_1, a_2) = (a_1 + a_2, \ a_2)$ and $U(a_1, a_2) = (2a_1, \ a_1 - a_2)$, compute $[U][T]$ and $[T][U]$, and compare with $UT$ and $TU$.


Reading the coefficients down, $[T] = \begin{pmatrix} 1 & 1 \\ 0 & 1 \end{pmatrix}$ and $[U] = \begin{pmatrix} 2 & 0 \\ 1 & -1 \end{pmatrix}$. Then

$$[U][T] = \begin{pmatrix} 2 & 0 \\ 1 & -1 \end{pmatrix}\begin{pmatrix} 1 & 1 \\ 0 & 1 \end{pmatrix} = \begin{pmatrix} 2 & 2 \\ 1 & 0 \end{pmatrix},$$

which is exactly the matrix of $UT(a_1, a_2) = (2a_1 + 2a_2, \ a_1)$. In the other order, $[T][U] = \begin{pmatrix} 3 & -1 \\ 1 & -1 \end{pmatrix}$, the matrix of $TU(a_1, a_2) = (3a_1 - a_2, \ a_1 - a_2)$.

The spaces don't have to match, as long as each map can take the last one's outputs. Then the sizes tell you which order even makes sense.

Let $T\colon \R^3 \to \R^2$ be $T(a_1, a_2, a_3) = (a_1 + a_3, \ a_2 - a_3)$ and $U\colon \R^2 \to \R^3$ be $U(b_1, b_2) = (b_1, \ b_2, \ b_1 + b_2)$. Find $[UT]$ and $[TU]$.


$[T] = \begin{pmatrix} 1 & 0 & 1 \\ 0 & 1 & -1 \end{pmatrix}$ is $2 \times 3$ and $[U] = \begin{pmatrix} 1 & 0 \\ 0 & 1 \\ 1 & 1 \end{pmatrix}$ is $3 \times 2$. $UT$ goes from $\R^3$ to $\R^3$:

$$[UT] = [U][T] = \begin{pmatrix} 1 & 0 & 1 \\ 0 & 1 & -1 \\ 1 & 1 & 0 \end{pmatrix}.$$

$TU$ goes from $\R^2$ to $\R^2$:

$$[TU] = [T][U] = \begin{pmatrix} 2 & 1 \\ -1 & 0 \end{pmatrix}.$$

Not even the same size. And notice $UT$ passes through $\R^2$ on its way back to $\R^3$, so its range is at most a plane: its $3 \times 3$ matrix has rank at most $2$.

Both examples follow the same rule, and it's worth saying once more.

The first map goes on the right: $[UT] = [U][T]$, because $[T]$ is the one that touches $x$ first in $[U][T]x$. Check sizes too: an $(m \times n)(n \times p)$ product is $m \times p$, and the inside numbers have to match, just as $U$ has to accept $T$'s outputs.

Pick a $T$ and a $U$ and watch the letter F go through both, in both orders.

Do $T$, Then $U$

Try "Rotate 90°" and "Reflect": $UT$ and $TU$ land the F in different places, and their matrices differ too. Your turn. In free response, type the matrix row by row, like 5, -1; -1, 0.

Loading practice engine...

Grouping and Order

With three maps in a row there are two ways to group them, and it never matters which you pick. That's a fact about doing things in order, so it comes for free.

If $S$, $T$, and $U$ are functions that can be done in that order, then $U(TS) = (UT)S$. Consequently matrix multiplication is associative: $A(BC) = (AB)C$ whenever the products are defined.

Show proof

For every $x$, both sides give $U(T(S(x)))$, so they are the same function.

For matrices, let $S$, $T$, $U$ be $x \mapsto Cx$, $x \mapsto Bx$, $x \mapsto Ax$, so $[S] = C$, $[T] = B$, and $[U] = A$. By the last theorem, $A(BC) = [U][TS] = [U(TS)]$ and $(AB)C = [UT][S] = [(UT)S]$. These are the standard matrices of the same map, and a map has only one standard matrix (The Matrix of a Linear Map). $\blacksquare$

Why it's true (picture)

This is a visual argument, not a formal proof. Grouping only says which two moves you combine into one before starting. Either way the F gets $S$, then $T$, then $U$, in that order, so it lands in the same place. The matrices are the matrices of those moves, so they agree too.

The full proof is in the n version.

Proving associativity with sums of sums of products: three pages of indices. Proving it by doing things in order: one line. Somebody should have told me this in high school.

Grouping is free. Order is not. Here are two very ordinary moves done in both orders.

Let $R$ be the quarter turn counterclockwise and $S$ the shear $S(a_1, a_2) = (a_1 + a_2, \ a_2)$. Find the matrix of "turn, then shear" and of "shear, then turn".


$[R] = \begin{pmatrix} 0 & -1 \\ 1 & 0 \end{pmatrix}$ and $[S] = \begin{pmatrix} 1 & 1 \\ 0 & 1 \end{pmatrix}$. "Turn, then shear" is $SR$, with the turn on the right:

$$[S][R] = \begin{pmatrix} 1 & 1 \\ 0 & 1 \end{pmatrix}\begin{pmatrix} 0 & -1 \\ 1 & 0 \end{pmatrix} = \begin{pmatrix} 1 & -1 \\ 1 & 0 \end{pmatrix}, \qquad [R][S] = \begin{pmatrix} 0 & -1 \\ 1 & 1 \end{pmatrix}.$$

Different matrices, and the F lands in different places:

So treat the order of a product as part of the problem, never as a choice.

Never swap the order of a product to make the arithmetic easier, and never cancel across it: from $AB = AC$ you can't conclude $B = C$ unless $A$ can be undone. Some pairs do commute (two turns, two stretches along the axes), but that's a fact to check, not a rule to assume.

Two names other books (and a few of these pages) use for things on this page:

The Kronecker delta is $\delta_{ij} = 1$ if $i = j$ and $\delta_{ij} = 0$ if $i \neq j$.

So $\delta_{ii} = 1$ and everything off the diagonal is $0$: the identity matrix in one symbol. The other name is for a map.

For an $m \times n$ matrix $A$, the left-multiplication transformation $L_A\colon \R^n \to \R^m$ is $L_A(x) = Ax$.

So $(I_n)_{ij} = \delta_{ij}$, and $[I_\V]_\beta = I_n$ for every ordered basis $\beta$ of an $n$-dimensional $\V$ (each $v_j$ has coordinates $e_j$). And $L_A$ is the map $x \mapsto Ax$ with a name: $[L_A] = A$, and the matrix-of-a-composition theorem says $L_{AB} = L_AL_B$.

Application: Angle Addition

Two turns of the plane, one after the other, make one turn by the sum of the angles. Put that next to the theorem about products, and trigonometry falls out.

The turn by $\theta$ has matrix $R_\theta = \begin{pmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{pmatrix}$, counterclockwise.

For all angles $\alpha$ and $\beta$, $R_\beta R_\alpha = R_{\alpha + \beta}$. In particular any two rotations commute, and $R_\theta^k = R_{k\theta}$.

Show proof

$R_\beta R_\alpha$ is the matrix of turning by $\alpha$ and then by $\beta$. That map turns every vector by $\alpha + \beta$ (angles measured from the positive $x$-axis add), so it is the turn by $\alpha + \beta$, whose matrix is $R_{\alpha + \beta}$. A map has only one standard matrix, so $R_\beta R_\alpha = R_{\alpha + \beta}$. The same argument with $\alpha$ and $\beta$ traded gives $R_\alpha R_\beta = R_{\alpha + \beta}$, and applying it $k - 1$ times gives $R_\theta^k = R_{k\theta}$. $\blacksquare$

Why it's true (picture)

This is a visual argument, not a formal proof. Watch $e_1$ and $e_2$ in the figure below: they turn by $30^\circ$, then by $45^\circ$ more, and end up turned by $75^\circ$, exactly where one turn by $75^\circ$ would put them. Same images of $e_1$ and $e_2$, so same matrix.

Here it is with $\alpha = 30^\circ$ and $\beta = 45^\circ$.

Now multiply the left side out, and compare entries.

Use $R_\beta R_\alpha = R_{\alpha + \beta}$ to find formulas for $\cos(\alpha + \beta)$ and $\sin(\alpha + \beta)$.


$$R_\beta R_\alpha = \begin{pmatrix} \cos\beta & -\sin\beta \\ \sin\beta & \cos\beta \end{pmatrix}\begin{pmatrix} \cos\alpha & -\sin\alpha \\ \sin\alpha & \cos\alpha \end{pmatrix} = \begin{pmatrix} \cos\alpha\cos\beta - \sin\alpha\sin\beta & \cdots \\ \sin\alpha\cos\beta + \cos\alpha\sin\beta & \cdots \end{pmatrix}.$$

The first column of $R_{\alpha + \beta}$ is $(\cos(\alpha + \beta), \sin(\alpha + \beta))$, so

$$\cos(\alpha + \beta) = \cos\alpha\cos\beta - \sin\alpha\sin\beta, \qquad \sin(\alpha + \beta) = \sin\alpha\cos\beta + \cos\alpha\sin\beta.$$

The angle-addition formulas, with no triangles at all.

The formulas turn special angles into new exact values.

Find $\cos 75^\circ$ exactly, and find $R_{30^\circ}^{12}$.


$75^\circ = 30^\circ + 45^\circ$, so

$$\cos 75^\circ = \cos 30^\circ\cos 45^\circ - \sin 30^\circ\sin 45^\circ = \frac{\sqrt{3}}{2}\cdot\frac{\sqrt{2}}{2} - \frac{1}{2}\cdot\frac{\sqrt{2}}{2} = \frac{\sqrt{6} - \sqrt{2}}{4}.$$

And twelve turns of $30^\circ$ make one full turn: $R_{30^\circ}^{12} = R_{360^\circ} = I$. Multiplying twelve matrices by hand would take a while; adding twelve angles doesn't.

Your turn. The answers have square roots in them, so this one is multiple choice.

Loading practice engine...

Composition has an "undo" question built in: when can a map $T$ be followed by some $U$ that puts everything back, $UT = I$? For a turn it's easy, turn back: $R_{-\theta}R_\theta = I$. In general the answer depends on null spaces and dimensions, and it's the next page.

The toolkit for this page: $UT$ means $T$ first. $[UT] = [U][T]$, with the first map on the right, and the sizes have to fit. Grouping never matters, $A(BC) = (AB)C$; order usually does. Two turns add their angles, $R_\beta R_\alpha = R_{\alpha + \beta}$.

Mixed Practice

Every question so far came right after its idea, which is a hint a quiz never gives you. Down here the hint is gone. First, true or false: a claim from anywhere on this page, and you decide whether it holds.

Loading practice engine...

Next, a mixed set: every question type on this page, shuffled. The answers are matrices, so this set is multiple choice.

Loading practice engine...

Made it this far? Time for something with a little heat. Three maps in a row this time. Keep the order straight. In free response, type the matrix row by row.

🌶️ SPICY BONUS 🌶️

Loading practice engine...