MATH-1362: Use "IntegerSequence.Incrementor".

This commit is contained in:
Gilles Sadowski 2019-12-23 12:33:33 +01:00
parent bf5d19e08b
commit 5828c2352d
7 changed files with 21 additions and 24 deletions

View File

@ -24,7 +24,7 @@ import org.apache.commons.math4.exception.NotStrictlyPositiveException;
import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.exception.NullArgumentException;
import org.apache.commons.math4.exception.NumberIsTooSmallException; import org.apache.commons.math4.exception.NumberIsTooSmallException;
import org.apache.commons.math4.exception.TooManyEvaluationsException; 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; import org.apache.commons.math4.util.MathUtils;
/** /**
@ -46,8 +46,6 @@ public abstract class BaseAbstractUnivariateIntegrator implements UnivariateInte
/** Default maximal iteration count. */ /** Default maximal iteration count. */
public static final int DEFAULT_MAX_ITERATIONS_COUNT = Integer.MAX_VALUE; public static final int DEFAULT_MAX_ITERATIONS_COUNT = Integer.MAX_VALUE;
/** The iteration count. */
protected final Incrementor iterations;
/** Maximum absolute error. */ /** Maximum absolute error. */
private final double absoluteAccuracy; private final double absoluteAccuracy;
@ -57,9 +55,14 @@ public abstract class BaseAbstractUnivariateIntegrator implements UnivariateInte
/** minimum number of iterations */ /** minimum number of iterations */
private final int minimalIterationCount; private final int minimalIterationCount;
/** maximum number of iterations */
private final int maximalIterationCount;
/** The iteration count. */
protected IntegerSequence.Incrementor iterations;
/** The functions evaluation count. */ /** The functions evaluation count. */
private final Incrementor evaluations; private IntegerSequence.Incrementor evaluations;
/** Function to integrate. */ /** Function to integrate. */
private UnivariateFunction function; private UnivariateFunction function;
@ -123,12 +126,7 @@ public abstract class BaseAbstractUnivariateIntegrator implements UnivariateInte
throw new NumberIsTooSmallException(maximalIterationCount, minimalIterationCount, false); throw new NumberIsTooSmallException(maximalIterationCount, minimalIterationCount, false);
} }
this.minimalIterationCount = minimalIterationCount; this.minimalIterationCount = minimalIterationCount;
this.iterations = new Incrementor(); this.maximalIterationCount = maximalIterationCount;
iterations.setMaximalCount(maximalIterationCount);
// prepare evaluations counter, but do not set it yet
evaluations = new Incrementor();
} }
/** /**
@ -218,7 +216,7 @@ public abstract class BaseAbstractUnivariateIntegrator implements UnivariateInte
protected double computeObjectiveValue(final double point) protected double computeObjectiveValue(final double point)
throws TooManyEvaluationsException { throws TooManyEvaluationsException {
try { try {
evaluations.incrementCount(); evaluations.increment();
} catch (MaxCountExceededException e) { } catch (MaxCountExceededException e) {
throw new TooManyEvaluationsException(e.getMax()); throw new TooManyEvaluationsException(e.getMax());
} }
@ -250,10 +248,10 @@ public abstract class BaseAbstractUnivariateIntegrator implements UnivariateInte
min = lower; min = lower;
max = upper; max = upper;
function = f; function = f;
evaluations.setMaximalCount(maxEval); iterations = IntegerSequence.Incrementor.create()
evaluations.resetCount(); .withMaximalCount(maximalIterationCount);
iterations.resetCount(); evaluations = IntegerSequence.Incrementor.create()
.withMaximalCount(maxEval);
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@ -268,7 +266,6 @@ public abstract class BaseAbstractUnivariateIntegrator implements UnivariateInte
// Perform computation. // Perform computation.
return doIntegrate(); return doIntegrate();
} }
/** /**

View File

@ -142,7 +142,7 @@ public class IterativeLegendreGaussIntegrator
final double ratio = FastMath.min(4, FastMath.pow(delta / limit, 0.5 / numberOfPoints)); final double ratio = FastMath.min(4, FastMath.pow(delta / limit, 0.5 / numberOfPoints));
n = FastMath.max((int) (ratio * n), n + 1); n = FastMath.max((int) (ratio * n), n + 1);
oldt = t; oldt = t;
iterations.incrementCount(); iterations.increment();
} }
} }

View File

@ -150,7 +150,7 @@ public class MidPointIntegrator extends BaseAbstractUnivariateIntegrator {
double oldt = diff * computeObjectiveValue(midPoint); double oldt = diff * computeObjectiveValue(midPoint);
while (true) { while (true) {
iterations.incrementCount(); iterations.increment();
final int i = iterations.getCount(); final int i = iterations.getCount();
final double t = stage(i, oldt, min, diff); final double t = stage(i, oldt, min, diff);
if (i >= getMinimalIterationCount()) { if (i >= getMinimalIterationCount()) {

View File

@ -107,7 +107,7 @@ public class RombergIntegrator extends BaseAbstractUnivariateIntegrator {
TrapezoidIntegrator qtrap = new TrapezoidIntegrator(); TrapezoidIntegrator qtrap = new TrapezoidIntegrator();
currentRow[0] = qtrap.stage(this, 0); currentRow[0] = qtrap.stage(this, 0);
iterations.incrementCount(); iterations.increment();
double olds = currentRow[0]; double olds = currentRow[0];
while (true) { while (true) {
@ -119,7 +119,7 @@ public class RombergIntegrator extends BaseAbstractUnivariateIntegrator {
currentRow = tmpRow; currentRow = tmpRow;
currentRow[0] = qtrap.stage(this, i); currentRow[0] = qtrap.stage(this, i);
iterations.incrementCount(); iterations.increment();
for (int j = 1; j <= i; j++) { for (int j = 1; j <= i; j++) {
// Richardson extrapolation coefficient // Richardson extrapolation coefficient
final double r = (1L << (2 * j)) - 1; final double r = (1L << (2 * j)) - 1;

View File

@ -105,7 +105,7 @@ public class SimpsonIntegrator extends BaseAbstractUnivariateIntegrator {
double olds = (4 * oldt - s0) / 3.0; double olds = (4 * oldt - s0) / 3.0;
while (true) { while (true) {
// The first iteration is the first refinement of the sum. // The first iteration is the first refinement of the sum.
iterations.incrementCount(); iterations.increment();
final int i = getIterations(); final int i = getIterations();
final double t = qtrap.stage(this, i + 1); // 1-stage ahead of the iteration final double t = qtrap.stage(this, i + 1); // 1-stage ahead of the iteration
final double s = (4 * t - oldt) / 3.0; final double s = (4 * t - oldt) / 3.0;

View File

@ -147,7 +147,7 @@ public class TrapezoidIntegrator extends BaseAbstractUnivariateIntegrator {
throws MathIllegalArgumentException, TooManyEvaluationsException, MaxCountExceededException { throws MathIllegalArgumentException, TooManyEvaluationsException, MaxCountExceededException {
double oldt = stage(this, 0); double oldt = stage(this, 0);
iterations.incrementCount(); iterations.increment();
while (true) { while (true) {
final int i = iterations.getCount(); final int i = iterations.getCount();
final double t = stage(this, i); final double t = stage(this, i);
@ -160,7 +160,7 @@ public class TrapezoidIntegrator extends BaseAbstractUnivariateIntegrator {
} }
} }
oldt = t; oldt = t;
iterations.incrementCount(); iterations.increment();
} }
} }

View File

@ -117,7 +117,7 @@ public class IterativeLegendreGaussIntegratorTest {
final double a = -5000; final double a = -5000;
final double b = 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); Assert.assertEquals(1, s, 1e-5);
} }