Skip to content

Basis and Duality

Bases define the coordinate systems of vector spaces, and duality reveals how linear functions act on vectors. This file covers linear independence, spanning sets, change of basis, dual spaces, and covectors, concepts behind PCA, feature transforms, and attention queries in ML.

  • We have seen that vectors live in spaces with a certain number of dimensions. But what defines those dimensions? This is where basis vectors come in.

  • A basis is a set of vectors that can build every other vector in the space through scaling and adding (linear combination), with no redundancy. They are the building blocks of the space.

  • A basis must satisfy two conditions:

    • Linearly independent: No basis vector can be built from the others. Each one contributes a genuinely new direction.

    • Spanning: Every vector in the space can be expressed as a combination of the basis vectors. Nothing is left out.

  • The number of vectors in a basis equals the dimension of the space. In \(\mathbb{R}^2\) you need 2, in \(\mathbb{R}^3\) you need 3, and so on.

  • The most natural basis is the standard basis, the unit vectors along each axis:

    • In \(\mathbb{R}^2\): \(\hat{\mathbf{i}} = (1, 0)\) and \(\hat{\mathbf{j}} = (0, 1)\)
    • In \(\mathbb{R}^3\): \(\hat{\mathbf{i}} = (1, 0, 0)\), \(\hat{\mathbf{j}} = (0, 1, 0)\), \(\hat{\mathbf{k}} = (0, 0, 1)\)
  • Any vector is just a weighted sum of these basis vectors. The vector \((3, 2)\) is really \(3\hat{\mathbf{i}} + 2\hat{\mathbf{j}}\). The weights (3 and 2) are the coordinates of the vector in that basis.

  • But the standard basis is not the only valid basis. In \(\mathbb{R}^2\), the vectors \((1, 1)\) and \((-1, 1)\) also form a basis. They are linearly independent and can reach any point in the plane. The same vector will just have different coordinates in this new basis.

  • A change of basis re-expresses the same vector using different basis. The vector has not moved, we are just describing it from a different perspective.

  • This is done by multiplying by a change of basis matrix \(P\), whose columns are the new basis vectors written in the old coordinates. To go back, multiply by \(P^{-1}\).

  • Think of it as two friends giving directions to the same café. You navigate by streets: \((3, 2)\) means "3 blocks east, 2 blocks north". Your friend navigates by diagonals: their directions are \(\mathbf{b}_1 = (1, 1)\) (north-east) and \(\mathbf{b}_2 = (-1, 1)\) (north-west), the new basis from earlier. Same café, two languages for describing how to get there.

  • Your friend says the café is at \((2.5, -0.5)\): "walk 2.5 steps along my north-east diagonal, then half a step backwards along my north-west diagonal". To find out what that means in your street language, you simply follow their recipe:

\[2.5 \begin{bmatrix} 1 \\ 1 \end{bmatrix} - 0.5 \begin{bmatrix} -1 \\ 1 \end{bmatrix} = \begin{bmatrix} 3 \\ 2 \end{bmatrix}\]
  • It is the same café you call \((3, 2)\)!

  • That "follow the recipe" step is exactly what multiplying by \(P\) does. Stack the friend's basis vectors as columns, and multiplying by a vector of their coordinates takes 2.5 of the first column and -0.5 of the second, the recipe again, just written compactly:

\[P = \begin{bmatrix} 1 & -1 \\ 1 & 1 \end{bmatrix}, \qquad P \begin{bmatrix} 2.5 \\ -0.5 \end{bmatrix} = \begin{bmatrix} 3 \\ 2 \end{bmatrix}\]
  • Translating the other way, from your \((3, 2)\) into the friend's language, means undoing the recipe, and undoing \(P\) is precisely what \(P^{-1}\) does:
\[P^{-1} \begin{bmatrix} 3 \\ 2 \end{bmatrix} = \begin{bmatrix} 2.5 \\ -0.5 \end{bmatrix}\]
  • Nothing moved. \((3, 2)\) and \((2.5, -0.5)\) are two descriptions of one point, and \(P\) is the dictionary for translating between them.

  • In ML, change of basis appears frequently. PCA, for example, finds a new basis (the principal components) where the data is easier to understand, the axes align with the directions of greatest variation.

  • The next set of concepts would be abstract and quite challenging to grasp for now, until when we apply the comepts in later chapters, so brace yourselff for impact, sorry.

  • Now, there is a deeper idea hiding here. When we write \(\mathbf{v} = (3, 2)\), the coordinates 3 and 2 are really the result of "measuring" \(\mathbf{v}\) along each basis direction. The first coordinate asks "how much of \(\hat{\mathbf{i}}\) is in \(\mathbf{v}\)?", the second asks "how much of \(\hat{\mathbf{j}}\)?"

  • In the café story, these measurements were the friend's questions: "how far along my north-east diagonal did you walk?" and "how far along my north-west one?". Every basis comes with its own set of questions, one per direction.

  • Each question is a linear functional: a function that takes in a vector and returns a single number, the reading.

  • The word linear means the functional respects the two vector space operations. Measure a sum of vectors and you get the sum of the readings; double a vector and the reading doubles:

\[f(\mathbf{u} + \mathbf{v}) = f(\mathbf{u}) + f(\mathbf{v}), \qquad f(c\mathbf{v}) = c\,f(\mathbf{v})\]
  • In other words, linear functionals are honest rulers. Vectors are the objects, linear functionals are the rulers that measure them, and the collection of all possible rulers forms the dual space \(V^\ast\).

  • For every basis \(\{\mathbf{e}_1, \mathbf{e}_2, \ldots, \mathbf{e}_n\}\) there is a matched set of rulers, the dual basis \(\{\mathbf{e}_1^\ast, \mathbf{e}_2^\ast, \ldots, \mathbf{e}_n^\ast\}\), where \(\mathbf{e}_i^\ast\) answers exactly one question: "how many steps of \(\mathbf{e}_i\)?". A well-calibrated ruler reads 1 on its own basis vector and completely ignores all the others, which is written compactly using the Kronecker delta \(\delta_{ij}\):

\[ \mathbf{e}_i^\ast(\mathbf{e}_j) = \delta_{ij} = \begin{cases} 1 & \text{if } i = j \\ 0 & \text{if } i \neq j \end{cases} \]
  • That tiny calibration rule is all a ruler needs, because linearity does the rest. Watch \(\mathbf{e}_1^\ast\) read the first coordinate of \(\mathbf{v} = 3\mathbf{e}_1 + 2\mathbf{e}_2\):
\[\mathbf{e}_1^\ast(3\mathbf{e}_1 + 2\mathbf{e}_2) = 3\,\mathbf{e}_1^\ast(\mathbf{e}_1) + 2\,\mathbf{e}_1^\ast(\mathbf{e}_2) = 3 \cdot 1 + 2 \cdot 0 = 3\]
  • The ruler ignores every direction except its own and reports the coordinate. Coordinates are dual basis readings.

  • So what does a ruler look like concretely? In \(\mathbb{R}^n\), every linear functional is just a row of numbers applied with a dot product. And for the friend's basis we have already built the rulers without noticing: translating \((3, 2)\) into the friend's language meant multiplying by \(P^{-1}\), and each row of \(P^{-1}\) produced one coordinate. The rows of \(P^{-1}\) are the dual basis:

