Methods and options#
The quadrature methods are objects held in two dictionaries. Their degrees, orders and sources are in the table on the methods page, which is generated from these dictionaries.
- padaquad.UNIFORM_METHODS: dict[str, MethodClass]#
The methods of the uniform-sampling solver, by name:
adaptive_heun,fehlberg2,bosh3,dopri5(Runge-Kutta);gk7,gk15,gk21,gk31(Gauss-Kronrod);cc5,cc9,cc17,cc33,cc65(Clenshaw-Curtis). Each solver uses its own copy, so changing a solver’s precision never changes these objects.
- padaquad.VARIABLE_METHODS: dict[str, MethodClass]#
The methods of the variable-sampling solver, by name:
adaptive_heun(the same object as inUNIFORM_METHODS) andinterpolatory3_variable.
- class padaquad.steps(*values)#
Sampling modes, which select the solver class and its list of methods.
FIXED: a non-adaptive mode. No solver supports it:adaptive_quadratureandintegrateraiseValueErrorfor it.ADAPTIVE_UNIFORM: adaptive splitting, with the nodes of every subinterval at the method’s fixed node fractions and fixed weights (UniformAdaptiveQuadrature, methods inUNIFORM_METHODS).ADAPTIVE_VARIABLE: adaptive splitting, with the weights computed from the actual node positions (VariableAdaptiveQuadrature, methods inVARIABLE_METHODS).
- FIXED = 0#
- ADAPTIVE_UNIFORM = 1#
- ADAPTIVE_VARIABLE = 2#
- class padaquad.methods.MethodClass(degree, error_exponent, references=())#
A quadrature method on one subinterval: the interface every solver uses.
The base class holds what every method shares (its degree and error exponent) and declares the operations a solver calls. Two generic subclasses implement them from data:
UniformMethodClassfrom fixed node fractions and weights,VariableMethodClassfrom node fractions and a weight function of the node positions. Every registered method is an instance of one of them.- Attributes:
- degreeint
Highest polynomial degree the method integrates exactly (it is not exact for every polynomial of degree
degree + 1), as defined in Gonnet 2012, Sec. 1 [Gonnet, 2012]. For weights that depend on the node positions, the degree at general positions.- error_exponentint
The power k such that the error estimate on a subinterval of width h shrinks like \(h^k\): k = d + 2, with d the degree of the embedded method. Sets the merge cutoff of the optimal mesh with
merge_rule="power_law"(see the user guide).- referencestuple of str
Keys of
docs/references.bibfor the sources of the method’s nodes, weights and error weights; the documentation’s methods table cites them.
Initialize with the method’s degree, error exponent and sources.
- Parameters:
- degreeint
Highest polynomial degree the method integrates exactly, >= 0.
- error_exponentint
The power k of the error estimate’s \(h^k\) scaling, >= 2.
- referencestuple of str
Keys of
docs/references.bibfor the method’s sources. Default: ().
- Raises:
- ValueError
If
degreeis not an int >= 0,error_exponentis not an int >= 2 (k = d + 2 with d >= 0), orreferencesis not a tuple of strings.
- property order#
Order of the method,
degree + 1.On a smooth integrand the total error of the method applied on subintervals of width h falls like \(h^{\text{order}}\); a method of degree n has order n + 1 (Gonnet 2012, Sec. 1 [Gonnet, 2012]).
- Returns:
- int
The degree plus one.
- abstractmethod node_fractions()#
Node positions of a new subinterval as fractions of its width.
Non-decreasing, from 0 (the subinterval’s left mesh point) to 1 (its right mesh point). The solver places node i of a subinterval [left, right] at
left + c_i * (right - left).- Returns:
- torch.Tensor
Fractions c. Shape: [C], with C the number of nodes per subinterval.
- abstractmethod weights(nodes)#
Return the weights for the integral and its error estimate on each subinterval.
A subinterval of width h with integrand values y_i at its nodes contributes
h * sum_i(b_i * y_i)to the integral andh * sum_i(b_error_i * y_i)to the error estimate.- Parameters:
- nodestorch.Tensor
Node positions of each subinterval. Shape: [N, C, T].
- Returns:
- tuple of torch.Tensor
(b, b_error), each of shape [N, C, 1].
- property n_nodes#
Number of nodes per subinterval (C).
- Returns:
- int
The length of
node_fractions().
- measure_degree(nodes=None, tol=1e-12)#
Measure the polynomial degree of the method and of its embedded method.
Integrates the Legendre polynomials \(P_k(2t - 1)\), k = 0, 1, …, 2C, over the subinterval [0, 1] (C is the number of nodes) with the method’s weights
b, and with the weightsb - b_errorof the embedded lower-degree method. The exact values are 1 for k = 0 and 0 for k >= 1. The measured degree is the last k before the first error larger thantol. The calculation runs in float64 on the CPU.Legendre polynomials are used instead of the powers \(t^k\) because a high power is almost entirely made of lower-degree polynomials, which the method integrates exactly. With k = d + 1, the error on \(t^k\) is the error on \(P_k(2t - 1)\) times \((k!)^2 / (2k)!\), the inverse of the leading coefficient of \(P_k(2t - 1)\) (derivation); for gk31 (k = 48) the factor is about 1.6e-28 and the error about 9e-32, which float64 cannot resolve. The Legendre errors one degree past each registered method’s degree are all at least 4e-6.
The search stops at k = 2C because no method with C nodes is exact at degree 2C (derivation): the polynomial \((t - c_1)^2 \cdots (t - c_C)^2\) has degree 2C and is zero at every node, so the method gives 0 for it, but its integral is positive.
- Parameters:
- nodestorch.Tensor or None
Node fractions at which to measure a method whose weights depend on the node positions: 1-D, C entries, non-decreasing from 0 to 1. Default:
node_fractions().- tolfloat
Errors at most this large count as exact. The default suits the float64 methods of
UNIFORM_METHODSandVARIABLE_METHODS; float32 weights are measured as their float32 values.
- Returns:
- DegreeMeasurement
The two measured degrees and the errors.
- Raises:
- ValueError
If
nodesis malformed or does not have C entries; ifnodesdiffers from the node fractions while the weights do not depend on the node positions (the measurement would apply the weights at nodes they were not made for); or if no error is larger thantolup to degree 2C (the weights cannot be right).
Examples
>>> import torch >>> from padaquad import UNIFORM_METHODS, VARIABLE_METHODS >>> measured = UNIFORM_METHODS["gk21"].measure_degree() >>> print(measured.degree, measured.lower_degree) 31 19
A method whose weights depend on the node positions can be measured at other node fractions:
>>> method = VARIABLE_METHODS["interpolatory3_variable"] >>> print(method.measure_degree().degree) # nodes 0, 1/2, 1: Simpson 3 >>> nodes = torch.tensor([0.0, 0.3, 1.0], dtype=torch.float64) >>> print(method.measure_degree(nodes).degree) 2
- clone()#
Return an independent deep copy (no tensor shared with
self).Each solver works on its own copy of the registry’s float64 method, so dtype or device changes on one solver never reach the registry or another solver (lowering the dtype loses precision).
- Returns:
- Self
The copy, of the same class as
self.
- class padaquad.methods.UniformMethodClass(degree, error_exponent, tableau, references=())#
A method with fixed node fractions and fixed weights.
Every subinterval uses the same fractions
tableau.cand the same weightstableau.bandtableau.b_error. Used by the Runge-Kutta, Gauss-Kronrod and Clenshaw-Curtis families.- Attributes:
- tableau_Tableau
The node fractions
c, weightsband error weightsb_error.
Initialize with the method’s degree, error exponent and tableau.
- Parameters:
- degreeint
Highest polynomial degree the method integrates exactly, >= 0.
- error_exponentint
The power k of the error estimate’s \(h^k\) scaling, >= 2.
- tableau_Tableau
The node fractions
c, weightsband error weightsb_error.- referencestuple of str
Keys of
docs/references.bibfor the method’s sources. Default: ().
- Raises:
- ValueError
If
degreeis not an int >= 0,error_exponentis not an int >= 2, or the node fractions are not a 1-D tensor of at least two non-decreasing values from 0 to 1.
- class padaquad.methods.VariableMethodClass(degree, error_exponent, c, weight_function, references=())#
A method whose weights are computed from each subinterval’s node positions.
A new subinterval is evaluated at the node fractions
c. The weights come fromweight_functionapplied to the subinterval’s nodes rescaled to [0, 1], so the method stays correct when the nodes sit elsewhere. Each method is an instance with its owncandweight_function(seemethods/interpolatory.py), as each uniform method is an instance with its own tableau.weight_functionmust be a module-level function:clone()keeps it by reference, and solvers can then be pickled (a lambda or a local function cannot be pickled). It is stored on the instance; assigning it to the class instead would make Python passselfto it.- Attributes:
- ctorch.Tensor
Node fractions of a new subinterval, non-decreasing from 0 to 1. Shape: [C].
- weight_functionCallable
Maps node positions rescaled to [0, 1], shape [N, C, T], to
(b, b_error), each of shape [N, C], in the dtype and on the device of its input.
Initialize with the degree, error exponent, node fractions and weight function.
- Parameters:
- degreeint
Highest polynomial degree the method integrates exactly at general node positions, >= 0.
- error_exponentint
The power k of the error estimate’s \(h^k\) scaling, >= 2.
- ctorch.Tensor
Node fractions of a new subinterval. Shape: [C].
- weight_functioncallable
Module-level function mapping nodes rescaled to [0, 1], shape [N, C, T], to
(b, b_error), each of shape [N, C].- referencestuple of str
Keys of
docs/references.bibfor the method’s sources. Default: ().
- Raises:
- ValueError
If
degreeis not an int >= 0,error_exponentis not an int >= 2, orcis not a 1-D tensor of at least two non-decreasing values from 0 to 1.
- class padaquad.methods.DegreeMeasurement(degree, lower_degree, errors, lower_errors)#
Result of
MethodClass.measure_degree: the two measured degrees and the errors.- Attributes:
- degreeint
Measured degree of the method (weights
b): the highest polynomial degree it integrates exactly. -1 if it does not even integrate a constant exactly.- lower_degreeint
Measured degree of the embedded lower-degree method used in the error estimate (weights
b - b_error).- errorstorch.Tensor
Error of the method on the Legendre polynomial P_k(2t - 1) over [0, 1], for k = 0, 1, …, 2C. Shape: [2C + 1], float64.
- lower_errorstorch.Tensor
The same for the lower-degree method. Shape: [2C + 1].
Create new instance of DegreeMeasurement(degree, lower_degree, errors, lower_errors)