MATH-1416: Delete functionality now in "Commons Numbers".

This commit is contained in:
Gilles 2017-05-14 23:30:00 +02:00
parent 2ec4deacfd
commit 44ab256961
4 changed files with 0 additions and 3207 deletions

View File

@ -1,484 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.special;
import org.apache.commons.numbers.gamma.Gamma;
import org.apache.commons.numbers.gamma.LogGamma;
import org.apache.commons.math4.exception.NumberIsTooSmallException;
import org.apache.commons.math4.exception.OutOfRangeException;
import org.apache.commons.math4.util.ContinuedFraction;
import org.apache.commons.math4.util.FastMath;
/**
* <p>
* This is a utility class that provides computation methods related to the
* Beta family of functions.
* </p>
* <p>
* Implementation of {@link #logBeta(double, double)} is based on the
* algorithms described in
* <ul>
* <li><a href="http://dx.doi.org/10.1145/22721.23109">Didonato and Morris
* (1986)</a>, <em>Computation of the Incomplete Gamma Function Ratios
* and their Inverse</em>, TOMS 12(4), 377-393,</li>
* <li><a href="http://dx.doi.org/10.1145/131766.131776">Didonato and Morris
* (1992)</a>, <em>Algorithm 708: Significant Digit Computation of the
* Incomplete Beta Function Ratios</em>, TOMS 18(3), 360-373,</li>
* </ul>
* and implemented in the
* <a href="http://www.dtic.mil/docs/citations/ADA476840">NSWC Library of Mathematical Functions</a>,
* available
* <a href="http://www.ualberta.ca/CNS/RESEARCH/Software/NumericalNSWC/site.html">here</a>.
* This library is "approved for public release", and the
* <a href="http://www.dtic.mil/dtic/pdf/announcements/CopyrightGuidance.pdf">Copyright guidance</a>
* 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.
*/
public class Beta {
/** Maximum allowed numerical error. */
private static final double DEFAULT_EPSILON = 1E-14;
/** The constant value of ½log 2π. */
private static final double HALF_LOG_TWO_PI = .9189385332046727;
/**
* <p>
* The coefficients of the series expansion of the Δ function. This function
* is defined as follows
* </p>
* <center>Δ(x) = log Γ(x) - (x - 0.5) log a + a - 0.5 log ,</center>
* <p>
* see equation (23) in Didonato and Morris (1992). The series expansion,
* which applies for x 10, reads
* </p>
* <pre>
* 14
* ====
* 1 \ 2 n
* Δ(x) = --- > d (10 / x)
* x / n
* ====
* n = 0
* <pre>
*/
private static final double[] DELTA = {
.833333333333333333333333333333E-01,
-.277777777777777777777777752282E-04,
.793650793650793650791732130419E-07,
-.595238095238095232389839236182E-09,
.841750841750832853294451671990E-11,
-.191752691751854612334149171243E-12,
.641025640510325475730918472625E-14,
-.295506514125338232839867823991E-15,
.179643716359402238723287696452E-16,
-.139228964661627791231203060395E-17,
.133802855014020915603275339093E-18,
-.154246009867966094273710216533E-19,
.197701992980957427278370133333E-20,
-.234065664793997056856992426667E-21,
.171348014966398575409015466667E-22
};
/**
* Default constructor. Prohibit instantiation.
*/
private Beta() {}
/**
* Returns the
* <a href="http://mathworld.wolfram.com/RegularizedBetaFunction.html">
* regularized beta function</a> I(x, a, b).
*
* @param x Value.
* @param a Parameter {@code a}.
* @param b Parameter {@code b}.
* @return the regularized beta function I(x, a, b).
* @throws org.apache.commons.math4.exception.MaxCountExceededException
* if the algorithm fails to converge.
*/
public static double regularizedBeta(double x, double a, double b) {
return regularizedBeta(x, a, b, DEFAULT_EPSILON, Integer.MAX_VALUE);
}
/**
* Returns the
* <a href="http://mathworld.wolfram.com/RegularizedBetaFunction.html">
* regularized beta function</a> I(x, a, b).
*
* @param x Value.
* @param a Parameter {@code a}.
* @param b Parameter {@code b}.
* @param 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.
* @return the regularized beta function I(x, a, b)
* @throws org.apache.commons.math4.exception.MaxCountExceededException
* if the algorithm fails to converge.
*/
public static double regularizedBeta(double x,
double a, double b,
double epsilon) {
return regularizedBeta(x, a, b, epsilon, Integer.MAX_VALUE);
}
/**
* Returns the regularized beta function I(x, a, b).
*
* @param x the value.
* @param a Parameter {@code a}.
* @param b Parameter {@code b}.
* @param maxIterations Maximum number of "iterations" to complete.
* @return the regularized beta function I(x, a, b)
* @throws org.apache.commons.math4.exception.MaxCountExceededException
* if the algorithm fails to converge.
*/
public static double regularizedBeta(double x,
double a, double b,
int maxIterations) {
return regularizedBeta(x, a, b, DEFAULT_EPSILON, maxIterations);
}
/**
* Returns the regularized beta function I(x, a, b).
*
* The implementation of this method is based on:
* <ul>
* <li>
* <a href="http://mathworld.wolfram.com/RegularizedBetaFunction.html">
* Regularized Beta Function</a>.</li>
* <li>
* <a href="http://functions.wolfram.com/06.21.10.0001.01">
* Regularized Beta Function</a>.</li>
* </ul>
*
* @param x the value.
* @param a Parameter {@code a}.
* @param b Parameter {@code b}.
* @param 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.
* @param maxIterations Maximum number of "iterations" to complete.
* @return the regularized beta function I(x, a, b)
* @throws org.apache.commons.math4.exception.MaxCountExceededException
* if the algorithm fails to converge.
*/
public static double regularizedBeta(double x,
final double a, final double b,
double epsilon, int maxIterations) {
double ret;
if (Double.isNaN(x) ||
Double.isNaN(a) ||
Double.isNaN(b) ||
x < 0 ||
x > 1 ||
a <= 0 ||
b <= 0) {
ret = Double.NaN;
} else if (x > (a + 1) / (2 + b + a) &&
1 - x <= (b + 1) / (2 + b + a)) {
ret = 1 - regularizedBeta(1 - x, b, a, epsilon, maxIterations);
} else {
ContinuedFraction fraction = new ContinuedFraction() {
/** {@inheritDoc} */
@Override
protected double getB(int n, double x) {
double ret;
double m;
if (n % 2 == 0) { // even
m = n / 2.0;
ret = (m * (b - m) * x) /
((a + (2 * m) - 1) * (a + (2 * m)));
} else {
m = (n - 1.0) / 2.0;
ret = -((a + m) * (a + b + m) * x) /
((a + (2 * m)) * (a + (2 * m) + 1.0));
}
return ret;
}
/** {@inheritDoc} */
@Override
protected double getA(int n, double x) {
return 1.0;
}
};
ret = FastMath.exp((a * FastMath.log(x)) + (b * FastMath.log1p(-x)) -
FastMath.log(a) - logBeta(a, b)) *
1.0 / fraction.evaluate(x, epsilon, maxIterations);
}
return ret;
}
/**
* Returns the value of log Γ(a + b) for 1 a, b 2. Based on the
* <em>NSWC Library of Mathematics Subroutines</em> double precision
* implementation, {@code DGSMLN}. In {@code BetaTest.testLogGammaSum()},
* this private method is accessed through reflection.
*
* @param a First argument.
* @param b Second argument.
* @return the value of {@code log(Gamma(a + b))}.
* @throws OutOfRangeException if {@code a} or {@code b} is lower than
* {@code 1.0} or greater than {@code 2.0}.
*/
private static double logGammaSum(final double a, final double b)
throws OutOfRangeException {
if ((a < 1.0) || (a > 2.0)) {
throw new OutOfRangeException(a, 1.0, 2.0);
}
if ((b < 1.0) || (b > 2.0)) {
throw new OutOfRangeException(b, 1.0, 2.0);
}
final double x = (a - 1.0) + (b - 1.0);
if (x <= 0.5) {
return org.apache.commons.math4.special.Gamma.logGamma1p(1.0 + x);
} else if (x <= 1.5) {
return org.apache.commons.math4.special.Gamma.logGamma1p(x) + FastMath.log1p(x);
} else {
return org.apache.commons.math4.special.Gamma.logGamma1p(x - 1.0) + FastMath.log(x * (1.0 + x));
}
}
/**
* Returns the value of log[Γ(b) / Γ(a + b)] for a 0 and b 10. Based on
* the <em>NSWC Library of Mathematics Subroutines</em> double precision
* implementation, {@code DLGDIV}. In
* {@code BetaTest.testLogGammaMinusLogGammaSum()}, this private method is
* accessed through reflection.
*
* @param a First argument.
* @param b Second argument.
* @return the value of {@code log(Gamma(b) / Gamma(a + b))}.
* @throws NumberIsTooSmallException if {@code a < 0.0} or {@code b < 10.0}.
*/
private static double logGammaMinusLogGammaSum(final double a,
final double b)
throws NumberIsTooSmallException {
if (a < 0.0) {
throw new NumberIsTooSmallException(a, 0.0, true);
}
if (b < 10.0) {
throw new NumberIsTooSmallException(b, 10.0, true);
}
/*
* d = a + b - 0.5
*/
final double d;
final double w;
if (a <= b) {
d = b + (a - 0.5);
w = deltaMinusDeltaSum(a, b);
} else {
d = a + (b - 0.5);
w = deltaMinusDeltaSum(b, a);
}
final double u = d * FastMath.log1p(a / b);
final double v = a * (FastMath.log(b) - 1.0);
return u <= v ? (w - u) - v : (w - v) - u;
}
/**
* Returns the value of Δ(b) - Δ(a + b), with 0 a b and b 10. Based
* on equations (26), (27) and (28) in Didonato and Morris (1992).
*
* @param a First argument.
* @param b Second argument.
* @return the value of {@code Delta(b) - Delta(a + b)}
* @throws OutOfRangeException if {@code a < 0} or {@code a > b}
* @throws NumberIsTooSmallException if {@code b < 10}
*/
private static double deltaMinusDeltaSum(final double a,
final double b)
throws OutOfRangeException, NumberIsTooSmallException {
if ((a < 0) || (a > b)) {
throw new OutOfRangeException(a, 0, b);
}
if (b < 10) {
throw new NumberIsTooSmallException(b, 10, true);
}
final double h = a / b;
final double p = h / (1.0 + h);
final double q = 1.0 / (1.0 + h);
final double q2 = q * q;
/*
* s[i] = 1 + q + ... - q**(2 * i)
*/
final double[] s = new double[DELTA.length];
s[0] = 1.0;
for (int i = 1; i < s.length; i++) {
s[i] = 1.0 + (q + q2 * s[i - 1]);
}
/*
* w = Delta(b) - Delta(a + b)
*/
final double sqrtT = 10.0 / b;
final double t = sqrtT * sqrtT;
double w = DELTA[DELTA.length - 1] * s[s.length - 1];
for (int i = DELTA.length - 2; i >= 0; i--) {
w = t * w + DELTA[i] * s[i];
}
return w * p / b;
}
/**
* Returns the value of Δ(p) + Δ(q) - Δ(p + q), with p, q 10. Based on
* the <em>NSWC Library of Mathematics Subroutines</em> double precision
* implementation, {@code DBCORR}. In
* {@code BetaTest.testSumDeltaMinusDeltaSum()}, this private method is
* accessed through reflection.
*
* @param p First argument.
* @param q Second argument.
* @return the value of {@code Delta(p) + Delta(q) - Delta(p + q)}.
* @throws NumberIsTooSmallException if {@code p < 10.0} or {@code q < 10.0}.
*/
private static double sumDeltaMinusDeltaSum(final double p,
final double q) {
if (p < 10.0) {
throw new NumberIsTooSmallException(p, 10.0, true);
}
if (q < 10.0) {
throw new NumberIsTooSmallException(q, 10.0, true);
}
final double a = FastMath.min(p, q);
final double b = FastMath.max(p, q);
final double sqrtT = 10.0 / a;
final double t = sqrtT * sqrtT;
double z = DELTA[DELTA.length - 1];
for (int i = DELTA.length - 2; i >= 0; i--) {
z = t * z + DELTA[i];
}
return z / a + deltaMinusDeltaSum(a, b);
}
/**
* Returns the value of {@code log B(p, q)} for {@code 0 x 1} and {@code p, q > 0}. Based on the
* <em>NSWC Library of Mathematics Subroutines</em> implementation,
* {@code DBETLN}.
*
* @param p First argument.
* @param q Second argument.
* @return the value of {@code log(Beta(p, q))}, {@code NaN} if
* {@code p <= 0} or {@code q <= 0}.
*/
public static double logBeta(final double p, final double q) {
if (Double.isNaN(p) || Double.isNaN(q) || (p <= 0.0) || (q <= 0.0)) {
return Double.NaN;
}
final double a = FastMath.min(p, q);
final double b = FastMath.max(p, q);
if (a >= 10.0) {
final double w = sumDeltaMinusDeltaSum(a, b);
final double h = a / b;
final double c = h / (1.0 + h);
final double u = -(a - 0.5) * FastMath.log(c);
final double v = b * FastMath.log1p(h);
if (u <= v) {
return (((-0.5 * FastMath.log(b) + HALF_LOG_TWO_PI) + w) - u) - v;
} else {
return (((-0.5 * FastMath.log(b) + HALF_LOG_TWO_PI) + w) - v) - u;
}
} else if (a > 2.0) {
if (b > 1000.0) {
final int n = (int) FastMath.floor(a - 1.0);
double prod = 1.0;
double ared = a;
for (int i = 0; i < n; i++) {
ared -= 1.0;
prod *= ared / (1.0 + ared / b);
}
return (FastMath.log(prod) - n * FastMath.log(b)) +
(LogGamma.value(ared) +
logGammaMinusLogGammaSum(ared, b));
} else {
double prod1 = 1.0;
double ared = a;
while (ared > 2.0) {
ared -= 1.0;
final double h = ared / b;
prod1 *= h / (1.0 + h);
}
if (b < 10.0) {
double prod2 = 1.0;
double bred = b;
while (bred > 2.0) {
bred -= 1.0;
prod2 *= bred / (ared + bred);
}
return FastMath.log(prod1) +
FastMath.log(prod2) +
(LogGamma.value(ared) +
(LogGamma.value(bred) -
logGammaSum(ared, bred)));
} else {
return FastMath.log(prod1) +
LogGamma.value(ared) +
logGammaMinusLogGammaSum(ared, b);
}
}
} else if (a >= 1.0) {
if (b > 2.0) {
if (b < 10.0) {
double prod = 1.0;
double bred = b;
while (bred > 2.0) {
bred -= 1.0;
prod *= bred / (a + bred);
}
return FastMath.log(prod) +
(LogGamma.value(a) +
(LogGamma.value(bred) -
logGammaSum(a, bred)));
} else {
return LogGamma.value(a) +
logGammaMinusLogGammaSum(a, b);
}
} else {
return LogGamma.value(a) +
LogGamma.value(b) -
logGammaSum(a, b);
}
} else {
if (b >= 10.0) {
return LogGamma.value(a) +
logGammaMinusLogGammaSum(a, b);
} else {
// The following command is the original NSWC implementation.
// return LogGamma.value(a) +
// (LogGamma.value(b) - LogGamma.value(a + b));
// The following command turns out to be more accurate.
return FastMath.log(Gamma.value(a) * Gamma.value(b) /
Gamma.value(a + b));
}
}
}
}

View File

