← Math|20 of 100
Linear Algebra

Basis and Dimension

Master basis, dimension, change of basis, coordinate systems, rank, null space, and column space with comprehensive examples and Python implementations.

📂 Vector Spaces📖 Lesson 20 of 100🎓 Free Course

Advertisement

Basis and Dimension


â„šī¸ Why It Matters

Basis and dimension are foundational concepts in linear algebra that determine how we represent, compress, and transform data. Every data point in machine learning exists in a high-dimensional vector space, and choosing the right basis — the right coordinate system — can reveal hidden structure, reduce computational cost, and improve model performance. PCA finds the basis of maximum variance; Fourier transforms use frequency basis to decompose signals; wavelet basis enables multi-resolution analysis. Understanding basis and dimension is essential for feature engineering, dimensionality reduction, and efficient computation in AI/ML pipelines.


Vector Space Axioms

DfVector Space

A vector space over a field F\mathbb{F} (typically R\mathbb{R} or C\mathbb{C}) is a set VV together with two operations — vector addition (++) and scalar multiplication (⋅\cdot) — satisfying the following eight axioms for all u⃗,v⃗,w⃗∈V\vec{u}, \vec{v}, \vec{w} \in V and all a,b∈Fa, b \in \mathbb{F}:

  1. Closure under addition: u⃗+v⃗∈V\vec{u} + \vec{v} \in V
  2. Commutativity: u⃗+v⃗=v⃗+u⃗\vec{u} + \vec{v} = \vec{v} + \vec{u}
  3. Associativity of addition: (u⃗+v⃗)+w⃗=u⃗+(v⃗+w⃗)(\vec{u} + \vec{v}) + \vec{w} = \vec{u} + (\vec{v} + \vec{w})
  4. Additive identity: There exists 0⃗∈V\vec{0} \in V such that v⃗+0⃗=v⃗\vec{v} + \vec{0} = \vec{v}
  5. Additive inverse: For each v⃗∈V\vec{v} \in V, there exists −v⃗∈V-\vec{v} \in V such that v⃗+(−v⃗)=0⃗\vec{v} + (-\vec{v}) = \vec{0}
  6. Closure under scalar multiplication: a⋅v⃗∈Va \cdot \vec{v} \in V
  7. Distributivity: a⋅(u⃗+v⃗)=a⋅u⃗+a⋅v⃗a \cdot (\vec{u} + \vec{v}) = a \cdot \vec{u} + a \cdot \vec{v} and (a+b)⋅v⃗=a⋅v⃗+b⋅v⃗(a + b) \cdot \vec{v} = a \cdot \vec{v} + b \cdot \vec{v}
  8. Compatibility of scalar multiplication: a⋅(b⋅v⃗)=(ab)⋅v⃗a \cdot (b \cdot \vec{v}) = (ab) \cdot \vec{v}, and 1⋅v⃗=v⃗1 \cdot \vec{v} = \vec{v}

📝Examples of Vector Spaces

Rn\mathbb{R}^n: The set of all nn-tuples of real numbers with componentwise addition and scalar multiplication. This is the canonical example.

Polynomials of degree ≤n\leq n: The set Pn={a0+a1x+⋯+anxn:ai∈R}P_n = \{a_0 + a_1 x + \cdots + a_n x^n : a_i \in \mathbb{R}\} with polynomial addition and scalar multiplication.

Matrices: The set Mm×n(R)M_{m \times n}(\mathbb{R}) of all m×nm \times n real matrices with matrix addition and scalar multiplication.

Function spaces: The set of all continuous functions C[0,1]C[0,1] with pointwise addition and scalar multiplication: (f+g)(x)=f(x)+g(x)(f+g)(x) = f(x) + g(x), (cf)(x)=c⋅f(x)(cf)(x) = c \cdot f(x).

📝Non-Example

The set {(x,y)∈R2:xâ‰Ĩ0,yâ‰Ĩ0}\{(x, y) \in \mathbb{R}^2 : x \geq 0, y \geq 0\} (first quadrant) is not a vector space because it is not closed under additive inverses — e.g., (1,1)∈V(1,1) \in V but −(1,1)=(−1,−1)∉V-(1,1) = (-1,-1) \notin V.


Subspace

DfSubspace

A subspace of a vector space VV is a non-empty subset W⊆VW \subseteq V that is itself a vector space under the same operations. Equivalently, WW is a subspace if and only if:

  1. 0⃗∈W\vec{0} \in W (contains the zero vector)
  2. If u⃗,v⃗∈W\vec{u}, \vec{v} \in W, then u⃗+v⃗∈W\vec{u} + \vec{v} \in W (closed under addition)
  3. If v⃗∈W\vec{v} \in W and c∈Rc \in \mathbb{R}, then cv⃗∈Wc\vec{v} \in W (closed under scalar multiplication)

📝Examples of Subspaces

Line through the origin: W={tv⃗:t∈R}W = \{t\vec{v} : t \in \mathbb{R}\} for any fixed v⃗∈Rn\vec{v} \in \mathbb{R}^n.

