TExcelTAD
Home › Linear Algebra & Matrix Theory › Basis and Dimension

Basis and Dimension

A basis of a subspace is a set of vectors that spans it with no redundancy. Every vector then has unique coordinates with respect to the basis, and the number of basis vectors, the dimension, does not depend on which basis you pick.

Basis

A set {v1,…,vk}\{\mathbf{v}_1, \ldots, \mathbf{v}_k\} is a basis of a subspace VV of Rn\mathbb{R}^n if

  1. it spans VV (Spans), and
  2. it is linearly independent (Linear Independence).

Spanning means "enough vectors to reach everything"; independence means "no vector too many". The standard basis of Rn\mathbb{R}^n is e1=[1,0,…,0]\mathbf{e}_1 = [1, 0, \ldots, 0], e2=[0,1,0,…,0]\mathbf{e}_2 = [0, 1, 0, \ldots, 0], …\ldots, en=[0,…,0,1]\mathbf{e}_n = [0, \ldots, 0, 1], but Rn\mathbb{R}^n has infinitely many other bases.

Coordinates. If B={v1,…,vk}B = \{\mathbf{v}_1, \ldots, \mathbf{v}_k\} is a basis of VV, every w∈V\mathbf{w} \in V can be written as w=c1v1+⋯+ckvk\mathbf{w} = c_1\mathbf{v}_1 + \cdots + c_k\mathbf{v}_k in exactly one way (spanning gives existence, independence gives uniqueness). The numbers [c1,…,ck][c_1, \ldots, c_k] are the coordinates of w\mathbf{w} relative to BB, written [w]B[\mathbf{w}]_B.

Dimension

Theorem (exchange lemma). If a subspace VV is spanned by mm vectors, then any independent set in VV has at most mm vectors.

Sketch: put mm spanning vectors as columns of SS and kk independent vectors u1,…,uk\mathbf{u}_1, \ldots, \mathbf{u}_k of VV as columns of UU. Each uj\mathbf{u}_j is a combination of the spanning vectors, so U=SCU = SC for some m×km \times k matrix CC. If k>mk > m, the homogeneous system Cx=0C\mathbf{x} = \mathbf{0} has a non-zero solution (more unknowns than equations), and then Ux=SCx=0U\mathbf{x} = SC\mathbf{x} = \mathbf{0} contradicts independence. So k≤mk \le m.

Applying this both ways to two bases shows they have the same number of elements. That common number is the dimension dim⁡V\dim V. Examples: dim⁡Rn=n\dim \mathbb{R}^n = n, a line through 0\mathbf{0} has dimension 1, a plane through 0\mathbf{0} dimension 2, and dim⁡{0}=0\dim\{\mathbf{0}\} = 0.

Useful consequences in a subspace VV of dimension dd:

Statement
any dd independent vectors in VV form a basis no need to check spanning
any dd vectors that span VV form a basis no need to check independence
more than dd vectors in VV are dependent
fewer than dd vectors cannot span VV
a subspace W⊆VW \subseteq V has dim⁡W≤d\dim W \le d, with equality only if W=VW = V

Finding a basis of a span

To get a basis of span⁡{v1,…,vk}\operatorname{span}\{\mathbf{v}_1, \ldots, \mathbf{v}_k\}, put the vectors as columns of AA and row-reduce. The original columns of AA in the pivot positions form a basis. (Use the original vectors, not the columns of the reduced matrix; row operations change the span of the columns.)

python · try it yourself
from sympy import Matrix

vs = [Matrix([1, 2, 0, 1]), Matrix([2, 4, 0, 2]), Matrix([0, 1, 1, 0]),
      Matrix([1, 3, 1, 1]), Matrix([0, 0, 1, 5])]
