# Some special functions

ANSI C99 and POSIX compliant compilers include functions tgamma and lgamma for computing the gamma function and its natural logarithm respectively. Visual Studio 2008, however, does not support these standards.

Found this fact during the last few weeks, so decided to explore these exotic mathematical functions which microsoft decided not to include in the visual studio 2008.

Gamma function computes the the gamma function,$\Gamma(x)$. The gamma function is a shifted version of the ordinary factorial, satisfying $\Gamma(n) = (n-1)!$ for integers $n > 0$. More generally, it is defined by
$\Gamma(x) = \int_0^{\infty} t^{x-1} e^{-t}\, dt$

for any real or complex $x$ with $\Re(x) > 0$ and for $\Re(x) < 0$ by analytic continuation.

LogGamma function computes the principal branch of the log-gamma function, $\ln \Gamma(z).$ Unlike $\ln(\Gamma(z))$, which has infinitely many complex branch cuts, the principal log-gamma function only has a single branch cut along the negative half-axis. The principal branch continuously matches the asymptotic Stirling expansion

$\ln \Gamma(z) \sim \frac{\ln(2 \pi)}{2} + \left(z-\frac{1}{2}\right) \ln(z) - z + O(z^{-1}).$

The real parts of both functions agree, but their imaginary parts generally differ by $2 n \pi$ for some $n \in \mathbb{Z}$.

They coincide for $z \in \mathbb{R}, z 0$.

Computationally, it is advantageous to use loggamma() instead of gamma() for extremely large arguments.

Error Functions

Computes the error function, $\mathrm{erf}(x)$. The error function is the normalized antiderivative of the Gaussian function $\exp(-t^2)$. More precisely,

$\mathrm{erf}(x) = \frac{2}{\sqrt \pi} \int_0^x \exp(-t^2) \,dt$

Scipy provides ton of special functions. The error functions, the gamma functions are part of them. All of them are found in the scipy.special module

So lets see how they look like.


import scipy.special as sp
import numpy as np
import matplotlib.pyplot as plt

x=np.linspace(-4.5,6,6601)

# gamma function
g=sp.gamma(x)

# plot
f, ax = plt.subplots(figsize=(10,5))
ax.plot(x,g)
ax.grid(True)




# loggamma function

g=sp.gammaln(x)

#plot
f, ax = plt.subplots(figsize=(10,5))
ax.plot(x,g)
ax.grid(True)




# erf function

g=sp.erf(x)