溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python中math模塊和cmath模塊怎么應(yīng)用

發(fā)布時(shí)間:2022-01-24 16:11:07 來源:億速云 閱讀:204 作者:zzz 欄目:開發(fā)技術(shù)

這篇文章主要介紹了Python中math模塊和cmath模塊怎么應(yīng)用的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Python中math模塊和cmath模塊怎么應(yīng)用文章都會(huì)有所收獲,下面我們一起來看看吧。

Python中math模塊和cmath模塊怎么應(yīng)用

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']
>>>

各個(gè)函數(shù)解析: Python中math模塊和cmath模塊怎么應(yīng)用

Python 復(fù)數(shù)的支持和 cmath 模塊應(yīng)用

Python 提供對(duì)于復(fù)數(shù)運(yùn)算的支持,復(fù)數(shù)在 Python 中的表達(dá)式為 C==c.real+c.imag*j,復(fù)數(shù) C 由他的實(shí)部和虛部組成。

對(duì)于復(fù)數(shù),Python 支持它的加減乘除運(yùn)算,同時(shí)提供了 cmath 模塊對(duì)其他復(fù)雜運(yùn)算進(jìn)行支持。cmath 模塊和 Python 中的 math 模塊對(duì)應(yīng), math提供對(duì)于實(shí)數(shù)的支持, 在這里主要討論 cmath 模塊中的幾個(gè)函數(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() 方法可以對(duì)復(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ù)對(duì)一個(gè)輸入的笛卡爾形勢(shì)的復(fù)數(shù)進(jìn)行計(jì)算,輸出為一個(gè)二元組,第一個(gè)值為Z的模值, 第二個(gè)為幅度值。 rect() 函數(shù)對(duì)輸入的模和幅度值進(jìn)行計(jì)算輸出笛卡爾表示。

如果需要單獨(dú)對(duì)一個(gè)復(fù)數(shù)進(jìn)行幅度值的求解,可以調(diào)用 cmath.phrase(x) 函數(shù),返回幅度值。

2.復(fù)數(shù)的冪指數(shù)與對(duì)數(shù)函數(shù)

復(fù)數(shù)的指數(shù)函數(shù)為 cmath.exp(x), 用來求解 e^x 表達(dá)式。

cmath.log(x[,base]) 用來求以 Base 為底的 x 的對(duì)數(shù)。

cmath.log10(x) 用來求以 10 為底 x 的對(duì)數(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)值, 表示自然對(duì)數(shù)的底

關(guān)于“Python中math模塊和cmath模塊怎么應(yīng)用”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Python中math模塊和cmath模塊怎么應(yīng)用”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI