Distances, Angles and Orthogonality
Once you have an inner product you get three more things for free: the distance between two vectors, the angle between them, and the notion of being perpendicular. Nearest-neighbour search, cosine similarity of embeddings and orthonormal bases all rest on these three ideas.
Distance and angle
The distance between and is the norm of their difference, and the angle between nonzero vectors comes from Cauchy-Schwarz:
is the cosine similarity. It ignores length and only measures direction: for the same direction, for perpendicular, for opposite. For unit vectors, distance and cosine are tied together:
so ranking unit vectors by Euclidean distance or by cosine similarity gives the same order. That is why vector databases normalize embeddings.
import numpy as np
docs = {"cats": np.array([3.0, 1.0, 0.0]), "kittens": np.array([6.0, 2.5, 0.2]),
"stocks": np.array([0.1, 0.2, 4.0])}
q = np.array([1.0, 0.3, 0.0])
def cos(a, b): return a @ b / (np.linalg.norm(a) * np.linalg.norm(b))
for name, v in docs.items():
print(f"{name:8s} cosine {cos(q, v):.4f} euclid {np.linalg.norm(q - v):.4f}")
u = {k: v / np.linalg.norm(v) for k, v in docs.items()}
qn = q / np.linalg.norm(q)
print("unit vectors, 2 - 2cos = squared distance:",
all(np.isclose(np.linalg.norm(qn - v)**2, 2 - 2 * (qn @ v)) for v in u.values()))cats cosine 0.9995 euclid 2.1190
kittens cosine 0.9942 euclid 5.4663
stocks cosine 0.0383 euclid 4.1012
unit vectors, 2 - 2cos = squared distance: True
Raw Euclidean distance says "kittens" is farther from the query than "stocks" only because its vector is longer; the cosine looks past the length.
Orthogonality and orthonormal bases
and are orthogonal, , if . A basis is orthonormal if for and each has length 1. Coordinates in an orthonormal basis are just inner products, no system to solve:
A square matrix with orthonormal columns is orthogonal: , so , and it preserves lengths and angles. The Gram-Schmidt process (or np.linalg.qr) turns any basis into an orthonormal one.
import numpy as np
B = np.array([[1.0, 1.0, 0.0],
[1.0, 0.0, 1.0],
[0.0, 1.0, 1.0]]).T # an ordinary basis as columns
Q, _ = np.linalg.qr(B) # orthonormalize
print("Q^T Q = I:", np.allclose(Q.T @ Q, np.eye(3)))
x = np.array([2.0, -1.0, 3.0])
coords = Q.T @ x # inner products with the basis vectors
print("coordinates:", np.round(coords, 4) + 0)
print("rebuilt x:", np.round(Q @ coords, 10))
print("length kept:", np.isclose(np.linalg.norm(Q @ x), np.linalg.norm(x)))Q^T Q = I: True
coordinates: [-0.7071 3.6742 0. ]
rebuilt x: [ 2. -1. 3.]
length kept: True
Orthogonal complement
The orthogonal complement of a subspace is the set of all vectors orthogonal to every vector of , and . A plane through the origin in has a line as its complement, spanned by the normal vector. The null space of is the orthogonal complement of its row space.
In software
- Semantic search ranks documents by cosine similarity of embeddings (Embeddings and Cosine Similarity); FAISS and pgvector offer inner-product and indexes.
- Orthogonal weight initialization (
torch.nn.init.orthogonal_) keeps signal norms stable through deep linear stacks. - QR factorization solves least squares more stably than the normal equations.
Exercises
Exercise 1: Angle by hand
Find the angle between and .
Answer
.
Solution
, so .
Exercise 2: Orthonormal coordinates
With the orthonormal basis , , find the coordinates of .
Answer
.
Solution
Check: .
Exercise 3: Nearest neighbour (code)
Which of , , has the largest cosine similarity with ?
Answer
.
Solution
import numpy as np
q = np.array([2.0, 3.0])
for v in ([1.0, 2.0], [4.0, 4.0], [-1.0, 3.0]):
v = np.array(v)
print(v, round(float(q @ v / np.linalg.norm(q) / np.linalg.norm(v)), 4))[1. 2.] 0.9923
[4. 4.] 0.9806
[-1. 3.] 0.6139
Test yourself
The cosine similarity of and is…
- 0
- 0.5
- 1
- 2
If is orthogonal, then equals…
For unit vectors, equals…