TExcelTAD

Spans

The span of a set of vectors is everything you can build from them with linear combinations. Asking "is w\mathbf{w} 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 v1,…,vk\mathbf{v}_1, \ldots, \mathbf{v}_k in Rn\mathbb{R}^n is the set of all their linear combinations:

span⁡{v1,…,vk}={ c1v1+c2v2+⋯+ckvk  :  c1,…,ck∈R }\operatorname{span}\{\mathbf{v}_1, \ldots, \mathbf{v}_k\} = \{\, c_1\mathbf{v}_1 + c_2\mathbf{v}_2 + \cdots + c_k\mathbf{v}_k \;:\; c_1, \ldots, c_k \in \mathbb{R} \,\}

By convention span⁡{}={0}\operatorname{span}\{\} = \{\mathbf{0}\} (no vectors). The span always contains 0\mathbf{0} (take all ci=0c_i = 0) and each vi\mathbf{v}_i itself.

Geometrically in R3\mathbb{R}^3:

Vectors Span
one non-zero vector v\mathbf{v} the line through 0\mathbf{0} in direction v\mathbf{v}
two non-parallel vectors the plane through 0\mathbf{0} containing both
two parallel vectors still just a line
three vectors not in a common plane all of R3\mathbb{R}^3

Spans always pass through the origin: the line x=1+tx = 1 + t is not a span.

Is w\mathbf{w} in the span? Solve a system

w∈span⁡{v1,…,vk}\mathbf{w} \in \operatorname{span}\{\mathbf{v}_1, \ldots, \mathbf{v}_k\} means some c1,…,ckc_1, \ldots, c_k satisfy c1v1+⋯+ckvk=wc_1\mathbf{v}_1 + \cdots + c_k\mathbf{v}_k = \mathbf{w}. Put the vi\mathbf{v}_i as columns of a matrix AA; the question becomes whether Ac=wA\mathbf{c} = \mathbf{w} is consistent. Row-reduce the augmented matrix [A∣w][A \mid \mathbf{w}]: w\mathbf{w} is in the span exactly when there is no pivot in the last column.

python · try it yourself
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: 2⋅[1,2,0]+3⋅[0,1,1]=[2,7,3]2 \cdot [1, 2, 0] + 3 \cdot [0, 1, 1] = [2, 7, 3]. ✓

When do vectors span all of Rn\mathbb{R}^n?

span⁡{v1,…,vk}=Rn\operatorname{span}\{\mathbf{v}_1, \ldots, \mathbf{v}_k\} = \mathbb{R}^n exactly when Ac=wA\mathbf{c} = \mathbf{w} is consistent for every w\mathbf{w}, which happens exactly when the rref of AA (without any augmented column) has a pivot in every row. Consequences:

  • You need at least nn vectors to span Rn\mathbb{R}^n: kk vectors give at most kk pivots.
  • Adding a vector that is already in the span does not make the span bigger.
python · try it yourself
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 v3=2v1+3v2\mathbf{v}_3 = 2\mathbf{v}_1 + 3\mathbf{v}_2 adds nothing new. That idea is made precise in Linear Independence.

Picture: a span in R2\mathbb{R}^2

Integer combinations of two non-parallel vectors form a slanted grid. Allowing all real coefficients fills in the gaps and covers the whole plane:

python · try it yourself
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 [0,1][0, 1]. That is a bounded piece of span⁡{R,G,B}\operatorname{span}\{R, G, B\}; 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 WW can only output vectors in the span of WW'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 span⁡{[1,−2,3],[−2,4,−6]}\operatorname{span}\{[1, -2, 3], [-2, 4, -6]\} in R3\mathbb{R}^3.

Answer

A line through the origin with direction [1,−2,3][1, -2, 3].

Solution

The second vector is −2-2 times the first, so every combination c1v1+c2v2=(c1−2c2)v1c_1\mathbf{v}_1 + c_2\mathbf{v}_2 = (c_1 - 2c_2)\mathbf{v}_1 is a multiple of v1\mathbf{v}_1. The span is the line {t⋅[1,−2,3]}\{t \cdot [1, -2, 3]\}.

Exercise 2: Membership by hand

Is w=[4,1,9]\mathbf{w} = [4, 1, 9] in span⁡{[1,0,2],[1,1,1]}\operatorname{span}\{[1, 0, 2], [1, 1, 1]\}? If so, find the coefficients; if not, show why.

Answer

No. The first two coordinates force c1=3c_1 = 3, c2=1c_2 = 1, and then the third coordinate is 7, not 9.

Solution
c1+c2=4,c2=1,2c1+c2=9→c2=1, c1=3,check 2⋅3+1=7≠9✗\begin{aligned} &c_1 + c_2 = 4, \quad c_2 = 1, \quad 2c_1 + c_2 = 9 \\ &\to c_2 = 1,\ c_1 = 3, \quad \text{check } 2 \cdot 3 + 1 = 7 \ne 9 \quad \text{✗} \end{aligned}

The third equation fails, so w\mathbf{w} is not in the span. Row reduction confirms it:

python · try it yourself
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 w\mathbf{w} column) means the system is inconsistent.

Exercise 3: Find the missing value

For which hh is [3,h,5][3, h, 5] in span⁡{[1,1,1],[1,2,3]}\operatorname{span}\{[1, 1, 1], [1, 2, 3]\}?

Answer

h=4h = 4.

Solution
c1+c2=3,c1+2c2=h,c1+3c2=5from eq 1 and eq 3: 2c2=2→c2=1, c1=2eq 2: h=2+2=4\begin{aligned} &c_1 + c_2 = 3, \quad c_1 + 2c_2 = h, \quad c_1 + 3c_2 = 5 \\ &\text{from eq 1 and eq 3: } 2c_2 = 2 \to c_2 = 1,\ c_1 = 2 \\ &\text{eq 2: } h = 2 + 2 = 4 \end{aligned}

Exercise 4: Spanning test in code

Write a function spans_rn(vectors) that returns True when the given vectors span Rn\mathbb{R}^n (nn = their length), using the rank. Test it on {[1,2],[2,4]}\{[1, 2], [2, 4]\} and {[1,2],[2,4],[0,1]}\{[1, 2], [2, 4], [0, 1]\}.

Answer

False for the first set, True for the second.

Solution

The vectors span Rn\mathbb{R}^n exactly when the matrix with them as columns has nn pivots, i.e. rank nn.

python · try it yourself
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 span⁡{[1,0,0],[0,1,0]}\operatorname{span}\{[1, 0, 0], [0, 1, 0]\} in R3\mathbb{R}^3?

  1. The xx-axis
  2. Just the two vectors
  3. All of R3\mathbb{R}^3
  4. The xyxy-plane

Four vectors in R5\mathbb{R}^5 are given. Can they span R5\mathbb{R}^5?

  1. Yes, always
  2. Yes, if no two are parallel
  3. No, never
  4. Only if one is zero

When reducing [A∣w][A \mid \mathbf{w}], what shows that w\mathbf{w} is NOT in the span of AA's columns?

  1. A zero row
  2. A pivot in the last (w\mathbf{w}) column
  3. A free variable
  4. A pivot in the first column