derivkit.utils.extrapolation module

Extrapolation methods for numerical approximations.

derivkit.utils.extrapolation.richardson_extrapolate(base_values: Sequence[ndarray[tuple[Any, ...], dtype[float64]] | float], p: int, r: float = 2.0) ndarray[tuple[Any, ...], dtype[float64]] | float

Computes Richardson extrapolation on a sequence of approximations.

Richardson extrapolation improves the accuracy of a sequence of numerical approximations that converge with a known leading-order error term. Given a sequence of approximations computed with decreasing step sizes, this method combines them to eliminate the leading error term, yielding a more accurate estimate of the true value.

Parameters:
  • base_values – Sequence of approximations at different step sizes. The step sizes are assumed to decrease by a factor of r between successive entries.

  • p – The order of the leading error term in the approximations.

  • r – The step-size reduction factor between successive entries (default is 2.0).

Returns:

The extrapolated value with improved accuracy.

Raises:

ValueError – If base_values has fewer than two entries.

derivkit.utils.extrapolation.ridders_extrapolate(base_values: ~typing.Sequence[~numpy.ndarray[tuple[~typing.Any, ...], ~numpy.dtype[~numpy.float64]] | float], r: float = 2.0, *, extrapolator=<function richardson_extrapolate>, p: int = 2) tuple[ndarray[tuple[Any, ...], dtype[float64]] | float, float]

Computes a Ridders-style extrapolation on a sequence of approximations.

This builds the usual Ridders diagonal assuming a central finite-difference scheme (leading error is approximately O(h^2)) by repeatedly extrapolating prefixes of base_values. By default it uses richardson_extrapolate() with p=2, but a different extrapolator can be passed if needed.

Parameters:
  • base_values – Sequence of derivative approximations at step sizes h, h/r, h/r^2, … (all same shape: scalar, vector, or tensor).

  • r – Step-size reduction factor (default 2.0).

  • extrapolator – Function implementing the extrapolation step. Must have the signature extrapolator(base_values, p, r) -> array_like. Defaults to richardson_extrapolate().

  • p – Leading error order passed to extrapolator (default 2).

Returns:

  • best_value is the extrapolated estimate chosen from the diagonal entries.

  • error_estimate is a heuristic scalar error scale given by the minimum difference between consecutive diagonal elements.

Return type:

A tuple (best_value, error_estimate) where

Raises:

ValueError – If fewer than two base values are provided.