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. * Prepare for computation.
* Subclasses must call this method if they override any of the * Subclasses must call this method if they the {@code integrate} method.
* {@code solve} methods.
* *
* @param maxEval Maximum number of evaluations. * @param maxEval Maximum number of evaluations.
* @param f the integrand function * @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.NullArgumentException;
import org.apache.commons.math4.exception.NumberIsTooLargeException; import org.apache.commons.math4.exception.NumberIsTooLargeException;
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;
/** /**
@ -51,7 +51,7 @@ public abstract class BaseAbstractUnivariateSolver<FUNC extends UnivariateFuncti
/** Relative accuracy. */ /** Relative accuracy. */
private final double relativeAccuracy; private final double relativeAccuracy;
/** Evaluations counter. */ /** Evaluations counter. */
private final Incrementor evaluations = new Incrementor(); private IntegerSequence.Incrementor evaluations;
/** Lower end of search interval. */ /** Lower end of search interval. */
private double searchMin; private double searchMin;
/** Higher end of search interval. */ /** Higher end of search interval. */
@ -158,8 +158,7 @@ public abstract class BaseAbstractUnivariateSolver<FUNC extends UnivariateFuncti
* @throws TooManyEvaluationsException if the maximal number of evaluations * @throws TooManyEvaluationsException if the maximal number of evaluations
* is exceeded. * is exceeded.
*/ */
protected double computeObjectiveValue(double point) protected double computeObjectiveValue(double point) {
throws TooManyEvaluationsException {
incrementEvaluationCount(); incrementEvaluationCount();
return function.value(point); return function.value(point);
} }
@ -179,8 +178,7 @@ public abstract class BaseAbstractUnivariateSolver<FUNC extends UnivariateFuncti
protected void setup(int maxEval, protected void setup(int maxEval,
FUNC f, FUNC f,
double min, double max, double min, double max,
double startValue) double startValue) {
throws NullArgumentException {
// Checks. // Checks.
MathUtils.checkNotNull(f); MathUtils.checkNotNull(f);
@ -189,15 +187,13 @@ public abstract class BaseAbstractUnivariateSolver<FUNC extends UnivariateFuncti
searchMax = max; searchMax = max;
searchStart = startValue; searchStart = startValue;
function = f; function = f;
evaluations.setMaximalCount(maxEval); evaluations = IntegerSequence.Incrementor.create()
evaluations.resetCount(); .withMaximalCount(maxEval);
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public double solve(int maxEval, FUNC f, double min, double max, double startValue) public double solve(int maxEval, FUNC f, double min, double max, double startValue) {
throws TooManyEvaluationsException,
NoBracketingException {
// Initialization. // Initialization.
setup(maxEval, f, min, max, startValue); setup(maxEval, f, min, max, startValue);
@ -213,9 +209,7 @@ public abstract class BaseAbstractUnivariateSolver<FUNC extends UnivariateFuncti
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public double solve(int maxEval, FUNC f, double startValue) public double solve(int maxEval, FUNC f, double startValue) {
throws TooManyEvaluationsException,
NoBracketingException {
return solve(maxEval, f, Double.NaN, Double.NaN, 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 * @throws NoBracketingException if the initial search interval does not bracket
* a root and the solver requires it. * a root and the solver requires it.
*/ */
protected abstract double doSolve() protected abstract double doSolve();
throws TooManyEvaluationsException, NoBracketingException;
/** /**
* Check whether the function takes opposite signs at the endpoints. * 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}. * @throws NumberIsTooLargeException if {@code lower >= upper}.
*/ */
protected void verifyInterval(final double lower, protected void verifyInterval(final double lower,
final double upper) final double upper) {
throws NumberIsTooLargeException {
UnivariateSolverUtils.verifyInterval(lower, upper); UnivariateSolverUtils.verifyInterval(lower, upper);
} }
@ -283,8 +275,7 @@ public abstract class BaseAbstractUnivariateSolver<FUNC extends UnivariateFuncti
*/ */
protected void verifySequence(final double lower, protected void verifySequence(final double lower,
final double initial, final double initial,
final double upper) final double upper) {
throws NumberIsTooLargeException {
UnivariateSolverUtils.verifySequence(lower, initial, upper); UnivariateSolverUtils.verifySequence(lower, initial, upper);
} }
@ -299,9 +290,7 @@ public abstract class BaseAbstractUnivariateSolver<FUNC extends UnivariateFuncti
* the endpoints. * the endpoints.
*/ */
protected void verifyBracketing(final double lower, protected void verifyBracketing(final double lower,
final double upper) final double upper) {
throws NullArgumentException,
NoBracketingException {
UnivariateSolverUtils.verifyBracketing(function, lower, 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 * @throws TooManyEvaluationsException when the allowed number of function
* evaluations has been exhausted. * evaluations has been exhausted.
*/ */
protected void incrementEvaluationCount() protected void incrementEvaluationCount() {
throws TooManyEvaluationsException {
try { try {
evaluations.incrementCount(); evaluations.increment();
} catch (MaxCountExceededException e) { } catch (MaxCountExceededException e) {
throw new TooManyEvaluationsException(e.getMax()); throw new TooManyEvaluationsException(e.getMax());
} }

View File

@ -264,7 +264,7 @@ public final class BrentSolverTest {
}; };
BrentSolver solver = new BrentSolver(); 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()); Assert.assertEquals(804.93558250, result, solver.getAbsoluteAccuracy());
} }
} }