Skip to content
BIASED

Inner products, norms and orthogonality

Lengths, distances and angles in any dimension: the dot product, the ℓ₁, ℓ₂ and ℓ∞ norms, Cauchy–Schwarz and cosine similarity of embeddings, orthogonal projection, least squares and the normal equations, orthogonal matrices and the QR decomposition.

The dot product: length and angle

The topic Matrices and linear systems ended with systems that have no solution. Take three equations in two unknowns:

x1=1,x2=2,x1+x2=6.x_1 = 1, \qquad x_2 = 2, \qquad x_1 + x_2 = 6 .

The first two force x1+x2=3x_1 + x_2 = 3, not 66. In matrix form Ax=bAx = b has no exact solution, but some xx come closer than others. To say "closer" we need lengths, and lengths come from one operation.

The dot product of x,y∈Rnx, y \in \mathbb R^n multiplies matching coordinates and adds them up:

⟨x,y⟩=x⊤y=∑i=1nxiyi.\langle x, y \rangle = x^\top y = \sum_{i=1}^n x_i y_i .

It gives the length ∥x∥=⟨x,x⟩\lVert x \rVert = \sqrt{\langle x, x \rangle} and the distance ∥x−y∥\lVert x - y \rVert. In the plane it also gives the angle, by the law of cosines:

⟨x,y⟩=∥x∥ ∥y∥cos⁡θ.\langle x, y \rangle = \lVert x \rVert\, \lVert y \rVert \cos\theta .

For u=(3,4)u = (3, 4) and v=(4,3)v = (4, 3): u⊤v=24u^\top v = 24, both lengths are 55, so cos⁡θ=0.96\cos\theta = 0.96 and θ≈16.3∘\theta \approx 16.3^\circ. In R768\mathbb R^{768} there is no protractor, so we turn this around and define the angle by the formula.

Two vectors are orthogonal, x⊥yx \perp y, when ⟨x,y⟩=0\langle x, y \rangle = 0. Then Pythagoras holds in any dimension: ∥x+y∥2=∥x∥2+∥y∥2\lVert x + y \rVert^2 = \lVert x \rVert^2 + \lVert y \rVert^2.

The dot product is one example of an inner product: a rule that is symmetric, linear in each argument and gives ⟨x,x⟩>0\langle x, x \rangle > 0 for every x≠0x \ne 0. Another is ⟨x,y⟩M=x⊤My\langle x, y \rangle_M = x^\top M y with M=diag⁡(m1,…,mn)M = \operatorname{diag}(m_1, \dots, m_n) and all mi>0m_i > 0. It weighs features differently, which is what rescaling features does to a model's geometry. Below, ⟨x,y⟩\langle x, y \rangle means the plain dot product.

The ℓ₁, ℓ₂ and ℓ∞ norms

The length ∥x∥\lVert x \rVert is one way to measure size. A norm is any rule with three properties: ∥x∥>0\lVert x \rVert > 0 for x≠0x \ne 0, ∥c x∥=∣c∣ ∥x∥\lVert c\,x \rVert = |c|\,\lVert x \rVert, and the triangle inequality ∥x+y∥≤∥x∥+∥y∥\lVert x + y \rVert \le \lVert x \rVert + \lVert y \rVert. Three norms do most of the work in ML:

∥x∥1=∑i∣xi∣,∥x∥2=∑ixi2,∥x∥∞=max⁡i∣xi∣.\lVert x \rVert_1 = \sum_i |x_i|, \qquad \lVert x \rVert_2 = \sqrt{\sum_i x_i^2}, \qquad \lVert x \rVert_\infty = \max_i |x_i| .

For x=(3,−4)x = (3, -4) they give 77, 55 and 44. Only ℓ2\ell_2 comes from an inner product; ∥x∥\lVert x \rVert without a subscript means ℓ2\ell_2. The test is the parallelogram law: expanding both squares and adding shows that a norm from an inner product has ∥u+v∥2+∥u−v∥2=2∥u∥2+2∥v∥2\lVert u + v \rVert^2 + \lVert u - v \rVert^2 = 2\lVert u \rVert^2 + 2\lVert v \rVert^2. For u=(1,0)u = (1, 0) and v=(0,1)v = (0, 1) the ℓ1\ell_1 norm gives 88 against 44, and ℓ∞\ell_\infty gives 22 against 44.

Each norm has its unit ball {x:∥x∥≤1}\{x : \lVert x \rVert \le 1\}. In the plane the ℓ1\ell_1 ball is a diamond, the ℓ2\ell_2 ball is a disc and the ℓ∞\ell_\infty ball is a square. The point (0.6,0.8)(0.6, 0.8) has norms 1.41.4, 11 and 0.80.8: outside the diamond, on the circle, inside the square.