The set of symmetric matrices: W={A∈Mn×n(R):A=AT}W = \{A \in M_{n \times n}(\mathbb{R}) : A = A^T\} is a subspace of Mn×n(R)M_{n \times n}(\mathbb{R}).

The set of solutions to Ax⃗=0⃗A\vec{x} = \vec{0}: The null space ker⁡(A)\ker(A) is always a subspace of Rn\mathbb{R}^n.

📝Non-Subspace

The set {(x,y)∈R2:x+y=1}\{(x, y) \in \mathbb{R}^2 : x + y = 1\} is not a subspace because 0⃗∉W\vec{0} \notin W (since 0+0≠10 + 0 \neq 1).


Linear Combinations

DfLinear Combination

A linear combination of vectors v1⃗,v2⃗,â€Ļ,vk⃗\vec{v_1}, \vec{v_2}, \ldots, \vec{v_k} is any expression of the form:

c1v1⃗+c2v2⃗+⋯+ckvk⃗c_1 \vec{v_1} + c_2 \vec{v_2} + \cdots + c_k \vec{v_k}

where c1,c2,â€Ļ,ckc_1, c_2, \ldots, c_k are scalars (called weights or coefficients).

📝Example

Let v1⃗=[10]\vec{v_1} = \begin{bmatrix}1\\0\end{bmatrix}, v2⃗=[01]\vec{v_2} = \begin{bmatrix}0\\1\end{bmatrix}.

Then [3−2]=3v1⃗+(−2)v2⃗\begin{bmatrix}3\\-2\end{bmatrix} = 3\vec{v_1} + (-2)\vec{v_2} is a linear combination of v1⃗\vec{v_1} and v2⃗\vec{v_2}.

The vector [111]\begin{bmatrix}1\\1\\1\end{bmatrix} cannot be written as a linear combination of v1⃗\vec{v_1} and v2⃗\vec{v_2} because they live in R2\mathbb{R}^2, not R3\mathbb{R}^3.

â„šī¸ Geometric Intuition

In R2\mathbb{R}^2, the set of all linear combinations of two non-parallel vectors is the entire plane. If the vectors are parallel (scalar multiples of each other), their linear combinations form only a line through the origin.


Span

DfSpan

The span of a set of vectors {v1⃗,v2⃗,â€Ļ,vk⃗}\{\vec{v_1}, \vec{v_2}, \ldots, \vec{v_k}\} is the set of all linear combinations:

span{v1⃗,v2⃗,â€Ļ,vk⃗}={c1v1⃗+c2v2⃗+⋯+ckvk⃗:ci∈R}\text{span}\{\vec{v_1}, \vec{v_2}, \ldots, \vec{v_k}\} = \{c_1 \vec{v_1} + c_2 \vec{v_2} + \cdots + c_k \vec{v_k} : c_i \in \mathbb{R}\}

The span is always a subspace — it is the smallest subspace containing all the given vectors.

📝Finding the Span

Let v1⃗=[123]\vec{v_1} = \begin{bmatrix}1\\2\\3\end{bmatrix}, v2⃗=[456]\vec{v_2} = \begin{bmatrix}4\\5\\6\end{bmatrix}.

span{v1⃗,v2⃗}\text{span}\{\vec{v_1}, \vec{v_2}\} is the plane in R3\mathbb{R}^3 passing through the origin and containing both vectors. Since v1⃗\vec{v_1} and v2⃗\vec{v_2} are not parallel, this is a 2-dimensional subspace of R3\mathbb{R}^3.

📝Span of Standard Basis

span{e1⃗,e2⃗,e3⃗}\text{span}\{\vec{e_1}, \vec{e_2}, \vec{e_3}\} in R3\mathbb{R}^3 is all of R3\mathbb{R}^3, since any vector [abc]=ae1⃗+be2⃗+ce3⃗\begin{bmatrix}a\\b\\c\end{bmatrix} = a\vec{e_1} + b\vec{e_2} + c\vec{e_3}.


Linear Independence

DfLinear Independence

A set of vectors {v1⃗,v2⃗,â€Ļ,vk⃗}\{\vec{v_1}, \vec{v_2}, \ldots, \vec{v_k}\} is linearly independent if the only solution to:

c1v1⃗+c2v2⃗+⋯+ckvk⃗=0⃗c_1 \vec{v_1} + c_2 \vec{v_2} + \cdots + c_k \vec{v_k} = \vec{0}

is c1=c2=⋯=ck=0c_1 = c_2 = \cdots = c_k = 0.

If a non-trivial solution exists (some ci≠0c_i \neq 0), the set is linearly dependent.

ThTest for Linear Independence

A set of vectors {v1⃗,v2⃗,â€Ļ,vk⃗}\{\vec{v_1}, \vec{v_2}, \ldots, \vec{v_k}\} in Rn\mathbb{R}^n is linearly independent if and only if the matrix A=[v1âƒ—âˆŖv2âƒ—âˆŖâ‹¯âˆŖvk⃗]A = [\vec{v_1} \mid \vec{v_2} \mid \cdots \mid \vec{v_k}] has a pivot in every column (i.e., rank(A)=k\text{rank}(A) = k).

