AI Knowledge Tree

Inner products, orthogonality, Cauchy–Schwarz

Define the inner product, prove the Cauchy–Schwarz inequality step by step from positivity of a quadratic, and use it to give precise meaning to angle and orthogonality.

⏱ 32 minPrerequisites:Norms and the geometry of distance

A norm tells you how long a vector is. An inner product tells you how two vectors relate: the angle between them, whether they are perpendicular, how much one points along another. Almost every geometric statement later in the course (projections, least squares, orthogonal attention directions) is an inner-product statement, and they all rest on one inequality we will prove carefully here.

Definition

An inner product on a real vector space VV is a rule ⟨u,v⟩\langle u, v\rangle assigning a real number to each pair of vectors, with three properties[1]:

  1. Symmetry: ⟨u,v⟩=⟨v,u⟩\langle u, v\rangle = \langle v, u\rangle.
  2. Linearity in the first slot: ⟨αu+βw, v⟩=α⟨u,v⟩+β⟨w,v⟩\langle \alpha u + \beta w,\, v\rangle = \alpha\langle u,v\rangle + \beta\langle w,v\rangle.
  3. Positive definiteness: ⟨v,v⟩≥0\langle v, v\rangle \ge 0, with equality only when v=0v = 0.

On Rn\mathbb{R}^n the standard choice is the dot product ⟨u,v⟩=u⊤v=∑iuivi\langle u, v\rangle = u^\top v = \sum_i u_i v_i. Property 3 lets us define a length from the inner product, the induced norm:

∥v∥=⟨v,v⟩.\lVert v\rVert = \sqrt{\langle v, v\rangle}.

For the dot product this is exactly the Euclidean norm from the previous lesson.

The inequality everything depends on

Cauchy–Schwarz inequality. For all u,v∈Vu, v \in V[2],

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

with equality if and only if uu and vv are scalar multiples of each other.

The proof is short once you see the trick: build a quantity that must be nonnegative (a squared length), expand it, and read off the inequality.

Proof. If v=0v = 0 both sides are 00 and the statement holds. So assume v≠0v \neq 0, and consider the vector u−tvu - t v for a real number tt we will choose later. By positive definiteness its squared length is nonnegative:

Step 1: start from a square that cannot be negative.

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

Step 2: expand using symmetry and linearity.

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

So for every real tt,

∥v∥2 t2  −  2⟨u,v⟩ t  +  ∥u∥2  ≥  0.\lVert v\rVert^2\, t^2 \;-\; 2\langle u,v\rangle\, t \;+\; \lVert u\rVert^2 \;\ge\; 0.

Step 3: a nonnegative quadratic has nonpositive discriminant. The left side is a quadratic at2+bt+cat^2 + bt + c in tt with a=∥v∥2>0a = \lVert v\rVert^2 > 0. A parabola that opens upward and never goes below zero can touch zero at most once, which means its discriminant b2−4acb^2 - 4ac is ≤0\le 0. Here

b2−4ac=(2⟨u,v⟩)2−4 ∥v∥2 ∥u∥2  ≤  0.b^2 - 4ac = \big(2\langle u,v\rangle\big)^2 - 4\,\lVert v\rVert^2\,\lVert u\rVert^2 \;\le\; 0.

Step 4: solve for the inequality. Divide by 44 and rearrange:

⟨u,v⟩2  ≤  ∥u∥2 ∥v∥2.\langle u,v\rangle^2 \;\le\; \lVert u\rVert^2\,\lVert v\rVert^2.

Taking square roots (both sides nonnegative) gives ∣⟨u,v⟩∣≤∥u∥ ∥v∥|\langle u,v\rangle| \le \lVert u\rVert\,\lVert v\rVert, the claim.

Equality. The discriminant is zero exactly when the quadratic has a real root t⋆t^\star, i.e. ∥u−t⋆v∥2=0\lVert u - t^\star v\rVert^2 = 0, which by positive definiteness forces u=t⋆vu = t^\star v. So equality holds precisely when uu and vv are parallel. ■\qquad\blacksquare

Angle and orthogonality

