1. The Geometry of Vector Magnitude & Angles
While vector spaces alone define addition and scalar multiplication, they lack any inherent notion of length, distance, or angle.
By equipping a vector space with an Inner Product, we transform an abstract algebraic structure into a geometric Hilbert space capable of computing distances, measuring similarity, and defining perpendicularity (orthogonality).
text
2. Inner Product Spaces
2.1 Formal Axioms
An inner product on a real vector space $V$ is a function $\langle \cdot, \cdot \rangle: V \times V \to \mathbb{R}$ satisfying three axioms for all $\mathbf{u}, \mathbf{v}, \mathbf{w} \in V$ and $c \in \mathbb{R}$:
- Symmetry: $$\langle \mathbf{u}, \mathbf{v} \rangle = \langle \mathbf{v}, \mathbf{u} \rangle$$
- Linearity in the First Argument: $$\langle c\mathbf{u} + \mathbf{v}, \mathbf{w} \rangle = c\langle \mathbf{u}, \mathbf{w} \rangle + \langle \mathbf{v}, \mathbf{w} \rangle$$
- Positive Definiteness: $$\langle \mathbf{u}, \mathbf{u} \rangle \ge 0, \quad \text{and} \quad \langle \mathbf{u}, \mathbf{u} \rangle = 0 \iff \mathbf{u} = \mathbf{0}$$
In standard Euclidean space $\mathbb{R}^n$, the inner product is the familiar dot product:
$$\langle \mathbf{u}, \mathbf{v} \rangle = \mathbf{u} \cdot \mathbf{v} = \mathbf{u}^T \mathbf{v} = \sum_{i=1}^n u_i v_i$$
3. Vector & Matrix Norms
A norm $|\cdot|: V \to \mathbb{R}$ measures the magnitude of a vector, satisfying:
- Non-negativity: $|\mathbf{v}| \ge 0$, and $|\mathbf{v}| = 0 \iff \mathbf{v} = \mathbf{0}$.
- Absolute scalability: $|c\mathbf{v}| = |c| |\mathbf{v}|$.
- Triangle Inequality: $|\mathbf{u} + \mathbf{v}| \le |\mathbf{u}| + |\mathbf{v}|$.
3.1 Common Vector $L_p$ Norms
For a vector $\mathbf{x} \in \mathbb{R}^n$:
$$|\mathbf{x}|p = \left( \sum{i=1}^n |x_i|^p \right)^{1/p}$$
- $L_1$ Norm (Manhattan / Taxicab): $$|\mathbf{x}|1 = \sum{i=1}^n |x_i|$$ (Used in Lasso regularization to induce parameter sparsity).
- $L_2$ Norm (Euclidean Norm): $$|\mathbf{x}|2 = \sqrt{\sum{i=1}^n x_i^2} = \sqrt{\mathbf{x}^T \mathbf{x}}$$ (Standard straight-line Euclidean distance).
- $L_\infty$ Norm (Chebyshev / Maximum Norm): $$|\mathbf{x}|\infty = \max{1 \le i \le n} |x_i|$$ (Used in adversarial robustness testing in machine learning).
text
3.2 Matrix Norms
For a matrix $\mathbf{A} \in \mathbb{R}^{m \times n}$:
- Frobenius Norm: The Euclidean length of the flattened matrix: $$|\mathbf{A}|F = \sqrt{\sum{i=1}^m \sum_{j=1}^n a_{ij}^2} = \sqrt{\text{Tr}(\mathbf{A}^T \mathbf{A})}$$
- Spectral Norm (Induced Operator 2-Norm): $$|\mathbf{A}|2 = \max{\mathbf{x} \neq \mathbf{0}} \frac{|\mathbf{A}\mathbf{x}|2}{|\mathbf{x}|2} = \sigma{\max}(\mathbf{A})$$ where $\sigma{\max}(\mathbf{A})$ is the largest singular value of $\mathbf{A}$.
4. The Cauchy-Schwarz Inequality & Cosine Similarity
One of the most widely used inequalities in all of mathematics:
Theorem (Cauchy-Schwarz)
For any vectors $\mathbf{u}$ and $\mathbf{v}$ in an inner product space:
$$|\langle \mathbf{u}, \mathbf{v} \rangle| \le |\mathbf{u}|_2 |\mathbf{v}|_2$$
Equality holds if and only if $\mathbf{u}$ and $\mathbf{v}$ are linearly dependent ($\mathbf{u} = c\mathbf{v}$).
Application in Vector Search Engines: Cosine Similarity
In retrieval-augmented generation (RAG) and semantic search (Pinecone, Qdrant, Milvus), text embeddings are normalized vectors. The angle $\theta$ between document vector $\mathbf{u}$ and query vector $\mathbf{v}$ measures semantic alignment:
$$\cos \theta = \frac{\mathbf{u} \cdot \mathbf{v}}{|\mathbf{u}|_2 |\mathbf{v}|_2} \in [-1, 1]$$
By the Cauchy-Schwarz inequality, this value is guaranteed to stay bounded within $[-1, 1]$.
- $\cos \theta = 1.0$: Identical semantic direction.
- $\cos \theta = 0.0$: Orthogonal (completely unrelated topics).
- $\cos \theta = -1.0$: Diametrically opposite meaning.
5. Orthogonality & Gram-Schmidt Process
Two vectors $\mathbf{u}$ and $\mathbf{v}$ are orthogonal ($\mathbf{u} \perp \mathbf{v}$) if their inner product is zero:
$$\langle \mathbf{u}, \mathbf{v} \rangle = 0$$
A set of vectors ${\mathbf{q}_1, \dots, \mathbf{q}_k}$ is orthonormal if:
$$\langle \mathbf{q}_i, \mathbf{q}j \rangle = \begin{cases} 1 & \text{if } i = j \ 0 & \text{if } i \neq j \end{cases} = \delta{ij}$$
5.1 The Gram-Schmidt Orthogonalization Algorithm
Given a linearly independent basis ${\mathbf{v}_1, \mathbf{v}_2, \dots, \mathbf{v}_k}$, construct an orthonormal basis ${\mathbf{q}_1, \mathbf{q}_2, \dots, \mathbf{q}_k}$ spanning the identical subspace.
text
$$\begin{aligned}
\mathbf{u}_1 &= \mathbf{v}_1, &\mathbf{q}_1 &= \frac{\mathbf{u}_1}{|\mathbf{u}_1|} \
\mathbf{u}_2 &= \mathbf{v}_2 - \langle \mathbf{v}_2, \mathbf{q}_1 \rangle \mathbf{q}_1, &\mathbf{q}_2 &= \frac{\mathbf{u}_2}{|\mathbf{u}_2|} \
\mathbf{u}_3 &= \mathbf{v}_3 - \langle \mathbf{v}_3, \mathbf{q}_1 \rangle \mathbf{q}_1 - \langle \mathbf{v}_3, \mathbf{q}_2 \rangle \mathbf{q}_2, &\mathbf{q}_3 &= \frac{\mathbf{u}_3}{|\mathbf{u}_3|} \
&\vdots & &\vdots \
\mathbf{u}_k &= \mathbf{v}k - \sum{j=1}^{k-1} \langle \mathbf{v}_k, \mathbf{q}_j \rangle \mathbf{q}_j, &\mathbf{q}_k &= \frac{\mathbf{u}_k}{|\mathbf{u}_k|}
\end{aligned}$$
6. Orthogonal Matrices
A real square matrix $\mathbf{Q} \in \mathbb{R}^{n \times n}$ is orthogonal if its columns are mutually orthonormal vectors:
$$\mathbf{Q}^T \mathbf{Q} = \mathbf{I} \iff \mathbf{Q}^{-1} = \mathbf{Q}^T$$
Invariance Properties of Orthogonal Transformations:
- Length Preservation (Isometry): $$|\mathbf{Q}\mathbf{x}|_2^2 = (\mathbf{Q}\mathbf{x})^T (\mathbf{Q}\mathbf{x}) = \mathbf{x}^T \mathbf{Q}^T \mathbf{Q} \mathbf{x} = \mathbf{x}^T \mathbf{I} \mathbf{x} = |\mathbf{x}|_2^2$$
- Angle Preservation: $$\langle \mathbf{Q}\mathbf{u}, \mathbf{Q}\mathbf{v} \rangle = (\mathbf{Q}\mathbf{u})^T (\mathbf{Q}\mathbf{v}) = \mathbf{u}^T \mathbf{Q}^T \mathbf{Q} \mathbf{v} = \mathbf{u}^T \mathbf{v} = \langle \mathbf{u}, \mathbf{v} \rangle$$
- Determinant: $\det(\mathbf{Q}) = \pm 1$.
- $\det(\mathbf{Q}) = +1$: Pure spatial rotation ($SO(n)$ group).
- $\det(\mathbf{Q}) = -1$: Reflection.
Because orthogonal matrices preserve lengths exactly, multiplying by an orthogonal matrix neither amplifies nor dampens floating-point roundoff error. This makes orthogonal factorizations (like QR decomposition) the gold standard for numerical stability in high-performance computing.
7. Orthogonal Projection onto Subspaces
Let $W$ be a subspace of $\mathbb{R}^n$ spanned by the linearly independent columns of matrix $\mathbf{A} \in \mathbb{R}^{n \times k}$.
For any arbitrary vector $\mathbf{b} \in \mathbb{R}^n$, we want to find the point $\mathbf{p} \in W$ closest to $\mathbf{b}$ in Euclidean distance:
$$\mathbf{p} = \arg\min_{\mathbf{w} \in W} |\mathbf{b} - \mathbf{w}|_2$$
text
Derivation of Projection Matrix $\mathbf{P}$:
- Since $\mathbf{p} \in W$, it can be written as a linear combination of the columns of $\mathbf{A}$: $\mathbf{p} = \mathbf{A}\mathbf{\hat{x}}$.
- The error vector $\mathbf{e} = \mathbf{b} - \mathbf{p}$ must be orthogonal to every column of $\mathbf{A}$: $$\mathbf{A}^T (\mathbf{b} - \mathbf{A}\mathbf{\hat{x}}) = \mathbf{0}$$
- Expanding and solving for $\mathbf{\hat{x}}$: $$\mathbf{A}^T \mathbf{b} - \mathbf{A}^T \mathbf{A}\mathbf{\hat{x}} = \mathbf{0} \implies \mathbf{A}^T \mathbf{A}\mathbf{\hat{x}} = \mathbf{A}^T \mathbf{b} \implies \mathbf{\hat{x}} = (\mathbf{A}^T \mathbf{A})^{-1} \mathbf{A}^T \mathbf{b}$$
- Substituting $\mathbf{\hat{x}}$ back into $\mathbf{p} = \mathbf{A}\mathbf{\hat{x}}$: $$\mathbf{p} = \mathbf{A}(\mathbf{A}^T \mathbf{A})^{-1} \mathbf{A}^T \mathbf{b} = \mathbf{P}\mathbf{b}$$
The Orthogonal Projection Matrix is:
$$\mathbf{P} = \mathbf{A}(\mathbf{A}^T \mathbf{A})^{-1} \mathbf{A}^T$$
It satisfies two definitive mathematical properties:
- Symmetric: $\mathbf{P}^T = \mathbf{P}$.
- Idempotent: $\mathbf{P}^2 = \mathbf{P}$.
8. Python Implementation: Gram-Schmidt & Cosine Similarity
python
9. Key Takeaways
- Inner Products Induce Norms: $|\mathbf{x}| = \sqrt{\langle \mathbf{x}, \mathbf{x} \rangle}$ defines Euclidean geometry.
- Orthogonal Invariance: Orthogonal matrices ($\mathbf{Q}^T \mathbf{Q} = \mathbf{I}$) preserve vector lengths and angles without numeric drift.
- Projection Matrix $\mathbf{P}$: $\mathbf{P} = \mathbf{A}(\mathbf{A}^T \mathbf{A})^{-1} \mathbf{A}^T$ projects vectors orthogonally onto the subspace spanned by $\mathbf{A}$.