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:42 UTC

[commons-math] branch master updated (bf5d19e -> 096cc55)

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

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


    from bf5d19e  Removed obsolete interface.
     new 5828c23  MATH-1362: Use "IntegerSequence.Incrementor".
     new 8bff9c3  Spurious "throws" clauses.
     new aa46bcc  MATH-1362: Use "IntegerSequence.Incrementor".
     new 22da230  MATH-1362: Use "IntegerSequence.Incrementor".
     new 8091c4f  MATH-1362: Use "IntegerSequence.Incrementor".
     new ef4596d  MATH-1362: Removed class that is not used anymore within "Commons Math".
     new 096cc55  Javadoc.

The 7 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/changes/changes.xml                            |   5 +-
 .../BaseAbstractUnivariateIntegrator.java          |  55 +++----
 .../IterativeLegendreGaussIntegrator.java          |  17 +-
 .../analysis/integration/MidPointIntegrator.java   |   2 +-
 .../analysis/integration/RombergIntegrator.java    |   4 +-
 .../analysis/integration/SimpsonIntegrator.java    |   2 +-
 .../analysis/integration/TrapezoidIntegrator.java  |   4 +-
 .../solvers/BaseAbstractUnivariateSolver.java      |  40 ++---
 .../fitting/leastsquares/GaussNewtonOptimizer.java |  10 +-
 .../fitting/leastsquares/LeastSquaresAdapter.java  |   6 +-
 .../fitting/leastsquares/LeastSquaresFactory.java  |   6 +-
 .../leastsquares/LevenbergMarquardtOptimizer.java  |  12 +-
 .../math4/linear/IterativeLinearSolver.java        |   6 +-
 .../PreconditionedIterativeLinearSolver.java       |   6 +-
 .../org/apache/commons/math4/linear/SymmLQ.java    |   6 +-
 .../math4/optim/AbstractOptimizationProblem.java   |  18 +-
 .../apache/commons/math4/optim/BaseOptimizer.java  |  53 +++---
 .../commons/math4/optim/OptimizationProblem.java   |   6 +-
 .../org/apache/commons/math4/util/Incrementor.java | 183 ---------------------
 .../commons/math4/util/IterationManager.java       |  47 ++++--
 .../IterativeLegendreGaussIntegratorTest.java      |   2 +-
 .../math4/analysis/solvers/BrentSolverTest.java    |   2 +-
 .../math4/optim/linear/SimplexSolverTest.java      |   2 +-
 .../math4/optim/univariate/BrentOptimizerTest.java |   2 +-
 .../apache/commons/math4/util/IncrementorTest.java | 139 ----------------
 25 files changed, 160 insertions(+), 475 deletions(-)
 delete mode 100644 src/main/java/org/apache/commons/math4/util/Incrementor.java
 delete mode 100644 src/test/java/org/apache/commons/math4/util/IncrementorTest.java


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

Posted by er...@apache.org.
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 5828c2352d5759f7d44dbc29e5a4f597b41a398c
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Mon Dec 23 12:33:33 2019 +0100

    MATH-1362: Use "IntegerSequence.Incrementor".
---
 .../BaseAbstractUnivariateIntegrator.java          | 29 ++++++++++------------
 .../IterativeLegendreGaussIntegrator.java          |  2 +-
 .../analysis/integration/MidPointIntegrator.java   |  2 +-
 .../analysis/integration/RombergIntegrator.java    |  4 +--
 .../analysis/integration/SimpsonIntegrator.java    |  2 +-
 .../analysis/integration/TrapezoidIntegrator.java  |  4 +--
 .../IterativeLegendreGaussIntegratorTest.java      |  2 +-
 7 files changed, 21 insertions(+), 24 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 595f18f..3cb31b6 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
@@ -24,7 +24,7 @@ import org.apache.commons.math4.exception.NotStrictlyPositiveException;
 import org.apache.commons.math4.exception.NullArgumentException;
 import org.apache.commons.math4.exception.NumberIsTooSmallException;
 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;
 
 /**
@@ -46,8 +46,6 @@ public abstract class BaseAbstractUnivariateIntegrator implements UnivariateInte
     /** Default maximal iteration count. */
     public static final int DEFAULT_MAX_ITERATIONS_COUNT = Integer.MAX_VALUE;
 
-    /** The iteration count. */
-    protected final Incrementor iterations;
 
     /** Maximum absolute error. */
     private final double absoluteAccuracy;
@@ -57,9 +55,14 @@ public abstract class BaseAbstractUnivariateIntegrator implements UnivariateInte
 
     /** minimum number of iterations */
     private final int minimalIterationCount;
+    /** maximum number of iterations */
+    private final int maximalIterationCount;
+
+    /** The iteration count. */
+    protected IntegerSequence.Incrementor iterations;
 
     /** The functions evaluation count. */
-    private final Incrementor evaluations;
+    private IntegerSequence.Incrementor evaluations;
 
     /** Function to integrate. */
     private UnivariateFunction function;
@@ -123,12 +126,7 @@ public abstract class BaseAbstractUnivariateIntegrator implements UnivariateInte
             throw new NumberIsTooSmallException(maximalIterationCount, minimalIterationCount, false);
         }
         this.minimalIterationCount = minimalIterationCount;
-        this.iterations            = new Incrementor();
-        iterations.setMaximalCount(maximalIterationCount);
-
-        // prepare evaluations counter, but do not set it yet
-        evaluations = new Incrementor();
-
+        this.maximalIterationCount = maximalIterationCount;
     }
 
     /**
@@ -218,7 +216,7 @@ public abstract class BaseAbstractUnivariateIntegrator implements UnivariateInte
     protected double computeObjectiveValue(final double point)
         throws TooManyEvaluationsException {
         try {
-            evaluations.incrementCount();
+            evaluations.increment();
         } catch (MaxCountExceededException e) {
             throw new TooManyEvaluationsException(e.getMax());
         }
@@ -250,10 +248,10 @@ public abstract class BaseAbstractUnivariateIntegrator implements UnivariateInte
         min = lower;
         max = upper;
         function = f;
-        evaluations.setMaximalCount(maxEval);
-        evaluations.resetCount();
-        iterations.resetCount();
-
+        iterations = IntegerSequence.Incrementor.create()
+            .withMaximalCount(maximalIterationCount);
+        evaluations = IntegerSequence.Incrementor.create()
+            .withMaximalCount(maxEval);
     }
 
     /** {@inheritDoc} */
@@ -268,7 +266,6 @@ public abstract class BaseAbstractUnivariateIntegrator implements UnivariateInte
 
         // Perform computation.
         return doIntegrate();
