---
title: Metric spaces
heading: Distance, abstracted, so the same theorems work for vectors, signals and codes
eyebrow: A14 · Lesson 32 · Metric spaces
xp: 40
---

::: hook
"Close" has meant $\lvert x - y\rvert$ small. But engineers measure closeness in many ways: the RMS difference between two signals, the largest deviation of a filter's response from a template, the number of bits by which a received word differs from a codeword. A **metric space** is any set with a distance function obeying three rules. Once those hold, convergence, Cauchy sequences, open sets and continuity all make sense, and theorems proved once apply to vectors, functions and codes alike. The next lesson's contraction mapping theorem is the payoff.
:::

::: play
- The curve is the unit ball $\{(x, y) : \lVert(x, y)\rVert_p \le 1\}$ for the p-norm $\lVert(x, y)\rVert_p = (\lvert x\rvert^p + \lvert y\rvert^p)^{1/p}$.
- p = 1 gives a diamond (the Manhattan, or taxicab, distance); p = 2 the ordinary circle; large p approaches the square of the max norm, $\max(\lvert x\rvert, \lvert y\rvert)$.
- The readout measures the distance from (0, 0) to the point (0.6, 0.8) in the p-norm: 1.4 at p = 1, exactly 1 at p = 2, and approaching 0.8 as p grows.
:::
:::

```sim plot
view: complex
range: 1.6
slider: p = 1..12 step 0.1 = 2
curve: sign(cos(u))*abs(cos(u))^(2/p) + j*sign(sin(u))*abs(sin(u))^(2/p) | over: u = 0..2*pi | label: unit ball
dot: 0.6 + 0.8*j | label: (0.6, 0.8)
readout: p-norm of (0.6, 0.8) = (0.6^p + 0.8^p)^(1/p)
```

::: predict
## In a code where every two codewords differ in at least 5 bits, how many bit errors can the decoder always correct?

- [ ] 5
- [ ] 4
- [x] 2
- [ ] 1

::: reveal
2. With Hamming distance as the metric, balls of radius 2 around the codewords don't overlap (2 + 2 < 5), so a received word with at most 2 errors is closer to the codeword it came from than to any other. In general a minimum distance d corrects $\lfloor(d - 1)/2\rfloor$ errors.
:::
:::

::: nameit
## The axioms

A **metric** on a set X is a function d(x, y) with, for all x, y, z:

1. $d(x, y) \ge 0$, and $d(x, y) = 0$ exactly when x = y;
2. $d(x, y) = d(y, x)$;
3. $d(x, z) \le d(x, y) + d(y, z)$ (the **triangle inequality**).

## Examples

| Space | Distance |
|---|---|
| $\mathbb R^n$, Euclidean | $\sqrt{\sum(x_i - y_i)^2}$ |
| $\mathbb R^n$, Manhattan | $\sum\lvert x_i - y_i\rvert$ |
| $\mathbb R^n$, max | $\max\lvert x_i - y_i\rvert$ |
| continuous functions on [a, b], sup | $\sup\lvert f(x) - g(x)\rvert$ |
| integrable functions, $L^1$ | $\int\lvert f - g\rvert$ |
| bit strings of length n, Hamming | number of positions that differ |

On $\mathbb R^n$ all the p-norms give the same convergent sequences: $\lVert v\rVert_\infty \le \lVert v\rVert_2 \le \lVert v\rVert_1 \le n\lVert v\rVert_\infty$. On spaces of functions they don't: a sequence can converge in $L^1$ but not in the sup metric (a narrowing spike of fixed height).

## The definitions carry over

Open balls $B(x, r) = \{y : d(x, y) < r\}$, open and closed sets, convergence ($d(x_n, x) \to 0$), Cauchy sequences and continuity are all defined exactly as on ℝ, with d in place of $\lvert x - y\rvert$. A metric space is **complete** if every Cauchy sequence converges in it (lesson 33).

## Codes

With Hamming distance, a code with minimum distance d detects up to d − 1 errors and corrects up to $\lfloor(d - 1)/2\rfloor$.
:::

::: example
## Find the Euclidean, Manhattan and max distances between (1, 4, −2) and (3, 1, 2).

::: solution
1. **Differences.** They are (2, −3, 4).
2. **Euclidean.**
   $$\sqrt{4 + 9 + 16} = 5.385$$
