Python 数学特殊函数
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()函数返回一个浮点值。伽马值等于阶乘(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处Gamma函数绝对值的自然对数。
语法
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
版权声明:本页面内容旨在传播知识,为用户自行发布,若有侵权等问题请及时与本网联系,我们将第一时间处理。E-mail:284563525@qq.com