A = Matrix.hstack(*vs)
R, piv = A.rref()
print("pivot columns:", piv)
basis = [vs[j] for j in piv]
print("basis:", [list(b) for b in basis])
print("dimension of the span:", len(basis))
pivot columns: (0, 2, 4)
basis: [[1, 2, 0, 1], [0, 1, 1, 0], [0, 0, 1, 5]]
dimension of the span: 3

v2=2v1\mathbf{v}_2 = 2\mathbf{v}_1 and v4=v1+v3\mathbf{v}_4 = \mathbf{v}_1 + \mathbf{v}_3 were the redundant ones.

Coordinates relative to a basis

To find [w]B[\mathbf{w}]_B, solve the system Bc=wB\mathbf{c} = \mathbf{w}, where the basis vectors are the columns of BB. In Rn\mathbb{R}^n with an nn-vector basis, BB is square and invertible:

python · try it yourself
from sympy import Matrix

b1, b2 = Matrix([2, 1]), Matrix([-1, 1])
B = Matrix.hstack(b1, b2)
w = Matrix([4, 5])
c = B.solve(w)
print("coordinates of w in basis {b1, b2}:", list(c))
print("check:", list(c[0] * b1 + c[1] * b2))
coordinates of w in basis {b1, b2}: [3, 2]
check: [4, 5]

So w=[4,5]\mathbf{w} = [4, 5] in the standard basis is [3,2][3, 2] in the basis {b1,b2}\{\mathbf{b}_1, \mathbf{b}_2\}: same vector, different coordinates.

In software

  • Local axes in games. An object's orientation is a basis: its right, up and forward vectors. The world position of a point with local coordinates [x,y,z][x, y, z] is x⋅right+y⋅up+z⋅forwardx \cdot \text{right} + y \cdot \text{up} + z \cdot \text{forward} (plus the object's origin). Going back from world to local is solving Bc=wB\mathbf{c} = \mathbf{w}; for an orthonormal basis that is just three dot products (Local, World and View Space).
  • Embedding dimension. An LLM embedding with 4096 coordinates lives in R4096\mathbb{R}^{4096}, but the vectors a model actually produces often cluster near a much lower-dimensional subspace, which is why dimensionality reduction loses little (Embeddings and Cosine Similarity).
  • PCA chooses a better basis. Principal Component Analysis replaces the original feature axes by an orthonormal basis ordered by variance, then keeps the first few coordinates (PCA).
  • Compression. JPEG rewrites 8×8 pixel blocks in a cosine basis where most coordinates are tiny and can be dropped.

Exercises

Exercise 1: Is it a basis of R3\mathbb{R}^3?

Is {[1,0,1],[1,1,0],[0,1,1]}\{[1, 0, 1], [1, 1, 0], [0, 1, 1]\} a basis of R3\mathbb{R}^3? Is {[1,2,3],[4,5,6],[7,8,9]}\{[1, 2, 3], [4, 5, 6], [7, 8, 9]\}?

Answer

The first is a basis; the second is not.

Solution

Three vectors in R3\mathbb{R}^3 form a basis exactly when they are independent (rank 3).

python · try it yourself
from sympy import Matrix
print(Matrix([[1, 1, 0], [0, 1, 1], [1, 0, 1]]).rank())
print(Matrix([[1, 4, 7], [2, 5, 8], [3, 6, 9]]).rank())
3
2

In the second set, [7,8,9]=2⋅[4,5,6]−[1,2,3][7, 8, 9] = 2 \cdot [4, 5, 6] - [1, 2, 3].

Exercise 2: Dimension of a plane

Find a basis and the dimension of the subspace of R4\mathbb{R}^4 given by x1+x2+x3+x4=0x_1 + x_2 + x_3 + x_4 = 0.

Answer

Basis {[−1,1,0,0],[−1,0,1,0],[−1,0,0,1]}\{[-1, 1, 0, 0], [-1, 0, 1, 0], [-1, 0, 0, 1]\}; dimension 3.

Solution

One equation in four unknowns: x1x_1 is a pivot, x2,x3,x4x_2, x_3, x_4 are free. x1=−x2−x3−x4x_1 = -x_2 - x_3 - x_4 gives

