Math for AILoading progress…

Determinants and inverses

Multiply matrices, track dimensions and interpret transformations, inverses, eigenvectors and SVD.

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.

ij
A =
det A = a·d − b·c = 2·1 − 1·0 = 2
area of the image of the unit square (shoelace) = 2
F figure: area 2.65 → 5.3 (ratio 2)

The determinant is positive: orientation is kept. Areas are scaled by 2.

A⁻¹ = (1/det) [[d, −b], [−c, a]] = [[0.5, -0.5], [0, 1]]
A⁻¹A = [[1, 0], [0, 1]]
Edit the matrix. The F figure shows the flip when the determinant is negative. Try the squash preset, then tick the undo box.

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 matrix

Compare 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.

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

Cij=∑kAikBkj,(AB)T=BTAT.C_{ij}=\sum_k A_{ik}B_{kj},\quad (AB)^T=B^TA^T. det⁡[abcd]=ad−bc,Av=λv.\det\begin{bmatrix}a&b\\c&d\end{bmatrix}=ad-bc,\quad Av=\lambda v. A=UΣVT.A=U\Sigma V^T.

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

[abcd]−1=1ad−bc[d−b−ca].\begin{bmatrix}a&b\\c&d\end{bmatrix}^{-1}=\frac1{ad-bc}\begin{bmatrix}d&-b\\-c&a\end{bmatrix}.

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.