ncuma_SymPy符號運算套件.pptx

NCU MCL
NCU MCLSoftware Developer at NCU MCL
1
SymPy 符號運算套件
簡要 python 學習講義
SymPy 符號運算套件
 符號運算 Python 套件,始於 2007 年
 為計算機代數系統(Computer Algebra
System),如 Maple、Mathematica、
MathCad
 套件使用 Python 程式語言開發出來
2
國立中央大學數學系
使用 sympy 套件
 import sympy :
使用 sympy 套件內定義的名稱之前需加上套件
名稱,即 sympy.
 from sympy import * :
直接使用 sympy 套件內定義的所有名稱
3
國立中央大學數學系
 為節省列印空間,以下皆使用 from sympy import *
定義符號變數:symbols()
4
國立中央大學數學系
>>> foo = symbols(’t’) # 設定 foo 符號變數代表符號 t
>>> foo
t
>>> a , b = symbols(’x y’) # 設定 a b 分別代表 x 與 y 兩個符號
# ’x y’ 也可加逗號寫成 ’x, y’
>>> a
x
>>> foo , a , b # 輸出 foo , a , b 三個符號變數
(t, x, y)
>>> foo - 2*a + 3*a # 簡單符號運算
t + x
>>> fn = a + 2*sin(foo) # 定義新符號變數 fn
>>> fn # 輸出 fn
x + 2*sin(t)
預設符號變數:var()
5
國立中央大學數學系
>>> x , y = symbols(”x, y”) # 設定 x 與 y 變數代表 x 與 y 兩符號
>>> var(”x y”) # 效果同上
(x, y)
>>> x + 2*y - 3*x
-2*x + 2*y
>>> w , t = var(”a, b”) # 也可如 symbols 一樣設定符號變數
>>> w + 3*t
a + 3*b
設定多個符號變數:使用「:」範圍
6
國立中央大學數學系
>>> var(”x:3”) # 三個符號變數:x0,x1,x2
(x0, x1, x2)
>>> var(”x:z”) # 三個符號變數:x,y,z
(x, y, z)
>>> var(”x:z:2”) # 六個符號變數,以上每個再依次
(x0, x1, y0, y1, z0, z1) 新增 0 1 兩個
展開與取代:展開 expand() 與取代 subs()
7
國立中央大學數學系
>>> var("x,y")
(x, y)
>>> ((x+2*y)**2).expand() # expand() 展開
x**2 + 4*x*y + 4*y**2
>>> expand((x+2*y)**2) # 同上
x**2 + 4*x*y + 4*y**2
>>> (x-y)**2).subs(x,1) # subs() :x 用 1 取代
(-y + 1)**2
>>> ((x+y)**2).subs(x,-2*y) # x 用 -2y 取代
y**2
>>> ((x-y)**2).subs(x,1).expand() # 先取代再展開
y**2 - 2*y + 1
因式分解:factor()
8
國立中央大學數學系
>>> ((x**2-2*x+1)).factor() # 因式乘積
(x - 1)**2
>>> ((x-1)**2).expand().factor() # 先展開再回復
(x - 1)**2
>>> (x**2-1)/(x-1)
(x**2 - 1)/(x - 1)
>>> fn = x**3 - 4*x - 1 # 定義 fn gn 兩式子
>>> gn = -2*x**2 + x + 5
>>> factor(fn-gn) # 因式分解
(x - 2)*(x + 1)*(x + 3)
簡化運算式:simplify()
9
國立中央大學數學系
>>> ((x**2-1)/(x-1)).simplify() # 簡化
x + 1
>>> simplify((x**2-1)/(x-1)) # 同上
x + 1
>>> fn = cos(x)**2 - sin(x)**2 # 定義 fn gn 兩式子
>>> gn = sin(2*x)
>>> fn/gn # 式子相除
(cos(x)**2 - sin(x)**2)/sin(2*x)
>>> simplify(fn/gn) # 簡化運算
1/tan(2*x)
>>> (gn**2/fn).simplify() # 簡化運算
sin(2*x)**2/cos(2*x)
Rational(a,b) :b/a 分數
10
國立中央大學數學系
>>> 1/3 # 浮點數
0.3333333333333333
>>> Rational(2,6) # 自動約分
1/3
>>> a = Rational(8)/3 # 等同設定 a = Rational(8,3)
>>> a**2
64/9
>>> # y 用 1/3 浮點數取代
>>> ((x-y)**2).subs(y,1/3)
(x - 0.333333333333333)**2
>>> # y 用分數 1/3 取代
>>> ((x-y)**2).subs(y,Rational(1,3))
(x - 1/3)**2
>>> ((x-y)**2).subs(y,Rational(1,3)).expand()
x**2 - 2*x/3 + 1/9
π,e,∞
11
國立中央大學數學系
>>> pi # 圓周率符號變數
pi
>>> E # 大寫 E 代表 Euler 數
E
>>> oo # 兩個小 o,無限大符號變數
oo
>> oo * oo # 無限大相乘
oo
>> oo - oo # 無限大相減,無定義
nan # nan 代表 not a number
常用函數
12
國立中央大學數學系
evalf :計算式子數值
13
國立中央大學數學系
>>> pi.evalf()
3.14159265358979
>>> x + Rational(1,2)*pi
x + pi/2
>>> (x + Rational(1,2)*pi).evalf()
x + 1.5707963267949
>>> asin(1)
pi/2
>>> asin(1).evalf()
1.57079632679490
微積分的應用
 limit():極限
 diff()、Derivative():微分
 integrate()、Integral():積分
 series():級數展開