The three norms are always ordered, and the gaps are at most n\sqrt n:

∥x∥∞≤∥x∥2≤∥x∥1,∥x∥1≤n ∥x∥2,∥x∥2≤n ∥x∥∞.\lVert x \rVert_\infty \le \lVert x \rVert_2 \le \lVert x \rVert_1, \qquad \lVert x \rVert_1 \le \sqrt n\, \lVert x \rVert_2, \qquad \lVert x \rVert_2 \le \sqrt n\, \lVert x \rVert_\infty .

The first chain holds because the largest xi2x_i^2 is at most the sum of all of them, and (∑i∣xi∣)2\big(\sum_i |x_i|\big)^2 is ∑ixi2\sum_i x_i^2 plus nonnegative cross terms. The last bound holds because each of the nn terms of ∑ixi2\sum_i x_i^2 is at most ∥x∥∞2\lVert x \rVert_\infty^2. The middle one needs Cauchy–Schwarz, the next block.

Each norm has its job. Weight decay penalizes ∥w∥22\lVert w \rVert_2^2 and shrinks all weights a little. The lasso penalizes ∥w∥1\lVert w \rVert_1; the diamond's corners lie on the axes, so the optimum often has exact zeros. Adversarial attacks bound ∥δ∥∞\lVert \delta \rVert_\infty: no pixel may change by more than ε\varepsilon. For a whole weight matrix the analog of ℓ2\ell_2 is the Frobenius norm ∥W∥F=∑i,jWij2\lVert W \rVert_F = \sqrt{\sum_{i,j} W_{ij}^2}.

The Cauchy–Schwarz inequality

The angle formula only makes sense if ⟨x,y⟩/(∥x∥∥y∥)\langle x, y \rangle / (\lVert x \rVert \lVert y \rVert) lies in [−1,1][-1, 1]. The Cauchy–Schwarz inequality guarantees it in every dimension:

∣⟨u,v⟩∣≤∥u∥ ∥v∥,|\langle u, v \rangle| \le \lVert u \rVert\, \lVert v \rVert ,

with equality exactly when one vector is a multiple of the other.

The proof uses only the fact that a squared length is never negative. For v≠0v \ne 0 and any number tt,

0≤∥u−tv∥2=∥u∥2−2t ⟨u,v⟩+t2∥v∥2.0 \le \lVert u - t v \rVert^2 = \lVert u \rVert^2 - 2t\,\langle u, v \rangle + t^2 \lVert v \rVert^2 .

The right-hand side is a quadratic in tt that never goes below zero, so it has at most one real root and its discriminant is not positive: 4⟨u,v⟩2−4∥u∥2∥v∥2≤04\langle u, v \rangle^2 - 4\lVert u \rVert^2 \lVert v \rVert^2 \le 0. That is the inequality. Equality means the quadratic touches zero, so u=tvu = t v for some tt.

Example: u=(1,2,2)u = (1, 2, 2) and v=(2,0,1)v = (2, 0, 1) give ⟨u,v⟩=4≤3⋅5≈6.71\langle u, v \rangle = 4 \le 3 \cdot \sqrt 5 \approx 6.71, and the angle is about 53.4∘53.4^\circ.

Three consequences come up again and again. The triangle inequality for ℓ2\ell_2 follows by bounding the cross term: ∥u+v∥2=∥u∥2+2⟨u,v⟩+∥v∥2≤(∥u∥+∥v∥)2\lVert u + v \rVert^2 = \lVert u \rVert^2 + 2\langle u, v \rangle + \lVert v \rVert^2 \le \big(\lVert u \rVert + \lVert v \rVert\big)^2. The bound ∥x∥1≤n ∥x∥2\lVert x \rVert_1 \le \sqrt n\, \lVert x \rVert_2 from the previous block is Cauchy–Schwarz for (1,…,1)(1, \dots, 1) and (∣x1∣,…,∣xn∣)(|x_1|, \dots, |x_n|): ∑i1⋅∣xi∣≤n ∥x∥2\sum_i 1 \cdot |x_i| \le \sqrt n\, \lVert x \rVert_2. And a correlation coefficient always lies in [−1,1][-1, 1]: it is the cosine between two centered data vectors.

Cosine similarity of embeddings

Embeddings of words, images or users are compared by the angle between them. Cosine similarity is

cos⁡(u,v)=⟨u,v⟩∥u∥ ∥v∥∈[−1,1].\cos(u, v) = \frac{\langle u, v \rangle}{\lVert u \rVert\, \lVert v \rVert} \in [-1, 1] .

It ignores length: doubling a vector changes nothing. That helps when length reflects something irrelevant, like how frequent a word is.

For unit vectors, cosine and Euclidean distance carry the same information. Expanding the square with ∥u∥=∥v∥=1\lVert u \rVert = \lVert v \rVert = 1 gives

