Goal
Understand matrix multiplication as composition of transformations. The product BA is not a recipe to memorize. It is the combined action you get by first applying A and then applying B.
Motivation
Machine learning models are usually built from layers.
Each layer transforms a vector.
The next layer transforms the output of the previous one.
So a stack of layers is really a composition of transformations, and matrix multiplication is the algebra that records that composition.
Prerequisites
-
Lesson 2: Matrices as Linear Transformations.
-
Comfort with basis vectors and matrix-vector multiplication.
Notation / Symbol Table
| Symbol | Meaning |
|---|---|
A | The first linear transformation in a composition. |
B | The second linear transformation in a composition. |
BA | The composed transformation: apply A first, then B. |
e_1, e_2 | Standard basis vectors. |
x | An input vector. |
Core Ideas
Matrix multiplication is composition
If A and B are linear transformations, then:
That means the order is read right-to-left:
-
first
A; -
then
B.
This is exactly like function composition.
The order matters
In general,
That is because doing one transformation before another can change the result dramatically.
Stretch-then-rotate is usually not the same as rotate-then-stretch.
Columns of the product come from transformed basis vectors
To understand the product BA, look at what it does to the basis vectors.
If the columns of A are A e_1 and A e_2, then the columns of BA are:
So matrix multiplication is a bookkeeping device for the transformed basis.
Composition is the real story
The row-by-column formula is useful, but it is not the meaning.
The meaning is:
first transform the space with one map, then transform the result with another.
That is the geometric story behind the arithmetic.
Worked Example
Let:
so A stretches the x-axis, and let:
so B rotates by 90^\circ counterclockwise.
Take:
First apply A:
Then apply B:
Now compute the product directly by tracking basis vectors.
The columns of A are:
Apply B to each column:
So:
If you reverse the order, you get a different matrix:
That difference is the whole reason composition matters.
Why This Shows Up In ML
Neural networks are stacks of transformations.
A layer takes an input vector, transforms it, and passes the result onward.
When you chain layers together, you are composing maps.
Matrix multiplication is the algebra that records that composition efficiently, which is why it appears everywhere from simple linear models to deep networks.
Exercises
-
Explain in your own words why
BAmeans “applyAfirst, thenB.” -
Let
Find the columns of BA without using row-by-row multiplication.
-
Explain why knowing where a linear transformation sends the basis vectors is enough to know what it does to every vector.
-
Decide whether
ABequalsBAfor the matrices in Exercise 2. -
Describe a machine learning layer stack as a composition of transformations.
Implementation Exercise
Write a TypeScript helper that composes two 2 x 2 matrices:
type Matrix2x2 = [[number, number], [number, number]];
export function multiply2x2(left: Matrix2x2, right: Matrix2x2): Matrix2x2
Then test it by comparing:
-
multiply2x2(B, A)with applyingAand thenBto a vector; -
multiply2x2(A, B)with applyingBand thenA.
Reflection Questions
-
Why does the order of matrix multiplication matter?
-
What is the geometric meaning of a matrix product?
-
How can you tell from a product whether a transformation is likely to preserve shape?
-
Why does composition feel so natural in programming?
Completed Exercises
Handwritten work submitted after completing this lesson.

