How it works#
Starting mesh. When no
meshis given, \([a, b]\) is cut into a few equal pieces, each split again at random points (N_init_stepssets the size). The points are random because evenly spaced subintervals can line up with the features of a periodic integrand, so that every error estimate misses them. Calltorch.manual_seedfirst for results that repeat exactly.Batched evaluation. The nodes of a batch of subintervals are stacked into one tensor and the integrand is called once on all of them. Unless
max_batchis given, the batch size is set from a short measurement of how much memory one evaluation takes, so that batches fill the device memory until a fraction1 - total_mem_usageof the total remains free.Error estimate per subinterval. Each method computes the integral of a subinterval twice: with its own weights and with a lower-degree method on the same nodes. The difference \(e\) is the error estimate. The error ratio \(|e| / \max(\text{atol}, \text{rtol} \cdot |I|)\), where \(I\) is the integral (per output element, \(|e_d| / (\text{atol} + \text{rtol} \cdot |I_d|)\) with
error_norm="failure_fraction"), decides: below 1 the subinterval is accepted, otherwise it is split at its midpoint.Refinement. Batches of evaluation and splitting alternate until every subinterval is accepted, or until
max_adaptive_splitsstops the splitting (thenconvergedis usuallyFalse).Optimal mesh. After convergence, pairs of neighbouring subintervals are merged into
mesh_optimal, whichsame_f=Truestarts from on the next call, when the merged subinterval is predicted to pass with a margin. By default the prediction interpolates the integrand values already stored for the two neighbours onto the merged subinterval’s nodes, so no new evaluation is needed;remove_cutsets the margin andmerge_rulethe prediction.
The starting mesh#
Without a mesh argument, and without a stored mesh from same_f=True, the
starting mesh is built from N_init_steps. With
\(s = \lfloor\sqrt{N_\text{init}}\rfloor\), the domain is cut into \(s\) equal
segments, and each segment receives \(s + 1\) mesh points: at random positions
(sorted) by default, or evenly spaced with random_initial_mesh=False. The
mesh has \(s(s+1)\) points and \(s(s+1) - 1\) subintervals (result.n_start_steps
reports the number).
The random positions are the default because evenly spaced subintervals can
line up with evenly spaced features of the integrand, such as the zeros of a
sinusoid. When every subinterval sees the same part of a period, every error
estimate can be small while the integral is wrong. Call torch.manual_seed
before integrating to get the same mesh, and so the same result to the last
digit, on every run.
A given mesh must be a tensor of shape [M, T] with increasing values from
mesh_init to mesh_final.
The optimal mesh and the merge rule#
After the integration converges, padaquad builds mesh_optimal, the mesh a
later call with same_f=True starts from. Pairs of neighbouring subintervals
whose merged subinterval is predicted to pass with a margin are merged, so that a
mesh finer than needed shrinks, and subintervals that failed are split. The
merged subinterval is not evaluated during this step; the next call evaluates it.
Let two neighbouring subintervals \([a, m]\) and \([m, b]\) have contributions \(Q_1\) and \(Q_2\), error estimates \(e_1\) and \(e_2\), and let \(\tau\) be the tolerance the merged subinterval \([a, b]\) would get (in cumulative mode, Which integral the relative tolerance uses, that of the right subinterval, where the merged one ends). The pair is merged when its predicted error \(P\) satisfies
where merge_rule sets \(P\) and the cutoff \(\texttt{remove\_cut}'\).
merge_rule="predicted" (the default). Every method places its nodes at
fixed fractions of a subinterval, including both ends, and the integrand values
at those nodes are already stored. On each neighbour, padaquad replaces the
integrand by the polynomial through that neighbour’s values (barycentric
interpolation, Berrut and Trefethen [2004], Eqs. (3.2) and (4.2)), evaluates it at the
nodes the merged subinterval would have, and applies the method’s weights. This
gives the merged subinterval’s integral \(\hat Q\) and error estimate \(\hat E\)
without evaluating the integrand. Then
and the cutoff is remove_cut itself.
\(|\hat E|\) predicts whether the merged subinterval will pass its own error estimate on the next call.
\(|\hat Q - (Q_1 + Q_2)|\) compares the merged subinterval with the sum of its two halves, the test that adaptive Simpson quadrature uses to decide refinement (Gander and Gautschi [2000], Sec. 3). When the stored values resolve the integrand, it approximates the merged subinterval’s true error. It stops a merge whose own error estimate would be blind: for a kink \(|t - 1/3|\) next to much wider neighbours, merging can leave every node of the merged subinterval on the straight part, so that its error estimate is exactly 0 while its true error is many times the tolerance.
\(|e_1| + |e_2|\) keeps a margin for the neighbours’ own errors.
merge_rule="power_law" (padaquad 1.3.0 and 1.4.0). \(P = |e_1| + |e_2|\) and the cutoff
is \(\texttt{remove\_cut} / 2^{k-1}\), with \(k\) the method’s error_exponent. If
the error estimate of a subinterval of width \(h\) scaled as \(K h^k\) with the same
\(K\) for both halves, the merged subinterval of width \(2h\) would have the error
\(K (2h)^k = 2^{k-1}(|e_1| + |e_2|)\) (padaquad’s own derivation,
Tolerances and error ratios). That holds only when the subintervals are small enough
for the leading Taylor term to dominate. On wider subintervals the error grows by
far less than \(2^{k-1}\) (measured: about \(2^8\) to \(2^9\) where \(2^{20}\) is assumed
for gk21), so this merge rule refuses merges that would pass, and warm-started
calls keep more subintervals than they need. When the estimates are at the
level of rounding error (high-degree methods at tight tolerances, or float32),
they do not shrink with \(h\) and few pairs are merged.
merge_rule="sum" (padaquad 1.2.2). \(P = |e_1| + |e_2|\) and the cutoff is
remove_cut. It ignores how the error grows when two subintervals merge, so
merged subintervals often fail when the next call evaluates them and are split
again.
The two older merge rules are kept to reproduce meshes of earlier versions.
What no merge rule can see. Every merge rule decides from the values already
stored. A feature of the integrand that lies between the stored nodes, such as a
narrow bump, is invisible to all of them. The next call evaluates the merged
subinterval at new nodes: if they hit the feature, its error estimate usually
fails and the subinterval is split again, but a method whose error estimate is
weak at that node can accept it with a large error (measured for dopri5, under
every merge rule). A merged mesh is only as reliable as the stored values’
coverage of the integrand.
Each call merges a subinterval at most once, so a mesh much finer than needed shrinks over several calls. A merged subinterval keeps the sum of the pair’s contributions and error estimates until it is evaluated again.
The verification script
examples/verification/merge_prediction.py
runs warm-start chains with "predicted" and "power_law" for every method and
checks that no warm call ends less accurate than the call it started from, at
ordinary and at rounding-level tolerances and in float32.