用Python作科學計算-SymPy符號運算

安裝sympy庫 bash

~$ sudo pip install sympy

例子code

>>> from __future__ import division
>>> from sympy import *
>>> x, y, z = symbols('x,y,z')
>>> k, m, n = symbols('k,m,n', integer=True)

>>> f, g, h = map(Function, 'fgh')
>>> E**(I*pi)+1
0
>>> expand( E**(I*x) )
exp(I*x)
>>> expand(exp(I*x), complex=True)
I*exp(-im(x))*sin(re(x)) + exp(-im(x))*cos(re(x))
>>> x = Symbol("x", real=True)
>>> expand(exp(I*x), complex=True)
I*sin(x) + cos(x)
>>> tmp = series(exp(I*x), x, 0, 10)
>>> tmp
1 + I*x - x**2/2 - I*x**3/6 + x**4/24 + I*x**5/120 - x**6/720 - I*x**7/5040 + x**8/40320 + I*x**9/362880 + O(x**10)
>>> re(tmp)
x**8/40320 - x**6/720 + x**4/24 - x**2/2 + re(O(x**10)) + 1
>>> im(tmp)
x**9/362880 - x**7/5040 + x**5/120 - x**3/6 + x + im(O(x**10))
>>> series(cos(x), x, 0, 10)
1 - x**2/2 + x**4/24 - x**6/720 + x**8/40320 + O(x**10)
>>> series(sin(x), x, 0, 10)
x - x**3/6 + x**5/120 - x**7/5040 + x**9/362880 + O(x**10)
>>> integrate(x*sin(x), x)
-x*cos(x) + sin(x)
>>> integrate(x*sin(x), (x, 0, 2*pi))
-2*pi
>>>
相關文章
相關標籤/搜索