Class Gamma
- java.lang.Object
-
- org.apache.commons.math3.special.Gamma
-
public class Gamma extends java.lang.ObjectThis is a utility class that provides computation methods related to the Γ (Gamma) family of functions.
Implementation of
invGamma1pm1(double)andlogGamma1p(double)is based on the algorithms described in- Didonato and Morris (1986), Computation of the Incomplete Gamma Function Ratios and their Inverse, TOMS 12(4), 377-393,
- Didonato and Morris (1992), Algorithm 708: Significant Digit Computation of the Incomplete Beta Function Ratios, TOMS 18(3), 360-373,
-
-
Field Summary
Fields Modifier and Type Field Description private static doubleC_LIMITC limit.private static doubleDEFAULT_EPSILONMaximum allowed numerical error.static doubleGAMMAprivate static doubleHALF_LOG_2_PIAvoid repeated computation of log of 2 PI in logGammaprivate static doubleINV_GAMMA1P_M1_A0The constantA0defined inDGAM1.private static doubleINV_GAMMA1P_M1_A1The constantA1defined inDGAM1.private static doubleINV_GAMMA1P_M1_B1The constantB1defined inDGAM1.private static doubleINV_GAMMA1P_M1_B2The constantB2defined inDGAM1.private static doubleINV_GAMMA1P_M1_B3The constantB3defined inDGAM1.private static doubleINV_GAMMA1P_M1_B4The constantB4defined inDGAM1.private static doubleINV_GAMMA1P_M1_B5The constantB5defined inDGAM1.private static doubleINV_GAMMA1P_M1_B6The constantB6defined inDGAM1.private static doubleINV_GAMMA1P_M1_B7The constantB7defined inDGAM1.private static doubleINV_GAMMA1P_M1_B8The constantB8defined inDGAM1.private static doubleINV_GAMMA1P_M1_CThe constantCdefined inDGAM1.private static doubleINV_GAMMA1P_M1_C0The constantC0defined inDGAM1.private static doubleINV_GAMMA1P_M1_C1The constantC1defined inDGAM1.private static doubleINV_GAMMA1P_M1_C10The constantC10defined inDGAM1.private static doubleINV_GAMMA1P_M1_C11The constantC11defined inDGAM1.private static doubleINV_GAMMA1P_M1_C12The constantC12defined inDGAM1.private static doubleINV_GAMMA1P_M1_C13The constantC13defined inDGAM1.private static doubleINV_GAMMA1P_M1_C2The constantC2defined inDGAM1.private static doubleINV_GAMMA1P_M1_C3The constantC3defined inDGAM1.private static doubleINV_GAMMA1P_M1_C4The constantC4defined inDGAM1.private static doubleINV_GAMMA1P_M1_C5The constantC5defined inDGAM1.private static doubleINV_GAMMA1P_M1_C6The constantC6defined inDGAM1.private static doubleINV_GAMMA1P_M1_C7The constantC7defined inDGAM1.private static doubleINV_GAMMA1P_M1_C8The constantC8defined inDGAM1.private static doubleINV_GAMMA1P_M1_C9The constantC9defined inDGAM1.private static doubleINV_GAMMA1P_M1_P0The constantP0defined inDGAM1.private static doubleINV_GAMMA1P_M1_P1The constantP1defined inDGAM1.private static doubleINV_GAMMA1P_M1_P2The constantP2defined inDGAM1.private static doubleINV_GAMMA1P_M1_P3The constantP3defined inDGAM1.private static doubleINV_GAMMA1P_M1_P4The constantP4defined inDGAM1.private static doubleINV_GAMMA1P_M1_P5The constantP5defined inDGAM1.private static doubleINV_GAMMA1P_M1_P6The constantP6defined inDGAM1.private static doubleINV_GAMMA1P_M1_Q1The constantQ1defined inDGAM1.private static doubleINV_GAMMA1P_M1_Q2The constantQ2defined inDGAM1.private static doubleINV_GAMMA1P_M1_Q3The constantQ3defined inDGAM1.private static doubleINV_GAMMA1P_M1_Q4The constantQ4defined inDGAM1.private static double[]LANCZOSLanczos coefficientsstatic doubleLANCZOS_GThe value of thegconstant in the Lanczos approximation, seelanczos(double).private static doubleS_LIMITS limit.private static doubleSQRT_TWO_PIThe constant value of √(2π).
-
Constructor Summary
Constructors Modifier Constructor Description privateGamma()Default constructor.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static doubledigamma(double x)Computes the digamma function of x.static doublegamma(double x)Returns the value of Γ(x).static doubleinvGamma1pm1(double x)Returns the value of 1 / Γ(1 + x) - 1 for -0.5 ≤ x ≤ 1.5.static doublelanczos(double x)Returns the Lanczos approximation used to compute the gamma function.static doublelogGamma(double x)Returns the value of log Γ(x) for x > 0.static doublelogGamma1p(double x)Returns the value of log Γ(1 + x) for -0.5 ≤ x ≤ 1.5.static doubleregularizedGammaP(double a, double x)Returns the regularized gamma function P(a, x).static doubleregularizedGammaP(double a, double x, double epsilon, int maxIterations)Returns the regularized gamma function P(a, x).static doubleregularizedGammaQ(double a, double x)Returns the regularized gamma function Q(a, x) = 1 - P(a, x).static doubleregularizedGammaQ(double a, double x, double epsilon, int maxIterations)Returns the regularized gamma function Q(a, x) = 1 - P(a, x).static doubletrigamma(double x)Computes the trigamma function of x.
-
-
-
Field Detail
-
GAMMA
public static final double GAMMA
- Since:
- 2.0
- See Also:
- Constant Field Values
-
LANCZOS_G
public static final double LANCZOS_G
The value of thegconstant in the Lanczos approximation, seelanczos(double).- Since:
- 3.1
- See Also:
- Constant Field Values
-
DEFAULT_EPSILON
private static final double DEFAULT_EPSILON
Maximum allowed numerical error.- See Also:
- Constant Field Values
-
LANCZOS
private static final double[] LANCZOS
Lanczos coefficients
-
HALF_LOG_2_PI
private static final double HALF_LOG_2_PI
Avoid repeated computation of log of 2 PI in logGamma
-
SQRT_TWO_PI
private static final double SQRT_TWO_PI
The constant value of √(2π).- See Also:
- Constant Field Values
-
C_LIMIT
private static final double C_LIMIT
C limit.- See Also:
- Constant Field Values
-
S_LIMIT
private static final double S_LIMIT
S limit.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_A0
private static final double INV_GAMMA1P_M1_A0
The constantA0defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_A1
private static final double INV_GAMMA1P_M1_A1
The constantA1defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_B1
private static final double INV_GAMMA1P_M1_B1
The constantB1defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_B2
private static final double INV_GAMMA1P_M1_B2
The constantB2defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_B3
private static final double INV_GAMMA1P_M1_B3
The constantB3defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_B4
private static final double INV_GAMMA1P_M1_B4
The constantB4defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_B5
private static final double INV_GAMMA1P_M1_B5
The constantB5defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_B6
private static final double INV_GAMMA1P_M1_B6
The constantB6defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_B7
private static final double INV_GAMMA1P_M1_B7
The constantB7defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_B8
private static final double INV_GAMMA1P_M1_B8
The constantB8defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_P0
private static final double INV_GAMMA1P_M1_P0
The constantP0defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_P1
private static final double INV_GAMMA1P_M1_P1
The constantP1defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_P2
private static final double INV_GAMMA1P_M1_P2
The constantP2defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_P3
private static final double INV_GAMMA1P_M1_P3
The constantP3defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_P4
private static final double INV_GAMMA1P_M1_P4
The constantP4defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_P5
private static final double INV_GAMMA1P_M1_P5
The constantP5defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_P6
private static final double INV_GAMMA1P_M1_P6
The constantP6defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_Q1
private static final double INV_GAMMA1P_M1_Q1
The constantQ1defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_Q2
private static final double INV_GAMMA1P_M1_Q2
The constantQ2defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_Q3
private static final double INV_GAMMA1P_M1_Q3
The constantQ3defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_Q4
private static final double INV_GAMMA1P_M1_Q4
The constantQ4defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_C
private static final double INV_GAMMA1P_M1_C
The constantCdefined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_C0
private static final double INV_GAMMA1P_M1_C0
The constantC0defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_C1
private static final double INV_GAMMA1P_M1_C1
The constantC1defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_C2
private static final double INV_GAMMA1P_M1_C2
The constantC2defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_C3
private static final double INV_GAMMA1P_M1_C3
The constantC3defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_C4
private static final double INV_GAMMA1P_M1_C4
The constantC4defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_C5
private static final double INV_GAMMA1P_M1_C5
The constantC5defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_C6
private static final double INV_GAMMA1P_M1_C6
The constantC6defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_C7
private static final double INV_GAMMA1P_M1_C7
The constantC7defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_C8
private static final double INV_GAMMA1P_M1_C8
The constantC8defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_C9
private static final double INV_GAMMA1P_M1_C9
The constantC9defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_C10
private static final double INV_GAMMA1P_M1_C10
The constantC10defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_C11
private static final double INV_GAMMA1P_M1_C11
The constantC11defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_C12
private static final double INV_GAMMA1P_M1_C12
The constantC12defined inDGAM1.- See Also:
- Constant Field Values
-
INV_GAMMA1P_M1_C13
private static final double INV_GAMMA1P_M1_C13
The constantC13defined inDGAM1.- See Also:
- Constant Field Values
-
-
Method Detail
-
logGamma
public static double logGamma(double x)
Returns the value of log Γ(x) for x > 0.
For x ≤ 8, the implementation is based on the double precision implementation in the NSWC Library of Mathematics Subroutines,
DGAMLN. For x > 8, the implementation is based on- Gamma Function, equation (28).
- Lanczos Approximation, equations (1) through (5).
- Paul Godfrey, A note on the computation of the convergent Lanczos complex Gamma approximation
- Parameters:
x- Argument.- Returns:
- the value of
log(Gamma(x)),Double.NaNifx <= 0.0.
-
regularizedGammaP
public static double regularizedGammaP(double a, double x)Returns the regularized gamma function P(a, x).- Parameters:
a- Parameter.x- Value.- Returns:
- the regularized gamma function P(a, x).
- Throws:
MaxCountExceededException- if the algorithm fails to converge.
-
regularizedGammaP
public static double regularizedGammaP(double a, double x, double epsilon, int maxIterations)Returns the regularized gamma function P(a, x). The implementation of this method is based on:- Regularized Gamma Function, equation (1)
- Incomplete Gamma Function, equation (4).
- Confluent Hypergeometric Function of the First Kind, equation (1).
- Parameters:
a- the a parameter.x- the value.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:
- the regularized gamma function P(a, x)
- Throws:
MaxCountExceededException- if the algorithm fails to converge.
-
regularizedGammaQ
public static double regularizedGammaQ(double a, double x)Returns the regularized gamma function Q(a, x) = 1 - P(a, x).- Parameters:
a- the a parameter.x- the value.- Returns:
- the regularized gamma function Q(a, x)
- Throws:
MaxCountExceededException- if the algorithm fails to converge.
-
regularizedGammaQ
public static double regularizedGammaQ(double a, double x, double epsilon, int maxIterations)Returns the regularized gamma function Q(a, x) = 1 - P(a, x). The implementation of this method is based on:- Parameters:
a- the a parameter.x- the value.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:
- the regularized gamma function P(a, x)
- Throws:
MaxCountExceededException- if the algorithm fails to converge.
-
digamma
public static double digamma(double x)
Computes the digamma function of x.
This is an independently written implementation of the algorithm described in Jose Bernardo, Algorithm AS 103: Psi (Digamma) Function, Applied Statistics, 1976.
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 absolute tolerance for x >= 10^-5 and within 10^-8 relative tolerance for x > 0.
Performance for large negative values of x will be quite expensive (proportional to |x|). Accuracy for negative values of x should be about 10^-8 absolute for results less than 10^5 and 10^-8 relative for results larger than that.
- Parameters:
x- Argument.- Returns:
- digamma(x) to within 10-8 relative or absolute error whichever is smaller.
- Since:
- 2.0
- See Also:
- Digamma, Bernardo's original article
-
trigamma
public static double trigamma(double x)
Computes the trigamma function of x. This function is derived by taking the derivative of the implementation of digamma.- Parameters:
x- Argument.- Returns:
- trigamma(x) to within 10-8 relative or absolute error whichever is smaller
- Since:
- 2.0
- See Also:
- Trigamma,
digamma(double)
-
lanczos
public static double lanczos(double x)
Returns the Lanczos approximation used to compute the gamma function. The Lanczos approximation is related to the Gamma function by the following equation
wheregamma(x) = sqrt(2 * pi) / x * (x + g + 0.5) ^ (x + 0.5) * exp(-x - g - 0.5) * lanczos(x),gis the Lanczos constant.- Parameters:
x- Argument.- Returns:
- The Lanczos approximation.
- Since:
- 3.1
- See Also:
- 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)
Returns the value of 1 / Γ(1 + x) - 1 for -0.5 ≤ x ≤ 1.5. This implementation is based on the double precision implementation in the NSWC Library of Mathematics Subroutines,DGAM1.- Parameters:
x- Argument.- Returns:
- The value of
1.0 / Gamma(1.0 + x) - 1.0. - Throws:
NumberIsTooSmallException- ifx < -0.5NumberIsTooLargeException- ifx > 1.5- Since:
- 3.1
-
logGamma1p
public static double logGamma1p(double x) throws NumberIsTooSmallException, NumberIsTooLargeExceptionReturns the value of log Γ(1 + x) for -0.5 ≤ x ≤ 1.5. This implementation is based on the double precision implementation in the NSWC Library of Mathematics Subroutines,DGMLN1.- Parameters:
x- Argument.- Returns:
- The value of
log(Gamma(1 + x)). - Throws:
NumberIsTooSmallException- ifx < -0.5.NumberIsTooLargeException- ifx > 1.5.- Since:
- 3.1
-
gamma
public static double gamma(double x)
Returns the value of Γ(x). Based on the NSWC Library of Mathematics Subroutines double precision implementation,DGAMMA.- Parameters:
x- Argument.- Returns:
- the value of
Gamma(x). - Since:
- 3.1
-
-