padaquad.integrate#

padaquad.integrate(f, method=None, sampling='uniform', **options)#

Compute the integral of f from mesh_init to mesh_final in one call.

Builds the solver with adaptive_quadrature(sampling, method, ...) and calls its integrate method once. Each keyword in options goes to that integrate call if it is one of the call’s arguments (for example mesh_init, mesh_final, mesh, y0, f_args, take_gradient, max_batch, result_device), and to the solver’s constructor otherwise (for example atol, rtol, remove_cut, merge_rule, use_absolute_error_ratio, device). Defaults are those of the solver class. A one-call solver has no previous mesh, so same_f=True has no effect here; use adaptive_quadrature for repeated calls.

The integrand depends only on the integration variable t (and on f_args), never on the running value of the integral, so the nodes of many subintervals can be evaluated in one batch.

Parameters:
fCallable

The integrand. Takes points of shape [N, T] and returns values of shape [N, D], or a pair (values, tracked_variables); see The integrand contract and Tracked variables.

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).

samplingstr or steps

"uniform" (fixed node fractions, the default) or "variable" (weights computed from the node positions), or any other sampling type adaptive_quadrature accepts.

**optionsAny

Keyword arguments of the solver’s integrate method and of its constructor.

Returns:
IntegrationResult

The integral, its error estimate, the mesh and convergence diagnostics.

Raises:
ValueError

If sampling is not a supported sampling type, or method is not a method of that sampling type.

TypeError

If an option is neither an argument of the integrate call nor of the constructor.

Examples

The integral of sin(t) from 0 to pi is 2.

>>> import torch
>>> from padaquad import integrate
>>> _ = torch.manual_seed(0)  # the starting mesh is random
>>> result = integrate(
...     torch.sin,
...     atol=1e-10,
...     rtol=1e-10,
...     mesh_init=torch.tensor([0.0], dtype=torch.float64),
...     mesh_final=torch.tensor([torch.pi], dtype=torch.float64),
...     device="cpu",
...     max_batch=10_000,
... )
>>> print(round(float(result.integral), 8), result.converged)
2.0 True