AnyLearn
All lessons
Mathintermediate

Vectors, Spans, and Subspaces

Vectors are more than arrows — they're the atoms of every ML model, physics engine, and signal processor alive. Build rock-solid intuition for linear combinations, span, independence, basis, and orthogonality, then verify it all in NumPy.

Not signed in — your progress and quiz score won't be saved.
Lesson progress1 / 9

Vectors: two lives at once

A vector in Rn\mathbb{R}^n lives two simultaneous lives.

Geometric life: an arrow from the origin, carrying direction and magnitude. Adding two arrows tip-to-tail gives a third; scaling stretches or flips.

Algebraic life: a column of numbers:

v=[v1v2vn]Rn.\mathbf{v} = \begin{bmatrix} v_1 \\ v_2 \\ \vdots \\ v_n \end{bmatrix} \in \mathbb{R}^n.

Both views are true at once. When computing, think algebraically. When confused, draw arrows. A neural network's weight vector may be 10610^6-dimensional — impossible to draw — but the arrow intuition still tells you whether two gradient steps "agree" (positive dot product) or "fight" (negative). Throughout this course: bold lowercase for vectors (v\mathbf{v}), v\|\mathbf{v}\| for Euclidean norm, uv\mathbf{u}^\top\mathbf{v} for the dot product.

Full lesson text

All 9 steps on one page — for reading, reference, and search.

Show

1. Vectors: two lives at once

A vector in Rn\mathbb{R}^n lives two simultaneous lives.

Geometric life: an arrow from the origin, carrying direction and magnitude. Adding two arrows tip-to-tail gives a third; scaling stretches or flips.

Algebraic life: a column of numbers:

v=[v1v2vn]Rn.\mathbf{v} = \begin{bmatrix} v_1 \\ v_2 \\ \vdots \\ v_n \end{bmatrix} \in \mathbb{R}^n.

Both views are true at once. When computing, think algebraically. When confused, draw arrows. A neural network's weight vector may be 10610^6-dimensional — impossible to draw — but the arrow intuition still tells you whether two gradient steps "agree" (positive dot product) or "fight" (negative). Throughout this course: bold lowercase for vectors (v\mathbf{v}), v\|\mathbf{v}\| for Euclidean norm, uv\mathbf{u}^\top\mathbf{v} for the dot product.

2. Linear combinations and span

A linear combination of v1,,vk\mathbf{v}_1, \dots, \mathbf{v}_k is any expression

c1v1+c2v2++ckvk,ciR.c_1\mathbf{v}_1 + c_2\mathbf{v}_2 + \cdots + c_k\mathbf{v}_k, \quad c_i \in \mathbb{R}.

The span is the set of all such combinations — every reachable point:

span(v1,,vk)={i=1kcivi   |   ciR}.\operatorname{span}(\mathbf{v}_1, \dots, \mathbf{v}_k) = \left\{\sum_{i=1}^k c_i \mathbf{v}_i \;\ \middle|\;\ c_i \in \mathbb{R}\right\}.

Geometrically: one nonzero vector spans a line; two non-collinear vectors span a plane; add a third off that plane and you span all of R3\mathbb{R}^3. Add a fourth — the span doesn't grow, because you can't escape R3\mathbb{R}^3.

This is the first hint that dimension is about the smallest set of vectors you need, not the largest. The span is always a subspace: contains 0\mathbf{0}, closed under addition and scalar multiplication.

3. Linear independence

Vectors v1,,vk\mathbf{v}_1, \dots, \mathbf{v}_k are linearly independent if the only solution to

c1v1++ckvk=0c_1\mathbf{v}_1 + \cdots + c_k\mathbf{v}_k = \mathbf{0}

is c1==ck=0c_1 = \cdots = c_k = 0. Otherwise one vector hides inside the span of the rest — pure redundancy.

Key facts:

  • Two vectors are dependent iff one is a scalar multiple of the other.
  • You can never fit more than nn independent vectors into Rn\mathbb{R}^n.
  • Three vectors in R2\mathbb{R}^2 are always dependent.

Worked example. Are v1=[1,2,3]\mathbf{v}_1 = [1,2,3]^\top, v2=[2,5,7]\mathbf{v}_2 = [2,5,7]^\top, v3=[1,3,4]\mathbf{v}_3 = [1,3,4]^\top independent? Row-reduce:

[121253374]R22R1,R33R1[121011011]R3R2[121011000].\begin{bmatrix}1&2&1\\2&5&3\\3&7&4\end{bmatrix} \xrightarrow{R_2-2R_1,\,R_3-3R_1} \begin{bmatrix}1&2&1\\0&1&1\\0&1&1\end{bmatrix} \xrightarrow{R_3-R_2} \begin{bmatrix}1&2&1\\0&1&1\\0&0&0\end{bmatrix}.

A zero row means a free variable: dependent. Indeed, v3=v2v1\mathbf{v}_3 = \mathbf{v}_2 - \mathbf{v}_1.

4. Basis and dimension

A basis of a subspace VV is a set of vectors that (1) spans VV and (2) is linearly independent — no redundancy, nothing missing.

The standard basis of R3\mathbb{R}^3:

e1=[100],e2=[010],e3=[001].\mathbf{e}_1=\begin{bmatrix}1\\0\\0\end{bmatrix},\quad \mathbf{e}_2=\begin{bmatrix}0\\1\\0\end{bmatrix},\quad \mathbf{e}_3=\begin{bmatrix}0\\0\\1\end{bmatrix}.

Every basis of VV has exactly the same size. That common size is the dimension dim(V)\dim(V). A line through the origin: dim=1\dim=1. A plane through the origin: dim=2\dim=2. All of Rn\mathbb{R}^n: dim=n\dim=n.

Every vector in VV has a unique coordinate representation in a given basis: v=x1b1+x2b2++xkbk.\mathbf{v} = x_1\mathbf{b}_1 + x_2\mathbf{b}_2 + \cdots + x_k\mathbf{b}_k.

Change the basis, change the numbers — but the vector itself is the same geometric object. Basis changes are the engine behind change-of-variable tricks everywhere in applied math.

5. Dot product, norms, and angles

The dot product of u,vRn\mathbf{u},\mathbf{v}\in\mathbb{R}^n:

uv=uv=i=1nuivi.\mathbf{u}\cdot\mathbf{v} = \mathbf{u}^\top\mathbf{v} = \sum_{i=1}^n u_i v_i.

The Euclidean norm: v=vv\|\mathbf{v}\| = \sqrt{\mathbf{v}^\top\mathbf{v}}. The single most useful formula connecting algebra to geometry:

uv=uvcosθcosθ=uvuv.\mathbf{u}^\top\mathbf{v} = \|\mathbf{u}\|\,\|\mathbf{v}\|\cos\theta \quad\Longrightarrow\quad \cos\theta = \frac{\mathbf{u}^\top\mathbf{v}}{\|\mathbf{u}\|\,\|\mathbf{v}\|}.

cosθ\cos\thetaRelationship
11Same direction
00Orthogonal
1-1Opposite direction

Orthogonality (uv=0\mathbf{u}^\top\mathbf{v}=0) means the vectors share zero information. The Cauchy-Schwarz inequality uvuv|\mathbf{u}^\top\mathbf{v}| \le \|\mathbf{u}\|\,\|\mathbf{v}\| guarantees cosθ[1,1]\cos\theta\in[-1,1], making the angle formula valid in any dimension.

6. Orthogonal bases and projection

An orthonormal basis {q1,,qn}\{\mathbf{q}_1,\dots,\mathbf{q}_n\} satisfies qiqj=δij\mathbf{q}_i^\top\mathbf{q}_j = \delta_{ij} — pairwise orthogonal unit vectors. These are golden: coordinates are just dot products.

The projection of b\mathbf{b} onto a\mathbf{a}:

projab=abaaa.\operatorname{proj}_{\mathbf{a}}\mathbf{b} = \frac{\mathbf{a}^\top\mathbf{b}}{\mathbf{a}^\top\mathbf{a}}\,\mathbf{a}.

The residual bprojab\mathbf{b} - \operatorname{proj}_{\mathbf{a}}\mathbf{b} is orthogonal to a\mathbf{a} — this right-angle split is the engine of least squares.

Gram-Schmidt converts any basis to an orthonormal one: at each step, subtract off the projections onto vectors already processed, then normalize. Mechanically: "remove what I've already explained, then report what's new and perpendicular." GS underlies QR factorization, which is how numerical linear algebra software actually runs least-squares solvers — Gaussian elimination is slower and less stable.

7. Subspaces: taxonomy and the affine trap

