/** * This method simply calls {@link #bracket(UnivariateFunction, double, double, double, * double, double, int) bracket(function, initial, lowerBound, upperBound, q, r, maximumIterations)} * with {@code q} and {@code r} set to 1.0. * @param function Function. * @param initial Initial midpoint of interval being expanded to * bracket a root. * @param lowerBound Lower bound (a is never lower than this value). * @param upperBound Upper bound (b never is greater than this * value). * @param maximumIterations Maximum number of iterations to perform * @return a two element array holding a and b. * @throws NoBracketingException if the algorithm fails to find a and b * satisfying the desired conditions. * @throws NotStrictlyPositiveException if {@code maximumIterations <= 0}. * @throws NullArgumentException if {@code function} is {@code null}. */ public static double[] bracket(UnivariateFunction function, double initial, double lowerBound, double upperBound, int maximumIterations) throws NullArgumentException, NotStrictlyPositiveException, NoBracketingException { return bracket(function, initial, lowerBound, upperBound, 1.0, 1.0, maximumIterations); }
/** * Check that the endpoints specify an interval and the end points * bracket a root. * * @param function Function. * @param lower Lower endpoint. * @param upper Upper endpoint. * @throws NoBracketingException if the function has the same sign at the * endpoints. * @throws NullArgumentException if {@code function} is {@code null}. */ public static void verifyBracketing(UnivariateFunction function, final double lower, final double upper) throws NullArgumentException, NoBracketingException { if (function == null) { throw new NullArgumentException(LocalizedFormats.FUNCTION); } verifyInterval(lower, upper); if (!isBracketing(function, lower, upper)) { throw new NoBracketingException(lower, upper, function.value(lower), function.value(upper)); } } }
/** * Check whether the arguments form a (strictly) increasing sequence. * * @param start First number. * @param mid Second number. * @param end Third number. * @return {@code true} if the arguments form an increasing sequence. */ protected boolean isSequence(final double start, final double mid, final double end) { return UnivariateSolverUtils.isSequence(start, mid, end); }
/** * Check that the endpoints specify an interval. * * @param lower Lower endpoint. * @param upper Upper endpoint. * @throws NumberIsTooLargeException if {@code lower >= upper}. */ protected void verifyInterval(final double lower, final double upper) throws NumberIsTooLargeException { UnivariateSolverUtils.verifyInterval(lower, upper); }
/** * Find a zero near the midpoint of {@code min} and {@code max}. * * @param f Function to solve. * @param min Lower bound for the interval. * @param max Upper bound for the interval. * @param maxEval Maximum number of evaluations. * @return the value where the function is zero. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the maximum evaluation count is exceeded. * @throws org.apache.commons.math3.exception.NumberIsTooLargeException * if {@code min >= max}. */ @Override public double solve(int maxEval, final UnivariateDifferentiableFunction f, final double min, final double max) throws TooManyEvaluationsException { return super.solve(maxEval, f, UnivariateSolverUtils.midpoint(min, max)); }
double x = UnivariateSolverUtils.solve(toSolve, lowerBound, upperBound, getSolverAbsoluteAccuracy());
/** * Check that {@code lower < initial < upper}. * * @param lower Lower endpoint. * @param initial Initial value. * @param upper Upper endpoint. * @throws NumberIsTooLargeException if {@code lower >= initial} or * {@code initial >= upper}. */ protected void verifySequence(final double lower, final double initial, final double upper) throws NumberIsTooLargeException { UnivariateSolverUtils.verifySequence(lower, initial, upper); }
/** * Check whether the function takes opposite signs at the endpoints. * * @param lower Lower endpoint. * @param upper Upper endpoint. * @return {@code true} if the function values have opposite signs at the * given points. */ protected boolean isBracketing(final double lower, final double upper) { return UnivariateSolverUtils.isBracketing(function, lower, upper); }
/** * Check that the endpoints specify an interval and the function takes * opposite signs at the endpoints. * * @param lower Lower endpoint. * @param upper Upper endpoint. * @throws NullArgumentException if the function has not been set. * @throws NoBracketingException if the function has the same sign at * the endpoints. */ protected void verifyBracketing(final double lower, final double upper) throws NullArgumentException, NoBracketingException { UnivariateSolverUtils.verifyBracketing(function, lower, upper); }
new PegasusSolver(solver.getRelativeAccuracy(), solver.getAbsoluteAccuracy()); root = forward ? UnivariateSolverUtils.forceSide(remainingEval, f, bracketing, baseRoot, ta, tb, AllowedSolution.RIGHT_SIDE) : UnivariateSolverUtils.forceSide(remainingEval, f, bracketing, baseRoot, tb, ta, AllowedSolution.LEFT_SIDE);
/** * Check that {@code lower < initial < upper}. * * @param lower Lower endpoint. * @param initial Initial value. * @param upper Upper endpoint. * @throws NumberIsTooLargeException if {@code lower >= initial} or * {@code initial >= upper}. */ public static void verifySequence(final double lower, final double initial, final double upper) throws NumberIsTooLargeException { verifyInterval(lower, initial); verifyInterval(initial, upper); }
/** * Find a zero near the midpoint of {@code min} and {@code max}. * * @param f Function to solve. * @param min Lower bound for the interval. * @param max Upper bound for the interval. * @param maxEval Maximum number of evaluations. * @return the value where the function is zero. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the maximum evaluation count is exceeded. * @throws org.apache.commons.math3.exception.NumberIsTooLargeException * if {@code min >= max}. */ @Override public double solve(int maxEval, final DifferentiableUnivariateFunction f, final double min, final double max) throws TooManyEvaluationsException { return super.solve(maxEval, f, UnivariateSolverUtils.midpoint(min, max)); }
double x = UnivariateSolverUtils.solve(toSolve, lowerBound, upperBound,
throw new NotStrictlyPositiveException(LocalizedFormats.INVALID_MAX_ITERATIONS, maximumIterations); verifySequence(lowerBound, initial, upperBound);
/** * Check whether the function takes opposite signs at the endpoints. * * @param lower Lower endpoint. * @param upper Upper endpoint. * @return {@code true} if the function values have opposite signs at the * given points. */ protected boolean isBracketing(final double lower, final double upper) { return UnivariateSolverUtils.isBracketing(function, lower, upper); }
/** * Check that the endpoints specify an interval and the function takes * opposite signs at the endpoints. * * @param lower Lower endpoint. * @param upper Upper endpoint. * @throws NullArgumentException if the function has not been set. * @throws NoBracketingException if the function has the same sign at * the endpoints. */ protected void verifyBracketing(final double lower, final double upper) throws NullArgumentException, NoBracketingException { UnivariateSolverUtils.verifyBracketing(function, lower, upper); }
new PegasusSolver(solver.getRelativeAccuracy(), solver.getAbsoluteAccuracy()); root = forward ? UnivariateSolverUtils.forceSide(remainingEval, f, bracketing, baseRoot, ta, tb, AllowedSolution.RIGHT_SIDE) : UnivariateSolverUtils.forceSide(remainingEval, f, bracketing, baseRoot, tb, ta, AllowedSolution.LEFT_SIDE);
/** * Check that the endpoints specify an interval and the end points * bracket a root. * * @param function Function. * @param lower Lower endpoint. * @param upper Upper endpoint. * @throws NoBracketingException if the function has the same sign at the * endpoints. * @throws NullArgumentException if {@code function} is {@code null}. */ public static void verifyBracketing(UnivariateFunction function, final double lower, final double upper) throws NullArgumentException, NoBracketingException { if (function == null) { throw new NullArgumentException(LocalizedFormats.FUNCTION); } verifyInterval(lower, upper); if (!isBracketing(function, lower, upper)) { throw new NoBracketingException(lower, upper, function.value(lower), function.value(upper)); } } }
/** * Prepare for computation. * Subclasses must call this method if they override any of the * {@code solve} methods. * * @param maxEval Maximum number of evaluations. * @param f the integrand function * @param lower the min bound for the interval * @param upper the upper bound for the interval * @throws NullArgumentException if {@code f} is {@code null}. * @throws MathIllegalArgumentException if {@code min >= max}. */ protected void setup(final int maxEval, final UnivariateFunction f, final double lower, final double upper) throws NullArgumentException, MathIllegalArgumentException { // Checks. MathUtils.checkNotNull(f); UnivariateSolverUtils.verifyInterval(lower, upper); // Reset. min = lower; max = upper; function = f; evaluations = evaluations.withMaximalCount(maxEval).withStart(0); count = count.withStart(0); }