24. Adv: metric spaces — open, closed, compact, complete

📖 Reading · 12 min
💡 Every code box below is live — edit it and hit Run.

Every concept built across this module — limits, completeness, Bolzano-Weierstrass, uniform continuity — has used exactly one notion of distance: |x-y| on the real line, or §9.0's Euclidean formula in the plane. This closing conceptual lesson strips distance down to its bare essential properties, defining a metric space as any set equipped with any function satisfying those properties — and shows every idea from §15.0 through §15.5 was really a special case of a far more general theory all along.

The metric space axioms

A metric on a set X is a function d:X\times X\to\mathbb R satisfying, for all x,y,z\in X:

  1. d(x,y)\ge0, with d(x,y)=0\iff x=y
  2. d(x,y)=d(y,x) (symmetry)
  3. d(x,z)\le d(x,y)+d(y,z) (the triangle inequality)

A metric space is a set X together with such a d. Ordinary distance on \mathbb R, d(x,y)=|x-y|, and §9.0's Euclidean distance in the plane are both metrics — this framework was implicitly running underneath every \varepsilon-\delta argument in this module already, just never stated as an abstract choice.

Other metrics on the same underlying set are perfectly valid too. The taxicab metric on \mathbb R^2, d(p,q)=|p_1-q_1|+|p_2-q_2| (distance traveled along a grid, like city blocks, rather than a direct diagonal), satisfies all three axioms just as Euclidean distance does — a genuinely different notion of "close," on the identical set of points.

Open and closed sets, generalized

Define the open ball B(x,r)=\{y\in X:d(x,y)<r\} — a direct generalization of §1.9's \delta-neighborhood, now defined by whatever metric d happens to be in use. A set U\subseteq X is open if every point of U has some open ball around it entirely contained in U. A set is closed if its complement is open.

§1.9's \varepsilon-\delta limit definition is secretly a statement about open balls: "|x-a|<\delta\Rightarrow|f(x)-L|<\varepsilon" reads as "the ball B(a,\delta) maps into the ball B(L,\varepsilon)" — the entire definition generalizes verbatim to any metric space, with no change to the logic at all.

Compactness

A set K is sequentially compact if every sequence with terms in K has a subsequence converging to a point within K.

§15.1's Bolzano-Weierstrass theorem is exactly the statement that closed, bounded subsets of \mathbb R are sequentially compact — the Heine-Borel theorem generalizes this to \mathbb R^n: a subset of \mathbb R^n is compact if and only if it is closed and bounded.

[0,1] is compact: every sequence in [0,1] has a convergent subsequence, and — crucially — the limit stays inside [0,1], since [0,1] is closed. The open interval (0,1) is not compact: the sequence a_n=\frac1n lies entirely within (0,1), and it (along with every one of its subsequences) converges to 0 — but 0\notin(0,1), so no subsequence converges to a point within the set. Compactness requires both closedness and boundedness together — dropping either one breaks it, exactly the same "closed and bounded" pairing that made §15.3's uniform continuity theorem work.

Completeness, generalized

