UnivariateSolverUtils { public static double[] bracket(UnivariateFunction function, double initial, double lowerBound, double upperBound) throws NullArgumentException, NotStrictlyPositiveException, NoBracketingException { return bracket(function, initial, lowerBound, upperBound, 1.0, 1.0, Integer.MAX_VALUE); } private  UnivariateSolverUtils(); static double solve(UnivariateFunction function, double x0, double x1); static double solve(UnivariateFunction function,
                               double x0, double x1,
                               double absoluteAccuracy); static double forceSide(final int maxEval, final UnivariateFunction f,
                                   final BracketedUnivariateSolver<UnivariateFunction> bracketing,
                                   final double baseRoot, final double min, final double max,
                                   final AllowedSolution allowedSolution); static double[] bracket(UnivariateFunction function,
                                   double initial,
                                   double lowerBound, double upperBound); static double[] bracket(UnivariateFunction function,
                                   double initial,
                                   double lowerBound, double upperBound,
                                   int maximumIterations); static double[] bracket(final UnivariateFunction function, final double initial,
                                   final double lowerBound, final double upperBound,
                                   final double q, final double r, final int maximumIterations); static double midpoint(double a, double b); static boolean isBracketing(UnivariateFunction function,
                                       final double lower,
                                       final double upper); static boolean isSequence(final double start,
                                     final double mid,
                                     final double end); static void verifyInterval(final double lower,
                                      final double upper); static void verifySequence(final double lower,
                                      final double initial,
                                      final double upper); static void verifyBracketing(UnivariateFunction function,
                                        final double lower,
                                        final double upper);  }
@Test public void testBracketSin() { double[] result = UnivariateSolverUtils.bracket(sin, 0.0, -2.0, 2.0); Assert.assertTrue(sin.value(result[0]) < 0); Assert.assertTrue(sin.value(result[1]) > 0); }
@Test public void testBracketCentered() { double initial = 0.1; double[] result = UnivariateSolverUtils.bracket(sin, initial, -2.0, 2.0, 0.2, 1.0, 100); Assert.assertTrue(result[0] < initial); Assert.assertTrue(result[1] > initial); Assert.assertTrue(sin.value(result[0]) < 0); Assert.assertTrue(sin.value(result[1]) > 0); }
@Test public void testBracketLow() { double initial = 0.5; double[] result = UnivariateSolverUtils.bracket(sin, initial, -2.0, 2.0, 0.2, 1.0, 100); Assert.assertTrue(result[0] < initial); Assert.assertTrue(result[1] < initial); Assert.assertTrue(sin.value(result[0]) < 0); Assert.assertTrue(sin.value(result[1]) > 0); }
@Test public void testBracketHigh(){ double initial = -0.5; double[] result = UnivariateSolverUtils.bracket(sin, initial, -2.0, 2.0, 0.2, 1.0, 100); Assert.assertTrue(result[0] > initial); Assert.assertTrue(result[1] > initial); Assert.assertTrue(sin.value(result[0]) < 0); Assert.assertTrue(sin.value(result[1]) > 0); }
@Test(expected=NoBracketingException.class) public void testBracketLinear(){ UnivariateSolverUtils.bracket(new UnivariateFunction() { public double value(double x) { return 1 - x; } }, 1000, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 1.0, 1.0, 100); }
@Test public void testBracketExponential(){ double[] result = UnivariateSolverUtils.bracket(new UnivariateFunction() { public double value(double x) { return 1 - x; } }, 1000, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 1.0, 2.0, 10); Assert.assertTrue(result[0] <= 1); Assert.assertTrue(result[1] >= 1); }
@Test public void testBracketEndpointRoot() { double[] result = UnivariateSolverUtils.bracket(sin, 1.5, 0, 2.0); Assert.assertEquals(0.0, sin.value(result[0]), 1.0e-15); Assert.assertTrue(sin.value(result[1]) > 0); }
@Test(expected=MathIllegalArgumentException.class) public void testNullFunction() { UnivariateSolverUtils.bracket(null, 1.5, 0, 2.0); }
@Test(expected=MathIllegalArgumentException.class) public void testBadInitial() { UnivariateSolverUtils.bracket(sin, 2.5, 0, 2.0); }
@Test(expected=MathIllegalArgumentException.class) public void testBadAdditive() { UnivariateSolverUtils.bracket(sin, 1.0, -2.0, 3.0, -1.0, 1.0, 100); }
@Test(expected=NoBracketingException.class) public void testIterationExceeded() { UnivariateSolverUtils.bracket(sin, 1.0, -2.0, 3.0, 1.0e-5, 1.0, 100); }
@Test(expected=MathIllegalArgumentException.class) public void testBadEndpoints() { UnivariateSolverUtils.bracket(sin, 1.5, 2.0, 1.0); }
@Test(expected=MathIllegalArgumentException.class) public void testBadMaximumIterations() { UnivariateSolverUtils.bracket(sin, 1.5, 0, 2.0, 0); }