TExcelTAD
Home › Linear Algebra & Matrix Theory › Column Space, Row Space and Null Space

Column Space, Row Space and Null Space

Every matrix AA comes with three subspaces: the column space (all outputs AxA\mathbf{x}), the row space (spanned by the rows) and the null space (all x\mathbf{x} with Ax=0A\mathbf{x} = \mathbf{0}). One row reduction gives bases for all three, and their dimensions are tied together by the rank-nullity theorem.

The three subspaces

For an m×nm \times n matrix AA:

Subspace Definition Lives in Basis from rref⁡(A)=R\operatorname{rref}(A) = R
column space col⁡(A)\operatorname{col}(A) span of the columns ={Ax:x∈Rn}= \{A\mathbf{x} : \mathbf{x} \in \mathbb{R}^n\} Rm\mathbb{R}^m the original columns of AA at the pivot positions
row space row⁡(A)\operatorname{row}(A) span of the rows Rn\mathbb{R}^n the non-zero rows of RR
null space null⁡(A)\operatorname{null}(A) {x:Ax=0}\{\mathbf{x} : A\mathbf{x} = \mathbf{0}\} Rn\mathbb{R}^n one vector per free variable (solve Rx=0R\mathbf{x} = \mathbf{0})

Why the recipes work:

  • Column space: AxA\mathbf{x} is a combination of the columns, and the pivot columns are a basis of their span (Basis and Dimension).
  • Row space: row operations replace rows by combinations of rows and can be undone, so they do not change the row space. The non-zero rows of RR span it and are independent (each has a leading 1 where the rows below have zeros).
  • Null space: Ax=0A\mathbf{x} = \mathbf{0} and Rx=0R\mathbf{x} = \mathbf{0} have the same solutions; write the pivot variables in terms of the free ones (Homogeneous Systems).

Also: Ax=bA\mathbf{x} = \mathbf{b} is consistent exactly when b∈col⁡(A)\mathbf{b} \in \operatorname{col}(A).

One reduction, three bases

python · try it yourself
from sympy import Matrix

A = Matrix([[1, 2, 0, 1, 3],
            [2, 4, 1, 4, 7],
            [1, 2, 2, 3, 3]])
R, piv = A.rref()
print("rref rows:")
for i in range(R.rows):
    print("  ", list(R.row(i)))
print("pivot columns:", piv)

print("col(A) basis:", [list(A.col(j)) for j in piv])
print("row(A) basis:", [list(R.row(i)) for i in range(len(piv))])
print("null(A) basis:", [list(v) for v in A.nullspace()])
print("rank =", A.rank(), " nullity =", len(A.nullspace()), " n =", A.cols)
rref rows:
   [1, 2, 0, 0, 2]
   [0, 0, 1, 0, -1]
   [0, 0, 0, 1, 1]
pivot columns: (0, 2, 3)
col(A) basis: [[1, 2, 1], [0, 1, 2], [1, 4, 3]]
row(A) basis: [[1, 2, 0, 0, 2], [0, 0, 1, 0, -1], [0, 0, 0, 1, 1]]
null(A) basis: [[-2, 1, 0, 0, 0], [-2, 0, 1, -1, 1]]
rank = 3  nullity = 2  n = 5

Rank and the rank-nullity theorem

The number of pivots counts three things at once:

rank⁡A=number of pivots=dim⁡col⁡(A)=dim⁡row⁡(A)\operatorname{rank} A = \text{number of pivots} = \dim \operatorname{col}(A) = \dim \operatorname{row}(A)

So row rank equals column rank (rank⁡A=rank⁡AT\operatorname{rank} A = \operatorname{rank} A^T), a surprising fact for a non-square matrix. Every column is either a pivot column or a free column, which gives the

Rank-nullity theorem. For an m×nm \times n matrix AA,

rank⁡A+dim⁡null⁡(A)=n(pivots+free variables=number of columns)\operatorname{rank} A + \dim \operatorname{null}(A) = n \qquad (\text{pivots} + \text{free variables} = \text{number of columns})

In the example: 3+2=53 + 2 = 5. Consequences: a 3×5 matrix always has a null space of dimension at least 2; an n×nn \times n matrix has null⁡(A)={0}\operatorname{null}(A) = \{\mathbf{0}\} exactly when rank⁡A=n\operatorname{rank} A = n, i.e. when AA is invertible.

