Which of the images in Fig. 1 is closest to the original F? Measure it the standard way, as the Euclidean distance between two arrays of 1,024 pixel values. The E comes first, then the P, then the L, then a blank image. The same F, moved three pixels to the right, comes last.

Six 32 by 32 images: the letter F, then E at distance 4.98, P at 5.29, L at 6.12, a blank image at 7.55, and the same F moved three pixels to the right at distance 8.32, the largest.
Fig. 1. Euclidean distance from the original F to each of the other images, computed on the raw 32 × 32 pixel arrays, closest first. The F moved three pixels to the right (red) is farther from the original than every other image, including a blank one. To move the F or pick other letters, open the interactive playground.

The arithmetic is right. The surprise comes from an earlier choice: which notion of distance to use. Pixel distance treats each of the 1,024 pixels as an unrelated number. Shuffle the pixels of every image in the same way, and not a single distance changes. So this distance has no way to see that the F moved. It doesn't know that pixels have neighbours.

This post builds the structures that make close mean something. Vector spaces let us add and scale. Norms measure length, metrics measure distance, and inner products measure angle. Each one is a choice, and each choice decides what a model can see. By the end, the puzzle has two answers: why the F is far from itself, and a distance under which it isn't.

This is Part 3 of Math for GDL, which works through a review paper by Sáez de Ocáriz Borde & Bronstein (2025), called the review below, one section at a time. So far the series has added two layers to a bare set. Part 1 had sets and maps: enough to define a hypothesis class, but with no way to say that two elements are close. Part 2 added symmetry: groups, their actions, and maps that respect them. Neither can measure how far apart two images are, which is what the puzzle is about. This part, covering §1.3 and §2, adds that measurement, in four steps. Fig. 2 shows where each step sits on the map of the series so far.

A map of structures on a set. Inside a dashed box labelled sets, from Part 1, are three regions. The left region is metric spaces, which have a distance; examples only there are distance along the Earth between stations, the Hausdorff distance between point clouds, and shift orbits of images. A dotted region labelled groups, from Part 2, contains the whole vector space region; groups that are not vector spaces, such as C4 and the permutations S n, sit in its lower strip. The vector space region, which can add and scale, has examples R n, functions on a set, tensors, and the positive numbers with multiplication as addition. Where metric spaces and vector spaces overlap sit vector spaces that also have a metric; one example, R n with the sum of square roots of coordinate differences, has a metric but no norm. Inside the overlap is a box for normed spaces, which have a length, with examples L1, L-infinity and L-p for p other than 2, and inside that a box for inner product spaces, which have an angle, with examples the dot product, the trace of A times B transpose, and the integral of f times g. A note at the bottom says that groups also act on every region, and the useful actions keep its structure. Sets (Part 1): the background, before any structure Metric spaces distance d(x, y) Vector spaces add and scale Normed: length ‖u‖, d(u, v) = ‖u − v‖ Inner product: angle ⟨u, v⟩, ‖u‖ = √⟨u, u⟩ Groups (Part 2) combine and undo • km between stations • Hausdorff distance • shift orbits of images • L₁, L∞, Lₚ with p ≠ 2 • dot product, L₂ • tr(ABᵀ), ∫ f g • ℝⁿ with Σ √|uᵢ − vᵢ|: a metric, but no norm • ℝⁿ, functions 𝓕(Ω), tensors [B, C, H, W] • positive numbers with ⊕ and ⊙ no length chosen yet • C₄, Sₙ: groups that cannot be scaled, so not vector spaces Groups also act on every region; the useful actions keep its structure (Fig. 3).
Fig. 2. The series so far in one picture. Every region is a set (Part 1, dashed) with some structure added. This post adds two kinds that live side by side: metrics (left), which measure distance, and vector spaces (right), which add and scale. Groups (Part 2) appear twice. As a structure, they contain every vector space, because rules 1 to 4 of Definition 1 say that \( (V, +) \) is a group (dotted). As symmetries, they act on every region (bottom note). A norm needs both a vector space and a metric, \( d(u, v) = \|u - v\| \), so normed spaces sit in the overlap, and inner product spaces sit inside those. None of these steps can be reversed. \( \mathbb{R}^n \) with \( \sum_i \sqrt{|u_i - v_i|} \) has addition and a metric but no norm behind it. \( L_1 \) is a norm with no inner product. Distance along the Earth is a metric on a set with no addition at all.
Table of contents
  1. Notation
  2. Vectors without arrows
  3. Tensors and Einstein summation
  4. Norms: length
  5. Metrics: distance
  6. Inner products: angle
  7. Angles in high dimensions
  8. Quick summary
  9. Open questions
  10. Code
  11. Citation
  12. References

Notation

