Sympy 简明教程

SymPy - Integration

SymPy 程序包包含 integrals 模块。它实现计算表达式的定积分和不定积分的方法。integrate() 方法用于计算定积分和不定积分。要计算不定积分或原始积分,只须在表达式后面传递变量。

The SymPy package contains integrals module. It implements methods to calculate definite and indefinite integrals of expressions. The integrate() method is used to compute both definite and indefinite integrals. To compute an indefinite or primitive integral, just pass the variable after the expression.

例如 -

For example −

integrate(f, x)

若要计算定积分,请按如下所示传递参数 -

To compute a definite integral, pass the argument as follows −

(integration_variable, lower_limit, upper_limit)
>>> from sympy import *
>>> x,y = symbols('x y')
>>> expr=x**2 + x + 1
>>> integrate(expr, x)

上面的代码片段给出的输出等同于以下表达式 −

The above code snippet gives an output equivalent to the below expression −

$\frac{x^3}{3} + \frac{x^2}{2} + x$

$\frac{x^3}{3} + \frac{x^2}{2} + x$

>>> expr=sin(x)*tan(x)
>>> expr
>>> integrate(expr,x)

上面的代码片段给出的输出等同于以下表达式 −

The above code snippet gives an output equivalent to the below expression −

$-\frac{\log(\sin(x) - 1)}{2} + \frac{\log(\sin(x) + 1)}{2} - \sin(x)$

$-\frac{\log(\sin(x) - 1)}{2} + \frac{\log(\sin(x) + 1)}{2} - \sin(x)$

以下是定积分的示例 -

The example of definite integral is given below −

>>> expr=exp(-x**2)
>>> integrate(expr,(x,0,oo) )

上面的代码片段给出的输出等同于以下表达式 −

The above code snippet gives an output equivalent to the below expression −

$\frac{\sqrt\pi}{2}$

$\frac{\sqrt\pi}{2}$

你可以传递多个限制元组来执行定积分。以下是示例 -

You can pass multiple limit tuples to perform a multiple integral. An example is given below −

>>> expr=exp(-x**2 - y**2)
>>> integrate(expr,(x,0,oo),(y,0,oo))

上面的代码片段给出的输出等同于以下表达式 −

The above code snippet gives an output equivalent to the below expression −

$\frac{\pi}{4}$

$\frac{\pi}{4}$

你可以使用 Integral 对象创建未计算的积分,而 Integral 对象可以通过调用 doit() 方法来计算。

You can create unevaluated integral using Integral object, which can be evaluated by calling doit() method.

>>> expr = Integral(log(x)**2, x)
>>> expr

上面的代码片段给出的输出等同于以下表达式 −

The above code snippet gives an output equivalent to the below expression −

$\int \mathrm\log(x)^2 \mathrm{d}x$

$\int \mathrm\log(x)^2 \mathrm{d}x$

>>> expr.doit()

上面的代码片段给出的输出等同于以下表达式 −

The above code snippet gives an output equivalent to the below expression −

$x\log(x)^2 - 2xlog(x) + 2x$

$x\log(x)^2 - 2xlog(x) + 2x$

Integral Transforms

SymPy 支持各种类型的积分变换,如下所示 -

SymPy supports various types of integral transforms as follows −

  1. laplace_transform

  2. fourier_transform

  3. sine_transform

  4. cosine_transform

  5. hankel_transform

这些函数在 sympy.integrals.transforms 模块中定义。下列示例分别为计算傅里叶变换和拉普拉斯变换。

These functions are defined in sympy.integrals.transforms module. Following examples compute Fourier transform and Laplace transform respectively.

Example 1

Example 1

>>> from sympy import fourier_transform, exp
>>> from sympy.abc import x, k
>>> expr=exp(-x**2)
>>> fourier_transform(expr, x, k)

在 Python shell 中执行上面的命令后,将生成以下输出 -

On executing the above command in python shell, following output will be generated −

sqrt(pi)*exp(-pi**2*k**2)

等效于 -

Which is equivalent to −

$\sqrt\pi * e{\pi2k^2}$

$\sqrt\pi * e{\pi2k^2}$

Example 2

Example 2

>>> from sympy.integrals import laplace_transform
>>> from sympy.abc import t, s, a
>>> laplace_transform(t**a, t, s)

在 Python shell 中执行上面的命令后,将生成以下输出 -

On executing the above command in python shell, following output will be generated −

(s**(-a)*gamma(a + 1)/s, 0, re(a) > -1)