In software: rank and null space in data

Take a data matrix XX (samples × features) and a linear model with predictions y^=Xw\hat{\mathbf{y}} = X\mathbf{w}.

  • Rank = number of truly independent features. Redundant columns (unit conversions, totals, one-hot dummies plus an intercept) lower the rank below the number of columns (Linear Independence).
  • The null space is the set of invisible weight changes. If Xz=0X\mathbf{z} = \mathbf{0}, then X(w+z)=XwX(\mathbf{w} + \mathbf{z}) = X\mathbf{w}: adding z\mathbf{z} to the weights changes no prediction at all. The model is not identifiable: the data cannot decide between w\mathbf{w} and w+tz\mathbf{w} + t\mathbf{z}, which is why coefficients of collinear features are meaningless and regularisation is needed to pick one solution.
python · try it yourself
import numpy as np

rng = np.random.default_rng(5)
n = 50
temp_c = rng.uniform(-5, 35, n)
temp_f = 1.8 * temp_c + 32
X = np.column_stack([np.ones(n), temp_c, temp_f])     # intercept, °C, °F
print("rank:", int(np.linalg.matrix_rank(X)), "of", X.shape[1], "columns")

z = np.array([32.0, 1.8, -1.0])                       # 32·1 + 1.8·C − F = 0
print("X @ z is zero:", np.allclose(X @ z, 0))

w = np.array([10.0, 0.5, 0.0])
w2 = w + 7 * z                                        # very different weights...
print("w2 =", w2.tolist())
print("same predictions:", np.allclose(X @ w, X @ w2))
rank: 2 of 3 columns
X @ z is zero: True
w2 = [234.0, 13.1, -7.0]
same predictions: True

The same ideas show up elsewhere: a recommendation system's user × item rating matrix is approximately low rank (a few taste factors explain most ratings), which is what matrix factorisation and the SVD exploit.

Exercises

Exercise 1: Bases by hand

For A=[132265]A = \begin{bmatrix} 1 & 3 & 2 \\ 2 & 6 & 5 \end{bmatrix}, find bases of col⁡(A)\operatorname{col}(A), row⁡(A)\operatorname{row}(A) and null⁡(A)\operatorname{null}(A), and check rank-nullity.

Answer

col⁡(A)\operatorname{col}(A): {[1,2],[2,5]}\{[1, 2], [2, 5]\}; row⁡(A)\operatorname{row}(A): {[1,3,0],[0,0,1]}\{[1, 3, 0], [0, 0, 1]\}; null⁡(A)\operatorname{null}(A): {[−3,1,0]}\{[-3, 1, 0]\}; 2+1=32 + 1 = 3.

Solution
R2−2R1:[0,0,1]R1−2R2:[1,3,0]rref⁡=[130001],pivots in columns 1 and 3, free x2null:x1=−3x2, x3=0→x2⋅[−3,1,0]col:original columns 1 and 3→[1,2],[2,5]rank 2+nullity 1=3 columns✓\begin{aligned} R_2 - 2R_1 &: [0, 0, 1] \\ R_1 - 2R_2 &: [1, 3, 0] \\ \operatorname{rref} &= \begin{bmatrix} 1 & 3 & 0 \\ 0 & 0 & 1 \end{bmatrix}, \quad \text{pivots in columns 1 and 3, free } x_2 \\ \text{null} &: x_1 = -3x_2,\ x_3 = 0 \to x_2 \cdot [-3, 1, 0] \\ \text{col} &: \text{original columns 1 and 3} \to [1, 2], [2, 5] \\ &\text{rank } 2 + \text{nullity } 1 = 3 \text{ columns} \checkmark \end{aligned}
python · try it yourself
from sympy import Matrix
A = Matrix([[1, 3, 2], [2, 6, 5]])
print(A.rref()[0].tolist(), [list(v) for v in A.nullspace()], [list(v) for v in A.columnspace()])
[[1, 3, 0], [0, 0, 1]] [[-3, 1, 0]] [[1, 2], [2, 5]]

Exercise 2: Is b\mathbf{b} reachable?

With AA from Exercise 1, is Ax=[4,9]A\mathbf{x} = [4, 9] solvable? Is Ax=[1,1]A\mathbf{x} = [1, 1]?

Answer

Both are solvable, since col⁡(A)=R2\operatorname{col}(A) = \mathbb{R}^2 (rank 2).

