This is an automated email from the ASF dual-hosted git repository. aherbert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-numbers.git
commit 1394949d2043d5b1ecbbc3d57f84b735e96a22ba Author: Alex Herbert <aherb...@apache.org> AuthorDate: Wed Jul 28 21:54:27 2021 +0100 Remove unnecessary nesting --- .../commons/numbers/core/ArithmeticUtils.java | 3 +- .../commons/numbers/gamma/ErfDifference.java | 24 ++++---- .../org/apache/commons/numbers/gamma/Gamma.java | 61 +++++++++---------- .../apache/commons/numbers/gamma/InvGamma1pm1.java | 65 ++++++++++---------- .../org/apache/commons/numbers/gamma/LogBeta.java | 70 ++++++++++------------ .../commons/numbers/gamma/RegularizedBeta.java | 7 +-- 6 files changed, 106 insertions(+), 124 deletions(-) diff --git a/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/ArithmeticUtils.java b/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/ArithmeticUtils.java index bbd8292..48450ac 100644 --- a/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/ArithmeticUtils.java +++ b/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/ArithmeticUtils.java @@ -106,9 +106,8 @@ public final class ArithmeticUtils { if (negatedGcd == Integer.MIN_VALUE) { throw new NumbersArithmeticException("overflow: gcd({0}, {1}) is 2^31", p, q); - } else { - return -negatedGcd; } + return -negatedGcd; } /** diff --git a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/ErfDifference.java b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/ErfDifference.java index 157beed..f57f127 100644 --- a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/ErfDifference.java +++ b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/ErfDifference.java @@ -51,21 +51,17 @@ public final class ErfDifference { double x2) { if (x1 > x2) { return -value(x2, x1); - } else { - if (x1 < -X_CRIT) { - if (x2 < 0) { - return Erfc.value(-x2) - Erfc.value(-x1); - } else { - return Erf.value(x2) - Erf.value(x1); - } - } else { - if (x2 > X_CRIT && - x1 > 0) { - return Erfc.value(x1) - Erfc.value(x2); - } else { - return Erf.value(x2) - Erf.value(x1); - } + } + if (x1 < -X_CRIT) { + if (x2 < 0) { + return Erfc.value(-x2) - Erfc.value(-x1); } + return Erf.value(x2) - Erf.value(x1); + } + if (x2 > X_CRIT && + x1 > 0) { + return Erfc.value(x1) - Erfc.value(x2); } + return Erf.value(x2) - Erf.value(x1); } } diff --git a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/Gamma.java b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/Gamma.java index c43e2a7..052d469 100644 --- a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/Gamma.java +++ b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/Gamma.java @@ -75,40 +75,37 @@ public final class Gamma { prod *= t; } return prod / (1 + InvGamma1pm1.value(t - 1)); - } else { - /* - * From the recurrence relation - * Gamma(x) = Gamma(x + n + 1) / [x * (x + 1) * ... * (x + n)] - * then - * Gamma(x + n + 1) = 1 / [1 + InvGamma1pm1.value(x + n)], - * which requires -0.5 <= x + n <= 1.5. - */ - double prod = x; - double t = x; - while (t < -0.5) { - t += 1; - prod *= t; - } - return 1 / (prod * (1 + InvGamma1pm1.value(t))); } - } else { - final double y = absX + LanczosApproximation.g() + 0.5; - final double gammaAbs = SQRT_TWO_PI / absX * - Math.pow(y, absX + 0.5) * - Math.exp(-y) * LanczosApproximation.value(absX); - if (x > 0) { - return 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)]. - */ - return -Math.PI / (x * Math.sin(Math.PI * x) * gammaAbs); + /* + * From the recurrence relation + * Gamma(x) = Gamma(x + n + 1) / [x * (x + 1) * ... * (x + n)] + * then + * Gamma(x + n + 1) = 1 / [1 + InvGamma1pm1.value(x + n)], + * which requires -0.5 <= x + n <= 1.5. + */ + double prod = x; + double t = x; + while (t < -0.5) { + t += 1; + prod *= t; } + return 1 / (prod * (1 + InvGamma1pm1.value(t))); + } + final double y = absX + LanczosApproximation.g() + 0.5; + final double gammaAbs = SQRT_TWO_PI / absX * + Math.pow(y, absX + 0.5) * + Math.exp(-y) * LanczosApproximation.value(absX); + if (x > 0) { + return gammaAbs; } + /* + * 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)]. + */ + return -Math.PI / (x * Math.sin(Math.PI * x) * gammaAbs); } } diff --git a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/InvGamma1pm1.java b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/InvGamma1pm1.java index 710ccdd..a9cba0f 100644 --- a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/InvGamma1pm1.java +++ b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/InvGamma1pm1.java @@ -147,44 +147,41 @@ final class InvGamma1pm1 { c = INV_GAMMA1P_M1_C + t * c; if (x > 0.5) { return t * c / x; - } else { - return 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; + return x * ((c + 0.5) + 0.5); + } + 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 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; + 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) { - return (t / x) * ((c - 0.5) - 0.5); - } else { - return x * c; - } + if (x > 0.5) { + return (t / x) * ((c - 0.5) - 0.5); } + return x * c; } } diff --git a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/LogBeta.java b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/LogBeta.java index 84b8da3..e937ff3 100644 --- a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/LogBeta.java +++ b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/LogBeta.java @@ -176,9 +176,8 @@ public final class LogBeta { final double v = b * Math.log1p(h); if (u <= v) { return (((-0.5 * Math.log(b) + HALF_LOG_TWO_PI) + w) - u) - v; - } else { - return (((-0.5 * Math.log(b) + HALF_LOG_TWO_PI) + w) - v) - u; } + return (((-0.5 * Math.log(b) + HALF_LOG_TWO_PI) + w) - v) - u; } else if (a > TWO) { if (b > THOUSAND) { final int n = (int) Math.floor(a - 1); @@ -191,32 +190,30 @@ public final class LogBeta { return (Math.log(prod) - n * Math.log(b)) + (LogGamma.value(ared) + logGammaMinusLogGammaSum(ared, b)); - } else { - double prod1 = 1; - double ared = a; - while (ared > 2) { - ared -= 1; - final double h = ared / b; - prod1 *= h / (1 + h); - } - if (b < TEN) { - double prod2 = 1; - double bred = b; - while (bred > 2) { - bred -= 1; - prod2 *= bred / (ared + bred); - } - return Math.log(prod1) + - Math.log(prod2) + - (LogGamma.value(ared) + - (LogGamma.value(bred) - - LogGammaSum.value(ared, bred))); - } else { - return Math.log(prod1) + - LogGamma.value(ared) + - logGammaMinusLogGammaSum(ared, b); + } + double prod1 = 1; + double ared = a; + while (ared > 2) { + ared -= 1; + final double h = ared / b; + prod1 *= h / (1 + h); + } + if (b < TEN) { + double prod2 = 1; + double bred = b; + while (bred > 2) { + bred -= 1; + prod2 *= bred / (ared + bred); } + return Math.log(prod1) + + Math.log(prod2) + + (LogGamma.value(ared) + + (LogGamma.value(bred) - + LogGammaSum.value(ared, bred))); } + return Math.log(prod1) + + LogGamma.value(ared) + + logGammaMinusLogGammaSum(ared, b); } else if (a >= 1) { if (b > TWO) { if (b < TEN) { @@ -230,26 +227,23 @@ public final class LogBeta { (LogGamma.value(a) + (LogGamma.value(bred) - LogGammaSum.value(a, bred))); - } else { - return LogGamma.value(a) + - logGammaMinusLogGammaSum(a, b); } - } else { return LogGamma.value(a) + - LogGamma.value(b) - - LogGammaSum.value(a, b); + logGammaMinusLogGammaSum(a, b); } + return LogGamma.value(a) + + LogGamma.value(b) - + LogGammaSum.value(a, b); } else { if (b >= TEN) { return LogGamma.value(a) + logGammaMinusLogGammaSum(a, b); - } else { - // The original NSWC implementation was - // LogGamma.value(a) + (LogGamma.value(b) - LogGamma.value(a + b)); - // but the following command turned out to be more accurate. - return Math.log(Gamma.value(a) * Gamma.value(b) / - Gamma.value(a + b)); } + // The original NSWC implementation was + // LogGamma.value(a) + (LogGamma.value(b) - LogGamma.value(a + b)); + // but the following command turned out to be more accurate. + return Math.log(Gamma.value(a) * Gamma.value(b) / + Gamma.value(a + b)); } } diff --git a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/RegularizedBeta.java b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/RegularizedBeta.java index 9d0b7ea..1ccd098 100644 --- a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/RegularizedBeta.java +++ b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/RegularizedBeta.java @@ -104,11 +104,10 @@ public final class RegularizedBeta { final double m = n / 2d; return (m * (b - m) * x) / ((a + (2 * m) - 1) * (a + (2 * m))); - } else { - final double m = (n - 1d) / 2d; - return -((a + m) * (a + b + m) * x) / - ((a + (2 * m)) * (a + (2 * m) + 1)); } + final double m = (n - 1d) / 2d; + return -((a + m) * (a + b + m) * x) / + ((a + (2 * m)) * (a + (2 * m) + 1)); } /** {@inheritDoc} */