[x1,x2,x3,x4]=x2[−1,1,0,0]+x3[−1,0,1,0]+x4[−1,0,0,1][x_1, x_2, x_3, x_4] = x_2[-1, 1, 0, 0] + x_3[-1, 0, 1, 0] + x_4[-1, 0, 0, 1]

The three vectors are independent (each has a 1 where the others have 0), so the dimension is 3.

Exercise 3: Coordinates

Find the coordinates of w=[1,7,5]\mathbf{w} = [1, 7, 5] with respect to the basis {[1,1,0],[0,1,1],[1,0,1]}\{[1, 1, 0], [0, 1, 1], [1, 0, 1]\}.

Answer

[w]B=[32,112,−12][\mathbf{w}]_B = \left[\frac{3}{2}, \frac{11}{2}, -\frac{1}{2}\right].

Solution
c1+c3=1,c1+c2=7,c2+c3=5add all: 2(c1+c2+c3)=13→c1+c2+c3=132c3=132−7=−12,c1=132−5=32,c2=132−1=112\begin{aligned} &c_1 + c_3 = 1, \quad c_1 + c_2 = 7, \quad c_2 + c_3 = 5 \\ &\text{add all: } 2(c_1 + c_2 + c_3) = 13 \to c_1 + c_2 + c_3 = \tfrac{13}{2} \\ &c_3 = \tfrac{13}{2} - 7 = -\tfrac{1}{2}, \quad c_1 = \tfrac{13}{2} - 5 = \tfrac{3}{2}, \quad c_2 = \tfrac{13}{2} - 1 = \tfrac{11}{2} \end{aligned}
python · try it yourself
from sympy import Matrix
B = Matrix([[1, 0, 1], [1, 1, 0], [0, 1, 1]])
print(list(B.solve(Matrix([1, 7, 5]))))
[3/2, 11/2, -1/2]

Exercise 4: Extend to a basis

Extend {[1,1,0,0],[0,0,1,1]}\{[1, 1, 0, 0], [0, 0, 1, 1]\} to a basis of R4\mathbb{R}^4 by adding standard basis vectors. (Hint: reduce [v1 v2 e1 e2 e3 e4][\mathbf{v}_1\ \mathbf{v}_2\ \mathbf{e}_1\ \mathbf{e}_2\ \mathbf{e}_3\ \mathbf{e}_4] and keep the pivot columns.)

Answer

Add e1\mathbf{e}_1 and e3\mathbf{e}_3: {[1,1,0,0],[0,0,1,1],e1,e3}\{[1, 1, 0, 0], [0, 0, 1, 1], \mathbf{e}_1, \mathbf{e}_3\}.

Solution

The pivot columns of the 4×6 matrix are the first two (the given vectors) plus the first standard vectors that add something new.

python · try it yourself
from sympy import Matrix, eye
A = Matrix.hstack(Matrix([1, 1, 0, 0]), Matrix([0, 0, 1, 1]), eye(4))
print(A.rref()[1])
(0, 1, 2, 4)

Columns 2 and 4 are e1\mathbf{e}_1 and e3\mathbf{e}_3.

Test yourself

What is the dimension of span⁡{[1,2],[2,4],[3,6]}\operatorname{span}\{[1, 2], [2, 4], [3, 6]\}?

  1. 0
  2. 1
  3. 2
  4. 3

To get a basis of the span of AA's columns, which vectors do you take?

  1. The original columns of AA in the pivot positions
  2. The pivot columns of rref⁡(A)\operatorname{rref}(A)
  3. The rows of rref⁡(A)\operatorname{rref}(A)
  4. All columns of AA

In a 5-dimensional subspace VV, five independent vectors are chosen. Do they form a basis of VV?

  1. Not necessarily
  2. Only if they are orthogonal
  3. Only if they include the standard basis
  4. Yes, automatically