Spans
The span of a set of vectors is everything you can build from them with linear combinations. Asking "is in the span?" is the same as asking "does a certain linear system have a solution?", so row reduction answers it.
Definition
The span of vectors in is the set of all their linear combinations:
By convention (no vectors). The span always contains (take all ) and each itself.
Geometrically in :
| Vectors | Span |
|---|---|
| one non-zero vector | the line through in direction |
| two non-parallel vectors | the plane through containing both |
| two parallel vectors | still just a line |
| three vectors not in a common plane | all of |
Spans always pass through the origin: the line is not a span.
Is in the span? Solve a system
means some satisfy . Put the as columns of a matrix ; the question becomes whether is consistent. Row-reduce the augmented matrix : is in the span exactly when there is no pivot in the last column.
from sympy import Matrix
v1 = Matrix([1, 2, 0])
v2 = Matrix([0, 1, 1])
A = v1.row_join(v2)
for w in (Matrix([2, 7, 3]), Matrix([1, 1, 1])):
R, piv = A.row_join(w).rref()
inside = 2 not in piv # column index 2 = the w column
print("w =", list(w), "in span:", inside)
if inside:
print(" coefficients c1, c2 =", R[0, 2], R[1, 2])w = [2, 7, 3] in span: True
coefficients c1, c2 = 2 3
w = [1, 1, 1] in span: False
Check: . ✓
When do vectors span all of ?
exactly when is consistent for every , which happens exactly when the rref of (without any augmented column) has a pivot in every row. Consequences:
- You need at least vectors to span : vectors give at most pivots.
- Adding a vector that is already in the span does not make the span bigger.
from sympy import Matrix
A = Matrix([[1, 0, 2],
[0, 1, 3],
[1, 1, 5]]) # columns v1, v2, v3 = 2 v1 + 3 v2
R, piv = A.rref()
print("pivots:", piv, "rows:", A.rows, "-> spans R^3:", len(piv) == A.rows)
B = Matrix([[1, 0, 1], [0, 1, 1], [1, 1, 0]])
print("pivots:", B.rref()[1], "-> spans R^3:", len(B.rref()[1]) == 3)pivots: (0, 1) rows: 3 -> spans R^3: False
pivots: (0, 1, 2) -> spans R^3: True
The first set has three vectors but spans only a plane, because adds nothing new. That idea is made precise in Linear Independence.
Picture: a span in
Integer combinations of two non-parallel vectors form a slanted grid. Allowing all real coefficients fills in the gaps and covers the whole plane:
import numpy as np
import matplotlib.pyplot as plt
v1, v2 = np.array([2.0, 0.5]), np.array([-0.5, 1.5])
pts = np.array([a * v1 + b * v2 for a in range(-3, 4) for b in range(-3, 4)])
plt.figure(figsize=(5, 5))
plt.scatter(pts[:, 0], pts[:, 1], s=12, color="gray", label="a·v1 + b·v2, integers a, b")
for v, name in ((v1, "v1"), (v2, "v2")):
plt.arrow(0, 0, *v, width=0.05, color="tab:blue", length_includes_head=True)
plt.text(*(v * 1.1), name)
plt.axhline(0, lw=0.5, color="k"); plt.axvline(0, lw=0.5, color="k")
plt.gca().set_aspect("equal"); plt.legend(loc="lower right")
print("grid points:", len(pts))grid points: 49
In software
- Colour: every colour a screen can show is a combination of its red, green and blue primaries with coefficients in . That is a bounded piece of ; colours outside it (out of gamut) cannot be displayed.
- Isometric and hex grids in games use two non-perpendicular basis vectors; every tile position is an integer combination of them, exactly the picture above.
- Expressiveness of a linear model: a linear layer with weight matrix can only output vectors in the span of 's columns (the column space, see Column Space, Row Space and Null Space). If that span is small, no amount of training lets it produce other outputs; this is the idea behind low-rank adapters (Low-Rank Approximation).
- Robotics and animation: blend shapes in facial animation are combinations of a few "expression" vectors; the reachable faces are their span (restricted to sensible weights).
Exercises
Exercise 1: Describe the span
Describe geometrically in .
Answer
A line through the origin with direction .
Solution
The second vector is times the first, so every combination is a multiple of . The span is the line .
Exercise 2: Membership by hand
Is in ? If so, find the coefficients; if not, show why.
Answer
No. The first two coordinates force , , and then the third coordinate is 7, not 9.
Solution
The third equation fails, so is not in the span. Row reduction confirms it:
from sympy import Matrix
A = Matrix([[1, 1], [0, 1], [2, 1]])
w = Matrix([4, 1, 9])
print(A.row_join(w).rref()[1])(0, 1, 2)
A pivot in column 2 (the column) means the system is inconsistent.
Exercise 3: Find the missing value
For which is in ?
Answer
.
Solution
Exercise 4: Spanning test in code
Write a function spans_rn(vectors) that returns True when the given vectors span ( = their length), using the rank. Test it on and .
Answer
False for the first set, True for the second.
Solution
The vectors span exactly when the matrix with them as columns has pivots, i.e. rank .
from sympy import Matrix
def spans_rn(vectors):
A = Matrix.hstack(*[Matrix(v) for v in vectors])
return A.rank() == A.rows
print(spans_rn([[1, 2], [2, 4]]))
print(spans_rn([[1, 2], [2, 4], [0, 1]]))False
True
Test yourself
What is in ?
- The -axis
- Just the two vectors
- All of
- The -plane
Four vectors in are given. Can they span ?
- Yes, always
- Yes, if no two are parallel
- No, never
- Only if one is zero
When reducing , what shows that is NOT in the span of 's columns?
- A zero row
- A pivot in the last () column
- A free variable
- A pivot in the first column