[Scipy-svn] r6579 - in trunk/scipy/optimize: . tests

scipy-svn@scip... scipy-svn@scip...
Tue Jun 29 18:03:31 CDT 2010


Author: warren.weckesser
Date: 2010-06-29 18:03:30 -0500 (Tue, 29 Jun 2010)
New Revision: 6579

Modified:
   trunk/scipy/optimize/tests/test_regression.py
   trunk/scipy/optimize/zeros.py
Log:
BUG: Ticket #1214: optimize.newton could return an incorrect value if the function coefficients and initial guess were integers.

Modified: trunk/scipy/optimize/tests/test_regression.py
===================================================================
--- trunk/scipy/optimize/tests/test_regression.py	2010-06-29 16:06:45 UTC (rev 6578)
+++ trunk/scipy/optimize/tests/test_regression.py	2010-06-29 23:03:30 UTC (rev 6579)
@@ -1,14 +1,24 @@
 """Regression tests for optimize.
 
 """
-from numpy.testing import *
-import numpy as np
 
+from numpy.testing import TestCase, run_module_suite, assert_almost_equal
+import scipy.optimize
 
 class TestRegression(TestCase):
+    
     def test_newton_x0_is_0(self):
         """Ticket #1074"""
-        import scipy.optimize
+
         tgt = 1
         res = scipy.optimize.newton(lambda x: x - 1, 0)
         assert_almost_equal(res, tgt)
+
+    def test_newton_integers(self):
+        """Ticket #1214"""
+        root = scipy.optimize.newton(lambda x: x**2 - 1, x0=2,
+                                    fprime=lambda x: 2*x)
+        assert_almost_equal(root, 1.0)
+    
+if __name__ == "__main__":
+    run_module_suite()
\ No newline at end of file

Modified: trunk/scipy/optimize/zeros.py
===================================================================
--- trunk/scipy/optimize/zeros.py	2010-06-29 16:06:45 UTC (rev 6578)
+++ trunk/scipy/optimize/zeros.py	2010-06-29 23:03:30 UTC (rev 6579)
@@ -95,7 +95,9 @@
     """
     if fprime is not None:
         # Newton-Rapheson method
-        p0 = x0
+        # Multiply by 1.0 to convert to floating point.  We don't use float(x0)
+        # so it still works if x0 is complex.
+        p0 = 1.0 * x0
         for iter in range(maxiter):
             myargs = (p0,) + args
             fder = fprime(*myargs)



More information about the Scipy-svn mailing list