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

scipy-svn at scipy.org scipy-svn at scipy.org
Sat Nov 29 07:41:36 EST 2008


Author: stefan
Date: 2008-11-29 06:41:23 -0600 (Sat, 29 Nov 2008)
New Revision: 5205

Modified:
   trunk/scipy/optimize/optimize.py
   trunk/scipy/optimize/tests/test_optimize.py
Log:
In `fminbound`, raise an error if non-scalar bounds are specified [patch by
Neil Muller].  Closes #544.

Modified: trunk/scipy/optimize/optimize.py
===================================================================
--- trunk/scipy/optimize/optimize.py	2008-11-29 10:53:10 UTC (rev 5204)
+++ trunk/scipy/optimize/optimize.py	2008-11-29 12:41:23 UTC (rev 5205)
@@ -1136,7 +1136,7 @@
 
       func : callable f(x,*args)
           Objective function to be minimized (must accept and return scalars).
-      x1, x2 : ndarray
+      x1, x2 : float or array scalar
           The optimization bounds.
       args : tuple
           Extra arguments passed to function.
@@ -1176,6 +1176,12 @@
 
 
     """
+    # Test bounds are of correct form
+    x1 = atleast_1d(x1)
+    x2 = atleast_1d(x2)
+    if len(x1) != 1 or len(x2) != 1:
+        raise ValueError, "Optimisation bounds must be scalars" \
+                " or length 1 arrays"
     if x1 > x2:
         raise ValueError, "The lower bound exceeds the upper bound."
 

Modified: trunk/scipy/optimize/tests/test_optimize.py
===================================================================
--- trunk/scipy/optimize/tests/test_optimize.py	2008-11-29 10:53:10 UTC (rev 5204)
+++ trunk/scipy/optimize/tests/test_optimize.py	2008-11-29 12:41:23 UTC (rev 5205)
@@ -147,8 +147,22 @@
         assert max((err1,err2,err3,err4)) < 1e-6
 
 
+    def test_fminbound(self):
+        """Test fminbound
+        """
+        x = optimize.fminbound(lambda x: (x - 1.5)**2 - 0.8, 0, 1)
+        assert abs(x - 1) < 1e-5
+        x = optimize.fminbound(lambda x: (x - 1.5)**2 - 0.8, 1, 5)
+        assert abs(x - 1.5) < 1e-6
+        x = optimize.fminbound(lambda x: (x - 1.5)**2 - 0.8,
+                               numpy.array([1]), numpy.array([5]))
+        assert abs(x - 1.5) < 1e-6
+        assert_raises(ValueError,
+                optimize.fminbound, lambda x: (x - 1.5)**2 - 0.8, 5, 1)
+        assert_raises(ValueError,
+                optimize.fminbound, lambda x: (x - 1.5)**2 - 0.8,
+                      np.zeros(2), 1)
 
-
 class TestTnc(TestCase):
     """TNC non-linear optimization.
 




More information about the Scipy-svn mailing list