http://blog.csdn.net/pipisorry/article/details/39123247
SymPy是Python的一個數學符號計算庫,目的在于成為一個富有特色的計算機代數系統。
pip install sympy
?SymPy的下載(Downloads)網頁
SymPy有三個內建的數值類型:實數,有理數和整數。
有理數類用兩個整數來表示分子與分母,Rational(1,2)代表1/2等等。
>>>from sympy import *
>>>a=Rational(1,2)
>>>a
1/2
>>>a*2
1
>>>Rational(2)**50/Rational(10)**50
1/88817841970012523233890533447265625
有一些特殊的常數像e、pi和無窮大oo,它們會被當作符號去對待。(1+pi不會求得值,反而它會保持為1+pi)。調用求表達式的浮點數-evalf()函數才會求值。
字符查詢器,示例:
>>>pi**2
pi**2
>>>pi.evalf()
3.14159265358979
>>>(pi+exp(1)).evalf()
5.85987448204884
>>>oo?>?99999
True
>>>oo?+?1
數學符號生成器?oo
最好將x替換操作傳到evalf參數里。If the?substitution will be followed by numerical evaluation, it is better to pass the substitution to evalf.
>>> (1/x).evalf(subs={x: 3.0}, n=21)
0.333333333333333333333rather than
>>> (1/x).subs({x: 3.0}).evalf(21)
0.333333333333333314830
皮皮blog
對比其他的計算機代數系統,在SymPy中要明確聲明符號變量:
from sympy import *
x=Symbol("x")
x = symbols('x')
x,y,z=symbols('x y z')
crazy = symbols('unrelated')
k, m, n = symbols(‘kmn’, integer=True)
from sympy.abc import x, y? ? #應該是定義好的
f = Function(“f”)
示例:
>>> x = symbols('x')
>>> x + 1
x + 1
>>>x,y,z=symbols('x y z')
>>> crazy = symbols('unrelated')
>>> crazy + 1
unrelated + 1
>>>x+y+x-y
2*x
>>> x = symbols('x')
>>> expr = x + 1
>>> x = 2 #Changing?x?to?2?had no effect on?expr. This is because?x?=?2?changes the Python variable?x?to?2, but has no effect on the SymPy Symbol?x, which was what we used in creating?expr.
>>> print(expr)
x + 1
((x+y)**2).expand()
2*x*y?+?x**2?+?y**2
mathematica符號運算,示例:
>>> x = symbols('x')
>>> expr = x + 1
>>> expr.subs(x, 2)
3
>>> from sympy import pi, exp, limit, oo
>>> from sympy.abc import x, y
>>> (1 + x*y).subs(x, pi)
pi*y + 1
>>> (1 + x*y).subs({x:pi, y:2})
1 + 2*pi
>>> (1 + x*y).subs([(x, pi), (y, 2)])
1 + 2*pi
>>> reps = [(y, x**2), (x, 2)]
>>> (x + y).subs(reps)
6
>>> (x + y).subs(reversed(reps))
x**2 + 2
>>> (x**2 + x**4).subs(x**2, y)
y**2 + y
>>> (x**2 + x**4).xreplace({x**2: y})
x**4 + y
simultaneous參數用法
>>> (x/y).subs([(x, 0), (y, 0)])
0
>>> (x/y).subs([(x, 0), (y, 0)], simultaneous=True)
nan
>>> ((x + y)/y).subs({x + y: y, y: x + y})
1
>>> ((x + y)/y).subs({x + y: y, y: x + y}, simultaneous=True)
y/(x + y)
[subs(*args,?**kwargs)]
示例
后綴表達式計算器,from sympy import symbols
cnt1turn = 3000 / 100
xy1 = (1 * cnt1turn, 100)
xy2 = (30 * cnt1turn, 10)
x = symbols('x')
k1000 = round((xy1[1] - xy2[1]) / (xy1[0] - xy2[0]), 4)
y1000 = k1000 * (x - xy1[0]) + xy1[1]
print(y1000)
103.102 - 0.1034*x
示例:
ai符號庫在哪、In [1]: 1/( (x+2)*(x+1) )
? ? ? ?1
───────────────
(2 + x)*(1 + x)
In [2]: apart(1/( (x+2)*(x+1) ), x)
? 1 ? ? ? 1
───── - ─────
1 + x ? 2 + x
In [3]: (x+1)/(x-1)
-(1 + x)
────────
?1 - x
In [4]: apart((x+1)/(x-1), x)
? ? ? 2
1 - ─────
? ? 1 - x
(相當于展開的逆運算)
計算器符號大全,In [7]: together(1/x + 1/y + 1/z)
x*y + x*z + y*z
───────────────
? ? ?x*y*z
In [8]: together(apart((x+1)/(x-1), x), x)
-1 - x
──────
1 - x
In [9]: together(apart(1/( (x+2)*(x+1) ), x), x)
? ? ? ?1
───────────────
(2 + x)*(1 + x)
在sympy中極限遵循語法limit(function,?variable,?point)?,如計算x->0時f(x)的極限limit(f,?x,?0)。
>>>from sympy import *
>>>x=Symbol("x")
>>>limit(sin(x)/x, x, 0)
1
>>>limit(x, x, oo)
oo
>>>limit(1/x, x, oo)
0
>>>limit(x**x, x, 0)
1
土地利用規劃符號庫,>>> limit(x**3 - 3*x, x, oo)
oo
有一些特殊的極限的例子,你可以閱讀文件test_demidovich.py
可以對任意SymPy表達式微分diff(func,?var)。
>>>from sympy import *
>>>x = Symbol('x')
>>>diff(sin(x), x)
cos(x)
>>>diff(sin(2*x), x)
2*cos(2*x)
>>>diff(tan(x), x)
1 + tan(x)**2
驗證
>>>limit((tan(x+y)-tan(x))/y, y, 0)
1 + tan(x)**2
計算高階微分 diff(func, var, n)?
>>>diff(sin(2*x), x, 1)
2*cos(2*x)
>>>diff(sin(2*x), x, 2)
-4*sin(2*x)
>>>diff(sin(2*x), x, 3)
-8*cos(2*x)
SymPy支持不定積分,超越函數與特殊函數的定積分。SymPy有力的擴展Risch-Norman?算法和模型匹配算法。
三調地類符號庫,>>>from sympy import *
?>>>x, y = symbols('xy')
?初等函數:
?>>>integrate(6*x**5, x)
?x**6
?>>>integrate(sin(x), x)
?-cos(x)
?>>>integrate(log(x), x)
?-x + x*log(x)
?>>>integrate(2*x + sinh(x), x)
?cosh(x) + x**2
?特殊函數:
?>>>integrate(exp(-x**2)*erf(x), x)
?pi**(1/2)*erf(x)**2/4
?定積分:
?>>>integrate(x**3, (x, -1, 1))
?0
?>>>integrate(sin(x), (x, 0, pi/2))
?1
?>>>integrate(cos(x), (x, -pi/2, pi/2))
?2
?一些廣義積分也可以被支持:
?>>>integrate(exp(-x), (x, 0, oo))
?1
?>>>integrate(log(x), (x, 0, 1))
?-1
函數series(var, point, order)
示例1:
?>>>from sympy import *
?>>>x = Symbol('x')
?>>>cos(x).series(x, 0, 10)
?1 - x**2/2 + x**4/24 - x**6/720 + x**8/40320 + O(x**10)
?>>>(1/cos(x)).series(x, 0, 10)
?1 + x**2/2 + 5*x**4/24 + 61*x**6/720 + 277*x**8/8064 + O(x**10)
示例2:
?from sympy import Integral, Symbol, pprint
?x = Symbol("x")
?y = Symbol("y")
?e = 1/(x + y)
?s = e.series(x, 0, 5)
?print(s)
?pprint(s)
?1/y + x**2*y**(-3) + x**4*y**(-5) - x*y**(-2) - x**3*y**(-4) + O(x**5)
? ? ? 2 ? ?4 ? ? ? ? 3
?1 ? x ? ?x ? ?x ? ?x
?─ + ── + ── - ── - ── + O(x**5)
?y ? ?3 ? ?5 ? ?2 ? ?4
? ? ?y ? ?y ? ?y ? ?y
>>>from sympy import Symbol, exp, I
>>>x = Symbol("x")
>>>exp(I*x).expand()
exp(I*x)
>>>exp(I*x).expand(complex=True)
I*exp(-im(x))*sin(re(x)) + cos(re(x))*exp(-im(x))
>>>x = Symbol("x", real=True)
>>>exp(I*x).expand(complex=True)? ? #表達式展開(通過復數形式)
I*sin(x) + cos(x)
In [1]: sin(x+y).expand(trig=True)? ? #表達式展開(通過三角函數形式)
? ? cos(x)*sin(y) + cos(y)*sin(x)
In [2]: cos(x+y).expand(trig=True)
? ? cos(x)*cos(y) - sin(x)*sin(y)
In [3]: sin(I*x)
? ? I*sinh(x)
In [4]: sinh(I*x)
? ? I*sin(x)
In [5]: asinh(I)
π*I
───
?2
In [6]: asinh(I*x)
? ? I*asin(x)
In [15]: sin(x).series(x, 0, 10)
? ? ?3 ? ? 5 ? ? 7 ? ? ? 9
? ? x ? ? x ? ? x ? ? ? x
x - ── + ─── - ──── + ────── + O(x**10)
? ? 6 ? ?120 ? 5040 ? 362880
In [16]: sinh(x).series(x, 0, 10)
? ? ?3 ? ? 5 ? ? 7 ? ? ? 9
? ? x ? ? x ? ? x ? ? ? x
x + ── + ─── + ──── + ────── + O(x**10)
? ? 6 ? ?120 ? 5040 ? 362880
In [17]: asin(x).series(x, 0, 10)
? ? ?3 ? ? ?5 ? ? ?7 ? ? ? 9
? ? x ? ?3*x ? ?5*x ? ?35*x
x + ── + ──── + ──── + ───── + O(x**10)
? ? 6 ? ? 40 ? ?112 ? ? 1152
In [18]: asinh(x).series(x, 0, 10)
? ? ?3 ? ? ?5 ? ? ?7 ? ? ? 9
? ? x ? ?3*x ? ?5*x ? ?35*x
x - ── + ──── - ──── + ───── + O(x**10)
? ? 6 ? ? 40 ? ?112 ? ? 1152
計算器符號表示什么?from sympy.abc import theta, phi
In [2]: Ylm(1, 0, theta, phi)
? ? ?————
╲╱ 3 *cos(θ)
────────────
? ? ? ? ——
? 2*╲╱ π
In [3]: Ylm(1, 1, theta, phi)
? ? —— ? ? ? ? ? ?I*φ
-╲╱ 6 ? *│sin(θ)│*?
────────────────────
? ? ? ? ? ?——
? ? ? 4*╲╱ π
In [4]: Ylm(2, 1, theta, phi)
? ?——— ? ? ? ? ? ? ? ? ?I*φ
-╲╱ 30 ?*│sin(θ)│*cos(θ)*?
────────────────────────────
? ? ? ? ? ? ? ? ——
? ? ? ? ? 4*╲╱ π
In [1]: x = Symbol("x")
In [2]: y = Symbol("y", integer=True)
In [3]: factorial(x)
? ? Γ(1 + x)
In [4]: factorial(y)
? ? y!
In [5]: factorial(x).series(x, 0, 3)
? ? ? ? ? ? ? ? ? ? 2 ? ? ? ? ? 2 ? ?2 ?2
? ? ? ? ? ? ? ? ? ?x *EulerGamma ? ?π *x
1 - x*EulerGamma + ────────────── + ───── + O(x**3)
? ? ? ? ? ? ? ? ? ? ? ? ?2 ? ? ? ? ? ?12
In [18]: zeta(4, x)
Out[18]: ζ(4, x)
In [19]: zeta(4, 1)
Out[19]:
?4
π
──
90
In [20]: zeta(4, 2)
Out[20]:
? ? ? 4
? ? ?π
-1 + ──
? ? ?90
In [21]: zeta(4, 3)
Out[21]:
? ? ? ? 4
? 17 ? π
- ── + ──
? 16 ? 90
計算字符的公式、In [1]: chebyshevt(2, x)
Out[1]:
? ? ? ? 2
-1 + 2*x
In [2]: chebyshevt(4, x)
Out[2]:
? ? ? ?2 ? ? ?4
1 - 8*x ?+ 8*x
In [3]: legendre(2, x)
Out[3]:
? ? ? ? ? 2
? ? ? ?3*x
-1/2 + ────
? ? ? ? 2
In [4]: legendre(8, x)
Out[4]:
? ? ? ? ? ?2 ? ? ? ? 4 ? ? ? ? 6 ? ? ? ? 8
?35 ? 315*x ? ?3465*x ? ?3003*x ? ?6435*x
─── - ────── + ─────── - ─────── + ───────
128 ? ? 32 ? ? ? ?64 ? ? ? ?32 ? ? ? 128
In [5]: assoc_legendre(2, 1, x)
Out[5]:
? ? ? ? ? ? ?—————
? ? ? ? ? ╱ ? ? 2
-3*x*╲╱ ?1 - x
In [6]: assoc_legendre(2, 2, x)
Out[6]:
? ? ? ?2
3 - 3*x
In [7]: hermite(3, x)
Out[7]:
? ? ? ? ? ?3
-12*x + 8*x
皮皮blog
In [4]: f(x).diff(x, x) + f(x) ? ? #注意在使用輸入該命令之前,一定要聲明f=Function('f')
Out[4]:
? ?2
? d
─────(f(x)) + f(x)
dx dx
In [5]: dsolve(f(x).diff(x, x) + f(x), f(x))
Out[5]: C?*sin(x) + C?*cos(x)
運算符號大全。In [7]: solve(x**4 - 1, x)
Out[7]: [i, 1, -1, -i]
In [8]: solve([x + 5*y - 2, -3*x + 6*y - 15], [x, y])
Out[8]: {y: 1, x: -3}
矩陣由矩陣類創立:
>>>from sympy import Matrix
>>>Matrix([[1,0], [0,1]])
[1, 0]
[0, 1]
不只是數值矩陣,亦可為代數矩陣,即矩陣中存在符號:
>>>x = Symbol('x')
>>>y = Symbol('y')
>>>A = Matrix([[1,x], [y,1]])
>>>A
[1, x]
[y, 1]
>>>A**2
[1 + x*y, ? ? 2*x]
[ ? ?2*y, 1 + x*y]
?
使用?.match()方法,引用Wild類,來執行表達式的匹配。該方法會返回一個字典。
>>>from sympy import *
>>>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}
如果匹配不成功,則返回None:
>>>print (x+1).match(p**x)
None
可以使用Wild類的‘exclude’參數(排除參數),排除不需要和無意義的匹配結果,來保證結論中的顯示是唯一的:
>>>x = Symbol('x')
>>>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}
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函數可以輸出不錯的ascii藝術:
>>>from sympy import Integral, pprint
>>>from sympy.abc import x
>>>pprint(x**2) #doctest: +NORMALIZE_WHITESPACE
?2
x
>>>pprint(1/x)
1
-
x
>>>pprint(Integral(x**2, x))
? /
?|
?| ?2
?| x ?dx
?|
/
[Pretty?PrintingWiki]
在python解釋器中,為使pretty?printing為默認輸出,使用:
>>>?from?sympy?import?*
>>>?import?sys
>>>?sys.displayhook?=?pprint
Python?printing
>>>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?printing
>>>from sympy import Integral, latex
>>>from sympy.abc import x
>>>latex(x**2)
$x^{2}$
>>>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)
<apply><power/><ci>x</ci><cn>2</cn></apply>
>>>print mathml(1/x)
<apply><power/><ci>x</ci><cn>-1</cn></apply>
Pyglet
>>>from sympy import Integral, preview
>>>from sympy.abc import x
>>>preview(Integral(x**2, x)) #doctest:+SKIP
用a pyglet window以LaTeX 提供表達式
如果安裝了pyglet,一個呈現LaTeX表達式的窗口會被打開:
注解
Isympy默認調用pprint,所以這就是為什么看到pretty?printing為默認的。
有一個打印的有效模塊,sympy.printing。用這個模塊實現其他的打印:
·?pretty(expr),?pretty_print(expr),?pprint(expr):?分別返回或者輸出,,表達式的漂亮描述。這是相同
·latex(expr),?print_latex(expr):分別返回或者輸出,LaTex描寫的表達式
·mathml(expr),?print_mathml(expr):分別返回或者輸出,MathML描寫的表達式
·print_gtk(expr):?表達式打印到Gtkmathview?,?這是一個GTK小配件顯示MathML代碼。Gtkmathview程序是必須的。
from:Sympy符號計算庫_皮皮blog-CSDN博客_sympy庫
ref: 學習更多的SymPy參考SymPy?User’s?Guide?和?SymPy?Modules?Reference.
公共WIKI(?wiki.sympy.org)有用的例子,教程和代碼。
SymPy簡易教程(一) - [Python]
SymPy簡易教程(二) - [Python]
SymPy簡易教程(三) - [Python]
SymPy簡易教程(四) - [Python]
sympy
SymPy’s documentation!
教程symPy 0.7.2-git documentation
SymPy-符號運算好幫手
SymPy Tutorial(譯)
?SymPy?User’s?Guide,?SymPy?Modules?Reference.
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态