Skip to content

Commit

Permalink
FIX: separate bracket and bounds parameters in minimize_scalar
Browse files Browse the repository at this point in the history
  • Loading branch information
dlax committed Apr 14, 2012
1 parent beb91c8 commit 65352cb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
27 changes: 17 additions & 10 deletions scipy/optimize/minimize.py
Expand Up @@ -369,8 +369,8 @@ def minimize(fun, x0, args=(), method='BFGS', jac=None, hess=None,
raise ValueError('Unknown solver %s' % method)


def minimize_scalar(fun, bs=None, args=(), method='brent',
options=dict(), full_output=False):
def minimize_scalar(fun, bracket=None, bounds=None, args=(),
method='brent', options=dict(), full_output=False):
"""
Minimization of scalar function of one variable.
Expand All @@ -379,15 +379,16 @@ def minimize_scalar(fun, bs=None, args=(), method='brent',
fun : callable
Objective function.
Scalar function, must return a scalar.
bs : sequence, optional
For methods 'brent' and 'golden', `bs` defines the bracketing
bracket : sequence, optional
For methods 'brent' and 'golden', `bracket` defines the bracketing
interval and can either have three items `(a, b, c)` so that `a < b
< c` and `fun(b) < fun(a), fun(c)` or two items `a` and `c` which
are assumed to be a starting interval for a downhill bracket search
(see `bracket`); it doesn't always mean that the obtained solution
will satisfy `a <= x <= c`.
For 'bounded' method `bs` is mandatory and must have two items `x1`
and `x2` corresponding to the optimization bounds.
bounds : sequence, optional
For method 'bounded', `bounds` is mandatory and must have two items
corresponding to the optimization bounds.
args : tuple, optional
Extra arguments passed to the objective function.
method : str, optional
Expand Down Expand Up @@ -468,18 +469,24 @@ def minimize_scalar(fun, bs=None, args=(), method='brent',
Using the *Bounded* method, we find a local minimum with specified
bounds as:
>>> xc = minimize_scalar(f, bs=(-3, -1), method='bounded')
>>> xc = minimize_scalar(f, bounds=(-3, -1), method='bounded')
>>> xc
-2.0000002026
"""
meth = method.lower()

if meth == 'brent':
return _minimize_scalar_brent(fun, bs, args, options, full_output)
return _minimize_scalar_brent(fun, bracket, args, options,
full_output)
elif meth == 'bounded':
return _minimize_scalar_bounded(fun, bs, args, options, full_output)
if bounds is None:
raise ValueError('The `bounds` parameter is mandatory for '
'method `bounded`.')
return _minimize_scalar_bounded(fun, bounds, args, options,
full_output)
elif meth == 'golden':
return _minimize_scalar_golden(fun, bs, args, options, full_output)
return _minimize_scalar_golden(fun, bracket, args, options,
full_output)
else:
raise ValueError('Unknown solver %s' % method)

Expand Down
18 changes: 9 additions & 9 deletions scipy/optimize/tests/test_optimize.py
Expand Up @@ -514,39 +514,39 @@ def test_minimize_scalar(self):
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')
x = optimize.minimize_scalar(self.fun, bracket = (-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),
x = optimize.minimize_scalar(self.fun, bracket=(-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, ),
x = optimize.minimize_scalar(self.fun, bounds=(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, ),
x = optimize.minimize_scalar(self.fun, bounds=(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])),
bounds=(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, ))
bounds=(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, ))
bounds=(np.zeros(2), 1), method='bounded', args=(1.5, ))

x = optimize.minimize_scalar(self.fun, bs=(1, np.array(5)),
x = optimize.minimize_scalar(self.fun, bounds=(1, np.array(5)),
method='bounded')
assert_allclose(x, self.solution, atol=1e-6)

Expand Down

0 comments on commit 65352cb

Please sign in to comment.