Sympy 简明教程

SymPy - Solvers

由于符号 = 和 == 在 Python 中被定义为赋值和相等运算符,它们无法用于制定符号方程式。SymPy 提供 Eq() 函数来设置方程式。

Since the symbols = and == are defined as assignment and equality operators in Python, they cannot be used to formulate symbolic equations. SymPy provides Eq() function to set up an equation.

>>> from sympy import *
>>> x,y=symbols('x y')
>>> Eq(x,y)

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

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

x = y

x = y

由于仅当 x-y=0 时,x=y 才成立,因此上述方程式可以写成 −

Since x=y is possible if and only if x-y=0, above equation can be written as −

>>> Eq(x-y,0)

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

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

x − y = 0

x − y = 0

SymPy 中的 solver 模块提供了 soveset() 函数,其原型如下 −

The solver module in SymPy provides soveset() function whose prototype is as follows −

solveset(equation, variable, domain)

默认的域是 S.Complexes。使用 solveset() 函数,我们可以如下求解代数方程式 −

The domain is by default S.Complexes. Using solveset() function, we can solve an algebraic equation as follows −

>>> solveset(Eq(x**2-9,0), x)

获得以下输出 −

The following output is obtained −

{−3, 3}

{−3, 3}

>>> solveset(Eq(x**2-3*x, -2),x)

执行上面的代码片段后获得以下输出 −

The following output is obtained after executing the above code snippet −

{1,2}

{1,2}

solveset 的输出是有限集的解。如果没有解,则返回空集

The output of solveset is a FiniteSet of the solutions. If there are no solutions, an EmptySet is returned

>>> solveset(exp(x),x)

执行上面的代码片段后获得以下输出 −

The following output is obtained after executing the above code snippet −

$\varnothing$

$\varnothing$

Linear equation

我们必须使用 linsolve() 函数来求解线性方程。

We have to use linsolve() function to solve linear equations.

举例来说,方程如下 −

For example, the equations are as follows −

x-y=4

x+y=1

>>> from sympy import *
>>> x,y=symbols('x y')
>>> linsolve([Eq(x-y,4),Eq( x + y ,1) ], (x, y))

执行上面的代码片段后获得以下输出 −

The following output is obtained after executing the above code snippet −

$\lbrace(\frac{5}{2},-\frac{3}{2})\rbrace$

$\lbrace(\frac{5}{2},-\frac{3}{2})\rbrace$

linsolve() 函数也可以解矩阵形式表示的线性方程。

The linsolve() function can also solve linear equations expressed in matrix form.

>>> a,b=symbols('a b')
>>> a=Matrix([[1,-1],[1,1]])
>>> b=Matrix([4,1])
>>> linsolve([a,b], (x,y))

如果执行以上代码片段,我们将得到以下输出 −

We get the following output if we execute the above code snippet −

$\lbrace(\frac{5}{2},-\frac{3}{2})\rbrace$

$\lbrace(\frac{5}{2},-\frac{3}{2})\rbrace$

Non-linear equation

为此,我们使用 nonlinsolve() 函数。此示例的方程 −

For this purpose, we use nonlinsolve() function. Equations for this example −

a2+a=0 a-b=0

>>> a,b=symbols('a b')
>>> nonlinsolve([a**2 + a, a - b], [a, b])

如果执行以上代码片段,我们将得到以下输出 −

We get the following output if we execute the above code snippet −

$\lbrace(-1, -1),(0,0)\rbrace$

$\lbrace(-1, -1),(0,0)\rbrace$

differential equation

首先,通过将 cls=Function 传递给 symbols 函数来创建一个未定义的函数。若要解微分方程,请使用 dsolve。

First, create an undefined function by passing cls=Function to the symbols function. To solve differential equations, use dsolve.

>>> x=Symbol('x')
>>> f=symbols('f', cls=Function)
>>> f(x)

执行上面的代码片段后获得以下输出 −

The following output is obtained after executing the above code snippet −

f(x)

f(x)

此处 f(x) 是一个未评估的函数。其导数如下 −

Here f(x) is an unevaluated function. Its derivative is as follows −

>>> f(x).diff(x)

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

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

$\frac{d}{dx}f(x)$

$\frac{d}{dx}f(x)$

我们首先创建与以下微分方程对应的 Eq 对象

We first create Eq object corresponding to following differential equation

>>> eqn=Eq(f(x).diff(x)-f(x), sin(x))
>>> eqn

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

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

$-f(x) + \frac{d}{dx}f(x)= \sin(x)$

$-f(x) + \frac{d}{dx}f(x)= \sin(x)$

>>> dsolve(eqn, f(x))

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

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

$f(x)=(c1-\frac{e-xsin(x)}{2}-\frac{e-xcos(x)}{2})ex$

$f(x)=(c1-\frac{e-xsin(x)}{2}-\frac{e-xcos(x)}{2})ex$