Integration

SciPy is capable of performing very robust numerical integration. Definite integrals of a set of special functions are evaluated accurately with routines in the scipy.special module. For other functions, there are several different algorithms to obtain reliable approximations in the scipy.integrate module.

Exponential/logarithm integrals

A summary of the indefinite and definite integrals in the category of exponential/logarithm is presented here: the exponential integrals (expn, expi, and exp1), Dawson's integral (dawsn), and Gauss error functions (erf and erfc). We also have Spence's dilogarithm (also known as Spence's integral). Let's have a look at the following formulas:

Exponential/logarithm integrals

Trigonometric and hyperbolic trigonometric integrals

In the category of trigonometric and hyperbolic trigonometric integrals, we have Fresnel sine and cosine integrals, as well as the sinc and hyperbolic trigonometric integrals. Let's have a look at the following formulas:

Trigonometric and hyperbolic trigonometric integrals

In the definitions given in the preceding list of integrals, the gamma symbol denotes the Euler-Mascheroni constant:

Trigonometric and hyperbolic trigonometric integrals

Elliptic integrals

Elliptic integrals arise naturally when computing the arc length of ellipses. SciPy follows the argument notation for elliptic integrals: complete (one argument) and incomplete (two arguments). Let's have a look at the following formulas:

Elliptic integrals

Gamma and beta integrals

In the category of gamma and beta integrals, we have one incomplete gamma function, one complemented incomplete gamma integral, and one incomplete beta integral. These are some of the most useful functions in this category. Let's have a look at the following formulas:

Gamma and beta integrals

Numerical integration

For any other functions, we are content with approximating definite integrals with quadrature formulae, such as quad (adaptive quadrature), fixed_quad (fixed-order Gaussian quadrature), quadrature (fixed-tolerance Gaussian quadrature), and romberg, (Romberg integration). For functions with more than one variable, we have dbquad (double integral) and tplquad (triple integral) methods. The syntax in all cases is a variation of quad:

quad(func, a, b, args=(), full_output=0, epsabs=1.49e-08, epsrel=1.49e-08, limit=50, points=None, weight=None, wvar=None, wopts=None, maxp1=50, limlst=50)

If we have samples instead of functions, we may use routines such as trapz, cumtrapz (composite trapezoidal rule and its cumulative version), romb (Romberg integration again), and simps (Simpson's rule) instead. In these routines, the syntax is simpler and changes the order of the parameters. For example, this is how we call simps:

>>> simps(y, x=None, dx=1, axis=-1, even='avg')

Those of us familiar with the QUADPACK libraries will find similar syntax, usage, and performance.

For extra information, run the scipy.integrate.quad_explain() command. In the IPython Notebook for this chapter, the alternative help command, scipy.integrate.quad, is executed and its output is displayed in the corresponding section. This explains with great detail all the different outputs of the quadrature integrals included in the module result, the estimation of absolute error and convergence, and explanation of the used weightings, if necessary. Let's give at least one meaningful example where we integrate a special function and compare the output of a quadrature formula against the more accurate value of the routines given in scipy.special:

>>> f=lambda t: numpy.exp(-t)*t**4
>>> from scipy.special import gammainc
>>> from scipy.integrate import quad
>>> from scipy.misc import factorial
>>> print (gammainc(5,1))

The output is as follows:

0.00365984682734

Let's take a look at following print command:

print('%.19f' % gammainc(5,1))

The output is as follows:

0.0036598468273437131

Let's look further into the code:

>>> import numpy
>>> result,error=quad(f,0,1)/factorial(4)
>>> result

The output is as follows:

0.0036598468273437122

To use a routine that integrates from samples, we have the flexibility of assigning the frequency and length of the data. For the following problem, we could try with 10,000 samples in the same interval:

>>> import numpy 
>>> import scipy.integrate
>>> x=numpy.linspace(0,1,10000)
>>> scipy.integrate.simps(f(x)/factorial(4), x)

The output is as follows:

0.0036598468273469071
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.26.221