@ -1,746 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.special;
import org.apache.commons.math4.exception.MaxCountExceededException;
import org.apache.commons.math4.exception.NumberIsTooLargeException;
import org.apache.commons.math4.exception.NumberIsTooSmallException;
import org.apache.commons.math4.util.ContinuedFraction;
import org.apache.commons.math4.util.FastMath;
/**
* <p>
* This is a utility class that provides computation methods related to the
* &Gamma; (Gamma) family of functions.
* </p>
* <p>
* Implementation of {@link #invGamma1pm1(double)} and
* {@link #logGamma1p(double)} is based on the algorithms described in
* <ul>
* <li><a href="http://dx.doi.org/10.1145/22721.23109">Didonato and Morris
* (1986)</a>, <em>Computation of the Incomplete Gamma Function Ratios and
* their Inverse</em>, TOMS 12(4), 377-393,</li>
* <li><a href="http://dx.doi.org/10.1145/131766.131776">Didonato and Morris
* (1992)</a>, <em>Algorithm 708: Significant Digit Computation of the
* Incomplete Beta Function Ratios</em>, TOMS 18(3), 360-373,</li>
* </ul>
* and implemented in the
* <a href="http://www.dtic.mil/docs/citations/ADA476840">NSWC Library of Mathematical Functions</a>,
* available
* <a href="http://www.ualberta.ca/CNS/RESEARCH/Software/NumericalNSWC/site.html">here</a>.
* This library is "approved for public release", and the
* <a href="http://www.dtic.mil/dtic/pdf/announcements/CopyrightGuidance.pdf">Copyright guidance</a>
* 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.
*
*/
public class Gamma {
/**
* <a href="http://en.wikipedia.org/wiki/Euler-Mascheroni_constant">Euler-Mascheroni constant</a>
*
* @since 2.0
*/
public static final double GAMMA = 0.577215664901532860606512090082;
/**
* Constant \( g = \frac{607}{128} \) in the {@link #lanczos(double) Lanczos approximation}.
*
* @since 3.1
*/
public static final double LANCZOS_G = 607.0 / 128.0;
/** Maximum allowed numerical error. */
private static final double DEFAULT_EPSILON = 10e-15;
/** Lanczos coefficients */
private static final double[] LANCZOS = {
0.99999999999999709182,
57.156235665862923517,
-59.597960355475491248,
14.136097974741747174,
-0.49191381609762019978,
.33994649984811888699e-4,
.46523628927048575665e-4,
-.98374475304879564677e-4,
.15808870322491248884e-3,
-.21026444172410488319e-3,
.21743961811521264320e-3,
-.16431810653676389022e-3,
.84418223983852743293e-4,
-.26190838401581408670e-4,
.36899182659531622704e-5,
};
/** Avoid repeated computation of log of 2 PI in logGamma */
private static final double HALF_LOG_2_PI = 0.5 * FastMath.log(2.0 * FastMath.PI);
/** The constant value of &radic;(2&pi;). */
private static final double SQRT_TWO_PI = 2.506628274631000502;
// limits for switching algorithm in digamma
/** C limit. */
private static final double C_LIMIT = 49;
/** S limit. */
private static final double S_LIMIT = 1e-5;
/*
* Constants for the computation of double invGamma1pm1(double).
* Copied from DGAM1 in the NSWC library.
*/
/** The constant {@code A0} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_A0 = .611609510448141581788E-08;
/** The constant {@code A1} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_A1 = .624730830116465516210E-08;
/** The constant {@code B1} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_B1 = .203610414066806987300E+00;
/** The constant {@code B2} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_B2 = .266205348428949217746E-01;
/** The constant {@code B3} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_B3 = .493944979382446875238E-03;
/** The constant {@code B4} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_B4 = -.851419432440314906588E-05;
/** The constant {@code B5} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_B5 = -.643045481779353022248E-05;
/** The constant {@code B6} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_B6 = .992641840672773722196E-06;
/** The constant {@code B7} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_B7 = -.607761895722825260739E-07;
/** The constant {@code B8} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_B8 = .195755836614639731882E-09;
/** The constant {@code P0} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_P0 = .6116095104481415817861E-08;
/** The constant {@code P1} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_P1 = .6871674113067198736152E-08;
/** The constant {@code P2} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_P2 = .6820161668496170657918E-09;
/** The constant {@code P3} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_P3 = .4686843322948848031080E-10;
/** The constant {@code P4} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_P4 = .1572833027710446286995E-11;
/** The constant {@code P5} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_P5 = -.1249441572276366213222E-12;
/** The constant {@code P6} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_P6 = .4343529937408594255178E-14;
/** The constant {@code Q1} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_Q1 = .3056961078365221025009E+00;
/** The constant {@code Q2} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_Q2 = .5464213086042296536016E-01;
/** The constant {@code Q3} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_Q3 = .4956830093825887312020E-02;
/** The constant {@code Q4} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_Q4 = .2692369466186361192876E-03;
/** The constant {@code C} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_C = -.422784335098467139393487909917598E+00;
/** The constant {@code C0} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_C0 = .577215664901532860606512090082402E+00;
/** The constant {@code C1} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_C1 = -.655878071520253881077019515145390E+00;
/** The constant {@code C2} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_C2 = -.420026350340952355290039348754298E-01;
/** The constant {@code C3} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_C3 = .166538611382291489501700795102105E+00;
/** The constant {@code C4} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_C4 = -.421977345555443367482083012891874E-01;
/** The constant {@code C5} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_C5 = -.962197152787697356211492167234820E-02;
/** The constant {@code C6} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_C6 = .721894324666309954239501034044657E-02;
/** The constant {@code C7} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_C7 = -.116516759185906511211397108401839E-02;
/** The constant {@code C8} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_C8 = -.215241674114950972815729963053648E-03;
/** The constant {@code C9} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_C9 = .128050282388116186153198626328164E-03;
/** The constant {@code C10} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_C10 = -.201348547807882386556893914210218E-04;
/** The constant {@code C11} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_C11 = -.125049348214267065734535947383309E-05;
/** The constant {@code C12} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_C12 = .113302723198169588237412962033074E-05;
/** The constant {@code C13} defined in {@code DGAM1}. */
private static final double INV_GAMMA1P_M1_C13 = -.205633841697760710345015413002057E-06;
/**
* Class contains only static methods.
*/
private Gamma() {}
/**
* Computes the function \( \ln \Gamma(x) \) for \( x \gt 0 \).
*
* <p>
* For \( x \leq 8 \), the implementation is based on the double precision
* implementation in the <em>NSWC Library of Mathematics Subroutines</em>,
* {@code DGAMLN}. For \( x \geq 8 \), the implementation is based on
* </p>
*
* <ul>
* <li><a href="http://mathworld.wolfram.com/GammaFunction.html">Gamma
* Function</a>, equation (28).</li>
* <li><a href="http://mathworld.wolfram.com/LanczosApproximation.html">
* Lanczos Approximation</a>, equations (1) through (5).</li>
* <li><a href="http://my.fit.edu/~gabdo/gamma.txt">Paul Godfrey, A note on
* the computation of the convergent Lanczos complex Gamma
* approximation</a></li>
* </ul>
*
* @param x Argument.
* @return \( \ln \Gamma(x) \), or {@code NaN} if {@code x <= 0}.
*/
public static double logGamma(double x) {
double ret;
if (Double.isNaN(x) || (x <= 0.0)) {
ret = Double.NaN;
} else if (x < 0.5) {
return logGamma1p(x) - FastMath.log(x);
} else if (x <= 2.5) {
return logGamma1p((x - 0.5) - 0.5);
} else if (x <= 8.0) {
final int n = (int) FastMath.floor(x - 1.5);
double prod = 1.0;
for (int i = 1; i <= n; i++) {
prod *= x - i;
}
return logGamma1p(x - (n + 1)) + FastMath.log(prod);
} else {
double sum = lanczos(x);
double tmp = x + LANCZOS_G + .5;
ret = ((x + .5) * FastMath.log(tmp)) - tmp +
HALF_LOG_2_PI + FastMath.log(sum / x);
}
return ret;
}
/**
* Computes the regularized gamma function \( P(a, x) \).
*
* @param a Parameter \( a \).
* @param x Value.
* @return \( P(a, x) \)
* @throws MaxCountExceededException if the algorithm fails to converge.
*/
public static double regularizedGammaP(double a, double x) {
return regularizedGammaP(a, x, DEFAULT_EPSILON, Integer.MAX_VALUE);
}
/**
* Computes the regularized gamma function \( P(a, x) \).
*
* The implementation of this method is based on:
* <ul>
* <li>
* <a href="http://mathworld.wolfram.com/RegularizedGammaFunction.html">
* Regularized Gamma Function</a>, equation (1)
* </li>
* <li>
* <a href="http://mathworld.wolfram.com/IncompleteGammaFunction.html">
* Incomplete Gamma Function</a>, equation (4).
* </li>
* <li>
* <a href="http://mathworld.wolfram.com/ConfluentHypergeometricFunctionoftheFirstKind.html">
* Confluent Hypergeometric Function of the First Kind</a>, equation (1).
* </li>
* </ul>
*
* @param a Parameter \( a \).
* @param x Argument.
* @param 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.
* @param maxIterations Maximum number of "iterations" to complete.
* @return \( P(a, x) \)
* @throws MaxCountExceededException if the algorithm fails to converge.
*/
public static double regularizedGammaP(double a,
double x,
double epsilon,
int maxIterations) {
double ret;
if (Double.isNaN(a) || Double.isNaN(x) || (a <= 0.0) || (x < 0.0)) {
ret = Double.NaN;
} else if (x == 0.0) {
ret = 0.0;
} else if (x >= a + 1) {
// use regularizedGammaQ because it should converge faster in this
// case.
ret = 1.0 - regularizedGammaQ(a, x, epsilon, maxIterations);
} else {
// calculate series
double n = 0.0; // current element index
double an = 1.0 / a; // n-th element in the series
double sum = an; // partial sum
while (FastMath.abs(an/sum) > epsilon &&
n < maxIterations &&
sum < Double.POSITIVE_INFINITY) {
// compute next element in the series
n += 1.0;
an *= x / (a + n);
// update partial sum
sum += an;
}
if (n >= maxIterations) {
throw new MaxCountExceededException(maxIterations);
} else if (Double.isInfinite(sum)) {
ret = 1.0;
} else {
ret = FastMath.exp(-x + (a * FastMath.log(x)) - logGamma(a)) * sum;
}
}
return ret;
}
/**
* Computes the regularized gamma function \( Q(a, x) = 1 - P(a, x) \).
*
* @param a Parameter \( a \).
* @param x Argument.
* @return \( Q(a, x) \)
* @throws MaxCountExceededException if the algorithm fails to converge.
*/
public static double regularizedGammaQ(double a, double x) {
return regularizedGammaQ(a, x, DEFAULT_EPSILON, Integer.MAX_VALUE);
}
/**
* Computes the regularized gamma function \( Q(a, x) = 1 - P(a, x) \).
*
* The implementation of this method is based on:
* <ul>
* <li>
* <a href="http://mathworld.wolfram.com/RegularizedGammaFunction.html">
* Regularized Gamma Function</a>, equation (1).
* </li>
* <li>
* <a href="http://functions.wolfram.com/GammaBetaErf/GammaRegularized/10/0003/">
* Regularized incomplete gamma function: Continued fraction representations
* (formula 06.08.10.0003)</a>
* </li>
* </ul>
*
* @param a Parameter \( a \).
* @param x Argument.
* @param 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.
* @param maxIterations Maximum number of "iterations" to complete.
* @return \( Q(a, x) \)
* @throws MaxCountExceededException if the algorithm fails to converge.
*/
public static double regularizedGammaQ(final double a,
double x,
double epsilon,
int maxIterations) {
double ret;
if (Double.isNaN(a) || Double.isNaN(x) || (a <= 0.0) || (x < 0.0)) {
ret = Double.NaN;
} else if (x == 0.0) {
ret = 1.0;
} else if (x < a + 1.0) {
// use regularizedGammaP because it should converge faster in this
// case.
ret = 1.0 - regularizedGammaP(a, x, epsilon, maxIterations);
} else {
// create continued fraction
ContinuedFraction cf = new ContinuedFraction() {
/** {@inheritDoc} */
@Override
protected double getA(int n, double x) {
return ((2.0 * n) + 1.0) - a + x;
}
/** {@inheritDoc} */
@Override
protected double getB(int n, double x) {
return n * (a - n);
}
};
ret = 1.0 / cf.evaluate(x, epsilon, maxIterations);
ret = FastMath.exp(-x + (a * FastMath.log(x)) - logGamma(a)) * ret;
}
return ret;
}
/**
* 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)} \).
*
* <p>This is an independently written implementation of the algorithm described in
* Jose Bernardo, Algorithm AS 103: Psi (Digamma) Function, Applied Statistics, 1976.
* A <a href="https://en.wikipedia.org/wiki/Digamma_function#Reflection_formula">
* reflection formula</a> is incorporated to improve performance on negative values.</p>
*
* <p>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 \le x \le 10^{-5} \) and within \( 10^{-8} \) absolute
* tolerance otherwise.</p>
*
* @param x Argument.
* @return digamma(x) to within \( 10^{-8} \) relative or absolute error whichever is larger.
*
* @see <a href="http://en.wikipedia.org/wiki/Digamma_function">Digamma</a>
* @see <a href="http://www.uv.es/~bernardo/1976AppStatist.pdf">Bernardo's original article</a>
*
* @since 2.0
*/
public static double digamma(double x) {
if (Double.isNaN(x) || Double.isInfinite(x)) {
return x;
}
double digamma = 0.0;
if (x < 0) {
// use reflection formula to fall back into positive values
digamma -= FastMath.PI / FastMath.tan(FastMath.PI * x);
x = 1 - x;
}
if (x > 0 && x <= S_LIMIT) {
// use method 5 from Bernardo AS103
// accurate to O(x)
return digamma -GAMMA - 1 / x;
}
while (x < C_LIMIT) {
digamma -= 1.0 / x;
x += 1.0;
}
// use method 4 (accurate to O(1/x^8)
double inv = 1 / (x * x);
// 1 1 1 1
// log(x) - --- - ------ + ------- - -------
// 2 x 12 x^2 120 x^4 252 x^6
digamma += FastMath.log(x) - 0.5 / x - inv * ((1.0 / 12) + inv * (1.0 / 120 - inv / 252));
return digamma;
}
/**
* Computes the trigamma function \( \psi_1(x) = \frac{d^2}{dx^2} (\ln \Gamma(x)) \).
* <p>
* This function is the derivative of the {@link #digamma(double) digamma function}.
* </p>
*
* @param x Argument.
* @return \( \psi_1(x) \) to within \( 10^{-8} \) relative or absolute
* error whichever is smaller
*
* @see <a href="http://en.wikipedia.org/wiki/Trigamma_function">Trigamma</a>
* @see #digamma(double)
*
* @since 2.0
*/
public static double trigamma(double x) {
if (Double.isNaN(x) || Double.isInfinite(x)) {
return x;
}
if (x > 0 && x <= S_LIMIT) {
return 1 / (x * x);
}
if (x >= C_LIMIT) {
double inv = 1 / (x * x);
// 1 1 1 1 1
// - + ---- + ---- - ----- + -----
// x 2 3 5 7
// 2 x 6 x 30 x 42 x
return 1 / x + inv / 2 + inv / x * (1.0 / 6 - inv * (1.0 / 30 + inv / 42));
}
return trigamma(x + 1) + 1 / (x * x);
}
/**
* Computes the Lanczos approximation used to compute the gamma function.
*
* <p>
* 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.
* </p>
*
* @param x Argument.
* @return The Lanczos approximation.
*
* @see <a href="http://mathworld.wolfram.com/LanczosApproximation.html">Lanczos Approximation</a>
* equations (1) through (5), and Paul Godfrey's
* <a href="http://my.fit.edu/~gabdo/gamma.txt">Note on the computation
* of the convergent Lanczos complex Gamma approximation</a>
*
* @since 3.1
*/
public static double lanczos(final double x) {
double sum = 0.0;
for (int i = LANCZOS.length - 1; i > 0; --i) {
sum += LANCZOS[i] / (x + i);
}
return sum + LANCZOS[0];
}
/**
* Computes the function \( \frac{1}{\Gamma(1 + x)} - 1 \) for \( -0.5 \leq x \leq 1.5 \).
* <p>
* This implementation is based on the double precision implementation in
* the <em>NSWC Library of Mathematics Subroutines</em>, {@code DGAM1}.
* </p>
*
* @param x Argument.
* @return \( \frac{1}{\Gamma(1 + x)} - 1 \)
* @throws NumberIsTooSmallException if {@code x < -0.5}
* @throws NumberIsTooLargeException if {@code x > 1.5}
*
* @since 3.1
*/
public static double invGamma1pm1(final double x) {
if (x < -0.5) {
throw new NumberIsTooSmallException(x, -0.5, true);
}
if (x > 1.5) {
throw new NumberIsTooLargeException(x, 1.5, true);
}
final double ret;
final double t = x <= 0.5 ? x : (x - 0.5) - 0.5;
if (t < 0.0) {
final double a = INV_GAMMA1P_M1_A0 + t * INV_GAMMA1P_M1_A1;
double b = INV_GAMMA1P_M1_B8;
b = INV_GAMMA1P_M1_B7 + t * b;
b = INV_GAMMA1P_M1_B6 + t * b;
b = INV_GAMMA1P_M1_B5 + t * b;
b = INV_GAMMA1P_M1_B4 + t * b;
b = INV_GAMMA1P_M1_B3 + t * b;
b = INV_GAMMA1P_M1_B2 + t * b;
b = INV_GAMMA1P_M1_B1 + t * b;
b = 1.0 + t * b;
double c = INV_GAMMA1P_M1_C13 + t * (a / b);
c = INV_GAMMA1P_M1_C12 + t * c;
c = INV_GAMMA1P_M1_C11 + t * c;
c = INV_GAMMA1P_M1_C10 + t * c;
c = INV_GAMMA1P_M1_C9 + t * c;
c = INV_GAMMA1P_M1_C8 + t * c;
c = INV_GAMMA1P_M1_C7 + t * c;
c = INV_GAMMA1P_M1_C6 + t * c;
c = INV_GAMMA1P_M1_C5 + t * c;
c = INV_GAMMA1P_M1_C4 + t * c;
c = INV_GAMMA1P_M1_C3 + t * c;
c = INV_GAMMA1P_M1_C2 + t * c;
c = INV_GAMMA1P_M1_C1 + t * c;
c = INV_GAMMA1P_M1_C + t * c;
if (x > 0.5) {
ret = t * c / x;
} else {
ret = x * ((c + 0.5) + 0.5);
}
} else {
double p = INV_GAMMA1P_M1_P6;
p = INV_GAMMA1P_M1_P5 + t * p;
p = INV_GAMMA1P_M1_P4 + t * p;
p = INV_GAMMA1P_M1_P3 + t * p;
p = INV_GAMMA1P_M1_P2 + t * p;
p = INV_GAMMA1P_M1_P1 + t * p;
p = INV_GAMMA1P_M1_P0 + t * p;
double q = INV_GAMMA1P_M1_Q4;
q = INV_GAMMA1P_M1_Q3 + t * q;
q = INV_GAMMA1P_M1_Q2 + t * q;
q = INV_GAMMA1P_M1_Q1 + t * q;
q = 1.0 + t * q;
double c = INV_GAMMA1P_M1_C13 + (p / q) * t;
c = INV_GAMMA1P_M1_C12 + t * c;
c = INV_GAMMA1P_M1_C11 + t * c;
c = INV_GAMMA1P_M1_C10 + t * c;
c = INV_GAMMA1P_M1_C9 + t * c;
c = INV_GAMMA1P_M1_C8 + t * c;
c = INV_GAMMA1P_M1_C7 + t * c;
c = INV_GAMMA1P_M1_C6 + t * c;
c = INV_GAMMA1P_M1_C5 + t * c;
c = INV_GAMMA1P_M1_C4 + t * c;
c = INV_GAMMA1P_M1_C3 + t * c;
c = INV_GAMMA1P_M1_C2 + t * c;
c = INV_GAMMA1P_M1_C1 + t * c;
c = INV_GAMMA1P_M1_C0 + t * c;
if (x > 0.5) {
ret = (t / x) * ((c - 0.5) - 0.5);
} else {
ret = x * c;
}
}
return ret;
}
/**
* Computes the function \( \ln \Gamma(1 + x) \) for \( -0.5 \leq x \leq 1.5 \).
* <p>
* This implementation is based on the double precision implementation in
* the <em>NSWC Library of Mathematics Subroutines</em>, {@code DGMLN1}.
* </p>
*
* @param x Argument.
* @return \( \ln \Gamma(1 + x) \)
* @throws NumberIsTooSmallException if {@code x < -0.5}.
* @throws NumberIsTooLargeException if {@code x > 1.5}.
* @since 3.1
*/
public static double logGamma1p(final double x)
throws NumberIsTooSmallException, NumberIsTooLargeException {
if (x < -0.5) {
throw new NumberIsTooSmallException(x, -0.5, true);
}
if (x > 1.5) {
throw new NumberIsTooLargeException(x, 1.5, true);
}
return -FastMath.log1p(invGamma1pm1(x));
}
/**
* Computes the value of \( \Gamma(x) \).
* <p>
* Based on the <em>NSWC Library of Mathematics Subroutines</em> double
* precision implementation, {@code DGAMMA}.
* </p>
*
* @param x Argument.
* @return \( \Gamma(x) \)
*
* @since 3.1
*/
public static double gamma(final double x) {
if ((x == FastMath.rint(x)) && (x <= 0.0)) {
return Double.NaN;
}
final double ret;
final double absX = FastMath.abs(x);
if (absX <= 20.0) {
if (x >= 1.0) {
/*
* From the recurrence relation
* Gamma(x) = (x - 1) * ... * (x - n) * Gamma(x - n),
* then
* Gamma(t) = 1 / [1 + invGamma1pm1(t - 1)],
* where t = x - n. This means that t must satisfy
* -0.5 <= t - 1 <= 1.5.
*/
double prod = 1.0;
double t = x;
while (t > 2.5) {
t -= 1.0;
prod *= t;
}
ret = prod / (1.0 + invGamma1pm1(t - 1.0));
} else {
/*
* From the recurrence relation
* Gamma(x) = Gamma(x + n + 1) / [x * (x + 1) * ... * (x + n)]
* then
* Gamma(x + n + 1) = 1 / [1 + invGamma1pm1(x + n)],
* which requires -0.5 <= x + n <= 1.5.
*/
double prod = x;
double t = x;
while (t < -0.5) {
t += 1.0;
prod *= t;
}
ret = 1.0 / (prod * (1.0 + invGamma1pm1(t)));
}
} else {
final double y = absX + LANCZOS_G + 0.5;
final double gammaAbs = SQRT_TWO_PI / absX *
FastMath.pow(y, absX + 0.5) *
FastMath.exp(-y) * lanczos(absX);
if (x > 0.0) {
ret = gammaAbs;
} else {
/*
* From the reflection formula
* Gamma(x) * Gamma(1 - x) * sin(pi * x) = pi,
* and the recurrence relation
* Gamma(1 - x) = -x * Gamma(-x),
* it is found
* Gamma(x) = -pi / [x * sin(pi * x) * Gamma(-x)].
*/
ret = -FastMath.PI /
(x * FastMath.sin(FastMath.PI * x) * gammaAbs);
}
}
return ret;
}
}

