[Scipy-svn] r6582 - in branches/0.8.x/scipy/optimize: . tests

scipy-svn at scipy.org scipy-svn at scipy.org
Sat Jul 3 00:47:43 EDT 2010


Author: rgommers
Date: 2010-07-02 23:47:43 -0500 (Fri, 02 Jul 2010)
New Revision: 6582

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

Backport of r6579.

Modified: branches/0.8.x/scipy/optimize/tests/test_regression.py
===================================================================
--- branches/0.8.x/scipy/optimize/tests/test_regression.py	2010-07-03 04:19:02 UTC (rev 6581)
+++ branches/0.8.x/scipy/optimize/tests/test_regression.py	2010-07-03 04:47:43 UTC (rev 6582)
@@ -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: branches/0.8.x/scipy/optimize/zeros.py
===================================================================
--- branches/0.8.x/scipy/optimize/zeros.py	2010-07-03 04:19:02 UTC (rev 6581)
+++ branches/0.8.x/scipy/optimize/zeros.py	2010-07-03 04:47:43 UTC (rev 6582)
@@ -92,7 +92,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
             fval = func(*myargs)




More information about the Scipy-svn mailing list