padaquad.IntegrationResult#

class padaquad.IntegrationResult(*, integral, integral_error, mesh_init, mesh_final, nodes, h, y, mesh_quadratures, mesh_quadrature_errors, error_ratios, gradient_taken, y0, mesh_optimal=None, tracked_variables=None, loss=None, converged=None, n_start_steps=None, n_start_steps_split=None, max_split_depth=None)#

Result of an integration: the integral, its error estimate, the mesh and diagnostics.

Every field is required and passed by keyword (kw_only=True), except mesh_optimal, tracked_variables, loss, converged, n_start_steps, n_start_steps_split and max_split_depth, which default to None. Below, N is the number of accepted subintervals, C the number of nodes per subinterval, P = C + (N - 1)(C - 1) the number of distinct nodes, and M the number of mesh points (see The integrand contract).

integral, integral_error, y0, mesh_optimal, mesh_init and mesh_final are on the solver’s device; the other tensors are on result_device (default "cpu").

The solver also builds one result per batch of accepted subintervals and passes it to loss_fxn. That result covers only the batch: integral is the batch’s contribution, y0 is zero, mesh_optimal, loss, converged and the split counts are None, and nodes, y and tracked_variables keep one row per subinterval (shapes [N, C, T], [N, C, D] and [N, C, ...]) instead of the flattened shapes below.

Attributes:
integraltorch.Tensor

y0 plus the integral of f from mesh_init to mesh_final. Shape [D].

integral_errortorch.Tensor

Sum of the subintervals’ signed error estimates. Shape [D]. With take_gradient=False, subintervals accepted and later split again can leave it off by their error estimates. See Tolerances and error ratios.

mesh_optimaltorch.Tensor or None

Mesh to start the next integration of the same integrand from (with same_f=True): the final mesh with low-error subintervals merged and failing ones split. Shape [M, T]. See The optimal mesh and the merge rule.

mesh_inittorch.Tensor

Lower integration bound. Shape [T].

mesh_finaltorch.Tensor

Upper integration bound. Shape [T].

nodestorch.Tensor

Nodes of all accepted subintervals in one non-decreasing sequence; the node shared by two neighbouring subintervals appears once. Shape [P, T].

htorch.Tensor

Width of each accepted subinterval. Shape [N, T].

ytorch.Tensor

Integrand values at nodes. Shape [P, D].

tracked_variablestuple of torch.Tensor or None

Extra values the integrand returned beside its values, at nodes and not integrated; None if it returned none. Each tensor is detached, with shape [P, ...]. See Tracked variables.

mesh_quadraturestorch.Tensor

Each subinterval’s contribution to the integral, \(h \sum_i b_i y_i\). Shape [N, D].

mesh_quadrature_errorstorch.Tensor

Absolute value of each subinterval’s error estimate, \(|h \sum_i b^{\text{error}}_i y_i|\). Shape [N, D].

error_ratiostorch.Tensor

Each subinterval’s error ratio when it was accepted, against the tolerance of that moment (with error_norm="failure_fraction", the fraction of failing outputs instead). Shape [N].

losstorch.Tensor or None

Sum over batches of loss_fxn applied to the per-batch results; by default the integral without y0. See Gradients.

gradient_takenbool

The call’s take_gradient: whether loss.backward() ran after each batch (it runs only when the loss requires a gradient).

y0torch.Tensor

The offset added to the integral, after conversion to the solver’s dtype and device. Shape [D].

convergedbool or None

True if every accepted subinterval meets the tolerance judged against the final integral (in cumulative mode, the final running integral through it); otherwise False, with a logged warning. See converged is False.

n_start_stepsint or None

Number of subintervals in the starting mesh (the given mesh, the mesh stored by same_f=True, or a new random one).

n_start_steps_splitint or None

Number of starting subintervals that were split at least once during the integration (splits made to build mesh_optimal do not count).

max_split_depthint or None

Largest number of times one starting subinterval was halved: 0 if none was split, at most max_adaptive_splits.

Examples

>>> import torch
>>> from padaquad import integrate
>>> _ = torch.manual_seed(0)  # the starting mesh is random
>>> result = integrate(
...     lambda t: torch.exp(t),
...     atol=1e-10,
...     rtol=1e-10,
...     mesh_init=torch.tensor([0.0], dtype=torch.float64),
...     mesh_final=torch.tensor([1.0], dtype=torch.float64),
...     y0=torch.tensor([1.0], dtype=torch.float64),
...     device="cpu",
...     max_batch=10_000,
... )
>>> print(round(float(result.integral), 8))  # 1 + (e - 1) = e
2.71828183
>>> print(result.converged, abs(float(result.integral_error)) < 1e-10)
True True
>>> print(round(float(result.h.sum()), 8))  # the widths add up to 1
1.0
__init__(*, integral, integral_error, mesh_init, mesh_final, nodes, h, y, mesh_quadratures, mesh_quadrature_errors, error_ratios, gradient_taken, y0, mesh_optimal=None, tracked_variables=None, loss=None, converged=None, n_start_steps=None, n_start_steps_split=None, max_split_depth=None)#

Methods

__init__(*, integral, integral_error, ...[, ...])

Attributes

converged

loss

max_split_depth

mesh_optimal

n_start_steps

n_start_steps_split

percent_start_steps_split

Percentage of the starting mesh's subintervals that were split at least once.

tracked_variables

integral

integral_error

mesh_init

mesh_final

nodes

h

y

mesh_quadratures

mesh_quadrature_errors

error_ratios

gradient_taken

y0