real analysis25 min

Introduction to Metric Spaces

Generalizing distance beyond the real line — metric spaces, open and closed sets, and the topology that analysis lives in

0/9Not Started

Why This Matters

In real analysis, you have been working with the absolute value |x - y| as your measure of distance on R. But the same ideas — convergence, continuity, open neighborhoods — work in far more general settings. A metric space is any set equipped with a distance function satisfying three axioms: non-negativity (with d(x,y) = 0 only when x = y), symmetry, and the triangle inequality.

Once you have a metric, you can define open sets (every point has a neighborhood entirely inside the set) and closed sets (the complement of an open set, or equivalently, a set containing all its limit points). These concepts form the foundation of topology, which underlies all of modern analysis. Every theorem you have seen about sequences, continuity, and compactness in R generalizes to metric spaces — and understanding this generalization reveals what those theorems really depend on.

Define Terms

Visual Model

Set XAny collection of objects
Metric d(x,y)Distance function on X
Three AxiomsPositive, symmetric, triangle
Open Ball B(x,r)Points within distance r
Open SetUnion of open balls
Closed SetComplement of open set
ExamplesR, R^n, function spaces

The full process at a glance. Click Start tour to walk through each step.

A metric space generalizes the concept of distance, enabling topology, convergence, and continuity in abstract settings.

Code Example

Code
// Metric space axiom verification
// A metric must satisfy: positivity, symmetry, triangle inequality

function verifyMetric(d, points, name) {
  console.log(`\nVerifying metric: ${name}`);
  let valid = true;
  
  for (const x of points) {
    // Axiom 1a: d(x,x) = 0
    if (Math.abs(d(x, x)) > 1e-10) {
      console.log(`  FAIL: d(${x},${x}) = ${d(x,x)} != 0`);
      valid = false;
    }
  }
  
  for (const x of points) {
    for (const y of points) {
      // Axiom 1b: d(x,y) >= 0
      if (d(x, y) < -1e-10) {
        console.log(`  FAIL: d(${x},${y}) = ${d(x,y)} < 0`);
        valid = false;
      }
      // Axiom 2: symmetry
      if (Math.abs(d(x, y) - d(y, x)) > 1e-10) {
        console.log(`  FAIL: d(${x},${y}) = ${d(x,y)} != d(${y},${x}) = ${d(y,x)}`);
        valid = false;
      }
      // Axiom 3: triangle inequality
      for (const z of points) {
        if (d(x, z) > d(x, y) + d(y, z) + 1e-10) {
          console.log(`  FAIL: d(${x},${z}) > d(${x},${y}) + d(${y},${z})`);
          valid = false;
        }
      }
    }
  }
  console.log(`  Result: ${valid ? "Valid metric" : "NOT a valid metric"}`);
  return valid;
}

// Standard metric on R
const euclidean1d = (x, y) => Math.abs(x - y);
verifyMetric(euclidean1d, [0, 1, 3, -2, 7], "Euclidean on R");

// Discrete metric
const discrete = (x, y) => (x === y ? 0 : 1);
verifyMetric(discrete, [0, 1, 3, -2, 7], "Discrete metric");

// NOT a metric: d(x,y) = (x - y)^2 (violates triangle inequality)
const badMetric = (x, y) => (x - y) ** 2;
verifyMetric(badMetric, [0, 1, 3], "(x-y)^2 (should fail)");

// Open balls
console.log("\nOpen ball B(0, 1) in R with standard metric:");
console.log("  Contains 0.5:", euclidean1d(0, 0.5) < 1);
console.log("  Contains 0.99:", euclidean1d(0, 0.99) < 1);
console.log("  Contains 1:", euclidean1d(0, 1) < 1);  // false: boundary
console.log("  Contains 1.5:", euclidean1d(0, 1.5) < 1);  // false

// Open vs closed sets
console.log("\n(0,1) is open: every point has a ball inside");
for (const x of [0.01, 0.1, 0.5, 0.9, 0.99]) {
  const r = Math.min(x, 1 - x);
  console.log(`  x=${x}: r=${r.toFixed(4)}, B(x,r) = (${(x-r).toFixed(4)}, ${(x+r).toFixed(4)}) subset of (0,1)`);
}

Interactive Experiment

Try these exercises:

  • Verify that d(x, y) = |x - y| / (1 + |x - y|) is a metric on R. Check all three axioms with sample points. This metric makes all distances less than 1.
  • On R^2, try the taxicab metric d((x1,y1), (x2,y2)) = |x1-x2| + |y1-y2|. What shape is the open ball of radius 1 centered at the origin? (It is a diamond, not a circle.)
  • Show that in the discrete metric, every subset of X is both open and closed. Check: what is B(x, 0.5)? What is B(x, 1.5)?
  • Take the set (0, 1) in R. Show it is open by finding for each x a radius r such that B(x, r) is contained in (0, 1). Show that [0, 1] is closed by checking it contains all limit points.
  • Check whether d(f, g) = max|f(x) - g(x)| on x in [0, 1] satisfies the metric axioms for functions f(x) = x and g(x) = x^2 and h(x) = sin(x).

Quick Quiz

Coding Challenge

Check Metric Axioms

Write a function called `isMetric` that takes a distance function d and an array of test points. It should return true if d satisfies all three metric axioms for every combination of the given points, and false otherwise. Check: (1) d(x,x) = 0 for all x, (2) d(x,y) is nonnegative for all x, y, (3) d(x,y) = d(y,x) for all x, y, and (4) d(x,z) is at most d(x,y) + d(y,z) for all x, y, z. Use a tolerance of 1e-9 for floating point comparisons.

Loading editor...

Real-World Usage

Metric spaces appear throughout mathematics, computer science, and data science:

  • Machine learning: Feature spaces with distance metrics (Euclidean, cosine, Mahalanobis) are metric spaces. K-nearest neighbors, clustering, and kernel methods all depend on metric structure.
  • Database indexing: Metric trees (ball trees, VP-trees, cover trees) organize data for efficient nearest-neighbor search by exploiting the triangle inequality.
  • Image retrieval: Perceptual similarity metrics turn image databases into metric spaces, enabling content-based search.
  • Network analysis: Graph metrics (shortest path distance) turn networks into metric spaces, enabling community detection and centrality analysis.
  • Functional analysis: Spaces of functions (C[a,b], L^p spaces) are metric spaces whose properties govern the behavior of differential equations, Fourier analysis, and optimization.

Connections