MATH-1362: Use "IntegerSequence.Incrementor".

Also removed spurious "throws" clauses.
This commit is contained in:
Gilles Sadowski 2019-12-24 11:44:43 +01:00
parent 8bff9c35f3
commit aa46bcc64b
3 changed files with 16 additions and 29 deletions

View File

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

View File

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

View File

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