Repository: commons-math Updated Branches: refs/heads/master d746a54c2 -> a56d4998c
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. Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/a56d4998 Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/a56d4998 Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/a56d4998 Branch: refs/heads/master Commit: a56d4998cf16ff08f5593fb7d4dda66ca05dc269 Parents: d746a54 Author: Evan Ward <evan.w...@nrl.navy.mil> Authored: Thu Feb 19 15:16:28 2015 -0500 Committer: Evan Ward <evan.w...@nrl.navy.mil> Committed: Thu Feb 19 15:16:28 2015 -0500 ---------------------------------------------------------------------- .../math4/analysis/solvers/UnivariateSolverUtils.java | 2 +- .../math4/analysis/solvers/UnivariateSolverUtilsTest.java | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-math/blob/a56d4998/src/main/java/org/apache/commons/math4/analysis/solvers/UnivariateSolverUtils.java ---------------------------------------------------------------------- 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 2521c9b..49742d8 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; http://git-wip-us.apache.org/repos/asf/commons-math/blob/a56d4998/src/test/java/org/apache/commons/math4/analysis/solvers/UnivariateSolverUtilsTest.java ---------------------------------------------------------------------- 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 e1ff1f2..fba50e3 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();