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 2019/12/24 12:14:45 UTC

[commons-math] 03/07: MATH-1362: Use "IntegerSequence.Incrementor".

This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git

commit aa46bcc64b64b4a3990f5938d0e49ce6f333875a
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Tue Dec 24 11:44:43 2019 +0100

    MATH-1362: Use "IntegerSequence.Incrementor".
    
    Also removed spurious "throws" clauses.
---
 .../BaseAbstractUnivariateIntegrator.java          |  3 +-
 .../solvers/BaseAbstractUnivariateSolver.java      | 40 ++++++++--------------
 .../math4/analysis/solvers/BrentSolverTest.java    |  2 +-
 3 files changed, 16 insertions(+), 29 deletions(-)

diff --git a/src/main/java/org/apache/commons/math4/analysis/integration/BaseAbstractUnivariateIntegrator.java b/src/main/java/org/apache/commons/math4/analysis/integration/BaseAbstractUnivariateIntegrator.java
index 690615f..1f89468 100644
--- a/src/main/java/org/apache/commons/math4/analysis/integration/BaseAbstractUnivariateIntegrator.java
+++ b/src/main/java/org/apache/commons/math4/analysis/integration/BaseAbstractUnivariateIntegrator.java
@@ -221,8 +221,7 @@ public abstract class BaseAbstractUnivariateIntegrator implements UnivariateInte
 
     /**
      * Prepare for computation.
-     * Subclasses must call this method if they override any of the
-     * {@code solve} methods.
+     * Subclasses must call this method if they the {@code integrate} method.
      *
      * @param maxEval Maximum number of evaluations.
      * @param f the integrand function
diff --git a/src/main/java/org/apache/commons/math4/analysis/solvers/BaseAbstractUnivariateSolver.java b/src/main/java/org/apache/commons/math4/analysis/solvers/BaseAbstractUnivariateSolver.java
index a65cd41..650a382 100644
--- a/src/main/java/org/apache/commons/math4/analysis/solvers/BaseAbstractUnivariateSolver.java
+++ b/src/main/java/org/apache/commons/math4/analysis/solvers/BaseAbstractUnivariateSolver.java
@@ -23,7 +23,7 @@ import org.apache.commons.math4.exception.NoBracketingException;
 import org.apache.commons.math4.exception.NullArgumentException;
 import org.apache.commons.math4.exception.NumberIsTooLargeException;
 import org.apache.commons.math4.exception.TooManyEvaluationsException;
-import org.apache.commons.math4.util.Incrementor;
+import org.apache.commons.math4.util.IntegerSequence;
 import org.apache.commons.math4.util.MathUtils;
 
 /**
@@ -51,7 +51,7 @@ public abstract class BaseAbstractUnivariateSolver<FUNC extends UnivariateFuncti
     /** Relative accuracy. */
     private final double relativeAccuracy;
     /** Evaluations counter. */
-    private final Incrementor evaluations = new Incrementor();
+    private IntegerSequence.Incrementor evaluations;
     /** Lower end of search interval. */
     private double searchMin;
     /** Higher end of search interval. */
@@ -158,8 +158,7 @@ public abstract class BaseAbstractUnivariateSolver<FUNC extends UnivariateFuncti
      * @throws TooManyEvaluationsException if the maximal number of evaluations
      * is exceeded.
      */
