integrate.quadrature

Module Contents

Classes

AccuracyWarning()

Functions

_cached_roots_legendre(n) Cache roots_legendre results to speed up calls of the fixed_quad
fixed_quad(func,a,b,args=tuple,n=5) Compute a definite integral using fixed-order Gaussian quadrature.
vectorize1(func,args=tuple,vec_func=False) Vectorize the call to a function.
quadrature(func,a,b,args=tuple,tol=1.49e-08,rtol=1.49e-08,maxiter=50,vec_func=True,miniter=1) Compute a definite integral using fixed-tolerance Gaussian quadrature.
tupleset(t,i,value)
cumtrapz(y,x=None,dx=1.0,axis=None,initial=None) Cumulatively integrate y(x) using the composite trapezoidal rule.
_basic_simps(y,start,stop,x,dx,axis)
simps(y,x=None,dx=1,axis=None,even=”avg”) Integrate y(x) using samples along the given axis and the composite
romb(y,dx=1.0,axis=None,show=False) Romberg integration using samples of a function.
_difftrap(function,interval,numtraps) Perform part of the trapezoidal rule to integrate a function.
_romberg_diff(b,c,k) Compute the differences for the Romberg quadrature corrections.
_printresmat(function,interval,resmat)
romberg(function,a,b,args=tuple,tol=1.48e-08,rtol=1.48e-08,show=False,divmax=10,vec_func=False) Romberg integration of a callable function or method.
newton_cotes(rn,equal=0) Return weights and error coefficient for Newton-Cotes integration.
class AccuracyWarning
_cached_roots_legendre(n)

Cache roots_legendre results to speed up calls of the fixed_quad function.

fixed_quad(func, a, b, args=tuple, n=5)

Compute a definite integral using fixed-order Gaussian quadrature.

Integrate func from a to b using Gaussian quadrature of order n.

func : callable
A Python function or method to integrate (must accept vector inputs). If integrating a vector-valued function, the returned array must have shape (..., len(x)).
a : float
Lower limit of integration.
b : float
Upper limit of integration.
args : tuple, optional
Extra arguments to pass to function, if any.
n : int, optional
Order of quadrature integration. Default is 5.
val : float
Gaussian quadrature approximation to the integral
none : None
Statically returned value of None

quad : adaptive quadrature using QUADPACK dblquad : double integrals tplquad : triple integrals romberg : adaptive Romberg quadrature quadrature : adaptive Gaussian quadrature romb : integrators for sampled data simps : integrators for sampled data cumtrapz : cumulative integration for sampled data ode : ODE integrator odeint : ODE integrator

vectorize1(func, args=tuple, vec_func=False)

Vectorize the call to a function.

This is an internal utility function used by romberg and quadrature to create a vectorized version of a function.

If vec_func is True, the function func is assumed to take vector arguments.

func : callable
User defined function.
args : tuple, optional
Extra arguments for the function.
vec_func : bool, optional
True if the function func takes vector arguments.
vfunc : callable
A function that will take a vector argument and return the result.
quadrature(func, a, b, args=tuple, tol=1.49e-08, rtol=1.49e-08, maxiter=50, vec_func=True, miniter=1)

Compute a definite integral using fixed-tolerance Gaussian quadrature.

Integrate func from a to b using Gaussian quadrature with absolute tolerance tol.

func : function
A Python function or method to integrate.
a : float
Lower limit of integration.
b : float
Upper limit of integration.
args : tuple, optional
Extra arguments to pass to function.
tol, rtol : float, optional
Iteration stops when error between last two iterates is less than tol OR the relative change is less than rtol.
maxiter : int, optional
Maximum order of Gaussian quadrature.
vec_func : bool, optional
True or False if func handles arrays as arguments (is a “vector” function). Default is True.
miniter : int, optional
Minimum order of Gaussian quadrature.
val : float
Gaussian quadrature approximation (within tolerance) to integral.
err : float
Difference between last two estimates of the integral.

