Python STL math&cmath

來源:互聯網
上載者:User

標籤:ack   eal   常用   lin   std   recent   module   truncate   實現   

Python標準庫mathmath所提供的數學常量
pi 數學常量 pi,所屬的變數空間為標準庫math
e 數學常量 e,e即自然常數,所屬的變數空間為標準庫math
math庫中常用的函數三角函數
函數名 格式 功能
sin sin(x) 返回的x弧度的正弦值
cos cos(x) 返回的x弧度的餘弦值
tan tan(x) 返回的x弧度的正切值
asin asin(x) 返回x反正弦弧度值
acos acos(x) 返回x反餘弦弧度值
atan atan(x) 返回x反正切弧度值
atan2 atan2(x,y) 返回給定的 XY座標值的反正切值。
degrees degrees(x) degrees() 將弧度x轉換為角度。
radians radians(x) 返回一個角度弧度值
數值函數
函數名 格式 功能
exp exp( x ) 返回x的指數\(e^x\)。
pow pow(x,y) pow() 方法返回(x的y次方) 的值。
sqrt sqrt(x) sqrt() 方法返回數字x的平方根。
hypot hypot(x, y) hypot() 返回歐幾裡德範數 sqrt(x*x + y*y)
modf modf(x) modf()方法返回x的整數部分小數部分
fabs fabs(x) fabs()方法返回數字x的絕對值
log log(x) log() 方法返回x自然對數
log10 log10(x) log10()方法返回以10為基數的x對數。
copysign copysign(x,y) 返回模為|x|,符號為sign(y)的數值
factorial factorial(x) 計算x!
fmod fmod(x,y) x對y求模,實現是以C庫為底,返回更為精確的浮點數
frexp frexp(x) 返回一個二元組,分別是x的指數部分尾數部分 (m, e)
x == m * 2**e
fsum fsum(x) 返回一個求和後得到的浮點數
其他的函數
  • math.isinf(x)

x 是不是正負無窮大.

  • math.isnan(x)

x是不是NaN(不是一個數字),

  • math.ldexp(x, i)

Return x * (2**i). 和frexp()相反.

  • math.trunc(x)

Return x的整數部分(truncated 截斷)int(x)

  • math.expm1(x)

Return e**x - 1. x非常小的時候使用,會更精確。

  • math.log1p(x)

Return log(1+x)以e為底,x近似於0時更準確

(雙曲線方法)Hyperbolic functions
  • math.acosh(x)

Return the inverse hyperbolic cosine of x.

  • math.asinh(x)

Return the inverse hyperbolic sine of x.

  • math.atanh(x)

Return the inverse hyperbolic tangent of x.

  • math.cosh(x)

Return the hyperbolic cosine of x.

  • math.sinh(x)

Return the hyperbolic sine of x.

  • math.tanh(x)

Return the hyperbolic tangent of x.

(特殊方法)Special functions
  • math.erf(x)

  • math.erfc(x)

  • math.gamma(x)

  • math.lgamma(x)

Python標準庫cmath

標準庫math缺乏對於複數的操作,這時候需要引入另外的一個Python內建的標準庫cmath。

如下:

>>> import math  >>> math.sqrt(-1)  Traceback (most recent call last):    File "<stdin>", line 1, in <module>  ValueError: math domain error  >>> import cmath  >>> cmath.sqrt(-1)  1j  

可以看到math.sqrt傳入的參數為負數時,因為操作範圍在實數會報錯;cmath.sqrt操作範圍在複數所以傳入參數為-1返回一個虛數。

cmath擁有與math相同絕大多數函數,只是操作範圍在複數域內;以下仍有幾個函數值得記憶:

  • cmath.phase(x) :

equal to math.atan2(x.imag, x.real).

  • cmath.polar(x) :

equal to (abs(x), phase(x)).

  • cmath.rect(r, phi):

equal to r * (math.cos(phi) + math.sin(phi)*1j).

Return (x.imag+x.real*j)

Python STL math&cmath

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.