You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2020/04/07 12:43:38 UTC

[commons-numbers] 06/09: ContinuedFraction: reverse names of coefficients a and b

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 d7b2895dc48b6804ef010188906209346edb6a1b
Author: aherbert <ah...@apache.org>
AuthorDate: Tue Apr 7 12:43:50 2020 +0100

    ContinuedFraction: reverse names of coefficients a and b
    
    a = numerator
    b = denominator
    
    This matches the original reference paper for the implemented method
    (Thompson & Barnett, 1986).
---
 .../numbers/fraction/ContinuedFraction.java        | 24 ++++++++++++----------
 .../numbers/fraction/ContinuedFractionTest.java    | 21 +++++++++----------
 .../commons/numbers/gamma/RegularizedBeta.java     |  4 ++--
 .../commons/numbers/gamma/RegularizedGamma.java    |  4 ++--
 4 files changed, 27 insertions(+), 26 deletions(-)

diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/ContinuedFraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/ContinuedFraction.java
index 4801318..a01fee8 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/ContinuedFraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/ContinuedFraction.java
@@ -21,19 +21,21 @@ import org.apache.commons.numbers.core.Precision;
 /**
  * Provides a generic means to evaluate
  * <a href="https://mathworld.wolfram.com/ContinuedFraction.html">continued fractions</a>.
- * Subclasses must provide the {@link #getA(int,double) a} and {@link #getB(int,double) b}
- * coefficients to evaluate the continued fraction.
  *
- * <p>The fraction uses the following form for the {@code a} and {@code b} coefficients:
+ * <p>The continued fraction uses the following form for the numerator ({@code a}) and
+ * denominator ({@code b}) coefficients:
  * <pre>
- *              b1
- * a0 + ------------------
- *      a1 +      b2
+ *              a1
+ * b0 + ------------------
+ *      b1 +      a2
  *           -------------
- *           a2 +    b3
+ *           b2 +    a3
  *                --------
- *                a3 + ...
+ *                b3 + ...
  * </pre>
+ *
+ * <p>Subclasses must provide the {@link #getA(int,double) a} and {@link #getB(int,double) b}
+ * coefficients to evaluate the continued fraction.
  */
 public abstract class ContinuedFraction {
     /**
@@ -106,7 +108,7 @@ public abstract class ContinuedFraction {
      * before the expected convergence is achieved.
      */
     public double evaluate(double x, double epsilon, int maxIterations) {
-        double hPrev = updateIfCloseToZero(getA(0, x));
+        double hPrev = updateIfCloseToZero(getB(0, x));
 
         int n = 1;
         double dPrev = 0.0;
@@ -117,8 +119,8 @@ public abstract class ContinuedFraction {
             final double a = getA(n, x);
             final double b = getB(n, x);
 
-            double dN = updateIfCloseToZero(a + b * dPrev);
-            final double cN = updateIfCloseToZero(a + b / cPrev);
+            double dN = updateIfCloseToZero(b + a * dPrev);
+            final double cN = updateIfCloseToZero(b + a / cPrev);
 
             dN = 1 / dN;
             final double deltaN = cN * dN;
diff --git a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/ContinuedFractionTest.java b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/ContinuedFractionTest.java
index ebb03ae..6760894 100644
--- a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/ContinuedFractionTest.java
+++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/ContinuedFractionTest.java
@@ -20,7 +20,6 @@ import java.util.Locale;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
-
 /**
  * Tests for {@link ContinuedFraction}.
  */
@@ -60,6 +59,11 @@ public class ContinuedFractionTest {
         ContinuedFraction cf = new ContinuedFraction() {
             @Override
             public double getA(int n, double x) {
+                return n <= 3 ? 1 : 0;
+            }
+
+            @Override
+            public double getB(int n, double x) {
                 switch (n) {
                     case 0: return 4;
                     case 1: return 2;
@@ -68,11 +72,6 @@ public class ContinuedFractionTest {
                     default: return 1;
                 }
             }
-
-            @Override
-            public double getB(int n, double x) {
-                return n <= 3 ? 1 : 0;
-            }
         };
 
         final double eps = 1e-8;
@@ -107,12 +106,12 @@ public class ContinuedFractionTest {
         ContinuedFraction cf = new ContinuedFraction() {
             @Override
             public double getA(int n, double x) {
-                return n == 0 ? 1 : Double.NaN;
+                return 1;
             }
 
             @Override
             public double getB(int n, double x) {
-                return 1;
+                return n == 0 ? 1 : Double.NaN;
             }
         };
 
@@ -125,16 +124,16 @@ public class ContinuedFractionTest {
     @Test
     public void testInfThrows() throws Exception {
         // Create an infinity during the iteration:
-        // b / cPrev  => b_1 / a_0 => Double.MAX_VALUE / 0.5
+        // a / cPrev  => a_1 / b_0 => Double.MAX_VALUE / 0.5
         ContinuedFraction cf = new ContinuedFraction() {
             @Override
             public double getA(int n, double x) {
-                return 0.5;
+                return n == 0 ? 1 : Double.MAX_VALUE;
             }
 
             @Override
             public double getB(int n, double x) {
-                return n == 0 ? 1 : Double.MAX_VALUE;
+                return 0.5;
             }
         };
 
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 2a4d4fa..d4c1c59 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
@@ -99,7 +99,7 @@ public final class RegularizedBeta {
             final ContinuedFraction fraction = new ContinuedFraction() {
                 /** {@inheritDoc} */
                 @Override
-                protected double getB(int n, double x) {
+                protected double getA(int n, double x) {
                     if (n % 2 == 0) { // even
                         final double m = n / 2d;
                         return (m * (b - m) * x) /
@@ -113,7 +113,7 @@ public final class RegularizedBeta {
 
                 /** {@inheritDoc} */
                 @Override
-                protected double getA(int n, double x) {
+                protected double getB(int n, double x) {
                     return 1;
                 }
             };
diff --git a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/RegularizedGamma.java b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/RegularizedGamma.java
index 448c8ea..f607fb9 100644
--- a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/RegularizedGamma.java
+++ b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/RegularizedGamma.java
@@ -192,13 +192,13 @@ public final class RegularizedGamma {
                         /** {@inheritDoc} */
                         @Override
                         protected double getA(int n, double x) {
-                            return ((2 * n) + 1) - a + x;
+                            return n * (a - n);
                         }
 
                         /** {@inheritDoc} */
                         @Override
                         protected double getB(int n, double x) {
-                            return n * (a - n);
+                            return ((2 * n) + 1) - a + x;
                         }
                     };