#範例4-2:複數系統cmath的應用:三種坐標轉換(複數坐標,極坐標,直角坐標) #(1).Python的兩個內建數學模組: math, cmath # math 模組:適合處理非向量運算,且運算效能較好,但math 模組只能處理實數運算 #cmath模組: 專門用來處理複數運算 #(2).cmath 模組的函數 : #cmath模組的運算元是複數, 常數增加了複數的 infj 與 nanj, 方法則大部分與 math 相同, 不過沒有階乘函數 (因為複數沒有階乘), #新增三個複數座標轉換的方法(在卡式座標與極座標之間轉換): #polar(x) 傳回複數 x 的極座標表示法元組 (r, p), r=長度, p=角度 #rect(r, p) 傳回極座標 (r, p) 的複數 #phase(x) 傳回複數 x 的弧度 (radian) import cmath, math print('全部cmath的指令 = ', dir(cmath)) #已經複數,傳回弧度phase(theda) d1 = cmath.phase(5+5j) print('已經複數,傳回弧度theda=', d1) print('再轉成角度=', math.degrees(d1) ) #已經複數,轉成極座標:polar(a+bi) =》 p(r,theda) print('複數轉極座標(r,theda)=', cmath.polar(5+5j)) #已經極座標,轉成直角坐標:p(r,theda) => rect(x,y) print('極座標轉直角坐標:p(r,theda) => rect(x,y)=',cmath.rect(7.0710678118654755, 0.7853981633974483)) #求直角坐標(1,1)的複數 print('求直角坐標(1,1)的複數=',complex(1.0,1.0)) #求直角坐標(1,1)的弧度 print('求直角坐標(1,1)的弧度=', cmath.phase(complex(1.0, 1.0))) #求直角坐標(1,1)的角度 print('求直角坐標(1,1)的角度=', math.degrees(cmath.phase(complex(1.0, 1.0)))) #求直角坐標(1,1)的極坐標 print('求直角坐標(1,1)的極坐標p(r,theda)=',cmath.polar(complex(1.0, 1.0))) #cmath.polar() 與 cmath.rect() 在坐標轉換時,非常實用