📝Linearly Independent

{[100],[010],[001]}\left\{\begin{bmatrix}1\\0\\0\end{bmatrix}, \begin{bmatrix}0\\1\\0\end{bmatrix}, \begin{bmatrix}0\\0\\1\end{bmatrix}\right\} is linearly independent because:

c1[100]+c2[010]+c3[001]=[c1c2c3]=[000]c_1 \begin{bmatrix}1\\0\\0\end{bmatrix} + c_2 \begin{bmatrix}0\\1\\0\end{bmatrix} + c_3 \begin{bmatrix}0\\0\\1\end{bmatrix} = \begin{bmatrix}c_1\\c_2\\c_3\end{bmatrix} = \begin{bmatrix}0\\0\\0\end{bmatrix}

implies c1=c2=c3=0c_1 = c_2 = c_3 = 0.

📝Linearly Dependent

{[12],[24]}\left\{\begin{bmatrix}1\\2\end{bmatrix}, \begin{bmatrix}2\\4\end{bmatrix}\right\} is linearly dependent because [24]=2⋅[12]\begin{bmatrix}2\\4\end{bmatrix} = 2 \cdot \begin{bmatrix}1\\2\end{bmatrix}, so 2v1⃗−v2⃗=0⃗2\vec{v_1} - \vec{v_2} = \vec{0} with non-zero coefficients.

ThKey Properties

  • Any set containing the zero vector is linearly dependent.
  • A set of kk vectors in Rn\mathbb{R}^n with k>nk > n is always linearly dependent.
  • A set with one vector is linearly independent if and only if that vector is non-zero.

Basis

DfBasis

A basis for a vector space VV is a set of vectors {v1⃗,v2⃗,â€Ļ,vn⃗}\{\vec{v_1}, \vec{v_2}, \ldots, \vec{v_n}\} that is:

  1. Linearly independent, and
  2. Spans VV (i.e., span{v1⃗,â€Ļ,vn⃗}=V\text{span}\{\vec{v_1}, \ldots, \vec{v_n}\} = V)

In other words, a basis is a minimal spanning set and a maximal linearly independent set.

📝Standard Basis

For Rn\mathbb{R}^n, the standard basis is:

e1⃗=[10⋮0],e2⃗=[01⋮0],â€Ļ,en⃗=[00⋮1]\vec{e_1} = \begin{bmatrix}1\\0\\\vdots\\0\end{bmatrix}, \vec{e_2} = \begin{bmatrix}0\\1\\\vdots\\0\end{bmatrix}, \ldots, \vec{e_n} = \begin{bmatrix}0\\0\\\vdots\\1\end{bmatrix}

For R2\mathbb{R}^2: e1⃗=[10]\vec{e_1} = \begin{bmatrix}1\\0\end{bmatrix}, e2⃗=[01]\vec{e_2} = \begin{bmatrix}0\\1\end{bmatrix}.

Every vector x⃗=[x1x2⋮xn]\vec{x} = \begin{bmatrix}x_1\\x_2\\\vdots\\x_n\end{bmatrix} can be uniquely written as x⃗=x1e1⃗+x2e2⃗+⋯+xnen⃗\vec{x} = x_1\vec{e_1} + x_2\vec{e_2} + \cdots + x_n\vec{e_n}.

📝Non-Standard Basis for R2

The vectors v1⃗=[11]\vec{v_1} = \begin{bmatrix}1\\1\end{bmatrix} and v2⃗=[1−1]\vec{v_2} = \begin{bmatrix}1\\-1\end{bmatrix} also form a basis for R2\mathbb{R}^2 because:

  • They are linearly independent (not scalar multiples).
  • They span R2\mathbb{R}^2: [ab]=a+b2v1⃗+a−b2v2⃗\begin{bmatrix}a\\b\end{bmatrix} = \frac{a+b}{2}\vec{v_1} + \frac{a-b}{2}\vec{v_2}.

âš ī¸ Uniqueness

A basis is not unique — any vector space has infinitely many bases. However, all bases for a given vector space have the same number of vectors (this is the dimension).


Dimension

DfDimension

The dimension of a vector space VV, denoted dim⁥(V)\dim(V), is the number of vectors in any basis for VV.

ThDimension Theorems

  1. dim⁥(Rn)=n\dim(\mathbb{R}^n) = n.
  2. If VV has a basis with nn vectors, then every basis for VV has exactly nn vectors.
  3. If dim⁥(V)=n\dim(V) = n, then any set of more than nn vectors in VV is linearly dependent.
  4. If dim⁥(V)=n\dim(V) = n, then any set of fewer than nn vectors cannot span VV.
  5. If dim⁥(V)=n\dim(V) = n, then any linearly independent set of nn vectors is automatically a basis.
  6. If dim⁥(V)=n\dim(V) = n, then any spanning set of nn vectors is automatically a basis.

