# The Gamma Function $\Gamma$

$\Gamma(p)$ is a generalization of the factorial function to real and complex $p$. If $p$ is a positive integer then $\Gamma(p) = (p-1)!$, the familar factorial __except with the argument offset by 1__ (widely described as an unfortunate historical accident).

More generally we can define

$$ \Gamma(z) = \int_0^{\infty} x^{z-1} e^{-x} dx \quad \text{for} \quad Re(z) > 0 $$

Gamma functions still obey the same recursion relation as factorials, $\Gamma(z+1) = z \Gamma(z)$, even for $Re(z) < 0$. Rearranging this slightly gives the definition for negative numbers:

$$ \Gamma(z) = \frac{1}{z} \Gamma(z+1) $$

However, the function is undefined (divergent) when $z$ is zero or a negative integer.


```python
import numpy as np
import matplotlib.pyplot as plt

plt.rcParams.update({'font.size': 16})
```

SciPy will evaluate Gamma functions very simply:


```python
import scipy.special as sp
xlims = (-4, 4)
x = np.linspace(xlims[0], xlims[1], 500)
gammas = sp.gamma(x)
# display(gammas)

plt.figure(figsize=(9, 9))
plt.plot(x, gammas)

plt.xlim(xlims)
plt.ylim((-10, 10))
plt.xlabel('$x$')
plt.ylabel('$\\Gamma(x)$')
plt.title('Plot of the Gamma function for real x')                                
plt.grid(True)
```

Complex values also work without fuss, but remember that $\sqrt{-1}$ is entered as `1j` in Python. Plots of $\Gamma(z)$ in the complex plane are not at all simple and quite pretty, so there are plenty of examples on the web.


```python
sp.gamma(1.1+0.5j)
```




    (0.7911433469580142-0.14123664385292767j)



SymPy will do all sorts of complicated things with Gamma and related functions, but I've not yet found anything simple and obviously useful to me.


```python
from sympy import gamma, pi, init_printing, diff
from sympy.abc import x
init_printing()

display(gamma(x))
display(diff(gamma(x),x))
```

## Beta functions

These are somewhat related to Gamma functions but take two arguments:

$$ B(p,q) = \int_0^1 x^{p-1} (1-x)^{q-1} dx, \quad p>0, \quad q>0 $$

They can be expressed in terms of Gamma functions:

$$ B(p,q) = \frac{\Gamma(p) \Gamma(q)}{\Gamma(p+q)} $$

## References:
    
- Boas, "Mathematical methods in the physical sciences", 3rd ed, sections 11.3-11.6
- MathWorld, http://mathworld.wolfram.com/GammaFunction.html and http://mathworld.wolfram.com/BetaFunction.html
- Wikipedia, https://en.wikipedia.org/wiki/Gamma_function and https://en.wikipedia.org/wiki/Beta_function


```python

```

1162b4214db4bb7bac89952142766eda0067f034
ipynb
Jupyter Notebook
math/Gamma.ipynb
colinleach/astro-Jupyter
8d7618068f0460ff0c514075ce84d2bda31870b6
MIT
2020-06-06T15:35:35.000Z
2020-06-06T15:35:35.000Z
math/Gamma.ipynb
colinleach/astro-Jupyter
8d7618068f0460ff0c514075ce84d2bda31870b6
MIT
2019-06-08T11:44:15.000Z
2019-06-10T17:42:32.000Z
math/Gamma.ipynb
colinleach/astro-Jupyter
8d7618068f0460ff0c514075ce84d2bda31870b6
MIT
2021-04-14T15:28:43.000Z
2021-04-14T15:28:43.000Z
Qwen/Qwen-72B
1. YES
2. YES