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 in . Everything we did with it (lengths, angles, projections) relied on only a few properties. An inner product on a real vector space is any function that has those properties for all in and every scalar :
| Axiom | Statement |
|---|---|
| Symmetry | |
| Linearity | and |
| Positivity | , and only when |
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 |
|---|---|---|
| standard (dot product) | ||
| , symmetric positive definite | weighted / -inner product | |
| continuous functions on | inner product | |
| matrices | Frobenius |
The weighted version with is : it stretches the second coordinate before measuring. Not every matrix works. With the vector gives , so positivity fails. Which matrices do work is the subject of Positive Definite Matrices.
For functions, positivity holds because for any continuous that is not identically zero.
Norm, distance and angle
Every inner product gives a norm and a distance . Two inequalities hold in every inner product space:
Proof sketch of Cauchy–Schwarz. If both sides are 0. Otherwise, for every real , . A quadratic in that never goes negative has discriminant , so . The triangle inequality follows by expanding and using Cauchy–Schwarz on the middle term.
Because of Cauchy–Schwarz, the ratio always lies in , so we can define the angle between and by , even between two functions.
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 we can measure polynomials. SymPy does the integrals exactly:
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 and are "close" on (about apart), while and are perpendicular. That last fact is the seed of Gram-Schmidt and Legendre polynomials.
In software
- Weighted distances. A -nearest-neighbour search on features measured in different units (metres and grams) uses a diagonal weight matrix; the Mahalanobis distance uses , the inverse covariance.
- Frobenius norm. 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 , 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 , check which inner product is meant. Many "mysterious" factors of 2 or of a matrix come from a weighted inner product.
Exercises
Exercise 1: Check the axioms
Is an inner product on ?
Answer
No. It is symmetric and linear, but positivity fails: .
Solution
Symmetry and linearity hold because it is with . Positivity needs for every ; the vector gives , and gives 0 although it is not the zero vector. Either failure is enough.
Exercise 2: A weighted length
In with , find and .
Answer
and the inner product is .
Solution
Exercise 3: Frobenius inner product
Compute for and , then check it equals the sum of entrywise products.
Answer
.
Solution
Entrywise: .
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 , find the angle between and .
Answer
, so .
Solution
, , . So and .
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 when . (The converse, that equality forces and to be parallel, follows from the proof sketch above: equality makes the discriminant zero, so for some .)
Answer
Both sides equal .
Solution
Test yourself
Which axiom does with fail?
- Symmetry
- Linearity
- Positivity
- None, it is an inner product
What does the Cauchy–Schwarz inequality guarantee?
- lies in , so angles are defined
- for all
- Every inner product is the dot product
Under , which pair is orthogonal?
- and
- and
- and
- and