SymbolMeaning
\( V \), \( \mathbb{F} \)A vector space and its field of scalars (here almost always \( \mathbb{R} \))
\( u, v, w \)Vectors; \( \alpha, \beta \) are scalars
\( \mathcal{F}(\Omega) \)All real-valued functions on a set \( \Omega \)
\( \|u\|_p \)The \( L_p \) norm of \( u \); \( \|u\| \) is a norm in general
\( d(x, y) \)The distance between \( x \) and \( y \) under a metric \( d \)
\( d_H(A, B) \)The Hausdorff distance between two sets of points
\( \langle u, v \rangle \)An inner product; on \( \mathbb{R}^n \), the dot product \( u^\top v \)
\( u \perp v \)\( u \) and \( v \) are orthogonal: \( \langle u, v \rangle = 0 \)
\( T_{ijk} v^j \)Einstein summation: a repeated index is summed over
\( t \)One day's temperatures at the four stations: \( t = (18, 12, 15, 9) \)

Vectors without arrows

Already comfortable with linear algebra? Skim to the function-space example and the strange vector space of positive numbers; the rest is a refresher.

Introductory courses describe vectors as arrows with a direction and a length, or as arrays of numbers. The review calls both pictures a crime against humanity, and it has a point. An arrow needs a length, which is extra structure (a norm). An array needs a choice of coordinate axes, which is also extra structure (a basis). Underneath both is something simpler. A vector is anything you can add to another vector and multiply by a number, as long as these two operations follow a short list of rules.

Definition 1 (Vector space). A vector space over a field \( \mathbb{F} \) is a set \( V \) with an addition \( + : V \times V \to V \) and a scalar multiplication \( \cdot : \mathbb{F} \times V \to V \) such that, for all \( u, v, w \in V \) and \( \alpha, \beta \in \mathbb{F} \):

  1. addition is associative: \( u + (v + w) = (u + v) + w \);
  2. addition is commutative: \( u + v = v + u \);
  3. there is a zero vector \( 0 \in V \) with \( u + 0 = u \);
  4. every \( v \) has a negative \( -v \) with \( v + (-v) = 0 \);
  5. \( \alpha \cdot (u + v) = \alpha \cdot u + \alpha \cdot v \);
  6. \( (\alpha + \beta) \cdot v = \alpha \cdot v + \beta \cdot v \);
  7. \( \alpha \cdot (\beta \cdot v) = (\alpha \beta) \cdot v \);
  8. \( 1 \cdot v = v \).

The first four rules say that \( (V, +) \) is an abelian group, in the sense of Part 2. The other four say how scaling interacts with adding. The same symbols do double duty: \( + \) adds scalars or vectors, and \( 0 \) is the zero scalar or the zero vector. Context tells them apart.

The standard example is \( \mathbb{R}^n \), lists of \( n \) numbers added and scaled entry by entry. Take the four weather stations of Parts 1 and 2: Aachen, Berlin, Cologne and Dresden. One day's readings, \( t = (18, 12, 15, 9) \) in that order, form a vector in \( \mathbb{R}^4 \). The second example in the review is more important for this series. The real-valued functions on a set \( \Omega \), $$ \mathcal{F}(\Omega) = \{ f : \Omega \to \mathbb{R} \}, $$ form a vector space when added and scaled point by point: \( (f + g)(x) = f(x) + g(x) \) and \( (\alpha f)(x) = \alpha f(x) \). When \( \Omega \) is the set of four stations, a function on \( \Omega \) assigns a number to each station, which is exactly a temperature reading. So \( \mathcal{F}(\text{stations}) \) and \( \mathbb{R}^4 \) are the same space written two ways. A grayscale image is a function on a grid of pixels, and a signal on a graph is a function on its nodes. Geometric deep learning lives in spaces like these: functions on a domain, added and scaled point by point.

The rules are what matter, not what the elements look like. Here is a strange but valid example. Take the positive numbers, and define adding \( u \oplus v \) to mean the product \( uv \), and scaling \( \alpha \odot u \) to mean the power \( u^\alpha \). All eight rules hold. The zero vector is the number 1, since \( u \oplus 1 = u \), and the negative of \( u \) is \( 1/u \). Nothing about this space looks like an arrow, and yet it is a vector space. In fact the logarithm turns it into \( \mathbb{R} \) with ordinary addition.

Proposition 1 (Zero and negatives). In any vector space, \( 0 \cdot v = 0 \) and \( (-1) \cdot v = -v \) for every \( v \).

Proof

