From 5828c2352d5759f7d44dbc29e5a4f597b41a398c Mon Sep 17 00:00:00 2001 From: Gilles Sadowski Date: Mon, 23 Dec 2019 12:33:33 +0100 Subject: [PATCH] MATH-1362: Use "IntegerSequence.Incrementor". --- .../BaseAbstractUnivariateIntegrator.java | 29 +++++++++---------- .../IterativeLegendreGaussIntegrator.java | 2 +- .../integration/MidPointIntegrator.java | 2 +- .../integration/RombergIntegrator.java | 4 +-- .../integration/SimpsonIntegrator.java | 2 +- .../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 595f18f3d..3cb31b63e 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 f65334c9f..9701c216e 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 edcb3007c..08cc3cc88 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 923de73f7..6205caa57 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 0ede25e94..19e61086e 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 893d602d0..2ccfed4e8 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 2f2b467e3..95f27ba4e 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); }