bundles / scipy latest / scipy / integrate / _tanhsinh / tanhsinh
function
scipy.integrate._tanhsinh:tanhsinh
source: build-install/usr/lib/python3.14/site-packages/scipy/integrate/_tanhsinh.py:22
Signature
def tanhsinh(f, a, b, *, args = (), kwargs = None, log = False, maxlevel = None, minlevel = 2, atol = None, rtol = None, preserve_shape = False, callback = None)Summary
Evaluate a convergent integral numerically using tanh-sinh quadrature.
Extended Summary
In practice, tanh-sinh quadrature achieves quadratic convergence for many integrands: the number of accurate digits scales roughly linearly with the number of function evaluations [1].
Either or both of the limits of integration may be infinite, and singularities at the endpoints are acceptable. Divergent integrals and integrands with non-finite derivatives or singularities within an interval are out of scope, but the latter may be evaluated be calling tanhsinh on each sub-interval separately.
Parameters
f: callableThe function to be integrated. The signature must be
f(xi: ndarray, *argsi) -> ndarraywhere each element of
xiis a finite real number andargsiis a tuple, which may contain an arbitrary number of arrays that are broadcastable withxi.fmust be an elementwise function: see documentation of parameterpreserve_shapefor details. It must not mutate the arrayxior the arrays inargsi. Iffreturns a value with complex dtype when evaluated at either endpoint, subsequent argumentsxwill have complex dtype (but zero imaginary part).a, b: float array_likeReal lower and upper limits of integration. Must be broadcastable with one another and with arrays in
args. Elements may be infinite.args: tuple of array_like, optionalAdditional positional array arguments to be passed to
f. Arrays must be broadcastable with one another and the arrays ofaandb. If the callable for which the root is desired requires arguments that are not broadcastable withx, wrap that callable withfsuch thatfaccepts onlyxand broadcastable*args.kwargs: dict of str:array_like, optionalAdditional keyword arguments to be passed to
f. Seeargs.log: bool, default: FalseSetting to True indicates that
freturns the log of the integrand and thatatolandrtolare expressed as the logs of the absolute and relative errors. In this case, the result object will contain the log of the integral and error. This is useful for integrands for which numerical underflow or overflow would lead to inaccuracies. Whenlog=True, the integrand (the exponential off) must be real, but it may be negative, in which case the log of the integrand is a complex number with an imaginary part that is an odd multiple of π.maxlevel: int, default: 10The maximum refinement level of the algorithm.
At the zeroth level,
fis called once, performing 16 function evaluations. At each subsequent level,fis called once more, approximately doubling the number of function evaluations that have been performed. Accordingly, for many integrands, each successive level will double the number of accurate digits in the result (up to the limits of floating point precision).The algorithm will terminate after completing level
maxlevelor after another termination condition is satisfied, whichever comes first.minlevel: int, default: 2The level at which to begin iteration (default: 2). This does not change the total number of function evaluations or the abscissae at which the function is evaluated; it changes only the number of times
fis called. Ifminlevel=k, then the integrand is evaluated at all abscissae from levels0throughkin a single call. Note that ifminlevelexceedsmaxlevel, the providedminlevelis ignored, andminlevelis set equal tomaxlevel.atol, rtol: float, optionalAbsolute termination tolerance (default: 0) and relative termination tolerance (default:
eps**0.75, whereepsis the precision of the result dtype), respectively. Must be non-negative and finite iflogis False, and must be expressed as the log of a non-negative and finite number iflogis True. Iteration will stop whenres.error < atolorres.error < res.integral * rtol.preserve_shape: bool, default: FalseIn the following, "arguments of
f" refers to the arrayxiand any arrays withinargsi. Letshapebe the broadcasted shape ofa,b, and all elements ofargs(which is conceptually distinct fromxiandargsipassed intof).When
preserve_shape=False(default),fmust accept arguments of any broadcastable shapes.When
preserve_shape=True,fmust accept arguments of shapeshape + (n,), wherenis the number of abscissae at which the function is being evaluated.
In either case, for each scalar element
xi[j]withinxi, the array returned byfmust include the scalarf(xi[j])at the same index. Consequently, the shape of the output is always the shape of the inputxi.See Examples.
callback: callable, optionalAn optional user-supplied function to be called before the first iteration and after each iteration. Called as
callback(res), whereresis a_RichResultsimilar to that returned by tanhsinh (but containing the current iterate's values of all variables). Ifcallbackraises aStopIteration, the algorithm will terminate immediately and tanhsinh will return a result object.callbackmust not mutate res or its attributes.
Returns
res: _RichResultAn object similar to an instance of scipy.optimize.OptimizeResult with the following attributes. (The descriptions are written as though the values will be scalars; however, if
freturns an array, the outputs will be arrays of the same shape.)success
Truewhen the algorithm terminated successfully (status0).Falseotherwise.status
An integer representing the exit status of the algorithm.
0The algorithm converged to the specified tolerances.-1(unused)-2The maximum number of iterations was reached.-3A non-finite value was encountered.-4Iteration was terminated bycallback.1The algorithm is proceeding normally (incallbackonly).
integral
An estimate of the integral.
error
An estimate of the error. Only available if level two or higher has been completed; otherwise NaN.
maxlevel
The maximum refinement level used.
nfev
The number of points at which
fwas evaluated.
Notes
Implements the algorithm as described in [1] with minor adaptations for finite-precision arithmetic, including some described by [2] and [3]. The tanh-sinh scheme was originally introduced in [4].
Two error estimation schemes are described in [1] Section 5: one attempts to detect and exploit quadratic convergence; the other simply compares the integral estimates at successive levels. While neither is theoretically rigorous or conservative, both work well in practice. Our error estimate uses the minimum of these two schemes with a lower bound of eps * res.integral.
Due to floating-point error in the abscissae, the function may be evaluated at the endpoints of the interval during iterations, but the values returned by the function at the endpoints will be ignored.
Examples
Evaluate the Gaussian integral:import numpy as np from scipy.integrate import tanhsinh def f(x): return np.exp(-x**2) res = tanhsinh(f, -np.inf, np.inf)✓
res.integral # true value is np.sqrt(np.pi), 1.7724538509055159 res.error # actual error is 0✗
np.float64(4.0007963937534104e-16)The value of the Gaussian function (bell curve) is nearly zero for arguments sufficiently far from zero, so the value of the integral over a finite interval is nearly the same.
tanhsinh(f, -20, 20).integral
✗np.float64(1.7724538509055137)However, with unfavorable integration limits, the integration scheme may not be able to find the important region.
tanhsinh(f, -np.inf, 1000).integral
✗np.float64(4.500490856616431)In such cases, or when there are singularities within the interval, break the integral into parts with endpoints at the important points.
tanhsinh(f, -np.inf, 0).integral + tanhsinh(f, 0, 1000).integral
✗np.float64(1.772453850905404)For integration involving very large or very small magnitudes, use log-integration. (For illustrative purposes, the following example shows a case in which both regular and log-integration work, but for more extreme limits of integration, log-integration would avoid the underflow experienced when evaluating the integral normally.)
res = tanhsinh(f, 20, 30, rtol=1e-10)
✓res.integral, res.error
✗(np.float64(4.7819613911309014e-176), np.float64(4.670364401645202e-187))
def log_f(x): return -x**2 res = tanhsinh(log_f, 20, 30, log=True, rtol=np.log(1e-10))✓
np.exp(res.integral), np.exp(res.error)
✗(np.float64(4.7819613911306924e-176), np.float64(4.670364401645093e-187))The limits of integration and elements of `args` may be broadcastable arrays, and integration is performed elementwise.
from scipy import stats dist = stats.gausshyper(13.8, 3.12, 2.51, 5.18) a, b = dist.support() x = np.linspace(a, b, 100) res = tanhsinh(dist.pdf, a, x) ref = dist.cdf(x) np.allclose(res.integral, ref)✓
TrueBy default, `preserve_shape` is False, and therefore the callable `f` may be called with arrays of any broadcastable shapes. For example:
shapes = [] def f(x, c): shape = np.broadcast_shapes(x.shape, c.shape) shapes.append(shape) return np.sin(c*x) c = [1, 10, 30, 100] res = tanhsinh(f, 0, 1, args=(c,), minlevel=1) shapes✓
[(4,), (4, 34), (4, 32), (3, 64), (2, 128), (1, 256)]To understand where these shapes are coming from - and to better understand how `tanhsinh` computes accurate results - note that higher values of ``c`` correspond with higher frequency sinusoids. The higher frequency sinusoids make the integrand more complicated, so more function evaluations are required to achieve the target accuracy:
res.nfev
✓array([ 67, 131, 259, 515], dtype=int32)The initial ``shape``, ``(4,)``, corresponds with evaluating the integrand at a single abscissa and all four frequencies; this is used for input validation and to determine the size and dtype of the arrays that store results. The next shape corresponds with evaluating the integrand at an initial grid of abscissae and all four frequencies. Successive calls to the function double the total number of abscissae at which the function has been evaluated. However, in later function evaluations, the integrand is evaluated at fewer frequencies because the corresponding integral has already converged to the required tolerance. This saves function evaluations to improve performance, but it requires the function to accept arguments of any shape. "Vector-valued" integrands, such as those written for use with `scipy.integrate.quad_vec`, are unlikely to satisfy this requirement. For example, consider
def f(x): return [x, np.sin(10*x), np.cos(30*x), x*np.sin(100*x)**2]✓
shapes = [] def f(x): shapes.append(x.shape) x0, x1, x2, x3 = x return [x0, np.sin(10*x1), np.cos(30*x2), x3*np.sin(100*x3)] a = np.zeros(4) res = tanhsinh(f, a, 1, preserve_shape=True) shapes✓
[(4, 1), (4, 66), (4, 64), (4, 128), (4, 256)]Here, the broadcasted shape of `a` and `b` is ``(4,)``, so with ``preserve_shape=True``, the callable ``f`` is always called with argument ``x`` of shape ``(4, n)``, where ``n`` is any number of abscissae.
See also
Aliases
scipy.integrate.tanhsinh