What is it?
A matrix is a linear transformation of space written down. Multiplying two matrices is composing two transformations: apply , then apply .
That is the definition worth carrying around. The arithmetic rule - take the dot product of each row of with each column of - is a consequence, not the meaning.
Why does it exist?
Start with what a matrix does to a vector. The key fact: the columns of a matrix are where the basis vectors land.
Take
Column one says lands on . Column two says lands on . Because the transformation is linear, that is all you need - every other vector goes where its combination of basis vectors goes:
Matrix-vector multiplication is a weighted sum of the columns. Once you see it that way, the product is forced: to find where sends the basis vectors, take the columns of (where sends them) and push each one through .
import numpy as np
A = np.array([[2, 1], [0, 3]])
B = np.array([[0, -1], [1, 0]]) # rotate 90° counter-clockwise
# columns of AB are A applied to each column of B
print(A @ B)
print(np.column_stack([A @ B[:, 0], A @ B[:, 1]])) # identical
How does it work?
Three properties stop being facts to memorise and become obvious:
Order matters. , because “rotate then stretch” is not “stretch then rotate.”
print(A @ B) # [[ 1 -2]
# [ 3 0]]
print(B @ A) # [[ 0 -3]
# [ 2 1]]
Dimensions must line up. An times an works because the second transformation must output vectors that live in the space the first one takes as input. The inner dimensions match or the composition is meaningless.
The determinant multiplies. , because the determinant is the factor by which area (or volume) is scaled, and doing two transformations scales area twice.
print(np.linalg.det(A @ B), np.linalg.det(A) * np.linalg.det(B)) # 6.0 6.0
A determinant of zero means the transformation squashes space into a lower dimension - and squashing loses information, which is exactly why such a matrix has no inverse.
Where is it actually used?
- Neural networks. A layer is a matrix multiply plus a non-linearity. Stack two linear layers with nothing between them and you get - a single linear transformation. That product identity is the reason activation functions have to exist.
- Graphics and robotics. Chains of rotations, translations and projections, composed by multiplying matrices in the order the operations happen.
- Portfolio mathematics. - a quadratic form, which is a transformation applied and then projected back onto the weights.
- Everything about performance. Matrix multiply is naively and is what GPUs are physically built to do. Understanding it as composition is also what tells you when you can reassociate a product to make it cheaper: computing versus gives the same answer and can differ by orders of magnitude in cost.
Going deeper
- Watch 3Blue1Brown episodes 3 and 4 with a pen. They are the best explanation of this that exists.
- Then write the transformation by hand: for a matrix, plot where a grid of points goes. Seeing the grid deform is the moment the row-column rule stops feeling arbitrary.