-    protected double computeObjectiveValue(double point)
-        throws TooManyEvaluationsException {
+    protected double computeObjectiveValue(double point) {
         incrementEvaluationCount();
         return function.value(point);
     }
@@ -179,8 +178,7 @@ public abstract class BaseAbstractUnivariateSolver<FUNC extends UnivariateFuncti
     protected void setup(int maxEval,
                          FUNC f,
                          double min, double max,
-                         double startValue)
-        throws NullArgumentException {
+                         double startValue) {
         // Checks.
         MathUtils.checkNotNull(f);
 
@@ -189,15 +187,13 @@ public abstract class BaseAbstractUnivariateSolver<FUNC extends UnivariateFuncti
         searchMax = max;
         searchStart = startValue;
         function = f;
-        evaluations.setMaximalCount(maxEval);
-        evaluations.resetCount();
+        evaluations = IntegerSequence.Incrementor.create()
+            .withMaximalCount(maxEval);
     }
 
     /** {@inheritDoc} */
     @Override
-    public double solve(int maxEval, FUNC f, double min, double max, double startValue)
-        throws TooManyEvaluationsException,
-               NoBracketingException {
+    public double solve(int maxEval, FUNC f, double min, double max, double startValue) {
         // Initialization.
         setup(maxEval, f, min, max, startValue);
 
@@ -213,9 +209,7 @@ public abstract class BaseAbstractUnivariateSolver<FUNC extends UnivariateFuncti
 
     /** {@inheritDoc} */
     @Override
-    public double solve(int maxEval, FUNC f, double startValue)
-        throws TooManyEvaluationsException,
-               NoBracketingException {
+    public double solve(int maxEval, FUNC f, double startValue) {
         return solve(maxEval, f, Double.NaN, Double.NaN, startValue);
     }
 
@@ -229,8 +223,7 @@ public abstract class BaseAbstractUnivariateSolver<FUNC extends UnivariateFuncti
      * @throws NoBracketingException if the initial search interval does not bracket
      * a root and the solver requires it.
      */
-    protected abstract double doSolve()
-        throws TooManyEvaluationsException, NoBracketingException;
+    protected abstract double doSolve();
 
     /**
      * Check whether the function takes opposite signs at the endpoints.
@@ -267,8 +260,7 @@ public abstract class BaseAbstractUnivariateSolver<FUNC extends UnivariateFuncti
      * @throws NumberIsTooLargeException if {@code lower >= upper}.
      */
     protected void verifyInterval(final double lower,
-                                  final double upper)
-        throws NumberIsTooLargeException {
+                                  final double upper) {
         UnivariateSolverUtils.verifyInterval(lower, upper);
     }
 
@@ -283,8 +275,7 @@ public abstract class BaseAbstractUnivariateSolver<FUNC extends UnivariateFuncti
      */
     protected void verifySequence(final double lower,
                                   final double initial,
-                                  final double upper)
-        throws NumberIsTooLargeException {
+                                  final double upper) {
         UnivariateSolverUtils.verifySequence(lower, initial, upper);
     }
 
@@ -299,9 +290,7 @@ public abstract class BaseAbstractUnivariateSolver<FUNC extends UnivariateFuncti
      * the endpoints.
      */
     protected void verifyBracketing(final double lower,
-                                    final double upper)
-        throws NullArgumentException,
-               NoBracketingException {
+                                    final double upper) {
         UnivariateSolverUtils.verifyBracketing(function, lower, upper);
     }
 
@@ -315,10 +304,9 @@ public abstract class BaseAbstractUnivariateSolver<FUNC extends UnivariateFuncti
      * @throws TooManyEvaluationsException when the allowed number of function
      * evaluations has been exhausted.
      */
-    protected void incrementEvaluationCount()
-        throws TooManyEvaluationsException {
+    protected void incrementEvaluationCount() {
         try {
-            evaluations.incrementCount();
+            evaluations.increment();
         } catch (MaxCountExceededException e) {
             throw new TooManyEvaluationsException(e.getMax());
         }
diff --git a/src/test/java/org/apache/commons/math4/analysis/solvers/BrentSolverTest.java b/src/test/java/org/apache/commons/math4/analysis/solvers/BrentSolverTest.java
index cc17d0c..79af777 100644
--- a/src/test/java/org/apache/commons/math4/analysis/solvers/BrentSolverTest.java
+++ b/src/test/java/org/apache/commons/math4/analysis/solvers/BrentSolverTest.java
@@ -264,7 +264,7 @@ public final class BrentSolverTest {
             };
 
         BrentSolver solver = new BrentSolver();
-        final double result = solver.solve(99, f, 1, 1e30, 1 + 1e-10);
+        final double result = solver.solve(100, f, 1, 1e30, 1 + 1e-10);
         Assert.assertEquals(804.93558250, result, solver.getAbsoluteAccuracy());
     }
 }