Composing transformations as a matrix product
A choreographer moves a whole formation one way and then another. Is there a single matrix for the combined move, and how is it built from the matrices of the two moves?
Where you are 9 in Matrices as linear transformations
Why this exists
Txia is a choreographer. She records each dancer’s spot as a vector from center stage, in meters: the first entry runs across the stage, positive to the audience’s right, and the second runs upstage, away from the audience. A move of the whole formation is then a function from \mathbb{R}^2 to \mathbb{R}^2, and her usual moves (slants, turns about center stage, mirror flips) are linear transformations, each with a matrix.
A piece is one move after another, and Txia wants one matrix for the combined move, rather than pushing every dancer through every matrix in turn. This lesson shows it is the product of the matrices, with the move done first on the right, and that this is why matrix multiplication is defined the way it is.
Definition
To compose two functions is to feed the output of one into the other. If S: \mathbb{R}^n \to \mathbb{R}^m and T: \mathbb{R}^m \to \mathbb{R}^p, the composition T \circ S: \mathbb{R}^n \to \mathbb{R}^p is
It is read “T after S”: S acts first though it is written second, because it sits next to \mathbf{x}. It makes sense only when the codomain of S is the domain of T.
The product BA of a p \times m matrix B and an m \times n matrix A is the p \times n matrix of Matrix multiplication. Read a column at a time, its row-by-column rule says column j of BA is B\mathbf{a}_j, where \mathbf{a}_j is column j of A, since entry i of B\mathbf{a}_j is row i of B times \mathbf{a}_j.
Intuition
A linear move’s matrix is read off from where it sends \mathbf{e}_1 and \mathbf{e}_2, so for a combined move, follow each standard vector through both moves.
Txia’s slant is the shear A = \begin{bmatrix} 1 & 2 \\ 0 & 1 \end{bmatrix}, (x, y) \mapsto (x + 2y,\ y): each dancer slides across by twice their distance upstage. Her quarter turn is B = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix}, (x, y) \mapsto (-y,\ x), counterclockwise about center stage on her floor plan. Shear first: \mathbf{e}_1 \to (1, 0) \to (0, 1) and \mathbf{e}_2 \to (2, 1) \to (-1, 2). Each image is B applied to a column of A, which is how the columns of BA are made. Turn first: \mathbf{e}_1 \to (0, 1) \to (2, 1) and \mathbf{e}_2 \to (-1, 0) \to (-1, 0), the columns of AB.
Two different parallelograms: the two orders are two different moves.
Results
The matrix of a composition is the product. If S(\mathbf{x}) = A\mathbf{x} with A of size m \times n, and T(\mathbf{y}) = B\mathbf{y} with B of size p \times m, then T \circ S is linear and, for every \mathbf{x} in \mathbb{R}^n,
The move done first stands on the right, and column j of BA is T(S(\mathbf{e}_j)).
Sizes chain. (p \times m)(m \times n) = p \times n. The inner size m is the middle space \mathbb{R}^m, where S lands and T starts, so the product is defined exactly when the composition is.
Order matters; grouping does not. In general AB \neq BA (when they are equal, the matrices commute). If U with matrix C is done third, (CB)A = C(BA), both the matrix of U \circ T \circ S.
Turns add. With R_\theta = \begin{bmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix}, the counterclockwise turn by \theta about the origin from Finding the matrix of a transformation,
Undoing goes in reverse order. If A and B are invertible n \times n matrices, (BA)^{-1} = A^{-1}B^{-1}: to undo “S, then T”, undo T first.
Why it is true
The composition is linear. By additivity of S and then of T,
and a scalar passes through both the same way. So T \circ S has a standard matrix, with column j equal to (T \circ S)(\mathbf{e}_j).
Its columns are those of BA. Since A\mathbf{e}_j = \mathbf{a}_j,
which is column j of BA. A linear transformation has only one standard matrix (Linear transformation), so the matrix of T \circ S is BA; that is, B(A\mathbf{x}) = (BA)\mathbf{x} for every \mathbf{x}. The product B\mathbf{a}_j needs \mathbf{a}_j to have as many entries as B has columns, which is the size rule.
Grouping. For every \mathbf{x}, both ((U \circ T) \circ S)(\mathbf{x}) and (U \circ (T \circ S))(\mathbf{x}) equal U(T(S(\mathbf{x}))): the same three moves in the same order. Their matrices are (CB)A and C(BA), and one function has one standard matrix, so these are equal.
Turns. A point at distance r from the origin and angle \varphi from the first axis goes to angle \varphi + \alpha, then \varphi + \alpha + \beta, its distance unchanged. That is the turn by \alpha + \beta, so R_\beta R_\alpha = R_{\alpha+\beta}, and swapping \alpha and \beta gives the same matrix.
Undoing. By grouping, (A^{-1}B^{-1})(BA) = A^{-1}(B^{-1}B)A = A^{-1}A = I, and likewise (BA)(A^{-1}B^{-1}) = I. When a move can be undone at all is When a matrix or transformation can be undone.
Worked examples
Shear, then turn
Eneko stands at \mathbf{v} = (3, 1). Txia applies the shear A and then the quarter turn B. Find where Eneko ends, one move at a time and with the single matrix of the combined move.
SolutionOne move at a time, the shear adds twice his upstage distance to the first entry, and the turn sends (x, y) to (-y, x):
The combined move has matrix BA, the shear on the right because it acts first:
Row 1 of B times column 2 of A is 0(2) + (-1)(1) = -1. Then (BA)\mathbf{v} = \big(0(3) + (-1)(1),\ 1(3) + 2(1)\big) = (-1, 5), the same spot, as the theorem says. The columns (0, 1) and (-1, 2) are the images of \mathbf{e}_1 and \mathbf{e}_2 traced in the Intuition section.
Turn, then shear
Now the turn comes first. Find the matrix, Eneko’s end spot, and every dancer who ends in the same place under both orders.
SolutionThe turn acts first, so it stands on the right:
Eneko ends at (AB)\mathbf{v} = (6 - 1,\ 3) = (5, 3), the same as turning him to (-1, 3) and shearing. The other order put him at (-1, 5).
A dancer at \mathbf{x} ends in the same place under both orders exactly when (BA - AB)\mathbf{x} = \mathbf{0}, by the distributive law. Here
so -2x = 0 and 2y = 0, and only \mathbf{x} = \mathbf{0} works: the dancer at center stage, whom both moves leave in place.
From three numbers to a spot on the floor
Txia writes a phrase as a count of three steps: a side step (1, 0), an upstage step (0, 1) and a diagonal step (1, 1), in meters. A phrase of a, b and c of these moves a dancer by S(a, b, c) = a(1, 0) + b(0, 1) + c(1, 1). To restage it on a stage facing a quarter turn away, she turns every displacement by B. Find the matrix of the combined map, its size, and the displacement for the phrase (2, 1, 3). Can the maps be composed in the other order?
SolutionS: \mathbb{R}^3 \to \mathbb{R}^2 has the three steps as its columns:
The combined map goes from \mathbb{R}^3 through \mathbb{R}^2 to \mathbb{R}^2. Its matrix is BC, of size (2 \times 2)(2 \times 3) = 2 \times 3, and each column is B applied to a step: B(1, 0) = (0, 1), B(0, 1) = (-1, 0), B(1, 1) = (-1, 1). So
and BC(2, 1, 3) = (0 - 1 - 3,\ 2 + 0 + 3) = (-4, 5).
The other order, CB, is (2 \times 3)(2 \times 2): C has 3 columns and B has 2 rows, so it is undefined. So are the maps: B outputs a displacement in \mathbb{R}^2, but S needs three step counts.
Three moves, grouped two ways
Txia adds a mirror flip across the center line, F = \begin{bmatrix} -1 & 0 \\ 0 & 1 \end{bmatrix}. The sequence is shear, turn, flip. Find its matrix as F(BA) and as (FB)A, and where Jalmari, starting at (2, -1), ends.
SolutionThe first move stands rightmost, so the matrix is FBA. With BA from Example 1, and with FB = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix},
The groupings agree because each is bookkeeping for the same three moves in the same order. Then FBA sends (2, -1) to \big(0(2) + 1(-1),\ 1(2) + 2(-1)\big) = (-1, 0). Move by move: the shear takes Jalmari to (0, -1), the turn to (1, 0), the flip to (-1, 0). Grouping is free but order is not: flip, then turn, then shear has matrix ABF = \begin{bmatrix} -2 & -1 \\ -1 & 0 \end{bmatrix}, a different move.
Turns add, and the angle-sum formulas
Use R_\beta R_\alpha = R_{\alpha+\beta} to find \cos(\alpha + \beta) and \sin(\alpha + \beta). Check with \alpha = 30^\circ and \beta = 60^\circ.
SolutionTurning by \alpha and then by \beta is turning by \alpha + \beta, so its matrix is R_{\alpha+\beta}, and by the composition rule it is also R_\beta R_\alpha, with \beta on the left because it comes second.
The first column of R_\beta R_\alpha is R_\beta times the first column of R_\alpha:
The first column of R_{\alpha+\beta} is (\cos(\alpha + \beta), \sin(\alpha + \beta)). Matching entries,
These are the Cosine of a sum or difference and the sum formula of Sine and tangent of a sum or difference, both read off one product.
For the check, \displaystyle \cos 30^\circ = \sin 60^\circ = \tfrac{\sqrt3}{2} and \sin 30^\circ = \cos 60^\circ = \tfrac12. Multiplying out, R_{60^\circ}R_{30^\circ} has first column (0, 1) and second column (-1, 0), so it is B = R_{90^\circ}.
Undoing a sequence
Find the matrix of the move that brings every dancer back after “shear, then turn”, and check it on Eneko’s end spot (-1, 5) from Example 1.
SolutionUndo the last move first. The turn is undone by the clockwise quarter turn (x, y) \mapsto (y, -x), the shear by sliding back, (x, y) \mapsto (x - 2y, y):
The undoing move is “B^{-1}, then A^{-1}”:
Multiplying it by BA from Example 1 gives I, and on Eneko, \big(2(-1) + 1(5),\ -1(-1) + 0(5)\big) = (3, 1), his starting spot. General 2 \times 2 inverses are in Inverse of a 2 by 2 matrix, and solving with it.
Boundaries
- The sizes must chain. T \circ S needs the codomain of S to be the domain of T, and BA needs as many columns in B as rows in A. Sometimes only one order is defined (Example 3).
- The order is not free. AB = BA holds for particular pairs (two turns about one center, any matrix with I or cI), never as a rule.
- Only linear moves have such matrices. A slide \mathbf{x} \mapsto \mathbf{x} + \mathbf{b} with \mathbf{b} \neq \mathbf{0} moves center stage, so it has no 2 \times 2 matrix, and a sequence with a slide in it is in general not linear either (Exercise 19).
- Undoing needs every move undoable. (BA)^{-1} = A^{-1}B^{-1} needs both inverses. A move that flattens the formation onto a line cannot be undone, and for square matrices neither can any sequence that includes it.
Where this goes next
Every matrix product can now be read as one move after another. When a matrix or transformation can be undone asks when a move can be undone, and Finding an inverse by row reducing A next to I computes the undoing matrix. Geometry’s Two reflections in a row becomes a short product (Exercise 4), and the turn law returns in Adding, subtracting and multiplying complex numbers, where multiplying by \cos\theta + i\sin\theta turns the plane. A product such as QKQ^{-1} (Exercise 15) is a move seen from a turned frame, the idea of Coordinates in a basis and change of basis. Powers A^k, one move repeated, lead to Diagonalization and Matrix powers, dynamical systems and steady states.
Exercises
Matrices of combined moves
- Let G = \begin{bmatrix} 1 & 0 \\ 3 & 1 \end{bmatrix}, a slant upstage, and Q = \begin{bmatrix} 0 & 1 \\ -1 & 0 \end{bmatrix}, the quarter turn clockwise. Find the matrix of “G, then Q” and of “Q, then G”.
- Txia spreads the formation with P = \begin{bmatrix} 2 & 0 \\ 0 & 1 \end{bmatrix} and then applies Q. Find the matrix of the combined move and where a dancer at (2, 3) ends, move by move and with that matrix.
- Let H = \begin{bmatrix} -1 & 0 \\ 0 & -1 \end{bmatrix}, the half turn, and K = \begin{bmatrix} 1 & -1 \\ 0 & 1 \end{bmatrix}. Show that HK = KH and explain why.
- Let X = \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}, the reflection across the first axis, and \displaystyle Y = \begin{bmatrix} \tfrac12 & \tfrac{\sqrt3}{2} \\ \tfrac{\sqrt3}{2} & -\tfrac12 \end{bmatrix}, the reflection across the line through the origin at 30^\circ to that axis. Find the matrices of “X, then Y” and “Y, then X”, and name each move.
- M is 2 \times 4 and N is 3 \times 2. Which of NM and MN is defined? For that one, say which map is done first, its size, and between which spaces it goes.
- Let L = \begin{bmatrix} 1 & 2 \\ 0 & 1 \\ 1 & 0 \end{bmatrix} and W = \begin{bmatrix} 1 & 0 & -1 \\ 2 & 1 & 0 \end{bmatrix}. Find the matrices of “L, then W” and “W, then L”, and the spaces each goes between.
- Txia does K (Exercise 3), then Q, then X. Find the matrix of the sequence as (XQ)K and as X(QK), and where a dancer at (1, 1) ends.
Turns
- Find R_{45^\circ}R_{45^\circ} by multiplying, and name the move.
- Find R_{120^\circ}R_{120^\circ} and then (R_{120^\circ})^3. Explain the second answer in terms of turns.
- Use R_\alpha R_{-\beta} = R_{\alpha-\beta} to find formulas for \cos(\alpha - \beta) and \sin(\alpha - \beta).
- Use R_\theta R_\theta = R_{2\theta} to find formulas for \cos 2\theta and \sin 2\theta.
Undoing a move
- Find the matrix that undoes “G, then Q” from Exercise 1, as a product of two inverses, and check that it does.
- Show that Q^{-1}G^{-1} does not undo “G, then Q”, by multiplying it with QG.
- Show that XQX = Q^{-1}, and say what it means for a clockwise quarter turn seen in a mirror.
- Txia wants to turn the formation first with Q and still end exactly where “K, then Q” puts everyone. Find the move J with JQ = QK, and describe it.
Reasoning
- A move S has matrix A and a move T has matrix B. Explain, without multiplying any entries, why the matrix of “S, then T” is BA and not AB.
- Prove that any two turns about the origin commute.
- Prove that if a 2 \times 2 matrix A commutes with B and with C, then A commutes with BC. Name the property each step uses.
- Show that “slide every dancer one meter to the right, then turn by Q” is not given by any 2 \times 2 matrix.
- Find every 2 \times 2 matrix that commutes with Q, and describe what such a matrix does.
Answers
- “G, then Q” is QG = \begin{bmatrix} 3 & 1 \\ -1 & 0 \end{bmatrix}; “Q, then G” is GQ = \begin{bmatrix} 0 & 1 \\ -1 & 3 \end{bmatrix}.
- QP = \begin{bmatrix} 0 & 1 \\ -2 & 0 \end{bmatrix}. Move by move, P(2, 3) = (4, 3) and Q(4, 3) = (3, -4); with the matrix, QP(2, 3) = (3, -4).
- Both products are \begin{bmatrix} -1 & 1 \\ 0 & -1 \end{bmatrix}. H = -I, and (-I)K = -K = K(-I).
- “X, then Y” is \displaystyle YX = \begin{bmatrix} \tfrac12 & -\tfrac{\sqrt3}{2} \\ \tfrac{\sqrt3}{2} & \tfrac12 \end{bmatrix} = R_{60^\circ}, the counterclockwise turn by 60^\circ; “Y, then X” is \displaystyle XY = \begin{bmatrix} \tfrac12 & \tfrac{\sqrt3}{2} \\ -\tfrac{\sqrt3}{2} & \tfrac12 \end{bmatrix}, the clockwise turn by 60^\circ. The mirrors cross at 30^\circ and each product turns by twice that, as Two reflections in a row says.
- NM, (3 \times 2)(2 \times 4), is 3 \times 4: M is done first, and NM goes from \mathbb{R}^4 to \mathbb{R}^3. MN is (2 \times 4)(3 \times 2) with 4 \neq 3, so it is undefined.
- “L, then W” is WL = \begin{bmatrix} 0 & 2 \\ 2 & 5 \end{bmatrix}, from \mathbb{R}^2 to \mathbb{R}^2. “W, then L” is LW = \begin{bmatrix} 5 & 2 & -1 \\ 2 & 1 & 0 \\ 1 & 0 & -1 \end{bmatrix}, from \mathbb{R}^3 to \mathbb{R}^3.
- XQ = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} and QK = \begin{bmatrix} 0 & 1 \\ -1 & 1 \end{bmatrix}, and both groupings give \begin{bmatrix} 0 & 1 \\ 1 & -1 \end{bmatrix}. The dancer ends at (1, 0).
- Each entry of R_{45^\circ} is \displaystyle \pm\tfrac{\sqrt2}{2}, and R_{45^\circ}R_{45^\circ} = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix}, the counterclockwise quarter turn R_{90^\circ}.
- \displaystyle R_{120^\circ}R_{120^\circ} = \begin{bmatrix} -\tfrac12 & \tfrac{\sqrt3}{2} \\ -\tfrac{\sqrt3}{2} & -\tfrac12 \end{bmatrix} = R_{240^\circ}, and (R_{120^\circ})^3 = I: three turns of 120^\circ make a full turn, which leaves every point where it was.
- R_{-\beta} has first column (\cos\beta, -\sin\beta), so the first column of R_\alpha R_{-\beta} gives \cos(\alpha - \beta) = \cos\alpha\cos\beta + \sin\alpha\sin\beta and \sin(\alpha - \beta) = \sin\alpha\cos\beta - \cos\alpha\sin\beta.
- The first column of R_\theta R_\theta is (\cos^2\theta - \sin^2\theta,\ 2\sin\theta\cos\theta), so \cos 2\theta = \cos^2\theta - \sin^2\theta and \sin 2\theta = 2\sin\theta\cos\theta.
- Undo Q first, then G: with G^{-1} = \begin{bmatrix} 1 & 0 \\ -3 & 1 \end{bmatrix} and Q^{-1} = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix}, G^{-1}Q^{-1} = \begin{bmatrix} 0 & -1 \\ 1 & 3 \end{bmatrix}, and its product with QG is I.
- Q^{-1}G^{-1} = \begin{bmatrix} 3 & -1 \\ 1 & 0 \end{bmatrix}, and (QG)(Q^{-1}G^{-1}) = \begin{bmatrix} 10 & -3 \\ -3 & 1 \end{bmatrix}, which is not I.
- XQX = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix}, which is Q^{-1} because its product with Q is I. A clockwise turn seen in a mirror runs counterclockwise.
- J = QKQ^{-1} = \begin{bmatrix} 1 & 0 \\ 1 & 1 \end{bmatrix}, which sends (x, y) to (x,\ y + x): a shear sliding each dancer upstage by their distance across. Check: JQ = \begin{bmatrix} 0 & 1 \\ -1 & 1 \end{bmatrix} = QK.
- “S, then T” sends \mathbf{x} to T(S(\mathbf{x})) = B(A\mathbf{x}). The matrix that acts first is the one next to \mathbf{x}, and B(A\mathbf{x}) = (BA)\mathbf{x}. AB would apply B first.
- By the turn law, R_\alpha R_\beta = R_{\beta+\alpha} and R_\beta R_\alpha = R_{\alpha+\beta}, and \alpha + \beta = \beta + \alpha, so the products are equal.
- A(BC) = (AB)C by associativity, = (BA)C since A commutes with B, = B(AC) by associativity, = B(CA) since A commutes with C, = (BC)A by associativity.
- The combined move sends \mathbf{0} to Q(1, 0) = (0, -1), not \mathbf{0}. Every matrix sends \mathbf{0} to \mathbf{0}, so no matrix gives this move.
- For Z = \begin{bmatrix} a & b \\ c & d \end{bmatrix}, comparing QZ and ZQ entry by entry gives c = -b and d = a: the matrices \begin{bmatrix} a & b \\ -b & a \end{bmatrix}. Unless a = b = 0, such a matrix is a turn about the origin followed by a scaling by \displaystyle \sqrt{a^2 + b^2}.