Determinants of 2×2 and 3×3 Matrices
The determinant of a square matrix is a single number that tells you whether the matrix is invertible and how much the matrix scales area (in 2D) or volume (in 3D). Its sign says whether the matrix keeps or flips orientation, which is exactly how a graphics card decides which side of a triangle faces the camera.
The 2×2 determinant
Example: . We met in the inverse formula (Matrix Inverses): is invertible exactly when .
The 3×3 determinant
Each term is an entry of the first row times the 2×2 determinant left after deleting that entry's row and column, with signs . This is the first instance of cofactor expansion (Minors and Cofactors).
Rule of Sarrus (3×3 only): copy the first two columns to the right, add the three "downhill" diagonal products and subtract the three "uphill" ones:
Warning: Sarrus' diagonal trick does not extend to 4×4 and larger matrices. Use cofactor expansion or row reduction there.
from sympy import Matrix, symbols
def det3(M):
a, b, c = M[0]; d, e, f = M[1]; g, h, i = M[2]
return a * (e * i - f * h) - b * (d * i - f * g) + c * (d * h - e * g)
A = [[2, -1, 3], [0, 4, 1], [5, 2, -2]]
print("by formula:", det3(A))
print("SymPy :", Matrix(A).det())
a, b, c, d = symbols("a b c d")
print("symbolic 2x2:", Matrix([[a, b], [c, d]]).det())by formula: -85
SymPy : -85
symbolic 2x2: a*d - b*c
Area, volume and orientation
2D. The columns and of span a parallelogram, the image of the unit square (Geometric Interpretation). Its area is . So a linear map with matrix multiplies every area by .
The sign records orientation: if turning from to is counter-clockwise (like to ), if it is clockwise, i.e. includes a reflection. means and are parallel and the "parallelogram" is flat.
3D. The three columns span a parallelepiped with volume , and the sign tells whether they form a right-handed () or left-handed () triple. In fact , the scalar triple product (The Cross Product).
| Meaning for the map | |
|---|---|
| 2 | doubles areas, keeps orientation |
| −1 | keeps areas, flips orientation (a reflection) |
| 1 | area-preserving and orientation-preserving (rotations, shears) |
| 0 | collapses the plane to a line or a point; not invertible |
In software: triangle winding and back-face culling
The signed area of a triangle with vertices , , in the plane is
Positive means the vertices go round counter-clockwise, negative clockwise. Meshes list each triangle's vertices in a fixed order (counter-clockwise when seen from outside, by OpenGL's default convention). After projection to the screen, a triangle that now appears clockwise is facing away from the camera, and the GPU skips it: back-face culling, which removes about half of a closed mesh's triangles before shading. Mirroring a model (a scale with ) flips every winding, which is why engines must swap the culling mode for mirrored objects.
import numpy as np
def signed_area(p0, p1, p2):
M = np.column_stack([np.subtract(p1, p0), np.subtract(p2, p0)])
return 0.5 * float(np.linalg.det(M))
tri = [(0, 0), (4, 0), (1, 3)]
print("signed area:", round(signed_area(*tri), 6), "-> counter-clockwise (front-facing)")
print("reversed :", round(signed_area(*tri[::-1]), 6), "-> clockwise (culled)")
mirror = np.array([[-1, 0], [0, 1]]) # det = -1
mirrored = [tuple(mirror @ p) for p in tri]
print("after mirroring:", round(signed_area(*mirrored), 6))
shear = np.array([[1, 2], [0, 1]]) # det = 1
sheared = [tuple(shear @ p) for p in tri]
print("after shear (det 1):", round(signed_area(*sheared), 6))signed area: 6.0 -> counter-clockwise (front-facing)
reversed : -6.0 -> clockwise (culled)
after mirroring: -6.0
after shear (det 1): 6.0
The same determinant test decides "is point left of the line ?", the core predicate of convex hull algorithms, polygon triangulation and many collision tests (Ray–Triangle).
Exercises
Exercise 1: Compute
Find and .
Answer
26 and 21.
Solution
from sympy import Matrix
print(Matrix([[5, -2], [3, 4]]).det(), Matrix([[1, 2, 0], [-1, 3, 4], [2, 0, 1]]).det())26 21
Exercise 2: When is it singular?
For which is ?
Answer
or .
Solution
.
Exercise 3: Area of a triangle
Find the area of the triangle with vertices , , , and say whether this vertex order is clockwise or counter-clockwise.
Answer
Area 9; counter-clockwise.
Solution
Exercise 4: Volume of a parallelepiped
Find the volume of the parallelepiped spanned by , , , and check it equals in NumPy.
Answer
Volume 13 (the determinant is −13: a left-handed triple).
Solution
import numpy as np
u, v, w = np.array([1, 0, 2]), np.array([0, 3, 1]), np.array([2, 1, 0])
print(round(float(np.linalg.det(np.column_stack([u, v, w]))), 6), int(u @ np.cross(v, w)))-13.0 -13
Test yourself
What is ?
- 13
- −13
- 1
- −1
A 2×2 matrix has determinant −3. What does it do to a shape of area 2?
- Maps it to area −6
- Maps it to area 6 and keeps orientation
- Maps it to area 6 and flips orientation
- Maps it to area
Why must a game engine change the culling mode when it renders a mirrored copy of a model (scale −1 on one axis)?
- Mirroring doubles the triangle count
- The determinant of the transform is negative, so every triangle's winding flips
- Mirroring breaks the depth buffer
- It does not need to