padaquad.integrate#
- padaquad.integrate(f, method=None, sampling='uniform', **options)#
Compute the integral of
ffrommesh_inittomesh_finalin one call.Builds the solver with
adaptive_quadrature(sampling, method, ...)and calls itsintegratemethod once. Each keyword inoptionsgoes to thatintegratecall if it is one of the call’s arguments (for examplemesh_init,mesh_final,mesh,y0,f_args,take_gradient,max_batch,result_device), and to the solver’s constructor otherwise (for exampleatol,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, sosame_f=Truehas no effect here; useadaptive_quadraturefor repeated calls.The integrand depends only on the integration variable
t(and onf_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) orVARIABLE_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 typeadaptive_quadratureaccepts.- **optionsAny
Keyword arguments of the solver’s
integratemethod and of its constructor.
- Returns:
- IntegrationResult
The integral, its error estimate, the mesh and convergence diagnostics.
- Raises:
- ValueError
If
samplingis not a supported sampling type, ormethodis not a method of that sampling type.- TypeError
If an option is neither an argument of the
integratecall nor of the constructor.
Examples
The integral of
sin(t)from 0 topiis 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