SymPy学习之Elementary

sympy.functions.elementary.complexes

re
#返回复数实数部分
>>> from sympy import re, im, I, E
>>> from sympy.abc import x, y
>>> re(2*E)
2*E
>>> re(2*I + 17)
17
>>> re(2*I)
0
>>> re(im(x) + x*I + 2)
2
#as_real_imag(deep=True, **hints)
#Returns the real number with a zero imaginary part.
im
#返回复数的虚数部分
>>> from sympy import re, im, E, I
>>> from sympy.abc import x, y
>>> im(2*E)
0
>>> re(2*I + 17)
17
>>> im(x*I)
re(x)
>>> im(re(x) + y)
im(y)
#as_real_imag(deep=True, **hints)
#Return the imaginary part with a zero real part.
>>> from sympy.functions import im
>>> from sympy import I
>>> im(2 + 3*I).as_real_imag()
(3, 0)
sign
>>> from sympy.functions import sign
>>> from sympy.core.numbers import I
#实数时,正返回1,0返回0,负返回-1
>>> sign(-1)
-1
>>> sign(0)
0
#纯虚数时,正返回I,负返回-I
>>> sign(-3*I)
-I
#复数返回原式
>>> sign(1 + I)
sign(1 + I)
#evalf方法返回cos(arg(expr)) + I*sin(arg(expr)).
#即将原式化为单位长度向量
>>> _.evalf()
0.707106781186548 + 0.707106781186548*I
Abs
#返回绝对值
>>> from sympy import Abs, Symbol, S
>>> Abs(-1)
1
>>> x = Symbol('x', real=True)
>>> Abs(-x)
Abs(x)
>>> Abs(x**2)
x**2
>>> abs(-x) # The Python built-in 传入符号变量自动转换成Abs
Abs(x)
arg
#返回参数角度,实数总是0,纯虚数总是pi/2
>>> from sympy.functions import arg
>>> from sympy import I, sqrt
>>> arg(2.0)
0
>>> arg(I)
pi/2
>>> arg(sqrt(2) + I*sqrt(2))
pi/4
conjugate
#返回共轭数
>>> from sympy import conjugate, I
>>> conjugate(2)
2
>>> conjugate(I)
-I

sympy.functions.elementary.trigonometric

sin
#返回正弦值
>>> from sympy import sin, pi
>>> from sympy.abc import x
>>> sin(x**2).diff(x)
2*x*cos(x**2)
>>> sin(1).diff(x)
0
>>> sin(pi)
0
>>> sin(pi/2)
1
>>> sin(pi/6)
1/2
>>> sin(pi/12)
-sqrt(2)/4 + sqrt(6)/4
cos
#返回余弦值
>>> from sympy import cos, pi
>>> from sympy.abc import x
>>> cos(x**2).diff(x)
-2*x*sin(x**2)
>>> cos(1).diff(x)
0
>>> cos(pi)
-1
>>> cos(pi/2)
0
>>> cos(2*pi/3)
-1/2
>>> cos(pi/12)
sqrt(2)/4 + sqrt(6)/4
tan
#返回正切值
>>> from sympy import tan, pi
>>> from sympy.abc import x
>>> tan(x**2).diff(x)
2*x*(tan(x**2)**2 + 1)
>>> tan(1).diff(x)
0
>>> tan(pi/8).expand()
-1 + sqrt(2)
cot
#返回余切值
>>> from sympy import cot, pi
>>> from sympy.abc import x
>>> cot(x**2).diff(x)
2*x*(-cot(x**2)**2 - 1)
>>> cot(1).diff(x)
0
>>> cot(pi/12)
sqrt(3) + 2
sec
#返回正割值
>>> from sympy import sec
>>> from sympy.abc import x
>>> sec(x**2).diff(x)
2*x*tan(x**2)*sec(x**2)
>>> sec(1).diff(x)
0
csc
#返回余割值
>>> from sympy import csc
>>> from sympy.abc import x
>>> csc(x**2).diff(x)
-2*x*cot(x**2)*csc(x**2)
>>> csc(1).diff(x)
0
sinc
#辛格函数 sinc(x) = sin(x)/x
>>> sinc(0)
1
>>> sinc(oo)
0
asin
#反正弦函数
>>> from sympy import asin, oo, pi
>>> asin(1)
pi/2
>>> asin(-1)
-pi/2
acos
#反余弦函数
>>> from sympy import acos, oo, pi
>>> acos(1)
0
>>> acos(0)
pi/2
>>> acos(oo)
oo*I
atan
#反正切函数
>>> from sympy import atan, oo, pi
>>> atan(0)
0
>>> atan(1)
pi/4
>>> atan(oo)
pi/2
acot
#反余切函数
>>> acot(0)
pi/2
>>> acot(1)
pi/4
>>> acot(oo)
0
asec
#反正割函数
>>> from sympy import asec, oo, pi
>>> asec(1)
0
>>> asec(-1)
pi
acsc
#反余割函数
>>> from sympy import acsc, oo, pi
>>> acsc(1)
pi/2
>>> acsc(-1)
-pi/2

