Debian Codegraph @

src/dr/math/functionEval/GammaFunction.java

raw symbols SCIP
1 /*
2 * GammaFunction.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.math.functionEval;
27
28 /**
29 * Gamma function (Euler's integral).
30 *
31 * @author Didier H. Besset
32 */
33 public final class GammaFunction
34 {
35 static double sqrt2Pi = Math.sqrt( 2 * Math.PI);
36 static double[] coefficients = { 76.18009172947146,
37 -86.50532032941677,
38 24.01409824083091,
39 -1.231739572450155,
40 0.1208650973866179e-2,
41 -0.5395239384953e-5};
42
43 /**
44 * @return double beta function of the arguments
45 * @param x double
46 * @param y double
47 */
48 public static double beta ( double x, double y)
49 {
50 return Math.exp( logGamma( x) + logGamma( y) - logGamma( x + y));
51 }
52 /**
53 * @return long factorial of n
54 * @param n long
55 */
56 public static long factorial ( long n)
57 {
58 return n < 2 ? 1 : n * factorial( n - 1);
59 }
60 /**
61 * @return double gamma function
62 * @param x double
63 */
64 public static double gamma ( double x)
65 {
66 return x > 1
67 ? Math.exp( leadingFactor(x)) * series(x) * sqrt2Pi / x
68 : ( x > 0 ? gamma(x + 1) / x
69 : Double.NaN);
70 }
71 /**
72 * @return double
73 * @param x double
74 */
75 private static double leadingFactor ( double x)
76 {
77 double temp = x + 5.5;
78 return Math.log( temp) * ( x + 0.5) - temp;
79 }
80 /**
81 * @return double logarithm of the beta function of the arguments
82 * @param x double
83 * @param y double
84 */
85 public static double logBeta ( double x, double y)
86 {
87 return logGamma( x) + logGamma( y) - logGamma( x + y);
88 }
89 /**
90 * @return double log of the gamma function
91 * @param x double
92 */
93 public static double logGamma ( double x)
94 {
95 return x > 1
96 ? leadingFactor(x) + Math.log( series(x) * sqrt2Pi / x)
97 : ( x > 0 ? logGamma(x + 1) - Math.log( x)
98 : Double.NaN);
99 }
100 /**
101 * @return double value of the series in Lanczos formula.
102 * @param x double
103 */
104 private static double series( double x)
105 {
106 double answer = 1.000000000190015;
107 double term = x;
108 for ( int i = 0; i < 6; i++)
109 {
110 term += 1;
111 answer += coefficients[i] / term;
112 }
113 return answer;
114 }
115 }