Sympy 简明教程
SymPy - Function class
Sympy 包具有 Function 类,该类在 sympy.core.function 模块中定义。它是一个基类,适用于所有应用的数学函数,也是未定义函数类的构造函数。
Sympy package has Function class, which is defined in sympy.core.function module. It is a base class for all applied mathematical functions, as also a constructor for undefined function classes.
以下类别的函数从 Function 类继承 −
Following categories of functions are inherited from Function class −
-
Functions for complex number
-
Trigonometric functions
-
Functions for integer number
-
Combinatorial functions
-
Other miscellaneous functions
Functions for complex number
这组函数在 sympy.functions.elementary.complexes 模块中定义。
This set of functions is defined in sympy.functions.elementary.complexes module.
re
re
此函数返回表达式的实部 −
This function returns real part of an expression −
>>> from sympy import *
>>> re(5+3*I)
以上代码片段的输出如下所示 −
The output for the above code snippet is given below −
5
5
>>> re(I)
以上代码片段的输出为 −
The output for the above code snippet is −
0
0
Im
Im
此函数返回表达式的虚部 −
This function returns imaginary part of an expression −
>>> im(5+3*I)
以上代码片段的输出如下所示 −
The output for the above code snippet is given below −
3
3
>>> im(I)
以上代码片段的输出如下所示 −
The output for the above code snippet is given below −
1
1
sign
sign
此函数返回表达式的复数标志。
This function returns the complex sign of an expression.
对于实部表达式,标志将为 −
For real expression, the sign will be −
-
1 if expression is positive
-
0 if expression is equal to zero
-
-1 if expression is negative
如果表达式为虚部,返回的标志为 −
If expression is imaginary the sign returned is −
-
I if im(expression) is positive
-
-I if im(expression) is negative
>>> sign(1.55), sign(-1), sign(S.Zero)
以上代码片段的输出如下所示 −
The output for the above code snippet is given below −
(1, -1, 0)
(1, -1, 0)
>>> sign (-3*I), sign(I*2)
以上代码片段的输出如下所示 −
The output for the above code snippet is given below −
(-I, I)
(-I, I)
Abs
Abs
此函数返回复数的绝对值。它定义为复平面中原点 (0,0) 和点 (a,b) 之间的距离。此函数是内置函数 abs() 的扩展,以接受符号值。
This function return absolute value of a complex number. It is defined as the distance between the origin (0,0) and the point (a,b) in the complex plane. This function is an extension of the built-in function abs() to accept symbolic values.
>>> Abs(2+3*I)
以上代码片段的输出如下所示 −
The output for the above code snippet is given below −
$\sqrt13$
$\sqrt13$
conjugate
conjugate
此函数返回复数的共轭。要找到复共轭,我们更改虚部的符号。
This function returns conjugate of a complex number. To find the complex conjugate we change the sign of the imaginary part.
>>> conjugate(4+7*I)
执行以上代码片段后,可以获得以下输出:
You get the following output after executing the above code snippet −
4 - 7i
4 - 7i
Trigonometric functions
SymPy 已定义所有三角比——sin cos、tan 等以及其逆如 asin、acos、atan 等。这些函数计算用弧度表示的给定角度的相应值。
SymPy has defintions for all trigonometric ratios - sin cos, tan etc as well as well as its inverse counterparts such as asin, acos, atan etc. These functions compute respective value for given angle expressed in radians.
>>> sin(pi/2), cos(pi/4), tan(pi/6)
以上代码片段的输出如下所示 −
The output for the above code snippet is given below −
(1, sqrt(2)/2, sqrt(3)/3)
(1, sqrt(2)/2, sqrt(3)/3)
>>> asin(1), acos(sqrt(2)/2), atan(sqrt(3)/3)
以上代码片段的输出如下所示 −
The output for the above code snippet is given below −
(pi/2, pi/4, pi/6)
(pi/2, pi/4, pi/6)
Functions on Integer Number
这是一组在整数上执行各种操作的函数。
This is a set of functions to perform various operations on integer number.
ceiling
ceiling
这是一个单变量函数,它返回不小于其参数的最小整数值。对于复数,分别对实部和虚部取上整。
This is a univariate function which returns the smallest integer value not less than its argument. In case of complex numbers, ceiling of the real and imaginary parts separately.
>>> ceiling(pi), ceiling(Rational(20,3)), ceiling(2.6+3.3*I)
以上代码片段的输出如下所示 −
The output for the above code snippet is given below −
(4, 7, 3 + 4*I)
(4, 7, 3 + 4*I)
floor
floor
此函数返回不大小于其参数的最大整数值。对于复数,此函数也分别对实部和虚部取下整。
This function returns the largest integer value not greater than its argument. In case of complex numbers, this function too takes the floor of the real and imaginary parts separately.
>>> floor(pi), floor(Rational(100,6)), floor(6.3-5.9*I)
以上代码片段的输出如下所示 −
The output for the above code snippet is given below −
(3, 16, 6 - 6*I)
(3, 16, 6 - 6*I)
frac
frac
此函数表示 x 的小数部分。
This function represents the fractional part of x.
>>> frac(3.99), frac(Rational(10,3)), frac(10)
以上代码片段的输出如下所示 −
The output for the above code snippet is given below −
(0.990000000000000, 1/3, 0)
(0.990000000000000, 1/3, 0)
Combinatorial functions
组合学是一个数学领域,涉及有限或离散系统中的选择、排列和运算问题。
Combinatorics is a field of mathematics concerned with problems of selection, arrangement, and operation within a finite or discrete system.
factorial
factorial
阶乘在组合数学中非常重要,它提供了一种排列 n 个对象的排列方式的数目。它以符号形式表示为 x! 这个函数实现了非负整数的阶乘函数,负整数的阶乘是复无穷大。
The factorial is very important in combinatorics where it gives the number of ways in which n objects can be permuted. It is symbolically represented as 𝑥! This function is implementation of factorial function over nonnegative integers, factorial of a negative integer is complex infinity.
>>> x=Symbol('x')
>>> factorial(x)
以上代码片段的输出如下所示 −
The output for the above code snippet is given below −
x!
x!
>>> factorial(5)
以上代码片段的输出如下所示 −
The output for the above code snippet is given below −
120
120
>>> factorial(-1)
以上代码片段的输出如下所示 −
The output for the above code snippet is given below −
$\infty\backsim$
$\infty\backsim$
binomial
此函数给出了从 n 个元素的集合中选择 k 个元素的方式的数量。
This function the number of ways we can choose k elements from a set of n elements.
>>> x,y=symbols('x y')
>>> binomial(x,y)
以上代码片段的输出如下所示 −
The output for the above code snippet is given below −
$(\frac{x}{y})$
$(\frac{x}{y})$
>>> binomial(4,2)
以上代码片段的输出如下所示 −
The output for the above code snippet is given below −
6
6
帕斯卡三角形的行可以使用二项式函数生成。
Rows of Pascal’s triangle can be generated with the binomial function.
>>> for i in range(5): print ([binomial(i,j) for j in range(i+1)])
执行以上代码片段后,可以获得以下输出:
You get the following output after executing the above code snippet −
[1]
[1]
[1, 1]
[1, 1]
[1, 2, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 4, 6, 4, 1]
fibonacci
fibonacci
斐波那契数是由初始项 F0=0, F1=1 和二项递推关系 Fn=Fn−1+Fn−2 定义的整数序列。
The Fibonacci numbers are the integer sequence defined by the initial terms F0=0, F1=1 and the two-term recurrence relation Fn=Fn−1+Fn−2.
>>> [fibonacci(x) for x in range(10)]
执行上面的代码片段后获得以下输出 −
The following output is obtained after executing the above code snippet −
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
tribonacci
tribonacci
Tribonacci 数列是由初始项 F0=0、F1=1、F2=1 和三项循环关系式 Fn=Fn-1+Fn-2+Fn-3 定义的整数序列。
The Tribonacci numbers are the integer sequence defined by the initial terms F0=0, F1=1, F2=1 and the three-term recurrence relation Fn=Fn-1+Fn-2+Fn-3.
>>> tribonacci(5, Symbol('x'))
上面的代码片段给出的输出等同于以下表达式 −
The above code snippet gives an output equivalent to the below expression −
$x^8 + 3x^5 + 3x^2$
$x^8 + 3x^5 + 3x^2$
>>> [tribonacci(x) for x in range(10)]
执行上面的代码片段后获得以下输出 −
The following output is obtained after executing the above code snippet −
[0, 1, 1, 2, 4, 7, 13, 24, 44, 81]
[0, 1, 1, 2, 4, 7, 13, 24, 44, 81]
Miscellaneous Functions
以下是一些常用函数的列表 −
Following is a list of some frequently used functions −
Min − 返回列表的最小值。它命名为 Min 以避免与内置函数 min 冲突。
Min − Returns minimum value of the list. It is named Min to avoid conflicts with the built-in function min.
Max − 返回列表的最大值。它命名为 Max 以避免与内置函数 max 冲突。
Max − Returns maximum value of the list. It is named Max to avoid conflicts with the built-in function max.
root − 返回 x 的 n 次方根。
root − Returns nth root of x.
sqrt − 返回 x 的主平方根。
sqrt − Returns the principal square root of x.
cbrt − 此函数计算 x 的主立方根(x++Rational(1,3) 的快捷方式)。
cbrt − This function computes the principal cube root of x, (shortcut for x++Rational(1,3)).
以下是以上杂项函数及其各自输出的示例 −
The following are the examples of the above miscellaneous functions and their respective outputs −
>>> Min(pi,E)
e
e
>>> Max(5, Rational(11,2))
$\frac{11}{2}$
$\frac{11}{2}$
>>> root(7,Rational(1,2))
49
49
>>> sqrt(2)
$\sqrt2$
$\sqrt2$
>>> cbrt(1000)
10
10