14
國立中央大學數學系
 一般極限:
15
國立中央大學數學系
>>> ((x**2-1)/(x+1)).subs(x,-1)
nan
>>> limit( (x**2-1)/(x+1) , x , -1 )
-2
>>> ((x**2-1)/(x+1)).limit(x,-1) # 同上
-2
limit():極限 (一)
 單邊極限:
16
國立中央大學數學系
>>> limit( floor(x)/x , x , 3 )
1
>>> limit( floor(x)/x , x , 3 , ’+’ ) # 同上
1
>>> limit( floor(x)/x , x , 3 , ’-’ )
2/3
>>> limit( sin(1/x) , x , 0 , ’+’ )
AccumBounds(-1,1) # 在 [-1,1] 之間
>>> limit( sqrt(x)*sin(1/x) , x , 0 , ’+’ )
limit():極限 (二)
 無窮極限:
17
國立中央大學數學系
limit():極限 (三)
>>> limit( (x**3-4)/(2*abs(x)**3+2) , x , oo )
1/2
>>> limit( (x**3-4)/(2*abs(x)**3+2) , x , -oo )
-1/2
>>> limit( sin(1/x) , x , oo )
0
>>> fn = x*sin(1/x)
>>> limit( fn , x , oo )
1
 Limit() 與 limit()
Limit() 為 limit() 的未運算版
在 init_printing() 下,Limit() 可印出漂亮
的輸出內容
Limit() 與 doit() 配合效果等同 limit()
18
國立中央大學數學系
limit():極限 (四)
19
國立中央大學數學系
limit():極限 (五)
 pprint() 與 pretty() :漂亮列印,漂亮字串
 在程式中,可使用 pprint() 產生漂亮的輸出
 pretty 可將 pprint() 的漂亮輸出存成字串
 pprint() 或列印 pretty 字串最好由新列起始,
否則會有對齊不良的情況發生
20
國立中央大學數學系
limit():極限 (六)
21
國立中央大學數學系
limit():極限 (七)
from sympy import *
init_printing()
var("x")
fn = x + floor(x) # fn = x +
for i in range(2) :
y1 = Limit( fn , x , i , ’-’ )
y2 = Limit( fn , x , i , ’+’ )
pprint(y1) # 漂亮列印 y1
print( ’=’ , y1.doit() , end="nn" )
pprint(y2) # 漂亮列印 y2
print( ’=’ , y2.doit() , end="nn" )
y3 = Limit( fn , x , i+Rational(1,2) )
pprint(y3) # 漂亮列印 y3
print( ’=’ , y3.doit() , end="nn" )
22
國立中央大學數學系
limit():極限 (八)
 pprint() 函式也產生微分,積分等函式的漂亮輸出
diff():微分 (一)
 單變數函式:
23
國立中央大學數學系
>>> fn = x**3 + 2*x**2 + 1
>>> diff(fn) # 計算一次微分
2
3·x + 4·x
>>> diff(fn,x) # 同上
2
3·x + 4·x
>>> diff(fn,x,1) # 同上,1 代表一次微分
2
3·x + 4·x
>>> diff(fn,x,2) # 函式 fn 對 x 連續執行兩次微分
2·(3·x + 2)
>>> diff(fn,x,x) # 同上
2·(3·x + 2)
>>> diff(fn,x,3) # 三次微分
6
>>> diff(fn,x,x,x) # 同上
6
diff():微分 (二)
 多變數函式:偏微分