First, \( 0 \cdot v = (0 + 0) \cdot v = 0 \cdot v + 0 \cdot v \) by rule 6. Adding \( -(0 \cdot v) \) to both sides leaves \( 0 = 0 \cdot v \). Then: $$ \begin{aligned} v + (-1) \cdot v &= 1 \cdot v + (-1) \cdot v && \text{; rule 8} \\ &= (1 - 1) \cdot v && \text{; rule 6} \\ &= 0 \cdot v = 0 && \text{; just shown} \end{aligned} $$ So \( (-1) \cdot v \) is a negative of \( v \). A vector has only one negative (the argument of Part 2's Proposition 1), so \( (-1) \cdot v = -v \). ∎

The notebook checks this in the strange example: \( 0 \odot u = u^0 = 1 \) is the zero vector, and \( (-1) \odot u = 1/u \) is the negative. Just as useful are sets that fail. Probability vectors are not a vector space: add two of them and the entries sum to 2. Neither are unit vectors, since \( (1, 0) + (1, 0) \) has length 2. Rotations of 3D space are not one either. You can compose them, but adding two rotation matrices does not give a rotation.

What about the array picture? Pick a basis: a list of vectors such that every vector is exactly one combination of them. Each vector then gets coordinates, the weights in that combination. But the coordinates belong to the basis, not to the vector. In the basis made of the columns of $$ B = \tfrac{1}{2} \begin{pmatrix} 1 & 1 & 1 & 1 \\ 1 & -1 & 1 & -1 \\ 1 & 1 & -1 & -1 \\ 1 & -1 & -1 & 1 \end{pmatrix}, $$ the readings \( t = (18, 12, 15, 9) \) have coordinates \( (27, 6, 3, 0) \). The first coordinate measures the overall level. The second measures how much warmer the west (Aachen and Cologne) is than the east. Same vector, different array.

Why ML cares. Every layer of a neural network turns vectors into vectors. Nearly every trick that mixes data uses addition and scaling, from averaging gradients over a batch to interpolating between two embeddings. Whether those operations mean anything for the data is a separate question. Adding two images pixel by pixel gives a valid vector, but usually not a picture of anything, which is the first hint of the puzzle in Fig. 1.

Tensors and Einstein summation

If you already use einsum, skip to Norms.

In code, data comes as tensors: arrays with any number of indices. A scalar has none, a vector has one, a matrix has two. A batch of color images has four, with shape [B, C, H, W] for batch size, channels, height and width. Arrays of a fixed shape add and scale entry by entry, so they form a vector space. A batch of eight \( 3 \times 32 \times 32 \) images is a single vector in a space of dimension \( 8 \cdot 3 \cdot 32 \cdot 32 = 24{,}576 \).

The Einstein summation convention keeps formulas with many indices short: an index that appears twice in a product is summed over. The dot product is \( u_i v^i \), and a matrix times a vector is \( M_{ij} v^j \). Take the review's \( 3 \times 2 \times 2 \) example tensor, with entries 1 to 12. Contract it with a vector over its middle index: $$ T_{ijk} v^j = M_{ik}. $$ With \( v = (1, 10) \), this gives $$ M = \begin{pmatrix} 31 & 42 \\ 75 & 86 \\ 119 & 130 \end{pmatrix}. $$ The result has the two free indices \( i \) and \( k \), so it is a matrix. NumPy's einsum takes this notation literally: np.einsum("ijk,j->ik", T, v). A \( 1 \times 1 \) convolution, which mixes channels at every pixel, is one line, np.einsum("oc,bchw->bohw", W, X): sum over the input channel \( c \), keep everything else.

Aside: what mathematicians mean by tensor

In deep learning, tensor means a multi-dimensional array, and that is how the review uses it. In mathematics and physics, a tensor is a multilinear map. An array is only its coordinates in some basis, and those coordinates change in a definite way when the basis changes. The upper and lower indices in \( u_i v^i \) come from that tradition, where they separate two kinds of objects. In NumPy, and in this series, the position of an index carries no meaning.

Norms: length

A vector space has no notion of size. To ask whether one reading is more extreme than another, or whether a weight vector is large, we need a norm.

Definition 2 (Norm). A norm on a real vector space \( V \) is a function \( \|\cdot\| : V \to \mathbb{R} \) such that, for all \( u, v \in V \) and \( \alpha \in \mathbb{R} \):

  1. absolute homogeneity: \( \|\alpha u\| = |\alpha|\, \|u\| \);
  2. triangle inequality: \( \|u + v\| \le \|u\| + \|v\| \);
  3. positive definiteness: \( \|u\| = 0 \) implies \( u = 0 \).

The pair \( (V, \|\cdot\|) \) is a normed space.

Two familiar facts follow from the rules. With \( \alpha = 0 \), rule 1 gives \( \|0\| = 0 \). And no length is negative: \( 0 = \|u - u\| \le \|u\| + \|-u\| = 2\|u\| \). The main examples on \( \mathbb{R}^n \) are the \( L_p \) norms, $$ \|u\|_p = \Big( \sum_{i=1}^n |u_i|^p \Big)^{1/p}, $$ and their limit as \( p \to \infty \), \( \|u\|_\infty = \max_i |u_i| \). Each one measures something different. Take the day's readings minus their mean, the anomaly \( (4.5, -1.5, 1.5, -4.5) \). Its \( L_1 \) norm, 12, is the total deviation across stations. Its \( L_\infty \) norm, 4.5, is the worst single station. Its \( L_2 \) norm, \( \sqrt{45} \approx 6.71 \), lies in between, as it always does: \( \|u\|_\infty \le \|u\|_2 \le \|u\|_1 \).

Two small corrections to the review. It calls rule 1 positive homogeneity, a name usually kept for the weaker rule with \( \alpha \ge 0 \). And the \( L_p \) formula is a norm only for \( p \ge 1 \), a condition it leaves out. For \( p = 1/2 \) the triangle inequality fails: \( \|(1, 1)\|_{1/2} = (1 + 1)^2 = 4 \), while \( \|(1, 0)\|_{1/2} + \|(0, 1)\|_{1/2} = 2 \).

Three panels showing unit balls in the plane: a diamond for L1, a circle for L2 and a square for L-infinity. Each is drawn with its image under a 30-degree rotation. The rotated circle coincides with the circle; the rotated diamond and square do not coincide with the originals.
Fig. 3. The unit ball, all vectors of length at most 1, for three norms on the plane (blue), and the same ball turned by 30° (dashed red). Only the \( L_2 \) ball is unchanged, so only \( L_2 \) lengths survive every rotation. The \( L_1 \) diamond and the \( L_\infty \) square come back to themselves only after a multiple of 90°.

Fig. 3 links norms back to Part 2. A linear map keeps every length exactly when it maps the unit ball onto itself. So the symmetries of a norm are the symmetries of its unit ball, and they form a group. For \( L_2 \) the ball is round, and the group contains every rotation and reflection. For \( L_1 \) the ball is a diamond, and in the plane the group shrinks to the diamond's 8 symmetries: four rotations and four reflections. The notebook checks all 8, and sweeps every whole-degree rotation to find that only 0°, 90°, 180° and 270° keep \( L_1 \) lengths. A rotation by 45° stretches \( (1, 0) \) to \( L_1 \) length \( \sqrt{2} \). Permutations of the coordinates keep every \( L_p \) norm, which is why relabelling the stations never changes one.

Norms extend to functions by turning the sum into an integral: \( \|f\|_p = \big( \int_\Omega |f(x)|^p \, dx \big)^{1/p} \). For example, \( \|\sin\|_2 = \sqrt{\pi} \) on \( [0, 2\pi] \). Read a function as a vector with one coordinate per point of \( \Omega \), and the integral is the natural limit of the sum.

Why ML cares. When a model must not depend on how a molecule is turned, it should only use quantities that rotations leave alone. E(n)-equivariant graph neural networks (Satorras et al. 2021) feed squared Euclidean distances between atoms into each layer for exactly this reason. An \( L_1 \) distance would change with the molecule's orientation. Norms also regularize. L2 regularization adds the squared \( L_2 \) norm of the weights to the loss, while weight decay shrinks the weights directly at every step. The review says weight decay penalizes the norm, but the penalty is its square. Loshchilov & Hutter (2019) showed that the two coincide for plain SGD, once the coefficient is rescaled by the learning rate. For Adam they differ, and decoupling them gave the AdamW optimizer.

Metrics: distance

A norm measures one vector. A metric measures how far apart two things are, and it needs much less: no addition, no scaling, just a set.

Definition 3 (Metric). A metric on a set \( X \) is a function \( d : X \times X \to \mathbb{R} \) such that, for all \( x, y, z \in X \):

  1. non-negativity: \( d(x, y) \ge 0 \);
  2. identity of indiscernibles: \( d(x, y) = 0 \) if and only if \( x = y \);
  3. symmetry: \( d(x, y) = d(y, x) \);
  4. triangle inequality: \( d(x, z) \le d(x, y) + d(y, z) \).

The pair \( (X, d) \) is a metric space.

The review introduces metrics through norms and then remarks that not every metric comes from one. Defining a metric on any set, as above, makes the direction of the relationship clear.

Proposition 2 (Norms give metrics). If \( \|\cdot\| \) is a norm on \( V \), then \( d(u, v) = \|u - v\| \) is a metric on \( V \).

Proof

Non-negativity holds because norms are non-negative. Next, \( d(u, v) = 0 \) means \( \|u - v\| = 0 \), so \( u - v = 0 \) and \( u = v \). Symmetry: \( \|u - v\| = \|(-1)(v - u)\| = |-1|\,\|v - u\| = \|v - u\| \). For the triangle inequality, write \( u - w = (u - v) + (v - w) \) and apply the norm's triangle inequality. ∎

The converse fails. On \( \mathbb{R}^n \), the review's example \( d(u, v) = \sum_i \sqrt{|u_i - v_i|} \) is a metric. But a metric that comes from a norm must grow in proportion to scale, \( d(\alpha u, \alpha v) = |\alpha|\, d(u, v) \), and doubling both points multiplies this one by \( \sqrt{2} \) instead. A more everyday example needs no vector space at all. Distance along the Earth's surface is a metric on the four stations: Aachen to Cologne is 64 km, Aachen to Dresden is 537 km. Adding Aachen to Berlin means nothing, but the distance between them is well defined. Graphs, coming later in the series, work the same way: on a connected graph, the number of edges on the shortest path between two nodes is a metric.

The review also names two relaxations. A pseudo-metric drops half of rule 2: distinct points may be at distance 0. A quasi-metric, in the review's sense, relaxes the triangle inequality to \( d(x, z) \le C\,(d(x, y) + d(y, z)) \) for some constant \( C \ge 1 \). Squared Euclidean distance, the quantity k-means minimizes, is the standard example. On a line, the points 0, 1 and 2 give \( 4 > 1 + 1 \), so the triangle inequality fails, but it always holds with \( C = 2 \). (The name has two meanings: many other authors use quasi-metric for a distance that need not be symmetric.)

Pseudo-metrics answer the puzzle. Pixel distance failed because it compares images position by position. Instead, compare an image with every shifted copy of the other one and keep the best match: $$ d_{\text{shift}}(x, y) = \min_{g \in G} \| x - g \cdot y \|_2. $$ Here \( G \) is the group of 2D cyclic shifts from Part 2. A \( 32 \times 32 \) grid has 1,024 of them. Under this distance the moved F is at distance 0 from the original. The E is still 4.69 away, since no shift turns an E into an F.

Proposition 3 (Orbit distance). Let a finite group \( G \) act on a normed space \( V \) by linear maps that keep the norm, \( \|g \cdot u\| = \|u\| \). Then \( d_G(x, y) = \min_{g \in G} \|x - g \cdot y\| \) is a pseudo-metric, and \( d_G(x, y) = 0 \) exactly when \( y \) lies in the orbit of \( x \).

Proof

Taking \( g = e \) gives \( d_G(x, x) = 0 \). Symmetry: \( \|x - g \cdot y\| = \|g^{-1} \cdot (x - g \cdot y)\| = \|g^{-1} \cdot x - y\| = \|y - g^{-1} \cdot x\| \), and as \( g \) runs over \( G \), so does \( g^{-1} \). Triangle inequality: pick \( g \) and \( h \) that achieve \( d_G(x, y) \) and \( d_G(y, z) \). Then $$ \begin{aligned} d_G(x, z) &\le \|x - gh \cdot z\| && \text{; } gh \in G \\ &\le \|x - g \cdot y\| + \|g \cdot y - g \cdot (h \cdot z)\| && \text{; triangle ineq.} \\ &= \|x - g \cdot y\| + \|g \cdot (y - h \cdot z)\| && \text{; } g \text{ is linear} \\ &= d_G(x, y) + d_G(y, z) && \text{; } g \text{ keeps norms} \end{aligned} $$ Finally, the minimum over a finite set is attained, so \( d_G(x, y) = 0 \) means \( x = g \cdot y \) for some \( g \), and then \( y = g^{-1} \cdot x \) is in the orbit of \( x \). Conversely, if \( y = h \cdot x \), then \( d_G(x, y) \le \|x - h^{-1} \cdot y\| = 0 \). ∎

Shifts move pixels around without changing their values, so they keep the Euclidean norm, and Proposition 3 applies. The orbit distance is not a metric on images, because distinct images in one orbit sit at distance 0. It is a metric on the orbits themselves, since replacing \( x \) by any \( h \cdot x \) leaves every \( d_G(x, y) \) unchanged. That is the geometric view of invariance from Part 2: decide which differences don't matter, then measure distance between the classes that are left.

One more distance from the review compares sets of points, such as two scans of the same object.

Definition 4 (Hausdorff distance). For non-empty subsets \( A, B \) of a metric space \( (X, d) \), $$ d_H(A, B) = \max\Big\{ \sup_{a \in A} \inf_{b \in B} d(a, b),\; \sup_{b \in B} \inf_{a \in A} d(a, b) \Big\}. $$

In words: find the point of either set that lies farthest from the other set, and report that distance. For finite point clouds, sup and inf are max and min, and \( d_H \) is always a metric. For infinite sets it is a metric on the closed and bounded ones. (The review asks only for closed sets, but two unbounded sets can be infinitely far apart.) Since \( d_H \) only sees sets, the order in which points are listed doesn't matter, as with the sets of Part 1.

The Hausdorff distance is also unforgiving. In the notebook, two copies of a 50-point cloud that differ by small jitter are about 0.03 apart. Add one stray point and the Hausdorff distance jumps to about 8. The Chamfer distance averages each point's distance to the nearest point of the other set, instead of taking the worst case. It only moves from 0.03 to 0.19.

Why ML cares. Metrics decide which points a model treats as neighbours. Graph networks for proteins and other molecules often connect each atom to the atoms within a fixed Euclidean radius, or to its \( k \) nearest ones. VQ-VAE (van den Oord et al. 2017) replaces each encoder output by the nearest entry of a learned codebook, in Euclidean distance. Networks that generate point clouds need a loss that ignores the order of points, and Fan et al. (2017) trained theirs with the Chamfer distance and the Earth Mover's distance. The puzzle itself is well known in image processing. Wang et al. (2004) showed that images with the same pixel error can look very different to people, and proposed the SSIM index, which compares local structure instead.

Inner products: angle

A norm says how long a vector is, but not which way it points relative to another. For angles we need one more structure.

Definition 5 (Inner product). An inner product on a real vector space \( V \) is a function \( \langle \cdot, \cdot \rangle : V \times V \to \mathbb{R} \) such that, for all \( u, v, w \in V \) and \( \alpha \in \mathbb{R} \):

  1. symmetry: \( \langle u, v \rangle = \langle v, u \rangle \);
  2. linearity: \( \langle \alpha u + w, v \rangle = \alpha \langle u, v \rangle + \langle w, v \rangle \);
  3. positive definiteness: \( \langle u, u \rangle \ge 0 \), with equality only for \( u = 0 \).

The pair \( (V, \langle \cdot, \cdot \rangle) \) is an inner product space.

The review labels rule 3 positive semi-definiteness, but the rule it states, with equality only at zero, is positive definiteness. It also treats complex spaces, where symmetry becomes \( \langle u, v \rangle = \overline{\langle v, u \rangle} \); this post stays real. The examples:

  • on \( \mathbb{R}^n \), the dot product \( \langle u, v \rangle = \sum_i u_i v_i = u^\top v \);
  • on matrices, \( \langle A, B \rangle = \operatorname{tr}(A B^\top) \), which is the dot product of the entries, \( \sum_{ij} A_{ij} B_{ij} \);
  • on functions, \( \langle f, g \rangle = \int_\Omega f(x) g(x)\, dx \). On \( [0, 2\pi] \), \( \sin \) and \( \cos \) are orthogonal, and \( \langle \sin, \sin \rangle = \pi \), matching \( \|\sin\|_2 = \sqrt{\pi} \).

Every inner product gives a norm, \( \|u\| = \sqrt{\langle u, u \rangle} \), and the dot product gives \( L_2 \). One inequality ties the two together, and it turns up all over analysis.

Proposition 4 (Cauchy–Schwarz). For all \( u, v \) in an inner product space, \( |\langle u, v \rangle| \le \|u\|\, \|v\| \).

Proof

If \( v = 0 \), both sides are 0. Otherwise, for every real \( s \), $$ \begin{aligned} 0 &\le \|u - s v\|^2 && \text{; rule 3} \\ &= \|u\|^2 - 2s \langle u, v \rangle + s^2 \|v\|^2 && \text{; rules 1, 2} \\ &= \|u\|^2 - \frac{\langle u, v \rangle^2}{\|v\|^2} && \text{; take } s = \tfrac{\langle u, v \rangle}{\|v\|^2} \end{aligned} $$ Multiply by \( \|v\|^2 \) and take square roots. ∎

Cauchy–Schwarz does two jobs. First, it makes \( \sqrt{\langle u, u \rangle} \) a norm. The hard rule is the triangle inequality, and the proof is one expansion with the cross term in blue: $$ \begin{aligned} \|u + v\|^2 &= \|u\|^2 + \textcolor{#3b82f6}{2\langle u, v \rangle} + \|v\|^2 && \text{; linearity, symmetry} \\ &\le \|u\|^2 + \textcolor{#3b82f6}{2\|u\|\,\|v\|} + \|v\|^2 && \text{; Cauchy–Schwarz} \\ &= (\|u\| + \|v\|)^2. \end{aligned} $$ Second, it guarantees that $$ \cos \angle(u, v) = \frac{\langle u, v \rangle}{\|u\|\, \|v\|} $$ lies between −1 and 1, so it is the cosine of a genuine angle.

Two vectors are orthogonal, \( u \perp v \), when \( \langle u, v \rangle = 0 \). The stations give a familiar example. Take a second day, \( t' = (16, 13, 14, 12) \), and subtract each day's mean. The cosine of the angle between the two anomalies is 0.983, which is exactly the Pearson correlation between the two days. Correlation is an angle: these anomalies are about 10.7° apart.

Not every norm comes from an inner product. A norm comes from one exactly when it satisfies the parallelogram law, $$ 2\|u\|^2 + 2\|v\|^2 = \|u + v\|^2 + \|u - v\|^2. $$ This is a theorem of Jordan & von Neumann (1935). The inner product is then recovered by the polarization identity, \( \langle u, v \rangle = \tfrac{1}{4} \big( \|u + v\|^2 - \|u - v\|^2 \big) \). Try \( u = (1, 0) \) and \( v = (0, 1) \). For \( L_2 \) both sides are 4. For \( L_1 \) the left side is 4 and the right side is 8. For \( L_\infty \) the right side is 2. So \( L_1 \) and \( L_\infty \) have no angles behind them, and polarization applied to \( L_1 \) produces something that is not even linear.

Theorem 1 (Pythagoras). If \( v_1, \ldots, v_n \) are pairwise orthogonal, then \( \big\| \sum_i v_i \big\|^2 = \sum_i \|v_i\|^2 \).

Proof

Expand with linearity and symmetry: $$ \Big\| \sum_i v_i \Big\|^2 = \sum_i \|v_i\|^2 + \underbrace{\textcolor{#e0453a}{2 \sum_{i < j} \langle v_i, v_j \rangle}}_{\text{cross terms}}. $$ Every cross term is 0. ∎

Aside: a slip in the review about non-orthogonal vectors

After Theorem 1, the review says that for non-orthogonal vectors the sum of squared norms is at most the squared norm of the sum. That inequality can go either way. The expansion in the proof shows why: the cross terms \( \langle v_i, v_j \rangle \) can be positive or negative. With \( v = (1, 2) \), adding \( v \) to itself gives \( \|2v\|^2 = 20 \), more than \( \|v\|^2 + \|v\|^2 = 10 \). Adding \( v \) and \( -v \) gives \( \|0\|^2 = 0 \), less than 10. The notebook checks both.

The inner product also completes the link to Part 2. The linear maps that keep every inner product are the matrices with \( Q^\top Q = I \), the rotations and reflections, which form the orthogonal group \( O(n) \). Since lengths, distances and angles are all built from the inner product, these maps keep all three. That is why the \( L_2 \) ball in Fig. 3 is round.

Why ML cares. The dot product of two embeddings grows with their lengths, and a length can reflect things other than meaning, such as the overall scale of an activation. Dividing by the lengths leaves the cosine similarity, which only sees direction. It is not an inner product itself (it is not linear), but it is the cosine of the angle that the dot product defines. SimCLR (Chen et al. 2020) learns image features this way. It pushes the cosine similarity up between two augmented views of the same image, and down between views of different images.

Angles in high dimensions

The review ends §2 with a fact about high dimensions that low-dimensional intuition gets wrong. Pick two random directions in a space with many dimensions, and they are almost always nearly orthogonal.

Left: histograms of the angle between two random vectors in 3, 30, 300 and 3,000 dimensions; the spread narrows sharply around 90 degrees as the dimension grows. Right: the largest softmax weight over 10 random keys against the dimension; with raw dot products it climbs from about 0.5 to nearly 1, while with scores divided by the square root of d it stays near 0.32.
Fig. 4. Left: the angle between two random vectors with independent standard normal entries, in 3 to 3,000 dimensions (20,000 pairs each). Right: the largest softmax weight when a random query attends to 10 random keys, averaged over 4,000 queries. Raw dot products (red) concentrate almost all the weight on one key as the dimension grows. Dividing by \( \sqrt{d} \) (blue) keeps it steady.

The cosine between two random vectors is a sum of \( d \) small independent terms, divided by the lengths. Its typical size is \( 1/\sqrt{d} \). The notebook confirms the \( 1/\sqrt{d} \) spread for every dimension in Fig. 4. In 3,000 dimensions, more than 99.9% of random pairs are within 6° of a right angle. Even large collections stay spread out. Draw 10,000 random directions in 1,000 dimensions, ten times more vectors than dimensions, and all of the roughly 50 million pairs are at least 78° apart. More than 99% are within 5° of a right angle.

Why ML cares. Two places in deep learning depend on this. The first is attention. A Transformer scores a query \( q \) against a key \( k \) with \( q \cdot k / \sqrt{d} \). Vaswani et al. (2017) explain the factor. Suppose the entries of \( q \) and \( k \) are independent, with mean 0 and variance 1. Then $$ \begin{aligned} \operatorname{Var}(q \cdot k) &= \sum_{i=1}^{d} \operatorname{Var}(q_i k_i) && \text{; independent terms} \\ &= \sum_{i=1}^{d} \underbrace{\mathbb{E}[q_i^2]\,\mathbb{E}[k_i^2]}_{=\,1 \cdot 1} = d && \text{; mean 0} \end{aligned} $$ so the scores grow like \( \sqrt{d} \). Large scores push the softmax onto a single key, where its gradients are tiny. Dividing by \( \sqrt{d} \) brings the variance back to 1 whatever the dimension (Fig. 4, right). The review says the factor reduces sensitivity to vector norm. More precisely, it keeps the typical size of the scores fixed as \( d \) grows.

The second is superposition. Elhage et al. (2022) study small networks that store more features than they have dimensions. This works when features are sparse, rarely active at the same time: each feature gets its own direction, and the network accepts a little interference between directions that are not exactly orthogonal. As the review puts it, high dimensions can be a blessing as well as a curse.

Quick summary

The four structures side by side, with the running examples from this post.

StructureAddsExample in this postKept byIn ML
Vector spaceadding, scalingreadings \( t = (18, 12, 15, 9) \in \mathbb{R}^4 \)invertible linear mapsbatches, embeddings, interpolation
Normlengthanomaly: \( L_1 = 12 \), \( L_2 \approx 6.71 \), \( L_\infty = 4.5 \)\( L_2 \): all of \( O(n) \); \( L_1 \): 8 maps in the planeweight decay, rotation-invariant features
MetricdistanceAachen to Cologne, 64 kmisometriesneighbour graphs, VQ-VAE, point-cloud losses
Inner productangletwo days' correlation 0.983, about 10.7°\( O(n) \)cosine similarity, attention
Orbit distancedistance up to a groupmoved F at 0, E at 4.69the group itselfinvariant comparison

Open questions

Questions I'm carrying into the next parts:

  • What about changes that are not a group? The orbit distance fixes shifts because shifts form a group. Handwriting also bends and stretches a little, and small deformations don't form a group in the same way. How do you build a distance, or a model, that is stable under them?
  • What happens in infinite dimensions? Functions on an interval form a vector space with no finite basis. The review's §5 adds completeness to get Banach and Hilbert spaces. What goes wrong without it?
  • Should the distance be learned? A good embedding makes Euclidean distance meaningful where pixel distance is not. How much of representation learning is the search for the right metric?

Next, Part 4 turns to §3, vector calculus. It leans on this post more than it might seem. A derivative says how fast a function changes along each direction, but turning it into a gradient vector needs an inner product. Change the inner product, and the gradient points somewhere else.

Code

All checks in this post, and Figs. 1, 3 and 4, are in math-gdl-03-spaces.ipynb (NumPy and Matplotlib; runs in seconds). Each demo ends with a short exercise, such as giving the positive numbers ordinary addition and finding which vector-space rule breaks.

To skip the setup, open the interactive playground. It runs the same checks in your browser and adds controls for the exercises. You can move the letter in Fig. 1, rotate the unit balls of different norms, test the parallelogram law, or watch random vectors become orthogonal as the dimension grows. The first load can take up to half a minute while Python starts up. Its source is math-gdl-03-spaces-playground.py, a marimo notebook.

Corrections are welcome at .

Previous: Part 2, Groups, Symmetry, and Equivariance. Next: Part 4, Slope, Flow, and Smoothness. Series overview: Eight Puzzles That Helped Me Understand Geometric Deep Learning.

Citation

Cited as:

Pham, Anh-Duy. (Nov 2026). Length, distance, and angle. Math for GDL, Part 3. https://www.adpham.de/blog/math-gdl-03-spaces.html

Or

@article{pham2026mathgdl03,
  title   = "Length, Distance, and Angle",
  author  = "Pham, Anh-Duy",
  journal = "www.adpham.de",
  year    = "2026",
  month   = "Nov",
  url     = "https://www.adpham.de/blog/math-gdl-03-spaces.html"
}

References

  1. Sáez de Ocáriz Borde, H. & Bronstein, M. M. “Mathematical Foundations of Geometric Deep Learning.” arXiv:2508.02723 (2025).
  2. Wang, Z., Bovik, A. C., Sheikh, H. R. & Simoncelli, E. P. “Image quality assessment: From error visibility to structural similarity.” IEEE Transactions on Image Processing 13(4), 600–612 (2004).
  3. Satorras, V. G., Hoogeboom, E. & Welling, M. “E(n) equivariant graph neural networks.” Proceedings of the 38th International Conference on Machine Learning, PMLR 139, 9323–9332 (2021).
  4. Loshchilov, I. & Hutter, F. “Decoupled weight decay regularization.” International Conference on Learning Representations (2019).
  5. Fan, H., Su, H. & Guibas, L. J. “A point set generation network for 3D object reconstruction from a single image.” IEEE Conference on Computer Vision and Pattern Recognition, 605–613 (2017).
  6. van den Oord, A., Vinyals, O. & Kavukcuoglu, K. “Neural discrete representation learning.” Advances in Neural Information Processing Systems 30 (2017).
  7. Jordan, P. & von Neumann, J. “On inner products in linear, metric spaces.” Annals of Mathematics 36(3), 719–723 (1935).
  8. Chen, T., Kornblith, S., Norouzi, M. & Hinton, G. “A simple framework for contrastive learning of visual representations.” Proceedings of the 37th International Conference on Machine Learning, PMLR 119, 1597–1607 (2020).
  9. Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., Kaiser, Ł. & Polosukhin, I. “Attention is all you need.” Advances in Neural Information Processing Systems 30 (2017).
  10. Elhage, N., Hume, T., Olsson, C., Schiefer, N., et al. “Toy models of superposition.” Transformer Circuits Thread (2022).