Cauchy–Schwarz says the ratio ⟨u,v⟩/(∥u∥ ∥v∥)\langle u,v\rangle / (\lVert u\rVert\,\lVert v\rVert) always lies in [−1,1][-1, 1]. That is exactly the range of cosine, so we may define the angle θ\theta between two nonzero vectors by

cos⁡θ=⟨u,v⟩∥u∥ ∥v∥.\cos\theta = \frac{\langle u, v\rangle}{\lVert u\rVert\,\lVert v\rVert}.

The definition is only legitimate because the inequality guarantees the right-hand side is a valid cosine. The normalized inner product is then an alignment score on a fixed scale from −1-1 to 11, and the familiar angles sit at recognizable values:

  • cos⁡θ=1\cos\theta = 1 (θ=0∘\theta = 0^\circ): the vectors point the same way, the equality case of Cauchy–Schwarz. u=(1,0)u = (1,0) and v=(3,0)v = (3,0).
  • cos⁡θ=0.5\cos\theta = 0.5 (θ=60∘\theta = 60^\circ): u=(1,0)u = (1,0) and v=(1,3)v = (1,\sqrt 3) give ⟨u,v⟩=1\langle u,v\rangle = 1, ∥u∥=1\lVert u\rVert = 1, ∥v∥=2\lVert v\rVert = 2, ratio 0.50.5.
  • cos⁡θ=0\cos\theta = 0 (θ=90∘\theta = 90^\circ): perpendicular, u=(1,0)u = (1,0) and v=(0,1)v = (0,1).
  • cos⁡θ=−1\cos\theta = -1 (θ=180∘\theta = 180^\circ): antiparallel, u=(1,0)u = (1,0) and v=(−3,0)v = (-3,0), the other equality case.

Two vectors are orthogonal when ⟨u,v⟩=0\langle u, v\rangle = 0, i.e. θ=90∘\theta = 90^\circ. This single condition is the seed of projections, the normal equations of least squares, and the orthogonality of principal components later in the course.

A companion fact, proved in one line from the expansion above with t=−1t = -1, is the triangle inequality for the induced norm: ∥u+v∥≤∥u∥+∥v∥\lVert u + v\rVert \le \lVert u\rVert + \lVert v\rVert. So the induced norm is genuinely a norm in the sense of the previous lesson.

The inner product measures alignment: u·v equals the length of v's projection onto u times |u|, and encodes the angle between them.
The inner product measures alignment: u·v equals the length of v's projection onto u times |u|, and encodes the angle between them.

Check it on arrays

Cauchy–Schwarz is a claim you can stress-test: for many random pairs the ratio ∣⟨u,v⟩∣/(∥u∥∥v∥)|\langle u,v\rangle| / (\lVert u\rVert\lVert v\rVert) should never exceed 11, and it should hit 11 exactly when the two vectors are parallel.

python · numpy
import numpy as np

rng = np.random.default_rng(0)

# 100000 random pairs in R^5: the ratio must stay <= 1.
U = rng.normal(size=(100_000, 5))
V = rng.normal(size=(100_000, 5))
dots = np.einsum("ij,ij->i", U, V)
ratio = np.abs(dots) / (np.linalg.norm(U, axis=1) * np.linalg.norm(V, axis=1))
print("max ratio over 100k random pairs:", ratio.max())   # < 1

# Parallel vectors saturate the bound (ratio == 1):
u = np.array([1.0, 2.0, 3.0])
v = -4.0 * u
print("parallel ratio:", abs(u @ v) / (np.linalg.norm(u) * np.linalg.norm(v)))  # 1.0

# Orthogonal vectors: inner product is 0, angle 90 degrees.
a = np.array([1.0, 0.0]); b = np.array([0.0, 1.0])
print("orthogonal dot:", a @ b, "-> angle", np.degrees(np.arccos(a @ b)))

The random ratios crowd under 11 but never cross it; the parallel pair sits exactly at 11 (the equality case of the proof); the orthogonal pair gives 00.

Exercises

Exercise 1. For u=(1,2,2)u = (1, 2, 2) and v=(2,0,1)v = (2, 0, 1) under the standard dot product, compute ⟨u,v⟩\langle u, v\rangle, the two norms, and the cosine of the angle between them. Confirm the Cauchy–Schwarz inequality holds.

