Java Code Examples for org.apache.commons.math3.exception.util.LocalizedFormats#FUNCTION

The following examples show how to use org.apache.commons.math3.exception.util.LocalizedFormats#FUNCTION . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnivariateSolverUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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));
    }
}
 
Example 2
Source File: UnivariateSolverUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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));
    }
}
 
Example 3
Source File: UnivariateSolverUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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));
    }
}
 
Example 4
Source File: UnivariateSolverUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 function has the same sign at the
 * endpoints.
 */
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));
    }
}
 
Example 5
Source File: UnivariateSolverUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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));
    }
}
 
Example 6
Source File: UnivariateSolverUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This method attempts to find two values a and b satisfying <ul>
 * <li> <code> lowerBound <= a < initial < b <= upperBound</code> </li>
 * <li> <code> f(a) * f(b) <= 0 </code> </li>
 * </ul>
 * If f is continuous on <code>[a,b],</code> this means that <code>a</code>
 * and <code>b</code> bracket a root of f.
 * <p>
 * The algorithm starts by setting
 * <code>a := initial -1; b := initial +1,</code> examines the value of the
 * function at <code>a</code> and <code>b</code> and keeps moving
 * the endpoints out by one unit each time through a loop that terminates
 * when one of the following happens: <ul>
 * <li> <code> f(a) * f(b) <= 0 </code> --  success!</li>
 * <li> <code> a = lower </code> and <code> b = upper</code>
 * -- NoBracketingException </li>
 * <li> <code> maximumIterations</code> iterations elapse
 * -- NoBracketingException </li></ul></p>
 *
 * @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 IllegalArgumentException if function is null, maximumIterations
 * is not positive, or initial is not between lowerBound and upperBound.
 */
public static double[] bracket(UnivariateFunction function,
                               double initial,
                               double lowerBound, double upperBound,
                               int maximumIterations)  {
    if (function == null) {
        throw new NullArgumentException(LocalizedFormats.FUNCTION);
    }
    if (maximumIterations <= 0)  {
        throw new NotStrictlyPositiveException(LocalizedFormats.INVALID_MAX_ITERATIONS, maximumIterations);
    }
    verifySequence(lowerBound, initial, upperBound);

    double a = initial;
    double b = initial;
    double fa;
    double fb;
    int numIterations = 0;

    do {
        a = FastMath.max(a - 1.0, lowerBound);
        b = FastMath.min(b + 1.0, upperBound);
        fa = function.value(a);

        fb = function.value(b);
        ++numIterations;
    } while ((fa * fb > 0.0) && (numIterations < maximumIterations) &&
            ((a > lowerBound) || (b < upperBound)));

    if (fa * fb > 0.0) {
        throw new NoBracketingException(LocalizedFormats.FAILED_BRACKETING,
                                        a, b, fa, fb,
                                        numIterations, maximumIterations, initial,
                                        lowerBound, upperBound);
    }

    return new double[] {a, b};
}
 
Example 7
Source File: UnivariateSolverUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 function has the same sign at the
 * endpoints.
 */
public static void verifyBracketing(UnivariateFunction function,
                                    final double lower,
                                    final double upper) {
    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));
    }
}
 
Example 8
Source File: UnivariateSolverUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check whether the interval bounds bracket a root. That is, if the
 * values at the endpoints are not equal to zero, then the function takes
 * opposite signs at the endpoints.
 *
 * @param function Function.
 * @param lower Lower endpoint.
 * @param upper Upper endpoint.
 * @return {@code true} if the function values have opposite signs at the
 * given points.
 */
public static boolean isBracketing(UnivariateFunction function,
                                   final double lower,
                                   final double upper)
    throws NullArgumentException {
    if (function == null) {
        throw new NullArgumentException(LocalizedFormats.FUNCTION);
    }
    final double fLo = function.value(lower);
    final double fHi = function.value(upper);
    return (fLo >= 0 && fHi <= 0) || (fLo <= 0 && fHi >= 0);
}
 
Example 9
Source File: UnivariateSolverUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 function has the same sign at the
 * endpoints.
 */
public static void verifyBracketing(UnivariateFunction function,
                                    final double lower,
                                    final double upper) {
    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));
    }
}
 
