commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brentwor...@apache.org
Subject svn commit: r654100 - in /commons/proper/math/trunk/src: java/org/apache/commons/math/analysis/BrentSolver.java site/xdoc/changes.xml test/org/apache/commons/math/analysis/BrentSolverTest.java
Date Wed, 07 May 2008 13:36:34 GMT
Author: brentworden
Date: Wed May  7 06:36:33 2008
New Revision: 654100

URL: http://svn.apache.org/viewvc?rev=654100&view=rev
Log:
MATH-204.  added root checks for the endpoints.

Modified:
    commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/BrentSolver.java
    commons/proper/math/trunk/src/site/xdoc/changes.xml
    commons/proper/math/trunk/src/test/org/apache/commons/math/analysis/BrentSolverTest.java

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/BrentSolver.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/BrentSolver.java?rev=654100&r1=654099&r2=654100&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/BrentSolver.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/BrentSolver.java Wed
May  7 06:36:33 2008
@@ -128,20 +128,41 @@
         clearResult();
         verifyInterval(min, max);
         
+        double ret = Double.NaN;
+        
         double yMin = f.value(min);
         double yMax = f.value(max);
         
         // Verify bracketing
-        if (yMin * yMax >= 0) {
-            throw new IllegalArgumentException
-            ("Function values at endpoints do not have different signs." +
-                    "  Endpoints: [" + min + "," + max + "]" + 
-                    "  Values: [" + yMin + "," + yMax + "]");       
+        double sign = yMin * yMax;
+        if (sign > 0) {
+            // check if either value is close to a zero
+            if (Math.abs(yMin) <= functionValueAccuracy) {
+                setResult(min, 0);
+                ret = min;
+            } else if (Math.abs(yMax) <= functionValueAccuracy) {
+                setResult(max, 0);
+                ret = max;
+            } else {
+                // neither value is close to zero and min and max do not bracket root.
+                throw new IllegalArgumentException
+                ("Function values at endpoints do not have different signs." +
+                        "  Endpoints: [" + min + "," + max + "]" + 
+                        "  Values: [" + yMin + "," + yMax + "]");
+            }
+        } else if (sign < 0){
+            // solve using only the first endpoint as initial guess
+            ret = solve(min, yMin, max, yMax, min, yMin);
+        } else {
+            // either min or max is a root
+            if (yMin == 0.0) {
+                ret = min;
+            } else {
+                ret = max;
+            }
         }
 
-        // solve using only the first endpoint as initial guess
-        return solve(min, yMin, max, yMax, min, yMin);
-
+        return ret;
     }
         
     /**

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=654100&r1=654099&r2=654100&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Wed May  7 06:36:33 2008
@@ -59,6 +59,9 @@
       <action dev="brentworden" type="fix" issue="MATH-201" due-to="Peter Wyngaard">
         Fixed truncation error in t-test classes for large t values.
       </action>
+      <action dev="brentworden" type="fix" issue="MATH-204" due-to="Mick">
+        Added root checks for the endpoints.
+      </action>
     </release>
     <release version="1.2" date="2008-02-24"
     description="This release combines bug fixes and new features. Most notable

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/analysis/BrentSolverTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/analysis/BrentSolverTest.java?rev=654100&r1=654099&r2=654100&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/analysis/BrentSolverTest.java
(original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/analysis/BrentSolverTest.java
Wed May  7 06:36:33 2008
@@ -268,6 +268,18 @@
         assertEquals(result, 1.0, 1E-6);
     }
     
+    public void testRootEndpoints() throws Exception {
+        UnivariateRealFunction f = new SinFunction();
+        UnivariateRealSolver solver = new BrentSolver(f);
+        
+        // endpoint is root
+        double result = solver.solve(Math.PI, 4);
+        assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
+
+        result = solver.solve(3, Math.PI);
+        assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
+    }
+    
     public void testBadEndpoints() throws Exception {
         UnivariateRealFunction f = new SinFunction();
         UnivariateRealSolver solver = new BrentSolver(f);



Mime
View raw message