Linear Algebra · Theory · M2
M2-T5 — Composition of Transformations
Doing one transformation and then another is a matrix product, with the first one on the right. The order is the whole story.
A robot measurement passes through several frames before it reaches the map. Each hop is a matrix, and the whole trip is their product. This lesson explains why the product is the composition, why it reads right to left, and what goes wrong when you reverse it.
Learning objectives
After this lesson, you should be able to:
- Show that applying T₁ and then T₂ has the matrix M₂M₁ (note the order).
- Compute the composition of two 2 × 2 transformations and apply it to a vector.
- Predict when two transformations commute and when they do not.
- Derive that composing rotations adds their angles.
Introduction and motivation
Sensor to body, body to base, base to world: a robot's measurements are composed through frames all the time, and so is the motion of each link of an arm. If composing were done in the wrong order, positions would be wrong by metres. The matrix product is built precisely so that composition is multiplication. The price is that the order matters, and this lesson is about paying it correctly.
The intuition
Suppose you first stretch a picture horizontally and then turn it by 90°, versus first turn it and then stretch horizontally. The pictures differ: in the second case the stretch happens along a direction that has already been turned. Composition is not commutative in general, because later transformations act on the result of earlier ones.
The matrix product records this exactly. If B acts first and A acts second, the combined matrix is A B. When you apply it to a vector, (AB)x = A(Bx): the vector meets B first, because B is closest to it. Read a product from right to left to follow the order of events.
Interactive figure. An F-shaped outline is transformed by a rotation and a stretch in two different orders. The green shape is stretch first then rotate and the orange shape is rotate first then stretch. The readout compares the two product matrices. This figure needs JavaScript to be interactive.
Geometric and physical interpretation
Geometric view. Track the unit square: the first matrix carries it to a parallelogram, and the second carries that parallelogram on to another one. The final columns are the second matrix applied to the columns of the first: the columns of AB are A times the columns of B. Physical view. Each link of a robot arm is a transformation relative to the previous link. Moving the shoulder moves everything beyond it, which is composition of frames: the elbow's matrix acts on the already-moved hand, never the other way round.
The mathematics
- T₁, T₂
- two linear transformations with matrices M₁, M₂.
- T₂∘T₁
- “T₂ after T₁”: first T₁, then T₂.
- R(θ)
- the 2D rotation matrix [[cos θ, −sin θ], [sin θ, cos θ]].
Composition is the product
(T2 ∘ T1)(x) = T2( T1(x) ) = M2 ( M1 x ) = ( M2 M1 ) x
What it means. Applying T₁ then T₂ is multiplication by the product M₂M₁. The first transformation sits on the right because it must act on x first.
Columns of a product
column j of (A B) = A ( column j of B )
What it means. Each column of AB is the matrix A applied to the corresponding column of B. This is the column view of the previous lesson applied once per column, and it is the most useful way to compute a product by hand.
Rotations compose by adding angles
R(a) R(b) = R(a + b)
What it means. Rotating by b and then by a is the same as rotating by a + b. In the plane, the order of rotations does not matter. (In 3D it does, as Module 7 shows.)
Composition is the matrix product with the first transformation on the right. The order matters, except for special pairs such as two planar rotations.
Step-by-step derivation: composition is a product, and rotations add angles
Composition is the product. Let T₁(x) = M₁x and T₂(y) = M₂y. Then for every vector x:
(T₂∘T₁)(x) = T₂(T₁(x)) = T₂(M₁x) = M₂(M₁x).- Matrix multiplication is associative (previous lessons), so
M₂(M₁x) = (M₂M₁)x. - Therefore the matrix of the composition is
M₂M₁. Note that the matrix applied first appears last in writing.
The columns of a product. The j-th column of M₂M₁ is (M₂M₁)ej = M₂(M₁ej), which is M₂ applied to the j-th column of M₁. That confirms the column-by-column rule.
Rotations add angles. Multiply R(a)R(b):
R(a) R(b) = [ cos a cos b − sin a sin b −(cos a sin b + sin a cos b) ]
[ sin a cos b + cos a sin b cos a cos b − sin a sin b ]What it means. Each entry combines products of cosines and sines. The angle-addition formulas recognise them in the next step.
The angle-addition identities cos(a + b) = cos a cos b − sin a sin b and sin(a + b) = sin a cos b + cos a sin b turn each entry into cos(a + b) or sin(a + b), so the product is R(a + b). Since a + b = b + a, also R(b)R(a) = R(a + b): planar rotations commute.
A worked example: order of a stretch and a quarter turn, and two rotations
Let R = [[0, −1], [1, 0]] (a 90° turn) and S = [[2, 0], [0, 1]] (stretch x by 2). Apply both to x = (1, 0).
- Rotate first, then stretch (
S R):R x = (0, 1), thenS (0, 1) = (0, 1). - Stretch first, then rotate (
R S):S x = (2, 0), thenR (2, 0) = (0, 2). - Matrices.
S R = [[0, −2], [1, 0]]andR S = [[0, −1], [2, 0]]. They differ, and the vector (1, 0) lands at (0, 1) or (0, 2) depending on the order. - Columns rule check. Column 1 of
R SisRapplied to column 1 ofS, which is(2, 0), giving(0, 2), matching the first column(0, 2)above. - Two rotations.
R(30°) R(60°) = R(90°) = [[0, −1], [1, 0]](up to rounding such as 6·10⁻¹⁷ for the zeros). Rotation by 30° twice givesR(60°) = [[0.5, −0.866], [0.866, 0.5]].
Robotics application
Chained frames. A LiDAR point in the sensor frame is multiplied by the sensor-to-body matrix, then the body-to-world matrix, giving x_world = W B x_sensor. Precompute WB if both are fixed, as for a sensor bolted to a robot with a fixed pose. When the robot moves, W changes at every step and B stays constant. See poses and frames and 2D rigid transforms in the SLAM track for the full rigid-body version.
Manipulator links. The position of a robot hand is the product of one transformation per joint. Module 8 builds the forward kinematics of a planar arm exactly in this way: rotate by the shoulder angle, translate along the first link, rotate by the elbow, translate along the second.
Calibration pipelines. A camera's pixel-to-metric scaling, lens correction and extrinsic rotation are applied in sequence. Reordering them gives a different, wrong, calibration, a bug that shows only as systematic error. The UAV track's camera, IMU and LiDAR extrinsic calibration lesson is a long worked chain.
Why the order bug is dangerous. Swapping a rotation and a translation moves the robot's sensor to a different place. The swapped code runs, produces finite numbers, and puts every obstacle slightly wrong: errors that look like noise.
Engineering implications and limitations
Practical rules
- Write the chain with frame names. A product like
W_T_B · B_T_S(world from body, body from sensor) has matching inner names that cancel, which makes order errors visible on the page. - Read from right to left. The rightmost matrix acts first. When in doubt, apply the product to a vector whose answer you know.
- Precompute fixed chains. The product of constant calibration matrices can be computed once, which also removes repeated rounding error.
- Numerical drift. Multiplying many rotation matrices accumulates rounding error, and the result is no longer exactly a rotation; periodically re-orthonormalise (Module 4 and Module 7).
- Test with a known vector. Feed (1, 0) and (0, 1) through your pipeline: the outputs must be the columns of the composite you expect.
Limits
The statement “planar rotations commute” is special to 2D. Rotations in 3D do not commute (a quarter turn about x then about y differs from y then x), and any combination with scaling or translation generally does not commute either. Do not carry the 2D intuition into 3D.
Common misconceptions
“Composition order does not matter, so I can multiply the matrices in any order.”
The product is not commutative. The rotation-then-stretch and stretch-then-rotation examples above land at different points.
“The first transformation is the left matrix.”
The first transformation is the right-most matrix, the one nearest the vector. Writing M₂M₁ for “M₁ then M₂” is the standard point of confusion.
“If two transformations are both rotations they always commute.”
Only in the plane. Two rotations about different axes in 3D generally do not commute.
“Precomputing the product loses information.”
The product contains exactly the combined effect. You lose only the ability to read off the intermediate frame, which is why keep the individual matrices when you need them.
Engineering takeaways
- The composition “T₁ then T₂” has matrix M₂M₁: the first transformation is written on the right.
- The columns of AB are A applied to the columns of B.
- Planar rotations commute and their angles add: R(a)R(b) = R(a + b). In general, composition is not commutative.
- A chain of frames (sensor to body to world) is a product of matrices, and the order of the factors is part of the definition.
- Reading a product from right to left follows the order of events.
References and further reading
Primary papers and authoritative documentation.
- MIT 18.06SC Linear Algebra (OCW): lecture on matrix multiplication as composition
- Modern Robotics (Lynch and Park), free preprint: chapter 3: composing rigid-body motions
- 3Blue1Brown, Essence of Linear Algebra: matrix multiplication as composition of transformations
- SLAM track: 2D rigid transforms: composition of rotation and translation as a pose algebra
Knowledge check
Three conceptual questions. Write an answer, then reveal the explanation. Your answers stay in your browser.
Question 1
A has rows (2, 0) and (0, 1) (stretch x by 2) and B has rows (0, −1) and (1, 0) (turn by 90°). Compute AB and BA and the image of (1, 1) under each.
Explanation
AB = [[0, −2], [1, 0]] (turn first, then stretch) and BA = [[0, −1], [2, 0]] (stretch first, then turn). AB (1, 1) = (−2, 1) and BA (1, 1) = (−1, 2). Different results: the stretch acts along a different direction depending on whether the turn has already happened.
Question 2
Why does the product in W B x have the body-to-world matrix W on the left, even though the sensor-to-body step happens first?
Explanation
Because each matrix multiplies the vector nearest to it on its right: the product W(Bx) evaluates Bx first (sensor to body) and then applies W (body to world). The left-to-right order on the page is the reverse of the order of events; the frame names around the matrices (world←body, body←sensor) line up so the inner names cancel.
Question 3
Show that R(45°) R(45°) = R(90°) using the entries, and say what would be different in 3D.
Explanation
With c = s = √2/2, R(45°)² has entries c² − s² = 0 and 2cs = 1, giving [[0, −1], [1, 0]] = R(90°). Planar rotations always add angles because their entries obey the angle-sum identities. In 3D two rotations about different axes do not commute, so the order and not just the total angles matter.
What’s next
Every transformation has a size effect: it can stretch area or squash it flat. The determinant measures this, and it also tells you whether a transformation can be undone.
Related content
Nothing below is required to finish this lesson.
- BeforeM2-T4: Matrices as Linear Transformations
Read the columns of a matrix as where the basis vectors land, and recognise scaling, rotation, reflection and shear.
- LabM2-L1: Matrix Transformation Playground
Move a robot outline with matrices: build the matrix that maps one shape onto another, and catch the order-of-composition and singular-matrix traps.
Get notified when the next lesson and new labs are released.
One email per new lesson or lab.