3. **Manhattan.**
   $$2 + 3 + 4 = 9$$
4. **Max.** The largest difference in size is 4.
5. **Check.** As always, max ≤ Euclidean ≤ Manhattan.

**Answer:** Euclidean 5.385, Manhattan 9 and max 4.
:::
:::

::: example
## What's the sup distance between $\sin x$ and x on [0, 1]?

::: solution
1. **Where the gap is largest.** $x - \sin x$ is increasing on [0, 1] (its derivative $1 - \cos x \ge 0$), so the sup is at x = 1:
   $$1 - \sin 1 = 0.1585$$
2. **Compare the $L^1$ distance.**
   $$\begin{aligned} \int_0^1(x - \sin x)\,dx &= \frac12 - (1 - \cos 1) \\ &= 0.0403 \end{aligned}$$

**Answer:** the sup distance is $1 - \sin 1 = 0.1585$, and the $L^1$ distance is 0.0403.
:::
:::

::: example
## What area do the unit balls of the 1-norm, 2-norm and max norm have in the plane?

::: solution
1. **The 1-norm ball.** $\lvert x\rvert + \lvert y\rvert \le 1$ is a diamond with diagonals of length 2: area 2.
2. **The 2-norm ball.** $x^2 + y^2 \le 1$ is the unit disk: area π.
3. **The max-norm ball.** $\max(\lvert x\rvert, \lvert y\rvert) \le 1$ is the square $[-1, 1]^2$: area 4.
4. **They nest.** Diamond inside disk inside square, matching
   $$\lVert v\rVert_\infty \le \lVert v\rVert_2 \le \lVert v\rVert_1$$
5. **What it means.** In finite dimensions all norms are equivalent, so they agree about which sequences converge, but the constants matter for error bounds.

**Answer:** the areas are 2 for the 1-norm, π for the 2-norm and 4 for the max norm.
:::
:::

::: check
given: a = 1 | -2; b = 3 | 5; c = 0 | 4; d = -1 | 2
answer: sqrt((a - c)^2 + (b - d)^2)
tolerance: 0.01%

## What's the Euclidean distance between ({a}, {b}) and ({c}, {d})?

::: right
Right. {answer}.
:::

::: misses
- [abs(a - c) + abs(b - d)] That's the Manhattan distance.
- [any] Not yet. $\sqrt{\Delta x^2 + \Delta y^2}$.
:::
:::

::: check
given: a = 1 | -2; b = 3 | 5; c = 0 | 4; d = -1 | 2
answer: (abs(a - c) + abs(b - d) + abs(abs(a - c) - abs(b - d)))/2
tolerance: 0.01%

## And the max-norm distance between ({a}, {b}) and ({c}, {d})?

::: right
Right. The larger coordinate difference: {answer}.
:::

::: misses
- [sqrt((a - c)^2 + (b - d)^2)] That's Euclidean.
- [any] Not yet. $\max(\lvert\Delta x\rvert, \lvert\Delta y\rvert)$.
:::
:::

::: check
given: p = 1 | 3 | 4; x = 0.6 | 1.5; y = 0.8 | 2
answer: (x^p + y^p)^(1/p)
tolerance: 0.01%

## What's the p-norm of ({x}, {y}) for p = {p}?

::: right
Right. $(\lvert x\rvert^p + \lvert y\rvert^p)^{1/p}$ = {answer}.
:::

::: misses
- [any] Not yet. $(\lvert x\rvert^p + \lvert y\rvert^p)^{1/p}$.
:::
:::

::: check
given: a = 180 | 75 | 201; b = 158 | 99 | 54
answer: abs((floor(a/1) - 2*floor(a/2)) - (floor(b/1) - 2*floor(b/2))) + abs((floor(a/2) - 2*floor(a/4)) - (floor(b/2) - 2*floor(b/4))) + abs((floor(a/4) - 2*floor(a/8)) - (floor(b/4) - 2*floor(b/8))) + abs((floor(a/8) - 2*floor(a/16)) - (floor(b/8) - 2*floor(b/16))) + abs((floor(a/16) - 2*floor(a/32)) - (floor(b/16) - 2*floor(b/32))) + abs((floor(a/32) - 2*floor(a/64)) - (floor(b/32) - 2*floor(b/64))) + abs((floor(a/64) - 2*floor(a/128)) - (floor(b/64) - 2*floor(b/128))) + abs((floor(a/128) - 2*floor(a/256)) - (floor(b/128) - 2*floor(b/256)))
tolerance: 0

