TactivergeLite

Lite / Independent Engineering Research / 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 · Full interactive version

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.

Predict

R rotates by 90°, S is the shear [[1, 1], [0, 1]]. Is SR (rotate first, then shear) equal to RS?

  1. Yes, always
  2. No, they differ
  3. Only for 90° rotations
Guess first, then reveal

SR = [[1, −1], [1, 0]] but RS = [[0, −1], [1, 1]]. Matrix multiplication is associative but not commutative.

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.

What the interactive shows (text version)

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).

You can also open the interactive on the full site when you have a faster connection.

Watch (optional)

Watch for: 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.

“Matrix multiplication as composition | Chapter 4, Essence of linear algebra” by 3Blue1Brown (opens YouTube; streaming video uses a lot of data)

The video is optional. Everything you need is in this lesson's text.

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

Intuitive

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.

Technical

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.

Applied

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

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

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)ᵀ?

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.

Reflect

Where in code you have written could a reversed product hide?

Write your answer on paper or in your own notes. Lite never stores what you write.

← Gaussian elimination, back substitution and LUInverses: undoing a linear map →