A metric space is complete if every Cauchy sequence (§15.1's definition, using d in place of |\cdot|) converges to a point within the space.

\mathbb R (with the ordinary metric) is complete — that's §15.0's completeness axiom, restated in this vocabulary. \mathbb Q is not complete: the sequence of decimal truncations of \sqrt2 (1,1.4,1.41,1.414,\ldots) is Cauchy (its terms crowd together, per §15.1) but has no limit within \mathbb Q — exactly §15.0's original example of \mathbb Q's incompleteness, now recognized as a special case of this fully general definition.

The payoff: everything in this module was one theory

  • §15.0's completeness = completeness of the metric space (\mathbb R,|\cdot|).
  • §15.1's Bolzano-Weierstrass = sequential compactness of closed, bounded subsets of \mathbb R.
  • §15.3's uniform continuity theorem = a general fact about continuous functions on compact metric spaces.
  • §15.4's Riemann integral relies on [a,b] being compact to guarantee uniform continuity applies.

Every proof technique this module built for \mathbb R specifically transfers, largely unchanged, to any metric space at all — which is exactly why this single abstract framework underlies the rest of mathematical analysis, well beyond calculus.

Doing it in Python

Confirming the triangle inequality holds for two different metrics on the same points — Euclidean and taxicab:

import math

def euclidean(p, q):
    return math.sqrt(sum((a - b)**2 for a, b in zip(p, q)))

def taxicab(p, q):
    return sum(abs(a - b) for a, b in zip(p, q))

x, y, z = (0, 0), (3, 4), (3, 0)

for name, d in (("Euclidean", euclidean), ("taxicab", taxicab)):
    direct = d(x, y)
    via_z = d(x, z) + d(z, y)
    print(f"{name}: d(x,y)={direct}, d(x,z)+d(z,y)={via_z}, triangle inequality holds: {direct <= via_z}")

Confirming (0,1) is not (sequentially) compact — the sequence \frac1n converges, but its limit escapes the set:

sequence = [1/n for n in range(1, 1000)]

in_set = all(0 < x < 1 for x in sequence)
limit = 0
limit_in_set = 0 < limit < 1

print(f"every term lies in (0,1): {in_set}")
print(f"the sequence's limit: {limit}")
print(f"limit lies in (0,1): {limit_in_set}")
print("\nconvergent sequence with no limit point inside the set -- not compact")

Confirming \mathbb Q's incompleteness directly: a Cauchy sequence of rationals with no rational limit:

from fractions import Fraction

# Newton's method (section 3.2) generates a Cauchy sequence of rationals converging to sqrt(2)
def newton_sqrt2(n_steps):
    x = Fraction(1)
    for _ in range(n_steps):
        x = (x + 2/x) / 2
    return x

for steps in (1, 3, 5, 8):
    approx = newton_sqrt2(steps)
    print(f"steps={steps}: {approx} = {float(approx):.10f}")
print(f"\ntrue sqrt(2) = {2**0.5:.10f}")
print("a Cauchy sequence of RATIONALS, with an IRRATIONAL limit -- Q is not complete")

Worked example

Verify the triangle inequality for the taxicab metric on \mathbb R^2, using x=(0,0), y=(3,4), z=(3,0).

d_{\text{taxi}}(x,y)=|0-3|+|0-4|=3+4=7

d_{\text{taxi}}(x,z)=|0-3|+|0-0|=3,\qquad d_{\text{taxi}}(z,y)=|3-3|+|0-4|=4

d_{\text{taxi}}(x,z)+d_{\text{taxi}}(z,y)=3+4=7

\boxed{d_{\text{taxi}}(x,y)=7\le7=d_{\text{taxi}}(x,z)+d_{\text{taxi}}(z,y)}

— the triangle inequality holds, with equality in this particular case.

Sanity check. Equality (rather than strict inequality) happens here because z=(3,0) lies exactly "on the way" from x to y under the taxicab metric's grid-like geometry — going from (0,0) to (3,0) then to (3,4) retraces no ground at all, unlike the Euclidean case where the direct diagonal path is strictly shorter than any detour. Compare to Euclidean distance on the same three points: d_{\text{Euclid}}(x,y)=\sqrt{9+16}=5, while d_{\text{Euclid}}(x,z)+d_{\text{Euclid}}(z,y)=3+4=7>5 — a strict inequality there, since the direct diagonal genuinely is shorter than the detour through z once distance is measured "as the crow flies" rather than along a grid. Two different metrics on the identical three points, both satisfying the triangle inequality, with different degrees of slack — exactly the flexibility the abstract axioms are built to allow. ✓

Your turn

1. Verify the triangle inequality for the Euclidean metric using the same three points x=(0,0), y=(3,4), z=(3,0) from the worked example (compute all three distances and check the inequality).

2. Determine whether [0,\infty) (all nonnegative reals) is compact, and justify your answer using the Heine-Borel theorem.

3. True or false: a metric space can fail to be complete even if every one of its Cauchy sequences is bounded.

Solutions

1. d(x,y)=\sqrt{(0-3)^2+(0-4)^2}=\sqrt{9+16}=5. d(x,z)=\sqrt{9+0}=3, d(z,y)=\sqrt{0+16}=4.

5\le3+4=7\ \checkmark

\boxed{\text{triangle inequality holds (strictly, since }5<7\text{)}}

2. [0,\infty) is closed (its complement, (-\infty,0), is open) but not bounded — it extends forever. By Heine-Borel (compact \iff closed and bounded), missing boundedness disqualifies it.

\boxed{\text{not compact}}

(Confirmed directly: the sequence a_n=n lies in [0,\infty) but has no convergent subsequence at all, since every subsequence also marches off to infinity — sequential compactness fails outright.)

3. True. Boundedness of individual Cauchy sequences is not the issue — being Cauchy already forces boundedness (§15.1). The problem is whether the sequence's limit lies within the space. \mathbb Q's Newton's-method sequence converging to \sqrt2 is a bounded, genuinely Cauchy sequence of rationals — it simply has no limit that is itself rational, which is exactly what makes \mathbb Q incomplete despite every individual Cauchy sequence within it being perfectly well-behaved and bounded.

Check yourself in code

Verify the triangle inequality for both the Euclidean and taxicab metrics on x=(0,0), y=(3,4), z=(3,0).

Print exactly this:

Euclidean: d(x,y)=5.0, d(x,z)+d(z,y)=7.0, holds: True
taxicab: d(x,y)=7, d(x,z)+d(z,y)=7, holds: True
import math

def euclidean(p, q):
    return math.sqrt(sum((a - b)**2 for a, b in zip(p, q)))

def taxicab(p, q):
    return sum(abs(a - b) for a, b in zip(p, q))

x, y, z = (0, 0), (3, 4), (3, 0)

for name, d in (("Euclidean", euclidean), ("taxicab", taxicab)):
    direct = d(x, y)
    via_z = d(x, z) + d(z, y)
    print(f"{name}: d(x,y)=..., d(x,z)+d(z,y)=..., holds: ...")
import math

def euclidean(p, q):
    return math.sqrt(sum((a - b)**2 for a, b in zip(p, q)))

def taxicab(p, q):
    return sum(abs(a - b) for a, b in zip(p, q))

x, y, z = (0, 0), (3, 4), (3, 0)

for name, d in (("Euclidean", euclidean), ("taxicab", taxicab)):
    direct = d(x, y)
    via_z = d(x, z) + d(z, y)
    print(f"{name}: d(x,y)={direct}, d(x,z)+d(z,y)={via_z}, holds: {direct <= via_z}")

A metric space needs only a distance function satisfying nonnegativity, symmetry, and the triangle inequality, and every concept this module built for \mathbb R — open balls generalizing \delta-neighborhoods, compactness generalizing Bolzano-Weierstrass, completeness generalizing §15.0's axiom — transfers to any such space unchanged. The open interval (0,1)'s failure to be compact, and \mathbb Q's failure to be complete, are the two running counterexamples that made every subtlety in this module concrete, now revealed as instances of one unified abstract theory.

Next, and last: the derivative itself, reconceived not as a number or a gradient but as a linear map — the framework that finally explains why the inverse and implicit function theorems work.