Sympy 简明教程

SymPy - Symbols

Symbol 是 symPy 库中最重要的类。如前所述,符号运算使用符号执行。SymPy 变量是 Symbols 类中的对象。

Symbol is the most important class in symPy library. As mentioned earlier, symbolic computations are done with symbols. SymPy variables are objects of Symbols class.

Symbol() 函数的参数是要分配给变量的符号包含的字符串。

Symbol() function’s argument is a string containing symbol which can be assigned to a variable.

>>> from sympy import Symbol
>>> x=Symbol('x')
>>> y=Symbol('y')
>>> expr=x**2+y**2
>>> expr

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

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

$x^2 + y^2$

$x^2 + y^2$

符号可以包含多个字母。

A symbol may be of more than one alphabets.

>>> s=Symbol('side')
>>> s**3

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

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

$side^3$

$side^3$

SymPy 还有一个 Symbols() 函数,该函数可以同时定义多个符号。字符串包含用逗号或空格分隔的变量名称。

SymPy also has a Symbols() function that can define multiple symbols at once. String contains names of variables separated by comma or space.

>>> from sympy import symbols
>>> x,y,z=symbols("x,y,z")

在 SymPy 的 abc 模块中,所有拉丁字母和希腊字母都被定义为符号。因此,这种方法的便利性在于不需要实例化 Symbol 对象。

In SymPy’s abc module, all Latin and Greek alphabets are defined as symbols. Hence, instead of instantiating Symbol object, this method is convenient.

>>> from sympy.abc import x,y,z

但是,名称 C, O, S, I, N, EQ 是预定义符号。此外,在 abc 模块中没有定义包含多个字母的符号,为此你应该像上面一样使用 Symbol 对象。abc 模块定义了特殊名称,可以检测默认 SymPy 名称空间中的定义。clash1 包含单个字母,clash2 有多个字母冲突符号

However, the names C, O, S, I, N, E and Q are predefined symbols. Also, symbols with more than one alphabets are not defined in abc module, for which you should use Symbol object as above. The abc module defines special names that can detect definitions in default SymPy namespace. clash1 contains single letters and clash2 has multi letter clashing symbols

>>> from sympy.abc import _clash1, _clash2
>>> _clash1

上述片段的输出如下 −

The output of the above snippet is as follows −

{'C': C, 'O': O, 'Q': Q, 'N': N, 'I': I, 'E': E, 'S': S}

{'C': C, 'O': O, 'Q': Q, 'N': N, 'I': I, 'E': E, 'S': S}

>>> _clash2

上述片段的输出如下 −

The output of the above snippet is as follows −

{'beta': beta, 'zeta': zeta, 'gamma': gamma, 'pi': pi}

{'beta': beta, 'zeta': zeta, 'gamma': gamma, 'pi': pi}

索引符号可以使用类似于 range() 函数的语法进行定义。范围由冒号指示。范围类型由冒号右侧的字符确定。如果 itr 是一个数字,则左侧的所有连续数字都将视为非负起始值。右侧的所有连续数字都将视为比结束值大 1。

Indexed symbols can be defined using syntax similar to range() function. Ranges are indicated by a colon. Type of range is determined by the character to the right of the colon. If itr is a digit, all contiguous digits to the left are taken as the nonnegative starting value. All contiguous digits to the right are taken as 1 greater than the ending value.

>>> from sympy import symbols
>>> symbols('a:5')

上述片段的输出如下 −

The output of the above snippet is as follows −

(a0, a1, a2, a3, a4)

(a0, a1, a2, a3, a4)

>>> symbols('mark(1:4)')

上述片段的输出如下 −

The output of the above snippet is as follows −

(mark1, mark2, mark3)

(mark1, mark2, mark3)