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.

So lets start with the definition

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


# gamma function

# plot
f, ax = plt.subplots(figsize=(10,5))


# loggamma function


f, ax = plt.subplots(figsize=(10,5))


# erf function


# plot
f, ax = plt.subplots(figsize=(10,5))


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s