View File

@ -1,979 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.special;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.apache.commons.math4.TestUtils;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.exception.NumberIsTooSmallException;
import org.apache.commons.math4.exception.OutOfRangeException;
import org.apache.commons.math4.special.Beta;
import org.apache.commons.math4.util.FastMath;
import org.junit.Assert;
import org.junit.Test;
/**
*/
public class BetaTest {
/*
* Use reflection to test private methods.
*/
private static final Method LOG_GAMMA_SUM_METHOD;
private static final Method LOG_GAMMA_MINUS_LOG_GAMMA_SUM_METHOD;
private static final Method SUM_DELTA_MINUS_DELTA_SUM_METHOD;
static {
final Class<Beta> b;
final Class<Double> d = Double.TYPE;
b = Beta.class;
Method m = null;
try {
m = b.getDeclaredMethod("logGammaSum", d, d);
} catch (NoSuchMethodException e) {
Assert.fail(e.getMessage());
}
LOG_GAMMA_SUM_METHOD = m;
LOG_GAMMA_SUM_METHOD.setAccessible(true);
m = null;
try {
m = b.getDeclaredMethod("logGammaMinusLogGammaSum",d, d);
} catch (NoSuchMethodException e) {
Assert.fail(e.getMessage());
}
LOG_GAMMA_MINUS_LOG_GAMMA_SUM_METHOD = m;
LOG_GAMMA_MINUS_LOG_GAMMA_SUM_METHOD.setAccessible(true);
m = null;
try {
m = b.getDeclaredMethod("sumDeltaMinusDeltaSum",d, d);
} catch (NoSuchMethodException e) {
Assert.fail(e.getMessage());
}
SUM_DELTA_MINUS_DELTA_SUM_METHOD = m;
SUM_DELTA_MINUS_DELTA_SUM_METHOD.setAccessible(true);
}
private void testRegularizedBeta(double expected, double x,
double a, double b) {
double actual = Beta.regularizedBeta(x, a, b);
TestUtils.assertEquals(expected, actual, 10e-15);
}
private void testLogBeta(double expected, double a, double b) {
double actual = Beta.logBeta(a, b);
TestUtils.assertEquals(expected, actual, 10e-15);
}
@Test
public void testRegularizedBetaNanPositivePositive() {
testRegularizedBeta(Double.NaN, Double.NaN, 1.0, 1.0);
}
@Test
public void testRegularizedBetaPositiveNanPositive() {
testRegularizedBeta(Double.NaN, 0.5, Double.NaN, 1.0);
}
@Test
public void testRegularizedBetaPositivePositiveNan() {
testRegularizedBeta(Double.NaN, 0.5, 1.0, Double.NaN);
}
@Test
public void testRegularizedBetaNegativePositivePositive() {
testRegularizedBeta(Double.NaN, -0.5, 1.0, 2.0);
}
@Test
public void testRegularizedBetaPositiveNegativePositive() {
testRegularizedBeta(Double.NaN, 0.5, -1.0, 2.0);
}
@Test
public void testRegularizedBetaPositivePositiveNegative() {
testRegularizedBeta(Double.NaN, 0.5, 1.0, -2.0);
}
@Test
public void testRegularizedBetaZeroPositivePositive() {
testRegularizedBeta(0.0, 0.0, 1.0, 2.0);
}
@Test
public void testRegularizedBetaPositiveZeroPositive() {
testRegularizedBeta(Double.NaN, 0.5, 0.0, 2.0);
}
@Test
public void testRegularizedBetaPositivePositiveZero() {
testRegularizedBeta(Double.NaN, 0.5, 1.0, 0.0);
}
@Test
public void testRegularizedBetaPositivePositivePositive() {
testRegularizedBeta(0.75, 0.5, 1.0, 2.0);
}
@Test
public void testRegularizedBetaTinyArgument() {
double actual = Beta.regularizedBeta(1e-17, 1.0, 1e12);
// This value is from R: pbeta(1e-17,1,1e12)
TestUtils.assertEquals(9.999950000166648e-6, actual, 1e-16);
}
@Test
public void testMath1067() {
final double x = 0.22580645161290325;
final double a = 64.33333333333334;
final double b = 223;
try {
Beta.regularizedBeta(x, a, b, 1e-14, 10000);
} catch (StackOverflowError error) {
Assert.fail("Infinite recursion");
}
}
@Test
public void testLogBetaNanPositive() {
testLogBeta(Double.NaN, Double.NaN, 2.0);
}
@Test
public void testLogBetaPositiveNan() {
testLogBeta(Double.NaN, 1.0, Double.NaN);
}
@Test
public void testLogBetaNegativePositive() {
testLogBeta(Double.NaN, -1.0, 2.0);
}
@Test
public void testLogBetaPositiveNegative() {
testLogBeta(Double.NaN, 1.0, -2.0);
}
@Test
public void testLogBetaZeroPositive() {
testLogBeta(Double.NaN, 0.0, 2.0);
}
@Test
public void testLogBetaPositiveZero() {
testLogBeta(Double.NaN, 1.0, 0.0);
}
@Test
public void testLogBetaPositivePositive() {
testLogBeta(-0.693147180559945, 1.0, 2.0);
}
/**
* Reference data for the {@link #logGammaSum(double, double)}
* function. This data was generated with the following
* <a href="http://maxima.sourceforge.net/">Maxima</a> script.
*
* <pre>
* kill(all);
*
* fpprec : 64;
* gsumln(a, b) := log(gamma(a + b));
*
* x : [1.0b0, 1.125b0, 1.25b0, 1.375b0, 1.5b0, 1.625b0, 1.75b0, 1.875b0, 2.0b0];
*
* for i : 1 while i <= length(x) do
* for j : 1 while j <= length(x) do block(
* a : x[i],
* b : x[j],
* print("{", float(a), ",", float(b), ",", float(gsumln(a, b)), "},")
* );
* </pre>
*/
private static final double[][] LOG_GAMMA_SUM_REF = {
{ 1.0 , 1.0 , 0.0 },
{ 1.0 , 1.125 , .05775985153034387 },
{ 1.0 , 1.25 , .1248717148923966 },
{ 1.0 , 1.375 , .2006984603774558 },
{ 1.0 , 1.5 , .2846828704729192 },
{ 1.0 , 1.625 , .3763336820249054 },
{ 1.0 , 1.75 , .4752146669149371 },
{ 1.0 , 1.875 , .5809359740231859 },
{ 1.0 , 2.0 , .6931471805599453 },
{ 1.125 , 1.0 , .05775985153034387 },
{ 1.125 , 1.125 , .1248717148923966 },
{ 1.125 , 1.25 , .2006984603774558 },
{ 1.125 , 1.375 , .2846828704729192 },
{ 1.125 , 1.5 , .3763336820249054 },
{ 1.125 , 1.625 , .4752146669149371 },
{ 1.125 , 1.75 , .5809359740231859 },
{ 1.125 , 1.875 , .6931471805599453 },
{ 1.125 , 2.0 , 0.811531653906724 },
{ 1.25 , 1.0 , .1248717148923966 },
{ 1.25 , 1.125 , .2006984603774558 },
{ 1.25 , 1.25 , .2846828704729192 },
{ 1.25 , 1.375 , .3763336820249054 },
{ 1.25 , 1.5 , .4752146669149371 },
{ 1.25 , 1.625 , .5809359740231859 },
{ 1.25 , 1.75 , .6931471805599453 },
{ 1.25 , 1.875 , 0.811531653906724 },
{ 1.25 , 2.0 , .9358019311087253 },
{ 1.375 , 1.0 , .2006984603774558 },
{ 1.375 , 1.125 , .2846828704729192 },
{ 1.375 , 1.25 , .3763336820249054 },
{ 1.375 , 1.375 , .4752146669149371 },
{ 1.375 , 1.5 , .5809359740231859 },
{ 1.375 , 1.625 , .6931471805599453 },
{ 1.375 , 1.75 , 0.811531653906724 },
{ 1.375 , 1.875 , .9358019311087253 },
{ 1.375 , 2.0 , 1.06569589786406 },
{ 1.5 , 1.0 , .2846828704729192 },
{ 1.5 , 1.125 , .3763336820249054 },
{ 1.5 , 1.25 , .4752146669149371 },
{ 1.5 , 1.375 , .5809359740231859 },
{ 1.5 , 1.5 , .6931471805599453 },
{ 1.5 , 1.625 , 0.811531653906724 },
{ 1.5 , 1.75 , .9358019311087253 },
{ 1.5 , 1.875 , 1.06569589786406 },
{ 1.5 , 2.0 , 1.200973602347074 },
{ 1.625 , 1.0 , .3763336820249054 },
{ 1.625 , 1.125 , .4752146669149371 },
{ 1.625 , 1.25 , .5809359740231859 },
{ 1.625 , 1.375 , .6931471805599453 },
{ 1.625 , 1.5 , 0.811531653906724 },
{ 1.625 , 1.625 , .9358019311087253 },
{ 1.625 , 1.75 , 1.06569589786406 },
{ 1.625 , 1.875 , 1.200973602347074 },
{ 1.625 , 2.0 , 1.341414578068493 },
{ 1.75 , 1.0 , .4752146669149371 },
{ 1.75 , 1.125 , .5809359740231859 },
{ 1.75 , 1.25 , .6931471805599453 },
{ 1.75 , 1.375 , 0.811531653906724 },
{ 1.75 , 1.5 , .9358019311087253 },
{ 1.75 , 1.625 , 1.06569589786406 },
{ 1.75 , 1.75 , 1.200973602347074 },
{ 1.75 , 1.875 , 1.341414578068493 },
{ 1.75 , 2.0 , 1.486815578593417 },
{ 1.875 , 1.0 , .5809359740231859 },
{ 1.875 , 1.125 , .6931471805599453 },
{ 1.875 , 1.25 , 0.811531653906724 },
{ 1.875 , 1.375 , .9358019311087253 },
{ 1.875 , 1.5 , 1.06569589786406 },
{ 1.875 , 1.625 , 1.200973602347074 },
{ 1.875 , 1.75 , 1.341414578068493 },
{ 1.875 , 1.875 , 1.486815578593417 },
{ 1.875 , 2.0 , 1.6369886482725 },
{ 2.0 , 1.0 , .6931471805599453 },
{ 2.0 , 1.125 , 0.811531653906724 },
{ 2.0 , 1.25 , .9358019311087253 },
{ 2.0 , 1.375 , 1.06569589786406 },
{ 2.0 , 1.5 , 1.200973602347074 },
{ 2.0 , 1.625 , 1.341414578068493 },
{ 2.0 , 1.75 , 1.486815578593417 },
{ 2.0 , 1.875 , 1.6369886482725 },
{ 2.0 , 2.0 , 1.791759469228055 },
};
private static double logGammaSum(final double a, final double b) {
/*
* Use reflection to access private method.
*/
try {
return ((Double) LOG_GAMMA_SUM_METHOD.invoke(null, a, b)).doubleValue();
} catch (final IllegalAccessException e) {
Assert.fail(e.getMessage());
} catch (final IllegalArgumentException e) {
Assert.fail(e.getMessage());
} catch (final InvocationTargetException e) {
final Throwable te = e.getTargetException();
if (te instanceof MathIllegalArgumentException) {
throw (MathIllegalArgumentException) te;
}
Assert.fail(e.getMessage());
}
return Double.NaN;
}
@Test
public void testLogGammaSum() {
final int ulps = 2;
for (int i = 0; i < LOG_GAMMA_SUM_REF.length; i++) {
final double[] ref = LOG_GAMMA_SUM_REF[i];
final double a = ref[0];
final double b = ref[1];
final double expected = ref[2];
final double actual = logGammaSum(a, b);
final double tol = ulps * FastMath.ulp(expected);
final StringBuilder builder = new StringBuilder();
builder.append(a).append(", ").append(b);
Assert.assertEquals(builder.toString(), expected, actual, tol);
}
}
@Test(expected = OutOfRangeException.class)
public void testLogGammaSumPrecondition1() {
logGammaSum(0.0, 1.0);
}
@Test(expected = OutOfRangeException.class)
public void testLogGammaSumPrecondition2() {
logGammaSum(3.0, 1.0);
}
@Test(expected = OutOfRangeException.class)
public void testLogGammaSumPrecondition3() {
logGammaSum(1.0, 0.0);
}
@Test(expected = OutOfRangeException.class)
public void testLogGammaSumPrecondition4() {
logGammaSum(1.0, 3.0);
}
private static final double[][] LOG_GAMMA_MINUS_LOG_GAMMA_SUM_REF = {
// { 0.0 , 8.0 , 0.0 },
// { 0.0 , 9.0 , 0.0 },
{ 0.0 , 10.0 , 0.0 },
{ 0.0 , 11.0 , 0.0 },
{ 0.0 , 12.0 , 0.0 },
{ 0.0 , 13.0 , 0.0 },
{ 0.0 , 14.0 , 0.0 },
{ 0.0 , 15.0 , 0.0 },
{ 0.0 , 16.0 , 0.0 },
{ 0.0 , 17.0 , 0.0 },
{ 0.0 , 18.0 , 0.0 },
// { 1.0 , 8.0 , - 2.079441541679836 },
// { 1.0 , 9.0 , - 2.19722457733622 },
{ 1.0 , 10.0 , - 2.302585092994046 },
{ 1.0 , 11.0 , - 2.397895272798371 },
{ 1.0 , 12.0 , - 2.484906649788 },
{ 1.0 , 13.0 , - 2.564949357461537 },
{ 1.0 , 14.0 , - 2.639057329615258 },
{ 1.0 , 15.0 , - 2.70805020110221 },
{ 1.0 , 16.0 , - 2.772588722239781 },
{ 1.0 , 17.0 , - 2.833213344056216 },
{ 1.0 , 18.0 , - 2.890371757896165 },
// { 2.0 , 8.0 , - 4.276666119016055 },
// { 2.0 , 9.0 , - 4.499809670330265 },
{ 2.0 , 10.0 , - 4.700480365792417 },
{ 2.0 , 11.0 , - 4.882801922586371 },
{ 2.0 , 12.0 , - 5.049856007249537 },
{ 2.0 , 13.0 , - 5.204006687076795 },
{ 2.0 , 14.0 , - 5.347107530717468 },
{ 2.0 , 15.0 , - 5.480638923341991 },
{ 2.0 , 16.0 , - 5.605802066295998 },
{ 2.0 , 17.0 , - 5.723585101952381 },
{ 2.0 , 18.0 , - 5.834810737062605 },
// { 3.0 , 8.0 , - 6.579251212010101 },
// { 3.0 , 9.0 , - 6.897704943128636 },
{ 3.0 , 10.0 , - 7.185387015580416 },
{ 3.0 , 11.0 , - 7.447751280047908 },
{ 3.0 , 12.0 , - 7.688913336864796 },
{ 3.0 , 13.0 , - 7.912056888179006 },
{ 3.0 , 14.0 , - 8.11969625295725 },
{ 3.0 , 15.0 , - 8.313852267398207 },
{ 3.0 , 16.0 , - 8.496173824192162 },
{ 3.0 , 17.0 , - 8.668024081118821 },
{ 3.0 , 18.0 , - 8.830543010616596 },
// { 4.0 , 8.0 , - 8.977146484808472 },
// { 4.0 , 9.0 , - 9.382611592916636 },
{ 4.0 , 10.0 , - 9.750336373041954 },
{ 4.0 , 11.0 , - 10.08680860966317 },
{ 4.0 , 12.0 , - 10.39696353796701 },
{ 4.0 , 13.0 , - 10.68464561041879 },
{ 4.0 , 14.0 , - 10.95290959701347 },
{ 4.0 , 15.0 , - 11.20422402529437 },
{ 4.0 , 16.0 , - 11.4406128033586 },
{ 4.0 , 17.0 , - 11.66375635467281 },
{ 4.0 , 18.0 , - 11.87506544834002 },
// { 5.0 , 8.0 , - 11.46205313459647 },
// { 5.0 , 9.0 , - 11.94756095037817 },
{ 5.0 , 10.0 , - 12.38939370265721 },
{ 5.0 , 11.0 , - 12.79485881076538 },
{ 5.0 , 12.0 , - 13.16955226020679 },
{ 5.0 , 13.0 , - 13.517858954475 },
{ 5.0 , 14.0 , - 13.84328135490963 },
{ 5.0 , 15.0 , - 14.14866300446081 },
{ 5.0 , 16.0 , - 14.43634507691259 },
{ 5.0 , 17.0 , - 14.70827879239624 },
{ 5.0 , 18.0 , - 14.96610790169833 },
// { 6.0 , 8.0 , - 14.02700249205801 },
// { 6.0 , 9.0 , - 14.58661827999343 },
{ 6.0 , 10.0 , - 15.09744390375942 },
{ 6.0 , 11.0 , - 15.56744753300516 },
{ 6.0 , 12.0 , - 16.002765604263 },
{ 6.0 , 13.0 , - 16.40823071237117 },
{ 6.0 , 14.0 , - 16.78772033407607 },
{ 6.0 , 15.0 , - 17.14439527801481 },
{ 6.0 , 16.0 , - 17.48086751463602 },
{ 6.0 , 17.0 , - 17.79932124575455 },
{ 6.0 , 18.0 , - 18.10160211762749 },
// { 7.0 , 8.0 , - 16.66605982167327 },
// { 7.0 , 9.0 , - 17.29466848109564 },
{ 7.0 , 10.0 , - 17.8700326259992 },
{ 7.0 , 11.0 , - 18.40066087706137 },
{ 7.0 , 12.0 , - 18.89313736215917 },
{ 7.0 , 13.0 , - 19.35266969153761 },
{ 7.0 , 14.0 , - 19.78345260763006 },
{ 7.0 , 15.0 , - 20.18891771573823 },
{ 7.0 , 16.0 , - 20.57190996799433 },
{ 7.0 , 17.0 , - 20.9348154616837 },
{ 7.0 , 18.0 , - 21.27965594797543 },
// { 8.0 , 8.0 , - 19.37411002277548 },
// { 8.0 , 9.0 , - 20.06725720333542 },
{ 8.0 , 10.0 , - 20.70324597005542 },
{ 8.0 , 11.0 , - 21.29103263495754 },
{ 8.0 , 12.0 , - 21.83757634132561 },
{ 8.0 , 13.0 , - 22.3484019650916 },
{ 8.0 , 14.0 , - 22.82797504535349 },
{ 8.0 , 15.0 , - 23.27996016909654 },
{ 8.0 , 16.0 , - 23.70740418392348 },
{ 8.0 , 17.0 , - 24.11286929203165 },
{ 8.0 , 18.0 , - 24.49853177284363 },
// { 9.0 , 8.0 , - 22.14669874501526 },
// { 9.0 , 9.0 , - 22.90047054739164 },
{ 9.0 , 10.0 , - 23.59361772795159 },
{ 9.0 , 11.0 , - 24.23547161412398 },
{ 9.0 , 12.0 , - 24.8333086148796 },
{ 9.0 , 13.0 , - 25.39292440281502 },
{ 9.0 , 14.0 , - 25.9190174987118 },
{ 9.0 , 15.0 , - 26.41545438502569 },
{ 9.0 , 16.0 , - 26.88545801427143 },
{ 9.0 , 17.0 , - 27.33174511689985 },
{ 9.0 , 18.0 , - 27.75662831086511 },
// { 10.0 , 8.0 , - 24.97991208907148 },
// { 10.0 , 9.0 , - 25.7908423052878 },
{ 10.0 , 10.0 , - 26.53805670711802 },
{ 10.0 , 11.0 , - 27.23120388767797 },
{ 10.0 , 12.0 , - 27.87783105260302 },
{ 10.0 , 13.0 , - 28.48396685617334 },
{ 10.0 , 14.0 , - 29.05451171464095 },
{ 10.0 , 15.0 , - 29.59350821537364 },
{ 10.0 , 16.0 , - 30.10433383913963 },
{ 10.0 , 17.0 , - 30.58984165492133 },
{ 10.0 , 18.0 , - 31.05246517686944 },
};
private static double logGammaMinusLogGammaSum(final double a, final double b) {
/*
* Use reflection to access private method.
*/
try {
final Method m = LOG_GAMMA_MINUS_LOG_GAMMA_SUM_METHOD;
return ((Double) m.invoke(null, a, b)).doubleValue();
} catch (final IllegalAccessException e) {
Assert.fail(e.getMessage());
} catch (final IllegalArgumentException e) {
Assert.fail(e.getMessage());
} catch (final InvocationTargetException e) {
final Throwable te = e.getTargetException();
if (te instanceof MathIllegalArgumentException) {
throw (MathIllegalArgumentException) te;
}
Assert.fail(e.getMessage());
}
return Double.NaN;
}
@Test
public void testLogGammaMinusLogGammaSum() {
final int ulps = 4;
for (int i = 0; i < LOG_GAMMA_MINUS_LOG_GAMMA_SUM_REF.length; i++) {
final double[] ref = LOG_GAMMA_MINUS_LOG_GAMMA_SUM_REF[i];
final double a = ref[0];
final double b = ref[1];
final double expected = ref[2];
final double actual = logGammaMinusLogGammaSum(a, b);
final double tol = ulps * FastMath.ulp(expected);
final StringBuilder builder = new StringBuilder();
builder.append(a).append(", ").append(b);
Assert.assertEquals(builder.toString(), expected, actual, tol);
}
}
@Test(expected = NumberIsTooSmallException.class)
public void testLogGammaMinusLogGammaSumPrecondition1() {
logGammaMinusLogGammaSum(-1.0, 8.0);
}
@Test(expected = NumberIsTooSmallException.class)
public void testLogGammaMinusLogGammaSumPrecondition2() {
logGammaMinusLogGammaSum(1.0, 7.0);
}
private static final double[][] SUM_DELTA_MINUS_DELTA_SUM_REF = {
{ 10.0 , 10.0 , .01249480717472882 },
{ 10.0 , 11.0 , .01193628470267385 },
{ 10.0 , 12.0 , .01148578547212797 },
{ 10.0 , 13.0 , .01111659739668398 },
{ 10.0 , 14.0 , .01080991216314295 },
{ 10.0 , 15.0 , .01055214134859758 },
{ 10.0 , 16.0 , .01033324912491747 },
{ 10.0 , 17.0 , .01014568069918883 },
{ 10.0 , 18.0 , .009983653199146491 },
{ 10.0 , 19.0 , .009842674320242729 },
{ 10.0 , 20.0 , 0.0097192081956071 },
{ 11.0 , 10.0 , .01193628470267385 },
{ 11.0 , 11.0 , .01135973290745925 },
{ 11.0 , 12.0 , .01089355537047828 },
{ 11.0 , 13.0 , .01051064829297728 },
{ 11.0 , 14.0 , 0.0101918899639826 },
{ 11.0 , 15.0 , .009923438811859604 },
{ 11.0 , 16.0 , .009695052724952705 },
{ 11.0 , 17.0 , 0.00949900745283617 },
{ 11.0 , 18.0 , .009329379874933402 },
{ 11.0 , 19.0 , 0.00918156080743147 },
{ 11.0 , 20.0 , 0.00905191635141762 },
{ 12.0 , 10.0 , .01148578547212797 },
{ 12.0 , 11.0 , .01089355537047828 },
{ 12.0 , 12.0 , .01041365883144029 },
{ 12.0 , 13.0 , .01001867865848564 },
{ 12.0 , 14.0 , 0.00968923999191334 },
{ 12.0 , 15.0 , .009411294976563555 },
{ 12.0 , 16.0 , .009174432043268762 },
{ 12.0 , 17.0 , .008970786693291802 },
{ 12.0 , 18.0 , .008794318926790865 },
{ 12.0 , 19.0 , .008640321527910711 },
{ 12.0 , 20.0 , .008505077879954796 },
{ 13.0 , 10.0 , .01111659739668398 },
{ 13.0 , 11.0 , .01051064829297728 },
{ 13.0 , 12.0 , .01001867865848564 },
{ 13.0 , 13.0 , .009613018147953376 },
{ 13.0 , 14.0 , .009274085618154277 },
{ 13.0 , 15.0 , 0.0089876637564166 },
{ 13.0 , 16.0 , .008743200745261382 },
{ 13.0 , 17.0 , .008532715206686251 },
{ 13.0 , 18.0 , .008350069108807093 },
{ 13.0 , 19.0 , .008190472517984874 },
{ 13.0 , 20.0 , .008050138630244345 },
{ 14.0 , 10.0 , .01080991216314295 },
{ 14.0 , 11.0 , 0.0101918899639826 },
{ 14.0 , 12.0 , 0.00968923999191334 },
{ 14.0 , 13.0 , .009274085618154277 },
{ 14.0 , 14.0 , .008926676241967286 },
{ 14.0 , 15.0 , .008632654302369184 },
{ 14.0 , 16.0 , .008381351102615795 },
{ 14.0 , 17.0 , .008164687232662443 },
{ 14.0 , 18.0 , .007976441942841219 },
{ 14.0 , 19.0 , .007811755112234388 },
{ 14.0 , 20.0 , .007666780069317652 },
{ 15.0 , 10.0 , .01055214134859758 },
{ 15.0 , 11.0 , .009923438811859604 },
{ 15.0 , 12.0 , .009411294976563555 },
{ 15.0 , 13.0 , 0.0089876637564166 },
{ 15.0 , 14.0 , .008632654302369184 },
{ 15.0 , 15.0 , 0.00833179217417291 },
{ 15.0 , 16.0 , .008074310643041299 },
{ 15.0 , 17.0 , .007852047581145882 },
{ 15.0 , 18.0 , .007658712051540045 },
{ 15.0 , 19.0 , .007489384065757007 },
{ 15.0 , 20.0 , .007340165635725612 },
{ 16.0 , 10.0 , .01033324912491747 },
{ 16.0 , 11.0 , .009695052724952705 },
{ 16.0 , 12.0 , .009174432043268762 },
{ 16.0 , 13.0 , .008743200745261382 },
{ 16.0 , 14.0 , .008381351102615795 },
{ 16.0 , 15.0 , .008074310643041299 },
{ 16.0 , 16.0 , .007811229919967624 },
{ 16.0 , 17.0 , .007583876618287594 },
{ 16.0 , 18.0 , .007385899933505551 },
{ 16.0 , 19.0 , .007212328560607852 },
{ 16.0 , 20.0 , .007059220321091879 },
{ 17.0 , 10.0 , .01014568069918883 },
{ 17.0 , 11.0 , 0.00949900745283617 },
{ 17.0 , 12.0 , .008970786693291802 },
{ 17.0 , 13.0 , .008532715206686251 },
{ 17.0 , 14.0 , .008164687232662443 },
{ 17.0 , 15.0 , .007852047581145882 },
{ 17.0 , 16.0 , .007583876618287594 },
{ 17.0 , 17.0 , .007351882161431358 },
{ 17.0 , 18.0 , .007149662089534654 },
{ 17.0 , 19.0 , .006972200907152378 },
{ 17.0 , 20.0 , .006815518216094137 },
{ 18.0 , 10.0 , .009983653199146491 },
{ 18.0 , 11.0 , .009329379874933402 },
{ 18.0 , 12.0 , .008794318926790865 },
{ 18.0 , 13.0 , .008350069108807093 },
{ 18.0 , 14.0 , .007976441942841219 },
{ 18.0 , 15.0 , .007658712051540045 },
{ 18.0 , 16.0 , .007385899933505551 },
{ 18.0 , 17.0 , .007149662089534654 },
{ 18.0 , 18.0 , .006943552208153373 },
{ 18.0 , 19.0 , .006762516574228829 },
{ 18.0 , 20.0 , .006602541598043117 },
{ 19.0 , 10.0 , .009842674320242729 },
{ 19.0 , 11.0 , 0.00918156080743147 },
{ 19.0 , 12.0 , .008640321527910711 },
{ 19.0 , 13.0 , .008190472517984874 },
{ 19.0 , 14.0 , .007811755112234388 },
{ 19.0 , 15.0 , .007489384065757007 },
{ 19.0 , 16.0 , .007212328560607852 },
{ 19.0 , 17.0 , .006972200907152378 },
{ 19.0 , 18.0 , .006762516574228829 },
{ 19.0 , 19.0 , .006578188655176814 },
{ 19.0 , 20.0 , .006415174623476747 },
{ 20.0 , 10.0 , 0.0097192081956071 },
{ 20.0 , 11.0 , 0.00905191635141762 },
{ 20.0 , 12.0 , .008505077879954796 },
{ 20.0 , 13.0 , .008050138630244345 },
{ 20.0 , 14.0 , .007666780069317652 },
{ 20.0 , 15.0 , .007340165635725612 },
{ 20.0 , 16.0 , .007059220321091879 },
{ 20.0 , 17.0 , .006815518216094137 },
{ 20.0 , 18.0 , .006602541598043117 },
{ 20.0 , 19.0 , .006415174623476747 },
{ 20.0 , 20.0 , .006249349445691423 },
};
private static double sumDeltaMinusDeltaSum(final double a,
final double b) {
/*
* Use reflection to access private method.
*/
try {
final Method m = SUM_DELTA_MINUS_DELTA_SUM_METHOD;
return ((Double) m.invoke(null, a, b)).doubleValue();
} catch (final IllegalAccessException e) {
Assert.fail(e.getMessage());
} catch (final IllegalArgumentException e) {
Assert.fail(e.getMessage());
} catch (final InvocationTargetException e) {
final Throwable te = e.getTargetException();
if (te instanceof MathIllegalArgumentException) {
throw (MathIllegalArgumentException) te;
}
Assert.fail(e.getMessage());
}
return Double.NaN;
}
@Test
public void testSumDeltaMinusDeltaSum() {
final int ulps = 3;
for (int i = 0; i < SUM_DELTA_MINUS_DELTA_SUM_REF.length; i++) {
final double[] ref = SUM_DELTA_MINUS_DELTA_SUM_REF[i];
final double a = ref[0];
final double b = ref[1];
final double expected = ref[2];
final double actual = sumDeltaMinusDeltaSum(a, b);
final double tol = ulps * FastMath.ulp(expected);
final StringBuilder builder = new StringBuilder();
builder.append(a).append(", ").append(b);
Assert.assertEquals(builder.toString(), expected, actual, tol);
}
}
@Test(expected = NumberIsTooSmallException.class)
public void testSumDeltaMinusDeltaSumPrecondition1() {
sumDeltaMinusDeltaSum(9.0, 10.0);
}
@Test(expected = NumberIsTooSmallException.class)
public void testSumDeltaMinusDeltaSumPrecondition2() {
sumDeltaMinusDeltaSum(10.0, 9.0);
}
private static final double[][] LOG_BETA_REF = {
{ 0.125 , 0.125 , 2.750814190409515 },
{ 0.125 , 0.25 , 2.444366899981226 },
{ 0.125 , 0.5 , 2.230953804989556 },
{ 0.125 , 1.0 , 2.079441541679836 },
{ 0.125 , 2.0 , 1.961658506023452 },
{ 0.125 , 3.0 , 1.901033884207018 },
{ 0.125 , 4.0 , 1.860211889686763 },
{ 0.125 , 5.0 , 1.829440231020009 },
{ 0.125 , 6.0 , 1.804747618429637 },
{ 0.125 , 7.0 , 1.784128331226902 },
{ 0.125 , 8.0 , 1.766428754127501 },
{ 0.125 , 9.0 , 1.750924567591535 },
{ 0.125 , 10.0 , 1.7371312454592 },
{ 0.125 , 1000.0 , 1.156003642015969 },
{ 0.125 , 1001.0 , 1.155878649827818 },
{ 0.125 , 10000.0 , .8681312798751318 },
{ 0.25 , 0.125 , 2.444366899981226 },
{ 0.25 , 0.25 , 2.003680106471455 },
{ 0.25 , 0.5 , 1.657106516191482 },
{ 0.25 , 1.0 , 1.386294361119891 },
{ 0.25 , 2.0 , 1.163150809805681 },
{ 0.25 , 3.0 , 1.045367774149297 },
{ 0.25 , 4.0 , 0.965325066475761 },
{ 0.25 , 5.0 , .9047004446593261 },
{ 0.25 , 6.0 , .8559102804898941 },
{ 0.25 , 7.0 , 0.815088285969639 },
{ 0.25 , 8.0 , .7799969661583689 },
{ 0.25 , 9.0 , .7492253074916152 },
{ 0.25 , 10.0 , .7218263333035008 },
{ 0.25 , 1000.0 , - .4388225372378877 },
{ 0.25 , 1001.0 , - .4390725059930951 },
{ 0.25 , 10000.0 , - 1.014553193217846 },
{ 0.5 , 0.125 , 2.230953804989556 },
{ 0.5 , 0.25 , 1.657106516191482 },
{ 0.5 , 0.5 , 1.1447298858494 },
{ 0.5 , 1.0 , .6931471805599453 },
{ 0.5 , 2.0 , .2876820724517809 },
{ 0.5 , 3.0 , .06453852113757118 },
// { 0.5 , 4.0 , - .08961215868968714 },
{ 0.5 , 5.0 , - .2073951943460706 },
{ 0.5 , 6.0 , - .3027053741503954 },
{ 0.5 , 7.0 , - .3827480818239319 },
{ 0.5 , 8.0 , - .4517409533108833 },
{ 0.5 , 9.0 , - .5123655751273182 },
{ 0.5 , 10.0 , - .5664327963975939 },
{ 0.5 , 1000.0 , - 2.881387696571577 },
{ 0.5 , 1001.0 , - 2.881887571613228 },
{ 0.5 , 10000.0 , - 4.032792743063396 },
{ 1.0 , 0.125 , 2.079441541679836 },
{ 1.0 , 0.25 , 1.386294361119891 },
{ 1.0 , 0.5 , .6931471805599453 },
{ 1.0 , 1.0 , 0.0 },
{ 1.0 , 2.0 , - .6931471805599453 },
{ 1.0 , 3.0 , - 1.09861228866811 },
{ 1.0 , 4.0 , - 1.386294361119891 },
{ 1.0 , 5.0 , - 1.6094379124341 },
{ 1.0 , 6.0 , - 1.791759469228055 },
{ 1.0 , 7.0 , - 1.945910149055313 },
{ 1.0 , 8.0 , - 2.079441541679836 },
{ 1.0 , 9.0 , - 2.19722457733622 },
{ 1.0 , 10.0 , - 2.302585092994046 },
{ 1.0 , 1000.0 , - 6.907755278982137 },
{ 1.0 , 1001.0 , - 6.90875477931522 },
{ 1.0 , 10000.0 , - 9.210340371976184 },
{ 2.0 , 0.125 , 1.961658506023452 },
{ 2.0 , 0.25 , 1.163150809805681 },
{ 2.0 , 0.5 , .2876820724517809 },
{ 2.0 , 1.0 , - .6931471805599453 },
{ 2.0 , 2.0 , - 1.791759469228055 },
{ 2.0 , 3.0 , - 2.484906649788 },
{ 2.0 , 4.0 , - 2.995732273553991 },
{ 2.0 , 5.0 , - 3.401197381662155 },
{ 2.0 , 6.0 , - 3.737669618283368 },
{ 2.0 , 7.0 , - 4.02535169073515 },
{ 2.0 , 8.0 , - 4.276666119016055 },
{ 2.0 , 9.0 , - 4.499809670330265 },
{ 2.0 , 10.0 , - 4.700480365792417 },
{ 2.0 , 1000.0 , - 13.81651005829736 },
{ 2.0 , 1001.0 , - 13.81850806096003 },
{ 2.0 , 10000.0 , - 18.4207807389527 },
{ 3.0 , 0.125 , 1.901033884207018 },
{ 3.0 , 0.25 , 1.045367774149297 },
{ 3.0 , 0.5 , .06453852113757118 },
{ 3.0 , 1.0 , - 1.09861228866811 },
{ 3.0 , 2.0 , - 2.484906649788 },
{ 3.0 , 3.0 , - 3.401197381662155 },
{ 3.0 , 4.0 , - 4.0943445622221 },
{ 3.0 , 5.0 , - 4.653960350157523 },
{ 3.0 , 6.0 , - 5.123963979403259 },
{ 3.0 , 7.0 , - 5.529429087511423 },
{ 3.0 , 8.0 , - 5.886104031450156 },
{ 3.0 , 9.0 , - 6.20455776256869 },
{ 3.0 , 10.0 , - 6.492239835020471 },
{ 3.0 , 1000.0 , - 20.03311615938222 },
{ 3.0 , 1001.0 , - 20.03611166836202 },
{ 3.0 , 10000.0 , - 26.9381739103716 },
{ 4.0 , 0.125 , 1.860211889686763 },
{ 4.0 , 0.25 , 0.965325066475761 },
// { 4.0 , 0.5 , - .08961215868968714 },
{ 4.0 , 1.0 , - 1.386294361119891 },
{ 4.0 , 2.0 , - 2.995732273553991 },
{ 4.0 , 3.0 , - 4.0943445622221 },
{ 4.0 , 4.0 , - 4.941642422609304 },
{ 4.0 , 5.0 , - 5.634789603169249 },
{ 4.0 , 6.0 , - 6.222576268071369 },
{ 4.0 , 7.0 , - 6.733401891837359 },
{ 4.0 , 8.0 , - 7.185387015580416 },
{ 4.0 , 9.0 , - 7.590852123688581 },
{ 4.0 , 10.0 , - 7.958576903813898 },
{ 4.0 , 1000.0 , - 25.84525465867605 },
{ 4.0 , 1001.0 , - 25.84924667994559 },
{ 4.0 , 10000.0 , - 35.05020194868867 },
{ 5.0 , 0.125 , 1.829440231020009 },
{ 5.0 , 0.25 , .9047004446593261 },
{ 5.0 , 0.5 , - .2073951943460706 },
{ 5.0 , 1.0 , - 1.6094379124341 },
{ 5.0 , 2.0 , - 3.401197381662155 },
{ 5.0 , 3.0 , - 4.653960350157523 },
{ 5.0 , 4.0 , - 5.634789603169249 },
{ 5.0 , 5.0 , - 6.445719819385578 },
{ 5.0 , 6.0 , - 7.138866999945524 },
{ 5.0 , 7.0 , - 7.745002803515839 },
{ 5.0 , 8.0 , - 8.283999304248526 },
{ 5.0 , 9.0 , - 8.769507120030227 },
{ 5.0 , 10.0 , - 9.211339872309265 },
{ 5.0 , 1000.0 , - 31.37070759780783 },
{ 5.0 , 1001.0 , - 31.37569513931887 },
{ 5.0 , 10000.0 , - 42.87464787956629 },
{ 6.0 , 0.125 , 1.804747618429637 },
{ 6.0 , 0.25 , .8559102804898941 },
{ 6.0 , 0.5 , - .3027053741503954 },
{ 6.0 , 1.0 , - 1.791759469228055 },
{ 6.0 , 2.0 , - 3.737669618283368 },
{ 6.0 , 3.0 , - 5.123963979403259 },
{ 6.0 , 4.0 , - 6.222576268071369 },
{ 6.0 , 5.0 , - 7.138866999945524 },
{ 6.0 , 6.0 , - 7.927324360309794 },
{ 6.0 , 7.0 , - 8.620471540869739 },
{ 6.0 , 8.0 , - 9.239510749275963 },
{ 6.0 , 9.0 , - 9.799126537211386 },
{ 6.0 , 10.0 , - 10.30995216097738 },
{ 6.0 , 1000.0 , - 36.67401250586691 },
{ 6.0 , 1001.0 , - 36.67999457754446 },
{ 6.0 , 10000.0 , - 50.47605021415003 },
{ 7.0 , 0.125 , 1.784128331226902 },
{ 7.0 , 0.25 , 0.815088285969639 },
{ 7.0 , 0.5 , - .3827480818239319 },
{ 7.0 , 1.0 , - 1.945910149055313 },
{ 7.0 , 2.0 , - 4.02535169073515 },
{ 7.0 , 3.0 , - 5.529429087511423 },
{ 7.0 , 4.0 , - 6.733401891837359 },
{ 7.0 , 5.0 , - 7.745002803515839 },
{ 7.0 , 6.0 , - 8.620471540869739 },
{ 7.0 , 7.0 , - 9.39366142910322 },
{ 7.0 , 8.0 , - 10.08680860966317 },
{ 7.0 , 9.0 , - 10.71541726908554 },
{ 7.0 , 10.0 , - 11.2907814139891 },
{ 7.0 , 1000.0 , - 41.79599038729854 },
{ 7.0 , 1001.0 , - 41.80296600103496 },
{ 7.0 , 10000.0 , - 57.89523093697012 },
{ 8.0 , 0.125 , 1.766428754127501 },
{ 8.0 , 0.25 , .7799969661583689 },
{ 8.0 , 0.5 , - .4517409533108833 },
{ 8.0 , 1.0 , - 2.079441541679836 },
{ 8.0 , 2.0 , - 4.276666119016055 },
{ 8.0 , 3.0 , - 5.886104031450156 },
{ 8.0 , 4.0 , - 7.185387015580416 },
{ 8.0 , 5.0 , - 8.283999304248526 },
{ 8.0 , 6.0 , - 9.239510749275963 },
{ 8.0 , 7.0 , - 10.08680860966317 },
{ 8.0 , 8.0 , - 10.84894866171006 },
{ 8.0 , 9.0 , - 11.54209584227001 },
{ 8.0 , 10.0 , - 12.17808460899001 },
{ 8.0 , 1000.0 , - 46.76481113096179 },
{ 8.0 , 1001.0 , - 46.77277930061096 },
{ 8.0 , 10000.0 , - 65.16036091500527 },
{ 9.0 , 0.125 , 1.750924567591535 },
{ 9.0 , 0.25 , .7492253074916152 },
{ 9.0 , 0.5 , - .5123655751273182 },
{ 9.0 , 1.0 , - 2.19722457733622 },
{ 9.0 , 2.0 , - 4.499809670330265 },
{ 9.0 , 3.0 , - 6.20455776256869 },
{ 9.0 , 4.0 , - 7.590852123688581 },
{ 9.0 , 5.0 , - 8.769507120030227 },
{ 9.0 , 6.0 , - 9.799126537211386 },
{ 9.0 , 7.0 , - 10.71541726908554 },
{ 9.0 , 8.0 , - 11.54209584227001 },
{ 9.0 , 9.0 , - 12.29586764464639 },
{ 9.0 , 10.0 , - 12.98901482520633 },
{ 9.0 , 1000.0 , - 51.60109303791327 },
{ 9.0 , 1001.0 , - 51.61005277928474 },
{ 9.0 , 10000.0 , - 72.29205942547217 },
{ 10.0 , 0.125 , 1.7371312454592 },
{ 10.0 , 0.25 , .7218263333035008 },
{ 10.0 , 0.5 , - .5664327963975939 },
{ 10.0 , 1.0 , - 2.302585092994046 },
{ 10.0 , 2.0 , - 4.700480365792417 },
{ 10.0 , 3.0 , - 6.492239835020471 },
{ 10.0 , 4.0 , - 7.958576903813898 },
{ 10.0 , 5.0 , - 9.211339872309265 },
{ 10.0 , 6.0 , - 10.30995216097738 },
{ 10.0 , 7.0 , - 11.2907814139891 },
{ 10.0 , 8.0 , - 12.17808460899001 },
{ 10.0 , 9.0 , - 12.98901482520633 },
{ 10.0 , 10.0 , - 13.73622922703655 },
{ 10.0 , 1000.0 , - 56.32058348093065 },
{ 10.0 , 1001.0 , - 56.33053381178382 },
{ 10.0 , 10000.0 , - 79.30607481535498 },
{ 1000.0 , 0.125 , 1.156003642015969 },
{ 1000.0 , 0.25 , - .4388225372378877 },
{ 1000.0 , 0.5 , - 2.881387696571577 },
{ 1000.0 , 1.0 , - 6.907755278982137 },
{ 1000.0 , 2.0 , - 13.81651005829736 },
{ 1000.0 , 3.0 , - 20.03311615938222 },
{ 1000.0 , 4.0 , - 25.84525465867605 },
{ 1000.0 , 5.0 , - 31.37070759780783 },
{ 1000.0 , 6.0 , - 36.67401250586691 },
{ 1000.0 , 7.0 , - 41.79599038729854 },
{ 1000.0 , 8.0 , - 46.76481113096179 },
{ 1000.0 , 9.0 , - 51.60109303791327 },
{ 1000.0 , 10.0 , - 56.32058348093065 },
{ 1000.0 , 1000.0 , - 1388.482601635902 },
{ 1000.0 , 1001.0 , - 1389.175748816462 },
{ 1000.0 , 10000.0 , - 3353.484270767097 },
{ 1001.0 , 0.125 , 1.155878649827818 },
{ 1001.0 , 0.25 , - .4390725059930951 },
{ 1001.0 , 0.5 , - 2.881887571613228 },
{ 1001.0 , 1.0 , - 6.90875477931522 },
{ 1001.0 , 2.0 , - 13.81850806096003 },
{ 1001.0 , 3.0 , - 20.03611166836202 },
{ 1001.0 , 4.0 , - 25.84924667994559 },
{ 1001.0 , 5.0 , - 31.37569513931887 },
{ 1001.0 , 6.0 , - 36.67999457754446 },
{ 1001.0 , 7.0 , - 41.80296600103496 },
{ 1001.0 , 8.0 , - 46.77277930061096 },
{ 1001.0 , 9.0 , - 51.61005277928474 },
{ 1001.0 , 10.0 , - 56.33053381178382 },
{ 1001.0 , 1000.0 , - 1389.175748816462 },
{ 1001.0 , 1001.0 , - 1389.869395872064 },
{ 1001.0 , 10000.0 , - 3355.882166039895 },
{ 10000.0 , 0.125 , .8681312798751318 },
{ 10000.0 , 0.25 , - 1.014553193217846 },
{ 10000.0 , 0.5 , - 4.032792743063396 },
{ 10000.0 , 1.0 , - 9.210340371976184 },
{ 10000.0 , 2.0 , - 18.4207807389527 },
{ 10000.0 , 3.0 , - 26.9381739103716 },
{ 10000.0 , 4.0 , - 35.05020194868867 },
{ 10000.0 , 5.0 , - 42.87464787956629 },
{ 10000.0 , 6.0 , - 50.47605021415003 },
{ 10000.0 , 7.0 , - 57.89523093697012 },
{ 10000.0 , 8.0 , - 65.16036091500527 },
{ 10000.0 , 9.0 , - 72.29205942547217 },
{ 10000.0 , 10.0 , - 79.30607481535498 },
{ 10000.0 , 1000.0 , - 3353.484270767097 },
{ 10000.0 , 1001.0 , - 3355.882166039895 },
{ 10000.0 , 10000.0 , - 13866.28325676141 },
};
@Test
public void testLogBeta() {
final int ulps = 3;
for (int i = 0; i < LOG_BETA_REF.length; i++) {
final double[] ref = LOG_BETA_REF[i];
final double a = ref[0];
final double b = ref[1];
final double expected = ref[2];
final double actual = Beta.logBeta(a, b);
final double tol = ulps * FastMath.ulp(expected);
final StringBuilder builder = new StringBuilder();
builder.append(a).append(", ").append(b);
Assert.assertEquals(builder.toString(), expected, actual, tol);
}
}}

