Double and Triple Integrals
A single integral adds up along a line. Double and triple integrals add up over areas and volumes, which is how probabilities and masses get computed.
Why Does This Exist?
Single integrals measure totals along a line: distance from speed, area under a curve. But ML quantities live over regions. A joint probability is an integral over a rectangle of outcomes. A mass is an integral over a volume. An expectation over two variables is an integral over their whole plane. None of these fit on a line, so the one-variable machinery from integration and area needs an upgrade that sums over areas and volumes instead.
Double and triple integrals are that upgrade, and their working method is reassuringly ordinary: integrate one variable at a time, turning a volume problem into a sequence of line problems. The same iteration pattern computes the normalizing constants behind integrals in probability.
The one idea you need first
A definite integral adds up infinitely many infinitesimal slices: is the signed area under the curve. A double integral does the same over a region , stacking slices in two directions to get a volume. Add a third variable and the volume becomes a hypervolume, which sounds exotic but computes identically.
Think of It Like This
Counting grain in a silo with a grid of tubes
You need the grain volume in a rectangular silo with an uneven surface. Drop a grid of measuring tubes: each tube reads the depth at its spot, and depth times the tube's footprint is its share of the volume. Add every tube and you have the total.
A double integral is that procedure with the grid refined forever. Each point contributes height times infinitesimal footprint, and the integral is the sum. Iterated integration reads the silo one row of tubes at a time: finish a row (inner integral), then add the rows (outer integral). The order of rows versus columns never changes the grain total, which is the everyday content of Fubini's theorem.
The analogy stops at uniformity. Grain has constant density, but integrands vary, and probability densities must additionally total exactly one, which constrains the surface in ways grain never faces.
How It Actually Works
A double integral over a rectangle iterates: hold fixed and integrate over , then integrate the result over :
In plain words: the inner integral collapses each vertical strip to a number depending on , and the outer integral adds those strip totals. Fubini's theorem says that for continuous functions on rectangles, either order gives the same answer, so integrate in whichever order is easier. A triple integral adds one more nesting for volumes in three variables.
Two habits keep the computation honest. First, inner limits may depend on the outer variable when the region is not a rectangle, as with triangles or disks. Second, the differential carries the geometry: for Cartesian grids, for polar ones, where the extra accounts for wedges widening outward.
Worked example
Integrate over the rectangle , .
Inner integral over , treating as a constant: . Outer integral over : . So the volume under this tilted plane over the rectangle is exactly .
Reverse the order as a check: . Same answer, as Fubini promises. For a triple taste: , since each variable contributes its average over the unit cube.
Code
A midpoint Riemann sum with live code, converging to the hand-computed 5. Linear integrands converge fast: even a 4-by-4 grid is already exact.
def midpoint(n: int) -> float: total = 0.0 for i in range(n): for j in range(n): x = (i + 0.5) / n # x in [0, 1] y = 2 * (j + 0.5) / n # y in [0, 2] total += x + 2 * y return total * (1 / n) * (2 / n)
print(midpoint(4)) # -> 5.0print(round(midpoint(50), 6)) # -> 5.0Both print 5, matching the iterated hand calculation. The coarse grid is exact here only because the integrand is linear; curved surfaces need the fine grid to converge.
Watch Out For
Integrating in the convenient order over the wrong limits
Fubini lets you swap the order, but the limits must describe the same region after the swap. On a triangle like , integrating first needs from to , and swapping means from to . Copying rectangular limits onto a triangular region integrates over a square instead, doubling the answer's territory. Sketch the region before choosing limits.
Dropping the Jacobian in curved coordinates
Switching to polar or spherical coordinates without the or factor integrates the right function over the wrong geometry, underweighting everything far from the origin. The factor is not decoration: it is the area of an infinitesimal wedge. Whenever coordinates curve, multiply by the Jacobian determinant.
The Quick Version
- Double integrals sum over areas and triple integrals over volumes by iterating one variable at a time.
- The inner integral collapses strips to numbers; the outer integral adds the strips.
- Fubini says the order does not matter for continuous functions, so pick the easier order.
- Non-rectangular regions need limits that depend on the outer variable; sketch first.
- Curved coordinates need the Jacobian factor, or distant regions get underweighted.