padaquad.adaptive_quadrature#

padaquad.adaptive_quadrature(sampling_type='uniform', method=None, **options)#

Build the solver for a sampling type and a method.

Returns a UniformAdaptiveQuadrature for uniform sampling and a VariableAdaptiveQuadrature for variable sampling. sampling_type and method may be passed by position; every other option is a keyword argument of the solver’s constructor.

Parameters:
sampling_typestr or steps

"uniform" (fixed node fractions) or "variable" (weights computed from the node positions); "adaptive_uniform", "adaptive_variable" and the steps values are also accepted.

methodstr or None

Name of the method, a key of UNIFORM_METHODS (uniform sampling) or VARIABLE_METHODS (variable sampling). None (default) means "gk21" (uniform) or "interpolatory3_variable" (variable).

**optionsAny

Keyword arguments of the solver’s constructor (for example atol, rtol, f, mesh_init, mesh_final, max_batch, device).

Returns:
UniformAdaptiveQuadrature or VariableAdaptiveQuadrature

The solver.

Raises:
ValueError

If sampling_type is not "uniform" or "variable" (or one of their other names), or method is not a method of that sampling type.

TypeError

If an option is not an argument of the solver’s constructor.

Examples

Build the solver once and call it twice on the same integrand. With same_f=True the second call starts from the mesh the first call ended with and reuses its max_batch.

>>> import torch
>>> from padaquad import adaptive_quadrature
>>> _ = torch.manual_seed(0)  # the starting mesh is random
>>> solver = adaptive_quadrature(
...     "variable", "interpolatory3_variable", atol=1e-8, rtol=1e-8, device="cpu"
... )
>>> bounds = {
...     "mesh_init": torch.tensor([0.0], dtype=torch.float64),
...     "mesh_final": torch.tensor([1.0], dtype=torch.float64),
... }
>>> first = solver.integrate(lambda t: t**2, max_batch=10_000, **bounds)
>>> second = solver.integrate(lambda t: t**2, same_f=True, **bounds)
>>> print(round(float(first.integral), 8), round(float(second.integral), 8))
0.33333333 0.33333333