\[P^{-1} = \frac{1}{2}\begin{bmatrix} 1 & 1 \\ -1 & 1 \end{bmatrix} \quad\Rightarrow\quad \mathbf{b}_1^\ast = (0.5,\ 0.5), \qquad \mathbf{b}_2^\ast = (-0.5,\ 0.5)\]
  • Check it against the café trip: \(\mathbf{b}_1^\ast \cdot (3, 2) = 0.5 \cdot 3 + 0.5 \cdot 2 = 2.5\), exactly the friend's first coordinate. The two halves of this file are one idea: a change of basis swaps the building blocks, and the dual basis is the matched set of rulers that reads off the new coordinates.

  • The calibration rule was hiding in plain sight too: \(P^{-1}P = I\) says precisely that ruler \(i\) reads 1 on basis vector \(i\) and 0 on the others. The identity matrix is the Kronecker delta written out as a table.

  • One warning. It is tempting to guess that the ruler for \(\mathbf{b}_1\) is \(\mathbf{b}_1\) itself, i.e. just dot with \((1, 1)\). But \((1, 1) \cdot (3, 2) = 5\), double the true coordinate of 2.5. Dotting with a basis vector reads off its own coordinate only when the basis is orthonormal (mutually perpendicular, each of length 1), like the standard basis. That is the only reason \(\mathbf{e}_1^\ast\) happens to look identical to \(\mathbf{e}_1\). For every other basis, the rulers live in the rows of \(P^{-1}\).

  • This also explains the dot product's double life. Every vector \(\mathbf{u}\) secretly defines a ruler (measure \(\mathbf{v}\) by computing \(\mathbf{u} \cdot \mathbf{v}\)), and every ruler is a dot product with some vector. In finite dimensions, the dual space is essentially a mirror image of the original space.

  • Duality may seem abstract now, but it underlies many practical ideas: coordinates are dual basis evaluations, the dot product is a duality pairing, and transformations like attention in neural networks operate by having one set of vectors "query" another, which is duality in action.

Coding Tasks (use CoLab or notebook)

  1. Express a vector in two different bases and verify they represent the same point. Try creating your own basis and see what coordinates the vector gets.

    import jax.numpy as jnp
    
    v = jnp.array([3.0, 2.0])
    
    # Standard basis: coordinates are just the components
    print(f"Standard basis coords: {v}")
    
    # New basis: (1,1) and (-1,1)
    P = jnp.array([[1.0, -1.0],
                   [1.0,  1.0]])
    new_coords = jnp.linalg.solve(P, v)
    print(f"New basis coords: {new_coords}")
    
    # Verify: reconstruct from new coords
    reconstructed = new_coords[0] * P[:, 0] + new_coords[1] * P[:, 1]
    print(f"Reconstructed: {reconstructed}")
    

  2. Build the dual basis for the friend's basis from task 1 (the rows of P⁻¹) and verify each ruler reads off exactly one coordinate. Then see why dotting with a basis vector itself only works for orthonormal bases.

    import jax.numpy as jnp
    
    v = jnp.array([3.0, 2.0])
    
    # The friend's basis (1,1) and (-1,1), stacked as columns
    P = jnp.array([[1.0, -1.0],
                   [1.0,  1.0]])
    
    # The dual basis: the rows of P^{-1}
    P_inv = jnp.linalg.inv(P)
    b1_star, b2_star = P_inv[0], P_inv[1]
    print(f"b1* = {b1_star}, b2* = {b2_star}")
    
    # Each ruler reads one coordinate in the friend's basis (matches task 1!)
    print(f"b1*(v) = {jnp.dot(b1_star, v)}")  # 2.5
    print(f"b2*(v) = {jnp.dot(b2_star, v)}")  # -0.5
    
    # Calibration: 1 on its own basis vector, 0 on the other
    print(f"b1*(b1) = {jnp.dot(b1_star, P[:, 0])}, b1*(b2) = {jnp.dot(b1_star, P[:, 1])}")
    
    # The trap: dotting with b1 itself does NOT give the coordinate
    print(f"b1 . v = {jnp.dot(P[:, 0], v)}  (5.0, not 2.5 -- b1 is not orthonormal)")
    
    # The standard basis IS orthonormal, so there the shortcut works
    e1 = jnp.array([1.0, 0.0])
    print(f"e1 . v = {jnp.dot(e1, v)}  (3.0, the first standard coordinate)")