Solution

Step 1. Inner product: ⟨u,v⟩=1⋅2+2⋅0+2⋅1=4\langle u, v\rangle = 1\cdot 2 + 2\cdot 0 + 2\cdot 1 = 4.

Step 2. Norms: ∥u∥=1+4+4=3\lVert u\rVert = \sqrt{1 + 4 + 4} = 3 and ∥v∥=4+0+1=5\lVert v\rVert = \sqrt{4 + 0 + 1} = \sqrt 5.

Step 3. Cosine: cos⁡θ=435≈0.596\cos\theta = \dfrac{4}{3\sqrt 5} \approx 0.596. Cauchy–Schwarz requires ∣⟨u,v⟩∣=4≤∥u∥ ∥v∥=35≈6.71|\langle u,v\rangle| = 4 \le \lVert u\rVert\,\lVert v\rVert = 3\sqrt 5 \approx 6.71, which holds, and the ratio 0.5960.596 lies in [−1,1][-1, 1] as it must.

Exercise 2. Prove the parallelogram law ∥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 any inner-product norm.

Solution

Step 1. Expand each squared norm with the inner product, using symmetry and linearity:

∥u+v∥2=∥u∥2+2⟨u,v⟩+∥v∥2.\lVert u + v\rVert^2 = \lVert u\rVert^2 + 2\langle u, v\rangle + \lVert v\rVert^2.

Step 2. Likewise, replacing vv with −v-v:

∥u−v∥2=∥u∥2−2⟨u,v⟩+∥v∥2.\lVert u - v\rVert^2 = \lVert u\rVert^2 - 2\langle u, v\rangle + \lVert v\rVert^2.

Step 3. Add the two. The cross terms ±2⟨u,v⟩\pm 2\langle u, v\rangle cancel, leaving 2∥u∥2+2∥v∥22\lVert u\rVert^2 + 2\lVert v\rVert^2, which is the claim.

Exercise 3 (code). A valid inner product can weight coordinates differently: ⟨u,v⟩W=u⊤Wv\langle u, v\rangle_W = u^\top W v for a diagonal WW with positive entries. Pick W=diag⁡(1,2,3)W = \operatorname{diag}(1, 2, 3), draw many random pairs in R3\mathbb{R}^3, and verify Cauchy–Schwarz holds in this weighted inner product (using its induced norm).

Solution
python · numpy
import numpy as np

rng = np.random.default_rng(0)
W = np.diag([1.0, 2.0, 3.0])      # positive diagonal -> valid inner product

def ip(u, v):                      # weighted inner product u^T W v
    return u @ W @ v

U = rng.normal(size=(100_000, 3))
V = rng.normal(size=(100_000, 3))

dots = np.einsum("ij,jk,ik->i", U, W, V)        # <u, v>_W per row
nu = np.sqrt(np.einsum("ij,jk,ik->i", U, W, U)) # induced norms
nv = np.sqrt(np.einsum("ij,jk,ik->i", V, W, V))
ratio = np.abs(dots) / (nu * nv)

assert ratio.max() <= 1.0 + 1e-12               # Cauchy-Schwarz, weighted
print("max weighted ratio:", round(ratio.max(), 6))

Every weighted ratio stays at or below 11, so Cauchy–Schwarz holds in the weighted inner product just as in the standard one; the proof never used anything beyond the three defining properties, which ⟨⋅,⋅⟩W\langle\cdot,\cdot\rangle_W satisfies.

The next lesson turns orthogonality into a construction, Gram–Schmidt, that manufactures an orthonormal basis from any basis.


References

  1. [1]Axler, S. (2015). Linear Algebra Done Right (3rd ed.), ch. 6. Springer. Inner-product spaces, orthogonality, and the induced norm (doi:10.1007/978-3-319-11080-6).↩
  2. [2]Horn, R. A., & Johnson, C. R. (2012). Matrix Analysis (2nd ed.). Cambridge University Press. The Cauchy-Schwarz inequality and its equality conditions.↩