Quick start#

import math
import torch
from padaquad import integrate

result = integrate(
    torch.sin,
    mesh_init=torch.tensor([0.0], dtype=torch.float64),
    mesh_final=torch.tensor([math.pi], dtype=torch.float64),
)
print(round(float(result.integral), 6))  # 2.0
print(float(result.integral_error))  # estimated error: signed sum over subintervals
print(result.converged)  # True

The integrand f takes a tensor t of shape [N, T] (a batch of points of the integration variable) and returns a tensor of shape [N, D] (its values there). Because f depends only on t, the solver can evaluate the nodes of many subintervals in one call, on a CPU or a GPU.

The integral above uses the default method and tolerances, and a batch size chosen from the free memory: the first call measures how much memory one evaluation of f uses, which takes a few seconds; pass max_batch to skip it. See User guide for what each option does, and padaquad.integrate() for the full list.