Example 10
Source File: UnivariateSolverUtils.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * This method attempts to find two values a and b satisfying <ul>
 * <li> <code> lowerBound <= a < initial < b <= upperBound</code> </li>
 * <li> <code> f(a) * f(b) <= 0 </code> </li>
 * </ul>
 * If f is continuous on <code>[a,b],</code> this means that <code>a</code>
 * and <code>b</code> bracket a root of f.
 * <p>
 * The algorithm starts by setting
 * <code>a := initial -1; b := initial +1,</code> examines the value of the
 * function at <code>a</code> and <code>b</code> and keeps moving
 * the endpoints out by one unit each time through a loop that terminates
 * when one of the following happens: <ul>
 * <li> <code> f(a) * f(b) <= 0 </code> --  success!</li>
 * <li> <code> a = lower </code> and <code> b = upper</code>
 * -- NoBracketingException </li>
 * <li> <code> maximumIterations</code> iterations elapse
 * -- NoBracketingException </li></ul></p>
 *
 * @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 {
    if (function == null) {
        throw new NullArgumentException(LocalizedFormats.FUNCTION);
    }
    if (maximumIterations <= 0)  {
        throw new NotStrictlyPositiveException(LocalizedFormats.INVALID_MAX_ITERATIONS, maximumIterations);
    }
    verifySequence(lowerBound, initial, upperBound);

    double a = initial;
    double b = initial;
    double fa;
    double fb;
    int numIterations = 0;

    do {
        a = FastMath.max(a - 1.0, lowerBound);
        b = FastMath.min(b + 1.0, upperBound);
        fa = function.value(a);

        fb = function.value(b);
        ++numIterations;
    } while ((fa * fb > 0.0) && (numIterations < maximumIterations) &&
            ((a > lowerBound) || (b < upperBound)));

    if (fa * fb > 0.0) {
        throw new NoBracketingException(LocalizedFormats.FAILED_BRACKETING,
                                        a, b, fa, fb,
                                        numIterations, maximumIterations, initial,
                                        lowerBound, upperBound);
    }

    return new double[] {a, b};
}
 
Example 11
Source File: UnivariateSolverUtils.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * This method attempts to find two values a and b satisfying <ul>
 * <li> <code> lowerBound <= a < initial < b <= upperBound</code> </li>
 * <li> <code> f(a) * f(b) <= 0 </code> </li>
 * </ul>
 * If f is continuous on <code>[a,b],</code> this means that <code>a</code>
 * and <code>b</code> bracket a root of f.
 * <p>
 * The algorithm starts by setting
 * <code>a := initial -1; b := initial +1,</code> examines the value of the
 * function at <code>a</code> and <code>b</code> and keeps moving
 * the endpoints out by one unit each time through a loop that terminates
 * when one of the following happens: <ul>
 * <li> <code> f(a) * f(b) <= 0 </code> --  success!</li>
 * <li> <code> a = lower </code> and <code> b = upper</code>
 * -- NoBracketingException </li>
 * <li> <code> maximumIterations</code> iterations elapse
 * -- NoBracketingException </li></ul></p>
 *
 * @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 {
    if (function == null) {
        throw new NullArgumentException(LocalizedFormats.FUNCTION);
    }
    if (maximumIterations <= 0)  {
        throw new NotStrictlyPositiveException(LocalizedFormats.INVALID_MAX_ITERATIONS, maximumIterations);
    }
    verifySequence(lowerBound, initial, upperBound);

    double a = initial;
    double b = initial;
    double fa;
    double fb;
    int numIterations = 0;

    do {
        a = FastMath.max(a - 1.0, lowerBound);
        b = FastMath.min(b + 1.0, upperBound);
        fa = function.value(a);

        fb = function.value(b);
        ++numIterations;
    } while ((fa * fb > 0.0) && (numIterations < maximumIterations) &&
            ((a > lowerBound) || (b < upperBound)));

    if (fa * fb > 0.0) {
        throw new NoBracketingException(LocalizedFormats.FAILED_BRACKETING,
                                        a, b, fa, fb,
                                        numIterations, maximumIterations, initial,
                                        lowerBound, upperBound);
    }

    return new double[] {a, b};
}
 
