AI Knowledge Tree

Vector spaces & subspaces

Define a vector space from its axioms, see that the only property we ever actually use is closure under linear combination, and learn the one-line test that decides whether a subset is a subspace.

⏱ 28 min

Everything in this course, a dataset, a layer of a network, a gradient, lives in a vector space. Before using that word freely we should pin down exactly what it means, and notice that out of the long list of axioms, almost all of our work leans on a single consequence: you can take linear combinations and stay inside the space.

What problem are we solving?

We want a setting where "add two things" and "scale a thing by a number" both make sense and behave the way arrows in the plane behave. Points in R2\mathbb{R}^2 work; so do polynomials, and so do functions. Rather than re-derive facts for each case, we name the shared structure once and prove things there.

The definition

A vector space over the real numbers is a set VV together with two operations, addition u+vu + v and scalar multiplication αv\alpha v (with α∈R\alpha \in \mathbb{R}), such that the following hold for all u,v,w∈Vu, v, w \in V and all α,β∈R\alpha, \beta \in \mathbb{R}[1, 2]:

(1) u+v=v+u(2) (u+v)+w=u+(v+w)(3) ∃ 0∈V: v+0=v(4) ∃ (−v): v+(−v)=0(5) 1 v=v(6) (αβ)v=α(βv)(7) α(u+v)=αu+αv(8) (α+β)v=αv+βv\begin{aligned} &\text{(1) } u + v = v + u && \text{(2) } (u+v)+w = u+(v+w) \\ &\text{(3) } \exists\, 0 \in V:\ v + 0 = v && \text{(4) } \exists\, (-v):\ v + (-v) = 0 \\ &\text{(5) } 1\,v = v && \text{(6) } (\alpha\beta)v = \alpha(\beta v) \\ &\text{(7) } \alpha(u+v) = \alpha u + \alpha v && \text{(8) } (\alpha+\beta)v = \alpha v + \beta v \end{aligned}

The list looks long, but read it once and move on. Its only purpose is to guarantee that the next definition behaves sensibly.

The one move: linear combinations

A linear combination of vectors v1,…,vkv_1, \dots, v_k is any expression

c1v1+c2v2+⋯+ckvk,ci∈R.c_1 v_1 + c_2 v_2 + \cdots + c_k v_k, \qquad c_i \in \mathbb{R}.

Axioms (1) through (8) are exactly what we need to know that this expression is again a well-defined element of VV, no matter how we group or reorder the terms. This is the property the rest of the course actually uses. When we later write a model output as a weighted sum of features, or a gradient as a combination of per-example gradients, we are relying on nothing more than closure under linear combination.

Subspaces

Often we care about a smaller set sitting inside VV that is itself a vector space under the same operations. Call W⊆VW \subseteq V a subspace if it is a vector space in its own right. Checking all eight axioms again would be tedious, so we prove a shortcut.

Subspace test. A nonempty subset W⊆VW \subseteq V is a subspace if and only if it is closed under addition and scalar multiplication[3]:

u,v∈W  ⟹  u+v∈W,α∈R, v∈W  ⟹  αv∈W.u, v \in W \implies u + v \in W, \qquad \alpha \in \mathbb{R},\ v \in W \implies \alpha v \in W.

Proof. (⇒\Rightarrow) If WW is a subspace it is closed under its own operations by definition, so both conditions hold.

(⇐\Leftarrow) Suppose the two closure conditions hold and WW is nonempty; we must recover the axioms. Take any v∈Wv \in W (one exists since W≠∅W \neq \varnothing).

  1. Zero vector. Scalar-multiply by 00: closure under scalar multiplication gives 0⋅v∈W0\cdot v \in W. And 0⋅v=00 \cdot v = 0 (a one-line consequence of axiom (8): 0v=(0+0)v=0v+0v0v = (0+0)v = 0v + 0v, then subtract 0v0v). So 0∈W0 \in W.
  2. Additive inverses. Scalar-multiply by −1-1: (−1)v∈W(-1)v \in W, and (−1)v=−v(-1)v = -v (again from the axioms). So every element of WW has its inverse in WW.
  3. Remaining axioms. Commutativity, associativity, and the scalar laws hold for all elements of VV, so in particular they hold for the elements of WW.

So WW, being closed and containing 00 and inverses while inheriting the rest, is a vector space. ■\qquad\blacksquare

The test collapses eight checks into two. A quick consequence, proved the same way: the intersection of two subspaces is a subspace (if u,vu, v lie in both, so does u+vu+v and αv\alpha v, since each subspace is closed).

Examples and one non-example

  • The line {(t,2t):t∈R}⊆R2\{(t, 2t) : t \in \mathbb{R}\} \subseteq \mathbb{R}^2 is a subspace: adding two such points or scaling one keeps the y=2xy = 2x relation.
  • The set of degree-≤n\le n polynomials is a subspace of all polynomials.
  • The line {(t,2t+1)}\{(t, 2t + 1)\} is not a subspace: it misses the origin (0∉W0 \notin W), so it fails the test immediately. Any set not containing 00 is disqualified.

Check it on arrays

The subspace test is something you can probe numerically: take the plane spanned by two vectors in R3\mathbb{R}^3, form a random linear combination of points already in it, and confirm the result still satisfies the plane's defining equation (its normal vector stays orthogonal to it).

python · numpy
import numpy as np

# A plane through the origin in R^3: span of b1, b2. Its normal is b1 x b2.
b1 = np.array([1.0, 0.0, 2.0])
b2 = np.array([0.0, 1.0, -1.0])
normal = np.cross(b1, b2)

