特殊函式



erf() 函式

math 模組中的 erf() 函式返回給定引數 x 處的誤差函式。在數學中,誤差函式(也稱為高斯誤差函式),通常用 erf 表示,是一個復變數的複函式。如果函式引數是實數,則函式值也是實數。

語法

math.erf(x)

引數

  • x − 我們感興趣的數字,找到其誤差函式。

返回值

erf() 函式返回一個浮點數,表示x 處的誤差函式。

示例

from math import erf, pi

x = 1
val = erf(x)
print ("x: ",x, "erf(x): ", val)

x = pi
val = erf(x)
print ("x: ",x, "erf(x): ", val)

x = -12.23
val = erf(x)
print ("x: ",x, "erf(x): ", val)

x = 0
val = erf(x)
print ("x: ",x, "erf(x): ", val)

它將產生以下輸出

x: 1 erf(x): 0.8427007929497149
x: 3.141592653589793 erf(x): 0.9999911238536323
x: -12.23 erf(x): -1.0
x: 0 erf(x): 0.0

erfc() 函式

math 模組中的 erfc() 函式代表互補誤差函式。erf(x) 的值等於 1-erf(x)。此函式用於較大的值,其中從 1 中減去會造成精度損失。

語法

math.erfc(x)

引數

  • x − 任何介於 -Inf 到 Inf 之間的數字。

返回值

erfc() 函式返回一個浮點數,表示x 的互補誤差函式,介於 0 到 2 之間。

示例

from math import erfc, pi

x = 1
val = erfc(x)
print ("x: ",x, "erfc(x): ", val)

x = pi
val = erfc(x)
print ("x: ",x, "erfc(x): ", val)

x = -12.23
val = erfc(x)
print ("x: ",x, "erfc(x): ", val)

x = 0
val = erfc(x)
print ("x: ",x, "erfc(x): ", val)

它將產生以下輸出

x: 1 erfc(x): 0.1572992070502851
x: 3.141592653589793 erfc(x): 8.876146367641612e-06
x: -12.23 erfc(x): 2.0
x: 0 erfc(x): 1.0

gamma() 函式

在數學中,伽馬函式(用 Γ 表示,即希臘字母中的大寫伽馬)是階乘函式到複數的一個常用擴充套件。伽馬函式定義於除非正整數外的所有複數。伽馬函式的數學表示法為 −

$$\Gamma \left ( n \right )=\left ( n-1 \right )!$$

語法

math.gamma(x)

引數

  • x − 用於查詢伽馬函式的數字。應大於等於 1。

返回值

gamma() 函式返回一個浮點值。伽馬值等於 factorial(x-1)。

示例

from math import gamma, pi

x = 6
val = gamma(x)
print ("x: ",x, "gamma(x): ", val)

x = 2
val = gamma(x)
print ("x: ",x, "gamma(x): ", val)

x = 1
val = gamma(x)
print ("x: ",x, "gamma(x): ", val)

x = 0
val = gamma(x)
print ("x: ",x, "gamma(x): ", val)

它將產生以下輸出

x: 12 gamma(x): 120.0
x: 2 gamma(x): 1.0
x: 1 gamma(x): 1.0
Traceback (most recent call last):
   File "C:\Users\mlath\examples\main.py", line 17, in <module>
      val = gamma(x)
            ^^^^^^^^
ValueError: math domain error

lgamma() 函式

lgamma() 函式返回 x 處伽馬函式絕對值的自然對數。

語法

math.lgamma(x)

引數

  • x − 用於 lgamma 計算的浮點值。

返回值

lgamma() 函式返回一個浮點值。

示例

from math import gamma, lgamma, log

x = 6
val = lgamma(x)
print ("x: ",x, "lgamma(x): ", val)
print ("Cross check")
y = log(gamma(x))
print ("lgamma(x) = log(gamma(x))", y )

x = 2
val = lgamma(x)
print ("x: ",x, "lgamma(x): ", val)

x = 1
val = lgamma(x)
print ("x: ",x, "lgamma(x): ", val)

它將產生以下輸出

x: 6 lgamma(x): 4.787491742782047
Cross check
lgamma(x) = log(gamma(x)) 4.787491742782046
x: 2 lgamma(x): 0.0
x: 1 lgamma(x): 0.0
python_maths.htm
廣告