Example 12
Source File: UnivariateSolverUtils.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * This method attempts to find two values a and b satisfying <ul>
 * <li> <code> lowerBound <= a < initial < b <= upperBound</code> </li>
 * <li> <code> f(a) * f(b) <= 0 </code> </li>
 * </ul>
 * If f is continuous on <code>[a,b],</code> this means that <code>a</code>
 * and <code>b</code> bracket a root of f.
 * <p>
 * The algorithm starts by setting
 * <code>a := initial -1; b := initial +1,</code> examines the value of the
 * function at <code>a</code> and <code>b</code> and keeps moving
 * the endpoints out by one unit each time through a loop that terminates
 * when one of the following happens: <ul>
 * <li> <code> f(a) * f(b) <= 0 </code> --  success!</li>
 * <li> <code> a = lower </code> and <code> b = upper</code>
 * -- NoBracketingException </li>
 * <li> <code> maximumIterations</code> iterations elapse
 * -- NoBracketingException </li></ul></p>
 *
 * @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 {
    if (function == null) {
        throw new NullArgumentException(LocalizedFormats.FUNCTION);
    }
    if (maximumIterations <= 0)  {
        throw new NotStrictlyPositiveException(LocalizedFormats.INVALID_MAX_ITERATIONS, maximumIterations);
    }
    verifySequence(lowerBound, initial, upperBound);

    double a = initial;
    double b = initial;
    double fa;
    double fb;
    int numIterations = 0;

    do {
        a = FastMath.max(a - 1.0, lowerBound);
        b = FastMath.min(b + 1.0, upperBound);
        fa = function.value(a);

        fb = function.value(b);
        ++numIterations;
    } while ((fa * fb > 0.0) && (numIterations < maximumIterations) &&
            ((a > lowerBound) || (b < upperBound)));

    if (fa * fb > 0.0) {
        throw new NoBracketingException(LocalizedFormats.FAILED_BRACKETING,
                                        a, b, fa, fb,
                                        numIterations, maximumIterations, initial,
                                        lowerBound, upperBound);
    }

    return new double[] {a, b};
}
 
Example 13
Source File: UnivariateSolverUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Convenience method to find a zero of a univariate real function.  A default
 * solver is used.
 *
 * @param function Function.
 * @param x0 Lower bound for the interval.
 * @param x1 Upper bound for the interval.
 * @return a value where the function is zero.
 * @throws IllegalArgumentException if f is null or the endpoints do not
 * specify a valid interval.
 */
public static double solve(UnivariateFunction function, double x0, double x1) {
    if (function == null) {
        throw new NullArgumentException(LocalizedFormats.FUNCTION);
    }
    final UnivariateSolver solver = new BrentSolver();
    return solver.solve(Integer.MAX_VALUE, function, x0, x1);
}
 
Example 14
Source File: UnivariateSolverUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Convenience method to find a zero of a univariate real function.  A default
 * solver is used.
 *
 * @param function Function.
 * @param x0 Lower bound for the interval.
 * @param x1 Upper bound for the interval.
 * @param absoluteAccuracy Accuracy to be used by the solver.
 * @return a value where the function is zero.
 * @throws NoBracketingException if the function has the same sign at the
 * endpoints.
 * @throws NullArgumentException if {@code function} is {@code null}.
 */
public static double solve(UnivariateFunction function,
                           double x0, double x1,
                           double absoluteAccuracy)
    throws NullArgumentException,
           NoBracketingException {
    if (function == null) {
        throw new NullArgumentException(LocalizedFormats.FUNCTION);
    }
    final UnivariateSolver solver = new BrentSolver(absoluteAccuracy);
    return solver.solve(Integer.MAX_VALUE, function, x0, x1);
}
 
Example 15
Source File: UnivariateSolverUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Convenience method to find a zero of a univariate real function.  A default
 * solver is used.
 *
 * @param function Function.
 * @param x0 Lower bound for the interval.
 * @param x1 Upper bound for the interval.
 * @return a value where the function is zero.
 * @throws NoBracketingException if the function has the same sign at the
 * endpoints.
 * @throws NullArgumentException if {@code function} is {@code null}.
 */
public static double solve(UnivariateFunction function, double x0, double x1)
    throws NullArgumentException,
           NoBracketingException {
    if (function == null) {
        throw new NullArgumentException(LocalizedFormats.FUNCTION);
    }
    final UnivariateSolver solver = new BrentSolver();
    return solver.solve(Integer.MAX_VALUE, function, x0, x1);
}
 