-
     }
 
     /**
diff --git a/src/main/java/org/apache/commons/math4/analysis/integration/IterativeLegendreGaussIntegrator.java b/src/main/java/org/apache/commons/math4/analysis/integration/IterativeLegendreGaussIntegrator.java
index f65334c..9701c21 100644
--- a/src/main/java/org/apache/commons/math4/analysis/integration/IterativeLegendreGaussIntegrator.java
+++ b/src/main/java/org/apache/commons/math4/analysis/integration/IterativeLegendreGaussIntegrator.java
@@ -142,7 +142,7 @@ public class IterativeLegendreGaussIntegrator
             final double ratio = FastMath.min(4, FastMath.pow(delta / limit, 0.5 / numberOfPoints));
             n = FastMath.max((int) (ratio * n), n + 1);
             oldt = t;
-            iterations.incrementCount();
+            iterations.increment();
         }
     }
 
diff --git a/src/main/java/org/apache/commons/math4/analysis/integration/MidPointIntegrator.java b/src/main/java/org/apache/commons/math4/analysis/integration/MidPointIntegrator.java
index edcb300..08cc3cc 100644
--- a/src/main/java/org/apache/commons/math4/analysis/integration/MidPointIntegrator.java
+++ b/src/main/java/org/apache/commons/math4/analysis/integration/MidPointIntegrator.java
@@ -150,7 +150,7 @@ public class MidPointIntegrator extends BaseAbstractUnivariateIntegrator {
         double oldt = diff * computeObjectiveValue(midPoint);
 
         while (true) {
-            iterations.incrementCount();
+            iterations.increment();
             final int i = iterations.getCount();
             final double t = stage(i, oldt, min, diff);
             if (i >= getMinimalIterationCount()) {
diff --git a/src/main/java/org/apache/commons/math4/analysis/integration/RombergIntegrator.java b/src/main/java/org/apache/commons/math4/analysis/integration/RombergIntegrator.java
index 923de73..6205caa 100644
--- a/src/main/java/org/apache/commons/math4/analysis/integration/RombergIntegrator.java
+++ b/src/main/java/org/apache/commons/math4/analysis/integration/RombergIntegrator.java
@@ -107,7 +107,7 @@ public class RombergIntegrator extends BaseAbstractUnivariateIntegrator {
 
         TrapezoidIntegrator qtrap = new TrapezoidIntegrator();
         currentRow[0] = qtrap.stage(this, 0);
-        iterations.incrementCount();
+        iterations.increment();
         double olds = currentRow[0];
         while (true) {
 
@@ -119,7 +119,7 @@ public class RombergIntegrator extends BaseAbstractUnivariateIntegrator {
             currentRow = tmpRow;
 
             currentRow[0] = qtrap.stage(this, i);
-            iterations.incrementCount();
+            iterations.increment();
             for (int j = 1; j <= i; j++) {
                 // Richardson extrapolation coefficient
                 final double r = (1L << (2 * j)) - 1;
diff --git a/src/main/java/org/apache/commons/math4/analysis/integration/SimpsonIntegrator.java b/src/main/java/org/apache/commons/math4/analysis/integration/SimpsonIntegrator.java
index 0ede25e..19e6108 100644
--- a/src/main/java/org/apache/commons/math4/analysis/integration/SimpsonIntegrator.java
+++ b/src/main/java/org/apache/commons/math4/analysis/integration/SimpsonIntegrator.java
@@ -105,7 +105,7 @@ public class SimpsonIntegrator extends BaseAbstractUnivariateIntegrator {
         double olds = (4 * oldt - s0) / 3.0;
         while (true) {
             // The first iteration is the first refinement of the sum.
-            iterations.incrementCount();
+            iterations.increment();
             final int i = getIterations();
             final double t = qtrap.stage(this, i + 1); // 1-stage ahead of the iteration
             final double s = (4 * t - oldt) / 3.0;
diff --git a/src/main/java/org/apache/commons/math4/analysis/integration/TrapezoidIntegrator.java b/src/main/java/org/apache/commons/math4/analysis/integration/TrapezoidIntegrator.java
index 893d602..2ccfed4 100644
--- a/src/main/java/org/apache/commons/math4/analysis/integration/TrapezoidIntegrator.java
+++ b/src/main/java/org/apache/commons/math4/analysis/integration/TrapezoidIntegrator.java
@@ -147,7 +147,7 @@ public class TrapezoidIntegrator extends BaseAbstractUnivariateIntegrator {
         throws MathIllegalArgumentException, TooManyEvaluationsException, MaxCountExceededException {
 
         double oldt = stage(this, 0);
-        iterations.incrementCount();
+        iterations.increment();
         while (true) {
             final int i = iterations.getCount();
             final double t = stage(this, i);
@@ -160,7 +160,7 @@ public class TrapezoidIntegrator extends BaseAbstractUnivariateIntegrator {
                 }
             }
             oldt = t;
-            iterations.incrementCount();
+            iterations.increment();
         }
 
     }
diff --git a/src/test/java/org/apache/commons/math4/analysis/integration/IterativeLegendreGaussIntegratorTest.java b/src/test/java/org/apache/commons/math4/analysis/integration/IterativeLegendreGaussIntegratorTest.java
index 2f2b467..95f27ba 100644
--- a/src/test/java/org/apache/commons/math4/analysis/integration/IterativeLegendreGaussIntegratorTest.java
+++ b/src/test/java/org/apache/commons/math4/analysis/integration/IterativeLegendreGaussIntegratorTest.java
@@ -117,7 +117,7 @@ public class IterativeLegendreGaussIntegratorTest {
 
         final double a = -5000;
         final double b = 5000;
-        final double s = integrator.integrate(50, normal, a, b);
+        final double s = integrator.integrate(60, normal, a, b);
         Assert.assertEquals(1, s, 1e-5);
     }
 


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

Posted by er...@apache.org.
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());
     }
 }


[commons-math] 06/07: MATH-1362: Removed class that is not used anymore within "Commons Math".

Posted by er...@apache.org.
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 ef4596df33979faac6253056c5f755792a3751ba
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Tue Dec 24 13:04:51 2019 +0100

    MATH-1362: Removed class that is not used anymore within "Commons Math".
---
 src/changes/changes.xml                            |   5 +-
 .../org/apache/commons/math4/util/Incrementor.java | 183 ---------------------
 .../apache/commons/math4/util/IncrementorTest.java | 139 ----------------
 3 files changed, 4 insertions(+), 323 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index c839678..14d6071 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -54,8 +54,11 @@ If the output is not quite correct, check for invisible trailing spaces!
     </release>
 
     <release version="4.0" date="XXXX-XX-XX" description="">
+      <action dev="erans" type="update" issue="MATH-1362">
+        Removed deprecated class "Incrementor" (replaced by "IntegerSequence.Incrementor").
+      </action>
       <action dev="erans" type="update" issue="MATH-1506">
-        Removed class "MultiDimensionalCounter" (ported to "Commons Numbers").
+        Removed class "MultidimensionalCounter" (ported to "Commons Numbers").
       </action>
       <action dev="erans" type="update" issue="MATH-1504">
         Removed class "BigFraction" (ported to "Commons Numbers").
diff --git a/src/main/java/org/apache/commons/math4/util/Incrementor.java b/src/main/java/org/apache/commons/math4/util/Incrementor.java
deleted file mode 100644
index 0898ec5..0000000
--- a/src/main/java/org/apache/commons/math4/util/Incrementor.java
+++ /dev/null
@@ -1,183 +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.util;
-
-import org.apache.commons.math4.exception.MaxCountExceededException;
-import org.apache.commons.math4.exception.NullArgumentException;
-
-/**
- * Utility that increments a counter until a maximum is reached, at
- * which point, the instance will by default throw a
- * {@link MaxCountExceededException}.
- * However, the user is able to override this behaviour by defining a
- * custom {@link MaxCountExceededCallback callback}, in order to e.g.
- * select which exception must be thrown.
- *
- * @since 3.0
- *
- * @deprecated Use {@link IntegerSequence.Incrementor} instead.
- */
-@Deprecated
-public class Incrementor {
-    /**
-     * Upper limit for the counter.
-     */
-    private int maximalCount;
-    /**
-     * Current count.
-     */
-    private int count = 0;
-    /**
-     * Function called at counter exhaustion.
-     */
-    private final MaxCountExceededCallback maxCountCallback;
-
-    /**
-     * Default constructor.
-     * For the new instance to be useful, the maximal count must be set
-     * by calling {@link #setMaximalCount(int) setMaximalCount}.
-     */
-    public Incrementor() {
-        this(0);
-    }
-
-    /**
-     * Defines a maximal count.
-     *
-     * @param max Maximal count.
-     */
-    public Incrementor(int max) {
-        this(max,
-             new MaxCountExceededCallback() {
-                 /** {@inheritDoc} */
-                 @Override
-                public void trigger(int max) throws MaxCountExceededException {
-                     throw new MaxCountExceededException(max);
-                 }
-             });
-    }
-
-    /**
-     * Defines a maximal count and a callback method to be triggered at
-     * counter exhaustion.
-     *
-     * @param max Maximal count.
-     * @param cb Function to be called when the maximal count has been reached.
-     * @throws NullArgumentException if {@code cb} is {@code null}
-     */
-    public Incrementor(int max, MaxCountExceededCallback cb)
-        throws NullArgumentException {
-        if (cb == null){
-            throw new NullArgumentException();
-        }
-        maximalCount = max;
-        maxCountCallback = cb;
-    }
-
-    /**
-     * Sets the upper limit for the counter.
-     * This does not automatically reset the current count to zero (see
-     * {@link #resetCount()}).
-     *
-     * @param max Upper limit of the counter.
-     */
-    public void setMaximalCount(int max) {
-        maximalCount = max;
-    }
-
-    /**
-     * Gets the upper limit of the counter.
-     *
-     * @return the counter upper limit.
-     */
-    public int getMaximalCount() {
-        return maximalCount;
-    }
-
-    /**
-     * Gets the current count.
-     *
-     * @return the current count.
-     */
-    public int getCount() {
-        return count;
-    }
-
-    /**
-     * Checks whether a single increment is allowed.
-     *
-     * @return {@code false} if the next call to {@link #incrementCount(int)
-     * incrementCount} will trigger a {@code MaxCountExceededException},
-     * {@code true} otherwise.
-     */
-    public boolean canIncrement() {
-        return count < maximalCount;
-    }
-
-    /**
-     * Performs multiple increments.
-     * See the other {@link #incrementCount() incrementCount} method).
-     *
-     * @param value Number of increments.
-     * @throws MaxCountExceededException at counter exhaustion.
-     */
-    public void incrementCount(int value) throws MaxCountExceededException {
-        for (int i = 0; i < value; i++) {
-            incrementCount();
-        }
-    }
-
-    /**
-     * Adds one to the current iteration count.
-     * At counter exhaustion, this method will call the
-     * {@link MaxCountExceededCallback#trigger(int) trigger} method of the
-     * callback object passed to the
-     * {@link #Incrementor(int,MaxCountExceededCallback) constructor}.
-     * If not explictly set, a default callback is used that will throw
-     * a {@code MaxCountExceededException}.
-     *
-     * @throws MaxCountExceededException at counter exhaustion, unless a
-     * custom {@link MaxCountExceededCallback callback} has been set at
-     * construction.
-     */
-    public void incrementCount() throws MaxCountExceededException {
-        if (++count > maximalCount) {
-            maxCountCallback.trigger(maximalCount);
-        }
-    }
-
-    /**
-     * Resets the counter to 0.
-     */
-    public void resetCount() {
-        count = 0;
-    }
-
-    /**
-     * Defines a method to be called at counter exhaustion.
-     * The {@link #trigger(int) trigger} method should usually throw an exception.
-     */
-    public interface MaxCountExceededCallback {
-        /**
-         * Function called when the maximal count has been reached.
-         *
-         * @param maximalCount Maximal count.
-         * @throws MaxCountExceededException at counter exhaustion
-         */
-        void trigger(int maximalCount) throws MaxCountExceededException;
-    }
-}
diff --git a/src/test/java/org/apache/commons/math4/util/IncrementorTest.java b/src/test/java/org/apache/commons/math4/util/IncrementorTest.java
deleted file mode 100644
index a28d065..0000000
--- a/src/test/java/org/apache/commons/math4/util/IncrementorTest.java
+++ /dev/null
@@ -1,139 +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.util;
-
-import org.apache.commons.math4.exception.MaxCountExceededException;
-import org.apache.commons.math4.exception.TooManyEvaluationsException;
-import org.apache.commons.math4.util.Incrementor;
-import org.junit.Assert;
-import org.junit.Test;
-
-/**
- * Test for {@link Incrementor}.
- */
-public class IncrementorTest {
-    @Test
-    public void testConstructor1() {
-        final Incrementor i = new Incrementor();
-        Assert.assertEquals(0, i.getMaximalCount());
-        Assert.assertEquals(0, i.getCount());
-    }
-
-    @Test
-    public void testConstructor2() {
-        final Incrementor i = new Incrementor(10);
-        Assert.assertEquals(10, i.getMaximalCount());
-        Assert.assertEquals(0, i.getCount());
-    }
-
-    @Test
-    public void testCanIncrement1() {
-        final Incrementor i = new Incrementor(3);
-        Assert.assertTrue(i.canIncrement());
-        i.incrementCount();
-        Assert.assertTrue(i.canIncrement());
-        i.incrementCount();
-        Assert.assertTrue(i.canIncrement());
-        i.incrementCount();
-        Assert.assertFalse(i.canIncrement());
-    }
-
-    @Test
-    public void testCanIncrement2() {
-        final Incrementor i = new Incrementor(3);
-        while (i.canIncrement()) {
-            i.incrementCount();
-        }
-
-        // Must keep try/catch because the exception must be generated here,
-        // and not in the previous loop.
-        try {
-            i.incrementCount();
-            Assert.fail("MaxCountExceededException expected");
-        } catch (MaxCountExceededException e) {
-            // Expected.
-        }
-    }
-
-    @Test
-    public void testAccessor() {
-        final Incrementor i = new Incrementor();
-
-        i.setMaximalCount(10);
-        Assert.assertEquals(10, i.getMaximalCount());
-        Assert.assertEquals(0, i.getCount());
-    }
-
-    @Test
-    public void testBelowMaxCount() {
-        final Incrementor i = new Incrementor();
-
-        i.setMaximalCount(3);
-        i.incrementCount();
-        i.incrementCount();
-        i.incrementCount();
-
-        Assert.assertEquals(3, i.getCount());
-    }
-
-    @Test(expected=MaxCountExceededException.class)
-    public void testAboveMaxCount() {
-        final Incrementor i = new Incrementor();
-
-        i.setMaximalCount(3);
-        i.incrementCount();
-        i.incrementCount();
-        i.incrementCount();
-        i.incrementCount();
-    }
-
-    @Test(expected=TooManyEvaluationsException.class)
-    public void testAlternateException() {
-        final Incrementor.MaxCountExceededCallback cb
-            = new Incrementor.MaxCountExceededCallback() {
-                    /** {@inheritDoc} */
-                    @Override
-                    public void trigger(int max) {
-                        throw new TooManyEvaluationsException(max);
-                    }
-                };
-
-        final Incrementor i = new Incrementor(0, cb);
-        i.incrementCount();
-    }
-
-    @Test
-    public void testReset() {
-        final Incrementor i = new Incrementor();
-
-        i.setMaximalCount(3);
-        i.incrementCount();
-        i.incrementCount();
-        i.incrementCount();
-        Assert.assertEquals(3, i.getCount());
-        i.resetCount();
-        Assert.assertEquals(0, i.getCount());
-    }
-
-    @Test
-    public void testBulkIncrement() {
-        final Incrementor i = new Incrementor();
-
-        i.setMaximalCount(3);
-        i.incrementCount(2);
-        Assert.assertEquals(2, i.getCount());
-        i.incrementCount(1);
-        Assert.assertEquals(3, i.getCount());
-    }
-}


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

