API Reference¶
Public API¶
integrationdifferentiation — SciPy-backed numerical integration and differentiation.
All public symbols are re-exported from integrationdifferentiation.core so
callers can simply write:
from integrationdifferentiation import integrate_adaptive, gauss_quadrature_rule
This package is a self-contained set of integration and differentiation utilities inspired by the IMSL Fortran Numerical Library Integration and Differentiation chapter, backed by scipy.integrate and numpy.
- class integrationdifferentiation.IntegrationResult(value, error, success, message, n_fev=0)¶
Bases:
objectResult of a numerical integration procedure.
- Parameters:
value (float)
error (float)
success (bool)
message (str)
n_fev (int)
- value¶
Integral estimate.
- Type:
float
- error¶
Estimated absolute error bound.
- Type:
float
- success¶
Whether the integration succeeded.
- Type:
bool
- message¶
Description of the termination condition.
- Type:
str
- n_fev¶
Number of function evaluations performed.
- Type:
int
- class integrationdifferentiation.GaussQuadratureRule(points, weights, n)¶
Bases:
objectA Gauss quadrature rule (abscissas and weights).
- Parameters:
points (ndarray)
weights (ndarray)
n (int)
- points¶
Quadrature abscissas sorted in ascending order.
- Type:
np.ndarray
- weights¶
Corresponding quadrature weights.
- Type:
np.ndarray
- n¶
Number of quadrature points.
- Type:
int
- integrationdifferentiation.integrate_adaptive(f, a, b, abs_tol=1.49e-08, rel_tol=1.49e-08, max_fev=500)¶
General-purpose adaptive quadrature (IMSL QDAG).
Uses global adaptive integration (QUADPACK DQAGSE) via
scipy.integrate.quad(). Suitable for smooth integrands with finite or infinite limits.- Parameters:
f (Callable) – Integrand:
f(x) -> float.a (float) – Lower integration limit (may be
-np.inf).b (float) – Upper integration limit (may be
np.inf).abs_tol (float) – Absolute error tolerance. Defaults to
1.49e-8.rel_tol (float) – Relative error tolerance. Defaults to
1.49e-8.max_fev (int) – Maximum number of function evaluations. Defaults to
500.
- Returns:
Integral estimate with error bound and diagnostics.
- Return type:
- Raises:
TypeError – If
fis not callable.ValueError – If
abs_tolorrel_tolis not positive.
- integrationdifferentiation.integrate_adaptive_endpoint_singular(f, a, b, abs_tol=1.49e-08, max_fev=500)¶
Adaptive quadrature with endpoint singularities (IMSL QDAGS).
Uses
scipy.integrate.quad()which automatically handles integrable algebraic and logarithmic endpoint singularities (QUADPACK DQAGSE).- Parameters:
f (Callable) – Integrand:
f(x) -> float. May diverge ataorbprovided the singularity is integrable.a (float) – Lower integration limit.
b (float) – Upper integration limit.
abs_tol (float) – Absolute error tolerance. Defaults to
1.49e-8.max_fev (int) – Maximum number of function evaluations. Defaults to
500.
- Returns:
Integral estimate with error bound and diagnostics.
- Return type:
- Raises:
TypeError – If
fis not callable.ValueError – If
abs_tolis not positive.
- integrationdifferentiation.integrate_adaptive_singular_points(f, a, b, points, abs_tol=1.49e-08, max_fev=500)¶
Adaptive quadrature with specified interior singularity points (IMSL QDAGP).
Splits the integration interval at each specified singularity point and applies adaptive integration to each sub-interval using QUADPACK DQAGPE.
- Parameters:
f (Callable) – Integrand:
f(x) -> float.a (float) – Lower integration limit.
b (float) – Upper integration limit.
points (list[float]) – Interior points where
fmay have a singularity or discontinuity. Must satisfya < points[i] < b.abs_tol (float) – Absolute error tolerance. Defaults to
1.49e-8.max_fev (int) – Maximum number of function evaluations. Defaults to
500.
- Returns:
Integral estimate with error bound and diagnostics.
- Return type:
- Raises:
TypeError – If
fis not callable.ValueError – If
abs_tolis not positive or any point is outside(a, b).
- integrationdifferentiation.integrate_adaptive_1d(f, a, b, abs_tol=1.49e-08, max_fev=500)¶
Adaptive 1D quadrature with possible internal singularity (IMSL QDAG1D).
Uses
scipy.integrate.quad()with a finer local control on singularity handling. Equivalent to QDAG but exposes a separate entry point consistent with the IMSL function family.- Parameters:
f (Callable) – Integrand:
f(x) -> float.a (float) – Lower integration limit.
b (float) – Upper integration limit.
abs_tol (float) – Absolute error tolerance. Defaults to
1.49e-8.max_fev (int) – Maximum number of function evaluations. Defaults to
500.
- Returns:
Integral estimate with error bound and diagnostics.
- Return type:
- Raises:
TypeError – If
fis not callable.ValueError – If
abs_tolis not positive.
- integrationdifferentiation.integrate_adaptive_infinite(f, a=-inf, b=inf, abs_tol=1.49e-08, max_fev=500)¶
Adaptive quadrature on an infinite or semi-infinite interval (IMSL QDAGI).
Uses
scipy.integrate.quad()with±np.inflimits. The integration is transformed to a finite interval internally by QUADPACK DQAGIE.- Parameters:
f (Callable) – Integrand:
f(x) -> float. Must decay sufficiently fast for the integral to converge.a (float) – Lower limit. Use
-np.inffor a fully infinite lower bound. Defaults to-np.inf.b (float) – Upper limit. Use
np.inffor a fully infinite upper bound. Defaults tonp.inf.abs_tol (float) – Absolute error tolerance. Defaults to
1.49e-8.max_fev (int) – Maximum number of function evaluations. Defaults to
500.
- Returns:
Integral estimate with error bound and diagnostics.
- Return type:
- Raises:
TypeError – If
fis not callable.ValueError – If
abs_tolis not positive.
- integrationdifferentiation.integrate_weighted_oscillatory(f, a, b, omega, weight='sin', abs_tol=1.49e-08, max_fev=500)¶
Adaptive weighted oscillatory quadrature (IMSL QDAWO).
Computes
∫_a^b f(x) * w(x) dxwherew(x)is eithersin(omega*x)orcos(omega*x). Uses QUADPACK DQAWO viascipy.integrate.quad()with the appropriateweightandwvararguments.- Parameters:
f (Callable) – Non-oscillatory part of the integrand:
f(x) -> float.a (float) – Lower integration limit (must be finite).
b (float) – Upper integration limit (must be finite).
omega (float) – Oscillation frequency
omegain the weight function.weight (str) – Weight type:
'sin'forsin(omega*x)or'cos'forcos(omega*x). Defaults to'sin'.abs_tol (float) – Absolute error tolerance. Defaults to
1.49e-8.max_fev (int) – Maximum number of function evaluations. Defaults to
500.
- Returns:
Integral estimate with error bound and diagnostics.
- Return type:
- Raises:
TypeError – If
fis not callable.ValueError – If
weightis not'sin'or'cos', or ifabs_tolis not positive, or if limits are infinite.
- integrationdifferentiation.integrate_weighted_fourier(f, a, omega, weight='sin', abs_tol=1.49e-08, max_fev=500)¶
Adaptive weighted Fourier quadrature on
[a, ∞)(IMSL QDAWF).Computes
∫_a^∞ f(x) * w(x) dxusing QUADPACK DQAWFE viascipy.integrate.quad()with semi-infinite limits and trigonometric weight.- Parameters:
f (Callable) – Non-oscillatory part of the integrand:
f(x) -> float. Must decay to zero asx → ∞.a (float) – Lower integration limit (must be finite).
omega (float) – Oscillation frequency in the weight function.
weight (str) – Weight type:
'sin'forsin(omega*x)or'cos'forcos(omega*x). Defaults to'sin'.abs_tol (float) – Absolute error tolerance. Defaults to
1.49e-8.max_fev (int) – Maximum number of function evaluations. Defaults to
500.
- Returns:
Integral estimate with error bound and diagnostics.
- Return type:
- Raises:
TypeError – If
fis not callable.ValueError – If
weightis not'sin'or'cos',abs_tolis not positive, orais infinite.
- integrationdifferentiation.integrate_weighted_algebraic(f, a, b, alpha, beta, weight_type=1, abs_tol=1.49e-08, max_fev=500)¶
Adaptive quadrature with algebraic-logarithmic endpoint weight (IMSL QDAWS).
Computes
∫_a^b f(x) * w(x) dxwherew(x)has algebraic and/or logarithmic endpoint singularities. Uses QUADPACK DQAWS viascipy.integrate.quad().Weight types (
weight_type):1:(x-a)^alpha * (b-x)^beta2:(x-a)^alpha * (b-x)^beta * log(x-a)3:(x-a)^alpha * (b-x)^beta * log(b-x)4:(x-a)^alpha * (b-x)^beta * log(x-a) * log(b-x)
- Parameters:
f (Callable) – Integrand function:
f(x) -> float.a (float) – Lower integration limit (finite).
b (float) – Upper integration limit (finite), must satisfy
b > a.alpha (float) – Exponent for the
(x-a)factor. Must be> -1.beta (float) – Exponent for the
(b-x)factor. Must be> -1.weight_type (int) – One of
1,2,3,4. Defaults to1.abs_tol (float) – Absolute error tolerance. Defaults to
1.49e-8.max_fev (int) – Maximum number of function evaluations. Defaults to
500.
- Returns:
Integral estimate with error bound and diagnostics.
- Return type:
- Raises:
TypeError – If
fis not callable.ValueError – If
weight_typeis not in{1, 2, 3, 4},alpha <= -1,beta <= -1,abs_tolnot positive, or limits are non-finite.
- integrationdifferentiation.integrate_cauchy_principal_value(f, a, b, c, abs_tol=1.49e-08, max_fev=500)¶
Adaptive Cauchy principal value integration (IMSL QDAWC).
Computes the Cauchy principal value of
∫_a^b f(x)/(x-c) dxviascipy.integrate.quad()withweight='cauchy'.- Parameters:
f (Callable) – Numerator function:
f(x) -> float. The integrand isf(x) / (x - c).a (float) – Lower integration limit (finite).
b (float) – Upper integration limit (finite), must satisfy
b > a.c (float) – Cauchy singularity point; must satisfy
a < c < b.abs_tol (float) – Absolute error tolerance. Defaults to
1.49e-8.max_fev (int) – Maximum number of function evaluations. Defaults to
500.
- Returns:
Principal-value integral estimate with error bound.
- Return type:
- Raises:
TypeError – If
fis not callable.ValueError – If
cis not strictly inside(a, b),abs_tolis not positive, or limits are non-finite.
- integrationdifferentiation.integrate_nonadaptive(f, a, b, n=10)¶
Non-adaptive quadrature using a Gaussian rule (IMSL QDNG).
Uses fixed-order Gauss-Legendre quadrature via
scipy.integrate.fixed_quad(). Suitable for smooth integrands where the number of evaluation points is known in advance.- Parameters:
f (Callable) – Integrand:
f(x) -> float.a (float) – Lower integration limit (finite).
b (float) – Upper integration limit (finite).
n (int) – Number of Gaussian quadrature points. Defaults to
10.
- Returns:
- Integral estimate. Error is set to
0.0since no error estimate is available from the fixed rule.
- Integral estimate. Error is set to
- Return type:
- Raises:
TypeError – If
fis not callable.ValueError – If
n < 1or limits are non-finite.
- integrationdifferentiation.integrate_2d(f, ax, bx, g, h, abs_tol=1.49e-08)¶
2D iterated integral over a variable-limit domain (IMSL TWODQ).
Computes:
∫_{ax}^{bx} ∫_{g(x)}^{h(x)} f(y, x) dy dxvia
scipy.integrate.dblquad().- Parameters:
f (Callable) – Integrand
f(y, x) -> float. Note the argument order: inner variable first.ax (float) – Lower limit for the outer integral in
x.bx (float) – Upper limit for the outer integral in
x.g (Callable) – Lower limit function for the inner integral:
g(x) -> float.h (Callable) – Upper limit function for the inner integral:
h(x) -> float.abs_tol (float) – Absolute error tolerance. Defaults to
1.49e-8.
- Returns:
Integral estimate with error bound.
- Return type:
- Raises:
TypeError – If
f,g, orhis not callable.ValueError – If
abs_tolis not positive.
- integrationdifferentiation.integrate_2d_singular(f, ax, bx, ay, by, abs_tol=1.49e-08)¶
2D quadrature over a rectangular domain with possible singularity (IMSL QDAG2D).
Computes
∫_{ax}^{bx} ∫_{ay}^{by} f(y, x) dy dxviascipy.integrate.dblquad()with constant inner limits.- Parameters:
f (Callable) – Integrand
f(y, x) -> float.ax (float) – Lower outer limit in
x.bx (float) – Upper outer limit in
x.ay (float) – Lower inner limit in
y.by (float) – Upper inner limit in
y.abs_tol (float) – Absolute error tolerance. Defaults to
1.49e-8.
- Returns:
Integral estimate with error bound.
- Return type:
- Raises:
TypeError – If
fis not callable.ValueError – If
abs_tolis not positive.
- integrationdifferentiation.integrate_3d_singular(f, ax, bx, ay, by, az, bz, abs_tol=1.49e-08)¶
3D quadrature over a rectangular domain with possible singularity (IMSL QDAG3D).
Computes
∫_{ax}^{bx} ∫_{ay}^{by} ∫_{az}^{bz} f(z, y, x) dz dy dxviascipy.integrate.tplquad().- Parameters:
f (Callable) – Integrand
f(z, y, x) -> float.ax (float) – Lower outer limit in
x.bx (float) – Upper outer limit in
x.ay (float) – Lower limit in
y.by (float) – Upper limit in
y.az (float) – Lower inner limit in
z.bz (float) – Upper inner limit in
z.abs_tol (float) – Absolute error tolerance. Defaults to
1.49e-8.
- Returns:
Integral estimate with error bound.
- Return type:
- Raises:
TypeError – If
fis not callable.ValueError – If
abs_tolis not positive.
- integrationdifferentiation.integrate_nd(f, ranges, abs_tol=1.49e-08)¶
N-dimensional quadrature over a hyperrectangle (IMSL QAND).
Computes the integral of
f(x_1, ..., x_d)over the product domain∏_i [ranges[i][0], ranges[i][1]]usingscipy.integrate.nquad().- Parameters:
f (Callable) – Integrand:
f(*args) -> floatwhereargshaslen(ranges)elements.ranges (list[tuple[float, float]]) – Integration bounds for each dimension as
(lower, upper)pairs. Innermost dimension first.abs_tol (float) – Absolute error tolerance. Defaults to
1.49e-8.
- Returns:
Integral estimate with error bound.
- Return type:
- Raises:
TypeError – If
fis not callable.ValueError – If
rangesis empty orabs_tolis not positive.
- integrationdifferentiation.integrate_monte_carlo(f, ranges, n_points=10000, seed=None)¶
Quasi-Monte Carlo integration over a hyperrectangle (IMSL QMC).
Uses a Halton quasi-random sequence via
scipy.integrate.qmc_quad()for low-discrepancy sampling, which typically converges faster than plain Monte Carlo for smooth integrands.- Parameters:
f (Callable) – Integrand that accepts a 1D array of length
len(ranges)and returns a scalar, i.e.f(x) -> floatwherex.shape == (d,).ranges (list[tuple[float, float]]) – Integration bounds for each dimension as
(lower, upper)pairs.n_points (int) – Number of quasi-random sample points per estimate. Defaults to
10000.seed (int | None) – Optional random seed for reproducibility. Defaults to
None.
- Returns:
Integral estimate with standard error bound.
- Return type:
- Raises:
TypeError – If
fis not callable.ValueError – If
rangesis empty orn_points < 1.
- integrationdifferentiation.gauss_quadrature_rule(n, weight_type='legendre')¶
Gauss quadrature rule for classical weight functions (IMSL GQRUL).
Returns the
n-point Gaussian quadrature abscissas and weights for the specified classical weight function usingscipy.specialroot-finding routines.Supported weight types:
'legendre'— Gauss-Legendre on[-1, 1], weight1'chebyshev'— Gauss-Chebyshev (Type 1) on[-1, 1], weight(1-x²)^(-1/2)'laguerre'— Gauss-Laguerre on[0, ∞), weightexp(-x)'hermite'— Gauss-Hermite on(-∞, ∞), weightexp(-x²)'jacobi'— Gauss-Jacobi on[-1, 1]with parametersalpha=0, beta=0(equivalent to Legendre)
- Parameters:
n (int) – Number of quadrature points. Must be
>= 1.weight_type (str) – Classical weight function identifier. Defaults to
'legendre'.
- Returns:
- Rule with
pointssorted in ascending order and corresponding
weights.
- Rule with
- Return type:
- Raises:
ValueError – If
n < 1orweight_typeis not one of the supported strings.
- integrationdifferentiation.gauss_quadrature_from_recurrence(alpha, beta)¶
Gauss quadrature rule from three-term recurrence coefficients (IMSL GQRCF).
Constructs the quadrature rule using the Golub–Welsch algorithm: builds the symmetric tridiagonal Jacobi matrix from the recurrence coefficients, computes its eigendecomposition via
numpy.linalg.eigh(), and derives the weights from the first components of the eigenvectors.The recurrence relation is:
p_{k+1}(x) = (x - alpha_k) p_k(x) - beta_k p_{k-1}(x)with
p_{-1} = 0,p_0 = 1, andbeta_0 = mu_0(total weight integral).- Parameters:
alpha (np.ndarray) – Diagonal recurrence coefficients, shape
(n,).beta (np.ndarray) – Off-diagonal recurrence coefficients, shape
(n,).beta[0]is the total weightmu_0;beta[k]fork >= 1satisfiesbeta[k] = ||p_k||^2 / ||p_{k-1}||^2.
- Returns:
n-point quadrature rule with nodes and weights.- Return type:
- Raises:
ValueError – If
alphaandbetado not have the same length, or ifn < 1.
- integrationdifferentiation.recurrence_coefficients_classical(n, weight_type='legendre', a=0.0, b=1.0)¶
Three-term recurrence coefficients for classical weight functions (IMSL RECCF).
Returns arrays
(alpha, beta)of lengthnfor the recurrencep_{k+1}(x) = (x - alpha_k) p_k(x) - beta_k p_{k-1}(x)Supported weight types:
'legendre'— on[-1, 1];a,bunused'chebyshev'— Chebyshev T on[-1, 1];a,bunused'laguerre'— on[0, ∞);aunused,bis the shape parameter (defaultb=1→ standard Laguerre)'hermite'— on(-∞, ∞);a,bunused'jacobi'— on[-1, 1];a= Jacobialpha,b= Jacobibeta
- Parameters:
n (int) – Number of recurrence coefficients. Must be
>= 1.weight_type (str) – Classical weight function. Defaults to
'legendre'.a (float) – Parameter
a(Jacobi alpha or Laguerre shape). Defaults to0.0.b (float) – Parameter
b(Jacobi beta). Defaults to1.0.
- Returns:
(alpha, beta)arrays of lengthn.beta[0]equals the total integral of the weight functionmu_0;beta[k]fork >= 1equals||p_k||^2/||p_{k-1}||^2.
- Return type:
tuple[np.ndarray, np.ndarray]
- Raises:
ValueError – If
n < 1orweight_typeis not supported.
- integrationdifferentiation.recurrence_from_quadrature(points, weights)¶
Recurrence coefficients from a discrete quadrature rule (IMSL RECQR).
Uses the Stieltjes–Lanczos three-term recurrence procedure to compute
(alpha, beta)from the givenn-point quadrature rule. The computed coefficients satisfy:p_{k+1}(x) = (x - alpha_k) p_k(x) - beta_k p_{k-1}(x)with
beta[0] = sum(weights)(total massmu_0).- Parameters:
points (np.ndarray) – Quadrature abscissas, shape
(n,).weights (np.ndarray) – Corresponding positive quadrature weights, shape
(n,).
- Returns:
(alpha, beta)each of lengthn.- Return type:
tuple[np.ndarray, np.ndarray]
- Raises:
ValueError – If
pointsandweightshave different lengths or are empty, or if any weight is non-positive.
- integrationdifferentiation.fejer_quadrature_rule(n)¶
Fejer Type-1 quadrature rule (IMSL FQRUL).
Computes the
n-point Fejér Type-1 rule on[-1, 1]with nodes atx_k = cos((2k - 1) * π / (2n)), k = 1, ..., n
and weights computed via the trigonometric sum formula. All weights are positive and the rule integrates polynomials of degree up to
n - 1exactly.- Parameters:
n (int) – Number of quadrature points. Must be
>= 1.- Returns:
Rule with nodes and weights in ascending order.
- Return type:
- Raises:
ValueError – If
n < 1.
- integrationdifferentiation.differentiate(f, x, order=1, h=None)¶
Approximate derivative using Richardson-extrapolated central differences (IMSL DERIV).
Computes the 1st, 2nd, or 3rd derivative of
fat the pointxusing central-difference formulas combined with Richardson extrapolation to achieve \(O(h^4)\) accuracy.Formulas:
order=1:D1(h) = (f(x+h) - f(x-h)) / (2h); Richardson:(4*D1(h/2) - D1(h)) / 3order=2:D2(h) = (f(x+h) - 2f(x) + f(x-h)) / h²; Richardson:(4*D2(h/2) - D2(h)) / 3order=3:D3(h) = (f(x+2h) - 2f(x+h) + 2f(x-h) - f(x-2h)) / (2h³); Richardson:(4*D3(h/2) - D3(h)) / 3
- Parameters:
f (Callable) – Function to differentiate:
f(x) -> float.x (float) – Point at which to evaluate the derivative.
order (int) – Derivative order:
1,2, or3. Defaults to1.h (float | None) – Step size. If
None, an adaptive stepmax(abs(x), 1.0) * 1e-4is used. Defaults toNone.
- Returns:
Approximation to the
order-th derivative offatx.- Return type:
float
- Raises:
TypeError – If
fis not callable.ValueError – If
orderis not1,2, or3, or ifhis provided but is not positive.
- integrationdifferentiation.launch_documentation(opener=<function open>, package_dir=None)¶
Launch package documentation in a web browser.
The launcher opens local HTML documentation only.
- Parameters:
opener (Callable[[str], bool]) – Browser opener function. Defaults to
webbrowser.open.package_dir (Path | None) – Optional package directory override used for tests. Defaults to the directory of this module.
- Returns:
The
file://...URI opened.- Return type:
str
- Raises:
FileNotFoundError – If no local documentation index can be located.