24
國立中央大學數學系
>>> var(”x,y”) # 定義兩符號變數
>>> fn = x**3 + y**3 + x*y
>>> fn
3 3
x + x·y + y
>>> diff(fn,x) # fn 對 x 微分
2
3·x + y
>>> diff(fn,x,y) # fn 先對 x 微分,再對 y 微分
2
x + 3·y
>>> diff(fn,x,2,y) # fn 先對 x 兩次微分,再對 y 微分
0
>>> diff(fn,x,x,y) # 同上
0
diff():微分 (三)
 Derivative:顯示漂亮微分式子
Derivative 在互動模式下可將微分式子以漂亮方式
顯示出來
在程式執行中則需使用 pprint() 才能印出漂亮式子
Derivative 僅印出微分式子,並不會如 diff 一樣
計算微分
Derivative 後執行 doit() 等同 diff
25
國立中央大學數學系
diff():微分 (四)
 範例一:計算函式對 x 的前四次偏微分
26
國立中央大學數學系
from sympy import *
init_printing()
var("x,y")
fn = cos(x*y) + sin(y)
# 顯示與計算 fn 函數對 x 的一到四次偏微分
for i in range(4) :
# dfn 儲存微分的漂亮輸出
dfn = Derivative(fn,x,i+1)
# 漂亮列印微分式子
pprint( dfn )
print( ’=’ , dfn.doit() ) # dfn.doit() 等同 diff(fn,x,i)
print()
輸出見次頁。
diff():微分 (五)
27
國立中央大學數學系
diff():微分 (六)
 範例二:計算函數的所有二次偏微分
28
國立中央大學數學系
from sympy import *
init_printing()
var("x,y")
# 定義函數
fn = cos(x*y) + sin(y)
# v1 在 x , y 兩符號變數迭代
for v1 in [ x , y ] :
# v2 在 x , y 兩符號變數迭代
for v2 in [ x , y ] :
# fn 先對 v1 微分,再對 v2 微分
dfn = Derivative(fn,v1,v2)
# 漂亮列印 fn 的微分式子
pprint( dfn )
# 印出 dfn 的微分運算結果
print( ’= ’ , dfn.doit() )
print()
integrate():積分 (一)
 單變數積分
 不定積分
29
國立中央大學數學系
integrate():積分 (二)
 定積分
30
國立中央大學數學系
integrate():積分 (三)
 多變數積分
 不定積分
31
國立中央大學數學系
integrate():積分 (四)
 定積分
32
國立中央大學數學系
integrate():積分 (五)
 Integral:顯示漂亮積分式子
 Integral 在互動模式下可將積分式子以漂亮方式顯示出來
 在程式執行中則需使用 pprint() 才能印出漂亮式子
 Integral 僅印出積分式子,並不會如 integrate 一樣
計算積分
 Integral 後執行 doit() 等同 integrate
33
國立中央大學數學系
integrate():積分 (六)
34
國立中央大學數學系
from sympy import *
init_printing()
var("x")
fn = x**2*cos(x)
# 列印連續三次積分過程
for i in range(3) :
ifn = Integral(fn,x) # 儲存漂亮積分式子
fn = ifn.doit() # 執行積分並變更 fn 為積分後函式
pprint( ifn ) # 列印漂亮積分式子
print( ’=’ )
pprint( fn ) # 列印漂亮 fn 函式
print()
integrate():積分 (七)
輸出:
35
國立中央大學數學系
series:級數展開 (一)
 泰勒展開式
36
國立中央大學數學系
>>> sin(x).series() # 預設到 O(x**6) 誤差
x - x**3/6 + x**5/120 + O(x**6)
>>> series(sin(x)) # 同上
x - x**3/6 + x**5/120 + O(x**6)
>>> sin(x).series(x,0,4) # 展開誤差到 O(x**4)
x - x**3/6 + O(x**4)
>>> sin(x).series(x,0,5) # 展開誤差到 O(x**5)
x - x**3/6 + O(x**5)
>>> sin(x).series(x,0,5).removeO() # 去除誤差項
-x**3/6 + x
series:級數展開 (二)
 Maclaurin 展開式:對 x = x0 展開
37
國立中央大學數學系
>>> cos(x-2).series(x,x0=2) # 對 x0 展開
1 - (x - 2)**2/2 + (x - 2)**4/24 + O((x - 2)**6, (x, 2))
>>> cos(x-2).series(x,x0=2,n=6).removeO() # 展開誤差到
(x - 2)**4/24 - (x - 2)**2/2 + 1 O(x**6)
1 of 37

