Matrices and Determinants
A matrix is a rectangular grid of numbers. Multiplying a vector by a matrix moves it to a new place, so a matrix can describe a rotation, a scaling, a shear or a combination of them. The determinant measures how much a matrix stretches area or volume, and whether it can be undone.
Matrices and matrix-vector products
An matrix has rows and columns. To multiply a matrix by a vector, take the dot product of each row with the vector:
There is a second way to read the same product, and it explains what a matrix does: the result is . The columns are where the basis vectors land. The matrix sends to its first column and to its second, and every other vector follows along because the map is linear.
| Transform (2D) | Matrix | Columns say |
|---|---|---|
| Scale by , | , | |
| Rotate by (counter-clockwise) | ||
| Shear in by | ||
| Reflect across the x-axis |
Composing transforms: matrix multiplication
Applying and then to a vector is the same as applying the single matrix . Entry of is the dot product of row of with column of . Matrix multiplication is associative but not commutative: in general , just as "rotate then scale" differs from "scale then rotate".
import numpy as np
theta = np.radians(90)
R = np.array([[np.cos(theta), -np.sin(theta)],
[np.sin(theta), np.cos(theta)]]).round(12)
S = np.array([[2.0, 0.0],
[0.0, 0.5]])
p = np.array([1.0, 1.0])
print("rotate p :", R @ p)
print("scale then rotate:", (R @ S) @ p)
print("rotate then scale:", (S @ R) @ p)
print("R @ S =\n", R @ S)
print("S @ R =\n", S @ R)rotate p : [-1. 1.]
scale then rotate: [-0.5 2. ]
rotate then scale: [-2. 0.5]
R @ S =
[[ 0. -0.5]
[ 2. 0. ]]
S @ R =
[[ 0. -2. ]
[ 0.5 0. ]]
In R @ S @ p the matrix nearest the vector acts first. Game engines build a model matrix from translation, rotation and scale (TRS) in exactly this way; see Matrices and TRS. Translation is not linear, so engines use (2D) or (3D) homogeneous matrices to include it.
The determinant
For a matrix:
For a matrix, expand along the first row (cofactor expansion):
The determinant has a clean geometric meaning:
| What the transform does | |
|---|---|
| factor by which it scales areas (2D) or volumes (3D) | |
| flips orientation (a mirror image) | |
| squashes space flat (onto a line or plane); has no inverse | |
| scale factors multiply when you compose |
The determinant of the rows , , equals the scalar triple product , the signed volume of the box they span. That links this lesson to the cross product.
import numpy as np
A = np.array([[3.0, 1.0],
[1.0, 2.0]])
square = np.array([[0, 1, 1, 0],
[0, 0, 1, 1]], dtype=float) # unit square corners as columns
print("det A:", round(np.linalg.det(A), 6))
print("image of the unit square:\n", A @ square)
M = np.array([[2.0, 0.0, 1.0],
[1.0, 3.0, 0.0],
[0.0, 1.0, 4.0]])
print("det M :", round(np.linalg.det(M), 6))
print("a . (b x c) :", np.dot(M[0], np.cross(M[1], M[2])))
print("det of a mirror:", np.linalg.det(np.diag([1.0, -1.0])))det A: 5.0
image of the unit square:
[[0. 3. 4. 1.]
[0. 1. 3. 2.]]
det M : 25.0
a . (b x c) : 25.0
det of a mirror: -1.0
The unit square (area 1) becomes the parallelogram with corners (0, 0), (3, 1), (4, 3), (1, 2), whose area is 5.
Inverses and linear systems
If , has an inverse with (the identity). For :
A system of linear equations is one matrix equation , solved by . In code, use np.linalg.solve, which is faster and more accurate than forming the inverse.
import numpy as np
# 2x + y = 7 and x + 3y = 11
A = np.array([[2.0, 1.0],
[1.0, 3.0]])
b = np.array([7.0, 11.0])
print("inverse:\n", np.linalg.inv(A))
print("solve :", np.linalg.solve(A, b))
print("check :", A @ np.linalg.solve(A, b))inverse:
[[ 0.6 -0.2]
[-0.2 0.4]]
solve : [2. 3.]
check : [ 7. 11.]
In software
- Graphics pipelines move every vertex through model, view and projection matrices; the camera "un-transform" uses the inverse.
- Neural networks: a dense layer computes , a matrix-vector product, millions of times per second on a GPU.
- Calculus ahead: the Jacobian is the matrix of a function's best local linear approximation, and its determinant is the area scale factor when you change variables in a double integral.
NumPy's @ operator, np.linalg.det, inv and solve are covered in NumPy Linear Algebra. A full course on these ideas is planned under Linear Algebra.
Exercises
Exercise 1: Multiply by hand
Compute and for and .
Answer
and , so .
Solution
Exercise 2: Area after a transform
The matrix is applied to a triangle of area 1.5. What is the area of the image?
Answer
Solution
, so every area is multiplied by : .
Exercise 3: A determinant
Find by cofactor expansion along the first row, and say whether the matrix is invertible.
Answer
−15, so it is invertible (and it flips orientation).
Solution
import numpy as np
print(round(np.linalg.det(np.array([[1, 2, 0], [3, -1, 2], [0, 4, 1]])), 6))-15.0
Exercise 4: Solve a system (code)
Solve , , with NumPy and check the answer by substitution.
Answer
, , .
Solution
import numpy as np
A = np.array([[1.0, 1.0, 1.0],
[0.0, 2.0, 5.0],
[2.0, 5.0, -1.0]])
b = np.array([6.0, -4.0, 27.0])
x = np.linalg.solve(A, b)
print(np.round(x, 10), A @ x)[ 5. 3. -2.] [ 6. -4. 27.]
Substitution: , , .
Test yourself
What is ?
- 24
- 0
- −24
- 12
The first column of a matrix tells you…
- where the vector ends up
- the determinant
- where ends up
- the rotation angle
Which statement about matrix multiplication is true?
- always
- means: apply first, then
- Only square matrices can be multiplied