Sympy 简明教程

SymPy - Simplification

Sympy 有强大的数学表达式简化能力。SymPy 中有许多函数可执行各种类型的化简。有一个称为 simplify() 的通用函数,它尝试到达表达式的最简单形式。

Sympy has powerful ability to simplify mathematical expressions. There are many functions in SymPy to perform various kinds of simplification. A general function called simplify() is there that attempts to arrive at the simplest form of an expression.

simplify

此函数在 sympy.simplify 模块中定义。simplify() 尝试应用智能启发式来使输入表达式“更简单”。以下代码显示了表达式 $sin 2(x)+cos 2(x)$ 的简化形式。

This function is defined in sympy.simplify module. simplify() tries to apply intelligent heuristics to make the input expression “simpler”. Following code shows simplifies expression $sin2(x)+cos2(x)$.

>>> from sympy import *
>>> x=Symbol('x')
>>> expr=sin(x)**2 + cos(x)**2
>>> simplify(expr)

上面的代码段给出了以下输出:

The above code snippet gives the following output −

1

1

expand

expand() 是 SymPy 中最常见的简化函数之一,用于展开多项式表达式。例如:

The expand() is one of the most common simplification functions in SymPy, used in expanding polynomial expressions. For example −

>>> a,b=symbols('a b')
>>> expand((a+b)**2)

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

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

$a^2 + 2ab + b^2$

$a^2 + 2ab + b^2$

>>> expand((a+b)*(a-b))

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

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

$a^2 - b^2$

$a^2 - b^2$

expand() 函数使表达式变大,而不是变小。通常情况下是这样,但expand() 经常会让表达式变小。

The expand() function makes expressions bigger, not smaller. Usually this is the case, but often an expression will become smaller upon calling expand() on it.

>>> expand((x + 1)*(x - 2) - (x - 1)*x)

上面的代码段给出了以下输出:

The above code snippet gives the following output −

-2

-2

factor

此函数采用一个多项式,并将其分解为有理数上的不可约因子。

This function takes a polynomial and factors it into irreducible factors over the rational numbers.

>>> x,y,z=symbols('x y z')
>>> expr=(x**2*z + 4*x*y*z + 4*y**2*z)
>>> factor(expr)

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

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

$z(x + 2y)^2$

$z(x + 2y)^2$

>>> factor(x**2+2*x+1)

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

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

$(x + 1)^2$

$(x + 1)^2$

factor() 函数是 expand() 的相反函数。 factor() 返回的每个因子都保证是不可约的。 factor_list() 函数会返还更具结构的输出。

The factor() function is the opposite of expand(). Each of the factors returned by factor() is guaranteed to be irreducible. The factor_list() function returns a more structured output.

>>> expr=(x**2*z + 4*x*y*z + 4*y**2*z)
>>> factor_list(expr)

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

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

(1, [(z, 1), (x + 2*y, 2)])

(1, [(z, 1), (x + 2*y, 2)])

collect

此函数收集表达式中的加法项,根据含有有理指数的表达式列表分项。

This function collects additve terms of an expression with respect to a list of expression up to powers with rational exponents.

>>> expr=x*y + x - 3 + 2*x**2 - z*x**2 + x**3
>>> expr

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

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

$x^3 + x^2z + 2x^2 + xy + x - 3$

$x^3 + x^2z + 2x^2 + xy + x - 3$

针对这个表达式,collect() 函数的结果如下 −

The collect() function on this expression results as follows −

>>> collect(expr,x)

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

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

$x^3 + x^2(2 - z) + x(y + 1) - 3$

$x^3 + x^2(2 - z) + x(y + 1) - 3$

>>> expr=y**2*x + 4*x*y*z + 4*y**2*z+y**3+2*x*y
>>> collect(expr,y)

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

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

$Y3+Y2(x+4z)+y(4xz+2x)$

$Y3+Y2(x+4z)+y(4xz+2x)$

cancel

cancel() 函数接受任意有理函数,并将其化成标准规范形式,p/q,其中 p 和 q 是没有公因子的展开多项式。p 和 q 的前导系数没有分母,即它们是整数。

The cancel() function will take any rational function and put it into the standard canonical form, p/q, where p and q are expanded polynomials with no common factors. The leading coefficients of p and q do not have denominators i.e., they are integers.

>>> expr1=x**2+2*x+1
>>> expr2=x+1
>>> cancel(expr1/expr2)

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

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

