微分方程(Differential Equations)
>>> from sympy import Function, Symbol, dsolve >>> f = Function('f') >>> x = Symbol('x') >>> f(x).diff(x, x) + f(x) 2 d f(x) + ---(f(x)) 2 dx >>> dsolve(f(x).diff(x, x) + f(x), f(x)) f(x) = C1*sin(x) + C2*cos(x)
代數方程式(Algebraic Equations)
>>> from sympy import solve, symbols >>> x, y = symbols('x,y') >>> solve(x**4 - 1, x) [-1, 1, -I, I] >>> solve([x + 5*y - 2, -3*x + 6*y - 15], [x, y]) {x: -3, y: 1}
線性代數(Linear Algebra)
建立矩陣可以使用 Matrix 類別:
>>> from sympy import Matrix, Symbol >>> Matrix([[1, 0], [0, 1]]) [1 0] [ ] [0 1]
矩陣中亦可包含 Symbol:
>>> x = Symbol('x') >>> y = Symbol('y') >>> A = Matrix([[1, x], [y, 1]]) >>> A [1 x] [ ] [y 1] >>> A**2 [x*y + 1 2*x ] [ ] [ 2*y x*y + 1]
Pattern Matching
使用 .match()
方法並配合 Wild 類別可以對 Symbol 進行比對,例如抓取某一項的係數等,這個方法會傳回一個 dictionary,例如:
>>> from sympy import Symbol, Wild >>> x = Symbol('x') >>> p = Wild('p') >>> (5*x**2).match(p*x**2) {p: 5} >>> q = Wild('q') >>> (x**2).match(p*x**q) {p: 1, q: 2}
如果沒有找到指定的 pattern,則會傳回 None:
>>> print (x + 1).match(p**x)
None
亦可使用 Wild 的 exclude
參數排除指定的比對結果:
>>> p = Wild('p', exclude=[1, x]) >>> print (x + 1).match(x + p) # 1 is excluded None >>> print (x + 1).match(p + 1) # x is excluded None >>> print (x + 1).match(x + 2 + p) # -1 is not excluded {p_: -1}
輸出顯示(Printing)
SymPy 的運算結果可以用好幾種不同的方式輸出,一般預設的方式是使用 str(expression)
的方式,例如:
>>> from sympy import Integral >>> from sympy.abc import x >>> print x**2 x**2 >>> print 1/x 1/x >>> print Integral(x**2, x) Integral(x**2, x)
而 pprint()
函數(pretty printing)可以將結果以 ascii-art 的方式「畫」出來,例如:
>>> from sympy import Integral, pprint >>> from sympy.abc import x >>> pprint(x**2) 2 x >>> pprint(1/x) 1 - x >>> pprint(Integral(x**2, x)) / | | 2 | x dx | /
若您的系統有安裝 unicode 字型,則 pprint()
函數預設就會使用它,亦可使用 use_unicode
參數自行設定。
>>> pprint(Integral(x**2, x), use_unicode=True)
⌠
⎮ 2
⎮ x dx
⌡
下面示範將 pprint()
設定為預設的輸出函數:
$ python Python 2.5.2 (r252:60911, Jun 25 2008, 17:58:32) [GCC 4.3.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from sympy import init_printing, var, Integral >>> init_printing(use_unicode=False, wrap_line=False, no_global=True) >>> var("x") x >>> x**3/3 3 x -- 3 >>> Integral(x**2, x) #doctest: +NORMALIZE_WHITESPACE / | | 2 | x dx | /
使用 Python 輸出:
>>> from sympy.printing.python import python >>> from sympy import Integral >>> from sympy.abc import x >>> print python(x**2) x = Symbol('x') e = x**2 >>> print python(1/x) x = Symbol('x') e = 1/x >>> print python(Integral(x**2, x)) x = Symbol('x') e = Integral(x**2, x)
使用 LaTeX 輸出:
>>> from sympy import Integral, latex >>> from sympy.abc import x >>> latex(x**2) x^{2} >>> latex(x**2, mode='inline') $x^{2}$ >>> latex(x**2, mode='equation') begin{equation}x^{2}end{equation} >>> latex(x**2, mode='equation*') begin{equation*}x^{2}end{equation*} >>> latex(1/x) frac{1}{x} >>> latex(Integral(x**2, x)) int x^{2}, dx
使用 MathML 輸出:
>>> from sympy.printing.mathml import mathml >>> from sympy import Integral, latex >>> from sympy.abc import x >>> print mathml(x**2)>>> print mathml(1/x) x 2 x -1
使用 Pyglet:
>>> from sympy import Integral, preview >>> from sympy.abc import x >>> preview(Integral(x**2, x))
如果系統上有安裝 pyglet 的話,就會跳出這個視窗:
參考資料:
http://docs.sympy.org/dev/index.html
http://docs.scipy.org/doc/numpy/user/
http://www.linuxjournal.com/content/symbolic-math-python
http://blog.ez2learn.com/2009/08/25/sympy/
http://www.360doc.com/content/12/0930/10/9369336_238871552.shtml
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!