∥u−v∥2=2−2cos⁡(u,v).\lVert u - v \rVert^2 = 2 - 2\cos(u, v) .

So after normalizing all embeddings to length 11, the nearest neighbors by distance are exactly the most similar by cosine. Many retrieval systems normalize once and then use the dot product. Attention, by contrast, scores queries against keys with raw dot products q⊤kq^\top k, so there length matters.

What counts as "similar" depends on the dimension. Two random directions in Rd\mathbb R^d are almost orthogonal: their cosine is around 00 with standard deviation about 1/d1/\sqrt d. For d=768d = 768 that is 0.0360.036, so a cosine of 0.20.2 is more than five standard deviations away from chance. The same 0.20.2 in R10\mathbb R^{10} would mean almost nothing.

Projection onto a line

Now the question we started with, in its simplest form: which point pp on the line through aa is closest to bb? Write p=x^ ap = \hat x\, a. The best point is the one where the error e=b−x^ ae = b - \hat x\, a is orthogonal to the line:

a⊤(b−x^ a)=0⟹x^=a⊤ba⊤a,p=a⊤ba⊤a a.a^\top (b - \hat x\, a) = 0 \quad\Longrightarrow\quad \hat x = \frac{a^\top b}{a^\top a}, \qquad p = \frac{a^\top b}{a^\top a}\, a .

Why the perpendicular wins: for any other point qq on the line, p−qp - q lies along aa and is orthogonal to ee. Pythagoras gives ∥b−q∥2=∥e∥2+∥p−q∥2≥∥e∥2\lVert b - q \rVert^2 = \lVert e \rVert^2 + \lVert p - q \rVert^2 \ge \lVert e \rVert^2.

For a=(2,1)a = (2, 1) and b=(3,4)b = (3, 4): x^=10/5=2\hat x = 10/5 = 2, p=(4,2)p = (4, 2) and e=(−1,2)e = (-1, 2). Indeed a⊤e=−2+2=0a^\top e = -2 + 2 = 0 and ∥b∥2=25=20+5=∥p∥2+∥e∥2\lVert b \rVert^2 = 25 = 20 + 5 = \lVert p \rVert^2 + \lVert e \rVert^2. The distance from bb to the line is ∥e∥=5\lVert e \rVert = \sqrt 5.

The projection is a linear map of bb, so it is a matrix:

P=aa⊤a⊤a,p=Pb.P = \frac{a a^\top}{a^\top a}, \qquad p = P b .

Projecting twice changes nothing, P2=PP^2 = P. In ML, x^\hat x is the best single weight when one feature aa predicts the targets bb.

Least squares

Back to the system with no solution. The columns a1,…,aka_1, \dots, a_k of AA span its column space, and AxAx ranges over all of it. The closest we can get is the projection p=Ax^p = A\hat x of bb onto that space. As with a line, the error must be orthogonal to the space, that is, to every column: aj⊤(b−Ax^)=0a_j^\top (b - A\hat x) = 0. Stacking these kk equations gives the normal equations:

A⊤A x^=A⊤b.A^\top A\, \hat x = A^\top b .

When the columns are independent, A⊤AA^\top A is invertible: if A⊤Az=0A^\top A z = 0, then z⊤A⊤Az=∥Az∥2=0z^\top A^\top A z = \lVert Az \rVert^2 = 0, so Az=0Az = 0 and z=0z = 0. Then x^=(A⊤A)−1A⊤b\hat x = (A^\top A)^{-1} A^\top b. This x^\hat x minimizes ∥Ax−b∥2\lVert Ax - b \rVert^2, which is why the method is called least squares. In the regression notation of the previous topic this is Xw≈yXw \approx y: the rows of XX are training examples, ww holds the weights, yy holds the targets, and ∥Xw−y∥2\lVert Xw - y \rVert^2 is the sum of squared errors.

In our example AA has columns (1,0,1)(1, 0, 1) and (0,1,1)(0, 1, 1), and b=(1,2,6)b = (1, 2, 6):

A⊤A=(2112),A⊤b=(78),x^=(23).A^\top A = \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix}, \qquad A^\top b = \begin{pmatrix} 7 \\ 8 \end{pmatrix}, \qquad \hat x = \begin{pmatrix} 2 \\ 3 \end{pmatrix}.

The fit is p=(2,3,5)p = (2, 3, 5) and the error is e=(−1,−1,1)e = (-1, -1, 1), orthogonal to both columns. No xx gets closer to bb than ∥e∥=3\lVert e \rVert = \sqrt 3. The best compromise raised x1x_1 and x2x_2 above 11 and 22 to bring their sum closer to 66.

The projection matrix onto the column space is