View File

@ -1,998 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.special;
import org.apache.commons.math4.TestUtils;
import org.apache.commons.math4.exception.NumberIsTooLargeException;
import org.apache.commons.math4.exception.NumberIsTooSmallException;
import org.apache.commons.math4.util.FastMath;
import org.junit.Assert;
import org.junit.Test;
/**
*/
public class GammaTest {
private void testRegularizedGamma(double expected, double a, double x) {
double actualP = Gamma.regularizedGammaP(a, x);
double actualQ = Gamma.regularizedGammaQ(a, x);
TestUtils.assertEquals(expected, actualP, 10e-15);
TestUtils.assertEquals(actualP, 1.0 - actualQ, 10e-15);
}
private void testLogGamma(double expected, double x) {
double actual = Gamma.logGamma(x);
TestUtils.assertEquals(expected, actual, 10e-15);
}
@Test
public void testRegularizedGammaNanPositive() {
testRegularizedGamma(Double.NaN, Double.NaN, 1.0);
}
@Test
public void testRegularizedGammaPositiveNan() {
testRegularizedGamma(Double.NaN, 1.0, Double.NaN);
}
@Test
public void testRegularizedGammaNegativePositive() {
testRegularizedGamma(Double.NaN, -1.5, 1.0);
}
@Test
public void testRegularizedGammaPositiveNegative() {
testRegularizedGamma(Double.NaN, 1.0, -1.0);
}
@Test
public void testRegularizedGammaZeroPositive() {
testRegularizedGamma(Double.NaN, 0.0, 1.0);
}
@Test
public void testRegularizedGammaPositiveZero() {
testRegularizedGamma(0.0, 1.0, 0.0);
}
@Test
public void testRegularizedGammaPositivePositive() {
testRegularizedGamma(0.632120558828558, 1.0, 1.0);
}
@Test
public void testLogGammaNan() {
testLogGamma(Double.NaN, Double.NaN);
}
@Test
public void testLogGammaNegative() {
testLogGamma(Double.NaN, -1.0);
}
@Test
public void testLogGammaZero() {
testLogGamma(Double.NaN, 0.0);
}
@Test
public void testLogGammaPositive() {
testLogGamma(0.6931471805599457, 3.0);
}
@Test
public void testDigammaLargeArgs() {
double eps = 1e-8;
Assert.assertEquals(4.6001618527380874002, Gamma.digamma(100), eps);
Assert.assertEquals(3.9019896734278921970, Gamma.digamma(50), eps);
Assert.assertEquals(2.9705239922421490509, Gamma.digamma(20), eps);
Assert.assertEquals(2.9958363947076465821, Gamma.digamma(20.5), eps);
Assert.assertEquals(2.2622143570941481605, Gamma.digamma(10.1), eps);
Assert.assertEquals(2.1168588189004379233, Gamma.digamma(8.8), eps);
Assert.assertEquals(1.8727843350984671394, Gamma.digamma(7), eps);
Assert.assertEquals(0.42278433509846713939, Gamma.digamma(2), eps);
Assert.assertEquals(-100.56088545786867450, Gamma.digamma(0.01), eps);
Assert.assertEquals(-4.0390398965921882955, Gamma.digamma(-0.8), eps);
Assert.assertEquals(4.2003210041401844726, Gamma.digamma(-6.3), eps);
Assert.assertEquals(-3.110625123035E-5, Gamma.digamma(1.4616), eps);
}
@Test
public void testDigammaSmallArgs() {
// values for negative powers of 10 from 1 to 30 as computed by webMathematica with 20 digits
// see functions.wolfram.com
double[] expected = {-10.423754940411076795, -100.56088545786867450, -1000.5755719318103005,
-10000.577051183514335, -100000.57719921568107, -1.0000005772140199687e6, -1.0000000577215500408e7,
-1.0000000057721564845e8, -1.0000000005772156633e9, -1.0000000000577215665e10, -1.0000000000057721566e11,
-1.0000000000005772157e12, -1.0000000000000577216e13, -1.0000000000000057722e14, -1.0000000000000005772e15, -1e+16,
-1e+17, -1e+18, -1e+19, -1e+20, -1e+21, -1e+22, -1e+23, -1e+24, -1e+25, -1e+26,
-1e+27, -1e+28, -1e+29, -1e+30};
for (double n = 1; n < 30; n++) {
checkRelativeError(String.format("Test %.0f: ", n), expected[(int) (n - 1)], Gamma.digamma(FastMath.pow(10.0, -n)), 1e-8);
}
}
@Test
public void testDigammaNonRealArgs() {
Assert.assertTrue(Double.isNaN(Gamma.digamma(Double.NaN)));
Assert.assertTrue(Double.isInfinite(Gamma.digamma(Double.POSITIVE_INFINITY)));
Assert.assertTrue(Double.isInfinite(Gamma.digamma(Double.NEGATIVE_INFINITY)));
}
@Test
public void testTrigamma() {
double eps = 1e-8;
// computed using webMathematica. For example, to compute trigamma($i) = Polygamma(1, $i), use
//
// http://functions.wolfram.com/webMathematica/Evaluated.jsp?name=PolyGamma2&plottype=0&vars={%221%22,%22$i%22}&digits=20
double[] data = {
1e-4, 1.0000000164469368793e8,
1e-3, 1.0000016425331958690e6,
1e-2, 10001.621213528313220,
1e-1, 101.43329915079275882,
1, 1.6449340668482264365,
2, 0.64493406684822643647,
3, 0.39493406684822643647,
4, 0.28382295573711532536,
5, 0.22132295573711532536,
10, 0.10516633568168574612,
20, 0.051270822935203119832,
50, 0.020201333226697125806,
100, 0.010050166663333571395
};
for (int i = data.length - 2; i >= 0; i -= 2) {
Assert.assertEquals(String.format("trigamma %.0f", data[i]), data[i + 1], Gamma.trigamma(data[i]), eps);
}
}
@Test
public void testTrigammaNonRealArgs() {
Assert.assertTrue(Double.isNaN(Gamma.trigamma(Double.NaN)));
Assert.assertTrue(Double.isInfinite(Gamma.trigamma(Double.POSITIVE_INFINITY)));
Assert.assertTrue(Double.isInfinite(Gamma.trigamma(Double.NEGATIVE_INFINITY)));
}
/**
* Reference data for the {@link Gamma#logGamma(double)} function. This data
* was generated with the following <a
* href="http://maxima.sourceforge.net/">Maxima</a> script.
*
* <pre>
* kill(all);
*
* fpprec : 64;
* gamln(x) := log(gamma(x));
* x : append(makelist(bfloat(i / 8), i, 1, 80),
* [0.8b0, 1b2, 1b3, 1b4, 1b5, 1b6, 1b7, 1b8, 1b9, 1b10]);
*
* for i : 1 while i <= length(x) do
* print("{", float(x[i]), ",", float(gamln(x[i])), "},");
* </pre>
*/
private static final double[][] LOG_GAMMA_REF = {
{ 0.125 , 2.019418357553796 },
{ 0.25 , 1.288022524698077 },
{ 0.375 , .8630739822706475 },
{ 0.5 , .5723649429247001 },
{ 0.625 , .3608294954889402 },
{ 0.75 , .2032809514312954 },
{ 0.875 , .08585870722533433 },
{ 0.890625 , .07353860936979656 },
{ 0.90625 , .06169536624059108 },
{ 0.921875 , .05031670080005688 },
{ 0.9375 , 0.0393909017345823 },
{ 0.953125 , .02890678734595923 },
{ 0.96875 , .01885367233441289 },
{ 0.984375 , .009221337197578781 },
{ 1.0 , 0.0 },
{ 1.015625 , - 0.00881970970573307 },
{ 1.03125 , - .01724677500176807 },
{ 1.046875 , - .02528981394675729 },
{ 1.0625 , - .03295710029357782 },
{ 1.078125 , - .04025658272400143 },
{ 1.09375 , - .04719590272716985 },
{ 1.109375 , - .05378241123619192 },
{ 1.125 , - .06002318412603958 },
{ 1.25 , - .09827183642181316 },
{ 1.375 , - .1177552707410788 },
{ 1.5 , - .1207822376352452 },
{ 1.625 , - .1091741337567954 },
{ 1.75 , - .08440112102048555 },
{ 1.875 , - 0.0476726853991883 },
{ 1.890625 , - .04229320615532515 },
{ 1.90625 , - .03674470657266143 },
{ 1.921875 , - .03102893865389552 },
{ 1.9375 , - .02514761940298887 },
{ 1.953125 , - .01910243184040138 },
{ 1.96875 , - .01289502598016741 },
{ 1.984375 , - .006527019770560387 },
{ 2.0 , 0.0 },
{ 2.015625 , .006684476830232185 },
{ 2.03125 , .01352488366498562 },
{ 2.046875 , .02051972208453692 },
{ 2.0625 , .02766752152285702 },
{ 2.078125 , 0.0349668385135861 },
{ 2.09375 , .04241625596251728 },
{ 2.109375 , .05001438244545164 },
{ 2.125 , .05775985153034387 },
{ 2.25 , .1248717148923966 },
{ 2.375 , .2006984603774558 },
{ 2.5 , .2846828704729192 },
{ 2.625 , .3763336820249054 },
{ 2.75 , .4752146669149371 },
{ 2.875 , .5809359740231859 },
{ 2.890625 , .5946142560817441 },
{ 2.90625 , .6083932548009232 },
{ 2.921875 , .6222723333588501 },
{ 2.9375 , .6362508628423761 },
{ 2.953125 , .6503282221022278 },
{ 2.96875 , .6645037976116387 },
{ 2.984375 , 0.678776983328359 },
{ 3.0 , .6931471805599453 },
{ 3.015625 , .7076137978322324 },
{ 3.03125 , .7221762507608962 },
{ 3.046875 , .7368339619260166 },
{ 3.0625 , 0.751586360749556 },
{ 3.078125 , .7664328833756681 },
{ 3.09375 , .7813729725537568 },
{ 3.109375 , .7964060775242092 },
{ 3.125 , 0.811531653906724 },
{ 3.25 , .9358019311087253 },
{ 3.375 , 1.06569589786406 },
{ 3.5 , 1.200973602347074 },
{ 3.625 , 1.341414578068493 },
{ 3.75 , 1.486815578593417 },
{ 3.875 , 1.6369886482725 },
{ 4.0 , 1.791759469228055 },
{ 4.125 , 1.950965937095089 },
{ 4.25 , 2.114456927450371 },
{ 4.375 , 2.282091222188554 },
{ 4.5 , 2.453736570842442 },
{ 4.625 , 2.62926886637513 },
{ 4.75 , 2.808571418575736 },
{ 4.875 , 2.99153431107781 },
{ 5.0 , 3.178053830347946 },
{ 5.125 , 3.368031956881733 },
{ 5.25 , 3.561375910386697 },
{ 5.375 , 3.757997741998131 },
{ 5.5 , 3.957813967618717 },
{ 5.625 , 4.160745237339519 },
{ 5.75 , 4.366716036622286 },
{ 5.875 , 4.57565441552762 },
{ 6.0 , 4.787491742782046 },
{ 6.125 , 5.002162481906205 },
{ 6.25 , 5.219603986990229 },
{ 6.375 , 5.439756316011858 },
{ 6.5 , 5.662562059857142 },
{ 6.625 , 5.887966185430003 },
{ 6.75 , 6.115915891431546 },
{ 6.875 , 6.346360475557843 },
{ 7.0 , 6.579251212010101 },
{ 7.125 , 6.814541238336996 },
{ 7.25 , 7.05218545073854 },
{ 7.375 , 7.292140407056348 },
{ 7.5 , 7.534364236758733 },
{ 7.625 , 7.778816557302289 },
{ 7.75 , 8.025458396315983 },
{ 7.875 , 8.274252119110479 },
{ 8.0 , 8.525161361065415 },
{ 8.125 , 8.77815096449171 },
{ 8.25 , 9.033186919605123 },
{ 8.375 , 9.290236309282232 },
{ 8.5 , 9.549267257300997 },
{ 8.625 , 9.810248879795765 },
{ 8.75 , 10.07315123968124 },
{ 8.875 , 10.33794530382217 },
{ 9.0 , 10.60460290274525 },
{ 9.125 , 10.87309669270751 },
{ 9.25 , 11.14340011995171 },
{ 9.375 , 11.41548738699336 },
{ 9.5 , 11.68933342079727 },
{ 9.625 , 11.96491384271319 },
{ 9.75 , 12.24220494005076 },
{ 9.875 , 12.52118363918365 },
{ 10.0 , 12.80182748008147 },
{ 0.8 , .1520596783998376 },
{ 100.0 , 359.1342053695754 },
{ 1000.0 , 5905.220423209181 },
{ 10000.0 , 82099.71749644238 },
{ 100000.0 , 1051287.708973657 },
{ 1000000.0 , 1.2815504569147612e+7 },
{ 10000000.0 , 1.511809493694739e+8 },
{ 1.e+8 , 1.7420680661038346e+9 },
{ 1.e+9 , 1.972326582750371e+10 },
{ 1.e+10 , 2.202585092888106e+11 },
};
@Test
public void testLogGamma() {
final int ulps = 3;
for (int i = 0; i < LOG_GAMMA_REF.length; i++) {
final double[] data = LOG_GAMMA_REF[i];
final double x = data[0];
final double expected = data[1];
final double actual = Gamma.logGamma(x);
final double tol;
if (expected == 0.0) {
tol = 1E-15;
} else {
tol = ulps * FastMath.ulp(expected);
}
Assert.assertEquals(Double.toString(x), expected, actual, tol);
}
}
@Test
public void testLogGammaPrecondition1() {
Assert.assertTrue(Double.isNaN(Gamma.logGamma(0.0)));
}
@Test
public void testLogGammaPrecondition2() {
Assert.assertTrue(Double.isNaN(Gamma.logGamma(-1.0)));
}
/**
* <p>
* Reference values for the {@link Gamma#invGamma1pm1(double)} method.
* These values were generated with the following <a
* href="http://maxima.sourceforge.net/">Maxima</a> script
* </p>
*
* <pre>
* kill(all);
*
* fpprec : 64;
* gam1(x) := 1 / gamma(1 + x) - 1;
* x : makelist(bfloat(i / 8), i, -4, 12);
*
* for i : 1 while i <= length(x) do print("{",
* float(x[i]),
* ",",
* float(gam1(x[i])),
* "},");
* </pre>
*/
private static final double[][] INV_GAMMA1P_M1_REF = {
{ -0.5 , -.4358104164522437 },
{ -0.375 , -.3029021533379859 },
{ -0.25 , -0.183951060901737 },
{ -0.125 , -.08227611018520711 },
{ 0.0 , 0.0 },
{ 0.125 , .06186116458306091 },
{ 0.25 , .1032626513208373 },
{ 0.375 , .1249687649039041 },
{ 0.5 , .1283791670955126 },
{ 0.625 , .1153565546592225 },
{ 0.75 , 0.0880652521310173 },
{ 0.875 , .04882730264547758 },
{ 1.0 , 0.0 },
{ 1.125 , -.05612340925950141 },
{ 1.25 , -.1173898789433302 },
{ 1.375 , -.1818408982517061 },
{ 1.5 , -0.247747221936325 },
};
@Test
public void testInvGamma1pm1() {
final int ulps = 3;
for (int i = 0; i < INV_GAMMA1P_M1_REF.length; i++) {
final double[] ref = INV_GAMMA1P_M1_REF[i];
final double x = ref[0];
final double expected = ref[1];
final double actual = Gamma.invGamma1pm1(x);
final double tol = ulps * FastMath.ulp(expected);
Assert.assertEquals(Double.toString(x), expected, actual, tol);
}
}
@Test(expected = NumberIsTooSmallException.class)
public void testInvGamma1pm1Precondition1() {
Gamma.invGamma1pm1(-0.51);
}
@Test(expected = NumberIsTooLargeException.class)
public void testInvGamma1pm1Precondition2() {
Gamma.invGamma1pm1(1.51);
}
private static final double[][] LOG_GAMMA1P_REF = {
{ - 0.5 , .5723649429247001 },
{ - 0.375 , .3608294954889402 },
{ - 0.25 , .2032809514312954 },
{ - 0.125 , .08585870722533433 },
{ 0.0 , 0.0 },
{ 0.125 , - .06002318412603958 },
{ 0.25 , - .09827183642181316 },
{ 0.375 , - .1177552707410788 },
{ 0.5 , - .1207822376352452 },
{ 0.625 , - .1091741337567954 },
{ 0.75 , - .08440112102048555 },
{ 0.875 , - 0.0476726853991883 },
{ 1.0 , 0.0 },
{ 1.125 , .05775985153034387 },
{ 1.25 , .1248717148923966 },
{ 1.375 , .2006984603774558 },
{ 1.5 , .2846828704729192 },
};
@Test
public void testLogGamma1p() {
final int ulps = 3;
for (int i = 0; i < LOG_GAMMA1P_REF.length; i++) {
final double[] ref = LOG_GAMMA1P_REF[i];
final double x = ref[0];
final double expected = ref[1];
final double actual = Gamma.logGamma1p(x);
final double tol = ulps * FastMath.ulp(expected);
Assert.assertEquals(Double.toString(x), expected, actual, tol);
}
}
@Test(expected = NumberIsTooSmallException.class)
public void testLogGamma1pPrecondition1() {
Gamma.logGamma1p(-0.51);
}
@Test(expected = NumberIsTooLargeException.class)
public void testLogGamma1pPrecondition2() {
Gamma.logGamma1p(1.51);
}
/**
* Reference data for the {@link Gamma#gamma(double)} function. This
* data was generated with the following <a
* href="http://maxima.sourceforge.net/">Maxima</a> script.
*
* <pre>
* kill(all);
*
* fpprec : 64;
*
* EPSILON : 10**(-fpprec + 1);
* isInteger(x) := abs(x - floor(x)) <= EPSILON * abs(x);
*
* x : makelist(bfloat(i / 8), i, -160, 160);
* x : append(x, makelist(bfloat(i / 2), i, 41, 200));
*
* for i : 1 while i <= length(x) do if not(isInteger(x[i])) then
* print("{", float(x[i]), ",", float(gamma(x[i])), "},");
* </pre>
*/
private static final double[][] GAMMA_REF = {
{ - 19.875 , 4.920331854832504e-18 },
{ - 19.75 , 3.879938752480031e-18 },
{ - 19.625 , 4.323498423815027e-18 },
{ - 19.5 , 5.811045977502237e-18 },
{ - 19.375 , 9.14330910942125e-18 },
{ - 19.25 , 1.735229114436739e-17 },
{ - 19.125 , 4.653521565668223e-17 },
{ - 18.875 , - 9.779159561479603e-17 },
{ - 18.75 , - 7.662879036148061e-17 },
{ - 18.625 , - 8.484865656736991e-17 },
{ - 18.5 , - 1.133153965612936e-16 },
{ - 18.375 , - 1.771516139950367e-16 },
{ - 18.25 , - 3.340316045290721e-16 },
{ - 18.125 , - 8.899859994340475e-16 },
{ - 17.875 , 1.845816367229275e-15 },
{ - 17.75 , 1.436789819277761e-15 },
{ - 17.625 , 1.580306228567265e-15 },
{ - 17.5 , 2.096334836383932e-15 },
{ - 17.375 , 3.255160907158799e-15 },
{ - 17.25 , 6.096076782655566e-15 },
{ - 17.125 , 1.613099623974211e-14 },
{ - 16.875 , - 3.29939675642233e-14 },
{ - 16.75 , - 2.550301929218027e-14 },
{ - 16.625 , - 2.785289727849803e-14 },
{ - 16.5 , - 3.66858596367188e-14 },
{ - 16.375 , - 5.655842076188414e-14 },
{ - 16.25 , - 1.051573245008085e-13 },
{ - 16.125 , - 2.762433106055837e-13 },
{ - 15.875 , 5.567732026462681e-13 },
{ - 15.75 , 4.271755731440195e-13 },
{ - 15.625 , 4.630544172550298e-13 },
{ - 15.5 , 6.053166840058604e-13 },
{ - 15.375 , 9.261441399758529e-13 },
{ - 15.25 , 1.708806523138138e-12 },
{ - 15.125 , 4.454423383515037e-12 },
{ - 14.875 , - 8.838774592009505e-12 },
{ - 14.75 , - 6.728015277018307e-12 },
{ - 14.625 , - 7.235225269609841e-12 },
{ - 14.5 , - 9.382408602090835e-12 },
{ - 14.375 , - 1.423946615212874e-11 },
{ - 14.25 , - 2.605929947785661e-11 },
{ - 14.125 , - 6.737315367566492e-11 },
{ - 13.875 , 1.314767720561414e-10 },
{ - 13.75 , 9.923822533602004e-11 },
{ - 13.625 , 1.058151695680439e-10 },
{ - 13.5 , 1.360449247303171e-10 },
{ - 13.375 , 2.046923259368506e-10 },
{ - 13.25 , 3.713450175594567e-10 },
{ - 13.125 , 9.516457956687671e-10 },
{ - 12.875 , - 1.8242402122789617e-9 },
{ - 12.75 , - 1.3645255983702756e-9 },
{ - 12.625 , - 1.4417316853645984e-9 },
{ - 12.5 , - 1.836606483859281e-9 },
{ - 12.375 , - 2.7377598594053765e-9 },
{ - 12.25 , - 4.9203214826628017e-9 },
{ - 12.125 , - 1.2490351068152569e-8 },
{ - 11.875 , 2.3487092733091633e-8 },
{ - 11.75 , 1.7397701379221012e-8 },
{ - 11.625 , 1.8201862527728055e-8 },
{ - 11.5 , 2.295758104824101e-8 },
{ - 11.375 , 3.3879778260141535e-8 },
{ - 11.25 , 6.027393816261931e-8 },
{ - 11.125 , 1.5144550670134987e-7 },
{ - 10.875 , - 2.7890922620546316e-7 },
{ - 10.75 , - 2.044229912058469e-7 },
{ - 10.625 , - 2.1159665188483867e-7 },
{ - 10.5 , - 2.640121820547716e-7 },
{ - 10.375 , - 3.8538247770911e-7 },
{ - 10.25 , - 6.780818043294673e-7 },
{ - 10.125 , - 1.6848312620525174e-6 },
{ - 9.875 , 3.0331378349844124e-6 },
{ - 9.75 , 2.1975471554628537e-6 },
{ - 9.625 , 2.2482144262764103e-6 },
{ - 9.5 , 2.772127911575102e-6 },
{ - 9.375 , 3.998343206232017e-6 },
{ - 9.25 , 6.95033849437704e-6 },
{ - 9.125 , 1.7058916528281737e-5 },
{ - 8.875 , - 2.9952236120471065e-5 },
{ - 8.75 , - 2.1426084765762826e-5 },
{ - 8.625 , - 2.163906385291045e-5 },
{ - 8.5 , - 2.633521515996347e-5 },
{ - 8.375 , - 3.748446755842515e-5 },
{ - 8.25 , - 6.429063107298763e-5 },
{ - 8.125 , - 1.5566261332057085e-4 },
{ - 7.875 , 2.658260955691807e-4 },
{ - 7.75 , 1.874782417004247e-4 },
{ - 7.625 , 1.8663692573135265e-4 },
{ - 7.5 , 2.238493288596895e-4 },
{ - 7.375 , 3.1393241580181064e-4 },
{ - 7.25 , 5.303977063521479e-4 },
{ - 7.125 , .001264758733229638 },
{ - 6.875 , - .002093380502607298 },
{ - 6.75 , - .001452956373178292 },
{ - 6.625 , - .001423106558701564 },
{ - 6.5 , - .001678869966447671 },
{ - 6.375 , - .002315251566538353 },
{ - 6.25 , - .003845383371053072 },
{ - 6.125 , - .009011405974261174 },
{ - 5.875 , .01439199095542518 },
{ - 5.75 , .009807455518953468 },
{ - 5.625 , .009428080951397862 },
{ - 5.5 , .01091265478190986 },
{ - 5.375 , 0.014759728736682 },
{ - 5.25 , 0.0240336460690817 },
{ - 5.125 , .05519486159234969 },
{ - 4.875 , - 0.0845529468631229 },
{ - 4.75 , - .05639286923398244 },
{ - 4.625 , - .05303295535161297 },
{ - 4.5 , - .06001960130050425 },
{ - 4.375 , - .07933354195966577 },
{ - 4.25 , - .1261766418626789 },
{ - 4.125 , - .2828736656607921 },
{ - 3.875 , .4121956159577241 },
{ - 3.75 , .2678661288614166 },
{ - 3.625 , 0.24527741850121 },
{ - 3.5 , .2700882058522691 },
{ - 3.375 , .3470842460735378 },
{ - 3.25 , .5362507279163854 },
{ - 3.125 , 1.166853870850768 },
{ - 2.875 , - 1.597258011836181 },
{ - 2.75 , - 1.004497983230312 },
{ - 2.625 , - .8891306420668862 },
{ - 2.5 , - .9453087204829419 },
{ - 2.375 , - 1.17140933049819 },
{ - 2.25 , - 1.742814865728253 },
{ - 2.125 , - 3.646418346408649 },
{ - 1.875 , 4.59211678402902 },
{ - 1.75 , 2.762369453883359 },
{ - 1.625 , 2.333967935425576 },
{ - 1.5 , 2.363271801207355 },
{ - 1.375 , 2.782097159933201 },
{ - 1.25 , 3.921333447888569 },
{ - 1.125 , 7.748638986118379 },
{ - 0.875 , - 8.610218970054413 },
{ - 0.75 , - 4.834146544295877 },
{ - 0.625 , - 3.792697895066561 },
{ - 0.5 , - 3.544907701811032 },
{ - 0.375 , - 3.825383594908152 },
{ - 0.25 , - 4.901666809860711 },
{ - 0.125 , - 8.717218859383175 },
{ 0.125 , 7.533941598797612 },
{ 0.25 , 3.625609908221908 },
{ 0.375 , 2.370436184416601 },
{ 0.5 , 1.772453850905516 },
{ 0.625 , 1.434518848090557 },
{ 0.75 , 1.225416702465178 },
{ 0.875 , 1.089652357422897 },
{ 1.0 , 1.0 },
{ 1.125 , .9417426998497015 },
{ 1.25 , 0.906402477055477 },
{ 1.375 , .8889135691562253 },
{ 1.5 , 0.886226925452758 },
{ 1.625 , 0.896574280056598 },
{ 1.75 , .9190625268488832 },
{ 1.875 , .9534458127450348 },
{ 2.0 , 1.0 },
{ 2.125 , 1.059460537330914 },
{ 2.25 , 1.133003096319346 },
{ 2.375 , 1.22225615758981 },
{ 2.5 , 1.329340388179137 },
{ 2.625 , 1.456933205091972 },
{ 2.75 , 1.608359421985546 },
{ 2.875 , 1.78771089889694 },
{ 3.0 , 2.0 },
{ 3.125 , 2.251353641828193 },
{ 3.25 , 2.549256966718529 },
{ 3.375 , 2.902858374275799 },
{ 3.5 , 3.323350970447843 },
{ 3.625 , 3.824449663366426 },
{ 3.75 , 4.422988410460251 },
{ 3.875 , 5.139668834328703 },
{ 4.0 , 6.0 },
{ 4.125 , 7.035480130713102 },
{ 4.25 , 8.28508514183522 },
{ 4.375 , 9.797147013180819 },
{ 4.5 , 11.63172839656745 },
{ 4.625 , 13.86363002970329 },
{ 4.75 , 16.58620653922594 },
{ 4.875 , 19.91621673302373 },
{ 5.0 , 24.0 },
{ 5.125 , 29.02135553919155 },
{ 5.25 , 35.21161185279968 },
{ 5.375 , 42.86251818266609 },
{ 5.5 , 52.34277778455352 },
{ 5.625 , 64.11928888737773 },
{ 5.75 , 78.78448106132322 },
{ 5.875 , 97.09155657349066 },
{ 6.0 , 120.0 },
{ 6.125 , 148.7344471383567 },
{ 6.25 , 184.8609622271983 },
{ 6.375 , 230.3860352318302 },
{ 6.5 , 287.8852778150443 },
{ 6.625 , 360.6709999914997 },
{ 6.75 , 453.0107661026085 },
{ 6.875 , 570.4128948692577 },
{ 7.0 , 720.0 },
{ 7.125 , 910.9984887224346 },
{ 7.25 , 1155.38101391999 },
{ 7.375 , 1468.710974602918 },
{ 7.5 , 1871.254305797788 },
{ 7.625 , 2389.445374943686 },
{ 7.75 , 3057.822671192607 },
{ 7.875 , 3921.588652226146 },
{ 8.0 , 5040.0 },
{ 8.125 , 6490.864232147346 },
{ 8.25 , 8376.512350919926 },
{ 8.375 , 10831.74343769652 },
{ 8.5 , 14034.40729348341 },
{ 8.625 , 18219.5209839456 },
{ 8.75 , 23698.12570174271 },
{ 8.875 , 30882.5106362809 },
{ 9.0 , 40320.0 },
{ 9.125 , 52738.27188619719 },
{ 9.25 , 69106.22689508938 },
{ 9.375 , 90715.85129070834 },
{ 9.5 , 119292.461994609 },
{ 9.625 , 157143.3684865308 },
{ 9.75 , 207358.5998902487 },
{ 9.875 , 274082.281896993 },
{ 10.0 , 362880.0 },
{ 10.125 , 481236.7309615494 },
{ 10.25 , 639232.5987795768 },
{ 10.375 , 850461.1058503906 },
{ 10.5 , 1133278.388948786 },
{ 10.625 , 1512504.921682859 },
{ 10.75 , 2021746.348929925 },
{ 10.875 , 2706562.533732806 },
{ 11.0 , 3628800.0 },
{ 11.125 , 4872521.900985687 },
{ 11.25 , 6552134.137490662 },
{ 11.375 , 8823533.973197803 },
{ 11.5 , 1.1899423083962249e+7 },
{ 11.625 , 1.6070364792880382e+7 },
{ 11.75 , 2.1733773250996688e+7 },
{ 11.875 , 2.943386755434427e+7 },
{ 12.0 , 3.99168e+7 },
{ 12.125 , 5.420680614846578e+7 },
{ 12.25 , 7.371150904676994e+7 },
{ 12.375 , 1.0036769894512501e+8 },
{ 12.5 , 1.3684336546556586e+8 },
{ 12.625 , 1.8681799071723443e+8 },
{ 12.75 , 2.5537183569921107e+8 },
{ 12.875 , 3.4952717720783816e+8 },
{ 13.0 , 4.790016e+8 },
{ 13.125 , 6.572575245501475e+8 },
{ 13.25 , 9.029659858229319e+8 },
{ 13.375 , 1.2420502744459219e+9 },
{ 13.5 , 1.7105420683195732e+9 },
{ 13.625 , 2.3585771328050845e+9 },
{ 13.75 , 3.2559909051649416e+9 },
{ 13.875 , 4.500162406550916e+9 },
{ 14.0 , 6.2270208e+9 },
{ 14.125 , 8.626505009720685e+9 },
{ 14.25 , 1.196429931215385e+10 },
{ 14.375 , 1.66124224207142e+10 },
{ 14.5 , 2.309231792231424e+10 },
{ 14.625 , 3.213561343446927e+10 },
{ 14.75 , 4.476987494601794e+10 },
{ 14.875 , 6.243975339089396e+10 },
{ 15.0 , 8.71782912e+10 },
{ 15.125 , 1.218493832623047e+11 },
{ 15.25 , 1.704912651981923e+11 },
{ 15.375 , 2.388035722977667e+11 },
{ 15.5 , 3.348386098735565e+11 },
{ 15.625 , 4.699833464791132e+11 },
{ 15.75 , 6.603556554537646e+11 },
{ 15.875 , 9.287913316895475e+11 },
{ 16.0 , 1.307674368e+12 },
{ 16.125 , 1.842971921842358e+12 },
{ 16.25 , 2.599991794272433e+12 },
{ 16.375 , 3.671604924078163e+12 },
{ 16.5 , 5.189998453040126e+12 },
{ 16.625 , 7.343489788736144e+12 },
{ 16.75 , 1.040060157339679e+13 },
{ 16.875 , 1.474456239057157e+13 },
{ 17.0 , 2.0922789888e+13 },
{ 17.125 , 2.971792223970803e+13 },
{ 17.25 , 4.224986665692704e+13 },
{ 17.375 , 6.012253063177992e+13 },
{ 17.5 , 8.563497447516206e+13 },
{ 17.625 , 1.220855177377384e+14 },
{ 17.75 , 1.742100763543963e+14 },
{ 17.875 , 2.488144903408952e+14 },
{ 18.0 , 3.55687428096e+14 },
{ 18.125 , 5.08919418355e+14 },
{ 18.25 , 7.288101998319914e+14 },
{ 18.375 , 1.044628969727176e+15 },
{ 18.5 , 1.498612053315336e+15 },
{ 18.625 , 2.151757250127639e+15 },
{ 18.75 , 3.092228855290534e+15 },
{ 18.875 , 4.447559014843502e+15 },
{ 19.0 , 6.402373705728e+15 },
{ 19.125 , 9.224164457684374e+15 },
{ 19.25 , 1.330078614693384e+16 },
{ 19.375 , 1.919505731873686e+16 },
{ 19.5 , 2.772432298633372e+16 },
{ 19.625 , 4.007647878362728e+16 },
{ 19.75 , 5.797929103669752e+16 },
{ 19.875 , 8.39476764051711e+16 },
{ 20.0 , 1.21645100408832e+17 },
{ 20.5 , 5.406242982335075e+17 },
{ 21.0 , 2.43290200817664e+18 },
{ 21.5 , 1.10827981137869e+19 },
{ 22.0 , 5.109094217170944e+19 },
{ 22.5 , 2.382801594464184e+20 },
{ 23.0 , 1.124000727777608e+21 },
{ 23.5 , 5.361303587544415e+21 },
{ 24.0 , 2.585201673888498e+22 },
{ 24.5 , 1.259906343072938e+23 },
{ 25.0 , 6.204484017332395e+23 },
{ 25.5 , 3.086770540528697e+24 },
{ 26.0 , 1.551121004333099e+25 },
{ 26.5 , 7.871264878348176e+25 },
{ 27.0 , 4.032914611266056e+26 },
{ 27.5 , 2.085885192762267e+27 },
{ 28.0 , 1.088886945041835e+28 },
{ 28.5 , 5.736184280096234e+28 },
{ 29.0 , 3.048883446117139e+29 },
{ 29.5 , 1.634812519827427e+30 },
{ 30.0 , 8.841761993739702e+30 },
{ 30.5 , 4.822696933490909e+31 },
{ 31.0 , 2.65252859812191e+32 },
{ 31.5 , 1.470922564714727e+33 },
{ 32.0 , 8.222838654177922e+33 },
{ 32.5 , 4.633406078851391e+34 },
{ 33.0 , 2.631308369336935e+35 },
{ 33.5 , 1.505856975626702e+36 },
{ 34.0 , 8.683317618811885e+36 },
{ 34.5 , 5.044620868349451e+37 },
{ 35.0 , 2.952327990396041e+38 },
{ 35.5 , 1.740394199580561e+39 },
{ 36.0 , 1.033314796638614e+40 },
{ 36.5 , 6.178399408510991e+40 },
{ 37.0 , 3.719933267899013e+41 },
{ 37.5 , 2.255115784106512e+42 },
{ 38.0 , 1.376375309122634e+43 },
{ 38.5 , 8.456684190399419e+43 },
{ 39.0 , 5.230226174666011e+44 },
{ 39.5 , 3.255823413303776e+45 },
{ 40.0 , 2.039788208119745e+46 },
{ 40.5 , 1.286050248254992e+47 },
{ 41.0 , 8.159152832478976e+47 },
{ 41.5 , 5.208503505432716e+48 },
{ 42.0 , 3.345252661316381e+49 },
{ 42.5 , 2.161528954754577e+50 },
{ 43.0 , 1.40500611775288e+51 },
{ 43.5 , 9.186498057706953e+51 },
{ 44.0 , 6.041526306337383e+52 },
{ 44.5 , 3.996126655102524e+53 },
{ 45.0 , 2.658271574788449e+54 },
{ 45.5 , 1.778276361520623e+55 },
{ 46.0 , 1.196222208654802e+56 },
{ 46.5 , 8.091157444918836e+56 },
{ 47.0 , 5.502622159812088e+57 },
{ 47.5 , 3.762388211887259e+58 },
{ 48.0 , 2.586232415111682e+59 },
{ 48.5 , 1.787134400646448e+60 },
{ 49.0 , 1.241391559253607e+61 },
{ 49.5 , 8.667601843135273e+61 },
{ 50.0 , 6.082818640342675e+62 },
{ 50.5 , 4.290462912351959e+63 },
{ 51.0 , 3.041409320171338e+64 },
{ 51.5 , 2.16668377073774e+65 },
{ 52.0 , 1.551118753287382e+66 },
{ 52.5 , 1.115842141929936e+67 },
{ 53.0 , 8.065817517094388e+67 },
{ 53.5 , 5.858171245132164e+68 },
{ 54.0 , 4.274883284060025e+69 },
{ 54.5 , 3.134121616145708e+70 },
{ 55.0 , 2.308436973392413e+71 },
{ 55.5 , 1.70809628079941e+72 },
{ 56.0 , 1.269640335365828e+73 },
{ 56.5 , 9.479934358436728e+73 },
{ 57.0 , 7.109985878048635e+74 },
{ 57.5 , 5.356162912516752e+75 },
{ 58.0 , 4.052691950487721e+76 },
{ 58.5 , 3.079793674697132e+77 },
{ 59.0 , 2.350561331282878e+78 },
{ 59.5 , 1.801679299697822e+79 },
{ 60.0 , 1.386831185456898e+80 },
{ 60.5 , 1.071999183320204e+81 },
{ 61.0 , 8.320987112741391e+81 },
{ 61.5 , 6.485595059087236e+82 },
{ 62.0 , 5.075802138772249e+83 },
{ 62.5 , 3.98864096133865e+84 },
{ 63.0 , 3.146997326038794e+85 },
{ 63.5 , 2.492900600836656e+86 },
{ 64.0 , 1.98260831540444e+87 },
{ 64.5 , 1.582991881531277e+88 },
{ 65.0 , 1.268869321858841e+89 },
{ 65.5 , 1.021029763587673e+90 },
{ 66.0 , 8.247650592082472e+90 },
{ 66.5 , 6.687744951499262e+91 },
{ 67.0 , 5.443449390774431e+92 },
{ 67.5 , 4.447350392747009e+93 },
{ 68.0 , 3.647111091818868e+94 },
{ 68.5 , 3.001961515104231e+95 },
{ 69.0 , 2.48003554243683e+96 },
{ 69.5 , 2.056343637846398e+97 },
{ 70.0 , 1.711224524281413e+98 },
{ 70.5 , 1.429158828303247e+99 },
{ 71.0 , 1.19785716699699e+100 },
{ 71.5 , 1.00755697395379e+101 },
{ 72.0 , 8.50478588567862e+101 },
{ 72.5 , 7.20403236376959e+102 },
{ 73.0 , 6.12344583768861e+103 },
{ 73.5 , 5.22292346373295e+104 },
{ 74.0 , 4.47011546151268e+105 },
{ 74.5 , 3.83884874584372e+106 },
{ 75.0 , 3.30788544151939e+107 },
{ 75.5 , 2.85994231565357e+108 },
{ 76.0 , 2.48091408113954e+109 },
{ 76.5 , 2.15925644831845e+110 },
{ 77.0 , 1.88549470166605e+111 },
{ 77.5 , 1.65183118296361e+112 },
{ 78.0 , 1.45183092028286e+113 },
{ 78.5 , 1.2801691667968e+114 },
{ 79.0 , 1.13242811782063e+115 },
{ 79.5 , 1.00493279593549e+116 },
{ 80.0 , 8.94618213078298e+116 },
{ 80.5 , 7.98921572768712e+117 },
{ 81.0 , 7.15694570462638e+118 },
{ 81.5 , 6.43131866078814e+119 },
{ 82.0 , 5.79712602074737e+120 },
{ 82.5 , 5.24152470854233e+121 },
{ 83.0 , 4.75364333701284e+122 },
{ 83.5 , 4.32425788454742e+123 },
{ 84.0 , 3.94552396972066e+124 },
{ 84.5 , 3.6107553335971e+125 },
{ 85.0 , 3.31424013456535e+126 },
{ 85.5 , 3.05108825688955e+127 },
{ 86.0 , 2.81710411438055e+128 },
{ 86.5 , 2.60868045964056e+129 },
{ 87.0 , 2.42270953836727e+130 },
{ 87.5 , 2.25650859758909e+131 },
{ 88.0 , 2.10775729837953e+132 },
{ 88.5 , 1.97444502289045e+133 },
{ 89.0 , 1.85482642257398e+134 },
{ 89.5 , 1.74738384525805e+135 },
{ 90.0 , 1.65079551609085e+136 },
{ 90.5 , 1.56390854150595e+137 },
{ 91.0 , 1.48571596448176e+138 },
{ 91.5 , 1.41533723006289e+139 },
{ 92.0 , 1.3520015276784e+140 },
{ 92.5 , 1.29503356550754e+141 },
{ 93.0 , 1.24384140546413e+142 },
{ 93.5 , 1.19790604809448e+143 },
{ 94.0 , 1.15677250708164e+144 },
{ 94.5 , 1.12004215496834e+145 },
{ 95.0 , 1.08736615665674e+146 },
{ 95.5 , 1.05843983644508e+147 },
{ 96.0 , 1.03299784882391e+148 },
{ 96.5 , 1.01081004380505e+149 },
{ 97.0 , 9.9167793487095e+149 },
{ 97.5 , 9.75431692271873e+150 },
{ 98.0 , 9.61927596824821e+151 },
{ 98.5 , 9.51045899965076e+152 },
{ 99.0 , 9.42689044888325e+153 },
{ 99.5 , 9.367802114656e+154 },
{ 100.0 , 9.33262154439441e+155 },
};
@Test
public void testGamma() {
for (int i = 0; i < GAMMA_REF.length; i++) {
final double[] ref = GAMMA_REF[i];
final double x = ref[0];
final double expected = ref[1];
final double actual = Gamma.gamma(x);
final double absX = FastMath.abs(x);
final int ulps;
if (absX <= 8.0) {
ulps = 3;
} else if (absX <= 20.0) {
ulps = 5;
} else if (absX <= 30.0) {
ulps = 50;
} else if (absX <= 50.0) {
ulps = 180;
} else {
ulps = 500;
}
final double tol = ulps * FastMath.ulp(expected);
Assert.assertEquals(Double.toString(x), expected, actual, tol);
}
}
@Test
public void testGammaNegativeInteger() {
for (int i = -100; i <= 0; i++) {
Assert.assertTrue(Integer.toString(i), Double.isNaN(Gamma.gamma(i)));
}
}
@Test
public void testGammaNegativeDouble() {
// check that the gamma function properly switches sign
// see: https://en.wikipedia.org/wiki/Gamma_function
double previousGamma = Gamma.gamma(-18.5);
for (double x = -19.5; x > -25; x -= 1.0) {
double gamma = Gamma.gamma(x);
Assert.assertEquals( (int) FastMath.signum(previousGamma),
- (int) FastMath.signum(gamma));
previousGamma = gamma;
}
}
private void checkRelativeError(String msg, double expected, double actual,
double tolerance) {
Assert.assertEquals(msg, expected, actual, FastMath.abs(tolerance * actual));
}
}