A subspace of Rn\mathbb{R}^n must satisfy three conditions:

  1. Contains 0\mathbf{0}.
  2. Closed under addition: u,vVu+vV\mathbf{u},\mathbf{v}\in V \Rightarrow \mathbf{u}+\mathbf{v}\in V.
  3. Closed under scalar multiplication: vV,cRcvV\mathbf{v}\in V, c\in\mathbb{R} \Rightarrow c\mathbf{v}\in V.
SubspaceExampleDim
Trivial{0}\{\mathbf{0}\}0
Line through originspan(v)\operatorname{span}(\mathbf{v})1
Plane through originspan(u,v)\operatorname{span}(\mathbf{u},\mathbf{v})2
Full spaceRn\mathbb{R}^nnn

Critical trap: the set {x:Ax=b}\{\mathbf{x}: A\mathbf{x}=\mathbf{b}\} with b0\mathbf{b}\ne\mathbf{0} is not a subspace — it misses the origin. It is an affine subspace (one particular solution plus the null space). This distinction is essential when studying solutions to linear systems.

8. NumPy: independence check and angles in code

Row reduction by hand is tedious. NumPy makes it a one-liner:

import numpy as np

v1 = np.array([1, 2, 3], dtype=float)
v2 = np.array([2, 5, 7], dtype=float)
v3 = np.array([1, 3, 4], dtype=float)

A = np.column_stack([v1, v2, v3])  # columns are our vectors
rank = np.linalg.matrix_rank(A)
print(f"Rank = {rank}")          # 2 → dependent! (v3 = v2 - v1)
print(f"v3 = v2 - v1: {np.allclose(v3, v2 - v1)}")  # True

# Angle between v1 and v2
cos_t = np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))
angle = np.degrees(np.arccos(np.clip(cos_t, -1, 1)))
print(f"Angle(v1, v2) = {angle:.2f}°")  # ~5.77° — nearly parallel

matrix_rank uses SVD under the hood and counts singular values above a threshold. Never use np.linalg.det to check independence for floating-point data — near-zero determinants look like zero even when they aren't. SVD-based rank is the numerically stable professional tool.

9. The conceptual scaffold for everything ahead

Here's the foundation this lesson built:

  • Vectors are elements of Rn\mathbb{R}^n with dual geometric-algebraic identity.
  • Linear combinations are the only operations linear algebra permits — scale and add.
  • Span is the reachable set; subspace is its rigorous container.
  • Independence means no redundancy; basis = minimal spanning set.
  • Dimension = basis size, an intrinsic property of the subspace.
  • Dot product encodes length, angle, and orthogonality in a single formula.

The next lesson asks: what happens when you apply a matrix to every vector in Rn\mathbb{R}^n? The answer reshapes space — stretching, rotating, collapsing — and understanding it requires everything built here. Rank, column space, null space, and invertibility all trace back to span, independence, and dimension.

Check your understanding

The lesson ends with a 5-question quiz. Take it in the player above to see your score.

  1. Three vectors in $\mathbb{R}^2$ are always linearly dependent. Why?
    • Because $\mathbb{R}^2$ has only two coordinates, so any three vectors must share an entry.
    • Because any set of more than two vectors is dependent by definition.
    • Because two non-parallel vectors already span all of $\mathbb{R}^2$, so the third lies in their span.
    • Because the dot product of any three vectors in $\mathbb{R}^2$ is zero.
  2. The span of $\{\mathbf{v}_1, \mathbf{v}_2\}$ where $\mathbf{v}_2 = 3\mathbf{v}_1$ is:
    • A plane through the origin.
    • All of $\mathbb{R}^n$.
    • A line through the origin.
    • The set $\{\mathbf{0}\}$ only.
  3. If $\mathbf{u}^\top\mathbf{v}=0$, $\|\mathbf{u}\|=3$, $\|\mathbf{v}\|=4$, what is $\|\mathbf{u}+\mathbf{v}\|$?
    • 7
    • 12
    • 5
    • 1
  4. A subspace $V\subseteq\mathbb{R}^5$ has dimension 3. How many vectors does any basis of $V$ contain?
    • 5, because the ambient space is $\mathbb{R}^5$.
    • 3, because dimension equals the number of basis vectors.
    • 2, because $5-3=2$ vectors are redundant.
    • It depends on which basis is chosen.
  5. The set $S=\{\mathbf{x}\in\mathbb{R}^3: x_1+x_2+x_3=1\}$ is:
    • A subspace of dimension 2.
    • A subspace of dimension 1.
    • An affine subspace — not a subspace — because it does not contain the origin.
    • Not a subspace because it contains more than one vector.

Related lessons