bundles / scipy 1.17.1 / scipy / integrate / _tanhsinh / tanhsinh
function
scipy.integrate._tanhsinh:tanhsinh
Signature
def tanhsinh ( f , a , b , * , args = () , 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.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 shapeshapeorshape + (n,), where(n,)is 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
success
status
status
integral
integral
error
error
maxlevel
maxlevel
nfev
nfev
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.
Array API Standard Support
tanhsinh has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.
==================== ==================== ==================== Library CPU GPU ==================== ==================== ==================== NumPy ✅ n/a CuPy n/a ✅ PyTorch ✅ ✅ JAX ⛔ ⛔ Dask ⛔ n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
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✗
tanhsinh(f, -20, 20).integral
✗tanhsinh(f, -np.inf, 1000).integral
✗tanhsinh(f, -np.inf, 0).integral + tanhsinh(f, 0, 1000).integral
✗res = tanhsinh(f, 20, 30, rtol=1e-10)
✓res.integral, res.error
✗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)
✗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)✓
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✓
res.nfev
✓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✓
See also
Aliases
-
scipy.integrate.tanhsinh