# Two points already in the subspace (linear combinations of b1, b2):
u = 3.0 * b1 - 2.0 * b2
v = -1.0 * b1 + 4.0 * b2

# A further linear combination of them:
w = 0.7 * u + 5.0 * v

# Closure check: w still lies in the plane <=> normal . w == 0
for name, vec in [("u", u), ("v", v), ("w", w)]:
    print(f"normal . {name} = {np.dot(normal, vec):.12f}")   # all ~0

Every point built as a linear combination of b1,b2b_1, b_2 stays on the plane. That is the subspace test confirmed: closed under addition and scaling. A point off the plane (say b1+(0,0,1)b_1 + (0,0,1)) would give a nonzero dot product, landing outside WW.

Exercises

Exercise 1. Decide whether W={(x,y,z)∈R3:x+y+z=0}W = \{(x, y, z) \in \mathbb{R}^3 : x + y + z = 0\} is a subspace of R3\mathbb{R}^3, using only the two-condition subspace test.

Solution

Step 1. Check nonempty: (0,0,0)(0, 0, 0) satisfies 0+0+0=00 + 0 + 0 = 0, so 0∈W0 \in W and WW is nonempty.

Step 2. Closure under addition. Take u=(u1,u2,u3)u = (u_1, u_2, u_3) and v=(v1,v2,v3)v = (v_1, v_2, v_3) in WW, so u1+u2+u3=0u_1 + u_2 + u_3 = 0 and v1+v2+v3=0v_1 + v_2 + v_3 = 0. Their sum has coordinate total (u1+v1)+(u2+v2)+(u3+v3)=(u1+u2+u3)+(v1+v2+v3)=0+0=0(u_1 + v_1) + (u_2 + v_2) + (u_3 + v_3) = (u_1 + u_2 + u_3) + (v_1 + v_2 + v_3) = 0 + 0 = 0, so u+v∈Wu + v \in W.

Step 3. Closure under scalar multiplication. For α∈R\alpha \in \mathbb{R} and v∈Wv \in W, the total is αv1+αv2+αv3=α(v1+v2+v3)=α⋅0=0\alpha v_1 + \alpha v_2 + \alpha v_3 = \alpha(v_1 + v_2 + v_3) = \alpha \cdot 0 = 0, so αv∈W\alpha v \in W.

Both conditions hold on a nonempty set, so WW is a subspace. It is the plane through the origin with normal (1,1,1)(1, 1, 1).

Exercise 2. Give a subset of R2\mathbb{R}^2 that is closed under scalar multiplication but not under addition, proving it fails exactly one condition.

Solution

Step 1. Take W={(x,y):xy=0}W = \{(x, y) : xy = 0\}, the union of the two coordinate axes.

Step 2. Closure under scalar multiplication holds: if xy=0xy = 0 then (αx)(αy)=α2(xy)=0(\alpha x)(\alpha y) = \alpha^2 (xy) = 0, so αv∈W\alpha v \in W for every scalar α\alpha.

Step 3. Closure under addition fails. The point (1,0)(1, 0) lies on the xx-axis and (0,1)(0, 1) lies on the yy-axis, so both are in WW. Their sum (1,1)(1, 1) has product 1⋅1=1≠01 \cdot 1 = 1 \neq 0, so (1,1)∉W(1, 1) \notin W.

A single counterexample to addition is enough; WW is not a subspace even though it passes the scalar-multiplication condition and contains 00.

Exercise 3 (code). The subspace test predicts that any linear combination of points in W={x:Ax=0}W = \{x : Ax = 0\} (the solution set of a homogeneous system) is again in WW. Verify this numerically for a random 3×53 \times 5 matrix AA: build two solutions, take a random linear combination, and assert it still solves Ax=0Ax = 0.

Solution

The null space of AA is a subspace, so closure must hold to machine precision. Two null-space vectors come from the right singular vectors of the smallest singular values (here the last two columns of VV, since a 3×53 \times 5 matrix has nullity at least 22).

python · numpy
import numpy as np

rng = np.random.default_rng(0)
A = rng.normal(size=(3, 5))

# Right singular vectors for the (near-)zero singular values span the null space.
_, _, Vt = np.linalg.svd(A)
x1 = Vt[-1]            # A @ x1 ~ 0
x2 = Vt[-2]            # A @ x2 ~ 0

# A random linear combination of two null-space vectors:
a, b = rng.normal(size=2)
w = a * x1 + b * x2

assert np.allclose(A @ x1, 0, atol=1e-12)
assert np.allclose(A @ x2, 0, atol=1e-12)
assert np.allclose(A @ w, 0, atol=1e-12)   # closure under linear combination
print("max |A @ w| =", np.max(np.abs(A @ w)))

The combination w=ax1+bx2w = a x_1 + b x_2 satisfies Aw=a(Ax1)+b(Ax2)=0Aw = a(Ax_1) + b(Ax_2) = 0 by linearity of AA, which is the subspace test at work: the solution set of a homogeneous system is closed under linear combination.

With the container defined, the next lesson asks the sharper question: how many vectors do we actually need to generate a space, and when is a set of them non-redundant. That is span, independence, basis, and dimension.


References

  1. [1]Axler, S. (2015). Linear Algebra Done Right (3rd ed.), ch. 1. Springer. The axiomatic, basis-free development of vector spaces (doi:10.1007/978-3-319-11080-6).↩
  2. [2]Goodfellow, I., Bengio, Y., & Courville, A. (2016). Deep Learning, ch. 2 (Linear Algebra). MIT Press. The machine-learning reader's review of the same objects.↩
  3. [3]Strang, G. (2016). Introduction to Linear Algebra (5th ed.). Wellesley-Cambridge Press. Subspaces and the closure conditions.↩