org.apache.commons.math4.special

## Class Gamma

• public class Gamma
extends Object

This is a utility class that provides computation methods related to the Γ (Gamma) family of functions.

Implementation of invGamma1pm1(double) and logGamma1p(double) is based on the algorithms described in

and implemented in the NSWC Library of Mathematical Functions, available here. This library is "approved for public release", and the Copyright guidance indicates that unless otherwise stated in the code, all FORTRAN functions in this library are license free. Since no such notice appears in the code these functions can safely be ported to Commons-Math.

• ### Method Summary

All Methods
Modifier and Type Method and Description
static double digamma(double x)
Computes the digamma function, defined as the logarithmic derivative of the $$\Gamma$$ function: $$\frac{d}{dx}(\ln \Gamma(x)) = \frac{\Gamma^\prime(x)}{\Gamma(x)}$$.
static double gamma(double x)
Computes the value of $$\Gamma(x)$$.
static double invGamma1pm1(double x)
Computes the function $$\frac{1}{\Gamma(1 + x)} - 1$$ for $$-0.5 \leq x \leq 1.5$$.
static double lanczos(double x)
Computes the Lanczos approximation used to compute the gamma function.
static double logGamma(double x)
Computes the function $$\ln \Gamma(x)$$ for $$x > 0$$.
static double logGamma1p(double x)
Computes the function $$\ln \Gamma(1 + x)$$ for $$-0.5 \leq x \leq 1.5$$.
static double regularizedGammaP(double a, double x)
Computes the regularized gamma function $$P(a, x)$$.
static double regularizedGammaP(double a, double x, double epsilon, int maxIterations)
Computes the regularized gamma function $$P(a, x)$$.
static double regularizedGammaQ(double a, double x)
Computes the regularized gamma function $$Q(a, x) = 1 - P(a, x)$$.
static double regularizedGammaQ(double a, double x, double epsilon, int maxIterations)
Computes the regularized gamma function $$Q(a, x) = 1 - P(a, x)$$.
static double trigamma(double x)
Computes the trigamma function $$\psi_1(x) = \frac{d^2}{dx^2} (\ln \Gamma(x))$$.
• ### Method Detail

• #### regularizedGammaP

public static double regularizedGammaP(double a,
double x)
Computes the regularized gamma function $$P(a, x)$$.
Parameters:
a - Parameter $$a$$.
x - Value.
Returns:
$$P(a, x)$$
Throws:
MaxCountExceededException - if the algorithm fails to converge.
• #### regularizedGammaP

public static double regularizedGammaP(double a,
double x,
double epsilon,
int maxIterations)
Computes the regularized gamma function $$P(a, x)$$. The implementation of this method is based on:
Parameters:
a - Parameter $$a$$.
x - Argument.
epsilon - When the absolute value of the nth item in the series is less than epsilon the approximation ceases to calculate further elements in the series.
maxIterations - Maximum number of "iterations" to complete.
Returns:
$$P(a, x)$$
Throws:
MaxCountExceededException - if the algorithm fails to converge.
• #### regularizedGammaQ

public static double regularizedGammaQ(double a,
double x)
Computes the regularized gamma function $$Q(a, x) = 1 - P(a, x)$$.
Parameters:
a - Parameter $$a$$.
x - Argument.
Returns:
$$Q(a, x)$$
Throws:
MaxCountExceededException - if the algorithm fails to converge.
• #### regularizedGammaQ

public static double regularizedGammaQ(double a,
double x,
double epsilon,
int maxIterations)
Computes the regularized gamma function $$Q(a, x) = 1 - P(a, x)$$. The implementation of this method is based on:
Parameters:
a - Parameter $$a$$.
x - Argument.
epsilon - When the absolute value of the nth item in the series is less than epsilon the approximation ceases to calculate further elements in the series.
maxIterations - Maximum number of "iterations" to complete.
Returns:
$$Q(a, x)$$
Throws:
MaxCountExceededException - if the algorithm fails to converge.
• #### digamma

public static double digamma(double x)
Computes the digamma function, defined as the logarithmic derivative of the $$\Gamma$$ function: $$\frac{d}{dx}(\ln \Gamma(x)) = \frac{\Gamma^\prime(x)}{\Gamma(x)}$$.

This is an independently written implementation of the algorithm described in Jose Bernardo, Algorithm AS 103: Psi (Digamma) Function, Applied Statistics, 1976. A reflection formula is incorporated to improve performance on negative values.

Some of the constants have been changed to increase accuracy at the moderate expense of run-time. The result should be accurate to within $$10^{-8}$$ relative tolerance for $$0 < x < 10^{-5}$$ and within $$10^{-8}$$ absolute tolerance otherwise.

Parameters:
x - Argument.
Returns:
digamma(x) to within $$10^{-8}$$ relative or absolute error whichever is larger.
Since:
2.0
Digamma, Bernardo's original article
• #### trigamma

public static double trigamma(double x)
Computes the trigamma function $$\psi_1(x) = \frac{d^2}{dx^2} (\ln \Gamma(x))$$.

This function is the derivative of the digamma function.

Parameters:
x - Argument.
Returns:
$$\psi_1(x)$$ to within $$10^{-8}$$ relative or absolute error whichever is smaller
Since:
2.0
Trigamma, digamma(double)
• #### lanczos

public static double lanczos(double x)
Computes the Lanczos approximation used to compute the gamma function.

The Lanczos approximation is related to the Gamma function by the following equation $\Gamma(x) = \sqrt{2\pi} \, \frac{(g + x + \frac{1}{2})^{x + \frac{1}{2}} \, e^{-(g + x + \frac{1}{2})} \, \mathrm{lanczos}(x)} {x}$ where $$g$$ is the Lanczos constant.

Parameters:
x - Argument.
Returns:
The Lanczos approximation.
Since:
3.1
Lanczos Approximation equations (1) through (5), and Paul Godfrey's Note on the computation of the convergent Lanczos complex Gamma approximation
• #### invGamma1pm1

public static double invGamma1pm1(double x)
Computes the function $$\frac{1}{\Gamma(1 + x)} - 1$$ for $$-0.5 \leq x \leq 1.5$$.

This implementation is based on the double precision implementation in the NSWC Library of Mathematics Subroutines, DGAM1.

Parameters:
x - Argument.
Returns:
$$\frac{1}{\Gamma(1 + x)} - 1$$
Throws:
NumberIsTooSmallException - if x < -0.5
NumberIsTooLargeException - if x > 1.5
Since:
3.1
• #### gamma

public static double gamma(double x)
Computes the value of $$\Gamma(x)$$.

Based on the NSWC Library of Mathematics Subroutines double precision implementation, DGAMMA.

Parameters:
x - Argument.
Returns:
$$\Gamma(x)$$
Since:
3.1