Rank and the four subspaces

Rank counts the dimensions a matrix keeps. Everything it throws away lives in the null space.

What you'll learn
  • Read rank as the number of dimensions that survive a transformation
  • Predict the rank of products, sums, and AᵀA without computing it
  • Place any vector in one of the four fundamental subspaces

The rank of a matrix is the number of independent directions in its output. Geometrically, it is how many dimensions survive the transformation. A 3×3 matrix of rank 3 keeps space three-dimensional; rank 2 flattens it onto a plane; rank 1 crushes it onto a line.

A cloud of points under a 3×3 matrix. Switch the rank: at rank 2 the third column is exactly the sum of the first two, so every output lands in their plane, and the determinant falls to zero.

Numerically, rank is one function call:

A = np.random.randint(1, 5, (5, 4))
np.linalg.matrix_rank(A)          # at most min(5, 4) = 4

To make a rank-deficient matrix on purpose, copy one column into another. The copy adds no new direction, so the rank drops by one:

A = np.round(10 * np.random.randn(4, 4))
A[:, -1] = A[:, -2]               # last column duplicates the one before
np.linalg.matrix_rank(A)          # 3, not 4

The rules the notebook verifies

These are worth knowing by heart, because they let you reason about rank without computing anything:

Expression Rank
\(cA\), with \(c \neq 0\) same as \(A\) — scaling never removes a direction
\(AB\) \(\le \min(\operatorname{rank}A, \operatorname{rank}B)\) — a chain is only as wide as its narrowest link
\(A + B\) \(\le \operatorname{rank}A + \operatorname{rank}B\)
\(A^{\mathsf{T}}A\) and \(AA^{\mathsf{T}}\) exactly \(\operatorname{rank}A\)

The product rule explains the challenge of building a 10×10 matrix of rank 4: a \(10\times4\) times a \(4\times10\) can never have rank above 4, however random the entries.

A = np.random.randn(10, 4)
B = np.random.randn(4, 10)
C = A @ B                          # 10 x 10, but rank 4

Shifting a matrix to full rank

Adding a small multiple of the identity — \(A + \lambda I\) — nudges every eigenvalue by \(\lambda\), which is usually enough to lift a rank-deficient matrix to full rank. This trick, often called regularisation, is the reason ridge regression and many numerical solvers behave when the plain problem would not.

The four fundamental subspaces

An \(m \times n\) matrix of rank \(r\) splits input and output space into four pieces:

The four fundamental subspaces. In the input space R^n, the row space of dimension r is perpendicular to the null space of dimension n minus r. In the output space R^m, the column space of dimension r is perpendicular to the left null space of dimension m minus r. A maps the row space one-to-one onto the column space and sends the null space to zero. The four fundamental subspaces. In the input space R^n, the row space of dimension r is perpendicular to the null space of dimension n minus r. In the output space R^m, the column space of dimension r is perpendicular to the left null space of dimension m minus r. A maps the row space one-to-one onto the column space and sends the null space to zero.
Inputs split into what A keeps (the row space) and what it destroys (the null space). Outputs split into what A can reach (the column space) and what it never can (the left null space).
  • Column space \(C(A)\): every output \(A\mathbf{x}\) can produce. Its dimension is the rank.
  • Null space \(N(A)\): every input that \(A\) sends to zero. These are the directions crushed in the animation, and there are \(n - r\) of them.
  • Row space and left null space: the same two ideas for \(A^{\mathsf{T}}\).

The rank–nullity theorem is the bookkeeping: \(r + (n - r) = n\). Every input dimension is either kept or destroyed.

Is a vector in the span?

The notebook’s final challenge asks whether \(\mathbf{v}\) lies in the span of a set of columns \(S\). The test is rank: append \(\mathbf{v}\) as a new column. If the rank does not go up, \(\mathbf{v}\) added no new direction — it was already in the span.

v = np.array([[1, 2, 3, 4]]).T
S = np.vstack([[4, 3, 6, 2], [0, 4, 0, 1]]).T
T = np.vstack([[1, 2, 2, 2], [0, 0, 1, 2]]).T

np.linalg.matrix_rank(np.concatenate((S, v), axis=1))  # 3: rank went up, v is not in span(S)
np.linalg.matrix_rank(np.concatenate((T, v), axis=1))  # 2: unchanged, v is in span(T)

Try it yourself. Build a 6×6 matrix of rank exactly 2 using one matrix product, and check it with matrix_rank. Then use scipy.linalg.null_space to find its null space and confirm it has dimension 4, as rank–nullity predicts.

View source on GitHub →