📝Dimension Examples

  • dim⁥(R3)=3\dim(\mathbb{R}^3) = 3. Standard basis: {e1⃗,e2⃗,e3⃗}\{\vec{e_1}, \vec{e_2}, \vec{e_3}\}.
  • dim⁥(P2)=3\dim(P_2) = 3 (polynomials of degree ≤2\leq 2). Basis: {1,x,x2}\{1, x, x^2\}.
  • dim⁥(M2×2(R))=4\dim(M_{2 \times 2}(\mathbb{R})) = 4. Basis: {[1000],[0100],[0010],[0001]}\left\{\begin{bmatrix}1&0\\0&0\end{bmatrix}, \begin{bmatrix}0&1\\0&0\end{bmatrix}, \begin{bmatrix}0&0\\1&0\end{bmatrix}, \begin{bmatrix}0&0\\0&1\end{bmatrix}\right\}.
  • The subspace span{[123],[456]}\text{span}\left\{\begin{bmatrix}1\\2\\3\end{bmatrix}, \begin{bmatrix}4\\5\\6\end{bmatrix}\right\} in R3\mathbb{R}^3 has dimension 2 (a plane through the origin).

Change of Basis

DfChange of Basis

Let B={v1⃗,â€Ļ,vn⃗}B = \{\vec{v_1}, \ldots, \vec{v_n}\} and B′={w1⃗,â€Ļ,wn⃗}B' = \{\vec{w_1}, \ldots, \vec{w_n}\} be two bases for Rn\mathbb{R}^n. The change-of-basis matrix from BB to B′B' is:

PB′←B=[[v1⃗]B′[v2⃗]Bâ€˛â‹¯[vn⃗]B′]P_{B' \leftarrow B} = \begin{bmatrix} [\vec{v_1}]_{B'} & [\vec{v_2}]_{B'} & \cdots & [\vec{v_n}]_{B'} \end{bmatrix}

Then for any vector x⃗\vec{x}:

[x⃗]B′=PB′←B⋅[x⃗]B[\vec{x}]_{B'} = P_{B' \leftarrow B} \cdot [\vec{x}]_B

📝Change of Basis Example

Let B={[11],[1−1]}B = \left\{\begin{bmatrix}1\\1\end{bmatrix}, \begin{bmatrix}1\\-1\end{bmatrix}\right\} and B′={[10],[01]}B' = \left\{\begin{bmatrix}1\\0\end{bmatrix}, \begin{bmatrix}0\\1\end{bmatrix}\right\} (standard basis).

