From a56d4998cf16ff08f5593fb7d4dda66ca05dc269 Mon Sep 17 00:00:00 2001 From: Evan Ward Date: Thu, 19 Feb 2015 15:16:28 -0500 Subject: [PATCH] MATH-1204 bracket function gives up too early In UnivariateSolverUtils.bracket(...) the search ends prematurely if a = lowerBound, which ignores some roots in the interval. Fixed by changing the loop condition so the search continues while b < upperBound. Also added a test case. --- .../math4/analysis/solvers/UnivariateSolverUtils.java | 2 +- .../math4/analysis/solvers/UnivariateSolverUtilsTest.java | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) 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();