The Matrix of a Linear Transformation
Every linear transformation is multiplication by a unique matrix, its standard matrix, and the recipe for finding it is short: the columns are the images of the standard basis vectors. Composition of transformations then becomes matrix multiplication.
The recipe
Let be the standard basis of . Any is , so by linearity
The last step is the column view of a matrix–vector product (Matrix Multiplication). So
satisfies for every , and it is the only such matrix (two matrices that agree on every have the same columns).
Example. The reflection of across the line swaps coordinates: and , so .
A gallery of standard matrices ()
| Transformation |
|
|
Matrix |
| Rotation by counter-clockwise |
|
|
|
| Reflection across the -axis |
|
|
|
| Scaling by () and () |
|
|
|
| Horizontal shear by |
|
|
|
| Projection onto the -axis |
|
|
|
| Projection onto the line through unit |
|
|
|
The projection entry comes from (The Dot Product): goes to .
python · try it yourselffrom sympy import Matrix, Rational, sqrt, cos, sin, pi, simplify
def standard_matrix(T, n):
cols = [T(Matrix.eye(n).col(j)) for j in range(n)]
return Matrix.hstack(*cols)
# projection onto the line spanned by [3, 4] (unit vector [3/5, 4/5])
u = Matrix([Rational(3, 5), Rational(4, 5)])
proj = lambda x: (u.dot(x)) * u
P = standard_matrix(proj, 2)
print("projection matrix:", P.tolist())
print("P == u u^T:", P == u * u.T, " P^2 == P:", P * P == P)
theta = pi / 6
R = Matrix([[cos(theta), -sin(theta)], [sin(theta), cos(theta)]])
print("rotation by 30 degrees:", R.tolist())
projection matrix: [[9/25, 12/25], [12/25, 16/25]]
P == u u^T: True P^2 == P: True
rotation by 30 degrees: [[sqrt(3)/2, -1/2], [1/2, sqrt(3)/2]]
says "projecting twice is the same as projecting once".
Composition is multiplication
If has matrix and has matrix , then , so has matrix . The rightmost matrix acts first. Rotating by and then by is rotating by , and multiplying the rotation matrices proves the angle-addition formulas of trigonometry:
python · try it yourselffrom sympy import symbols, cos, sin, Matrix, simplify, expand_trig
a, b = symbols("alpha beta")
rot = lambda t: Matrix([[cos(t), -sin(t)], [sin(t), cos(t)]])
product = rot(b) * rot(a)
print("entry (1,1):", product[0, 0])
print("equals cos(alpha+beta):", simplify(product[0, 0] - cos(a + b)) == 0)
print("whole matrix equals rot(alpha+beta):", simplify(product - rot(a + b)) == Matrix.zeros(2, 2))
entry (1,1): -sin(alpha)*sin(beta) + cos(alpha)*cos(beta)
equals cos(alpha+beta): True
whole matrix equals rot(alpha+beta): True
When is known on another basis
Sometimes you know on a basis that is not the standard one. With and , the equation gives
For example, a reflection is easiest to describe on a basis adapted to its mirror line: across the line , the direction is fixed and the perpendicular flips. So and , and (see Exercise 3).
In software
- Vertex shaders multiply every vertex by a matrix, and the columns of that matrix are literally where the object's local , , axes end up in the world. Reading a transform matrix column by column tells you the object's orientation and scale at a glance (Matrices and Multiplication).
- Row vs column vectors. Math and OpenGL/GLM write with column vectors; DirectX/HLSL and many older engines use row vectors, , with . Products then compose left to right instead of right to left. Mixing conventions transposes your matrices, so check which one a library uses (Handedness, Up Axes and Units).
- Learned linear maps. A neural network layer is a learned standard matrix: column of (in the convention) is the output pattern produced by the -th input feature on its own.
Exercises
Exercise 1: Write the matrix
Find the standard matrix of .
Answer
.
Solution
Exercise 2: Compose two maps
rotates by 90° counter-clockwise, reflects across the -axis. Find the matrices of and . Are they equal?
Answer
, ; they are not equal.
Solution
is the reflection across and the reflection across .
Exercise 3: Reflection across
Using with and , compute the reflection matrix and check that applying it twice gives .
Answer
, and .
Solution
python · try it yourselffrom sympy import Matrix, eye
B = Matrix([[1, -2], [2, 1]]); C = Matrix([[1, 2], [2, -1]])
A = C * B.inv()
print(A.tolist(), A * A == eye(2))
[[-3/5, 4/5], [4/5, 3/5]] True
Exercise 4: A projection in code
Using standard_matrix from the lesson (or by hand), find the matrix of the projection of onto the line spanned by .
Answer
Every entry is : .
Solution
The unit vector is , and has every entry .
python · try it yourselffrom sympy import Matrix
v = Matrix([1, 1, 1])
A = v * v.T / v.dot(v)
print(A.tolist(), list(A * Matrix([3, 0, 6])))
[[1/3, 1/3, 1/3], [1/3, 1/3, 1/3], [1/3, 1/3, 1/3]] [3, 3, 3]
Test yourself
sends to and to . What is its standard matrix?
has matrix and has matrix . What is the matrix of 'first , then '?
What size is the standard matrix of a linear map ?
- 4×2
- 2×4
- 4×4
- 2×2