Permalink
Browse files

TST: tests for minimize_scalar

  • Loading branch information...
1 parent e528a96 commit 11ef6672d1c4db56b1f3ed82f902d94b516df8db Denis Laxalde committed Mar 15, 2012
Showing with 43 additions and 2 deletions.
  1. +43 −2 scipy/optimize/tests/test_optimize.py
@@ -471,9 +471,9 @@ class TestOptimizeScalar(TestCase):
def setUp(self):
self.solution = 1.5
- def fun(self, x):
+ def fun(self, x, a=1.5):
"""Objective function"""
- return (x - 1.5)**2 - 0.8
+ return (x - a)**2 - 0.8
def test_brent(self):
""" brent algorithm """
@@ -509,6 +509,47 @@ def test_fminbound_scalar(self):
assert_allclose(x, self.solution, atol=1e-6)
+ def test_minimize_scalar(self):
+ # combine all tests above for the minimize_scalar wrapper
+ x = optimize.minimize_scalar(self.fun)
+ assert_allclose(x, self.solution, atol=1e-6)
+
+ x = optimize.minimize_scalar(self.fun, bs = (-3, -2), args=(1.5, ),
+ method='Brent')
+ assert_allclose(x, self.solution, atol=1e-6)
+
+ x = optimize.minimize_scalar(self.fun, method='Brent',
+ args=(1.5, ), full_output=True)[0]
+ assert_allclose(x, self.solution, atol=1e-6)
+
+ x = optimize.minimize_scalar(self.fun, bs = (-15, -1, 15),
+ args=(1.5, ), method='Brent')
+ assert_allclose(x, self.solution, atol=1e-6)
+
+ x = optimize.minimize_scalar(self.fun, bs=(0, 1), args=(1.5, ),
+ method='Bounded')
+ assert_allclose(x, 1, atol=1e-4)
+
+ x = optimize.minimize_scalar(self.fun, bs=(1, 5), args=(1.5, ),
+ method='bounded')
+ assert_allclose(x, self.solution, atol=1e-6)
+
+ x = optimize.minimize_scalar(self.fun,
+ bs=(np.array([1]), np.array([5])),
+ args=(np.array([1.5]), ),
+ method='bounded')
+ assert_allclose(x, self.solution, atol=1e-6)
+
+ assert_raises(ValueError, optimize.minimize_scalar, self.fun,
+ bs=(5, 1), method='bounded', args=(1.5, ))
+
+ assert_raises(ValueError, optimize.minimize_scalar, self.fun,
+ bs=(np.zeros(2), 1), method='bounded', args=(1.5, ))
+
+ x = optimize.minimize_scalar(self.fun, bs=(1, np.array(5)),
+ method='bounded')
+ assert_allclose(x, self.solution, atol=1e-6)
+
class TestTnc(TestCase):
"""TNC non-linear optimization.

0 comments on commit 11ef667

Please sign in to comment.