diff --git a/src/main/java/org/apache/commons/math4/analysis/solvers/UnivariateSolverUtils.java b/src/main/java/org/apache/commons/math4/analysis/solvers/UnivariateSolverUtils.java index 2521c9b5a..49742d84b 100644 --- a/src/main/java/org/apache/commons/math4/analysis/solvers/UnivariateSolverUtils.java +++ b/src/main/java/org/apache/commons/math4/analysis/solvers/UnivariateSolverUtils.java @@ -314,7 +314,7 @@ public class UnivariateSolverUtils { double delta = 0; for (int numIterations = 0; - (numIterations < maximumIterations) && (a > lowerBound || b > upperBound); + (numIterations < maximumIterations) && (a > lowerBound || b < upperBound); ++numIterations) { final double previousA = a; diff --git a/src/test/java/org/apache/commons/math4/analysis/solvers/UnivariateSolverUtilsTest.java b/src/test/java/org/apache/commons/math4/analysis/solvers/UnivariateSolverUtilsTest.java index e1ff1f2d0..fba50e33b 100644 --- a/src/test/java/org/apache/commons/math4/analysis/solvers/UnivariateSolverUtilsTest.java +++ b/src/test/java/org/apache/commons/math4/analysis/solvers/UnivariateSolverUtilsTest.java @@ -176,6 +176,14 @@ public class UnivariateSolverUtilsTest { UnivariateSolverUtils.bracket(sin, 1.5, 0, 2.0, 0); } + /** check the search continues when a = lowerBound and b < upperBound. */ + @Test + public void testBracketLoopConditionForB() { + double[] result = UnivariateSolverUtils.bracket(sin, -0.9, -1, 1, 0.1, 1, 100); + Assert.assertTrue(result[0] <= 0); + Assert.assertTrue(result[1] >= 0); + } + @Test public void testMisc() { UnivariateFunction f = new QuinticFunction();