sympy.functions.elementary.integers

ceiling
#返回大于给定值的最小整值
>>> from sympy import ceiling, E, I, Float, Rational
>>> ceiling(17)
17
>>> ceiling(Rational(23, 10))
3
>>> ceiling(2*E)
6
>>> ceiling(-Float(0.567))
0
>>> ceiling(I/2)
I
floor
#返回小于给定值的最大整值
>>> from sympy import floor, E, I, Float, Rational
>>> floor(17)
17
>>> floor(Rational(23, 10))
2
>>> floor(2*E)
5
>>> floor(-Float(0.567))
-1
>>> floor(-I/2)
-I
round
#返回四舍五入值
>>> round(5.5)
6
>>> round(5.4)
5
frac
#取小数部分
>>> from sympy import Symbol, frac, Rational, floor, ceiling, I
>>> frac(Rational(4, 3))
1/3
>>> frac(-Rational(4, 3))
2/3

sympy.functions.elementary.exponential

exp
#指数函数
>>> from sympy import I
>>> from sympy.abc import x
>>> from sympy.functions import exp
>>> exp(x).as_real_imag()
(exp(re(x))*cos(im(x)), exp(re(x))*sin(im(x)))
>>> exp(1).as_real_imag()
(E, 0)
>>> exp(I).as_real_imag()
(cos(1), sin(1))
>>> exp(1+I).as_real_imag()
(E*cos(1), E*sin(1))
>>> exp(3).base
E
log
#对数函数
>>> from sympy import I
>>> from sympy.abc import x
>>> from sympy.functions import log
>>> log(x).as_real_imag()
(log(Abs(x)), arg(x))
>>> log(I).as_real_imag()
(0, pi/2)
>>> log(1 + I).as_real_imag()
(log(sqrt(2)), pi/4)
>>> log(I*x).as_real_imag()
(log(Abs(x)), arg(I*x))

sympy.functions.elementary.piecewise

Piecewise
#分段函数
>>> from sympy import Piecewise, log
>>> from sympy.abc import x
>>> f = x**2
>>> g = log(x)
>>> p = Piecewise( (0, x<-1), (f, x<=1), (g, True))
>>> p.subs(x,1)
1
>>> p.subs(x,5)
log(5)
#重新构成一个分段函数
>>> from sympy import Piecewise, piecewise_fold, sympify as S
>>> from sympy.abc import x
>>> p = Piecewise((x, x < 1), (1, S(1) <= x))
>>> piecewise_fold(x*p)
Piecewise((x**2, x < 1), (x, 1 <= x))

sympy.functions.elementary.miscellaneous

Min
#返回最小值
>>> from sympy import Min, Symbol, oo
>>> from sympy.abc import x, y
>>> p = Symbol('p', positive=True)
>>> n = Symbol('n', negative=True)
>>> Min(x, -2) 
Min(x, -2)
>>> Min(x, -2).subs(x, 3)
-2
>>> Min(p, -3)
-3
>>> Min(x, y) 
Min(x, y)
>>> Min(n, 8, p, -7, p, oo) 
Min(n, -7)
Max
#返回最大值
>>> from sympy import Max, Symbol, oo
>>> from sympy.abc import x, y
>>> p = Symbol('p', positive=True)
>>> n = Symbol('n', negative=True)
>>> Max(x, -2) 
Max(x, -2)
>>> Max(x, -2).subs(x, 3)
3
>>> Max(p, -2)
p
>>> Max(x, y) 
Max(x, y)
>>> Max(x, y) == Max(y, x)
True
>>> Max(x, Max(y, z)) 
Max(x, y, z)
>>> Max(n, 8, p, 7, -oo) 
Max(8, p)
>>> Max (1, x, oo)
oo
root
#开n次方根
>>> from sympy import root, Rational
>>> from sympy.abc import x, n
>>> root(x, 2)
sqrt(x)
>>> root(x, 3)
x**(1/3)
Run code block in SymPy Live
>>> root(x, n)
x**(1/n)
>>> root(x, -Rational(2, 3))
x**(-3/2)
sqrt
#开根号
>>> from sympy import sqrt, Symbol
>>> x = Symbol('x')
>>> sqrt(x)
sqrt(x)
>>> sqrt(x)**2
x

你可能感兴趣的:(Python,Sympy,数学计算,Python,Sympy)