TExcelTAD
Home › Linear Algebra & Matrix Theory › Real Inner Product Spaces

Real Inner Product Spaces

An inner product is the dot product made abstract: a rule that takes two vectors and returns a number, so that lengths, angles and "perpendicular" make sense in any vector space, including spaces of polynomials, functions and matrices.

From the dot product to an axiom list

In The Dot Product we used u⋅v=u1v1+⋯+unvn\mathbf{u} \cdot \mathbf{v} = u_1v_1 + \cdots + u_nv_n in Rn\mathbb{R}^n. Everything we did with it (lengths, angles, projections) relied on only a few properties. An inner product on a real vector space VV is any function ⟨u,v⟩∈R\langle \mathbf{u}, \mathbf{v} \rangle \in \mathbb{R} that has those properties for all u,v,w\mathbf{u}, \mathbf{v}, \mathbf{w} in VV and every scalar cc:

Axiom Statement
Symmetry ⟨u,v⟩=⟨v,u⟩\langle \mathbf{u}, \mathbf{v} \rangle = \langle \mathbf{v}, \mathbf{u} \rangle
Linearity ⟨u+w,v⟩=⟨u,v⟩+⟨w,v⟩\langle \mathbf{u} + \mathbf{w}, \mathbf{v} \rangle = \langle \mathbf{u}, \mathbf{v} \rangle + \langle \mathbf{w}, \mathbf{v} \rangle and ⟨cu,v⟩=c⟨u,v⟩\langle c\mathbf{u}, \mathbf{v} \rangle = c\langle \mathbf{u}, \mathbf{v} \rangle
Positivity ⟨v,v⟩≥0\langle \mathbf{v}, \mathbf{v} \rangle \ge 0, and ⟨v,v⟩=0\langle \mathbf{v}, \mathbf{v} \rangle = 0 only when v=0\mathbf{v} = \mathbf{0}

By symmetry, linearity in the first slot gives linearity in the second slot too. A vector space together with a chosen inner product is a real inner product space.

Four examples

Space Inner product Name
Rn\mathbb{R}^n ⟨u,v⟩=uTv=∑uivi\langle \mathbf{u}, \mathbf{v} \rangle = \mathbf{u}^T\mathbf{v} = \sum u_iv_i standard (dot product)
Rn\mathbb{R}^n ⟨u,v⟩=uTAv\langle \mathbf{u}, \mathbf{v} \rangle = \mathbf{u}^TA\mathbf{v}, AA symmetric positive definite weighted / AA-inner product
continuous functions on [a,b][a, b] ⟨f,g⟩=∫abf(x) g(x) dx\langle f, g \rangle = \int_a^b f(x)\,g(x)\,dx L2L^2 inner product
m×nm \times n matrices ⟨A,B⟩=trace⁡(ATB)=∑aijbij\langle A, B \rangle = \operatorname{trace}(A^TB) = \sum a_{ij}b_{ij} Frobenius

The weighted version with A=diag⁡(2,5)A = \operatorname{diag}(2, 5) is ⟨u,v⟩=2u1v1+5u2v2\langle \mathbf{u}, \mathbf{v} \rangle = 2u_1v_1 + 5u_2v_2: it stretches the second coordinate before measuring. Not every matrix works. With A=[1221]A = \begin{bmatrix} 1 & 2 \\ 2 & 1 \end{bmatrix} the vector v=(1,−1)\mathbf{v} = (1, -1) gives vTAv=1−4+1=−2<0\mathbf{v}^TA\mathbf{v} = 1 - 4 + 1 = -2 < 0, so positivity fails. Which matrices do work is the subject of Positive Definite Matrices.

For functions, positivity holds because ∫f2 dx>0\int f^2\,dx > 0 for any continuous ff that is not identically zero.

Norm, distance and angle

Every inner product gives a norm ∥v∥=⟨v,v⟩\lVert \mathbf{v} \rVert = \sqrt{\langle \mathbf{v}, \mathbf{v} \rangle} and a distance d(u,v)=∥u−v∥d(\mathbf{u}, \mathbf{v}) = \lVert \mathbf{u} - \mathbf{v} \rVert. Two inequalities hold in every inner product space:

Cauchy–Schwarz:∣⟨u,v⟩∣≤∥u∥ ∥v∥Triangle:∥u+v∥≤∥u∥+∥v∥\begin{aligned} &\text{Cauchy–Schwarz:} && \lvert \langle \mathbf{u}, \mathbf{v} \rangle \rvert \le \lVert \mathbf{u} \rVert \, \lVert \mathbf{v} \rVert \\ &\text{Triangle:} && \lVert \mathbf{u} + \mathbf{v} \rVert \le \lVert \mathbf{u} \rVert + \lVert \mathbf{v} \rVert \end{aligned}

Proof sketch of Cauchy–Schwarz. If v=0\mathbf{v} = \mathbf{0} both sides are 0. Otherwise, for every real tt, 0≤⟨u−tv,u−tv⟩=∥u∥2−2t⟨u,v⟩+t2∥v∥20 \le \langle \mathbf{u} - t\mathbf{v}, \mathbf{u} - t\mathbf{v} \rangle = \lVert \mathbf{u} \rVert^2 - 2t\langle \mathbf{u}, \mathbf{v} \rangle + t^2\lVert \mathbf{v} \rVert^2. A quadratic in tt that never goes negative has discriminant ≤0\le 0, so 4⟨u,v⟩2−4∥u∥2∥v∥2≤04\langle \mathbf{u}, \mathbf{v} \rangle^2 - 4\lVert \mathbf{u} \rVert^2\lVert \mathbf{v} \rVert^2 \le 0. The triangle inequality follows by expanding ∥u+v∥2\lVert \mathbf{u} + \mathbf{v} \rVert^2 and using Cauchy–Schwarz on the middle term.

Because of Cauchy–Schwarz, the ratio ⟨u,v⟩/(∥u∥∥v∥)\langle \mathbf{u}, \mathbf{v} \rangle / (\lVert \mathbf{u} \rVert \lVert \mathbf{v} \rVert) always lies in [−1,1][-1, 1], so we can define the angle θ\theta between u\mathbf{u} and v\mathbf{v} by cos⁡θ=⟨u,v⟩/(∥u∥ ∥v∥)\cos\theta = \langle \mathbf{u}, \mathbf{v} \rangle / (\lVert \mathbf{u} \rVert \, \lVert \mathbf{v} \rVert), even between two functions.

python · try it yourself
import numpy as np

def ip(u, v, A):
    return float(u @ A @ v)

u = np.array([1.0, 2.0])
v = np.array([3.0, -1.0])
I = np.eye(2)
W = np.diag([2.0, 5.0])

for name, A in [("standard", I), ("weighted", W)]:
    nu, nv = np.sqrt(ip(u, u, A)), np.sqrt(ip(v, v, A))
    cos = ip(u, v, A) / (nu * nv)
    print(f"{name:8s} <u,v>={ip(u, v, A):5.1f}  |u|={nu:.4f}  |v|={nv:.4f}  "
          f"angle={np.degrees(np.arccos(cos)):.2f} deg")

bad = np.array([[1.0, 2.0], [2.0, 1.0]])
x = np.array([1.0, -1.0])
print("x^T A x for the bad matrix:", ip(x, x, bad))
standard <u,v>=  1.0  |u|=2.2361  |v|=3.1623  angle=81.87 deg
weighted <u,v>= -4.0  |u|=4.6904  |v|=4.7958  angle=100.24 deg
x^T A x for the bad matrix: -2.0

The same two vectors are nearly perpendicular in one inner product and obtuse in another: angles depend on the inner product you chose.

Inner products of functions

With ⟨f,g⟩=∫01f(x) g(x) dx\langle f, g \rangle = \int_0^1 f(x)\,g(x)\,dx we can measure polynomials. SymPy does the integrals exactly:

python · try it yourself
from sympy import symbols, integrate, sqrt, acos, deg, N, Rational

x = symbols("x")
ip = lambda f, g: integrate(f * g, (x, 0, 1))

f, g = x, x**2
print("<x, x^2> =", ip(f, g))
print("|x| =", sqrt(ip(f, f)), "  |x^2| =", sqrt(ip(g, g)))
cos = ip(f, g) / (sqrt(ip(f, f)) * sqrt(ip(g, g)))
print("cos(angle) =", cos.simplify(), "≈", N(cos, 6))
print("angle in degrees ≈", N(deg(acos(cos)), 5))
print("<1, x - 1/2> =", ip(1, x - Rational(1, 2)))
<x, x^2> = 1/4
|x| = sqrt(3)/3   |x^2| = sqrt(5)/5
cos(angle) = sqrt(15)/4 ≈ 0.968246
angle in degrees ≈ 14.478
<1, x - 1/2> = 0