Recommended

SymPy在微積分上的應用.ppt by
SymPy在微積分上的應用.pptSymPy在微積分上的應用.ppt
SymPy在微積分上的應用.pptmclmath
424 views41 slides
Sym py edu by
Sym py eduSym py edu
Sym py eduAlisha Smile
4.6K views39 slides
SymPy在微積分上的應用:範例.pptx by
SymPy在微積分上的應用:範例.pptxSymPy在微積分上的應用:範例.pptx
SymPy在微積分上的應用:範例.pptxmclmath
910 views7 slides
Ch9 by
Ch9Ch9
Ch9Alisha Smile
131 views77 slides
Ch9 教學 by
Ch9 教學Ch9 教學
Ch9 教學hungchiayang1
467 views32 slides
函數微分_範例.pptx by
函數微分_範例.pptx函數微分_範例.pptx
函數微分_範例.pptxmclmath
40 views5 slides

More Related Content

Similar to ncuma_SymPy符號運算套件.pptx

Ppt 1-50 by
Ppt 1-50Ppt 1-50
Ppt 1-50hungchiayang1
1.6K views50 slides
Ppt 138-142 by
Ppt 138-142Ppt 138-142
Ppt 138-142hungchiayang1
8.4K views5 slides
实验一 Mathematica软件简介 by
实验一   Mathematica软件简介实验一   Mathematica软件简介
实验一 Mathematica软件简介Xin Zheng
1.7K views12 slides
实验一 Mathematica软件简介 by
实验一   Mathematica软件简介实验一   Mathematica软件简介
实验一 Mathematica软件简介guestfe33f0e
1.2K views12 slides
Ch5 by
Ch5Ch5
Ch5Alisha Smile
85 views81 slides
Python入門:5大概念初心者必備 2021/11/18 by
Python入門:5大概念初心者必備 2021/11/18Python入門:5大概念初心者必備 2021/11/18
Python入門:5大概念初心者必備 2021/11/18Derek Lee
227 views72 slides

Similar to ncuma_SymPy符號運算套件.pptx(20)

实验一 Mathematica软件简介 by Xin Zheng
实验一   Mathematica软件简介实验一   Mathematica软件简介
实验一 Mathematica软件简介
Xin Zheng1.7K views
实验一 Mathematica软件简介 by guestfe33f0e
实验一   Mathematica软件简介实验一   Mathematica软件简介
实验一 Mathematica软件简介
guestfe33f0e1.2K views
Python入門:5大概念初心者必備 2021/11/18 by Derek Lee
Python入門:5大概念初心者必備 2021/11/18Python入門:5大概念初心者必備 2021/11/18
Python入門:5大概念初心者必備 2021/11/18
Derek Lee227 views
函數畫圖.pptx by mclmath
函數畫圖.pptx函數畫圖.pptx
函數畫圖.pptx
mclmath180 views
ncuma_函數畫圖.pptx by NCU MCL
ncuma_函數畫圖.pptxncuma_函數畫圖.pptx
ncuma_函數畫圖.pptx
NCU MCL4.4K views
ncuma_邏輯與迴圈.pptx by NCU MCL
ncuma_邏輯與迴圈.pptxncuma_邏輯與迴圈.pptx
ncuma_邏輯與迴圈.pptx
NCU MCL657 views
Ihome inaction 篇外篇之fp介绍 by dennis zhuang
Ihome inaction 篇外篇之fp介绍Ihome inaction 篇外篇之fp介绍
Ihome inaction 篇外篇之fp介绍
dennis zhuang712 views
第2章符 号 运 算 by eterou
第2章符 号 运 算第2章符 号 运 算
第2章符 号 运 算
eterou1.1K views
A Tour of Go 學習筆記 by 昕暐 黃
A Tour of Go 學習筆記A Tour of Go 學習筆記
A Tour of Go 學習筆記
昕暐 黃1.6K views
C程式-函式與巨集 by 艾鍗科技
C程式-函式與巨集C程式-函式與巨集
C程式-函式與巨集
艾鍗科技42.6K views

More from NCU MCL

