scipy.integrate — Numerical integration
The scipy.integrate submodule provides numerical integration
routines for real-valued, single-variable functions. The algorithms are
not ported from CPython’s scipy.integrate but derived from the
reference implementation at https://www.genivia.com/qthsh.html.
Numerical integration works best with float64 math enabled. With float32
math the routines still work, with reduced precision. The required error
tolerance can be specified via the eps keyword argument; the
default is the compile-time etolerance value (1e-14 for fp64,
1e-8 for fp32). Complex numbers are not supported.
Functions
- scipy.integrate.quad(f: Callable[[float], float], a: float, b: float, *, order: int = 5, eps: float = etolerance) tuple[float, float]
Integrate f from a to b using an Adaptive Gauss-Kronrod (G10, K21) quadrature. This is the recommended general-purpose integrator.
- Parameters:
f – callable taking a single
floatand returning afloat.a – lower integration limit.
b – upper integration limit.
order – order of integration (default
5).eps – error tolerance (default
etolerance).
- Returns:
a 2-tuple
(result, error)of floats giving the value of the integral and an error estimate.
- scipy.integrate.romberg(f: Callable[[float], float], a: float, b: float, *, steps: int = 100, eps: float = etolerance) float
Integrate f from a to b using Romberg’s method, a Newton-Cotes formula that evaluates the integrand at equally spaced points. Best suited to integrands with continuous derivatives. Note that
scipy.integrate.rombergis deprecated in CPython since SciPy 1.12.0; preferquad()for new code.- Parameters:
f – callable taking a single
floatand returning afloat.a – lower integration limit.
b – upper integration limit.
steps – number of steps (default
100).eps – error tolerance (default
etolerance).
- Returns:
the value of the integral as a
float.
- scipy.integrate.simpson(f: Callable[[float], float], a: float, b: float, *, steps: int = 100, eps: float = etolerance) float
Integrate f from a to b using adaptive Simpson’s rule. Unlike CPython’s
scipy.integrate.simpsonthis function takes a callable and chooses the sample spacing internally rather than taking an array of pre-computed function values.- Parameters:
f – callable taking a single
floatand returning afloat.a – lower integration limit.
b – upper integration limit.
steps – number of steps (default
100).eps – error tolerance (default
etolerance).
- Returns:
the value of the integral as a
float.
- scipy.integrate.tanhsinh(f: Callable[[float], float], a: float, b: float, *, levels: int = 6, eps: float = etolerance) tuple[float, float]
Integrate f from a to b using the Tanh-Sinh, Sinh-Sinh, and Exp-Sinh (double-exponential) quadrature family. This is the routine to use when the integrand has singularities or infinite derivatives at the endpoints, and it is the only routine in this submodule that accepts infinite integration limits (e.g.
-np.inf,np.inf).- Parameters:
f – callable taking a single
floatand returning afloat.a – lower integration limit; may be
-np.inf.b – upper integration limit; may be
np.inf.levels – number of refinement levels (default
6).eps – error tolerance (default
etolerance).
- Returns:
a 2-tuple
(result, error)of floats giving the value of the integral and an error estimate.