Skip to content

Commit

Permalink
In fminbound, raise an error if non-scalar bounds are specified [pa…
Browse files Browse the repository at this point in the history
…tch by

Neil Muller].  Closes scipy#544.
  • Loading branch information
stefanv committed Nov 29, 2008
1 parent 5f37449 commit e5f1108
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
8 changes: 7 additions & 1 deletion scipy/optimize/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ def fminbound(func, x1, x2, args=(), xtol=1e-5, maxfun=500,
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.
Expand Down Expand Up @@ -1176,6 +1176,12 @@ def fminbound(func, x1, x2, args=(), xtol=1e-5, maxfun=500,
"""
# 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."

Expand Down
16 changes: 15 additions & 1 deletion scipy/optimize/tests/test_optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,21 @@ def test_brent(self):
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.
Expand Down

0 comments on commit e5f1108

Please sign in to comment.