## What's the Hamming distance between the bytes {a:bin8} and {b:bin8}?

::: right
Right. They differ in {answer} positions.
:::

::: misses
- [any] Not yet. Compare the bits position by position.
:::
:::

::: check
given: d = 3 | 5 | 8 | 11
answer: floor((d - 1)/2)
tolerance: 0

## A code's minimum Hamming distance is {d}. How many bit errors can it always correct?

::: right
Right. $\lfloor(d - 1)/2\rfloor$ = {answer}.
:::

::: misses
- [d - 1] That's how many it can detect.
- [any] Not yet. Balls of radius t around codewords must not overlap: 2t < d.
:::
:::

::: check
given: a = 0.5 | 1 | 1.5
answer: a - sin(a)
tolerance: 0.01%

## What's the sup distance between $\sin x$ and x on [0, {a}]?

::: right
Right. $a - \sin a$ = {answer}, at the right end.
:::

::: misses
- [any] Not yet. $x - \sin x$ increases on [0, a].
:::
:::

::: check
kind: choice

## Which of these is not a metric on ℝ?

- [ ] $\lvert x - y\rvert$
- [ ] $\min(1, \lvert x - y\rvert)$
- [x] $(x - y)^2$
- [ ] $\lvert x^3 - y^3\rvert$

::: misses
- [1] It's the standard metric.
- [2] It satisfies all three axioms (check the triangle inequality case by case).
- [4] Since $x^3$ is one-to-one, this is the standard metric after a change of variable.
:::
:::

::: check
kind: parts
given: a = 1 | -2; b = 4 | 0; c = 3 | 2; d = 1 | -3

## Distances between ({a}, {b}) and ({c}, {d}).

::: part
answer: abs(a - c) + abs(b - d)
tolerance: 0.5%

### What is the 1-norm (Manhattan) distance?

::: misses
- [any] Add the absolute coordinate differences.
:::
:::

::: part
answer: (abs(a - c)^3 + abs(b - d)^3)^(1/3)
tolerance: 0.5%

### What is the 3-norm distance?

::: misses
- [any] $\left(\lvert\Delta x\rvert^3 + \lvert\Delta y\rvert^3\right)^{1/3}$.
:::
:::
:::

::: check
kind: derivation
vars: n = 2..10

## The sup distance between x and $x^n$ on [0, 1].

::: step
answer: n^(-1/(n - 1))

### Where is $x - x^n$ largest?
:::

::: step
answer: n^(-1/(n - 1)) - n^(-n/(n - 1))

### What is that largest value?
:::

::: right
Right. For n = 2 it's ¼ at x = ½.
:::
:::

::: explain
## Why is the triangle inequality the axiom that makes a distance useful for analysis?

::: model
Most convergence arguments chain approximations: if $x_n$ is close to x, and x is close to y, then $x_n$ is close to y. The triangle inequality is exactly the statement that distances add at most, so errors from several steps can be bounded by adding them up, as in the ε/2 + ε/2 arguments. Without it, two points could each be close to a third yet far from each other, and limits wouldn't be unique. The function $(x - y)^2$ fails it: 0 and 2 are at distance 4, though each is at distance 1 from 1, so it can't support those arguments.
:::
:::

::: practice

::: check
given: a = 2 | -1; b = 0 | 6; c = 5 | 1; d = 4 | -3
answer: abs(a - c) + abs(b - d)
tolerance: 0.01%

## Manhattan distance between ({a}, {b}) and ({c}, {d})?

::: misses
- [any] $\lvert\Delta x\rvert + \lvert\Delta y\rvert$.
:::
:::

::: check
given: d = 4 | 7 | 16
answer: floor((d - 1)/2)
tolerance: 0

## Correctable errors at minimum distance {d}?

::: misses
- [any] $\lfloor(d - 1)/2\rfloor$.
:::
:::

::: check
given: a = 0.5 | 0.8 | 1 | 1.5 | 2 | 3
answer: a - sin(a)
tolerance: 0.01%

## Sup distance between $\sin x$ and x on [0, {a}]?

::: misses
- [any] $a - \sin a$.
:::
:::
:::