Solution

col⁡(A)\operatorname{col}(A) is a 2-dimensional subspace of R2\mathbb{R}^2, so it is all of R2\mathbb{R}^2 and every b\mathbf{b} is reachable. To find an x\mathbf{x}, write b\mathbf{b} in the column basis {[1,2],[2,5]}\{[1, 2], [2, 5]\} and put the coefficients on columns 1 and 3 (x2=0x_2 = 0):

b=[4,9]:c1+2c2=4, 2c1+5c2=9  →  c2=1, c1=2  →  x=[2,0,1]b=[1,1]:c1+2c2=1, 2c1+5c2=1  →  c2=−1, c1=3  →  x=[3,0,−1]check:[2+0+2,4+0+5]=[4,9]✓[3+0−2,6+0−5]=[1,1]✓\begin{aligned} \mathbf{b} = [4, 9]&: \quad c_1 + 2c_2 = 4,\ 2c_1 + 5c_2 = 9 \;\to\; c_2 = 1,\ c_1 = 2 \;\to\; \mathbf{x} = [2, 0, 1] \\ \mathbf{b} = [1, 1]&: \quad c_1 + 2c_2 = 1,\ 2c_1 + 5c_2 = 1 \;\to\; c_2 = -1,\ c_1 = 3 \;\to\; \mathbf{x} = [3, 0, -1] \\ \text{check}&: \quad [2 + 0 + 2, 4 + 0 + 5] = [4, 9] \checkmark \qquad [3 + 0 - 2, 6 + 0 - 5] = [1, 1] \checkmark \end{aligned}

Adding any multiple of the null-space vector [−3,1,0][-3, 1, 0] gives other solutions.

Exercise 3: Dimensions without computing

AA is a 6×9 matrix of rank 4. Give dim⁡col⁡(A)\dim \operatorname{col}(A), dim⁡row⁡(A)\dim \operatorname{row}(A) and dim⁡null⁡(A)\dim \operatorname{null}(A), and the dimension of the null space of ATA^T.

Answer

4, 4, 5 and 2.

Solution

dim⁡col⁡=dim⁡row⁡=rank⁡=4\dim \operatorname{col} = \dim \operatorname{row} = \operatorname{rank} = 4. Rank-nullity for AA (9 columns): nullity=9−4=5\text{nullity} = 9 - 4 = 5. ATA^T is 9×6 with the same rank 4, so null⁡(AT)\operatorname{null}(A^T) has dimension 6−4=26 - 4 = 2.

Exercise 4: Find the invisible weights

A dataset has columns [1,a,b,a+2b][1, a, b, a + 2b] (an intercept, two features and their combination). Find a non-zero null-space vector z\mathbf{z}, and show numerically that adding 3z3\mathbf{z} to some weights does not change the predictions.

Answer

z=[0,1,2,−1]\mathbf{z} = [0, 1, 2, -1] (or any multiple).

Solution

a+2b−(a+2b)=0a + 2b - (a + 2b) = 0 gives 0⋅1+1⋅a+2⋅b−1⋅(a+2b)=00 \cdot 1 + 1 \cdot a + 2 \cdot b - 1 \cdot (a + 2b) = 0, so z=[0,1,2,−1]\mathbf{z} = [0, 1, 2, -1].

python · try it yourself
import numpy as np
rng = np.random.default_rng(1)
a, b = rng.normal(size=(2, 30))
X = np.column_stack([np.ones(30), a, b, a + 2 * b])
z = np.array([0, 1, 2, -1.0])
w = np.array([1.0, -2.0, 0.5, 4.0])
print(np.allclose(X @ z, 0), np.allclose(X @ w, X @ (w + 3 * z)))
True True

Test yourself

AA is 4×7 with rank 3. What is the dimension of its null space?

  1. 1
  2. 3
  3. 7
  4. 4

Which statement about row operations is true?

  1. They preserve the column space
  2. They preserve the columns
  3. They preserve the row space
  4. They change the rank

A linear model y^=Xw\hat{\mathbf{y}} = X\mathbf{w} has a non-zero z\mathbf{z} with Xz=0X\mathbf{z} = \mathbf{0}. What does that mean?

  1. The model fits perfectly
  2. Different weights w\mathbf{w} and w+z\mathbf{w} + \mathbf{z} give identical predictions
  3. XX has full column rank
  4. Training will diverge