$x+1$

$x+1$

>>> expr = 1/x + (3*x/2 - 2)/(x - 4)
>>> expr

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

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

$\frac{\frac{3x}{2} - 2}{x - 4} + \frac{1}{x}$

$\frac{\frac{3x}{2} - 2}{x - 4} + \frac{1}{x}$

>>> cancel(expr)

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

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

$\frac{3x^2 - 2x - 8}{2x^2 - 8}$

$\frac{3x^2 - 2x - 8}{2x^2 - 8}$

>>> expr=1/sin(x)**2
>>> expr1=sin(x)
>>> cancel(expr1*expr)

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

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

$\frac{1}{\sin(x)}$

$\frac{1}{\sin(x)}$

trigsimp

此函数用于化简三角恒等式。需要注意的是,反三角函数的命名规则是将 a 附在函数名的前面。例如,反余弦或余弦弧称为 acos()。

This function is used to simplify trigonometric identities. It may be noted that naming conventions for inverse trigonometric functions is to append an a to the front of the function’s name. For example, the inverse cosine, or arc cosine, is called acos().

>>> from sympy import trigsimp, sin, cos
>>> from sympy.abc import x, y
>>> expr = 2*sin(x)**2 + 2*cos(x)**2
>>> trigsimp(expr)

2

2

trigsimp 函数使用启发式方法来应用最合适三角恒等式。

The trigsimp function uses heuristics to apply the best suitable trigonometric identity.

powersimp

此函数通过将具有类似底数和指数的幂结合起来,来化简给定的表达式。

This function reduces given expression by combining powers with similar bases and exponents.

>>> expr=x**y*x**z*y**z
>>> expr

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

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

$x^y x^z y^z$

$x^y x^z y^z$

>>> powsimp(expr)

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

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

$x^{y+z} y^z$

$x^{y+z} y^z$

可以通过改变 combine=’base’ 或 combine=’exp’ 来使 powsimp() 仅合并底数或仅合并指数。默认情况下,combine=’all’,它同时执行这两个操作。如果 force 为 True,则会在不检查假设的情况下合并底数。

You can make powsimp() only combine bases or only combine exponents by changing combine=’base’ or combine=’exp’. By default, combine=’all’, which does both.If force is True then bases will be combined without checking for assumptions.

>>> powsimp(expr, combine='base', force=True)

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

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

$xy(xy)z$

$xy(xy)z$

combsimp

涉及阶乘和二项式的组合表达式可使用 combsimp() 函数进行化简。SymPy 提供了一个 factorial() 函数

Combinatorial expressions involving factorial an binomials can be simplified by using combsimp() function. SymPy provides a factorial() function

>>> expr=factorial(x)/factorial(x - 3)
>>> expr

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

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

$\frac{x!}{(x - 3)!}$

$\frac{x!}{(x - 3)!}$

为了化简上述组合表达式,我们使用 combsimp() 函数,如下所示:

To simplify above combinatorial expression we use combsimp() function as follows −

>>> combsimp(expr)

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

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

$x(x-2)(x-1)$

$x(x-2)(x-1)$

二项式 (x, y) 是从一组 x 个不同的项目中选择 y 个项目的方法的数量。通常也写为 xCy。

The binomial(x, y) is the number of ways to choose y items from a set of x distinct items. It is also often written as xCy.

>>> binomial(x,y)

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

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

$(\frac{x}{y})$

$(\frac{x}{y})$

>>> combsimp(binomial(x+1, y+1)/binomial(x, y))

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

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

$\frac{x + 1}{y + 1}$

$\frac{x + 1}{y + 1}$

logcombine

此函数取对数并将它们使用以下规则进行合并:

This function takes logarithms and combines them using the following rules −

  1. log(x) + log(y) == log(x*y) if both are positive

  2. a*log(x) == log(x**a) if x is positive and a is real

>>> logcombine(a*log(x) + log(y) - log(z))

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

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

$a\log(x) + \log(y) - \log(z)$

$a\log(x) + \log(y) - \log(z)$

如果此函数的 force 参数设置为 True,则如果在某个量上还没有假设,则将假定上述假设成立。

If force parameter of this function is set to True then the assumptions above will be assumed to hold if there is no assumption already in place on a quantity.

>>> logcombine(a*log(x) + log(y) - log(z), force=True)

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

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

$\log\frac{x^a y}{z}$

$\log\frac{x^a y}{z}$