函數畫圖_習題4.pptx by
函數畫圖_習題4.pptx函數畫圖_習題4.pptx
函數畫圖_習題4.pptxNCU MCL
402 views1 slide
數值積分法_3.pptx by
數值積分法_3.pptx數值積分法_3.pptx
數值積分法_3.pptxNCU MCL
1.2K views1 slide
數值積分法_2.pptx by
數值積分法_2.pptx數值積分法_2.pptx
數值積分法_2.pptxNCU MCL
64 views1 slide
數值積分法_1.pptx by
數值積分法_1.pptx數值積分法_1.pptx
數值積分法_1.pptxNCU MCL
74 views1 slide
數值求根習題_1.pptx by
數值求根習題_1.pptx數值求根習題_1.pptx
數值求根習題_1.pptxNCU MCL
89 views1 slide
函數微分習題_3.pptx by
函數微分習題_3.pptx函數微分習題_3.pptx
函數微分習題_3.pptxNCU MCL
539 views3 slides

More from NCU MCL(20)

函數畫圖_習題4.pptx by NCU MCL
函數畫圖_習題4.pptx函數畫圖_習題4.pptx
函數畫圖_習題4.pptx
NCU MCL402 views
數值積分法_3.pptx by NCU MCL
數值積分法_3.pptx數值積分法_3.pptx
數值積分法_3.pptx
NCU MCL1.2K views
數值積分法_2.pptx by NCU MCL
數值積分法_2.pptx數值積分法_2.pptx
數值積分法_2.pptx
NCU MCL64 views
數值積分法_1.pptx by NCU MCL
數值積分法_1.pptx數值積分法_1.pptx
數值積分法_1.pptx
NCU MCL74 views
數值求根習題_1.pptx by NCU MCL
數值求根習題_1.pptx數值求根習題_1.pptx
數值求根習題_1.pptx
NCU MCL89 views
函數微分習題_3.pptx by NCU MCL
函數微分習題_3.pptx函數微分習題_3.pptx
函數微分習題_3.pptx
NCU MCL539 views
SymPy 在微積分上的應用_3.pptx by NCU MCL
SymPy 在微積分上的應用_3.pptxSymPy 在微積分上的應用_3.pptx
SymPy 在微積分上的應用_3.pptx
NCU MCL28 views
SymPy 在微積分上的應用_2.pptx by NCU MCL
SymPy 在微積分上的應用_2.pptxSymPy 在微積分上的應用_2.pptx
SymPy 在微積分上的應用_2.pptx
NCU MCL27 views
SymPy 在微積分上的應用_1.pptx by NCU MCL
SymPy 在微積分上的應用_1.pptxSymPy 在微積分上的應用_1.pptx
SymPy 在微積分上的應用_1.pptx
NCU MCL32 views
極座標畫圖_3.pptx by NCU MCL
極座標畫圖_3.pptx極座標畫圖_3.pptx
極座標畫圖_3.pptx
NCU MCL12 views
極座標畫圖_2.pptx by NCU MCL
極座標畫圖_2.pptx極座標畫圖_2.pptx
極座標畫圖_2.pptx
NCU MCL15 views
極座標畫圖_1.pptx by NCU MCL
極座標畫圖_1.pptx極座標畫圖_1.pptx
極座標畫圖_1.pptx
NCU MCL12 views
Taylor 多項式_3.pptx by NCU MCL
Taylor 多項式_3.pptxTaylor 多項式_3.pptx
Taylor 多項式_3.pptx
NCU MCL8 views
Taylor 多項式_2.pptx by NCU MCL
Taylor 多項式_2.pptxTaylor 多項式_2.pptx
Taylor 多項式_2.pptx
NCU MCL11 views
Taylor 多項式_1.pptx by NCU MCL
Taylor 多項式_1.pptxTaylor 多項式_1.pptx
Taylor 多項式_1.pptx
NCU MCL9 views
微分方程式求解_3.pptx by NCU MCL
微分方程式求解_3.pptx微分方程式求解_3.pptx
微分方程式求解_3.pptx
NCU MCL53 views
微分方程式求解_2.pptx by NCU MCL
微分方程式求解_2.pptx微分方程式求解_2.pptx
微分方程式求解_2.pptx
NCU MCL30 views
微分方程式求解_1.pptx by NCU MCL
微分方程式求解_1.pptx微分方程式求解_1.pptx
微分方程式求解_1.pptx
NCU MCL28 views
牛頓迭代法_3.pptx by NCU MCL
牛頓迭代法_3.pptx牛頓迭代法_3.pptx
牛頓迭代法_3.pptx
NCU MCL18 views
牛頓迭代法_2.pptx by NCU MCL
牛頓迭代法_2.pptx牛頓迭代法_2.pptx
牛頓迭代法_2.pptx
NCU MCL17 views

ncuma_SymPy符號運算套件.pptx