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.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();
}
/**

View File

@ -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();
}
}

View File

@ -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()) {

View File

@ -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;

View File

@ -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;

View File

@ -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();
}
}

View File

@ -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);
}