Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Bases and stencils

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 DzD_z basis functions ϕd:Ω→R\phi_d: \Omega \to \mathbb{R},

f(s)=∑d=1Dzzdϕd(s)f(\vect{s}) = \sum_{d=1}^{D_z} z_d \phi_d(\vect{s})

with coefficients z∈RDz\vect{z} \in \mathbb{R}^{D_z}. The integral is linear in the coefficients,

I[f]=∑d=1Dzzd∫Ωϕd(s)dμ(s)=v⊤zI[f] = \sum_{d=1}^{D_z} z_d \int_\Omega \phi_d(\vect{s}) \mathrm{d}\mu(\vect{s}) = \vect{v}^\top \vect{z}

where v∈RDz\vect{v} \in \mathbb{R}^{D_z} holds the integrals of the basis functions, vd=∫Ωϕddμv_d = \int_\Omega \phi_d \mathrm{d}\mu. 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.

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 [0,4][0, 4] 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 f∈RN\vect{f} \in \mathbb{R}^N at nodes sn\vect{s}_n, with as many nodes as basis functions (N=DzN = D_z), interpolation finds the coefficients by solving Φz=f\gmat{\Phi} \vect{z} = \vect{f}, where Φ∈RN×Dz\gmat{\Phi} \in \mathbb{R}^{N \times D_z} is the basis matrix with [Φ]nd=ϕd(sn)[\gmat{\Phi}]_{nd} = \phi_d(\vect{s}_n). Then

I[f]≈v⊤Φ−1f=w⊤f,Φ⊤w=vI[f] \approx \vect{v}^\top \gmat{\Phi}^{-1} \vect{f} = \vect{w}^\top \vect{f}, \quad \gmat{\Phi}^\top \vect{w} = \vect{v}

so the rule’s weights solve a linear system with the transposed basis matrix. With monomials as the basis, Φ⊤\gmat{\Phi}^\top is the matrix V\mat{V} of (2.3) and v\vect{v} the moments b\vect{b}, which is chapter 2’s construction. With hat functions, Φ\gmat{\Phi} is the identity on the knots and w=v\vect{w} = \vect{v}, 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 H∈RNtarget×Nsource\mat{H} \in \mathbb{R}^{N_\text{target} \times N_\text{source}}, so that f^=Hc\hat{\vect{f}} = \mat{H} \vect{c} with c∈RNsource\vect{c} \in \mathbb{R}^{N_\text{source}} and f^∈RNtarget\hat{\vect{f}} \in \mathbb{R}^{N_\text{target}}. For a local scheme each row of H\mat{H} 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 k(s∗,s)k(\vect{s}_*, \vect{s})) the rows are dense.

If the targets carry a rule with weights wtarget∈RNtarget\vect{w}_\text{target} \in \mathbb{R}^{N_\text{target}}, then

wtarget⊤Hc=(H⊤wtarget)⊤c\vect{w}_\text{target}^\top \mat{H} \vect{c} = (\mat{H}^\top \vect{w}_\text{target})^\top \vect{c}

so integrating the read-out with the target rule is the same as integrating the source values with the weights H⊤wtarget\mat{H}^\top \vect{w}_\text{target}. The adjoint of the read-out moves a rule from one grid to another. A sparse H\mat{H} 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 H⊤wtarget\mat{H}^\top \vect{w}_\text{target}, 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.