scipy.special.gamma#

scipy.special.gamma(z, out=None) = <ufunc 'gamma'>#

gamma 函数。

伽玛函数定义为

\[\Gamma(z) = \int_0^\infty t^{z-1} e^{-t} dt\]

对于 \(\Re(z) > 0\),并且通过解析延拓扩展到复平面的其余部分。更多细节请参见 [dlmf]

参数:
zarray_like

实数或复数值参数

ndarray,可选

函数值的可选输出数组

返回:
标量或ndarray

伽马函数的值

注释

伽马函数常被称为广义阶乘,因为对于自然数 \(n\),有 \(\Gamma(n + 1) = n!\)。更一般地,它满足递归关系 \(\Gamma(z + 1) = z \cdot \Gamma(z)\) 对于复数 \(z\),结合 \(\Gamma(1) = 1\) 的事实,可以推导出上述对于 \(z = n\) 的恒等式。

参考文献

[dlmf]

NIST 数学函数数字图书馆 https://dlmf.nist.gov/5.2#E1

示例

>>> import numpy as np
>>> from scipy.special import gamma, factorial
>>> gamma([0, 0.5, 1, 5])
array([         inf,   1.77245385,   1.        ,  24.        ])
>>> z = 2.5 + 1j
>>> gamma(z)
(0.77476210455108352+0.70763120437959293j)
>>> gamma(z+1), z*gamma(z)  # Recurrence property
((1.2292740569981171+2.5438401155000685j),
 (1.2292740569981158+2.5438401155000658j))
>>> gamma(0.5)**2  # gamma(0.5) = sqrt(pi)
3.1415926535897927

绘制实数 x 的 gamma(x)

>>> x = np.linspace(-3.5, 5.5, 2251)
>>> y = gamma(x)
>>> import matplotlib.pyplot as plt
>>> plt.plot(x, y, 'b', alpha=0.6, label='gamma(x)')
>>> k = np.arange(1, 7)
>>> plt.plot(k, factorial(k-1), 'k*', alpha=0.6,
...          label='(x-1)!, x = 1, 2, ...')
>>> plt.xlim(-3.5, 5.5)
>>> plt.ylim(-10, 25)
>>> plt.grid()
>>> plt.xlabel('x')
>>> plt.legend(loc='lower right')
>>> plt.show()
../../_images/scipy-special-gamma-1.png