You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2017/07/03 14:24:35 UTC

[1/5] commons-numbers git commit: Javadoc (unit test).

Repository: commons-numbers
Updated Branches:
  refs/heads/master 6b523a37f -> 5a2f9d149


Javadoc (unit test).


Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/e2f06629
Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/e2f06629
Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/e2f06629

Branch: refs/heads/master
Commit: e2f06629114971c3a3c52ede491446a394c6f7a9
Parents: 6b523a3
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
Authored: Mon Jul 3 14:06:08 2017 +0200
Committer: Gilles Sadowski <gi...@harfang.homelinux.org>
Committed: Mon Jul 3 14:06:08 2017 +0200

----------------------------------------------------------------------
 .../org/apache/commons/numbers/fraction/ContinuedFractionTest.java  | 1 +
 1 file changed, 1 insertion(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/e2f06629/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/ContinuedFractionTest.java
----------------------------------------------------------------------
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 f548fb1..06e2bb1 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
@@ -21,6 +21,7 @@ import org.junit.Test;
 
 
 /**
+ * Tests for {@link ContinuedFraction}.
  */
 public class ContinuedFractionTest {
 


[3/5] commons-numbers git commit: NUMBER-46: Meaning of "maxIterations" argument.

Posted by er...@apache.org.
NUMBER-46: Meaning of "maxIterations" argument.


Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/bc83d14f
Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/bc83d14f
Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/bc83d14f

Branch: refs/heads/master
Commit: bc83d14fe2b7b4e86f6b01d37ce6b92bbf468916
Parents: 47e19ab
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
Authored: Mon Jul 3 16:08:19 2017 +0200
Committer: Gilles Sadowski <gi...@harfang.homelinux.org>
Committed: Mon Jul 3 16:08:19 2017 +0200

----------------------------------------------------------------------
 .../numbers/fraction/ContinuedFraction.java     | 28 ++++++-------
 .../numbers/fraction/ContinuedFractionTest.java | 44 +++++++++++++++++++-
 2 files changed, 54 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/bc83d14f/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/ContinuedFraction.java
----------------------------------------------------------------------
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 47bd9b9..5c5648d 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
@@ -80,7 +80,7 @@ public abstract class ContinuedFraction {
     }
 
     /**
-     * Evaluates the continued fraction at the value x.
+     * Evaluates the continued fraction.
      * <p>
      * The implementation of this method is based on the modified Lentz algorithm as described
      * on page 18 ff. in:
@@ -99,12 +99,13 @@ public abstract class ContinuedFraction {
      * <a href="http://mathworld.wolfram.com/ContinuedFraction.html">Continued Fraction @ MathWorld</a>.
      * </p>
      *
-     * @param x the evaluation point.
-     * @param epsilon maximum error allowed.
-     * @param maxIterations maximum number of convergents
-     * @return the value of the continued fraction evaluated at x.
+     * @param x Point at which to evaluate the continued fraction.
+     * @param epsilon Maximum error allowed.
+     * @param maxIterations Maximum number of iterations.
+     * @return the value of the continued fraction evaluated at {@code x}.
      * @throws ArithmeticException if the algorithm fails to converge.
-     * @throws ArithmeticException if maximal number of iterations is reached
+     * @throws ArithmeticException if the maximal number of iterations is reached
+     * before the expected convergence is achieved.
      */
     public double evaluate(double x, double epsilon, int maxIterations) {
         final double small = 1e-50;
@@ -120,7 +121,7 @@ public abstract class ContinuedFraction {
         double cPrev = hPrev;
         double hN = hPrev;
 
-        while (n < maxIterations) {
+        while (n <= maxIterations) {
             final double a = getA(n, x);
             final double b = getB(n, x);
 
@@ -146,21 +147,16 @@ public abstract class ContinuedFraction {
                                                x);
             }
 
-            if (Math.abs(deltaN - 1.0) < epsilon) {
-                break;
+            if (Math.abs(deltaN - 1) < epsilon) {
+                return hN;
             }
 
             dPrev = dN;
             cPrev = cN;
             hPrev = hN;
-            n++;
-        }
-
-        if (n >= maxIterations) {
-            throw new FractionException("maximal count ({0}) exceeded", maxIterations);
+            ++n;
         }
 
-        return hN;
+        throw new FractionException("maximal count ({0}) exceeded", maxIterations);
     }
-
 }

http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/bc83d14f/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/ContinuedFractionTest.java
----------------------------------------------------------------------
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 8f450aa..8543f0a 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
@@ -28,8 +28,47 @@ public class ContinuedFractionTest {
     @Test
     public void testGoldenRatio() throws Exception {
         ContinuedFraction cf = new ContinuedFraction() {
+            @Override
+            public double getA(int n, double x) {
+                return 1;
+            }
 
             @Override
+            public double getB(int n, double x) {
+                return 1;
+            }
+        };
+
+        final double eps = 1e-8;
+        double gr = cf.evaluate(0, eps);
+        Assert.assertEquals(1.61803399, gr, eps);
+    }
+
+    // NUMBERS-46
+    @Test
+    public void testOneIteration() {
+        ContinuedFraction cf = new ContinuedFraction() {
+            @Override
+            public double getA(int n, double x) {
+                return 1;
+            }
+
+            @Override
+            public double getB(int n, double x) {
+                return 1;
+            }
+        };
+
+        final double eps = 10;
+        double gr = cf.evaluate(0, eps, 1);
+        Assert.assertEquals(1.61, gr, eps);
+    }
+
+    // NUMBERS-46
+    @Test
+    public void testTwoIterations() {
+        ContinuedFraction cf = new ContinuedFraction() {
+            @Override
             public double getA(int n, double x) {
                 return 1;
             }
@@ -40,7 +79,8 @@ public class ContinuedFractionTest {
             }
         };
 
-        double gr = cf.evaluate(0.0, 1e-8);
-        Assert.assertEquals(1.61803399, gr, 1e-8);
+        final double eps = 0.5;
+        double gr = cf.evaluate(0, eps, 2);
+        Assert.assertEquals(1.5, gr, 0d);
     }
 }


[5/5] commons-numbers git commit: Fix probable typo.

Posted by er...@apache.org.
Fix probable typo.


Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/5a2f9d14
Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/5a2f9d14
Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/5a2f9d14

Branch: refs/heads/master
Commit: 5a2f9d1492f0a9679c64a91d4e536b936fd815e4
Parents: 9c04d44
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
Authored: Mon Jul 3 16:23:46 2017 +0200
Committer: Gilles Sadowski <gi...@harfang.homelinux.org>
Committed: Mon Jul 3 16:23:46 2017 +0200

----------------------------------------------------------------------
 .../org/apache/commons/numbers/fraction/ContinuedFraction.java     | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/5a2f9d14/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/ContinuedFraction.java
----------------------------------------------------------------------
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 155c700..121b573 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
@@ -26,7 +26,7 @@ import org.apache.commons.numbers.core.Precision;
  */
 public abstract class ContinuedFraction {
     /** Maximum allowed numerical error. */
-    private static final double DEFAULT_EPSILON = 10e-9;
+    private static final double DEFAULT_EPSILON = 1e-9;
 
     /**
      * Defines the <a href="http://mathworld.wolfram.com/ContinuedFraction.html">


[2/5] commons-numbers git commit: Use standard notation.

Posted by er...@apache.org.
Use standard notation.


Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/47e19ab3
Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/47e19ab3
Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/47e19ab3

Branch: refs/heads/master
Commit: 47e19ab375e512518fa25bb446e16fd5c64ac0a2
Parents: e2f0662
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
Authored: Mon Jul 3 14:08:18 2017 +0200
Committer: Gilles Sadowski <gi...@harfang.homelinux.org>
Committed: Mon Jul 3 14:08:18 2017 +0200

----------------------------------------------------------------------
 .../commons/numbers/fraction/ContinuedFractionTest.java      | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/47e19ab3/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/ContinuedFractionTest.java
----------------------------------------------------------------------
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 06e2bb1..8f450aa 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
@@ -31,16 +31,16 @@ public class ContinuedFractionTest {
 
             @Override
             public double getA(int n, double x) {
-                return 1.0;
+                return 1;
             }
 
             @Override
             public double getB(int n, double x) {
-                return 1.0;
+                return 1;
             }
         };
 
-        double gr = cf.evaluate(0.0, 10e-9);
-        Assert.assertEquals(1.61803399, gr, 10e-9);
+        double gr = cf.evaluate(0.0, 1e-8);
+        Assert.assertEquals(1.61803399, gr, 1e-8);
     }
 }


[4/5] commons-numbers git commit: Javadoc.

Posted by er...@apache.org.
Javadoc.


Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/9c04d444
Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/9c04d444
Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/9c04d444

Branch: refs/heads/master
Commit: 9c04d444c3338196412f5583a7da1035bd741472
Parents: bc83d14
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
Authored: Mon Jul 3 16:21:57 2017 +0200
Committer: Gilles Sadowski <gi...@harfang.homelinux.org>
Committed: Mon Jul 3 16:21:57 2017 +0200

----------------------------------------------------------------------
 .../numbers/fraction/ContinuedFraction.java     | 58 ++++++++++++--------
 1 file changed, 34 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/9c04d444/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/ContinuedFraction.java
----------------------------------------------------------------------
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 5c5648d..155c700 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
@@ -29,39 +29,51 @@ public abstract class ContinuedFraction {
     private static final double DEFAULT_EPSILON = 10e-9;
 
     /**
-     * Access the n-th a coefficient of the continued fraction.  Since a can be
-     * a function of the evaluation point, x, that is passed in as well.
-     * @param n the coefficient index to retrieve.
-     * @param x the evaluation point.
-     * @return the n-th a coefficient.
+     * Defines the <a href="http://mathworld.wolfram.com/ContinuedFraction.html">
+     * {@code n}-th "a" coefficient</a> of the continued fraction.
+     *
+     * @param n Index of the coefficient to retrieve.
+     * @param x Evaluation point.
+     * @return the coefficient <code>a<sub>n</sub></code>.
      */
     protected abstract double getA(int n, double x);
 
     /**
-     * Access the n-th b coefficient of the continued fraction.  Since b can be
-     * a function of the evaluation point, x, that is passed in as well.
-     * @param n the coefficient index to retrieve.
-     * @param x the evaluation point.
-     * @return the n-th b coefficient.
+     * Defines the <a href="http://mathworld.wolfram.com/ContinuedFraction.html">
+     * {@code n}-th "b" coefficient</a> of the continued fraction.
+     *
+     * @param n Index of the coefficient to retrieve.
+     * @param x Evaluation point.
+     * @return the coefficient <code>b<sub>n</sub></code>.
      */
     protected abstract double getB(int n, double x);
 
     /**
-     * Evaluates the continued fraction at the value x.
-     * @param x the evaluation point.
-     * @return the value of the continued fraction evaluated at x.
+     * Evaluates the continued fraction.
+     *
+     * @param x Point at which to evaluate the continued fraction.
+     * @return the value of the continued fraction evaluated at {@code x}.
      * @throws ArithmeticException if the algorithm fails to converge.
+     * @throws ArithmeticException if the maximal number of iterations is reached
+     * before the expected convergence is achieved.
+     *
+     * @see #evaluate(double,double,int)
      */
     public double evaluate(double x) {
         return evaluate(x, DEFAULT_EPSILON, Integer.MAX_VALUE);
     }
 
     /**
-     * Evaluates the continued fraction at the value x.
+     * Evaluates the continued fraction.
+     *
      * @param x the evaluation point.
-     * @param epsilon maximum error allowed.
-     * @return the value of the continued fraction evaluated at x.
+     * @param epsilon Maximum error allowed.
+     * @return the value of the continued fraction evaluated at {@code x}.
      * @throws ArithmeticException if the algorithm fails to converge.
+     * @throws ArithmeticException if the maximal number of iterations is reached
+     * before the expected convergence is achieved.
+     *
+     * @see #evaluate(double,double,int)
      */
     public double evaluate(double x, double epsilon) {
         return evaluate(x, epsilon, Integer.MAX_VALUE);
@@ -70,10 +82,13 @@ public abstract class ContinuedFraction {
     /**
      * Evaluates the continued fraction at the value x.
      * @param x the evaluation point.
-     * @param maxIterations maximum number of convergents
-     * @return the value of the continued fraction evaluated at x.
+     * @param maxIterations Maximum number of iterations.
+     * @return the value of the continued fraction evaluated at {@code x}.
      * @throws ArithmeticException if the algorithm fails to converge.
-     * @throws ArithmeticException if maximal number of iterations is reached
+     * @throws ArithmeticException if the maximal number of iterations is reached
+     * before the expected convergence is achieved.
+     *
+     * @see #evaluate(double,double,int)
      */
     public double evaluate(double x, int maxIterations) {
         return evaluate(x, DEFAULT_EPSILON, maxIterations);
@@ -94,11 +109,6 @@ public abstract class ContinuedFraction {
      *   </li>
      * </ul>
      *
-     * <p>
-     * <b>Note:</b> the implementation uses the terms a<sub>i</sub> and b<sub>i</sub> as defined in
-     * <a href="http://mathworld.wolfram.com/ContinuedFraction.html">Continued Fraction @ MathWorld</a>.
-     * </p>
-     *
      * @param x Point at which to evaluate the continued fraction.
      * @param epsilon Maximum error allowed.
      * @param maxIterations Maximum number of iterations.