Example 16
Source File: UnivariateSolverUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Convenience method to find a zero of a univariate real function.  A default
 * solver is used.
 *
 * @param function Function.
 * @param x0 Lower bound for the interval.
 * @param x1 Upper bound for the interval.
 * @return a value where the function is zero.
 * @throws NoBracketingException if the function has the same sign at the
 * endpoints.
 * @throws NullArgumentException if {@code function} is {@code null}.
 */
public static double solve(UnivariateFunction function, double x0, double x1)
    throws NullArgumentException,
           NoBracketingException {
    if (function == null) {
        throw new NullArgumentException(LocalizedFormats.FUNCTION);
    }
    final UnivariateSolver solver = new BrentSolver();
    return solver.solve(Integer.MAX_VALUE, function, x0, x1);
}
 
Example 17
Source File: UnivariateSolverUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Convenience method to find a zero of a univariate real function.  A default
 * solver is used.
 *
 * @param function Function.
 * @param x0 Lower bound for the interval.
 * @param x1 Upper bound for the interval.
 * @param absoluteAccuracy Accuracy to be used by the solver.
 * @return a value where the function is zero.
 * @throws IllegalArgumentException if {@code function} is {@code null},
 * the endpoints do not specify a valid interval, or the absolute accuracy
 * is not valid for the default solver.
 */
public static double solve(UnivariateFunction function,
                           double x0, double x1,
                           double absoluteAccuracy) {
    if (function == null) {
        throw new NullArgumentException(LocalizedFormats.FUNCTION);
    }
    final UnivariateSolver solver = new BrentSolver(absoluteAccuracy);
    return solver.solve(Integer.MAX_VALUE, function, x0, x1);
}
 
Example 18
Source File: UnivariateSolverUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Check whether the interval bounds bracket a root. That is, if the
 * values at the endpoints are not equal to zero, then the function takes
 * opposite signs at the endpoints.
 *
 * @param function Function.
 * @param lower Lower endpoint.
 * @param upper Upper endpoint.
 * @return {@code true} if the function values have opposite signs at the
 * given points.
 * @throws NullArgumentException if {@code function} is {@code null}.
 */
public static boolean isBracketing(UnivariateFunction function,
                                   final double lower,
                                   final double upper)
    throws NullArgumentException {
    if (function == null) {
        throw new NullArgumentException(LocalizedFormats.FUNCTION);
    }
    final double fLo = function.value(lower);
    final double fHi = function.value(upper);
    return (fLo >= 0 && fHi <= 0) || (fLo <= 0 && fHi >= 0);
}
 
Example 19
Source File: UnivariateSolverUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Convenience method to find a zero of a univariate real function.  A default
 * solver is used.
 *
 * @param function Function.
 * @param x0 Lower bound for the interval.
 * @param x1 Upper bound for the interval.
 * @param absoluteAccuracy Accuracy to be used by the solver.
 * @return a value where the function is zero.
 * @throws NoBracketingException if the function has the same sign at the
 * endpoints.
 * @throws NullArgumentException if {@code function} is {@code null}.
 */
public static double solve(UnivariateFunction function,
                           double x0, double x1,
                           double absoluteAccuracy)
    throws NullArgumentException,
           NoBracketingException {
    if (function == null) {
        throw new NullArgumentException(LocalizedFormats.FUNCTION);
    }
    final UnivariateSolver solver = new BrentSolver(absoluteAccuracy);
    return solver.solve(Integer.MAX_VALUE, function, x0, x1);
}
 
Example 20
Source File: UnivariateSolverUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Check whether the interval bounds bracket a root. That is, if the
 * values at the endpoints are not equal to zero, then the function takes
 * opposite signs at the endpoints.
 *
 * @param function Function.
 * @param lower Lower endpoint.
 * @param upper Upper endpoint.
 * @return {@code true} if the function values have opposite signs at the
 * given points.
 * @throws NullArgumentException if {@code function} is {@code null}.
 */
public static boolean isBracketing(UnivariateFunction function,
                                   final double lower,
                                   final double upper)
    throws NullArgumentException {
    if (function == null) {
        throw new NullArgumentException(LocalizedFormats.FUNCTION);
    }
    final double fLo = function.value(lower);
    final double fHi = function.value(upper);
    return (fLo >= 0 && fHi <= 0) || (fLo <= 0 && fHi >= 0);
}