python math.gamma()方法,获取伽玛函数值
math.gamma()方法
python模块math中的gamma()方法,可以获取参数指定的值的伽玛函数值。而什么是伽玛函数呢?据资料,伽玛函数,也叫欧拉第二积分,是阶乘函数在实数与复数上扩展的一类函数,在分析学、概率论、偏微分方程和组合数学中有重要的应用。
语法
gamma(x, /)
python源码中对gamma()方法的介绍:
Gamma function at x.
参数
x参数为real number,比如int或float,且大于0,如果非real number,python会抛出TypeError,如果小于等于0,python会抛出valueError。
返回值
>>> import math
>>> math.gamma(1)
1.0
>>> math.gamma(1.5)
0.886226925452758
>>> math.gamma('2') #参数为字符串
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be real number, not str
>>> math.gamma(0) #参数等于0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: math domain error
>>> math.gamma(-1) #参数小于0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: math domain error
>>> math.gamma(1+2j) #参数为复数complex
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't convert complex to float