複數(Complex numbers)
SymPy 也可以處理複數,Symbol 亦可以指定屬性(attribute),例如:real、positive 或 complex 等,指定屬性會影響其運算的結果。
>>> from sympy import Symbol, exp, I >>> x = Symbol("x") # a plain x with no attributes >>> exp(I*x).expand() I*x e >>> exp(I*x).expand(complex=True) -im(x) -im(x) I*e *sin(re(x)) + e *cos(re(x)) >>> x = Symbol("x", real=True) >>> exp(I*x).expand(complex=True) I*sin(x) + cos(x)
三角函數(Trigonometric)
>>> from sympy import asin, asinh, cos, sin, sinh, symbols, I >>> x, y = symbols('x,y') >>> sin(x + y).expand(trig=True) sin(x)*cos(y) + sin(y)*cos(x) >>> cos(x + y).expand(trig=True) -sin(x)*sin(y) + cos(x)*cos(y) >>> sin(I*x) I*sinh(x) >>> sinh(I*x) I*sin(x) >>> asinh(I) I*pi ---- 2 >>> asinh(I*x) I*asin(x) >>> sin(x).series(x, 0, 10) 3 5 7 9 x x x x / 10 x - -- + --- - ---- + ------ + Ox / 6 120 5040 362880 >>> sinh(x).series(x, 0, 10) 3 5 7 9 x x x x / 10 x + -- + --- + ---- + ------ + Ox / 6 120 5040 362880 >>> asin(x).series(x, 0, 10) 3 5 7 9 x 3*x 5*x 35*x / 10 x + -- + ---- + ---- + ----- + Ox / 6 40 112 1152 >>> asinh(x).series(x, 0, 10) 3 5 7 9 x 3*x 5*x 35*x / 10 x - -- + ---- - ---- + ----- + Ox / 6 40 112 1152
Spherical Harmonics
>>> from sympy import Ylm >>> from sympy.abc import theta, phi >>> Ylm(1, 0, theta, phi) ___ / 3 *cos(theta) ---------------- ____ 2*/ pi >>> Ylm(1, 1, theta, phi) ___ I*phi -/ 6 *e *sin(theta) ------------------------ ____ 4*/ pi >>> Ylm(2, 1, theta, phi) ____ I*phi -/ 30 *e *sin(theta)*cos(theta) ------------------------------------ ____ 4*/ pi
階乘(factorials)與 Gamma 函數
>>> from sympy import factorial, gamma, Symbol >>> x = Symbol("x") >>> n = Symbol("n", integer=True) >>> factorial(x) x! >>> factorial(n) n! >>> gamma(x + 1).series(x, 0, 3) # i.e. factorial(x) / 2 2 2 |EulerGamma pi | / 3 1 - EulerGamma*x + x *|----------- + ---| + Ox / 2 12/
Zeta 函數
>>> from sympy import zeta >>> zeta(4, x) zeta(4, x) >>> zeta(4, 1) 4 pi --- 90 >>> zeta(4, 2) 4 pi -1 + --- 90 >>> zeta(4, 3) 4 17 pi - -- + --- 16 90
多項式(polynomials)
>>> from sympy import assoc_legendre, chebyshevt, legendre, hermite >>> chebyshevt(2, x) 2 2*x - 1 >>> chebyshevt(4, x) 4 2 8*x - 8*x + 1 >>> legendre(2, x) 2 3*x 1 ---- - - 2 2 >>> legendre(8, x) 8 6 4 2 6435*x 3003*x 3465*x 315*x 35 ------- - ------- + ------- - ------ + --- 128 32 64 32 128 >>> assoc_legendre(2, 1, x) __________ / 2 -3*x*/ - x + 1 >>> assoc_legendre(2, 2, x) 2 - 3*x + 3 >>> hermite(3, x) 3 8*x - 12*x
Allen
There is a typo in the first command of linux, which is
sudo apt-get instlal python-sympy
The following should be the right one
sudo apt-get install python-sympy
G. T. Wang
Thank you very much!