The functions xx and x2x^2 are "close" on [0,1][0, 1] (about 14.5∘14.5^\circ apart), while 11 and x−12x - \frac{1}{2} are perpendicular. That last fact is the seed of Gram-Schmidt and Legendre polynomials.

In software

  • Weighted distances. A kk-nearest-neighbour search on features measured in different units (metres and grams) uses a diagonal weight matrix; the Mahalanobis distance uses A=Σ−1A = \Sigma^{-1}, the inverse covariance.
  • Frobenius norm. ∥W∥F=trace⁡(WTW)\lVert W \rVert_F = \sqrt{\operatorname{trace}(W^TW)} is the "weight decay" penalty in neural network training and the error measure for low-rank approximation.
  • Kernels. A support vector machine (SVM) only ever needs inner products ⟨φ(x),φ(y)⟩\langle \varphi(x), \varphi(y) \rangle, so it can work in huge feature spaces without building them.
  • Signals. Audio correlation and the Fourier transform are inner products of sampled functions, see Fourier series.

Tip: When a formula in a paper writes ⟨x,y⟩\langle x, y \rangle, check which inner product is meant. Many "mysterious" factors of 2 or of a matrix MM come from a weighted inner product.

Exercises

Exercise 1: Check the axioms

Is ⟨u,v⟩=u1v1−u2v2\langle \mathbf{u}, \mathbf{v} \rangle = u_1v_1 - u_2v_2 an inner product on R2\mathbb{R}^2?

Answer

No. It is symmetric and linear, but positivity fails: ⟨(0,1),(0,1)⟩=−1\langle (0, 1), (0, 1) \rangle = -1.

Solution

Symmetry and linearity hold because it is uTAv\mathbf{u}^TA\mathbf{v} with A=diag⁡(1,−1)A = \operatorname{diag}(1, -1). Positivity needs ⟨v,v⟩=v12−v22≥0\langle \mathbf{v}, \mathbf{v} \rangle = v_1^2 - v_2^2 \ge 0 for every v\mathbf{v}; the vector (0,1)(0, 1) gives −1-1, and (1,1)(1, 1) gives 0 although it is not the zero vector. Either failure is enough.

Exercise 2: A weighted length

In R3\mathbb{R}^3 with ⟨u,v⟩=u1v1+4u2v2+9u3v3\langle \mathbf{u}, \mathbf{v} \rangle = u_1v_1 + 4u_2v_2 + 9u_3v_3, find ∥(1,1,1)∥\lVert (1, 1, 1) \rVert and ⟨(1,1,1),(3,0,−1)⟩\langle (1, 1, 1), (3, 0, -1) \rangle.

Answer

∥(1,1,1)∥=14≈3.7417\lVert (1, 1, 1) \rVert = \sqrt{14} \approx 3.7417 and the inner product is −6-6.

Solution
⟨v,v⟩=1+4+9=14,∥v∥=14⟨v,w⟩=1⋅3+4⋅1⋅0+9⋅1⋅(−1)=3−9=−6\begin{aligned} \langle \mathbf{v}, \mathbf{v} \rangle &= 1 + 4 + 9 = 14, \quad \lVert \mathbf{v} \rVert = \sqrt{14} \\ \langle \mathbf{v}, \mathbf{w} \rangle &= 1 \cdot 3 + 4 \cdot 1 \cdot 0 + 9 \cdot 1 \cdot (-1) = 3 - 9 = -6 \end{aligned}

Exercise 3: Frobenius inner product

Compute ⟨A,B⟩=trace⁡(ATB)\langle A, B \rangle = \operatorname{trace}(A^TB) for A=[1203]A = \begin{bmatrix} 1 & 2 \\ 0 & 3 \end{bmatrix} and B=[4−121]B = \begin{bmatrix} 4 & -1 \\ 2 & 1 \end{bmatrix}, then check it equals the sum of entrywise products.

Answer

⟨A,B⟩=5\langle A, B \rangle = 5.

Solution

Entrywise: 1⋅4+2⋅(−1)+0⋅2+3⋅1=4−2+0+3=51 \cdot 4 + 2 \cdot (-1) + 0 \cdot 2 + 3 \cdot 1 = 4 - 2 + 0 + 3 = 5.

