分類: 實用工具

SymPy:使用 Python 幫你導煩人的數學公式

複數(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

Page: 1 2 3 4 5

G. T. Wang

個人使用 Linux 經驗長達十餘年,樂於分享各種自由軟體技術與實作文章。

Share
Published by
G. T. Wang
標籤: 數學

Recent Posts

[開箱] 德國美善品 Thermomix TM6 多功能料理機

本篇是德國美善品 Thermo...

3 年 ago

[開箱] Nokia 215 4G 經典功能型手機

本篇是 Nokia 215 4...

3 年 ago

[開箱] Holy Stone HS210 迷你遙控飛機

本篇是 Holy Stone ...

3 年 ago

[DIY] 用竹子製作吹火筒教學

本篇記錄我用乾燥的竹子製作吹火...

3 年 ago

高鐵會員 TGO 點數兌換商品教學

這裡示範如何線上使用高鐵會員 ...

3 年 ago

Google AdSense 廣告放送量受限問題記錄

這篇是本站的 Google A...

3 年 ago