A field is more than its samples¶
Most fields in a geoscience workflow are not stored as plain samples. A spectral model stores spherical-harmonic coefficients, a finite-element ocean stores the coefficients of hat functions on a mesh, a finite-volume model stores cell averages, and a Gaussian-process emulator stores the weights of its kernel functions. When we integrate such a field, the honest thing is to integrate the representation itself, not to sample it on some grid and start again. It turns out that this gives us the weights of chapter 2 from a different direction, and it explains where the stencils of an interpolation scheme fit in.
Integrating a basis expansion¶
Let the integrand be an expansion in basis functions ,
with coefficients . The integral is linear in the coefficients,
where holds the integrals of the basis functions, . So a basis comes with its own weights, and they are computed once, from the basis alone. As seen above, this is (2.2) again with the coefficients in place of the samples.
The two families of chapter 3 show up here as two kinds of basis.
Global (spectral) bases have functions that are non-zero on the whole domain: Legendre polynomials, Fourier modes, spherical harmonics, the eigenfunctions of a kernel. Their integral weights are often trivial, because the functions are orthogonal to the constant. On , for and 0 otherwise, so the zonal mean of a Legendre expansion is its first coefficient, and the global mean of a spherical-harmonic expansion is its coefficient. However, every coefficient touches every point, so a change in one coefficient changes the field everywhere.
Compact-support (local) bases have functions that are non-zero on a few tiles only: the hat functions of linear finite elements, B-splines, the indicator functions of finite-volume cells. Their integral weights are local too. The integral of the indicator of a cell is its area (the finite-volume rule), and the integral of a hat function is the trapezoid weight of its node.
The second statement is worth checking, since it says that the trapezoid rule is nothing but the exact integral of the piecewise-linear interpolant. We integrate the five hat functions on with a fine Gauss rule on each tile, as one batched call.
import jax
jax.config.update("jax_enable_x64", True)
import einx
import jax.numpy as jnp
import numpy as np
from functax import quad
from integrix import rule
# Shapes: K = 4 tiles; N_G = 8 Gauss nodes per tile; N = 32 nodes; D_z = 5 hats
# knots sₖ = k, the tile edges and the hat centres
knots = jnp.arange(5.0) # → (D_z,)
# today (functax)
# 8 Gauss–Legendre nodes t_g and weights w_g on the unit tile [0, 1]
tile_nodes, tile_weights = quad.gauss_legendre(8, interval=(0.0, 1.0))
# sₖ,g = sₖ + t_g
nodes = einx.add( # → (N,)
"tile, gauss -> (tile gauss)", knots[:-1], tile_nodes
)
# wₖ,g = w_g, the same on every tile
weights = jnp.tile(tile_weights, 4) # (N_G,) → (N,)
# dₙ,d = sₙ − s_d
distance = einx.subtract( # → (N, D_z)
"node, basis -> node basis", nodes, knots
)
# |dₙ,d|
gap = jnp.abs(distance) # (N, D_z) → (N, D_z)
# 1 − |dₙ,d|
slope = 1.0 - gap # (N, D_z) → (N, D_z)
# φ_d(sₙ) = max(0, 1 − |sₙ − s_d|), the hat on knot s_d
hats = jnp.maximum(0.0, slope) # (N, D_z) → (N, D_z)
# today (integrix)
# v_d = ∫ φ_d(s) ds ≈ ∑ₙ wₙ φ_d(sₙ)
basis_weights = rule.integrate(weights, hats, axis=0).value # (N, D_z) → (D_z,)
print("v =", jnp.round(basis_weights, 12).tolist())v = [0.5, 1.0, 1.0, 1.0, 0.5]These are the trapezoid weights for a unit spacing, so a piecewise-linear field integrated exactly and its samples integrated by the trapezoid rule give the same number.
From samples to coefficients¶
If the field is given by its samples at nodes , with as many nodes as basis functions (), interpolation finds the coefficients by solving , where is the basis matrix with . Then
so the rule’s weights solve a linear system with the transposed basis matrix. With monomials as the basis, is the matrix of (2.3) and the moments , which is chapter 2’s construction. With hat functions, is the identity on the knots and , which is the check above. Any basis gives a rule this way, and the rule is global or local exactly when the basis is.
Stencils: integrating a read-out¶
The last piece is the read-out. Interpolation schemes (nearest, linear, cubic, conservative remapping) all map values on a source grid to values at target points with a linear map , so that with and . For a local scheme each row of has a few non-zero taps, the stencil, which functax stores as a sparse Stencil of indices and weights. For a global scheme (a kernel read-out ) the rows are dense.
If the targets carry a rule with weights , then
so integrating the read-out with the target rule is the same as integrating the source values with the weights . The adjoint of the read-out moves a rule from one grid to another. A sparse moves a local rule to a local rule, and a dense one spreads every target weight over every source.
Here is the smallest example: linear interpolation to the midpoints of four samples, then the midpoint rule on the three midpoints. The weights it puts on the samples are the trapezoid weights again.
from functax.interp import Stencil
# Shapes: N_source = 4 samples; N_target = 3 midpoints, 2 taps each
# today (functax)
# row k reads Hₖ c = (cₖ + cₖ₊₁)/2
midpoints = Stencil( # (N_source,) → (N_target,) op
jnp.array([[0, 1], [1, 2], [2, 3]]),
jnp.full((3, 2), 0.5),
n_source=4,
)
# the midpoint rule on unit tiles: w_target = (1, 1, 1)
target_weights = jnp.ones(3) # → (N_target,)
# today (functax)
# w_source = Hᵀ w_target
source_weights = midpoints.apply_adjoint(target_weights) # → (N_source,)
print("H^T w =", source_weights.tolist())H^T w = [0.5, 1.0, 1.0, 0.5]This is the same identity that chapter 9 uses for gradients: the gradient of an integral with respect to the source values is , computed by the same adjoint.
Who owns what¶
Bases, stencils and rules are data, and the procedures that use them are code, so the stack splits them. functax owns the bases (functax.basis), the read-outs (Stencil, and Plan to build them) and the rules (functax.quad). integrix owns what is done with them: the sums of (2.2) and (4.2), the scans, the adaptive refinement, the marches and their adjoints. gaussx owns the Gaussian expectations that come with a closed rule (Gauss–Hermite, unscented). The pages in this section take their rules from functax.quad (gauss_legendre, trapezoid, and polyline for curves); integrix accepts any object with nodes and weights, or a plain weights array, so a rule built by hand, such as the exact cell areas of chapter 5, works just as well.