python · try it yourself
import numpy as np
A = np.array([[1, 2], [0, 3]])
B = np.array([[4, -1], [2, 1]])
print(int(np.trace(A.T @ B)), int((A * B).sum()))
5 5

Exercise 4: Angle between functions (code)

With ⟨f,g⟩=∫01f g dx\langle f, g \rangle = \int_0^1 f\,g\,dx, find the angle between f(x)=1f(x) = 1 and g(x)=xg(x) = x.

Answer

cos⁡θ=3/2\cos\theta = \sqrt{3}/2, so θ=30∘\theta = 30^\circ.

Solution

⟨1,x⟩=12\langle 1, x \rangle = \frac{1}{2}, ∥1∥=1\lVert 1 \rVert = 1, ∥x∥=1/3\lVert x \rVert = \sqrt{1/3}. So cos⁡θ=1/21/3=3/2\cos\theta = \frac{1/2}{1/\sqrt{3}} = \sqrt{3}/2 and θ=30∘\theta = 30^\circ.

python · try it yourself
from sympy import symbols, integrate, sqrt, acos, deg, simplify
x = symbols("x")
ip = lambda f, g: integrate(f * g, (x, 0, 1))
c = simplify(ip(1, x) / (sqrt(ip(1, 1)) * sqrt(ip(x, x))))
print(c, deg(acos(c)))
sqrt(3)/2 30

Exercise 5: Equality in Cauchy–Schwarz

Show that ∣⟨u,v⟩∣=∥u∥∥v∥\lvert \langle \mathbf{u}, \mathbf{v} \rangle \rvert = \lVert \mathbf{u} \rVert \lVert \mathbf{v} \rVert when v=cu\mathbf{v} = c\mathbf{u}. (The converse, that equality forces u\mathbf{u} and v\mathbf{v} to be parallel, follows from the proof sketch above: equality makes the discriminant zero, so u−tv=0\mathbf{u} - t\mathbf{v} = \mathbf{0} for some tt.)

Answer

Both sides equal ∣c∣ ∥u∥2\lvert c \rvert \, \lVert \mathbf{u} \rVert^2.

Solution
∣⟨u,cu⟩∣=∣c∣ ⟨u,u⟩=∣c∣ ∥u∥2∥u∥ ∥cu∥=∥u∥⋅c2⟨u,u⟩=∣c∣ ∥u∥2\begin{aligned} \lvert \langle \mathbf{u}, c\mathbf{u} \rangle \rvert &= \lvert c \rvert \, \langle \mathbf{u}, \mathbf{u} \rangle = \lvert c \rvert \, \lVert \mathbf{u} \rVert^2 \\ \lVert \mathbf{u} \rVert \, \lVert c\mathbf{u} \rVert &= \lVert \mathbf{u} \rVert \cdot \sqrt{c^2 \langle \mathbf{u}, \mathbf{u} \rangle} = \lvert c \rvert \, \lVert \mathbf{u} \rVert^2 \end{aligned}

Test yourself

Which axiom does ⟨u,v⟩=uTAv\langle \mathbf{u}, \mathbf{v} \rangle = \mathbf{u}^TA\mathbf{v} with A=[1221]A = \begin{bmatrix} 1 & 2 \\ 2 & 1 \end{bmatrix} fail?

  1. Symmetry
  2. Linearity
  3. Positivity
  4. None, it is an inner product

What does the Cauchy–Schwarz inequality guarantee?

  1. ∥u+v∥=∥u∥+∥v∥\lVert \mathbf{u} + \mathbf{v} \rVert = \lVert \mathbf{u} \rVert + \lVert \mathbf{v} \rVert
  2. ⟨u,v⟩/(∥u∥∥v∥)\langle \mathbf{u}, \mathbf{v} \rangle / (\lVert \mathbf{u} \rVert \lVert \mathbf{v} \rVert) lies in [−1,1][-1, 1], so angles are defined
  3. ⟨u,v⟩≥0\langle \mathbf{u}, \mathbf{v} \rangle \ge 0 for all u,v\mathbf{u}, \mathbf{v}
  4. Every inner product is the dot product

Under ⟨f,g⟩=∫01f g dx\langle f, g \rangle = \int_0^1 f\,g\,dx, which pair is orthogonal?

  1. 11 and xx
  2. xx and x2x^2
  3. 11 and x−1/2x - 1/2
  4. xx and 1−x1 - x