School/✦ Research Engineer/Week 2
Matrix multiplication as composition
Rotate then shear, or shear then rotate: the same two moves, different results. Why does order matter for matrices?
About 40 min · 3 goals · Checked by automated tests
1.Why should I care?
Chaining coordinate frames along a robot arm, cascading filters, combining a sensor model with a calibration: all are matrix products. Getting the order wrong is one of the most common bugs in robotics code.
- ◇ Compute matrix products by the row-times-column rule and check dimensions.
- ◇ Interpret BA as 'apply A, then B' and show AB ≠ BA with an example.
- ◇ Use transposes and the rule (AB)ᵀ = BᵀAᵀ.
2.Watch
→ Watch how the product's columns are found by applying the second map to the first map's columns.
Explains why multiplying matrices means doing one transformation after another, and why the order matters.
10 min 3 s · captions on YouTube · source
Nothing loads from YouTube until you press play.
The video is optional: everything you need is also in the lesson text and the interactive.
3.Predict
R rotates by 90°, S is the shear [[1, 1], [0, 1]]. Is SR (rotate first, then shear) equal to RS?
Guess first. Being wrong here is how the surprise works.
4.Discover
Load 'Shear' into A and choose 'Then apply B: Rotate 30°'. Note where î and ĵ land. Now load 'Rotate 30°' into A and apply B = 'Shear'. Same two maps, different picture. Check that det(BA) = det(B)·det(A) both times.
Loading the interactive…
Can't use the interactive? Read a description instead
The transformation grid shows a grid and unit square mapped by the combined matrix B·A, with î and ĵ drawn as labelled arrows. Text gives the combined matrix entries and det A, det B and det(BA).
5.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1 · a fresh version every time
A = [[-4, 1], [5, 5]] and B = [[-2, 2], [3, 0]]. What is the entry in row 2, column 1 of the product AB?
6.Understand
The rule
If A is m×n and B is n×p, then AB is m×p with (AB)ᵢⱼ = Σₖ aᵢₖbₖⱼ: row i of A dotted with column j of B. Inner dimensions must match. Equivalently, column j of AB is A times column j of B.
Composition
(BA)x = B(Ax): first A acts, then B. So BA means 'A then B', read right to left, just like function composition f(g(x)). Associativity, (CB)A = C(BA), lets you pre-multiply a chain of frames into one matrix once.
Order and transposes
In general AB ≠ BA; commuting is the exception (for example two rotations about the same axis, or diagonal matrices). The transpose swaps rows and columns, and reverses products: (AB)ᵀ = BᵀAᵀ. Similarly (AB)⁻¹ = B⁻¹A⁻¹: to undo 'put on socks, then shoes', take off shoes first.
Explain it another way
A matrix is a move. Multiplying matrices is doing moves in sequence. Turning left then walking forward is not the same as walking forward then turning left.
Matrices represent linear maps relative to a basis; the product represents the composite map. Cost of an n×n product by the definition is n³ multiply–adds.
A camera pose in world coordinates is T_world←robot · T_robot←camera. Swap the order and the camera ends up somewhere physically meaningless.
Worked example 1
Compute AB for A = [[1, 2], [3, 4]], B = [[0, 1], [1, 0]], and BA.
AB = [[1·0 + 2·1, 1·1 + 2·0], [3·0 + 4·1, 3·1 + 4·0]] = [[2, 1], [4, 3]] (columns of A swapped). BA = [[3, 4], [1, 2]] (rows of A swapped). Different.
Common mix-ups
- AB = BA, like ordinary numbers.
✓ Matrix products usually do not commute; the order encodes which map happens first. - Matrix multiplication multiplies matching entries.
✓ That is the element-wise (Hadamard) product. The matrix product is rows times columns.
7.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2
To apply A first and then B to a vector x, you compute…
Practice 3
A is 3×2 and B is 2×4. What size is AB?
Practice 4
Which equals (AB)ᵀ?
Practice 5 · a fresh version every time
A = [[0, -4, 2], [1, 0, 0], [-3, -3, -3]] and B = [[0, -4, -2], [3, 4, 1], [5, -4, 0]]. What is the entry in row 3, column 1 of the product AB?
8.Apply it
Build a two-link planar arm transform in the Code Lab: T = Rot(θ₁)·Trans(L₁)·Rot(θ₂)·Trans(L₂) with 3×3 homogeneous matrices. Swap two factors and show the hand position changes.
9.Reflect
Concepts: Matrices and linear transformations