P=A(A⊤A)−1A⊤,p=Pb.P = A (A^\top A)^{-1} A^\top , \qquad p = P b .

It is symmetric and idempotent, and I−PI - P projects onto the directions orthogonal to every column, where the errors live.

The underdetermined case asks for the shortest exact solution. That is an orthogonality condition too: the shortest solution has no component in the null space of AA. The topic The singular value decomposition gives a formula for it.

Orthonormal bases and orthogonal matrices

Projections become easy when the basis is orthogonal. Vectors q1,…,qkq_1, \dots, q_k are orthonormal when each has length 11 and they are pairwise orthogonal. As columns of a matrix QQ, that reads

Q⊤Q=I.Q^\top Q = I .

Coordinates in such a basis need no system to solve. Each one is a dot product: x=∑iciqix = \sum_i c_i q_i with ci=qi⊤xc_i = q_i^\top x. For q1=(0.8,0.6)q_1 = (0.8, 0.6), q2=(−0.6,0.8)q_2 = (-0.6, 0.8) and x=(2,4)x = (2, 4) the coordinates are 44 and 22, and indeed 4q1+2q2=(2,4)4q_1 + 2q_2 = (2, 4). The projection onto their span is just p=QQ⊤bp = QQ^\top b: the normal equations with A⊤A=IA^\top A = I.

A square QQ with Q⊤Q=IQ^\top Q = I is an orthogonal matrix. Its inverse is its transpose, Q−1=Q⊤Q^{-1} = Q^\top. It keeps all the geometry:

⟨Qx,Qy⟩=x⊤Q⊤Q y=⟨x,y⟩,∥Qx∥=∥x∥.\langle Qx, Qy \rangle = x^\top Q^\top Q\, y = \langle x, y \rangle , \qquad \lVert Qx \rVert = \lVert x \rVert .

Rotations, reflections and permutations are orthogonal. In ML this is why PCA (later topics) only rotates the data into new axes without distorting it, and why orthogonal initialization of weights keeps the signal's scale through many linear layers: no direction is stretched or squashed.

Gram–Schmidt and the QR decomposition

Any independent columns can be made orthonormal. The Gram–Schmidt process goes column by column: from aja_j subtract its projections onto the qiq_i already built, then normalize what is left:

vj=aj−∑i<j(qi⊤aj) qi,qj=vj∥vj∥.v_j = a_j - \sum_{i < j} (q_i^\top a_j)\, q_i, \qquad q_j = \frac{v_j}{\lVert v_j \rVert} .

For a1=(4,3)a_1 = (4, 3) and a2=(2,4)a_2 = (2, 4): q1=a1/5=(0.8,0.6)q_1 = a_1/5 = (0.8, 0.6); then q1⊤a2=4q_1^\top a_2 = 4, v2=(2,4)−4 (0.8,0.6)=(−1.2,1.6)v_2 = (2, 4) - 4\,(0.8, 0.6) = (-1.2, 1.6), ∥v2∥=2\lVert v_2 \rVert = 2 and q2=(−0.6,0.8)q_2 = (-0.6, 0.8).

Recording the coefficients gives the QR decomposition A=QRA = QR, with Q⊤Q=IQ^\top Q = I and RR upper triangular, rij=qi⊤ajr_{ij} = q_i^\top a_j and rjj=∥vj∥r_{jj} = \lVert v_j \rVert:

(4234)=(0.8−0.60.60.8)(5402).\begin{pmatrix} 4 & 2 \\ 3 & 4 \end{pmatrix} = \begin{pmatrix} 0.8 & -0.6 \\ 0.6 & 0.8 \end{pmatrix} \begin{pmatrix} 5 & 4 \\ 0 & 2 \end{pmatrix}.

Least squares then reduces to a triangular system. Substitute A=QRA = QR into the normal equations, use Q⊤Q=IQ^\top Q = I and cancel R⊤R^\top, which is invertible because RR is triangular with a positive diagonal:

R⊤Q⊤QR x^=R⊤Q⊤b⟹R⊤R x^=R⊤Q⊤b⟹Rx^=Q⊤b.R^\top Q^\top Q R\, \hat x = R^\top Q^\top b \quad\Longrightarrow\quad R^\top R\, \hat x = R^\top Q^\top b \quad\Longrightarrow\quad R\hat x = Q^\top b .

It is solved from the bottom row up, and A⊤AA^\top A is never formed. Libraries prefer this route to the normal equations: forming A⊤AA^\top A loses precision, as the topic Conditioning and numerical stability will show. They also orthogonalize with Householder reflections rather than Gram–Schmidt, for the same reason.

An orthogonal matrix keeps every length. A general matrix does not: it stretches some directions more than others and turns most vectors as well. Are there directions that a matrix only stretches, without turning? That is the question of the next topic, Determinants and eigenvalues.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.