Determinants and inverses
Multiply matrices, track dimensions and interpret transformations, inverses, eigenvectors and SVD.
THE IDEA
Determinant: how much the area grows
Push the unit square through a matrix and it becomes a parallelogram. The determinant is the signed factor by which areas change: for [[a, b], [c, d]] it is ad − bc. Determinant 2 doubles every area; 0.5 halves it. A negative determinant means the plane has been flipped over, like a mirror image. For A = [[1, 2], [3, 4]]: 1·4 − 2·3 = -2, so areas double and the plane is flipped.
The determinant is positive: orientation is kept. Areas are scaled by 2.
Determinant zero: the collapse
If the determinant is 0, the whole plane is squashed onto a line (or a point). Many different inputs now land on the same output, so there is no way to tell which one you started with. A transformation that loses information has no inverse. The matrix [[1, 2], [2, 4]] has determinant 1·4 − 2·2 = 0: its second row is twice its first, the columns lie on one line, and the unit square collapses onto it.
Predict first. Is [[1, 2], [2, 4]] invertible?
The inverse undoes the transformation
When the determinant is not 0, a matrix has an inverse A⁻¹ with A⁻¹A = A A⁻¹ = I. For 2 by 2, swap the diagonal, negate the other two, divide by the determinant: A⁻¹ = (1/(ad − bc)) [[d, −b], [−c, a]]. For [[1, 2], [3, 4]] that gives [[-2, 1], [1.5, -0.5]]. Tick "undo with A⁻¹" in the picture above to watch the F return home.
Inverses are the theory; in practice you rarely compute them. To solve A x = b, call np.linalg.solve(A, b), which is usually faster and more accurate than inv(A) @ b.
Common mistakes
Almost singular is nearly as bad. [[1, 1], [1, 1.0001]] has determinant 1·1.0001 − 1·1 = 0.0001, not 0, so it is invertible on paper. But its inverse is about [[10001, -10000], [-10000, 10000]]: tiny changes in the input produce huge changes in the output, and rounding noise gets amplified. A near-zero determinant signals numerical sensitivity.
import numpy as np
M = np.array([[0.1, 0.2],
[0.3, 0.6]]) # second row is exactly 3 x the first, on paper
np.linalg.det(M) # a tiny nonzero number (about 1e-18 here), not 0.0
np.linalg.matrix_rank(M) # 1: uses a tolerance, so it sees the collapse
np.linalg.inv(np.array([[1., 2.], [2., 4.]])) # LinAlgError: Singular matrixCompare with a tolerance that scales with the entries, or ask for the rank. And remember that a determinant on its own is not a good measure of "nearly singular": scaling every entry of a 2 by 2 matrix by 1e-6 gives determinant 1e-12 but the matrix is perfectly fine, just small.
Chapter context
A matrix represents a linear map once input and output bases are chosen. Multiplication composes maps. Tracking shapes is an algebraic requirement and also a practical way to detect implementation mistakes before running code.
TRY IT YOURSELF
Part 1. Do it by hand
Watch the determinant, the area ratio and the inverse as you edit. When no inverse exists the readout says so.
The determinant is positive: orientation is kept. Areas are scaled by 1.
- To do: Make a matrix with determinant exactly -2: it flips orientation and doubles the area
- To do: Squash the unit square onto the line y = 2x: both columns on that line, not both zero
- To do: Find the matrix whose inverse is [[1, -1], [0, 1]]
Part 2. Do it in code
- Write is_invertible(A, tol=1e-10): False for a non-square matrix, and False when the matrix is numerically singular. Compare with a tolerance, never with == 0.
- Write inverse2(A): the 2 by 2 inverse from the formula (1/(ad-bc)) [[d,-b],[-c,a]]. Raise ValueError for a singular matrix.
- Write solve_system(A, b): solve A x = b without forming the inverse (np.linalg.solve), raising ValueError for a singular A.
Stuck, or something not working? You can come back to it any time.
CHECK YOUR UNDERSTANDING
Is [[1,2],[2,4]] invertible?
Chapter reference: math, worked example & code
This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.
Math and notation
U and V contain orthonormal singular directions; diagonal entries of Sigma are singular values. Eigenvalues and singular values are different concepts.
For an invertible 2x2 matrix, the inverse is
This formula requires a nonzero determinant. A near-zero determinant can signal numerical sensitivity, so exact invertibility alone does not guarantee a well-conditioned computation.
Worked example
Multiply A=[[1,2],[3,4]] by B=[[2,0],[1,2]]. Row-column products give C=[[4,4],[10,8]]. The determinant of A is 1×4−2×3=−2, so A is invertible. In general AB is not equal to BA; their shapes may even differ.
Reference code
The following example uses the Python standard library and does not call a remote service.
A, B = [[1,2],[3,4]], [[2,0],[1,2]]
C = [[sum(A[i][k]*B[k][j] for k in range(2)) for j in range(2)] for i in range(2)]
assert C == [[4,4],[10,8]]
assert A[0][0]*A[1][1] - A[0][1]*A[1][0] == -2
Python examples are provided for study and local execution; this page does not execute Python.
Common mistakes
AB usually differs from BA. A transpose is not generally an inverse. A negative eigenvalue is possible, while singular values are nonnegative.