Where We Are
Chapter 009 showed that a matrix can act on a vector:
x→Ax.
A deep network does not stop after one transformation.
It repeatedly applies transformations:
x→Ax→B(Ax).
Writing nested expressions works for two layers. It becomes painful for twenty.
We need a way to combine transformations themselves.
Today: we discover matrix–matrix multiplication as function composition, many dot products, and “apply the right matrix first.”
Next: once matrices can represent transformations, we will study what makes a transformation linear and what geometry it preserves.
The Picture to Hold in Your Head
Matrix multiplication looks strange only if matrices are tables. If matrices are actions on space, multiplication means something natural: do one action, then another.
The product is the single matrix that has exactly the same final effect as the two-step journey. The order matters because the second transformation acts on a space that the first transformation has already changed. “Rotate then shear” and “shear then rotate” are different physical instructions, so their matrices must differ too.
Read products from right to left, just like nested functions: ABx means B touches the vector first, then A.
🎛️ Play First, then second and immediately play The other order. Do not compare only the final numbers; watch which directions the second operation inherits from the first.
Suppose we first stretch x-coordinates by 2:
A=[2001].
Then shear horizontally:
B=[1011].
Take
x=[12].
First apply A:
Ax=[22].
Then apply B:
B(Ax)=[1011][22]=[42].
Can one matrix C do the whole job?
We want
Cx=B(Ax)
for every vector x, not only this one.
2. What Would the Combined Matrix Need to Do?
From Chapter 009, a matrix is determined by where it sends the basis vectors.
So to discover the combined matrix, ask what the two-step process does to
e1=[10]
and
e2=[01].
Whatever those final vectors are, they must become the columns of C.
This gives us a route to matrix multiplication without memorizing a rule.
Start with
e1=[10].
Apply A:
Ae1=[20].
Now apply B:
B(Ae1)=[1011][20]=[20].
So the first column of the combined matrix is
[20].
Now
e2=[01].
Apply A:
Ae2=[01].
Apply B:
B(Ae2)=[11].
So the second column is
[11].
Therefore
C=[2011].
Check our original vector:
C[12]=[2(1)+1(2)0(1)+1(2)]=[42].
It matches the two-step process.
5. The Discovery: Matrix Multiplication
We write the combined transformation as
C=BA
so that
(BA)x=B(Ax).
This notation encodes order.
The matrix closest to the vector acts first.
That is exactly like function composition:
(g∘f)(x)=g(f(x)).
Matrix multiplication is transformation composition.
6. Why the Order Looks Backward
Suppose you read
BAx.
Human language may tempt you to say “B then A.”
But parentheses reveal the truth:
B(Ax).
A touches x first.
Then B acts on the result.
This is not an arbitrary convention. It follows from how function composition works.
7. Derive the Entry Formula
Let
A=[acbd],B=[egfh].
The first column of BA is B applied to the first column of A:
B[ac]=[ea+fcga+hc].
The second column is
B[bd]=[eb+fdgb+hd].
Therefore
BA=[ea+fcga+hceb+fdgb+hd]
Each entry is a row–column dot product.
8. The Row–Column Rule
To find entry (i,j) of BA:
- take row i of B,
- take column j of A,
- dot them.
Symbolically:
(BA)ij=k∑BikAkj.
The repeated index k is the inner dimension being summed over.
This formula looks abstract only until you remember:
one output entry = one dot product.
9. Shape Rule
Suppose
A∈Rm×n
and
B∈Rp×m.
Then
BA∈Rp×n.
Shape mnemonic:
(p × m)(m × n) = (p × n)
↑ ↑
must match
The inner dimensions disappear because they are summed over in dot products.
The outer dimensions survive.
10. Why Matrix Multiplication Is Not Commutative
For ordinary numbers,
2×3=3×2.
For matrices, usually
AB=BA.
Why?
Because order of transformations matters.
Stretch then rotate is generally not the same as rotate then stretch.
That is a geometric fact, not a symbolic annoyance.
11. Concrete Example: Order Matters
Let
S=[2001]
and a 90° rotation
R=[01−10].
Take
x=[11].
Stretch then rotate
Sx=[21]
then
R(Sx)=[−12].
Rotate then stretch
Rx=[−11]
then
S(Rx)=[−21].
Different answers.
Therefore
RS=SR.
12. A Tempting Wrong Idea: “Just Multiply Matching Entries”
Element-wise multiplication would give
A⊙B.
But composition requires that each output coordinate of one transformation be fed into every relevant input of the next.
That mixing is exactly what row–column dot products achieve.
A Tempting Wrong Idea
Element-wise multiplication combines corresponding entries. Matrix multiplication composes transformations. They solve different problems.
13. Associativity: Why Deep Composition Is Manageable
Matrix multiplication satisfies
(AB)C=A(BC).
So parentheses can move without changing the final transformation.
This matters computationally.
Suppose shapes are:
A:(1000×10),B:(10×1000),C:(1000×1).
Different parenthesizations can require very different amounts of work.
The mathematics is the same; the computational cost may not be.
This becomes important in optimized numerical computing.
14. Identity Matrix
The identity matrix does nothing:
I=[1001].
For every vector,
Ix=x.
And for compatible matrices,
IA=A,
AI=A.
It plays the same role as the number 1 in ordinary multiplication.
15. Inverse Matrix as “Undo”
If a matrix A has an inverse A−1, then
A−1A=I.
So if
y=Ax,
then
A−1y=x.
The inverse undoes the transformation.
Not every matrix has one. Later, rank will explain why.
16. Neural-Network Connection
Ignoring nonlinearities for a moment, imagine two dense layers:
h=W1x
and
y=W2h.
Substitute:
y=W2(W1x).
Therefore
y=(W2W1)x.
Two purely linear layers collapse into one linear layer.
That observation will become crucial when we ask why neural networks need nonlinear activation functions.
17. Batch Computation Preview
Suppose each column of X is one input vector.
Then
Y=WX
applies the same transformation to many examples at once.
Matrix–matrix multiplication therefore appears in two ways:
- compose transformations,
- process batches of vectors efficiently.
Modern deep-learning workloads are dominated by this operation.
18. Code From Scratch
def matmul(A, B):
rows_a = len(A)
cols_a = len(A[0])
rows_b = len(B)
cols_b = len(B[0])
if cols_a != rows_b:
raise ValueError("inner dimensions must match")
out = [[0.0 for _ in range(cols_b)] for _ in range(rows_a)]
for i in range(rows_a):
for j in range(cols_b):
total = 0.0
for k in range(cols_a):
total += A[i][k] * B[k][j]
out[i][j] = total
return out
The three loops have clear meanings:
i: output row,
j: output column,
k: dot-product accumulation.
NumPy compresses all of that into
C = A @ B
but the mathematics underneath is unchanged.
19. Break It
Shape mismatch
A (3×4) matrix cannot multiply a (5×2) matrix because 4 ≠ 5.
Wrong order
Even when both AB and BA exist, they usually differ.
Assuming every matrix has an inverse
A transformation that squashes a plane onto a line loses information. No inverse can recover what was destroyed.
Forgetting nonlinearities
Two linear neural-network layers collapse into one. Adding nonlinear activations prevents this collapse and gives deep networks their expressive power.
20. History Lens — Composition as Algebra
One of linear algebra’s great achievements is turning sequences of geometric operations into algebraic objects that can themselves be multiplied.
Instead of separately describing “rotate, then stretch, then shear,” we can multiply matrices and obtain a single transformation.
That ability to compose operations is why matrices became fundamental across geometry, mechanics, graphics, control theory and neural networks.
21. Distinctions That Matter
| Pair | Difference |
|---|
| matrix–vector vs matrix–matrix | transform one vector vs compose/apply across many vectors |
| matrix multiplication vs element-wise multiplication | composition via dot products vs matching-entry products |
| AB vs BA | usually different transformation order |
| identity vs inverse | identity does nothing; inverse undoes a specific transformation |
| associativity vs commutativity | parentheses may move; order usually may not |
22. What We Discovered
- Matrix multiplication composes transformations.
- In BAx, A acts first.
- Each matrix-product entry is a row–column dot product.
- Shape rule: (p×m)(m×n)=(p×n).
- Matrix multiplication is associative but generally not commutative.
- The identity matrix leaves vectors unchanged.
- Inverse matrices, when they exist, undo transformations.
- Multiple purely linear neural-network layers collapse into one matrix multiplication.
- Matrix–matrix multiplication also powers batch computation.
23. One-Minute Explanation
A matrix transforms vectors. If one matrix acts and then another acts, the two transformations can be combined by multiplying the matrices. The rightmost matrix acts first, just like nested functions. Each entry of the product is a dot product between a row from the left matrix and a column from the right matrix. Order matters because transformations do not generally commute: rotate-then-stretch is not the same as stretch-then-rotate. In neural networks, matrix multiplication composes layers and processes large batches efficiently.
24. Mastery Check
- Why does BAx mean apply A first?
- Derive a 2×2 matrix product by hand.
- Why are matrix-product entries row–column dot products?
- What shape results from
(7×3) @ (3×5)?
- Why is
(7×3) @ (4×5) invalid?
- Give a geometric explanation for AB=BA.
- What does the identity matrix do?
- What does an inverse matrix mean geometrically?
- Why can two linear neural-network layers collapse into one?
- Why does adding a nonlinearity change that conclusion?
🔭 Bridge to Chapter 011
We can now represent transformations and compose them.
But we have been using the word linear without fully asking what it permits and forbids.
What exactly makes a transformation linear, and what geometric structure must it preserve?
That is the next question.