romberg: adaptive Romberg quadrature fixed_quad: fixed-order Gaussian quadrature quad: adaptive quadrature using QUADPACK dblquad: double integrals tplquad: triple integrals romb: integrator for sampled data simps: integrator for sampled data cumtrapz: cumulative integration for sampled data ode: ODE integrator odeint: ODE integrator

tupleset(t, i, value)
cumtrapz(y, x=None, dx=1.0, axis=None, initial=None)

Cumulatively integrate y(x) using the composite trapezoidal rule.

y : array_like
Values to integrate.
x : array_like, optional
The coordinate to integrate along. If None (default), use spacing dx between consecutive elements in y.
dx : float, optional
Spacing between elements of y. Only used if x is None.
axis : int, optional
Specifies the axis to cumulate. Default is -1 (last axis).
initial : scalar, optional
If given, uses this value as the first value in the returned result. Typically this value should be 0. Default is None, which means no value at x[0] is returned and res has one element less than y along the axis of integration.
res : ndarray
The result of cumulative integration of y along axis. If initial is None, the shape is such that the axis of integration has one less value than y. If initial is given, the shape is equal to that of y.

numpy.cumsum, numpy.cumprod quad: adaptive quadrature using QUADPACK romberg: adaptive Romberg quadrature quadrature: adaptive Gaussian quadrature fixed_quad: fixed-order Gaussian quadrature dblquad: double integrals tplquad: triple integrals romb: integrators for sampled data ode: ODE integrators odeint: ODE integrators

>>> from scipy import integrate
>>> import matplotlib.pyplot as plt
>>> x = np.linspace(-2, 2, num=20)
>>> y = x
>>> y_int = integrate.cumtrapz(y, x, initial=0)
>>> plt.plot(x, y_int, 'ro', x, y[0] + 0.5 * x**2, 'b-')
>>> plt.show()
_basic_simps(y, start, stop, x, dx, axis)
simps(y, x=None, dx=1, axis=None, even="avg")

Integrate y(x) using samples along the given axis and the composite Simpson’s rule. If x is None, spacing of dx is assumed.

If there are an even number of samples, N, then there are an odd number of intervals (N-1), but Simpson’s rule requires an even number of intervals. The parameter ‘even’ controls how this is handled.

y : array_like
Array to be integrated.
x : array_like, optional
If given, the points at which y is sampled.
dx : int, optional
Spacing of integration points along axis of y. Only used when x is None. Default is 1.
axis : int, optional
Axis along which to integrate. Default is the last axis.
even : str {‘avg’, ‘first’, ‘last’}, optional
‘avg’ : Average two results:1) use the first N-2 intervals with
a trapezoidal rule on the last interval and 2) use the last N-2 intervals with a trapezoidal rule on the first interval.
‘first’ : Use Simpson’s rule for the first N-2 intervals with
a trapezoidal rule on the last interval.
‘last’ : Use Simpson’s rule for the last N-2 intervals with a
trapezoidal rule on the first interval.

quad: adaptive quadrature using QUADPACK romberg: adaptive Romberg quadrature quadrature: adaptive Gaussian quadrature fixed_quad: fixed-order Gaussian quadrature dblquad: double integrals tplquad: triple integrals romb: integrators for sampled data cumtrapz: cumulative integration for sampled data ode: ODE integrators odeint: ODE integrators

For an odd number of samples that are equally spaced the result is exact if the function is a polynomial of order 3 or less. If the samples are not equally spaced, then the result is exact only if the function is a polynomial of order 2 or less.

romb(y, dx=1.0, axis=None, show=False)

Romberg integration using samples of a function.

y : array_like
A vector of 2**k + 1 equally-spaced samples of a function.
dx : float, optional
The sample spacing. Default is 1.
axis : int, optional
The axis along which to integrate. Default is -1 (last axis).
show : bool, optional
When y is a single 1-D array, then if this argument is True print the table showing Richardson extrapolation from the samples. Default is False.
romb : ndarray
The integrated result for axis.