To find PB′←BP_{B' \leftarrow B}, express each vector of BB in the standard basis:

PB′←B=[111−1]P_{B' \leftarrow B} = \begin{bmatrix}1&1\\1&-1\end{bmatrix}

For x⃗=[31]\vec{x} = \begin{bmatrix}3\\1\end{bmatrix} in the standard basis:

[x⃗]B=PB′←B−1x⃗=1−2[−1−1−11][31]=[21][\vec{x}]_B = P_{B' \leftarrow B}^{-1} \vec{x} = \frac{1}{-2}\begin{bmatrix}-1&-1\\-1&1\end{bmatrix}\begin{bmatrix}3\\1\end{bmatrix} = \begin{bmatrix}2\\1\end{bmatrix}

Verification: 2[11]+1[1−1]=[31]2\begin{bmatrix}1\\1\end{bmatrix} + 1\begin{bmatrix}1\\-1\end{bmatrix} = \begin{bmatrix}3\\1\end{bmatrix} ✓

import numpy as np

# Change of basis
B = np.array([[1, 1], [1, -1]])  # New basis vectors as columns
B_inv = np.linalg.inv(B)

# Vector in standard basis
x_standard = np.array([3, 1])

# Coordinates in basis B
x_in_B = B_inv @ x_standard
print(f"Coordinates in new basis: {x_in_B}")  # [2. 1.]

# Verify: reconstruct from new coordinates
x_reconstructed = B @ x_in_B
print(f"Reconstructed: {x_reconstructed}")  # [3. 1.]

Rank

DfRank

The rank of a matrix AA, denoted rank(A)\text{rank}(A), is the dimension of the column space (equivalently, the row space):

rank(A)=dim⁥(col(A))=dim⁥(row(A))\text{rank}(A) = \dim(\text{col}(A)) = \dim(\text{row}(A))

It equals the number of pivot positions in the row echelon form of AA.

ThRank-Nullity Theorem

For an m×nm \times n matrix AA:

rank(A)+nullity(A)=n\text{rank}(A) + \text{nullity}(A) = n

where nullity(A)=dim⁥(ker⁥(A))\text{nullity}(A) = \dim(\ker(A)) is the dimension of the null space.

📝Computing Rank

Let A=[123456789]A = \begin{bmatrix}1&2&3\\4&5&6\\7&8&9\end{bmatrix}.

Row reduce: [123456789]→R2−4R1,R3−7R1[1230−3−60−6−12]→R3−2R2[1230−3−6000]\begin{bmatrix}1&2&3\\4&5&6\\7&8&9\end{bmatrix} \xrightarrow{R_2 - 4R_1, R_3 - 7R_1} \begin{bmatrix}1&2&3\\0&-3&-6\\0&-6&-12\end{bmatrix} \xrightarrow{R_3 - 2R_2} \begin{bmatrix}1&2&3\\0&-3&-6\\0&0&0\end{bmatrix}

Two pivots, so rank(A)=2\text{rank}(A) = 2. By rank-nullity: nullity(A)=3−2=1\text{nullity}(A) = 3 - 2 = 1.


Null Space

DfNull Space

The null space (or kernel) of an m×nm \times n matrix AA is the set of all solutions to Ax⃗=0⃗A\vec{x} = \vec{0}:

ker⁡(A)={x⃗∈Rn:Ax⃗=0⃗}\ker(A) = \{\vec{x} \in \mathbb{R}^n : A\vec{x} = \vec{0}\}

The null space is always a subspace of Rn\mathbb{R}^n.

📝Finding the Null Space

Let A=[120001000]A = \begin{bmatrix}1&2&0\\0&0&1\\0&0&0\end{bmatrix}.

Solve Ax⃗=0⃗A\vec{x} = \vec{0}:

  • Row 1: x1+2x2=0⇒x1=−2x2x_1 + 2x_2 = 0 \Rightarrow x_1 = -2x_2
  • Row 2: x3=0x_3 = 0
  • x2x_2 is free.
ker⁡(A)=span{[−210]}\ker(A) = \text{span}\left\{\begin{bmatrix}-2\\1\\0\end{bmatrix}\right\}

The null space is a line through the origin in R3\mathbb{R}^3 with dimension 1 (nullity = 1).

import numpy as np
from scipy.linalg import null_space

A = np.array([[1, 2, 0],
              [0, 0, 1],
              [0, 0, 0]])

ns = null_space(A)
print("Null space basis:")
print(ns)
# [[-0.89442719]
#  [ 0.4472136 ]
#  [ 0.        ]]

Column Space

DfColumn Space

The column space (or range) of an m×nm \times n matrix AA is the span of its columns:

col(A)={Ax⃗:x⃗∈Rn}=span{a1⃗,a2⃗,â€Ļ,an⃗}\text{col}(A) = \{A\vec{x} : \vec{x} \in \mathbb{R}^n\} = \text{span}\{\vec{a_1}, \vec{a_2}, \ldots, \vec{a_n}\}

The column space is a subspace of Rm\mathbb{R}^m.

ThConsistency of Linear Systems

The system Ax⃗=b⃗A\vec{x} = \vec{b} has a solution if and only if b⃗∈col(A)\vec{b} \in \text{col}(A).

📝Column Space and Ax = b

Let A=[100111]A = \begin{bmatrix}1&0\\0&1\\1&1\end{bmatrix}. The column space is the plane {[aba+b]:a,b∈R}\left\{\begin{bmatrix}a\\b\\a+b\end{bmatrix} : a, b \in \mathbb{R}\right\} in R3\mathbb{R}^3.

  • Ax⃗=[123]A\vec{x} = \begin{bmatrix}1\\2\\3\end{bmatrix}: Is [123]\begin{bmatrix}1\\2\\3\end{bmatrix} in col(A)\text{col}(A)? Yes, since 3=1+23 = 1 + 2. Solution: x⃗=[12]\vec{x} = \begin{bmatrix}1\\2\end{bmatrix}.

  • Ax⃗=[124]A\vec{x} = \begin{bmatrix}1\\2\\4\end{bmatrix}: Is [124]\begin{bmatrix}1\\2\\4\end{bmatrix} in col(A)\text{col}(A)? No, since 4≠1+24 \neq 1 + 2. No solution exists.

â„šī¸ Row Space vs Column Space

  • The row space of AA is the column space of ATA^T: row(A)=col(AT)\text{row}(A) = \text{col}(A^T).
  • rank(A)=rank(AT)\text{rank}(A) = \text{rank}(A^T) — row rank always equals column rank.
  • The null space is orthogonal to the row space: ker⁥(A)âŠĨrow(A)\ker(A) \perp \text{row}(A).

Python Implementation

import numpy as np
from scipy.linalg import null_space, orth

def analyze_vector_space(A):
    """Comprehensive analysis of a matrix's vector space properties."""
    m, n = A.shape
    
    print(f"Matrix A ({m}x{n}):")
    print(A)
    print()
    
    # Rank
    rank = np.linalg.matrix_rank(A)
    print(f"Rank: {rank}")
    
    # Nullity
    nullity = n - rank
    print(f"Nullity: {nullity}")
    print(f"Rank + Nullity = {rank} + {nullity} = {rank + nullity} = n ✓")
    
    # Null space
    ns = null_space(A)
    print(f"\nNull space basis ({nullity} vectors):")
    print(ns) if ns.size > 0 else print("  {0}")
    
    # Column space
    cs = orth(A)
    print(f"\nColumn space basis ({rank} vectors):")
    print(cs)
    
    # Check if Ax = b has solutions
    b = np.array([1, 2, 3])
    try:
        x, residuals, rank_check, sv = np.linalg.lstsq(A, b, rcond=None)
        if np.allclose(A @ x, b):
            print(f"\nAx = b has solution: x = {x}")
        else:
            print(f"\nAx = b has no solution (b not in column space)")
    except:
        print(f"\nAx = b has no solution")
    
    return rank, nullity

# Example
A = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

rank, nullity = analyze_vector_space(A)

print("\n" + "="*50)

# Finding a basis for the column space
def column_space_basis(A):
    """Find a basis for the column space using row reduction."""
    # Row reduce and find pivot columns
    U = np.array(A, dtype=float)
    pivot_cols = []
    row, col = 0, 0
    
    while row < U.shape[0] and col < U.shape[1]:
        # Find pivot
        max_row = np.argmax(np.abs(U[row:, col])) + row
        if U[max_row, col] == 0:
            col += 1
            continue
        
        # Swap rows
        U[[row, max_row]] = U[[max_row, row]]
        pivot_cols.append(col)
        
        # Eliminate below
        for i in range(row + 1, U.shape[0]):
            if U[i, col] != 0:
                U[i] -= U[i, col] / U[row, col] * U[row]
        
        row += 1
        col += 1
    
    return A[:, pivot_cols], pivot_cols

basis, pivots = column_space_basis(A)
print(f"Pivot columns: {pivots}")
print(f"Column space basis:")
print(basis)

# Linear independence check
def check_linear_independence(vectors):
    """Check if a set of vectors is linearly independent."""
    if vectors.shape[1] == 0:
        return True
    rank = np.linalg.matrix_rank(vectors)
    return rank == vectors.shape[1]

# Examples
v1 = np.array([[1], [0], [0]])
v2 = np.array([[0], [1], [0]])
v3 = np.array([[1], [1], [0]])
v4 = np.array([[1], [1], [1]])

print("\n{[1,0,0], [0,1,0]}:", check_linear_independence(np.hstack([v1, v2])))
print("{[1,0,0], [0,1,0], [1,1,0]}:", check_linear_independence(np.hstack([v1, v2, v3])))
print("{[1,0,0], [0,1,0], [1,1,1]}:", check_linear_independence(np.hstack([v1, v2, v4])))

# Change of basis
def change_of_basis(x_standard, P):
    """Change coordinates from standard basis to basis P."""
    P_inv = np.linalg.inv(P)
    return P_inv @ x_standard

# Vector in standard basis
x = np.array([3, 1])

# New basis: rotated 45 degrees
theta = np.pi / 4
P = np.array([[np.cos(theta), -np.sin(theta)],
              [np.sin(theta),  np.cos(theta)]])

x_new = change_of_basis(x, P)
print(f"\nVector {x} in standard basis")
print(f"Coordinates in rotated basis: {x_new}")
print(f"Verification: {P @ x_new}")

Applications in AI/ML

Dimensionality Reduction

  • PCA (Principal Component Analysis): Finds the orthonormal basis of maximum variance. The first kk principal components form a basis for the kk-dimensional subspace that best approximates the data in the least-squares sense.
  • SVD (Singular Value Decomposition): Provides optimal low-rank approximation. Truncating to kk singular values gives the best rank-kk approximation.

Feature Spaces

  • Kernel methods: Map data to high-dimensional feature spaces where linear separation is possible. The basis of this feature space determines the kernel function.
  • Word embeddings: Words are represented as vectors in a learned basis. The dimension of the embedding space (e.g., 300 for Word2Vec) is the number of basis vectors.

Signal Processing

  • Fourier transform: Decomposes signals into frequency basis vectors.
  • Wavelet transform: Multi-resolution basis for time-frequency analysis.
  • Compressed sensing: Exploits sparsity in a given basis for signal recovery from few measurements.

Neural Networks

  • Weight matrices: Each layer's weight matrix transforms from one basis (input features) to another (hidden features). Learning is finding optimal bases.
  • Batch normalization: Normalizes activations to have zero mean and unit variance, effectively choosing a convenient basis for each layer.
# PCA as change of basis
from sklearn.decomposition import PCA
import numpy as np

# Generate sample data
np.random.seed(42)
n_samples = 200
X = np.random.randn(n_samples, 2) @ np.array([[3, 1], [1, 2]])

# PCA finds the optimal basis
pca = PCA(n_components=2)
X_transformed = pca.fit_transform(X)

print("Original data shape:", X.shape)
print("Transformed data shape:", X_transformed.shape)
print("Explained variance ratio:", pca.explained_variance_ratio_)
print("Principal components (new basis):")
print(pca.components_)  # These are the basis vectors

# The principal components form an orthonormal basis
dot_product = np.dot(pca.components_[0], pca.components_[1])
print(f"Dot product of components (should be 0): {dot_product:.10f}")

# Reconstruction: X ≈ X_transformed @ pca.components_
X_reconstructed = X_transformed @ pca.components_ + pca.mean_
reconstruction_error = np.mean((X - X_reconstructed) ** 2)
print(f"Reconstruction MSE: {reconstruction_error:.6f}")

Common Mistakes

MistakeCorrection
Confusing basis with spanning setA basis must be both spanning AND linearly independent
Assuming the standard basis is the only basisAny linearly independent set of nn vectors in Rn\mathbb{R}^n is a basis
Forgetting the zero vector is always dependentAny set containing 0⃗\vec{0} is linearly dependent
Using P−1x⃗P^{-1}\vec{x} when PP has new basis as rowsUse columns: P=[v1âƒ—âˆŖâ‹¯âˆŖvn⃗]P = [\vec{v_1} \mid \cdots \mid \vec{v_n}]
Confusing column space with row spacecol(A)⊆Rm\text{col}(A) \subseteq \mathbb{R}^m, row(A)⊆Rn\text{row}(A) \subseteq \mathbb{R}^n (different ambient spaces!)
Assuming rank = number of rowsRank = number of pivots, not rows; a tall matrix can have rank < rows
Forgetting rank-nullity theoremAlways: rank(A)+nullity(A)=n\text{rank}(A) + \text{nullity}(A) = n (number of columns)
Confusing linear independence with orthogonalityOrthogonal non-zero vectors are independent, but independent vectors need not be orthogonal
Assuming span of kk vectors in Rn\mathbb{R}^n has dimension kkIf vectors are dependent, dim⁥(span)<k\dim(\text{span}) < k
Not checking if b∈col(A)b \in \text{col}(A) before solving Ax=bAx = bFirst verify bb is in the column space; otherwise no solution exists

Interview Questions

Q1: What is the difference between a basis and a spanning set?

A spanning set generates the entire space but may contain redundant vectors. A basis is a spanning set with no redundancy — it is linearly independent. Every spanning set can be reduced to a basis by removing dependent vectors.

Q2: How do you find a basis for the null space of a matrix?

Solve Ax⃗=0⃗A\vec{x} = \vec{0} via row reduction. Express the solution in parametric vector form. The vectors multiplying each free variable form a basis for the null space.

Q3: Why is the dimension of Rn\mathbb{R}^n equal to nn?

Because any basis for Rn\mathbb{R}^n must have exactly nn vectors. The standard basis {e1⃗,â€Ļ,en⃗}\{\vec{e_1}, \ldots, \vec{e_n}\} has nn vectors, and by the dimension theorem, all bases have the same size.

Q4: What is the relationship between rank, column space, and null space?

  • rank(A)=dim⁥(col(A))\text{rank}(A) = \dim(\text{col}(A)) = number of pivots.
  • nullity(A)=dim⁥(ker⁥(A))\text{nullity}(A) = \dim(\ker(A)) = number of free variables.
  • By rank-nullity: rank(A)+nullity(A)=n\text{rank}(A) + \text{nullity}(A) = n (columns).
  • The column space tells you which b⃗\vec{b} allow solutions to Ax⃗=b⃗A\vec{x} = \vec{b}.
  • The null space tells you the solutions when they exist (particular + null space).

Q5: Can a matrix have the same row space and column space?

Yes. Example: A=[1001]A = \begin{bmatrix}1&0\\0&1\end{bmatrix} (identity matrix). Row space = column space = R2\mathbb{R}^2. But generally, row space ⊆Rn\subseteq \mathbb{R}^n and column space ⊆Rm\subseteq \mathbb{R}^m are in different ambient spaces unless m=nm = n.

Q6: How does change of basis relate to diagonalization?

If AA is diagonalizable, A=PDP−1A = PDP^{-1} where DD is diagonal and PP's columns are eigenvectors. This means: in the eigenbasis, AA acts as scaling by eigenvalues. Change of basis to the eigenbasis simplifies matrix operations from O(n2)O(n^2) to O(n)O(n).

Q7: What is the rank of the product ABAB?

rank(AB)≤min⁡(rank(A),rank(B))\text{rank}(AB) \leq \min(\text{rank}(A), \text{rank}(B)). Equality holds when the column space of BB intersects the null space of AA only at 0⃗\vec{0}.


Practice Problems

📝Problem 1

Find a basis and the dimension of the subspace W={(x,y,z)∈R3:x+y+z=0}W = \{(x, y, z) \in \mathbb{R}^3 : x + y + z = 0\}.

💡Solution

The equation x+y+z=0x + y + z = 0 gives x=−y−zx = -y - z. So:

W={(−y−z,y,z):y,z∈R}=span{[−110],[−101]}W = \{(-y-z, y, z) : y, z \in \mathbb{R}\} = \text{span}\left\{\begin{bmatrix}-1\\1\\0\end{bmatrix}, \begin{bmatrix}-1\\0\\1\end{bmatrix}\right\}

These two vectors are linearly independent (not scalar multiples), so they form a basis.

dim⁥(W)=2\dim(W) = 2.

📝Problem 2

Determine if {[123],[456],[789]}\left\{\begin{bmatrix}1\\2\\3\end{bmatrix}, \begin{bmatrix}4\\5\\6\end{bmatrix}, \begin{bmatrix}7\\8\\9\end{bmatrix}\right\} is a basis for R3\mathbb{R}^3.

💡Solution

Form the matrix and row reduce:

[147258369]→[1470−3−60−6−12]→[1470−3−6000]\begin{bmatrix}1&4&7\\2&5&8\\3&6&9\end{bmatrix} \to \begin{bmatrix}1&4&7\\0&-3&-6\\0&-6&-12\end{bmatrix} \to \begin{bmatrix}1&4&7\\0&-3&-6\\0&0&0\end{bmatrix}

Only 2 pivots, so rank=2<3\text{rank} = 2 < 3. The vectors are not a basis for R3\mathbb{R}^3.

They are linearly dependent and span only a 2-dimensional subspace (a plane).

📝Problem 3

Find a basis for the column space of A=[123456789]A = \begin{bmatrix}1&2&3\\4&5&6\\7&8&9\end{bmatrix} and determine the dimension.

💡Solution

Row reduce AA:

[123456789]→[1230−3−6000]\begin{bmatrix}1&2&3\\4&5&6\\7&8&9\end{bmatrix} \to \begin{bmatrix}1&2&3\\0&-3&-6\\0&0&0\end{bmatrix}

Pivot columns are columns 1 and 2. The corresponding columns of the original matrix form a basis:

Basis={[147],[258]}\text{Basis} = \left\{\begin{bmatrix}1\\4\\7\end{bmatrix}, \begin{bmatrix}2\\5\\8\end{bmatrix}\right\}

dim⁥(col(A))=rank(A)=2\dim(\text{col}(A)) = \text{rank}(A) = 2.

📝Problem 4

If AA is a 5×35 \times 3 matrix with rank 2, what is the dimension of the null space?

💡Solution

By rank-nullity: rank(A)+nullity(A)=n=3\text{rank}(A) + \text{nullity}(A) = n = 3.

nullity(A)=3−2=1\text{nullity}(A) = 3 - 2 = 1.

The null space is a 1-dimensional subspace of R3\mathbb{R}^3 (a line through the origin).

📝Problem 5

Find a change-of-basis matrix from B={[11],[01]}B = \left\{\begin{bmatrix}1\\1\end{bmatrix}, \begin{bmatrix}0\\1\end{bmatrix}\right\} to the standard basis, and use it to find the coordinates of x⃗=[35]\vec{x} = \begin{bmatrix}3\\5\end{bmatrix} in basis BB.

💡Solution

The change-of-basis matrix from BB to standard is:

P=[1011]P = \begin{bmatrix}1&0\\1&1\end{bmatrix}

To find [x⃗]B[\vec{x}]_B: solve P[x⃗]B=x⃗P[\vec{x}]_B = \vec{x}:

[x⃗]B=P−1x⃗=[10−11][35]=[32][\vec{x}]_B = P^{-1}\vec{x} = \begin{bmatrix}1&0\\-1&1\end{bmatrix}\begin{bmatrix}3\\5\end{bmatrix} = \begin{bmatrix}3\\2\end{bmatrix}

Verification: 3[11]+2[01]=[35]3\begin{bmatrix}1\\1\end{bmatrix} + 2\begin{bmatrix}0\\1\end{bmatrix} = \begin{bmatrix}3\\5\end{bmatrix} ✓


Quick Reference

ConceptDefinitionKey Formula
Linear combinationc1v1⃗+⋯+ckvk⃗c_1\vec{v_1} + \cdots + c_k\vec{v_k}Scalars multiply vectors, then add
SpanSet of all linear combinationsspan{v1⃗,â€Ļ,vk⃗}\text{span}\{\vec{v_1}, \ldots, \vec{v_k}\}
Linear independenceOnly trivial solution to ∑civi⃗=0⃗\sum c_i\vec{v_i} = \vec{0}Row reduce; pivot in every column
BasisLinearly independent spanning setMinimal spanning set = maximal independent set
DimensionNumber of vectors in any basisdim⁥(Rn)=n\dim(\mathbb{R}^n) = n
RankDimension of column spacerank(A)=#\text{rank}(A) = \# pivots
Null spaceSolutions to Ax⃗=0⃗A\vec{x} = \vec{0}ker⁡(A)\ker(A); subspace of Rn\mathbb{R}^n
Column spaceSpan of columns of AAcol(A)\text{col}(A); subspace of Rm\mathbb{R}^m
Rank-nullityFundamental theoremrank(A)+nullity(A)=n\text{rank}(A) + \text{nullity}(A) = n
Change of basis[x⃗]B′=P−1[x⃗]B[\vec{x}]_{B'} = P^{-1}[\vec{x}]_BP=[v1âƒ—âˆŖâ‹¯âˆŖvn⃗]P = [\vec{v_1} \mid \cdots \mid \vec{v_n}]

Cross-References

Lesson Progress20 / 100