Author: luc Date: Tue Feb 23 21:01:38 2010 New Revision: 915517 URL: http://svn.apache.org/viewvc?rev=915517&view=rev Log: Fixed a missing bracketing check of initial interval in Brent solver JIRA: MATH-343
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/BrentSolver.java commons/proper/math/trunk/src/site/xdoc/changes.xml commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/solvers/BrentSolverTest.java Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/BrentSolver.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/BrentSolver.java?rev=915517&r1=915516&r2=915517&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/BrentSolver.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/BrentSolver.java Tue Feb 23 21:01:38 2010 @@ -32,6 +32,11 @@ */ public class BrentSolver extends UnivariateRealSolverImpl { + /** Error message for non-bracketing interval. */ + private static final String NON_BRACKETING_MESSAGE = + "function values at endpoints do not have different signs. " + + "Endpoints: [{0}, {1}], Values: [{2}, {3}]"; + /** Serializable version identifier */ private static final long serialVersionUID = 7694577816772532779L; @@ -128,6 +133,11 @@ return solve(f, initial, yInitial, max, yMax, initial, yInitial); } + if (yMin * yMax > 0) { + throw MathRuntimeException.createIllegalArgumentException( + NON_BRACKETING_MESSAGE, min, max, yMin, yMax); + } + // full Brent algorithm starting with provided initial guess return solve(f, min, yMin, max, yMax, initial, yInitial); @@ -176,9 +186,7 @@ } else { // neither value is close to zero and min and max do not bracket root. throw MathRuntimeException.createIllegalArgumentException( - "function values at endpoints do not have different signs. " + - "Endpoints: [{0}, {1}], Values: [{2}, {3}]", - min, max, yMin, yMax); + NON_BRACKETING_MESSAGE, min, max, yMin, yMax); } } else if (sign < 0){ // solve using only the first endpoint as initial guess Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=915517&r1=915516&r2=915517&view=diff ============================================================================== --- commons/proper/math/trunk/src/site/xdoc/changes.xml (original) +++ commons/proper/math/trunk/src/site/xdoc/changes.xml Tue Feb 23 21:01:38 2010 @@ -39,6 +39,9 @@ </properties> <body> <release version="2.1" date="TBD" description="TBD"> + <action dev="luc" type="fix" issue="MATH-343" > + Fixed a missing bracketing check of initial interval in Brent solver. + </action> <action dev="dimpbx" type="fix" issue="MATH-342"> In SVD, the matrices passed to EigenDecomposition are now symmetric by construction (rather than simply by definition). In EigenDecomposition, Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/solvers/BrentSolverTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/solvers/BrentSolverTest.java?rev=915517&r1=915516&r2=915517&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/solvers/BrentSolverTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/solvers/BrentSolverTest.java Tue Feb 23 21:01:38 2010 @@ -329,6 +329,12 @@ } catch (IllegalArgumentException ex) { // expected } + try { // no bracket + solver.solve(f, 1, 1.5, 1.2); + fail("Expecting IllegalArgumentException - non-bracketing"); + } catch (IllegalArgumentException ex) { + // expected + } } public void testInitialGuess() throws MathException {