A metric space is a set with a distance function satisfying positivity, symmetry, and the triangle inequality. Every concept from real analysis (convergence, continuity, compactness, completeness) generalizes to metric spaces. The reals are just one example.
Definition and examples
A metric d on a set X satisfies: (1) d(x, y) ≥ 0 with equality iff x = y, (2) d(x, y) = d(y, x), (3) d(x, z) ≤ d(x, y) + d(y, z). The reals with |x - y| are a metric space. So is R^n with Euclidean distance. So is the set of bounded functions with the sup metric.
Scheme
; Metric space axioms: d(x,y) for various metrics; Euclidean metric on R^2
(define (d-euclidean p q)
(sqrt (+ (expt (- (car p) (car q)) 2)
(expt (- (cadr p) (cadr q)) 2))))
; Manhattan metric on R^2
(define (d-manhattan p q)
(+ (abs (- (car p) (car q)))
(abs (- (cadr p) (cadr q)))))
; Discrete metric: d(x,y) = 0 if x=y, 1 otherwise
(define (d-discrete x y)
(if (equal? x y) 01))
(define p (list 12))
(define q (list 46))
(display "Points: p=(1,2), q=(4,6)") (newline)
(display "Euclidean: ") (display (d-euclidean p q)) (newline)
(display "Manhattan: ") (display (d-manhattan p q)) (newline)
; Triangle inequality: d(p,r) <= d(p,q) + d(q,r)
(define r (list 33))
(display "Triangle inequality check:") (newline)
(display " d(p,r) = ") (display (d-euclidean p r)) (newline)
(display " d(p,q) + d(q,r) = ")
(display (+ (d-euclidean p q) (d-euclidean q r))) (newline)
(display " d(p,r) <= d(p,q)+d(q,r)? ")
(display (<= (d-euclidean p r)
(+ (d-euclidean p q) (d-euclidean q r))))
The open ball B(p, r) is the set of all x with d(x, p) < r. A set is open if every point has an open ball around it contained in the set. A set is closed if its complement is open, equivalently if it contains all its limit points.
Scheme
; Open and closed sets in R; (0, 1) is open: every point has a ball inside; [0, 1] is closed: contains its limit points
(define (in-open-interval? x a b) (and (> x a) (< x b)))
(define (in-closed-interval? x a b) (and (>= x a) (<= x b)))
; For any x in (0,1), r = min(x, 1-x) gives B(x,r) inside (0,1)
(define (ball-radius x)
(min x (- 1 x)))
(display "For x in (0,1), ball radius that fits:") (newline)
(for-each (lambda (x)
(display " x=") (display x)
(display ", r=") (display (ball-radius x)) (newline))
(list 0.10.50.90.010.99))
; 0 is a limit point of (0,1) but not in (0,1); so (0,1) is not closed
(display "0 is a limit point of (0,1): ")
(display "1/n -> 0, each 1/n in (0,1)") (newline)
(display "(0,1) is open but not closed") (newline)
(display "[0,1] is closed (contains 0 and 1)")
Python
# Python equivalentprint("For x in (0,1), ball radius that fits:")
for x in [0.1, 0.5, 0.9, 0.01, 0.99]:
r = min(x, 1 - x)
print(" x=" + str(x) + ", r=" + str(r))
print("0 is a limit point of (0,1): 1/n -> 0, each 1/n in (0,1)")
print("(0,1) is open but not closed")
print("[0,1] is closed (contains 0 and 1)")
Compactness
A set is compact if every open cover has a finite subcover. In R^n, compact = closed and bounded (Heine-Borel). Compact sets are where the extreme value theorem and uniform continuity live. Compactness is the topological substitute for "closed bounded interval."
Scheme
; Heine-Borel: in R, compact = closed and bounded; [0, 1] is compact; (0, 1) is NOT compact (not closed); [0, infinity) is NOT compact (not bounded); Consequence: continuous f on compact set attains max/min
(define (f x) (- (sin (* 3.14159 x)) (* 0.5 x)))
; Search for max on [0, 1] (compact)
(define (find-max f a b n)
(let loop ((i 0) (xmax a) (fmax (f a)))
(if (> i n)
(begin
(display " max at x=") (display xmax)
(display ", f(x)=") (display fmax))
(let* ((x (+ a (* (/ i n) (- b a))))
(fx (f x)))
(loop (+ i 1)
(if (> fx fmax) x xmax)
(if (> fx fmax) fx fmax))))))
(display "f(x) = sin(pi*x) - x/2 on [0,1]:") (newline)
(find-max f 0.01.010000) (newline)
(display "Compactness guarantees this max is attained")
Python
# Python equivalentimportmath
f = lambda x: math.sin(math.pi * x) - 0.5 * x
xs = [i / 10000for i inrange(10001)]
fvals = [(x, f(x)) for x in xs]
xmax, fmax = max(fvals, key=lambda p: p[1])
print("f(x) = sin(pi*x) - x/2 on [0,1]:")
print(" max at x=" + str(round(xmax, 4)) + ", f(x)=" + str(round(fmax, 6)))
print("Compactness guarantees this max is attained")
Completeness in metric spaces
A metric space is complete if every Cauchy sequence converges. The reals are complete (Ch. 2). The rationals are not. Complete metric spaces are where the Banach fixed-point theorem lives: a contraction on a complete metric space has a unique fixed point.
Scheme
; Banach fixed point theorem; f(x) = cos(x) is a contraction on [0, 1]; Iterating from any start converges to the fixed point
(define (iterate f x n)
(if (= n 0) x (iterate f (f x) (- n 1))))
(display "Iterating cos(x) from x=0:") (newline)
(for-each (lambda (n)
(let ((val (iterate cos 0.0 n)))
(display " step ") (display n)
(display ": ") (display val) (newline)))
(list 125102050))
; Fixed point: x = cos(x) => x ~ 0.7390851332
(display "Fixed point: cos(x) = x at x = ")
(display (iterate cos 0.0100)) (newline)
; Verify: cos(fixed point) = fixed point
(let ((fp (iterate cos 0.0100)))
(display "cos(fp) = ") (display (cos fp)) (newline)
(display "Difference: ") (display (abs (- fp (cos fp)))))
Python
# Python equivalentimportmath
x = 0.0print("Iterating cos(x) from x=0:")
for n in [1, 2, 5, 10, 20, 50]:
val = 0.0for _ inrange(n):
val = math.cos(val)
print(" step " + str(n) + ":", val)
# Fixed point
fp = 0.0for _ inrange(100):
fp = math.cos(fp)
print("Fixed point: cos(x) = x at x =", fp)
print("cos(fp) =", math.cos(fp))
print("Difference:", abs(fp - math.cos(fp)))
🍞 Leinster 2021 — magnitude of metric spaces: a single number capturing the "effective size"
△ Geometry Ch.7 — metric spaces in geometric context with visual intuition
🤖 ML Ch.5 — kernel methods define metrics in feature space
Translation notes
The metric space axioms are easy to verify computationally. The deep results (Heine-Borel, completeness of R, Banach fixed point) are theorems that our code illustrates but cannot prove. The contraction mapping iteration is the constructive content of the Banach theorem: it gives you the fixed point and a convergence rate.