Permalink
Browse files

ENH: jac is either a bool or a callable in minimize

  • Loading branch information...
1 parent aa60bb6 commit 4ed75ba9f28d70936d04f1ee0df8d98a6e416e63 Denis Laxalde committed Mar 5, 2012
Showing with 17 additions and 12 deletions.
  1. +15 −10 scipy/optimize/minimize.py
  2. +1 −1 scipy/optimize/tests/test_optimize.py
  3. +1 −1 scipy/optimize/tests/test_slsqp.py
@@ -24,7 +24,7 @@
from cobyla import _minimize_cobyla
from slsqp import _minimize_slsqp
-def minimize(fun, x0, args=(), method='BFGS', jac=None, hess=None,
+def minimize(fun, x0, args=(), method='BFGS', jac=False, hess=None,
bounds=None, constraints=(),
options=dict(), full_output=False, callback=None,
retall=False):
@@ -44,11 +44,14 @@ def minimize(fun, x0, args=(), method='BFGS', jac=None, hess=None,
Type of solver. Should be one of:
{'Nelder-Mead', 'Powell', 'CG', 'BFGS', 'Newton-CG', 'Anneal',
'L-BFGS-B', 'TNC', 'COBYLA', 'SLSQP'}.
- jac : callable, optional
+ jac : bool or callable, optional
Jacobian of objective function. Only for CG, BFGS, Newton-CG.
- If None, Jacobian will be estimated numerically. If the `jac==fun`,
- `fun` is assumed to return the value of Jacobian along with the
- objective function.
+ If `jac` is a Boolean and is True, `fun` is assumed to return the
+ value of Jacobian along with the objective function. If False, the
+ Jacobian will be estimated numerically.
+ `jac` can also be a callable returning the Jacobian of the
+ objective. In this case, it must accept the same arguments as
+ `fun`.
hess : callable, optional
Hessian of objective function. Only for Newton-CG.
The function `hess` can either return the Hessian matrix of `fun`
@@ -287,8 +290,7 @@ def minimize(fun, x0, args=(), method='BFGS', jac=None, hess=None,
meth = method.lower()
# check if optional parameters are supported by the selected method
# - jac
- if meth in ['nelder-mead', 'powell', 'anneal', 'cobyla'] and \
- jac is not None:
+ if meth in ['nelder-mead', 'powell', 'anneal', 'cobyla'] and bool(jac):
warn('Method %s does not use gradient information (jac).' % method,
RuntimeWarning)
# - hess
@@ -318,9 +320,12 @@ def minimize(fun, x0, args=(), method='BFGS', jac=None, hess=None,
RuntimeWarning)
# fun also returns the jacobian
- if jac == fun:
- fun = MemoizeJac(fun)
- jac = fun.derivative
+ if not callable(jac):
+ if bool(jac):
+ fun = MemoizeJac(fun)
+ jac = fun.derivative
+ else:
+ jac = None
if meth == 'nelder-mead':
return _minimize_neldermead(fun, x0, args, options, full_output,
@@ -627,7 +627,7 @@ def test_minimize_tnc1c(self):
x0, bnds = [-2, 1], ([-np.inf, None],[-1.5, None])
xopt = [1, 1]
x = optimize.minimize(self.fg1, x0, method='TNC',
- jac=self.fg1, bounds=bnds,
+ jac=True, bounds=bnds,
options=self.opts)
assert_allclose(self.f1(x), self.f1(xopt), atol=1e-8)
@@ -96,7 +96,7 @@ def test_minimize_unbounded_combined(self):
Minimize, method='SLSQP': unbounded, combined function and jacobian.
"""
x, info = minimize(self.fun_and_jac, [-1.0, 1.0], args = (-1.0, ),
- jac=self.fun_and_jac, method='SLSQP',
+ jac=True, method='SLSQP',
options=self.opts, full_output=True)
assert_(info['success'], info['message'])
assert_allclose(x, [2, 1])

0 comments on commit 4ed75ba

Please sign in to comment.