Posted by er...@apache.org.
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 8091c4fe8395ef69a652885bb6eef0c2ac935927
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Tue Dec 24 12:56:13 2019 +0100

    MATH-1362: Use "IntegerSequence.Incrementor".
---
 .../commons/math4/util/IterationManager.java       | 47 ++++++++++++++--------
 1 file changed, 31 insertions(+), 16 deletions(-)

diff --git a/src/main/java/org/apache/commons/math4/util/IterationManager.java b/src/main/java/org/apache/commons/math4/util/IterationManager.java
index 550bd25..bd29ed7 100644
--- a/src/main/java/org/apache/commons/math4/util/IterationManager.java
+++ b/src/main/java/org/apache/commons/math4/util/IterationManager.java
@@ -29,36 +29,43 @@ import org.apache.commons.math4.exception.MaxCountExceededException;
  *
  */
 public class IterationManager {
-
-    /** Keeps a count of the number of iterations. */
-    private final Incrementor iterations;
-
     /** The collection of all listeners attached to this iterative algorithm. */
     private final Collection<IterationListener> listeners;
+    /** Maximum number of iterations. */
+    private final int maxIterations;
+    /** Callback. */
+    private final IntegerSequence.Incrementor.MaxCountExceededCallback callback;
+    /** Keeps a count of the number of iterations. */
+    private IntegerSequence.Incrementor iterations;
 
     /**
      * Creates a new instance of this class.
      *
-     * @param maxIterations the maximum number of iterations
+     * @param maxIterations Maximum number of iterations.
+     * {@link MaxCountExceededException} will be raised at
+     * counter exhaustion.
      */
     public IterationManager(final int maxIterations) {
-        this.iterations = new Incrementor(maxIterations);
-        this.listeners = new CopyOnWriteArrayList<>();
+        this(maxIterations, null);
     }
 
     /**
      * Creates a new instance of this class.
      *
      * @param maxIterations the maximum number of iterations
-     * @param callBack the function to be called when the maximum number of
-     * iterations has been reached
-     * @throws org.apache.commons.math4.exception.NullArgumentException if {@code callBack} is {@code null}
+     * @param callback the function to be called when the maximum number of
+     * iterations has been reached.
+     * If {@code null}, {@link MaxCountExceededException} will be raised at
+     * counter exhaustion.
+     *
      * @since 3.1
      */
     public IterationManager(final int maxIterations,
-                            final Incrementor.MaxCountExceededCallback callBack) {
-        this.iterations = new Incrementor(maxIterations, callBack);
+                            final IntegerSequence.Incrementor.MaxCountExceededCallback callback) {
+        this.maxIterations = maxIterations;
+        this.callback = callback;
         this.listeners = new CopyOnWriteArrayList<>();
+        resetCounter();
     }
 
     /**
@@ -145,9 +152,8 @@ public class IterationManager {
      * @throws MaxCountExceededException if the maximum number of iterations is
      * reached.
      */
-    public void incrementIterationCount()
-        throws MaxCountExceededException {
-        iterations.incrementCount();
+    public void incrementIterationCount() {
+        iterations.increment();
     }
 
     /**
@@ -167,6 +173,15 @@ public class IterationManager {
      * initial phase.
      */
     public void resetIterationCount() {
-        iterations.resetCount();
+        resetCounter();
+    }
+
+    /** Reset counter. */
+    private void resetCounter() {
+        iterations = IntegerSequence.Incrementor.create()
+            .withMaximalCount(maxIterations);
+        if (callback != null) {
+            iterations = iterations.withCallback(callback);
+        }
     }
 }


[commons-math] 02/07: Spurious "throws" clauses.

Posted by er...@apache.org.
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 8bff9c35f3722f8b1a550d06ba55950fddf17af0
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Tue Dec 24 11:28:31 2019 +0100

    Spurious "throws" clauses.
---
 .../BaseAbstractUnivariateIntegrator.java          | 23 +++++++---------------
 .../IterativeLegendreGaussIntegrator.java          | 15 +++++---------
 2 files changed, 12 insertions(+), 26 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 3cb31b6..690615f 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
@@ -111,9 +111,7 @@ public abstract class BaseAbstractUnivariateIntegrator implements UnivariateInte
     protected BaseAbstractUnivariateIntegrator(final double relativeAccuracy,
                                                final double absoluteAccuracy,
                                                final int minimalIterationCount,
-                                               final int maximalIterationCount)
-        throws NotStrictlyPositiveException, NumberIsTooSmallException {
-
+                                               final int maximalIterationCount) {
         // accuracy settings
         this.relativeAccuracy      = relativeAccuracy;
         this.absoluteAccuracy      = absoluteAccuracy;
@@ -135,7 +133,7 @@ public abstract class BaseAbstractUnivariateIntegrator implements UnivariateInte
      * @param absoluteAccuracy absolute accuracy of the result
      */
     protected BaseAbstractUnivariateIntegrator(final double relativeAccuracy,
-                                           final double absoluteAccuracy) {
+                                               final double absoluteAccuracy) {
         this(relativeAccuracy, absoluteAccuracy,
              DEFAULT_MIN_ITERATIONS_COUNT, DEFAULT_MAX_ITERATIONS_COUNT);
     }
@@ -150,8 +148,7 @@ public abstract class BaseAbstractUnivariateIntegrator implements UnivariateInte
      * is lesser than or equal to the minimal number of iterations
      */
     protected BaseAbstractUnivariateIntegrator(final int minimalIterationCount,
-                                           final int maximalIterationCount)
-        throws NotStrictlyPositiveException, NumberIsTooSmallException {
+                                               final int maximalIterationCount) {
         this(DEFAULT_RELATIVE_ACCURACY, DEFAULT_ABSOLUTE_ACCURACY,
              minimalIterationCount, maximalIterationCount);
     }
@@ -213,8 +210,7 @@ public abstract class BaseAbstractUnivariateIntegrator implements UnivariateInte
      * @throws TooManyEvaluationsException if the maximal number of function
      * evaluations is exceeded.
      */
-    protected double computeObjectiveValue(final double point)
-        throws TooManyEvaluationsException {
+    protected double computeObjectiveValue(final double point) {
         try {
             evaluations.increment();
         } catch (MaxCountExceededException e) {
@@ -237,8 +233,7 @@ public abstract class BaseAbstractUnivariateIntegrator implements UnivariateInte
      */
     protected void setup(final int maxEval,
                          final UnivariateFunction f,
-                         final double lower, final double upper)
-        throws NullArgumentException, MathIllegalArgumentException {
+                         final double lower, final double upper) {
 
         // Checks.
         MathUtils.checkNotNull(f);
@@ -257,9 +252,7 @@ public abstract class BaseAbstractUnivariateIntegrator implements UnivariateInte
     /** {@inheritDoc} */
     @Override
     public double integrate(final int maxEval, final UnivariateFunction f,
-                            final double lower, final double upper)
-        throws TooManyEvaluationsException, MaxCountExceededException,
-               MathIllegalArgumentException, NullArgumentException {
+                            final double lower, final double upper) {
 
         // Initialization.
         setup(maxEval, f, lower, upper);
@@ -278,7 +271,5 @@ public abstract class BaseAbstractUnivariateIntegrator implements UnivariateInte
      * @throws MaxCountExceededException if the maximum iteration count is exceeded
      * or the integrator detects convergence problems otherwise
      */
-    protected abstract double doIntegrate()
-        throws TooManyEvaluationsException, MaxCountExceededException;
-
+    protected abstract double doIntegrate() ;
 }
diff --git a/src/main/java/org/apache/commons/math4/analysis/integration/IterativeLegendreGaussIntegrator.java b/src/main/java/org/apache/commons/math4/analysis/integration/IterativeLegendreGaussIntegrator.java
index 9701c21..310b290 100644
--- a/src/main/java/org/apache/commons/math4/analysis/integration/IterativeLegendreGaussIntegrator.java
+++ b/src/main/java/org/apache/commons/math4/analysis/integration/IterativeLegendreGaussIntegrator.java
@@ -69,8 +69,7 @@ public class IterativeLegendreGaussIntegrator
                                             final double relativeAccuracy,
                                             final double absoluteAccuracy,
                                             final int minimalIterationCount,
-                                            final int maximalIterationCount)
-        throws NotStrictlyPositiveException, NumberIsTooSmallException {
+                                            final int maximalIterationCount) {
         super(relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount);
         if (n <= 0) {
             throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_POINTS, n);
@@ -88,8 +87,7 @@ public class IterativeLegendreGaussIntegrator
      */
     public IterativeLegendreGaussIntegrator(final int n,
                                             final double relativeAccuracy,
-                                            final double absoluteAccuracy)
-        throws NotStrictlyPositiveException {
+                                            final double absoluteAccuracy) {
         this(n, relativeAccuracy, absoluteAccuracy,
              DEFAULT_MIN_ITERATIONS_COUNT, DEFAULT_MAX_ITERATIONS_COUNT);
     }
@@ -108,16 +106,14 @@ public class IterativeLegendreGaussIntegrator
      */
     public IterativeLegendreGaussIntegrator(final int n,
                                             final int minimalIterationCount,
-                                            final int maximalIterationCount)
-        throws NotStrictlyPositiveException, NumberIsTooSmallException {
+                                            final int maximalIterationCount) {
         this(n, DEFAULT_RELATIVE_ACCURACY, DEFAULT_ABSOLUTE_ACCURACY,
              minimalIterationCount, maximalIterationCount);
     }
 
     /** {@inheritDoc} */
     @Override
-    protected double doIntegrate()
-        throws MathIllegalArgumentException, TooManyEvaluationsException, MaxCountExceededException {
+    protected double doIntegrate() {
         // Compute first estimate with a single step.
         double oldt = stage(1);
 
@@ -160,8 +156,7 @@ public class IterativeLegendreGaussIntegrator
         final UnivariateFunction f = new UnivariateFunction() {
                 /** {@inheritDoc} */
                 @Override
-                public double value(double x)
-                    throws MathIllegalArgumentException, TooManyEvaluationsException {
+                public double value(double x) {
                     return computeObjectiveValue(x);
                 }
             };


[commons-math] 07/07: Javadoc.

Posted by er...@apache.org.
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 096cc553b697c47bc453496694edb599066ced74
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Tue Dec 24 13:12:36 2019 +0100

    Javadoc.
---
 .../java/org/apache/commons/math4/linear/IterativeLinearSolver.java | 6 +++---
 .../commons/math4/linear/PreconditionedIterativeLinearSolver.java   | 6 +++---
 src/main/java/org/apache/commons/math4/linear/SymmLQ.java           | 6 +++---
 3 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/src/main/java/org/apache/commons/math4/linear/IterativeLinearSolver.java b/src/main/java/org/apache/commons/math4/linear/IterativeLinearSolver.java
index 877d48b..995ee51 100644
--- a/src/main/java/org/apache/commons/math4/linear/IterativeLinearSolver.java
+++ b/src/main/java/org/apache/commons/math4/linear/IterativeLinearSolver.java
@@ -113,7 +113,7 @@ public abstract class IterativeLinearSolver {
      * inconsistent with {@code a}
      * @throws MaxCountExceededException at exhaustion of the iteration count,
      * unless a custom
-     * {@link org.apache.commons.math4.util.Incrementor.MaxCountExceededCallback callback}
+     * {@link org.apache.commons.math4.util.IntegerSequence.Incrementor.MaxCountExceededCallback callback}
      * has been set at construction of the {@link IterationManager}
      */
     public RealVector solve(final RealLinearOperator a, final RealVector b)
@@ -139,7 +139,7 @@ public abstract class IterativeLinearSolver {
      * dimensions inconsistent with {@code a}
      * @throws MaxCountExceededException at exhaustion of the iteration count,
      * unless a custom
-     * {@link org.apache.commons.math4.util.Incrementor.MaxCountExceededCallback callback}
+     * {@link org.apache.commons.math4.util.IntegerSequence.Incrementor.MaxCountExceededCallback callback}
      * has been set at construction of the {@link IterationManager}
      */
     public RealVector solve(RealLinearOperator a, RealVector b, RealVector x0)
@@ -164,7 +164,7 @@ public abstract class IterativeLinearSolver {
      * dimensions inconsistent with {@code a}
      * @throws MaxCountExceededException at exhaustion of the iteration count,
      * unless a custom
-     * {@link org.apache.commons.math4.util.Incrementor.MaxCountExceededCallback callback}
+     * {@link org.apache.commons.math4.util.IntegerSequence.Incrementor.MaxCountExceededCallback callback}
      * has been set at construction of the {@link IterationManager}
      */
     public abstract RealVector solveInPlace(RealLinearOperator a, RealVector b,
diff --git a/src/main/java/org/apache/commons/math4/linear/PreconditionedIterativeLinearSolver.java b/src/main/java/org/apache/commons/math4/linear/PreconditionedIterativeLinearSolver.java
index 31f538d..d968540 100644
--- a/src/main/java/org/apache/commons/math4/linear/PreconditionedIterativeLinearSolver.java
+++ b/src/main/java/org/apache/commons/math4/linear/PreconditionedIterativeLinearSolver.java
@@ -86,7 +86,7 @@ public abstract class PreconditionedIterativeLinearSolver
      * {@code x0} have dimensions inconsistent with {@code a}
      * @throws MaxCountExceededException at exhaustion of the iteration count,
      * unless a custom
-     * {@link org.apache.commons.math4.util.Incrementor.MaxCountExceededCallback callback}
+     * {@link org.apache.commons.math4.util.IntegerSequence.Incrementor.MaxCountExceededCallback callback}
      * has been set at construction of the {@link IterationManager}
      */
     public RealVector solve(final RealLinearOperator a,
@@ -167,7 +167,7 @@ public abstract class PreconditionedIterativeLinearSolver
      * dimensions inconsistent with {@code a}
      * @throws MaxCountExceededException at exhaustion of the iteration count,
      * unless a custom
-     * {@link org.apache.commons.math4.util.Incrementor.MaxCountExceededCallback callback}
+     * {@link org.apache.commons.math4.util.IntegerSequence.Incrementor.MaxCountExceededCallback callback}
      * has been set at construction of the {@link IterationManager}
      */
     public RealVector solve(RealLinearOperator a, RealLinearOperator m,
@@ -195,7 +195,7 @@ public abstract class PreconditionedIterativeLinearSolver
      * {@code x0} have dimensions inconsistent with {@code a}
      * @throws MaxCountExceededException at exhaustion of the iteration count,
      * unless a custom
-     * {@link org.apache.commons.math4.util.Incrementor.MaxCountExceededCallback callback}
+     * {@link org.apache.commons.math4.util.IntegerSequence.Incrementor.MaxCountExceededCallback callback}
      * has been set at construction of the {@link IterationManager}
      */
     public abstract RealVector solveInPlace(RealLinearOperator a,
diff --git a/src/main/java/org/apache/commons/math4/linear/SymmLQ.java b/src/main/java/org/apache/commons/math4/linear/SymmLQ.java
index 7411427..9de08b4 100644
--- a/src/main/java/org/apache/commons/math4/linear/SymmLQ.java
+++ b/src/main/java/org/apache/commons/math4/linear/SymmLQ.java
@@ -954,7 +954,7 @@ public class SymmLQ
      * inconsistent with {@code a}
      * @throws MaxCountExceededException at exhaustion of the iteration count,
      * unless a custom
-     * {@link org.apache.commons.math4.util.Incrementor.MaxCountExceededCallback callback}
+     * {@link org.apache.commons.math4.util.IntegerSequence.Incrementor.MaxCountExceededCallback callback}
      * has been set at construction of the {@link IterationManager}
      * @throws NonSelfAdjointOperatorException if {@link #getCheck()} is
      * {@code true}, and {@code a} or {@code m} is not self-adjoint
@@ -1042,7 +1042,7 @@ public class SymmLQ
      * inconsistent with {@code a}
      * @throws MaxCountExceededException at exhaustion of the iteration count,
      * unless a custom
-     * {@link org.apache.commons.math4.util.Incrementor.MaxCountExceededCallback callback}
+     * {@link org.apache.commons.math4.util.IntegerSequence.Incrementor.MaxCountExceededCallback callback}
      * has been set at construction of the {@link IterationManager}
      * @throws NonSelfAdjointOperatorException if {@link #getCheck()} is
      * {@code true}, and {@code a} is not self-adjoint
@@ -1132,7 +1132,7 @@ public class SymmLQ
      * have dimensions inconsistent with {@code a}.
      * @throws MaxCountExceededException at exhaustion of the iteration count,
      * unless a custom
-     * {@link org.apache.commons.math4.util.Incrementor.MaxCountExceededCallback callback}
+     * {@link org.apache.commons.math4.util.IntegerSequence.Incrementor.MaxCountExceededCallback callback}
      * has been set at construction of the {@link IterationManager}
      * @throws NonSelfAdjointOperatorException if {@link #getCheck()} is
      * {@code true}, and {@code a} or {@code m} is not self-adjoint


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

Posted by er...@apache.org.
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 22da2300b824b508047880be840c6e89efa871b1
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Tue Dec 24 12:32:15 2019 +0100

    MATH-1362: Use "IntegerSequence.Incrementor".
---
 .../fitting/leastsquares/GaussNewtonOptimizer.java | 10 ++--
 .../fitting/leastsquares/LeastSquaresAdapter.java  |  6 +--
 .../fitting/leastsquares/LeastSquaresFactory.java  |  6 +--
 .../leastsquares/LevenbergMarquardtOptimizer.java  | 12 ++---
 .../math4/optim/AbstractOptimizationProblem.java   | 18 +++++---
 .../apache/commons/math4/optim/BaseOptimizer.java  | 53 ++++++++++++++--------
 .../commons/math4/optim/OptimizationProblem.java   |  6 +--
 .../math4/optim/linear/SimplexSolverTest.java      |  2 +-
 .../math4/optim/univariate/BrentOptimizerTest.java |  2 +-
 9 files changed, 67 insertions(+), 48 deletions(-)

diff --git a/src/main/java/org/apache/commons/math4/fitting/leastsquares/GaussNewtonOptimizer.java b/src/main/java/org/apache/commons/math4/fitting/leastsquares/GaussNewtonOptimizer.java
index 8ef5c89..677c581 100644
--- a/src/main/java/org/apache/commons/math4/fitting/leastsquares/GaussNewtonOptimizer.java
+++ b/src/main/java/org/apache/commons/math4/fitting/leastsquares/GaussNewtonOptimizer.java
@@ -31,7 +31,7 @@ import org.apache.commons.math4.linear.RealVector;
 import org.apache.commons.math4.linear.SingularMatrixException;
 import org.apache.commons.math4.linear.SingularValueDecomposition;
 import org.apache.commons.math4.optim.ConvergenceChecker;
-import org.apache.commons.math4.util.Incrementor;
+import org.apache.commons.math4.util.IntegerSequence;
 import org.apache.commons.math4.util.Pair;
 
 /**
@@ -207,8 +207,8 @@ public class GaussNewtonOptimizer implements LeastSquaresOptimizer {
     @Override
     public Optimum optimize(final LeastSquaresProblem lsp) {
         //create local evaluation and iteration counts
-        final Incrementor evaluationCounter = lsp.getEvaluationCounter();
-        final Incrementor iterationCounter = lsp.getIterationCounter();
+        final IntegerSequence.Incrementor evaluationCounter = lsp.getEvaluationCounter();
+        final IntegerSequence.Incrementor iterationCounter = lsp.getIterationCounter();
         final ConvergenceChecker<Evaluation> checker
                 = lsp.getConvergenceChecker();
 
@@ -222,12 +222,12 @@ public class GaussNewtonOptimizer implements LeastSquaresOptimizer {
         // iterate until convergence is reached
         Evaluation current = null;
         while (true) {
-            iterationCounter.incrementCount();
+            iterationCounter.increment();
 
             // evaluate the objective function and its jacobian
             Evaluation previous = current;
             // Value of the objective function at "currentPoint".
-            evaluationCounter.incrementCount();
+            evaluationCounter.increment();
             current = lsp.evaluate(currentPoint);
             final RealVector currentResiduals = current.getResiduals();
             final RealMatrix weightedJacobian = current.getJacobian();
diff --git a/src/main/java/org/apache/commons/math4/fitting/leastsquares/LeastSquaresAdapter.java b/src/main/java/org/apache/commons/math4/fitting/leastsquares/LeastSquaresAdapter.java
index 94e6a90..af6043a 100644
--- a/src/main/java/org/apache/commons/math4/fitting/leastsquares/LeastSquaresAdapter.java
+++ b/src/main/java/org/apache/commons/math4/fitting/leastsquares/LeastSquaresAdapter.java
@@ -18,7 +18,7 @@ package org.apache.commons.math4.fitting.leastsquares;
 
 import org.apache.commons.math4.linear.RealVector;
 import org.apache.commons.math4.optim.ConvergenceChecker;
-import org.apache.commons.math4.util.Incrementor;
+import org.apache.commons.math4.util.IntegerSequence;
 
 /**
  * An adapter that delegates to another implementation of {@link LeastSquaresProblem}.
@@ -66,13 +66,13 @@ public class LeastSquaresAdapter implements LeastSquaresProblem {
 
     /** {@inheritDoc} */
     @Override
-    public Incrementor getEvaluationCounter() {
+    public IntegerSequence.Incrementor getEvaluationCounter() {
         return problem.getEvaluationCounter();
     }
 
     /** {@inheritDoc} */
     @Override
-    public Incrementor getIterationCounter() {
+    public IntegerSequence.Incrementor getIterationCounter() {
         return problem.getIterationCounter();
     }
 
diff --git a/src/main/java/org/apache/commons/math4/fitting/leastsquares/LeastSquaresFactory.java b/src/main/java/org/apache/commons/math4/fitting/leastsquares/LeastSquaresFactory.java
index 8c0db8a..c4efc13 100644
--- a/src/main/java/org/apache/commons/math4/fitting/leastsquares/LeastSquaresFactory.java
+++ b/src/main/java/org/apache/commons/math4/fitting/leastsquares/LeastSquaresFactory.java
@@ -31,7 +31,7 @@ import org.apache.commons.math4.optim.AbstractOptimizationProblem;
 import org.apache.commons.math4.optim.ConvergenceChecker;
 import org.apache.commons.math4.optim.PointVectorValuePair;
 import org.apache.commons.math4.util.FastMath;
-import org.apache.commons.math4.util.Incrementor;
+import org.apache.commons.math4.util.IntegerSequence;
 import org.apache.commons.math4.util.Pair;
 
 /**
@@ -224,13 +224,13 @@ public class LeastSquaresFactory {
      * @return a least squares problem that tracks evaluations
      */
     public static LeastSquaresProblem countEvaluations(final LeastSquaresProblem problem,
-                                                       final Incrementor counter) {
+                                                       final IntegerSequence.Incrementor counter) {
         return new LeastSquaresAdapter(problem) {
 
             /** {@inheritDoc} */
             @Override
             public Evaluation evaluate(final RealVector point) {
-                counter.incrementCount();
+                counter.increment();
                 return super.evaluate(point);
             }
 
diff --git a/src/main/java/org/apache/commons/math4/fitting/leastsquares/LevenbergMarquardtOptimizer.java b/src/main/java/org/apache/commons/math4/fitting/leastsquares/LevenbergMarquardtOptimizer.java
index 1cf0c58..8c667c4 100644
--- a/src/main/java/org/apache/commons/math4/fitting/leastsquares/LevenbergMarquardtOptimizer.java
+++ b/src/main/java/org/apache/commons/math4/fitting/leastsquares/LevenbergMarquardtOptimizer.java
@@ -25,7 +25,7 @@ import org.apache.commons.math4.linear.ArrayRealVector;
 import org.apache.commons.math4.linear.RealMatrix;
 import org.apache.commons.math4.optim.ConvergenceChecker;
 import org.apache.commons.math4.util.FastMath;
-import org.apache.commons.math4.util.Incrementor;
+import org.apache.commons.math4.util.IntegerSequence;
 import org.apache.commons.numbers.core.Precision;
 
 
@@ -299,8 +299,8 @@ public class LevenbergMarquardtOptimizer implements LeastSquaresOptimizer {
         final int nR = problem.getObservationSize(); // Number of observed data.
         final int nC = problem.getParameterSize(); // Number of parameters.
         // Counters.
-        final Incrementor iterationCounter = problem.getIterationCounter();
-        final Incrementor evaluationCounter = problem.getEvaluationCounter();
+        final IntegerSequence.Incrementor iterationCounter = problem.getIterationCounter();
+        final IntegerSequence.Incrementor evaluationCounter = problem.getEvaluationCounter();
         // Convergence criterion.
         final ConvergenceChecker<Evaluation> checker = problem.getConvergenceChecker();
 
@@ -324,7 +324,7 @@ public class LevenbergMarquardtOptimizer implements LeastSquaresOptimizer {
 
 
         // Evaluate the function at the starting point and calculate its norm.
-        evaluationCounter.incrementCount();
+        evaluationCounter.increment();
         //value will be reassigned in the loop
         Evaluation current = problem.evaluate(problem.getStart());
         double[] currentResiduals = current.getResiduals().toArray();
@@ -334,7 +334,7 @@ public class LevenbergMarquardtOptimizer implements LeastSquaresOptimizer {
         // Outer loop.
         boolean firstIteration = true;
         while (true) {
-            iterationCounter.incrementCount();
+            iterationCounter.increment();
 
             final Evaluation previous = current;
 
@@ -443,7 +443,7 @@ public class LevenbergMarquardtOptimizer implements LeastSquaresOptimizer {
                 }
 
                 // Evaluate the function at x + p and calculate its norm.
-                evaluationCounter.incrementCount();
+                evaluationCounter.increment();
                 current = problem.evaluate(new ArrayRealVector(currentPoint));
                 currentResiduals = current.getResiduals().toArray();
                 currentCost = current.getCost();
diff --git a/src/main/java/org/apache/commons/math4/optim/AbstractOptimizationProblem.java b/src/main/java/org/apache/commons/math4/optim/AbstractOptimizationProblem.java
index 488e4f7..0e079f3 100644
--- a/src/main/java/org/apache/commons/math4/optim/AbstractOptimizationProblem.java
+++ b/src/main/java/org/apache/commons/math4/optim/AbstractOptimizationProblem.java
@@ -18,7 +18,7 @@ package org.apache.commons.math4.optim;
 
 import org.apache.commons.math4.exception.TooManyEvaluationsException;
 import org.apache.commons.math4.exception.TooManyIterationsException;
-import org.apache.commons.math4.util.Incrementor;
+import org.apache.commons.math4.util.IntegerSequence;
 
 /**
  * Base class for implementing optimization problems. It contains the boiler-plate code
@@ -60,14 +60,18 @@ public abstract class AbstractOptimizationProblem<PAIR>
 
     /** {@inheritDoc} */
     @Override
-    public Incrementor getEvaluationCounter() {
-        return new Incrementor(this.maxEvaluations, MAX_EVAL_CALLBACK);
+    public IntegerSequence.Incrementor getEvaluationCounter() {
+        return IntegerSequence.Incrementor.create()
+            .withMaximalCount(maxEvaluations)
+            .withCallback(MAX_EVAL_CALLBACK);
     }
 
     /** {@inheritDoc} */
     @Override
-    public Incrementor getIterationCounter() {
-        return new Incrementor(this.maxIterations, MAX_ITER_CALLBACK);
+    public IntegerSequence.Incrementor getIterationCounter() {
+        return IntegerSequence.Incrementor.create()
+            .withMaximalCount(maxIterations)
+            .withCallback(MAX_ITER_CALLBACK);
     }
 
     /** {@inheritDoc} */
@@ -78,7 +82,7 @@ public abstract class AbstractOptimizationProblem<PAIR>
 
     /** Defines the action to perform when reaching the maximum number of evaluations. */
     private static class MaxEvalCallback
-            implements Incrementor.MaxCountExceededCallback {
+        implements IntegerSequence.Incrementor.MaxCountExceededCallback {
         /**
          * {@inheritDoc}
          *
@@ -92,7 +96,7 @@ public abstract class AbstractOptimizationProblem<PAIR>
 
     /** Defines the action to perform when reaching the maximum number of evaluations. */
     private static class MaxIterCallback
-            implements Incrementor.MaxCountExceededCallback {
+        implements IntegerSequence.Incrementor.MaxCountExceededCallback {
         /**
          * {@inheritDoc}
          *
diff --git a/src/main/java/org/apache/commons/math4/optim/BaseOptimizer.java b/src/main/java/org/apache/commons/math4/optim/BaseOptimizer.java
index 1a3ae4c..323c4ef 100644
--- a/src/main/java/org/apache/commons/math4/optim/BaseOptimizer.java
+++ b/src/main/java/org/apache/commons/math4/optim/BaseOptimizer.java
@@ -18,7 +18,7 @@ package org.apache.commons.math4.optim;
 
 import org.apache.commons.math4.exception.TooManyEvaluationsException;
 import org.apache.commons.math4.exception.TooManyIterationsException;
-import org.apache.commons.math4.util.Incrementor;
+import org.apache.commons.math4.util.IntegerSequence;
 
 /**
  * Base class for implementing optimizers.
@@ -33,12 +33,21 @@ import org.apache.commons.math4.util.Incrementor;
  * @since 3.1
  */
 public abstract class BaseOptimizer<PAIR> {
-    /** Evaluations counter. */
-    protected final Incrementor evaluations;
-    /** Iterations counter. */
-    protected final Incrementor iterations;
+    /** Callback to use for the evaluation counter. */
+    private static final MaxEvalCallback MAX_EVAL_CALLBACK = new MaxEvalCallback();
+    /** Callback to use for the iteration counter. */
+    private static final MaxIterCallback MAX_ITER_CALLBACK = new MaxIterCallback();
+
     /** Convergence checker. */
     private final ConvergenceChecker<PAIR> checker;
+    /** Maximum number of evaluations. */
+    private int maxEvaluations;
+    /** Maximum number of iterations. */
+    private int maxIterations;
+    /** Evaluations counter. */
+    private IntegerSequence.Incrementor evaluations;
+    /** Iterations counter. */
+    private IntegerSequence.Incrementor iterations;
 
     /**
      * @param checker Convergence checker.
@@ -56,9 +65,8 @@ public abstract class BaseOptimizer<PAIR> {
                             int maxEval,
                             int maxIter) {
         this.checker = checker;
-
-        evaluations = new Incrementor(maxEval, new MaxEvalCallback());
-        iterations = new Incrementor(maxIter, new MaxIterCallback());
+        this.maxEvaluations = maxEval;
+        this.maxIterations = maxIter;
     }
 
     /**
@@ -145,10 +153,8 @@ public abstract class BaseOptimizer<PAIR> {
                TooManyIterationsException {
         // Parse options.
         parseOptimizationData(optData);
-
         // Reset counters.
-        evaluations.resetCount();
-        iterations.resetCount();
+        resetCounters();
         // Perform optimization.
         return doOptimize();
     }
@@ -166,8 +172,7 @@ public abstract class BaseOptimizer<PAIR> {
         throws TooManyEvaluationsException,
                TooManyIterationsException {
         // Reset counters.
-        evaluations.resetCount();
-        iterations.resetCount();
+        resetCounters();
         // Perform optimization.
         return doOptimize();
     }
@@ -188,7 +193,7 @@ public abstract class BaseOptimizer<PAIR> {
      */
     protected void incrementEvaluationCount()
         throws TooManyEvaluationsException {
-        evaluations.incrementCount();
+        evaluations.increment();
     }
 
     /**
@@ -199,7 +204,7 @@ public abstract class BaseOptimizer<PAIR> {
      */
     protected void incrementIterationCount()
         throws TooManyIterationsException {
-        iterations.incrementCount();
+        iterations.increment();
     }
 
     /**
@@ -218,22 +223,32 @@ public abstract class BaseOptimizer<PAIR> {
         // not provided in the argument list.
         for (OptimizationData data : optData) {
             if (data instanceof MaxEval) {
-                evaluations.setMaximalCount(((MaxEval) data).getMaxEval());
+                maxEvaluations = ((MaxEval) data).getMaxEval();
                 continue;
             }
             if (data instanceof MaxIter) {
-                iterations.setMaximalCount(((MaxIter) data).getMaxIter());
+                maxIterations = ((MaxIter) data).getMaxIter();
                 continue;
             }
         }
     }
 
+    /** Reset counters. */
+    private void resetCounters() {
+        evaluations = IntegerSequence.Incrementor.create()
+            .withMaximalCount(maxEvaluations)
+            .withCallback(MAX_EVAL_CALLBACK);
+        iterations = IntegerSequence.Incrementor.create()
+            .withMaximalCount(maxIterations)
+            .withCallback(MAX_ITER_CALLBACK);
+    }
+
     /**
      * Defines the action to perform when reaching the maximum number
      * of evaluations.
      */
     private static class MaxEvalCallback
-        implements  Incrementor.MaxCountExceededCallback {
+        implements IntegerSequence.Incrementor.MaxCountExceededCallback {
         /**
          * {@inheritDoc}
          * @throws TooManyEvaluationsException
@@ -249,7 +264,7 @@ public abstract class BaseOptimizer<PAIR> {
      * of evaluations.
      */
     private static class MaxIterCallback
-        implements Incrementor.MaxCountExceededCallback {
+        implements IntegerSequence.Incrementor.MaxCountExceededCallback {
         /**
          * {@inheritDoc}
          * @throws TooManyIterationsException
diff --git a/src/main/java/org/apache/commons/math4/optim/OptimizationProblem.java b/src/main/java/org/apache/commons/math4/optim/OptimizationProblem.java
index 35623b4..1bbec93 100644
--- a/src/main/java/org/apache/commons/math4/optim/OptimizationProblem.java
+++ b/src/main/java/org/apache/commons/math4/optim/OptimizationProblem.java
@@ -16,7 +16,7 @@
  */
 package org.apache.commons.math4.optim;
 
-import org.apache.commons.math4.util.Incrementor;
+import org.apache.commons.math4.util.IntegerSequence;
 
 /**
  * Common settings for all optimization problems. Includes divergence and convergence
@@ -34,7 +34,7 @@ public interface OptimizationProblem<PAIR> {
      *
      * @return a counter for the evaluations.
      */
-    Incrementor getEvaluationCounter();
+    IntegerSequence.Incrementor getEvaluationCounter();
 
     /**
      * Get a independent Incrementor that counts up to the maximum number of iterations
@@ -42,7 +42,7 @@ public interface OptimizationProblem<PAIR> {
      *
      * @return a counter for the evaluations.
      */
-    Incrementor getIterationCounter();
+    IntegerSequence.Incrementor getIterationCounter();
 
     /**
      * Gets the convergence checker.
diff --git a/src/test/java/org/apache/commons/math4/optim/linear/SimplexSolverTest.java b/src/test/java/org/apache/commons/math4/optim/linear/SimplexSolverTest.java
index c540652..ecac904 100644
--- a/src/test/java/org/apache/commons/math4/optim/linear/SimplexSolverTest.java
+++ b/src/test/java/org/apache/commons/math4/optim/linear/SimplexSolverTest.java
@@ -779,7 +779,7 @@ public class SimplexSolverTest {
         Assert.assertFalse(callback.isSolutionOptimal());
 
         try {
-            solver.optimize(new MaxIter(3), f, new LinearConstraintSet(constraints),
+            solver.optimize(new MaxIter(4), f, new LinearConstraintSet(constraints),
                             GoalType.MAXIMIZE, new NonNegativeConstraint(true), callback);
             Assert.fail("expected TooManyIterationsException");
         } catch (TooManyIterationsException ex) {
diff --git a/src/test/java/org/apache/commons/math4/optim/univariate/BrentOptimizerTest.java b/src/test/java/org/apache/commons/math4/optim/univariate/BrentOptimizerTest.java
index cb778a8..dde5a95 100644
--- a/src/test/java/org/apache/commons/math4/optim/univariate/BrentOptimizerTest.java
+++ b/src/test/java/org/apache/commons/math4/optim/univariate/BrentOptimizerTest.java
@@ -228,7 +228,7 @@ public final class BrentOptimizerTest {
             };
 
         UnivariateOptimizer optimizer = new BrentOptimizer(1e-10, 1e-8);
-        final double result = optimizer.optimize(new MaxEval(1483),
+        final double result = optimizer.optimize(new MaxEval(1484),
                                                  new UnivariateObjectiveFunction(f),
                                                  GoalType.MINIMIZE,
                                                  new SearchInterval(Double.MIN_VALUE,