quad : adaptive quadrature using QUADPACK romberg : adaptive Romberg quadrature quadrature : adaptive Gaussian quadrature fixed_quad : fixed-order Gaussian quadrature dblquad : double integrals tplquad : triple integrals simps : integrators for sampled data cumtrapz : cumulative integration for sampled data ode : ODE integrators odeint : ODE integrators

_difftrap(function, interval, numtraps)

Perform part of the trapezoidal rule to integrate a function. Assume that we had called difftrap with all lower powers-of-2 starting with 1. Calling difftrap only returns the summation of the new ordinates. It does _not_ multiply by the width of the trapezoids. This must be performed by the caller.

‘function’ is the function to evaluate (must accept vector arguments). ‘interval’ is a sequence with lower and upper limits

of integration.
‘numtraps’ is the number of trapezoids to use (must be a
power-of-2).
_romberg_diff(b, c, k)

Compute the differences for the Romberg quadrature corrections. See Forman Acton’s “Real Computing Made Real,” p 143.

_printresmat(function, interval, resmat)
romberg(function, a, b, args=tuple, tol=1.48e-08, rtol=1.48e-08, show=False, divmax=10, vec_func=False)

Romberg integration of a callable function or method.

Returns the integral of function (a function of one variable) over the interval (a, b).

If show is 1, the triangular array of the intermediate results will be printed. If vec_func is True (default is False), then function is assumed to support vector arguments.

function : callable
Function to be integrated.
a : float
Lower limit of integration.
b : float
Upper limit of integration.
results : float
Result of the integration.
args : tuple, optional
Extra arguments to pass to function. Each element of args will be passed as a single argument to func. Default is to pass no extra arguments.
tol, rtol : float, optional
The desired absolute and relative tolerances. Defaults are 1.48e-8.
show : bool, optional
Whether to print the results. Default is False.
divmax : int, optional
Maximum order of extrapolation. Default is 10.
vec_func : bool, optional
Whether func handles arrays as arguments (i.e whether it is a “vector” function). Default is False.

fixed_quad : Fixed-order Gaussian quadrature. quad : Adaptive quadrature using QUADPACK. dblquad : Double integrals. tplquad : Triple integrals. romb : Integrators for sampled data. simps : Integrators for sampled data. cumtrapz : Cumulative integration for sampled data. ode : ODE integrator. odeint : ODE integrator.

[1]‘Romberg’s method’ http://en.wikipedia.org/wiki/Romberg%27s_method

Integrate a gaussian from 0 to 1 and compare to the error function.

>>> from scipy import integrate
>>> from scipy.special import erf
>>> gaussian = lambda x: 1/np.sqrt(np.pi) * np.exp(-x**2)
>>> result = integrate.romberg(gaussian, 0, 1, show=True)
Romberg integration of <function vfunc at ...> from [0, 1]
Steps  StepSize  Results
    1  1.000000  0.385872
    2  0.500000  0.412631  0.421551
    4  0.250000  0.419184  0.421368  0.421356
    8  0.125000  0.420810  0.421352  0.421350  0.421350
   16  0.062500  0.421215  0.421350  0.421350  0.421350  0.421350
   32  0.031250  0.421317  0.421350  0.421350  0.421350  0.421350  0.421350

The final result is 0.421350396475 after 33 function evaluations.

>>> print("%g %g" % (2*result, erf(1)))
0.842701 0.842701
newton_cotes(rn, equal=0)

Return weights and error coefficient for Newton-Cotes integration.

Suppose we have (N+1) samples of f at the positions x_0, x_1, …, x_N. Then an N-point Newton-Cotes formula for the integral between x_0 and x_N is:

where and is the average samples spacing.

If the samples are equally-spaced and N is even, then the error term is .

rn : int
The integer order for equally-spaced data or the relative positions of the samples with the first sample at 0 and the last at N, where N+1 is the length of rn. N is the order of the Newton-Cotes integration.
equal : int, optional
Set to 1 to enforce equally spaced data.
an : ndarray
1-D array of weights to apply to the function at the provided sample positions.
B : float
Error coefficient.

Normally, the Newton-Cotes rules are used on smaller integration regions and a composite rule is used to return the total integral.