Adding a method#
A quadrature method in padaquad is an object, not a subclass: an instance of
UniformMethodClass (fixed node fractions and weights) or VariableMethodClass
(node fractions and a function that computes the weights from the actual node
positions). The solvers reach a method only through the interface of the
abstract MethodClass (node_fractions(), weights(nodes), degree, order,
error_exponent), so a new method needs no change to the solvers.
What a method needs#
Item |
Meaning |
|---|---|
node fractions |
Positions of the nodes in a subinterval scaled to \([0, 1]\): a 1-D float64 tensor starting at 0, ending at 1, never decreasing. Methods whose nodes do not include the ends add them with weight 0 (as the Gauss-Kronrod methods do). |
weights |
The method’s weights; the subinterval’s integral is \(h \sum_i b_i f(t_i)\). They sum to 1. |
error weights |
|
|
The highest polynomial degree |
|
The degree of the lower-degree method plus 2: the error estimate on a subinterval of width \(h\) shrinks like \(h^k\) with this \(k\). The solver’s merge rule uses it. |
|
Keys of |
Example: Simpson’s rule#
Simpson’s rule has the nodes \(0, 1/2, 1\) and the weights \(1/6, 2/3, 1/6\), and is exact for polynomials up to degree 3. The trapezoid rule on the end nodes, weights \(1/2, 0, 1/2\) (degree 1), gives the error estimate.
import math
import torch
from padaquad import UNIFORM_METHODS, integrate
from padaquad.methods import UniformMethodClass, _Tableau
f64 = torch.float64
simpson = UniformMethodClass(
degree=3, # Simpson's rule is exact for cubics
error_exponent=3, # the trapezoid rule (degree 1) is subtracted: 1 + 2
tableau=_Tableau(
c=torch.tensor([0.0, 0.5, 1.0], dtype=f64),
b=torch.tensor([1 / 6, 2 / 3, 1 / 6], dtype=f64),
# b minus the trapezoid weights [1/2, 0, 1/2]
b_error=torch.tensor([1 / 6 - 1 / 2, 2 / 3, 1 / 6 - 1 / 2], dtype=f64),
),
)
# Check the claimed degrees against the weights before using the method.
measured = simpson.measure_degree()
assert (measured.degree, measured.lower_degree) == (3, 1)
# Make it available to the uniform solver by name (lower case).
UNIFORM_METHODS["simpson"] = simpson
result = integrate(
torch.sin,
"simpson",
atol=1e-10,
rtol=1e-10,
mesh_init=torch.tensor([0.0], dtype=f64),
mesh_final=torch.tensor([math.pi], dtype=f64),
device="cpu",
max_batch=10_000,
)
assert abs(float(result.integral) - 2.0) < 1e-9
This block runs in the test suite (tests/test_examples_run.py), so the
assertions above are checked.
Registering the method in UNIFORM_METHODS at run time makes it available to
every solver created afterwards; each solver works on its own copy.
A method whose weights depend on the node positions#
VariableMethodClass(degree, error_exponent, c, weight_function, references)
takes a weight_function instead of fixed weights. It receives the nodes of
every subinterval scaled to \([0, 1]\), a tensor of shape [N, C, T], and returns
(b, b_error), two tensors of shape [N, C] in the same dtype and on the same
device. It must be a function defined at module level, not a lambda or a nested
function, so that solvers can be copied and pickled.
_interpolatory3_weights in padaquad/methods/interpolatory.py is the
pattern to follow; register the method in VARIABLE_METHODS.
Adding a method to padaquad itself#
Define it in the module of its family (
padaquad/methods/runge_kutta.py,gauss_kronrod.py,clenshaw_curtis.pyorinterpolatory.py) and add it to that module’s registry dictionary (RK_METHODS,GK_METHODS,CC_METHODSorINTERPOLATORY_METHODS).UNIFORM_METHODSandVARIABLE_METHODSare built from these, and the methods table on the methods page is generated from them, so it gains a row automatically. A new family needs its own module, a registry, and an entry inFAMILIESindocs/_ext/methods_table.py.Put a comment above each block of numbers saying where it comes from (author, year, venue, table or equation), and list the sources in
references.Run the checks that every method must pass. Some include a new method automatically; others use hand-written lists that you must extend:
automatic for a uniform method:
tests/test_exactness.pymeasures the degree in both directions and checkserror_exponentagainst the lower-degree method, for every entry ofUNIFORM_METHODS;automatic for every method:
tests/test_docs_methods_table.pychecks that its row is complete and that its references are in the bibliography, andexamples/verification/polynomial_degree.pyandexamples/verification/integral_accuracy.pyinclude it;by hand for a variable method:
tests/test_exactness.pyhas one test per variable method (test_interpolatory3_*,test_variable_adaptive_heun); add one;by hand for every method:
tests/test_tableau.pychecks the weights against an independent calculation through per-family lists (GAUSS_POINTS, the Clenshaw-Curtis names,RK_LOWER_WEIGHTS); add the new method to its family’s list, or write a test for a new family.
For a uniform method, regenerate the snapshot values (
TPD_REGENERATE_SNAPSHOTS=1 pytest tests/test_snapshots.py) and say in the commit that only new entries were added. The snapshot cells cover uniform methods only.