Scipy 简明教程

SciPy - Integrate

当无法分析积分一个函数或者很难分析积分一个函数时,通常会使用数值积分法。SciPy 有很多例程可执行数值积分。其中大多数都可以在相同的 scipy.integrate 库中找到。下表列出了一些常用的函数。

When a function cannot be integrated analytically, or is very difficult to integrate analytically, one generally turns to numerical integration methods. SciPy has a number of routines for performing numerical integration. Most of them are found in the same scipy.integrate library. The following table lists some commonly used functions.

Sr No.

Function & Description

1

quad Single integration

2

dblquad Double integration

3

tplquad Triple integration

4

nquad n-fold multiple integration

5

fixed_quad Gaussian quadrature, order n

6

quadrature Gaussian quadrature to tolerance

7

romberg Romberg integration

8

trapz Trapezoidal rule

9

cumtrapz Trapezoidal rule to cumulatively compute integral

10

simps Simpson’s rule

11

romb Romberg integration

12

polyint Analytical polynomial integration (NumPy)

13

poly1d Helper function for polyint (NumPy)

Single Integrals

Quad 函数是 SciPy 集成函数的核心。数值积分有时被称为 quadrature ,因此得名。它通常是针对给定范围 a 到 b 上函数 f(x) 的单积分执行的默认选择。

The Quad function is the workhorse of SciPy’s integration functions. Numerical integration is sometimes called quadrature, hence the name. It is normally the default choice for performing single integrals of a function f(x) over a given fixed range from a to b.

\int_{a}^{b} f(x)dx

quad 的通用形式为,其中,‘f’是被积函数的名称。而 ‘a’ 和 ‘b’ 分别是下限和上限。让我们看一下一个范围在 0 和 1 之间的 Gaussian 函数的示例。

The general form of quad is scipy.integrate.quad(f, a, b), Where ‘f’ is the name of the function to be integrated. Whereas, ‘a’ and ‘b’ are the lower and upper limits, respectively. Let us see an example of the Gaussian function, integrated over a range of 0 and 1.

我们首先需要定义函数 → $f(x) = e {-x 2}$ ,这可以使用 lambda 表达式完成,然后在该函数上调用 quad 方法。

We first need to define the function → $f(x) = e{-x2}$ , this can be done using a lambda expression and then call the quad method on that function.

import scipy.integrate
from numpy import exp
f= lambda x:exp(-x**2)
i = scipy.integrate.quad(f, 0, 1)
print i

上述程序将生成以下输出。

The above program will generate the following output.

(0.7468241328124271, 8.291413475940725e-15)

quad 函数返回两个值,其中第一个数字是积分值,第二个数字是积分值绝对误差的估计。

The quad function returns the two values, in which the first number is the value of integral and the second value is the estimate of the absolute error in the value of integral.

Note − 由于 quad 需要函数作为第一个参数,因此我们不能直接传递 exp 作为参数。Quad 函数接受正无穷大和负无穷大作为极限。Quad 函数可以对单变量标准预定义 NumPy 函数(如 exp、sin 和 cos)进行积分。

Note − Since quad requires the function as the first argument, we cannot directly pass exp as the argument. The Quad function accepts positive and negative infinity as limits. The Quad function can integrate standard predefined NumPy functions of a single variable, such as exp, sin and cos.

Multiple Integrals

双重和三重集成的机制已封装到函数 dblquad, tplquadnquad 中。这些函数分别集成四个或六个参数。所有内部积分的极限需要定义为函数。

The mechanics for double and triple integration have been wrapped up into the functions dblquad, tplquad and nquad. These functions integrate four or six arguments, respectively. The limits of all inner integrals need to be defined as functions.

Double Integrals

dblquad 的通用形式为 scipy.integrate.dblquad(func, a, b, gfun, hfun)。其中,func 是被积函数的名称,‘a’ 和 ‘b’ 分别是 x 变量的下限和上限,而 gfun 和 hfun 是定义 y 变量的下限和上限的函数的名称。

The general form of dblquad is scipy.integrate.dblquad(func, a, b, gfun, hfun). Where, func is the name of the function to be integrated, ‘a’ and ‘b’ are the lower and upper limits of the x variable, respectively, while gfun and hfun are the names of the functions that define the lower and upper limits of the y variable.

举个例子,让我们执行二重积分法。

As an example, let us perform the double integral method.

\int_{0}^{1/2} dy \int_{0} {\sqrt{1-4y 2}} 16xy \:dx

\int_{0}^{1/2} dy \int_{0}{\sqrt{1-4y2}} 16xy \:dx

我们使用 lambda 表达式定义函数 f、g 和 h。请注意,即使 g 和 h 是常数(在很多情况下它们可能都是),也必须将它们定义为函数,就像我们在此处为下限所做的那样。

We define the functions f, g, and h, using the lambda expressions. Note that even if g and h are constants, as they may be in many cases, they must be defined as functions, as we have done here for the lower limit.

import scipy.integrate
from numpy import exp
from math import sqrt
f = lambda x, y : 16*x*y
g = lambda x : 0
h = lambda y : sqrt(1-4*y**2)
i = scipy.integrate.dblquad(f, 0, 0.5, g, h)
print i

上述程序将生成以下输出。

The above program will generate the following output.

(0.5, 1.7092350012594845e-14)

除了上面描述的例程之外,scipy.integrate 还有许多其他积分例程,包括执行 n 倍多重积分的 nquad,以及实现各种积分算法的其他例程。但是,对于我们的数值积分需求,quad 和 dblquad 将满足其中大部分需求。

In addition to the routines described above, scipy.integrate has a number of other integration routines, including nquad, which performs n-fold multiple integration, as well as other routines that implement various integration algorithms. However, quad and dblquad will meet most of our needs for numerical integration.