新聞中心
Python math 模塊提供了許多對浮點(diǎn)數(shù)的數(shù)學(xué)運(yùn)算函數(shù)。Python cmath 模塊包含了一些用于復(fù)數(shù)運(yùn)算的函數(shù)。

鼓樓網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。成都創(chuàng)新互聯(lián)公司于2013年創(chuàng)立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)公司。
cmath 模塊的函數(shù)跟 math 模塊函數(shù)基本一致,區(qū)別是 cmath 模塊運(yùn)算的是復(fù)數(shù),math 模塊運(yùn)算的是數(shù)學(xué)運(yùn)算。
要使用 math 或 cmath 函數(shù)必須先導(dǎo)入:
import math
查看 math 查看包中的內(nèi)容:
>>> import math
>>> dir(math)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
>>>
各個函數(shù)解析:
Python 復(fù)數(shù)的支持和 cmath 模塊應(yīng)用
Python 提供對于復(fù)數(shù)運(yùn)算的支持,復(fù)數(shù)在 Python 中的表達(dá)式為 C==c.real+c.imag*j,復(fù)數(shù) C 由他的實(shí)部和虛部組成。
對于復(fù)數(shù),Python 支持它的加減乘除運(yùn)算,同時提供了 cmath 模塊對其他復(fù)雜運(yùn)算進(jìn)行支持。cmath 模塊和 Python 中的 math 模塊對應(yīng), math提供對于實(shí)數(shù)的支持, 在這里主要討論 cmath 模塊中的幾個函數(shù)的用法。
查看 cmath 查看包中的內(nèi)容
>>> import cmath
>>> dir(cmath)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh', 'cos', 'cosh', 'e', 'exp', 'inf', 'infj', 'isclose', 'isfinite', 'isinf', 'isnan', 'log', 'log10', 'nan', 'nanj', 'phase', 'pi', 'polar', 'rect', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau']
>>>
1.極坐標(biāo)和笛卡爾坐標(biāo)表示的轉(zhuǎn)換。
C==c.real+c.imag*j 的復(fù)數(shù)表示方法為復(fù)數(shù)的笛卡爾表示法, cmath 模塊中的 polar() 方法和 rect() 方法可以對復(fù)數(shù)進(jìn)行極坐標(biāo)表示和笛卡爾表示方法的轉(zhuǎn)換。 例:
>>> import cmath
>>> Z=1+2j
>>> print cmath.polar(Z)
(2.23606797749979, 1.1071487177940904)
>>> a,b=cmath.polar(Z)
>>> print cmath.rect(a,b)
(1+2j)
>>>
polar 函數(shù)對一個輸入的笛卡爾形勢的復(fù)數(shù)進(jìn)行計(jì)算,輸出為一個二元組,第一個值為Z的模值, 第二個為幅度值。 rect() 函數(shù)對輸入的模和幅度值進(jìn)行計(jì)算輸出笛卡爾表示。
如果需要單獨(dú)對一個復(fù)數(shù)進(jìn)行幅度值的求解,可以調(diào)用 cmath.phrase(x) 函數(shù),返回幅度值。
2.復(fù)數(shù)的冪指數(shù)與對數(shù)函數(shù)
復(fù)數(shù)的指數(shù)函數(shù)為 cmath.exp(x), 用來求解 e^x 表達(dá)式。
cmath.log(x[,base]) 用來求以 Base 為底的 x 的對數(shù)。
cmath.log10(x) 用來求以 10 為底 x 的對數(shù)
cmath.sqrt(x) 用來求 x 的平方根。
3.復(fù)數(shù)的三角函數(shù)方程
包括所有的三角函數(shù)計(jì)算 acos(x) asin(x) atan(x) sin(x) cos(x) tan(x)。
4. 參數(shù)類判斷
cmath.isinf(x) 如果x的實(shí)部或者虛部為無窮大,則返回true。
cmath.isnan(x)如果x的實(shí)部或者虛步不是數(shù)字則返回true。
5. 常量支持
cmat.pi 浮點(diǎn)值, 表示圓周率的大小
cmat.e 浮點(diǎn)值, 表示自然對數(shù)的底
網(wǎng)站標(biāo)題:Pythonmath與cmath講解
URL分享:http://fisionsoft.com.cn/article/dpcecji.html


咨詢
建站咨詢
