def fmin(func, x0, args=(), xtol=<NUM_LIT>, ftol=<NUM_LIT>, maxiter=None, maxfun=None,<EOL>full_output=<NUM_LIT:0>, disp=<NUM_LIT:1>, retall=<NUM_LIT:0>, callback=None):

opts = {'<STR_LIT>': xtol,<EOL>'<STR_LIT>': ftol,<EOL>'<STR_LIT>': maxiter,<EOL>'<STR_LIT>': maxfun,<EOL>'<STR_LIT>': disp,<EOL>'<STR_LIT>': retall}<EOL>res = _minimize_neldermead(func, x0, args, callback=callback, **opts)<EOL>if full_output:<EOL><INDENT>retlist = res['<STR_LIT:x>'], res['<STR_LIT>'], res['<STR_LIT>'], res['<STR_LIT>'], res['<STR_LIT:status>']<EOL>if retall:<EOL><INDENT>retlist += (res['<STR_LIT>'], )<EOL><DEDENT>return retlist<EOL><DEDENT>else:<EOL><INDENT>if retall:<EOL><INDENT>return res['<STR_LIT:x>'], res['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>return res['<STR_LIT:x>']<EOL><DEDENT><DEDENT>

Minimize a function using the downhill simplex algorithm.

This algorithm only uses function values, not derivatives or second
derivatives.

Parameters
----------
func : callable func(x,*args)
    The objective function to be minimized.
x0 : ndarray
    Initial guess.
args : tuple, optional
    Extra arguments passed to func, i.e. ``f(x,*args)``.
callback : callable, optional
    Called after each iteration, as callback(xk), where xk is the
    current parameter vector.
xtol : float, optional
    Relative error in xopt acceptable for convergence.
ftol : number, optional
    Relative error in func(xopt) acceptable for convergence.
maxiter : int, optional
    Maximum number of iterations to perform.
maxfun : number, optional
    Maximum number of function evaluations to make.
full_output : bool, optional
    Set to True if fopt and warnflag outputs are desired.
disp : bool, optional
    Set to True to print convergence messages.
retall : bool, optional
    Set to True to return list of solutions at each iteration.

Returns
-------
xopt : ndarray
    Parameter that minimizes function.
fopt : float
    Value of function at minimum: ``fopt = func(xopt)``.
iter : int
    Number of iterations performed.
funcalls : int
    Number of function calls made.
warnflag : int
    1 : Maximum number of function evaluations made.
    2 : Maximum number of iterations reached.
allvecs : list
    Solution at each iteration.

See also
--------
minimize: Interface to minimization algorithms for multivariate
    functions. See the 'Nelder-Mead' `method` in particular.

Notes
-----
Uses a Nelder-Mead simplex algorithm to find the minimum of function of
one or more variables.

This algorithm has a long history of successful use in applications.
But it will usually be slower than an algorithm that uses first or
second derivative information. In practice it can have poor
performance in high-dimensional problems and is not robust to
minimizing complicated functions. Additionally, there currently is no
complete theory describing when the algorithm will successfully
converge to the minimum, or how fast it will if it does.

References
----------
.. [1] Nelder, J.A. and Mead, R. (1965), "A simplex method for function
       minimization", The Computer Journal, 7, pp. 308-313

.. [2] Wright, M.H. (1996), "Direct Search Methods: Once Scorned, Now
       Respectable", in Numerical Analysis 1995, Proceedings of the
       1995 Dundee Biennial Conference in Numerical Analysis, D.F.
       Griffiths and G.A. Watson (Eds.), Addison Wesley Longman,
       Harlow, UK, pp. 191-208.


def _minimize_neldermead(func, x0, args=(), callback=None,<EOL>xtol=<NUM_LIT>, ftol=<NUM_LIT>, maxiter=None, maxfev=None,<EOL>disp=False, return_all=False,<EOL>**unknown_options):

_check_unknown_options(unknown_options)<EOL>maxfun = maxfev<EOL>retall = return_all<EOL>fcalls, func = wrap_function(func, args)<EOL>x0 = asfarray(x0).flatten()<EOL>N = len(x0)<EOL>if maxiter is None:<EOL><INDENT>maxiter = N * <NUM_LIT:200><EOL><DEDENT>if maxfun is None:<EOL><INDENT>maxfun = N * <NUM_LIT:200><EOL><DEDENT>rho = <NUM_LIT:1><EOL>chi = <NUM_LIT:2><EOL>psi = <NUM_LIT:0.5><EOL>sigma = <NUM_LIT:0.5><EOL>one2np1 = list(range(<NUM_LIT:1>, N + <NUM_LIT:1>))<EOL>sim = numpy.zeros((N + <NUM_LIT:1>, N), dtype=x0.dtype)<EOL>fsim = numpy.zeros((N + <NUM_LIT:1>,), float)<EOL>sim[<NUM_LIT:0>] = x0<EOL>if retall:<EOL><INDENT>allvecs = [sim[<NUM_LIT:0>]]<EOL><DEDENT>fsim[<NUM_LIT:0>] = func(x0)<EOL>nonzdelt = <NUM_LIT><EOL>zdelt = <NUM_LIT><EOL>for k in range(<NUM_LIT:0>, N):<EOL><INDENT>y = numpy.array(x0, copy=True)<EOL>if y[k] != <NUM_LIT:0>:<EOL><INDENT>y[k] = (<NUM_LIT:1> + nonzdelt)*y[k]<EOL><DEDENT>else:<EOL><INDENT>y[k] = zdelt<EOL><DEDENT>sim[k + <NUM_LIT:1>] = y<EOL>f = func(y)<EOL>fsim[k + <NUM_LIT:1>] = f<EOL><DEDENT>ind = numpy.argsort(fsim)<EOL>fsim = numpy.take(fsim, ind, <NUM_LIT:0>)<EOL>sim = numpy.take(sim, ind, <NUM_LIT:0>)<EOL>iterations = <NUM_LIT:1><EOL>while (fcalls[<NUM_LIT:0>] < maxfun and iterations < maxiter):<EOL><INDENT>if (numpy.max(numpy.ravel(numpy.abs(sim[<NUM_LIT:1>:] - sim[<NUM_LIT:0>]))) <= xtol and<EOL>numpy.max(numpy.abs(fsim[<NUM_LIT:0>] - fsim[<NUM_LIT:1>:])) <= ftol):<EOL><INDENT>break<EOL><DEDENT>xbar = numpy.add.reduce(sim[:-<NUM_LIT:1>], <NUM_LIT:0>) / N<EOL>xr = (<NUM_LIT:1> + rho) * xbar - rho * sim[-<NUM_LIT:1>]<EOL>fxr = func(xr)<EOL>doshrink = <NUM_LIT:0><EOL>if fxr < fsim[<NUM_LIT:0>]:<EOL><INDENT>xe = (<NUM_LIT:1> + rho * chi) * xbar - rho * chi * sim[-<NUM_LIT:1>]<EOL>fxe = func(xe)<EOL>if fxe < fxr:<EOL><INDENT>sim[-<NUM_LIT:1>] = xe<EOL>fsim[-<NUM_LIT:1>] = fxe<EOL><DEDENT>else:<EOL><INDENT>sim[-<NUM_LIT:1>] = xr<EOL>fsim[-<NUM_LIT:1>] = fxr<EOL><DEDENT><DEDENT>else:  <EOL><INDENT>if fxr < fsim[-<NUM_LIT:2>]:<EOL><INDENT>sim[-<NUM_LIT:1>] = xr<EOL>fsim[-<NUM_LIT:1>] = fxr<EOL><DEDENT>else:  <EOL><INDENT>if fxr < fsim[-<NUM_LIT:1>]:<EOL><INDENT>xc = (<NUM_LIT:1> + psi * rho) * xbar - psi * rho * sim[-<NUM_LIT:1>]<EOL>fxc = func(xc)<EOL>if fxc <= fxr:<EOL><INDENT>sim[-<NUM_LIT:1>] = xc<EOL>fsim[-<NUM_LIT:1>] = fxc<EOL><DEDENT>else:<EOL><INDENT>doshrink = <NUM_LIT:1><EOL><DEDENT><DEDENT>else:<EOL><INDENT>xcc = (<NUM_LIT:1> - psi) * xbar + psi * sim[-<NUM_LIT:1>]<EOL>fxcc = func(xcc)<EOL>if fxcc < fsim[-<NUM_LIT:1>]:<EOL><INDENT>sim[-<NUM_LIT:1>] = xcc<EOL>fsim[-<NUM_LIT:1>] = fxcc<EOL><DEDENT>else:<EOL><INDENT>doshrink = <NUM_LIT:1><EOL><DEDENT><DEDENT>if doshrink:<EOL><INDENT>for j in one2np1:<EOL><INDENT>sim[j] = sim[<NUM_LIT:0>] + sigma * (sim[j] - sim[<NUM_LIT:0>])<EOL>fsim[j] = func(sim[j])<EOL><DEDENT><DEDENT><DEDENT><DEDENT>ind = numpy.argsort(fsim)<EOL>sim = numpy.take(sim, ind, <NUM_LIT:0>)<EOL>fsim = numpy.take(fsim, ind, <NUM_LIT:0>)<EOL>if callback is not None:<EOL><INDENT>callback(sim[<NUM_LIT:0>])<EOL><DEDENT>iterations += <NUM_LIT:1><EOL>if retall:<EOL><INDENT>allvecs.append(sim[<NUM_LIT:0>])<EOL><DEDENT><DEDENT>x = sim[<NUM_LIT:0>]<EOL>fval = numpy.min(fsim)<EOL>warnflag = <NUM_LIT:0><EOL>if fcalls[<NUM_LIT:0>] >= maxfun:<EOL><INDENT>warnflag = <NUM_LIT:1><EOL>msg = _status_message['<STR_LIT>']<EOL>if disp:<EOL><INDENT>print('<STR_LIT>' + msg)<EOL><DEDENT><DEDENT>elif iterations >= maxiter:<EOL><INDENT>warnflag = <NUM_LIT:2><EOL>msg = _status_message['<STR_LIT>']<EOL>if disp:<EOL><INDENT>print('<STR_LIT>' + msg)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>msg = _status_message['<STR_LIT:success>']<EOL>if disp:<EOL><INDENT>print(msg)<EOL>print("<STR_LIT>" % fval)<EOL>print("<STR_LIT>" % iterations)<EOL>print("<STR_LIT>" % fcalls[<NUM_LIT:0>])<EOL><DEDENT><DEDENT>result = OptimizeResult(fun=fval, nit=iterations, nfev=fcalls[<NUM_LIT:0>],<EOL>status=warnflag, success=(warnflag == <NUM_LIT:0>),<EOL>message=msg, x=x)<EOL>if retall:<EOL><INDENT>result['<STR_LIT>'] = allvecs<EOL><DEDENT>return result<EOL>

Minimization of scalar function of one or more variables using the
Nelder-Mead algorithm.

Options for the Nelder-Mead algorithm are:
    disp : bool
        Set to True to print convergence messages.
    xtol : float
        Relative error in solution `xopt` acceptable for convergence.
    ftol : float
        Relative error in ``fun(xopt)`` acceptable for convergence.
    maxiter : int
        Maximum number of iterations to perform.
    maxfev : int
        Maximum number of function evaluations to make.

This function is called by the `minimize` function with
`method=Nelder-Mead`. It is not supposed to be called directly.


def _approx_fprime_helper(xk, f, epsilon, args=(), f0=None):

if f0 is None:<EOL><INDENT>f0 = f(*((xk,) + args))<EOL><DEDENT>grad = numpy.zeros((len(xk),), float)<EOL>ei = numpy.zeros((len(xk),), float)<EOL>for k in range(len(xk)):<EOL><INDENT>ei[k] = <NUM_LIT:1.0><EOL>d = epsilon * ei<EOL>grad[k] = (f(*((xk + d,) + args)) - f0) / d[k]<EOL>ei[k] = <NUM_LIT:0.0><EOL><DEDENT>return grad<EOL>

See ``approx_fprime``.  An optional initial function value arg is added.


def approx_fprime(xk, f, epsilon, *args):

return _approx_fprime_helper(xk, f, epsilon, args=args)<EOL>

Finite-difference approximation of the gradient of a scalar function.

    Parameters
    ----------
    xk : array_like
        The coordinate vector at which to determine the gradient of `f`.
    f : callable
        The function of which to determine the gradient (partial derivatives).
        Should take `xk` as first argument, other arguments to `f` can be
        supplied in ``*args``.  Should return a scalar, the value of the
        function at `xk`.
    epsilon : array_like
        Increment to `xk` to use for determining the function gradient.
        If a scalar, uses the same finite difference delta for all partial
        derivatives.  If an array, should contain one value per element of
        `xk`.
    \*args : args, optional
        Any other arguments that are to be passed to `f`.

    Returns
    -------
    grad : ndarray
        The partial derivatives of `f` to `xk`.

    See Also
    --------
    check_grad : Check correctness of gradient function against approx_fprime.

    Notes
    -----
    The function gradient is determined by the forward finite difference
    formula::

                 f(xk[i] + epsilon[i]) - f(xk[i])
        f'[i] = ---------------------------------
                            epsilon[i]

    The main use of `approx_fprime` is in scalar function optimizers like
    `fmin_bfgs`, to determine numerically the Jacobian of a function.

    Examples
    --------
    >>> from scipy import optimize
    >>> def func(x, c0, c1):
    ...     "Coordinate vector `x` should be an array of size two."
    ...     return c0 * x[0]**2 + c1*x[1]**2

    >>> x = np.ones(2)
    >>> c0, c1 = (1, 200)
    >>> eps = np.sqrt(np.finfo(np.float).eps)
    >>> optimize.approx_fprime(x, func, [eps, np.sqrt(200) * eps], c0, c1)
    array([   2.        ,  400.00004198])


def check_grad(func, grad, x0, *args, **kwargs):

step = kwargs.pop('<STR_LIT>', _epsilon)<EOL>if kwargs:<EOL><INDENT>raise ValueError("<STR_LIT>" %<EOL>(list(kwargs.keys()),))<EOL><DEDENT>return sqrt(sum((grad(x0, *args) -<EOL>approx_fprime(x0, func, step, *args))**<NUM_LIT:2>))<EOL>

Check the correctness of a gradient function by comparing it against a
    (forward) finite-difference approximation of the gradient.

    Parameters
    ----------
    func : callable func(x0,*args)
        Function whose derivative is to be checked.
    grad : callable grad(x0, *args)
        Gradient of `func`.
    x0 : ndarray
        Points to check `grad` against forward difference approximation of grad
        using `func`.
    args : \*args, optional
        Extra arguments passed to `func` and `grad`.
    epsilon : float, optional
        Step size used for the finite difference approximation. It defaults to
        ``sqrt(numpy.finfo(float).eps)``, which is approximately 1.49e-08.

    Returns
    -------
    err : float
        The square root of the sum of squares (i.e. the 2-norm) of the
        difference between ``grad(x0, *args)`` and the finite difference
        approximation of `grad` using func at the points `x0`.

    See Also
    --------
    approx_fprime

    Examples
    --------
    >>> def func(x): return x[0]**2 - 0.5 * x[1]**3
    >>> def grad(x): return [2 * x[0], -1.5 * x[1]**2]
    >>> check_grad(func, grad, [1.5, -1.5])
    2.9802322387695312e-08


def _line_search_wolfe12(f, fprime, xk, pk, gfk, old_fval, old_old_fval,<EOL>**kwargs):

ret = line_search_wolfe1(f, fprime, xk, pk, gfk,<EOL>old_fval, old_old_fval,<EOL>**kwargs)<EOL>if ret[<NUM_LIT:0>] is None:<EOL><INDENT>ret = line_search_wolfe2(f, fprime, xk, pk, gfk,<EOL>old_fval, old_old_fval)<EOL><DEDENT>if ret[<NUM_LIT:0>] is None:<EOL><INDENT>raise _LineSearchError()<EOL><DEDENT>return ret<EOL>

Same as line_search_wolfe1, but fall back to line_search_wolfe2 if
suitable step length is not found, and raise an exception if a
suitable step length is not found.

Raises
------
_LineSearchError
    If no suitable step size is found


def fmin_bfgs(f, x0, fprime=None, args=(), gtol=<NUM_LIT>, norm=Inf,<EOL>epsilon=_epsilon, maxiter=None, full_output=<NUM_LIT:0>, disp=<NUM_LIT:1>,<EOL>retall=<NUM_LIT:0>, callback=None):

opts = {'<STR_LIT>': gtol,<EOL>'<STR_LIT>': norm,<EOL>'<STR_LIT>': epsilon,<EOL>'<STR_LIT>': disp,<EOL>'<STR_LIT>': maxiter,<EOL>'<STR_LIT>': retall}<EOL>res = _minimize_bfgs(f, x0, args, fprime, callback=callback, **opts)<EOL>if full_output:<EOL><INDENT>retlist = (res['<STR_LIT:x>'], res['<STR_LIT>'], res['<STR_LIT>'], res['<STR_LIT>'],<EOL>res['<STR_LIT>'], res['<STR_LIT>'], res['<STR_LIT:status>'])<EOL>if retall:<EOL><INDENT>retlist += (res['<STR_LIT>'], )<EOL><DEDENT>return retlist<EOL><DEDENT>else:<EOL><INDENT>if retall:<EOL><INDENT>return res['<STR_LIT:x>'], res['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>return res['<STR_LIT:x>']<EOL><DEDENT><DEDENT>

Minimize a function using the BFGS algorithm.

Parameters
----------
f : callable f(x,*args)
    Objective function to be minimized.
x0 : ndarray
    Initial guess.
fprime : callable f'(x,*args), optional
    Gradient of f.
args : tuple, optional
    Extra arguments passed to f and fprime.
gtol : float, optional
    Gradient norm must be less than gtol before successful termination.
norm : float, optional
    Order of norm (Inf is max, -Inf is min)
epsilon : int or ndarray, optional
    If fprime is approximated, use this value for the step size.
callback : callable, optional
    An optional user-supplied function to call after each
    iteration.  Called as callback(xk), where xk is the
    current parameter vector.
maxiter : int, optional
    Maximum number of iterations to perform.
full_output : bool, optional
    If True,return fopt, func_calls, grad_calls, and warnflag
    in addition to xopt.
disp : bool, optional
    Print convergence message if True.
retall : bool, optional
    Return a list of results at each iteration if True.

Returns
-------
xopt : ndarray
    Parameters which minimize f, i.e. f(xopt) == fopt.
fopt : float
    Minimum value.
gopt : ndarray
    Value of gradient at minimum, f'(xopt), which should be near 0.
Bopt : ndarray
    Value of 1/f''(xopt), i.e. the inverse hessian matrix.
func_calls : int
    Number of function_calls made.
grad_calls : int
    Number of gradient calls made.
warnflag : integer
    1 : Maximum number of iterations exceeded.
    2 : Gradient and/or function calls not changing.
allvecs  :  list
    `OptimizeResult` at each iteration.  Only returned if retall is True.

See also
--------
minimize: Interface to minimization algorithms for multivariate
    functions. See the 'BFGS' `method` in particular.

Notes
-----
Optimize the function, f, whose gradient is given by fprime
using the quasi-Newton method of Broyden, Fletcher, Goldfarb,
and Shanno (BFGS)

References
----------
Wright, and Nocedal 'Numerical Optimization', 1999, pg. 198.


def _minimize_bfgs(fun, x0, args=(), jac=None, callback=None,<EOL>gtol=<NUM_LIT>, norm=Inf, eps=_epsilon, maxiter=None,<EOL>disp=False, return_all=False,<EOL>**unknown_options):

_check_unknown_options(unknown_options)<EOL>f = fun<EOL>fprime = jac<EOL>epsilon = eps<EOL>retall = return_all<EOL>x0 = asarray(x0).flatten()<EOL>if x0.ndim == <NUM_LIT:0>:<EOL><INDENT>x0.shape = (<NUM_LIT:1>,)<EOL><DEDENT>if maxiter is None:<EOL><INDENT>maxiter = len(x0) * <NUM_LIT:200><EOL><DEDENT>func_calls, f = wrap_function(f, args)<EOL>if fprime is None:<EOL><INDENT>grad_calls, myfprime = wrap_function(approx_fprime, (f, epsilon))<EOL><DEDENT>else:<EOL><INDENT>grad_calls, myfprime = wrap_function(fprime, args)<EOL><DEDENT>gfk = myfprime(x0)<EOL>k = <NUM_LIT:0><EOL>N = len(x0)<EOL>I = numpy.eye(N, dtype=int)<EOL>Hk = I<EOL>old_fval = f(x0)<EOL>old_old_fval = None<EOL>xk = x0<EOL>if retall:<EOL><INDENT>allvecs = [x0]<EOL><DEDENT>sk = [<NUM_LIT:2> * gtol]<EOL>warnflag = <NUM_LIT:0><EOL>gnorm = vecnorm(gfk, ord=norm)<EOL>while (gnorm > gtol) and (k < maxiter):<EOL><INDENT>pk = -numpy.dot(Hk, gfk)<EOL>try:<EOL><INDENT>alpha_k, fc, gc, old_fval, old_old_fval, gfkp1 =_line_search_wolfe12(f, myfprime, xk, pk, gfk,<EOL>old_fval, old_old_fval)<EOL><DEDENT>except _LineSearchError:<EOL><INDENT>warnflag = <NUM_LIT:2><EOL>break<EOL><DEDENT>xkp1 = xk + alpha_k * pk<EOL>if retall:<EOL><INDENT>allvecs.append(xkp1)<EOL><DEDENT>sk = xkp1 - xk<EOL>xk = xkp1<EOL>if gfkp1 is None:<EOL><INDENT>gfkp1 = myfprime(xkp1)<EOL><DEDENT>yk = gfkp1 - gfk<EOL>gfk = gfkp1<EOL>if callback is not None:<EOL><INDENT>callback(xk)<EOL><DEDENT>k += <NUM_LIT:1><EOL>gnorm = vecnorm(gfk, ord=norm)<EOL>if (gnorm <= gtol):<EOL><INDENT>break<EOL><DEDENT>if not numpy.isfinite(old_fval):<EOL><INDENT>warnflag = <NUM_LIT:2><EOL>break<EOL><DEDENT>try:  <EOL><INDENT>rhok = <NUM_LIT:1.0> / (numpy.dot(yk, sk))<EOL><DEDENT>except ZeroDivisionError:<EOL><INDENT>rhok = <NUM_LIT><EOL>if disp:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT><DEDENT>if isinf(rhok):  <EOL><INDENT>rhok = <NUM_LIT><EOL>if disp:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT><DEDENT>A1 = I - sk[:, numpy.newaxis] * yk[numpy.newaxis, :] * rhok<EOL>A2 = I - yk[:, numpy.newaxis] * sk[numpy.newaxis, :] * rhok<EOL>Hk = numpy.dot(A1, numpy.dot(Hk, A2)) + (rhok * sk[:, numpy.newaxis] *<EOL>sk[numpy.newaxis, :])<EOL><DEDENT>fval = old_fval<EOL>if warnflag == <NUM_LIT:2>:<EOL><INDENT>msg = _status_message['<STR_LIT>']<EOL>if disp:<EOL><INDENT>print("<STR_LIT>" + msg)<EOL>print("<STR_LIT>" % fval)<EOL>print("<STR_LIT>" % k)<EOL>print("<STR_LIT>" % func_calls[<NUM_LIT:0>])<EOL>print("<STR_LIT>" % grad_calls[<NUM_LIT:0>])<EOL><DEDENT><DEDENT>elif k >= maxiter:<EOL><INDENT>warnflag = <NUM_LIT:1><EOL>msg = _status_message['<STR_LIT>']<EOL>if disp:<EOL><INDENT>print("<STR_LIT>" + msg)<EOL>print("<STR_LIT>" % fval)<EOL>print("<STR_LIT>" % k)<EOL>print("<STR_LIT>" % func_calls[<NUM_LIT:0>])<EOL>print("<STR_LIT>" % grad_calls[<NUM_LIT:0>])<EOL><DEDENT><DEDENT>else:<EOL><INDENT>msg = _status_message['<STR_LIT:success>']<EOL>if disp:<EOL><INDENT>print(msg)<EOL>print("<STR_LIT>" % fval)<EOL>print("<STR_LIT>" % k)<EOL>print("<STR_LIT>" % func_calls[<NUM_LIT:0>])<EOL>print("<STR_LIT>" % grad_calls[<NUM_LIT:0>])<EOL><DEDENT><DEDENT>result = OptimizeResult(fun=fval, jac=gfk, hess_inv=Hk, nfev=func_calls[<NUM_LIT:0>],<EOL>njev=grad_calls[<NUM_LIT:0>], status=warnflag,<EOL>success=(warnflag == <NUM_LIT:0>), message=msg, x=xk)<EOL>if retall:<EOL><INDENT>result['<STR_LIT>'] = allvecs<EOL><DEDENT>return result<EOL>

Minimization of scalar function of one or more variables using the
BFGS algorithm.

Options for the BFGS algorithm are:
    disp : bool
        Set to True to print convergence messages.
    maxiter : int
        Maximum number of iterations to perform.
    gtol : float
        Gradient norm must be less than `gtol` before successful
        termination.
    norm : float
        Order of norm (Inf is max, -Inf is min).
    eps : float or ndarray
        If `jac` is approximated, use this value for the step size.

This function is called by the `minimize` function with `method=BFGS`.
It is not supposed to be called directly.


def fmin_cg(f, x0, fprime=None, args=(), gtol=<NUM_LIT>, norm=Inf, epsilon=_epsilon,<EOL>maxiter=None, full_output=<NUM_LIT:0>, disp=<NUM_LIT:1>, retall=<NUM_LIT:0>, callback=None):

opts = {'<STR_LIT>': gtol,<EOL>'<STR_LIT>': norm,<EOL>'<STR_LIT>': epsilon,<EOL>'<STR_LIT>': disp,<EOL>'<STR_LIT>': maxiter,<EOL>'<STR_LIT>': retall}<EOL>res = _minimize_cg(f, x0, args, fprime, callback=callback, **opts)<EOL>if full_output:<EOL><INDENT>retlist = res['<STR_LIT:x>'], res['<STR_LIT>'], res['<STR_LIT>'], res['<STR_LIT>'], res['<STR_LIT:status>']<EOL>if retall:<EOL><INDENT>retlist += (res['<STR_LIT>'], )<EOL><DEDENT>return retlist<EOL><DEDENT>else:<EOL><INDENT>if retall:<EOL><INDENT>return res['<STR_LIT:x>'], res['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>return res['<STR_LIT:x>']<EOL><DEDENT><DEDENT>

Minimize a function using a nonlinear conjugate gradient algorithm.

Parameters
----------
f : callable, ``f(x, *args)``
    Objective function to be minimized.  Here `x` must be a 1-D array of
    the variables that are to be changed in the search for a minimum, and
    `args` are the other (fixed) parameters of `f`.
x0 : ndarray
    A user-supplied initial estimate of `xopt`, the optimal value of `x`.
    It must be a 1-D array of values.
fprime : callable, ``fprime(x, *args)``, optional
    A function that returns the gradient of `f` at `x`. Here `x` and `args`
    are as described above for `f`. The returned value must be a 1-D array.
    Defaults to None, in which case the gradient is approximated
    numerically (see `epsilon`, below).
args : tuple, optional
    Parameter values passed to `f` and `fprime`. Must be supplied whenever
    additional fixed parameters are needed to completely specify the
    functions `f` and `fprime`.
gtol : float, optional
    Stop when the norm of the gradient is less than `gtol`.
norm : float, optional
    Order to use for the norm of the gradient
    (``-np.Inf`` is min, ``np.Inf`` is max).
epsilon : float or ndarray, optional
    Step size(s) to use when `fprime` is approximated numerically. Can be a
    scalar or a 1-D array.  Defaults to ``sqrt(eps)``, with eps the
    floating point machine precision.  Usually ``sqrt(eps)`` is about
    1.5e-8.
maxiter : int, optional
    Maximum number of iterations to perform. Default is ``200 * len(x0)``.
full_output : bool, optional
    If True, return `fopt`, `func_calls`, `grad_calls`, and `warnflag` in
    addition to `xopt`.  See the Returns section below for additional
    information on optional return values.
disp : bool, optional
    If True, return a convergence message, followed by `xopt`.
retall : bool, optional
    If True, add to the returned values the results of each iteration.
callback : callable, optional
    An optional user-supplied function, called after each iteration.
    Called as ``callback(xk)``, where ``xk`` is the current value of `x0`.

Returns
-------
xopt : ndarray
    Parameters which minimize f, i.e. ``f(xopt) == fopt``.
fopt : float, optional
    Minimum value found, f(xopt).  Only returned if `full_output` is True.
func_calls : int, optional
    The number of function_calls made.  Only returned if `full_output`
    is True.
grad_calls : int, optional
    The number of gradient calls made. Only returned if `full_output` is
    True.
warnflag : int, optional
    Integer value with warning status, only returned if `full_output` is
    True.

    0 : Success.

    1 : The maximum number of iterations was exceeded.

    2 : Gradient and/or function calls were not changing.  May indicate
        that precision was lost, i.e., the routine did not converge.

allvecs : list of ndarray, optional
    List of arrays, containing the results at each iteration.
    Only returned if `retall` is True.

See Also
--------
minimize : common interface to all `scipy.optimize` algorithms for
           unconstrained and constrained minimization of multivariate
           functions.  It provides an alternative way to call
           ``fmin_cg``, by specifying ``method='CG'``.

Notes
-----
This conjugate gradient algorithm is based on that of Polak and Ribiere
[1]_.

Conjugate gradient methods tend to work better when:

1. `f` has a unique global minimizing point, and no local minima or
   other stationary points,
2. `f` is, at least locally, reasonably well approximated by a
   quadratic function of the variables,
3. `f` is continuous and has a continuous gradient,
4. `fprime` is not too large, e.g., has a norm less than 1000,
5. The initial guess, `x0`, is reasonably close to `f` 's global
   minimizing point, `xopt`.

References
----------
.. [1] Wright & Nocedal, "Numerical Optimization", 1999, pp. 120-122.

Examples
--------
Example 1: seek the minimum value of the expression
``a*u**2 + b*u*v + c*v**2 + d*u + e*v + f`` for given values
of the parameters and an initial guess ``(u, v) = (0, 0)``.

>>> args = (2, 3, 7, 8, 9, 10)  # parameter values
>>> def f(x, *args):
...     u, v = x
...     a, b, c, d, e, f = args
...     return a*u**2 + b*u*v + c*v**2 + d*u + e*v + f
>>> def gradf(x, *args):
...     u, v = x
...     a, b, c, d, e, f = args
...     gu = 2*a*u + b*v + d     # u-component of the gradient
...     gv = b*u + 2*c*v + e     # v-component of the gradient
...     return np.asarray((gu, gv))
>>> x0 = np.asarray((0, 0))  # Initial guess.
>>> from scipy import optimize
>>> res1 = optimize.fmin_cg(f, x0, fprime=gradf, args=args)
>>> print 'res1 = ', res1
Optimization terminated successfully.
         Current function value: 1.617021
         Iterations: 2
         Function evaluations: 5
         Gradient evaluations: 5
res1 =  [-1.80851064 -0.25531915]

Example 2: solve the same problem using the `minimize` function.
(This `myopts` dictionary shows all of the available options,
although in practice only non-default values would be needed.
The returned value will be a dictionary.)

>>> opts = {'maxiter' : None,    # default value.
...         'disp' : True,    # non-default value.
...         'gtol' : 1e-5,    # default value.
...         'norm' : np.inf,  # default value.
...         'eps' : 1.4901161193847656e-08}  # default value.
>>> res2 = optimize.minimize(f, x0, jac=gradf, args=args,
...                          method='CG', options=opts)
Optimization terminated successfully.
        Current function value: 1.617021
        Iterations: 2
        Function evaluations: 5
        Gradient evaluations: 5
>>> res2.x  # minimum found
array([-1.80851064 -0.25531915])


def _minimize_cg(fun, x0, args=(), jac=None, callback=None,<EOL>gtol=<NUM_LIT>, norm=Inf, eps=_epsilon, maxiter=None,<EOL>disp=False, return_all=False,<EOL>**unknown_options):

_check_unknown_options(unknown_options)<EOL>f = fun<EOL>fprime = jac<EOL>epsilon = eps<EOL>retall = return_all<EOL>x0 = asarray(x0).flatten()<EOL>if maxiter is None:<EOL><INDENT>maxiter = len(x0) * <NUM_LIT:200><EOL><DEDENT>func_calls, f = wrap_function(f, args)<EOL>if fprime is None:<EOL><INDENT>grad_calls, myfprime = wrap_function(approx_fprime, (f, epsilon))<EOL><DEDENT>else:<EOL><INDENT>grad_calls, myfprime = wrap_function(fprime, args)<EOL><DEDENT>gfk = myfprime(x0)<EOL>k = <NUM_LIT:0><EOL>xk = x0<EOL>old_fval = f(xk)<EOL>old_old_fval = None<EOL>if retall:<EOL><INDENT>allvecs = [xk]<EOL><DEDENT>warnflag = <NUM_LIT:0><EOL>pk = -gfk<EOL>gnorm = vecnorm(gfk, ord=norm)<EOL>while (gnorm > gtol) and (k < maxiter):<EOL><INDENT>deltak = numpy.dot(gfk, gfk)<EOL>try:<EOL><INDENT>alpha_k, fc, gc, old_fval, old_old_fval, gfkp1 =_line_search_wolfe12(f, myfprime, xk, pk, gfk, old_fval,<EOL>old_old_fval, c2=<NUM_LIT>)<EOL><DEDENT>except _LineSearchError:<EOL><INDENT>warnflag = <NUM_LIT:2><EOL>break<EOL><DEDENT>xk = xk + alpha_k * pk<EOL>if retall:<EOL><INDENT>allvecs.append(xk)<EOL><DEDENT>if gfkp1 is None:<EOL><INDENT>gfkp1 = myfprime(xk)<EOL><DEDENT>yk = gfkp1 - gfk<EOL>beta_k = max(<NUM_LIT:0>, numpy.dot(yk, gfkp1) / deltak)<EOL>pk = -gfkp1 + beta_k * pk<EOL>gfk = gfkp1<EOL>gnorm = vecnorm(gfk, ord=norm)<EOL>if callback is not None:<EOL><INDENT>callback(xk)<EOL><DEDENT>k += <NUM_LIT:1><EOL><DEDENT>fval = old_fval<EOL>if warnflag == <NUM_LIT:2>:<EOL><INDENT>msg = _status_message['<STR_LIT>']<EOL>if disp:<EOL><INDENT>print("<STR_LIT>" + msg)<EOL>print("<STR_LIT>" % fval)<EOL>print("<STR_LIT>" % k)<EOL>print("<STR_LIT>" % func_calls[<NUM_LIT:0>])<EOL>print("<STR_LIT>" % grad_calls[<NUM_LIT:0>])<EOL><DEDENT><DEDENT>elif k >= maxiter:<EOL><INDENT>warnflag = <NUM_LIT:1><EOL>msg = _status_message['<STR_LIT>']<EOL>if disp:<EOL><INDENT>print("<STR_LIT>" + msg)<EOL>print("<STR_LIT>" % fval)<EOL>print("<STR_LIT>" % k)<EOL>print("<STR_LIT>" % func_calls[<NUM_LIT:0>])<EOL>print("<STR_LIT>" % grad_calls[<NUM_LIT:0>])<EOL><DEDENT><DEDENT>else:<EOL><INDENT>msg = _status_message['<STR_LIT:success>']<EOL>if disp:<EOL><INDENT>print(msg)<EOL>print("<STR_LIT>" % fval)<EOL>print("<STR_LIT>" % k)<EOL>print("<STR_LIT>" % func_calls[<NUM_LIT:0>])<EOL>print("<STR_LIT>" % grad_calls[<NUM_LIT:0>])<EOL><DEDENT><DEDENT>result = OptimizeResult(fun=fval, jac=gfk, nfev=func_calls[<NUM_LIT:0>],<EOL>njev=grad_calls[<NUM_LIT:0>], status=warnflag,<EOL>success=(warnflag == <NUM_LIT:0>), message=msg, x=xk)<EOL>if retall:<EOL><INDENT>result['<STR_LIT>'] = allvecs<EOL><DEDENT>return result<EOL>

Minimization of scalar function of one or more variables using the
conjugate gradient algorithm.

Options for the conjugate gradient algorithm are:
    disp : bool
        Set to True to print convergence messages.
    maxiter : int
        Maximum number of iterations to perform.
    gtol : float
        Gradient norm must be less than `gtol` before successful
        termination.
    norm : float
        Order of norm (Inf is max, -Inf is min).
    eps : float or ndarray
        If `jac` is approximated, use this value for the step size.

This function is called by the `minimize` function with `method=CG`. It
is not supposed to be called directly.


def fmin_ncg(f, x0, fprime, fhess_p=None, fhess=None, args=(), avextol=<NUM_LIT>,<EOL>epsilon=_epsilon, maxiter=None, full_output=<NUM_LIT:0>, disp=<NUM_LIT:1>, retall=<NUM_LIT:0>,<EOL>callback=None):

opts = {'<STR_LIT>': avextol,<EOL>'<STR_LIT>': epsilon,<EOL>'<STR_LIT>': maxiter,<EOL>'<STR_LIT>': disp,<EOL>'<STR_LIT>': retall}<EOL>res = _minimize_newtoncg(f, x0, args, fprime, fhess, fhess_p,<EOL>callback=callback, **opts)<EOL>if full_output:<EOL><INDENT>retlist = (res['<STR_LIT:x>'], res['<STR_LIT>'], res['<STR_LIT>'], res['<STR_LIT>'],<EOL>res['<STR_LIT>'], res['<STR_LIT:status>'])<EOL>if retall:<EOL><INDENT>retlist += (res['<STR_LIT>'], )<EOL><DEDENT>return retlist<EOL><DEDENT>else:<EOL><INDENT>if retall:<EOL><INDENT>return res['<STR_LIT:x>'], res['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>return res['<STR_LIT:x>']<EOL><DEDENT><DEDENT>

Unconstrained minimization of a function using the Newton-CG method.

Parameters
----------
f : callable ``f(x, *args)``
    Objective function to be minimized.
x0 : ndarray
    Initial guess.
fprime : callable ``f'(x, *args)``
    Gradient of f.
fhess_p : callable ``fhess_p(x, p, *args)``, optional
    Function which computes the Hessian of f times an
    arbitrary vector, p.
fhess : callable ``fhess(x, *args)``, optional
    Function to compute the Hessian matrix of f.
args : tuple, optional
    Extra arguments passed to f, fprime, fhess_p, and fhess
    (the same set of extra arguments is supplied to all of
    these functions).
epsilon : float or ndarray, optional
    If fhess is approximated, use this value for the step size.
callback : callable, optional
    An optional user-supplied function which is called after
    each iteration.  Called as callback(xk), where xk is the
    current parameter vector.
avextol : float, optional
    Convergence is assumed when the average relative error in
    the minimizer falls below this amount.
maxiter : int, optional
    Maximum number of iterations to perform.
full_output : bool, optional
    If True, return the optional outputs.
disp : bool, optional
    If True, print convergence message.
retall : bool, optional
    If True, return a list of results at each iteration.

Returns
-------
xopt : ndarray
    Parameters which minimize f, i.e. ``f(xopt) == fopt``.
fopt : float
    Value of the function at xopt, i.e. ``fopt = f(xopt)``.
fcalls : int
    Number of function calls made.
gcalls : int
    Number of gradient calls made.
hcalls : int
    Number of hessian calls made.
warnflag : int
    Warnings generated by the algorithm.
    1 : Maximum number of iterations exceeded.
allvecs : list
    The result at each iteration, if retall is True (see below).

See also
--------
minimize: Interface to minimization algorithms for multivariate
    functions. See the 'Newton-CG' `method` in particular.

Notes
-----
Only one of `fhess_p` or `fhess` need to be given.  If `fhess`
is provided, then `fhess_p` will be ignored.  If neither `fhess`
nor `fhess_p` is provided, then the hessian product will be
approximated using finite differences on `fprime`. `fhess_p`
must compute the hessian times an arbitrary vector. If it is not
given, finite-differences on `fprime` are used to compute
it.

Newton-CG methods are also called truncated Newton methods. This
function differs from scipy.optimize.fmin_tnc because

1. scipy.optimize.fmin_ncg is written purely in python using numpy
    and scipy while scipy.optimize.fmin_tnc calls a C function.
2. scipy.optimize.fmin_ncg is only for unconstrained minimization
    while scipy.optimize.fmin_tnc is for unconstrained minimization
    or box constrained minimization. (Box constraints give
    lower and upper bounds for each variable separately.)

References
----------
Wright & Nocedal, 'Numerical Optimization', 1999, pg. 140.


def _minimize_newtoncg(fun, x0, args=(), jac=None, hess=None, hessp=None,<EOL>callback=None, xtol=<NUM_LIT>, eps=_epsilon, maxiter=None,<EOL>disp=False, return_all=False,<EOL>**unknown_options):

_check_unknown_options(unknown_options)<EOL>if jac is None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>f = fun<EOL>fprime = jac<EOL>fhess_p = hessp<EOL>fhess = hess<EOL>avextol = xtol<EOL>epsilon = eps<EOL>retall = return_all<EOL>x0 = asarray(x0).flatten()<EOL>fcalls, f = wrap_function(f, args)<EOL>gcalls, fprime = wrap_function(fprime, args)<EOL>hcalls = <NUM_LIT:0><EOL>if maxiter is None:<EOL><INDENT>maxiter = len(x0)*<NUM_LIT:200><EOL><DEDENT>xtol = len(x0) * avextol<EOL>update = [<NUM_LIT:2> * xtol]<EOL>xk = x0<EOL>if retall:<EOL><INDENT>allvecs = [xk]<EOL><DEDENT>k = <NUM_LIT:0><EOL>old_fval = f(x0)<EOL>old_old_fval = None<EOL>float64eps = numpy.finfo(numpy.float64).eps<EOL>warnflag = <NUM_LIT:0><EOL>while (numpy.add.reduce(numpy.abs(update)) > xtol) and (k < maxiter):<EOL><INDENT>b = -fprime(xk)<EOL>maggrad = numpy.add.reduce(numpy.abs(b))<EOL>eta = numpy.min([<NUM_LIT:0.5>, numpy.sqrt(maggrad)])<EOL>termcond = eta * maggrad<EOL>xsupi = zeros(len(x0), dtype=x0.dtype)<EOL>ri = -b<EOL>psupi = -ri<EOL>i = <NUM_LIT:0><EOL>dri0 = numpy.dot(ri, ri)<EOL>if fhess is not None:             <EOL><INDENT>A = fhess(*(xk,) + args)<EOL>hcalls = hcalls + <NUM_LIT:1><EOL><DEDENT>while numpy.add.reduce(numpy.abs(ri)) > termcond:<EOL><INDENT>if fhess is None:<EOL><INDENT>if fhess_p is None:<EOL><INDENT>Ap = approx_fhess_p(xk, psupi, fprime, epsilon)<EOL><DEDENT>else:<EOL><INDENT>Ap = fhess_p(xk, psupi, *args)<EOL>hcalls = hcalls + <NUM_LIT:1><EOL><DEDENT><DEDENT>else:<EOL><INDENT>Ap = numpy.dot(A, psupi)<EOL><DEDENT>Ap = asarray(Ap).squeeze()  <EOL>curv = numpy.dot(psupi, Ap)<EOL>if <NUM_LIT:0> <= curv <= <NUM_LIT:3> * float64eps:<EOL><INDENT>break<EOL><DEDENT>elif curv < <NUM_LIT:0>:<EOL><INDENT>if (i > <NUM_LIT:0>):<EOL><INDENT>break<EOL><DEDENT>else:<EOL><INDENT>xsupi = dri0 / (-curv) * b<EOL>break<EOL><DEDENT><DEDENT>alphai = dri0 / curv<EOL>xsupi = xsupi + alphai * psupi<EOL>ri = ri + alphai * Ap<EOL>dri1 = numpy.dot(ri, ri)<EOL>betai = dri1 / dri0<EOL>psupi = -ri + betai * psupi<EOL>i = i + <NUM_LIT:1><EOL>dri0 = dri1          <EOL><DEDENT>pk = xsupi  <EOL>gfk = -b    <EOL>try:<EOL><INDENT>alphak, fc, gc, old_fval, old_old_fval, gfkp1 =_line_search_wolfe12(f, fprime, xk, pk, gfk,<EOL>old_fval, old_old_fval)<EOL><DEDENT>except _LineSearchError:<EOL><INDENT>warnflag = <NUM_LIT:2><EOL>break<EOL><DEDENT>update = alphak * pk<EOL>xk = xk + update        <EOL>if callback is not None:<EOL><INDENT>callback(xk)<EOL><DEDENT>if retall:<EOL><INDENT>allvecs.append(xk)<EOL><DEDENT>k += <NUM_LIT:1><EOL><DEDENT>fval = old_fval<EOL>if warnflag == <NUM_LIT:2>:<EOL><INDENT>msg = _status_message['<STR_LIT>']<EOL>if disp:<EOL><INDENT>print("<STR_LIT>" + msg)<EOL>print("<STR_LIT>" % fval)<EOL>print("<STR_LIT>" % k)<EOL>print("<STR_LIT>" % fcalls[<NUM_LIT:0>])<EOL>print("<STR_LIT>" % gcalls[<NUM_LIT:0>])<EOL>print("<STR_LIT>" % hcalls)<EOL><DEDENT><DEDENT>elif k >= maxiter:<EOL><INDENT>warnflag = <NUM_LIT:1><EOL>msg = _status_message['<STR_LIT>']<EOL>if disp:<EOL><INDENT>print("<STR_LIT>" + msg)<EOL>print("<STR_LIT>" % fval)<EOL>print("<STR_LIT>" % k)<EOL>print("<STR_LIT>" % fcalls[<NUM_LIT:0>])<EOL>print("<STR_LIT>" % gcalls[<NUM_LIT:0>])<EOL>print("<STR_LIT>" % hcalls)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>msg = _status_message['<STR_LIT:success>']<EOL>if disp:<EOL><INDENT>print(msg)<EOL>print("<STR_LIT>" % fval)<EOL>print("<STR_LIT>" % k)<EOL>print("<STR_LIT>" % fcalls[<NUM_LIT:0>])<EOL>print("<STR_LIT>" % gcalls[<NUM_LIT:0>])<EOL>print("<STR_LIT>" % hcalls)<EOL><DEDENT><DEDENT>result = OptimizeResult(fun=fval, jac=gfk, nfev=fcalls[<NUM_LIT:0>], njev=gcalls[<NUM_LIT:0>],<EOL>nhev=hcalls, status=warnflag,<EOL>success=(warnflag == <NUM_LIT:0>), message=msg, x=xk)<EOL>if retall:<EOL><INDENT>result['<STR_LIT>'] = allvecs<EOL><DEDENT>return result<EOL>

Minimization of scalar function of one or more variables using the
Newton-CG algorithm.

Options for the Newton-CG algorithm are:
    disp : bool
        Set to True to print convergence messages.
    xtol : float
        Average relative error in solution `xopt` acceptable for
        convergence.
    maxiter : int
        Maximum number of iterations to perform.
    eps : float or ndarray
        If `jac` is approximated, use this value for the step size.

This function is called by the `minimize` function with
`method=Newton-CG`. It is not supposed to be called directly.

Also note that the `jac` parameter (Jacobian) is required.


def fminbound(func, x1, x2, args=(), xtol=<NUM_LIT>, maxfun=<NUM_LIT>,<EOL>full_output=<NUM_LIT:0>, disp=<NUM_LIT:1>):

options = {'<STR_LIT>': xtol,<EOL>'<STR_LIT>': maxfun,<EOL>'<STR_LIT>': disp}<EOL>res = _minimize_scalar_bounded(func, (x1, x2), args, **options)<EOL>if full_output:<EOL><INDENT>return res['<STR_LIT:x>'], res['<STR_LIT>'], res['<STR_LIT:status>'], res['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>return res['<STR_LIT:x>']<EOL><DEDENT>

Bounded minimization for scalar functions.

    Parameters
    ----------
    func : callable f(x,*args)
        Objective function to be minimized (must accept and return scalars).
    x1, x2 : float or array scalar
        The optimization bounds.
    args : tuple, optional
        Extra arguments passed to function.
    xtol : float, optional
        The convergence tolerance.
    maxfun : int, optional
        Maximum number of function evaluations allowed.
    full_output : bool, optional
        If True, return optional outputs.
    disp : int, optional
        If non-zero, print messages.
            0 : no message printing.
            1 : non-convergence notification messages only.
            2 : print a message on convergence too.
            3 : print iteration results.

    Returns
    -------
    xopt : ndarray
        Parameters (over given interval) which minimize the
        objective function.
    fval : number
        The function value at the minimum point.
    ierr : int
        An error flag (0 if converged, 1 if maximum number of
        function calls reached).
    numfunc : int
      The number of function calls made.

    See also
    --------
    minimize_scalar: Interface to minimization algorithms for scalar
        univariate functions. See the 'Bounded' `method` in particular.

    Notes
    -----
    Finds a local minimizer of the scalar function `func` in the
    interval x1 < xopt < x2 using Brent's method.  (See `brent`
    for auto-bracketing).


def brent(func, args=(), brack=None, tol=<NUM_LIT>, full_output=<NUM_LIT:0>, maxiter=<NUM_LIT>):

options = {'<STR_LIT>': tol,<EOL>'<STR_LIT>': maxiter}<EOL>res = _minimize_scalar_brent(func, brack, args, **options)<EOL>if full_output:<EOL><INDENT>return res['<STR_LIT:x>'], res['<STR_LIT>'], res['<STR_LIT>'], res['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>return res['<STR_LIT:x>']<EOL><DEDENT>

Given a function of one-variable and a possible bracketing interval,
return the minimum of the function isolated to a fractional precision of
tol.

Parameters
----------
func : callable f(x,*args)
    Objective function.
args
    Additional arguments (if present).
brack : tuple
    Triple (a,b,c) where (a<b<c) and func(b) <
    func(a),func(c).  If bracket consists of two numbers (a,c)
    then they 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.
tol : float
    Stop if between iteration change is less than `tol`.
full_output : bool
    If True, return all output args (xmin, fval, iter,
    funcalls).
maxiter : int
    Maximum number of iterations in solution.

Returns
-------
xmin : ndarray
    Optimum point.
fval : float
    Optimum value.
iter : int
    Number of iterations.
funcalls : int
    Number of objective function evaluations made.

See also
--------
minimize_scalar: Interface to minimization algorithms for scalar
    univariate functions. See the 'Brent' `method` in particular.

Notes
-----
Uses inverse parabolic interpolation when possible to speed up
convergence of golden section method.


def golden(func, args=(), brack=None, tol=_epsilon, full_output=<NUM_LIT:0>):

options = {'<STR_LIT>': tol}<EOL>res = _minimize_scalar_golden(func, brack, args, **options)<EOL>if full_output:<EOL><INDENT>return res['<STR_LIT:x>'], res['<STR_LIT>'], res['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>return res['<STR_LIT:x>']<EOL><DEDENT>

Return the minimum of a function of one variable.

Given a function of one variable and a possible bracketing interval,
return the minimum of the function isolated to a fractional precision of
tol.

Parameters
----------
func : callable func(x,*args)
    Objective function to minimize.
args : tuple
    Additional arguments (if present), passed to func.
brack : tuple
    Triple (a,b,c), where (a<b<c) and func(b) <
    func(a),func(c).  If bracket consists of two numbers (a,
    c), then they are assumed to be a starting interval for a
    downhill bracket search (see `bracket`); it doesn't always
    mean that obtained solution will satisfy a<=x<=c.
tol : float
    x tolerance stop criterion
full_output : bool
    If True, return optional outputs.

See also
--------
minimize_scalar: Interface to minimization algorithms for scalar
    univariate functions. See the 'Golden' `method` in particular.

Notes
-----
Uses analog of bisection method to decrease the bracketed
interval.


def bracket(func, xa=<NUM_LIT:0.0>, xb=<NUM_LIT:1.0>, args=(), grow_limit=<NUM_LIT>, maxiter=<NUM_LIT:1000>):

_gold = <NUM_LIT><EOL>_verysmall_num = <NUM_LIT><EOL>fa = func(*(xa,) + args)<EOL>fb = func(*(xb,) + args)<EOL>if (fa < fb):                      <EOL><INDENT>xa, xb = xb, xa<EOL>fa, fb = fb, fa<EOL><DEDENT>xc = xb + _gold * (xb - xa)<EOL>fc = func(*((xc,) + args))<EOL>funcalls = <NUM_LIT:3><EOL>iter = <NUM_LIT:0><EOL>while (fc < fb):<EOL><INDENT>tmp1 = (xb - xa) * (fb - fc)<EOL>tmp2 = (xb - xc) * (fb - fa)<EOL>val = tmp2 - tmp1<EOL>if numpy.abs(val) < _verysmall_num:<EOL><INDENT>denom = <NUM_LIT> * _verysmall_num<EOL><DEDENT>else:<EOL><INDENT>denom = <NUM_LIT> * val<EOL><DEDENT>w = xb - ((xb - xc) * tmp2 - (xb - xa) * tmp1) / denom<EOL>wlim = xb + grow_limit * (xc - xb)<EOL>if iter > maxiter:<EOL><INDENT>raise RuntimeError("<STR_LIT>")<EOL><DEDENT>iter += <NUM_LIT:1><EOL>if (w - xc) * (xb - w) > <NUM_LIT:0.0>:<EOL><INDENT>fw = func(*((w,) + args))<EOL>funcalls += <NUM_LIT:1><EOL>if (fw < fc):<EOL><INDENT>xa = xb<EOL>xb = w<EOL>fa = fb<EOL>fb = fw<EOL>return xa, xb, xc, fa, fb, fc, funcalls<EOL><DEDENT>elif (fw > fb):<EOL><INDENT>xc = w<EOL>fc = fw<EOL>return xa, xb, xc, fa, fb, fc, funcalls<EOL><DEDENT>w = xc + _gold * (xc - xb)<EOL>fw = func(*((w,) + args))<EOL>funcalls += <NUM_LIT:1><EOL><DEDENT>elif (w - wlim)*(wlim - xc) >= <NUM_LIT:0.0>:<EOL><INDENT>w = wlim<EOL>fw = func(*((w,) + args))<EOL>funcalls += <NUM_LIT:1><EOL><DEDENT>elif (w - wlim)*(xc - w) > <NUM_LIT:0.0>:<EOL><INDENT>fw = func(*((w,) + args))<EOL>funcalls += <NUM_LIT:1><EOL>if (fw < fc):<EOL><INDENT>xb = xc<EOL>xc = w<EOL>w = xc + _gold * (xc - xb)<EOL>fb = fc<EOL>fc = fw<EOL>fw = func(*((w,) + args))<EOL>funcalls += <NUM_LIT:1><EOL><DEDENT><DEDENT>else:<EOL><INDENT>w = xc + _gold * (xc - xb)<EOL>fw = func(*((w,) + args))<EOL>funcalls += <NUM_LIT:1><EOL><DEDENT>xa = xb<EOL>xb = xc<EOL>xc = w<EOL>fa = fb<EOL>fb = fc<EOL>fc = fw<EOL><DEDENT>return xa, xb, xc, fa, fb, fc, funcalls<EOL>

Bracket the minimum of the function.

Given a function and distinct initial points, search in the
downhill direction (as defined by the initital points) and return
new points xa, xb, xc that bracket the minimum of the function
f(xa) > f(xb) < f(xc). It doesn't always mean that obtained
solution will satisfy xa<=x<=xb

Parameters
----------
func : callable f(x,*args)
    Objective function to minimize.
xa, xb : float, optional
    Bracketing interval. Defaults `xa` to 0.0, and `xb` to 1.0.
args : tuple, optional
    Additional arguments (if present), passed to `func`.
grow_limit : float, optional
    Maximum grow limit.  Defaults to 110.0
maxiter : int, optional
    Maximum number of iterations to perform. Defaults to 1000.

Returns
-------
xa, xb, xc : float
    Bracket.
fa, fb, fc : float
    Objective function values in bracket.
funcalls : int
    Number of function evaluations made.


def _linesearch_powell(func, p, xi, tol=<NUM_LIT>):

def myfunc(alpha):<EOL><INDENT>return func(p + alpha*xi)<EOL><DEDENT>alpha_min, fret, iter, num = brent(myfunc, full_output=<NUM_LIT:1>, tol=tol)<EOL>xi = alpha_min*xi<EOL>return squeeze(fret), p + xi, xi<EOL>

Line-search algorithm using fminbound.

    Find the minimium of the function ``func(x0+ alpha*direc)``.


def fmin_powell(func, x0, args=(), xtol=<NUM_LIT>, ftol=<NUM_LIT>, maxiter=None,<EOL>maxfun=None, full_output=<NUM_LIT:0>, disp=<NUM_LIT:1>, retall=<NUM_LIT:0>, callback=None,<EOL>direc=None):

opts = {'<STR_LIT>': xtol,<EOL>'<STR_LIT>': ftol,<EOL>'<STR_LIT>': maxiter,<EOL>'<STR_LIT>': maxfun,<EOL>'<STR_LIT>': disp,<EOL>'<STR_LIT>': direc,<EOL>'<STR_LIT>': retall}<EOL>res = _minimize_powell(func, x0, args, callback=callback, **opts)<EOL>if full_output:<EOL><INDENT>retlist = (res['<STR_LIT:x>'], res['<STR_LIT>'], res['<STR_LIT>'], res['<STR_LIT>'],<EOL>res['<STR_LIT>'], res['<STR_LIT:status>'])<EOL>if retall:<EOL><INDENT>retlist += (res['<STR_LIT>'], )<EOL><DEDENT>return retlist<EOL><DEDENT>else:<EOL><INDENT>if retall:<EOL><INDENT>return res['<STR_LIT:x>'], res['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>return res['<STR_LIT:x>']<EOL><DEDENT><DEDENT>

Minimize a function using modified Powell's method. This method
only uses function values, not derivatives.

Parameters
----------
func : callable f(x,*args)
    Objective function to be minimized.
x0 : ndarray
    Initial guess.
args : tuple, optional
    Extra arguments passed to func.
callback : callable, optional
    An optional user-supplied function, called after each
    iteration.  Called as ``callback(xk)``, where ``xk`` is the
    current parameter vector.
direc : ndarray, optional
    Initial direction set.
xtol : float, optional
    Line-search error tolerance.
ftol : float, optional
    Relative error in ``func(xopt)`` acceptable for convergence.
maxiter : int, optional
    Maximum number of iterations to perform.
maxfun : int, optional
    Maximum number of function evaluations to make.
full_output : bool, optional
    If True, fopt, xi, direc, iter, funcalls, and
    warnflag are returned.
disp : bool, optional
    If True, print convergence messages.
retall : bool, optional
    If True, return a list of the solution at each iteration.

Returns
-------
xopt : ndarray
    Parameter which minimizes `func`.
fopt : number
    Value of function at minimum: ``fopt = func(xopt)``.
direc : ndarray
    Current direction set.
iter : int
    Number of iterations.
funcalls : int
    Number of function calls made.
warnflag : int
    Integer warning flag:
        1 : Maximum number of function evaluations.
        2 : Maximum number of iterations.
allvecs : list
    List of solutions at each iteration.

See also
--------
minimize: Interface to unconstrained minimization algorithms for
    multivariate functions. See the 'Powell' `method` in particular.

Notes
-----
Uses a modification of Powell's method to find the minimum of
a function of N variables. Powell's method is a conjugate
direction method.

The algorithm has two loops. The outer loop
merely iterates over the inner loop. The inner loop minimizes
over each current direction in the direction set. At the end
of the inner loop, if certain conditions are met, the direction
that gave the largest decrease is dropped and replaced with
the difference between the current estiamted x and the estimated
x from the beginning of the inner-loop.

The technical conditions for replacing the direction of greatest
increase amount to checking that

1. No further gain can be made along the direction of greatest increase
   from that iteration.
2. The direction of greatest increase accounted for a large sufficient
   fraction of the decrease in the function value from that iteration of
   the inner loop.

References
----------
Powell M.J.D. (1964) An efficient method for finding the minimum of a
function of several variables without calculating derivatives,
Computer Journal, 7 (2):155-162.

Press W., Teukolsky S.A., Vetterling W.T., and Flannery B.P.:
Numerical Recipes (any edition), Cambridge University Press


def _minimize_powell(func, x0, args=(), callback=None,<EOL>xtol=<NUM_LIT>, ftol=<NUM_LIT>, maxiter=None, maxfev=None,<EOL>disp=False, direc=None, return_all=False,<EOL>**unknown_options):

_check_unknown_options(unknown_options)<EOL>maxfun = maxfev<EOL>retall = return_all<EOL>fcalls, func = wrap_function(func, args)<EOL>x = asarray(x0).flatten()<EOL>if retall:<EOL><INDENT>allvecs = [x]<EOL><DEDENT>N = len(x)<EOL>if maxiter is None:<EOL><INDENT>maxiter = N * <NUM_LIT:1000><EOL><DEDENT>if maxfun is None:<EOL><INDENT>maxfun = N * <NUM_LIT:1000><EOL><DEDENT>if direc is None:<EOL><INDENT>direc = eye(N, dtype=float)<EOL><DEDENT>else:<EOL><INDENT>direc = asarray(direc, dtype=float)<EOL><DEDENT>fval = squeeze(func(x))<EOL>x1 = x.copy()<EOL>iter = <NUM_LIT:0><EOL>ilist = list(range(N))<EOL>while True:<EOL><INDENT>fx = fval<EOL>bigind = <NUM_LIT:0><EOL>delta = <NUM_LIT:0.0><EOL>for i in ilist:<EOL><INDENT>direc1 = direc[i]<EOL>fx2 = fval<EOL>fval, x, direc1 = _linesearch_powell(func, x, direc1,<EOL>tol=xtol * <NUM_LIT:100>)<EOL>if (fx2 - fval) > delta:<EOL><INDENT>delta = fx2 - fval<EOL>bigind = i<EOL><DEDENT><DEDENT>iter += <NUM_LIT:1><EOL>if callback is not None:<EOL><INDENT>callback(x)<EOL><DEDENT>if retall:<EOL><INDENT>allvecs.append(x)<EOL><DEDENT>bnd = ftol * (numpy.abs(fx) + numpy.abs(fval)) + <NUM_LIT><EOL>if <NUM_LIT> * (fx - fval) <= bnd:<EOL><INDENT>break<EOL><DEDENT>if fcalls[<NUM_LIT:0>] >= maxfun:<EOL><INDENT>break<EOL><DEDENT>if iter >= maxiter:<EOL><INDENT>break<EOL><DEDENT>direc1 = x - x1<EOL>x2 = <NUM_LIT:2>*x - x1<EOL>x1 = x.copy()<EOL>fx2 = squeeze(func(x2))<EOL>if (fx > fx2):<EOL><INDENT>t = <NUM_LIT>*(fx + fx2 - <NUM_LIT>*fval)<EOL>temp = (fx - fval - delta)<EOL>t *= temp*temp<EOL>temp = fx - fx2<EOL>t -= delta*temp*temp<EOL>if t < <NUM_LIT:0.0>:<EOL><INDENT>fval, x, direc1 = _linesearch_powell(func, x, direc1,<EOL>tol=xtol*<NUM_LIT:100>)<EOL>direc[bigind] = direc[-<NUM_LIT:1>]<EOL>direc[-<NUM_LIT:1>] = direc1<EOL><DEDENT><DEDENT><DEDENT>warnflag = <NUM_LIT:0><EOL>if fcalls[<NUM_LIT:0>] >= maxfun:<EOL><INDENT>warnflag = <NUM_LIT:1><EOL>msg = _status_message['<STR_LIT>']<EOL>if disp:<EOL><INDENT>print("<STR_LIT>" + msg)<EOL><DEDENT><DEDENT>elif iter >= maxiter:<EOL><INDENT>warnflag = <NUM_LIT:2><EOL>msg = _status_message['<STR_LIT>']<EOL>if disp:<EOL><INDENT>print("<STR_LIT>" + msg)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>msg = _status_message['<STR_LIT:success>']<EOL>if disp:<EOL><INDENT>print(msg)<EOL>print("<STR_LIT>" % fval)<EOL>print("<STR_LIT>" % iter)<EOL>print("<STR_LIT>" % fcalls[<NUM_LIT:0>])<EOL><DEDENT><DEDENT>x = squeeze(x)<EOL>result = OptimizeResult(fun=fval, direc=direc, nit=iter, nfev=fcalls[<NUM_LIT:0>],<EOL>status=warnflag, success=(warnflag == <NUM_LIT:0>),<EOL>message=msg, x=x)<EOL>if retall:<EOL><INDENT>result['<STR_LIT>'] = allvecs<EOL><DEDENT>return result<EOL>

Minimization of scalar function of one or more variables using the
modified Powell algorithm.

Options for the Powell algorithm are:
    disp : bool
        Set to True to print convergence messages.
    xtol : float
        Relative error in solution `xopt` acceptable for convergence.
    ftol : float
        Relative error in ``fun(xopt)`` acceptable for convergence.
    maxiter : int
        Maximum number of iterations to perform.
    maxfev : int
        Maximum number of function evaluations to make.
    direc : ndarray
        Initial set of direction vectors for the Powell method.

This function is called by the `minimize` function with
`method=Powell`. It is not supposed to be called directly.


def brute(func, ranges, args=(), Ns=<NUM_LIT:20>, full_output=<NUM_LIT:0>, finish=fmin,<EOL>disp=False):

N = len(ranges)<EOL>if N > <NUM_LIT>:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>lrange = list(ranges)<EOL>for k in range(N):<EOL><INDENT>if type(lrange[k]) is not type(slice(None)):<EOL><INDENT>if len(lrange[k]) < <NUM_LIT:3>:<EOL><INDENT>lrange[k] = tuple(lrange[k]) + (complex(Ns),)<EOL><DEDENT>lrange[k] = slice(*lrange[k])<EOL><DEDENT><DEDENT>if (N == <NUM_LIT:1>):<EOL><INDENT>lrange = lrange[<NUM_LIT:0>]<EOL><DEDENT>def _scalarfunc(*params):<EOL><INDENT>params = squeeze(asarray(params))<EOL>return func(params, *args)<EOL><DEDENT>vecfunc = vectorize(_scalarfunc)<EOL>grid = mgrid[lrange]<EOL>if (N == <NUM_LIT:1>):<EOL><INDENT>grid = (grid,)<EOL><DEDENT>Jout = vecfunc(*grid)<EOL>Nshape = shape(Jout)<EOL>indx = argmin(Jout.ravel(), axis=-<NUM_LIT:1>)<EOL>Nindx = zeros(N, int)<EOL>xmin = zeros(N, float)<EOL>for k in range(N - <NUM_LIT:1>, -<NUM_LIT:1>, -<NUM_LIT:1>):<EOL><INDENT>thisN = Nshape[k]<EOL>Nindx[k] = indx % Nshape[k]<EOL>indx = indx // thisN<EOL><DEDENT>for k in range(N):<EOL><INDENT>xmin[k] = grid[k][tuple(Nindx)]<EOL><DEDENT>Jmin = Jout[tuple(Nindx)]<EOL>if (N == <NUM_LIT:1>):<EOL><INDENT>grid = grid[<NUM_LIT:0>]<EOL>xmin = xmin[<NUM_LIT:0>]<EOL><DEDENT>if callable(finish):<EOL><INDENT>vals = finish(func, xmin, args=args, full_output=<NUM_LIT:1>, disp=disp)<EOL>xmin = vals[<NUM_LIT:0>]<EOL>Jmin = vals[<NUM_LIT:1>]<EOL>if vals[-<NUM_LIT:1>] > <NUM_LIT:0>:<EOL><INDENT>if disp:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT><DEDENT><DEDENT>if full_output:<EOL><INDENT>return xmin, Jmin, grid, Jout<EOL><DEDENT>else:<EOL><INDENT>return xmin<EOL><DEDENT>

Minimize a function over a given range by brute force.

    Uses the "brute force" method, i.e. computes the function's value
    at each point of a multidimensional grid of points, to find the global
    minimum of the function.

    The function is evaluated everywhere in the range with the datatype of the
    first call to the function, as enforced by the ``vectorize`` NumPy
    function.  The value and type of the function evaluation returned when
    ``full_output=True`` are affected in addition by the ``finish`` argument
    (see Notes).

    Parameters
    ----------
    func : callable
        The objective function to be minimized. Must be in the
        form ``f(x, *args)``, where ``x`` is the argument in
        the form of a 1-D array and ``args`` is a tuple of any
        additional fixed parameters needed to completely specify
        the function.
    ranges : tuple
        Each component of the `ranges` tuple must be either a
        "slice object" or a range tuple of the form ``(low, high)``.
        The program uses these to create the grid of points on which
        the objective function will be computed. See `Note 2` for
        more detail.
    args : tuple, optional
        Any additional fixed parameters needed to completely specify
        the function.
    Ns : int, optional
        Number of grid points along the axes, if not otherwise
        specified. See `Note2`.
    full_output : bool, optional
        If True, return the evaluation grid and the objective function's
        values on it.
    finish : callable, optional
        An optimization function that is called with the result of brute force
        minimization as initial guess.  `finish` should take the initial guess
        as positional argument, and take `args`, `full_output` and `disp`
        as keyword arguments.  Use None if no "polishing" function is to be
        used.  See Notes for more details.
    disp : bool, optional
        Set to True to print convergence messages.

    Returns
    -------
    x0 : ndarray
        A 1-D array containing the coordinates of a point at which the
        objective function had its minimum value. (See `Note 1` for
        which point is returned.)
    fval : float
        Function value at the point `x0`. (Returned when `full_output` is
        True.)
    grid : tuple
        Representation of the evaluation grid.  It has the same
        length as `x0`. (Returned when `full_output` is True.)
    Jout : ndarray
        Function values at each point of the evaluation
        grid, `i.e.`, ``Jout = func(*grid)``. (Returned
        when `full_output` is True.)

    See Also
    --------
    anneal : Another approach to seeking the global minimum of
    multivariate, multimodal functions.

    Notes
    -----
    *Note 1*: The program finds the gridpoint at which the lowest value
    of the objective function occurs.  If `finish` is None, that is the
    point returned.  When the global minimum occurs within (or not very far
    outside) the grid's boundaries, and the grid is fine enough, that
    point will be in the neighborhood of the gobal minimum.

    However, users often employ some other optimization program to
    "polish" the gridpoint values, `i.e.`, to seek a more precise
    (local) minimum near `brute's` best gridpoint.
    The `brute` function's `finish` option provides a convenient way to do
    that.  Any polishing program used must take `brute's` output as its
    initial guess as a positional argument, and take `brute's` input values
    for `args` and `full_output` as keyword arguments, otherwise an error
    will be raised.

    `brute` assumes that the `finish` function returns a tuple in the form:
    ``(xmin, Jmin, ... , statuscode)``, where ``xmin`` is the minimizing value
    of the argument, ``Jmin`` is the minimum value of the objective function,
    "..." may be some other returned values (which are not used by `brute`),
    and ``statuscode`` is the status code of the `finish` program.

    Note that when `finish` is not None, the values returned are those
    of the `finish` program, *not* the gridpoint ones.  Consequently,
    while `brute` confines its search to the input grid points,
    the `finish` program's results usually will not coincide with any
    gridpoint, and may fall outside the grid's boundary.

    *Note 2*: The grid of points is a `numpy.mgrid` object.
    For `brute` the `ranges` and `Ns` inputs have the following effect.
    Each component of the `ranges` tuple can be either a slice object or a
    two-tuple giving a range of values, such as (0, 5).  If the component is a
    slice object, `brute` uses it directly.  If the component is a two-tuple
    range, `brute` internally converts it to a slice object that interpolates
    `Ns` points from its low-value to its high-value, inclusive.

    Examples
    --------
    We illustrate the use of `brute` to seek the global minimum of a function
    of two variables that is given as the sum of a positive-definite
    quadratic and two deep "Gaussian-shaped" craters.  Specifically, define
    the objective function `f` as the sum of three other functions,
    ``f = f1 + f2 + f3``.  We suppose each of these has a signature
    ``(z, *params)``, where ``z = (x, y)``,  and ``params`` and the functions
    are as defined below.

    >>> params = (2, 3, 7, 8, 9, 10, 44, -1, 2, 26, 1, -2, 0.5)
    >>> def f1(z, *params):
    ...     x, y = z
    ...     a, b, c, d, e, f, g, h, i, j, k, l, scale = params
    ...     return (a * x**2 + b * x * y + c * y**2 + d*x + e*y + f)

    >>> def f2(z, *params):
    ...     x, y = z
    ...     a, b, c, d, e, f, g, h, i, j, k, l, scale = params
    ...     return (-g*np.exp(-((x-h)**2 + (y-i)**2) / scale))

    >>> def f3(z, *params):
    ...     x, y = z
    ...     a, b, c, d, e, f, g, h, i, j, k, l, scale = params
    ...     return (-j*np.exp(-((x-k)**2 + (y-l)**2) / scale))

    >>> def f(z, *params):
    ...     x, y = z
    ...     a, b, c, d, e, f, g, h, i, j, k, l, scale = params
    ...     return f1(z, *params) + f2(z, *params) + f3(z, *params)

    Thus, the objective function may have local minima near the minimum
    of each of the three functions of which it is composed.  To
    use `fmin` to polish its gridpoint result, we may then continue as
    follows:

    >>> rranges = (slice(-4, 4, 0.25), slice(-4, 4, 0.25))
    >>> from scipy import optimize
    >>> resbrute = optimize.brute(f, rranges, args=params, full_output=True,
                                  finish=optimize.fmin)
    >>> resbrute[0]  # global minimum
    array([-1.05665192,  1.80834843])
    >>> resbrute[1]  # function value at global minimum
    -3.4085818767

    Note that if `finish` had been set to None, we would have gotten the
    gridpoint [-1.0 1.75] where the rounded function value is -2.892.


import textwrap<EOL>if solver is None:<EOL><INDENT>print("<STR_LIT>")<EOL>print("<STR_LIT>")<EOL>show_options('<STR_LIT>')<EOL>print("<STR_LIT>")<EOL>print("<STR_LIT>")<EOL>show_options('<STR_LIT>')<EOL>print("<STR_LIT>")<EOL>print("<STR_LIT>")<EOL>show_options('<STR_LIT:root>')<EOL>print('<STR_LIT>')<EOL>print('<STR_LIT>')<EOL>show_options('<STR_LIT>')<EOL>return<EOL><DEDENT>solver = solver.lower()<EOL>if solver not in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT:root>', '<STR_LIT>'):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>solvers_doc = [s.strip()<EOL>for s in show_options.__doc__.split('<STR_LIT>')[<NUM_LIT:1>:]]<EOL>solver_doc = [s for s in solvers_doc<EOL>if s.lower().startswith(solver + "<STR_LIT>")]<EOL>if method is None:<EOL><INDENT>doc = ['<STR_LIT>'] + solver_doc<EOL><DEDENT>else:<EOL><INDENT>doc = solver_doc[<NUM_LIT:0>].split('<STR_LIT>')[<NUM_LIT:1>:]<EOL>doc = ['<STR_LIT>'] + [s for s in doc if s.lower().startswith(method.lower())]<EOL><DEDENT>print(textwrap.dedent('<STR_LIT>'.join(doc)).rstrip())<EOL>return<EOL>

Show documentation for additional options of optimization solvers.

These are method-specific options that can be supplied through the
``options`` dict.

Parameters
----------
solver : str
    Type of optimization solver. One of 'minimize', 'minimize_scalar',
    'root', or 'linprog'.
method : str, optional
    If not given, shows all methods of the specified solver. Otherwise,
    show only the options for the specified method. Valid values
    corresponds to methods' names of respective solver (e.g. 'BFGS' for
    'minimize').

Notes
-----

**Minimize options**

*BFGS* options:

    gtol : float
        Gradient norm must be less than `gtol` before successful
        termination.
    norm : float
        Order of norm (Inf is max, -Inf is min).
    eps : float or ndarray
        If `jac` is approximated, use this value for the step size.

*Nelder-Mead* options:

    xtol : float
        Relative error in solution `xopt` acceptable for convergence.
    ftol : float
        Relative error in ``fun(xopt)`` acceptable for convergence.
    maxfev : int
        Maximum number of function evaluations to make.

*Newton-CG* options:

    xtol : float
        Average relative error in solution `xopt` acceptable for
        convergence.
    eps : float or ndarray
        If `jac` is approximated, use this value for the step size.

*CG* options:

    gtol : float
        Gradient norm must be less than `gtol` before successful
        termination.
    norm : float
        Order of norm (Inf is max, -Inf is min).
    eps : float or ndarray
        If `jac` is approximated, use this value for the step size.

*Powell* options:

    xtol : float
        Relative error in solution `xopt` acceptable for convergence.
    ftol : float
        Relative error in ``fun(xopt)`` acceptable for convergence.
    maxfev : int
        Maximum number of function evaluations to make.
    direc : ndarray
        Initial set of direction vectors for the Powell method.

*Anneal* options:

    ftol : float
        Relative error in ``fun(x)`` acceptable for convergence.
    schedule : str
        Annealing schedule to use. One of: 'fast', 'cauchy' or
        'boltzmann'.
    T0 : float
        Initial Temperature (estimated as 1.2 times the largest
        cost-function deviation over random points in the range).
    Tf : float
        Final goal temperature.
    maxfev : int
        Maximum number of function evaluations to make.
    maxaccept : int
        Maximum changes to accept.
    boltzmann : float
        Boltzmann constant in acceptance test (increase for less
        stringent test at each temperature).
    learn_rate : float
        Scale constant for adjusting guesses.
    quench, m, n : float
        Parameters to alter fast_sa schedule.
    lower, upper : float or ndarray
        Lower and upper bounds on `x`.
    dwell : int
        The number of times to search the space at each temperature.

*L-BFGS-B* options:

    ftol : float
        The iteration stops when ``(f^k -
        f^{k+1})/max{|f^k|,|f^{k+1}|,1} <= ftol``.
    gtol : float
        The iteration will stop when ``max{|proj g_i | i = 1, ..., n}
        <= gtol`` where ``pg_i`` is the i-th component of the
        projected gradient.
    eps : float or ndarray
        If `jac` is approximated, use this value for the step size.
    maxcor : int
        The maximum number of variable metric corrections used to
        define the limited memory matrix. (The limited memory BFGS
        method does not store the full hessian but uses this many terms
        in an approximation to it.)
    maxfun : int
        Maximum number of function evaluations.
    maxiter : int
        Maximum number of iterations.

*TNC* options:

    ftol : float
        Precision goal for the value of f in the stoping criterion.
        If ftol < 0.0, ftol is set to 0.0 defaults to -1.
    xtol : float
        Precision goal for the value of x in the stopping
        criterion (after applying x scaling factors).  If xtol <
        0.0, xtol is set to sqrt(machine_precision).  Defaults to
        -1.
    gtol : float
        Precision goal for the value of the projected gradient in
        the stopping criterion (after applying x scaling factors).
        If gtol < 0.0, gtol is set to 1e-2 * sqrt(accuracy).
        Setting it to 0.0 is not recommended.  Defaults to -1.
    scale : list of floats
        Scaling factors to apply to each variable.  If None, the
        factors are up-low for interval bounded variables and
        1+|x] fo the others.  Defaults to None
    offset : float
        Value to subtract from each variable.  If None, the
        offsets are (up+low)/2 for interval bounded variables
        and x for the others.
    maxCGit : int
        Maximum number of hessian*vector evaluations per main
        iteration.  If maxCGit == 0, the direction chosen is
        -gradient if maxCGit < 0, maxCGit is set to
        max(1,min(50,n/2)).  Defaults to -1.
    maxiter : int
        Maximum number of function evaluation.  if None, `maxiter` is
        set to max(100, 10*len(x0)).  Defaults to None.
    eta : float
        Severity of the line search. if < 0 or > 1, set to 0.25.
        Defaults to -1.
    stepmx : float
        Maximum step for the line search.  May be increased during
        call.  If too small, it will be set to 10.0.  Defaults to 0.
    accuracy : float
        Relative precision for finite difference calculations.  If
        <= machine_precision, set to sqrt(machine_precision).
        Defaults to 0.
    minfev : float
        Minimum function value estimate.  Defaults to 0.
    rescale : float
        Scaling factor (in log10) used to trigger f value
        rescaling.  If 0, rescale at each iteration.  If a large
        value, never rescale.  If < 0, rescale is set to 1.3.

*COBYLA* options:

    tol : float
        Final accuracy in the optimization (not precisely guaranteed).
        This is a lower bound on the size of the trust region.
    rhobeg : float
        Reasonable initial changes to the variables.
    maxfev : int
        Maximum number of function evaluations.
    catol : float
        Absolute tolerance for constraint violations (default: 1e-6).

*SLSQP* options:

    ftol : float
        Precision goal for the value of f in the stopping criterion.
    eps : float
        Step size used for numerical approximation of the jacobian.
    maxiter : int
        Maximum number of iterations.

*dogleg* options:

    initial_trust_radius : float
        Initial trust-region radius.
    max_trust_radius : float
        Maximum value of the trust-region radius. No steps that are longer
        than this value will be proposed.
    eta : float
        Trust region related acceptance stringency for proposed steps.
    gtol : float
        Gradient norm must be less than `gtol` before successful
        termination.

*trust-ncg* options:

    See dogleg options.

**minimize_scalar options**

*brent* options:

    xtol : float

        Relative error in solution `xopt` acceptable for convergence.

*bounded* options:

    xatol : float
        Absolute error in solution `xopt` acceptable for convergence.

*golden* options:

    xtol : float
        Relative error in solution `xopt` acceptable for convergence.

**root options**

*hybrd* options:

    col_deriv : bool
        Specify whether the Jacobian function computes derivatives down
        the columns (faster, because there is no transpose operation).
    xtol : float
        The calculation will terminate if the relative error between
        two consecutive iterates is at most `xtol`.
    maxfev : int
        The maximum number of calls to the function. If zero, then
        ``100*(N+1)`` is the maximum where N is the number of elements
        in `x0`.
    band : sequence
        If set to a two-sequence containing the number of sub- and
        super-diagonals within the band of the Jacobi matrix, the
        Jacobi matrix is considered banded (only for ``fprime=None``).
    epsfcn : float
        A suitable step length for the forward-difference approximation
        of the Jacobian (for ``fprime=None``). If `epsfcn` is less than
        the machine precision, it is assumed that the relative errors
        in the functions are of the order of the machine precision.
    factor : float
        A parameter determining the initial step bound (``factor * ||
        diag * x||``).  Should be in the interval ``(0.1, 100)``.
    diag : sequence
        N positive entries that serve as a scale factors for the
        variables.

*LM* options:

    col_deriv : bool
        non-zero to specify that the Jacobian function computes derivatives
        down the columns (faster, because there is no transpose operation).
    ftol : float
        Relative error desired in the sum of squares.
    xtol : float
        Relative error desired in the approximate solution.
    gtol : float
        Orthogonality desired between the function vector and the columns
        of the Jacobian.
    maxiter : int
        The maximum number of calls to the function. If zero, then
        100*(N+1) is the maximum where N is the number of elements in x0.
    epsfcn : float
        A suitable step length for the forward-difference approximation of
        the Jacobian (for Dfun=None). If epsfcn is less than the machine
        precision, it is assumed that the relative errors in the functions
        are of the order of the machine precision.
    factor : float
        A parameter determining the initial step bound
        (``factor * || diag * x||``). Should be in interval ``(0.1, 100)``.
    diag : sequence
        N positive entries that serve as a scale factors for the variables.

*Broyden1* options:

    nit : int, optional
        Number of iterations to make. If omitted (default), make as many
        as required to meet tolerances.
    disp : bool, optional
        Print status to stdout on every iteration.
    maxiter : int, optional
        Maximum number of iterations to make. If more are needed to
        meet convergence, `NoConvergence` is raised.
    ftol : float, optional
        Relative tolerance for the residual. If omitted, not used.
    fatol : float, optional
        Absolute tolerance (in max-norm) for the residual.
        If omitted, default is 6e-6.
    xtol : float, optional
        Relative minimum step size. If omitted, not used.
    xatol : float, optional
        Absolute minimum step size, as determined from the Jacobian
        approximation. If the step size is smaller than this, optimization
        is terminated as successful. If omitted, not used.
    tol_norm : function(vector) -> scalar, optional
        Norm to use in convergence check. Default is the maximum norm.
    line_search : {None, 'armijo' (default), 'wolfe'}, optional
        Which type of a line search to use to determine the step size in
        the direction given by the Jacobian approximation. Defaults to
        'armijo'.
    jac_options : dict, optional
        Options for the respective Jacobian approximation.
            alpha : float, optional
                Initial guess for the Jacobian is (-1/alpha).
            reduction_method : str or tuple, optional
                Method used in ensuring that the rank of the Broyden
                matrix stays low. Can either be a string giving the
                name of the method, or a tuple of the form ``(method,
                param1, param2, ...)`` that gives the name of the
                method and values for additional parameters.

                Methods available:
                    - ``restart``: drop all matrix columns. Has no
                        extra parameters.
                    - ``simple``: drop oldest matrix column. Has no
                        extra parameters.
                    - ``svd``: keep only the most significant SVD
                        components.
                      Extra parameters:
                          - ``to_retain`: number of SVD components to
                              retain when rank reduction is done.
                              Default is ``max_rank - 2``.
            max_rank : int, optional
                Maximum rank for the Broyden matrix.
                Default is infinity (ie., no rank reduction).

*Broyden2* options:

    nit : int, optional
        Number of iterations to make. If omitted (default), make as many
        as required to meet tolerances.
    disp : bool, optional
        Print status to stdout on every iteration.
    maxiter : int, optional
        Maximum number of iterations to make. If more are needed to
        meet convergence, `NoConvergence` is raised.
    ftol : float, optional
        Relative tolerance for the residual. If omitted, not used.
    fatol : float, optional
        Absolute tolerance (in max-norm) for the residual.
        If omitted, default is 6e-6.
    xtol : float, optional
        Relative minimum step size. If omitted, not used.
    xatol : float, optional
        Absolute minimum step size, as determined from the Jacobian
        approximation. If the step size is smaller than this, optimization
        is terminated as successful. If omitted, not used.
    tol_norm : function(vector) -> scalar, optional
        Norm to use in convergence check. Default is the maximum norm.
    line_search : {None, 'armijo' (default), 'wolfe'}, optional
        Which type of a line search to use to determine the step size in
        the direction given by the Jacobian approximation. Defaults to
        'armijo'.
    jac_options : dict, optional
        Options for the respective Jacobian approximation.

        alpha : float, optional
            Initial guess for the Jacobian is (-1/alpha).
        reduction_method : str or tuple, optional
            Method used in ensuring that the rank of the Broyden
            matrix stays low. Can either be a string giving the
            name of the method, or a tuple of the form ``(method,
            param1, param2, ...)`` that gives the name of the
            method and values for additional parameters.

            Methods available:
                - ``restart``: drop all matrix columns. Has no
                    extra parameters.
                - ``simple``: drop oldest matrix column. Has no
                    extra parameters.
                - ``svd``: keep only the most significant SVD
                    components.
                  Extra parameters:
                      - ``to_retain`: number of SVD components to
                          retain when rank reduction is done.
                          Default is ``max_rank - 2``.
        max_rank : int, optional
            Maximum rank for the Broyden matrix.
            Default is infinity (ie., no rank reduction).

*Anderson* options:

    nit : int, optional
        Number of iterations to make. If omitted (default), make as many
        as required to meet tolerances.
    disp : bool, optional
        Print status to stdout on every iteration.
    maxiter : int, optional
        Maximum number of iterations to make. If more are needed to
        meet convergence, `NoConvergence` is raised.
    ftol : float, optional
        Relative tolerance for the residual. If omitted, not used.
    fatol : float, optional
        Absolute tolerance (in max-norm) for the residual.
        If omitted, default is 6e-6.
    xtol : float, optional
        Relative minimum step size. If omitted, not used.
    xatol : float, optional
        Absolute minimum step size, as determined from the Jacobian
        approximation. If the step size is smaller than this, optimization
        is terminated as successful. If omitted, not used.
    tol_norm : function(vector) -> scalar, optional
        Norm to use in convergence check. Default is the maximum norm.
    line_search : {None, 'armijo' (default), 'wolfe'}, optional
        Which type of a line search to use to determine the step size in
        the direction given by the Jacobian approximation. Defaults to
        'armijo'.
    jac_options : dict, optional
        Options for the respective Jacobian approximation.

        alpha : float, optional
            Initial guess for the Jacobian is (-1/alpha).
        M : float, optional
            Number of previous vectors to retain. Defaults to 5.
        w0 : float, optional
            Regularization parameter for numerical stability.
            Compared to unity, good values of the order of 0.01.

*LinearMixing* options:

    nit : int, optional
        Number of iterations to make. If omitted (default), make as many
        as required to meet tolerances.
    disp : bool, optional
        Print status to stdout on every iteration.
    maxiter : int, optional
        Maximum number of iterations to make. If more are needed to
        meet convergence, `NoConvergence` is raised.
    ftol : float, optional
        Relative tolerance for the residual. If omitted, not used.
    fatol : float, optional
        Absolute tolerance (in max-norm) for the residual.
        If omitted, default is 6e-6.
    xtol : float, optional
        Relative minimum step size. If omitted, not used.
    xatol : float, optional
        Absolute minimum step size, as determined from the Jacobian
        approximation. If the step size is smaller than this, optimization
        is terminated as successful. If omitted, not used.
    tol_norm : function(vector) -> scalar, optional
        Norm to use in convergence check. Default is the maximum norm.
    line_search : {None, 'armijo' (default), 'wolfe'}, optional
        Which type of a line search to use to determine the step size in
        the direction given by the Jacobian approximation. Defaults to
        'armijo'.
    jac_options : dict, optional
        Options for the respective Jacobian approximation.

        alpha : float, optional
            initial guess for the jacobian is (-1/alpha).

*DiagBroyden* options:

    nit : int, optional
        Number of iterations to make. If omitted (default), make as many
        as required to meet tolerances.
    disp : bool, optional
        Print status to stdout on every iteration.
    maxiter : int, optional
        Maximum number of iterations to make. If more are needed to
        meet convergence, `NoConvergence` is raised.
    ftol : float, optional
        Relative tolerance for the residual. If omitted, not used.
    fatol : float, optional
        Absolute tolerance (in max-norm) for the residual.
        If omitted, default is 6e-6.
    xtol : float, optional
        Relative minimum step size. If omitted, not used.
    xatol : float, optional
        Absolute minimum step size, as determined from the Jacobian
        approximation. If the step size is smaller than this, optimization
        is terminated as successful. If omitted, not used.
    tol_norm : function(vector) -> scalar, optional
        Norm to use in convergence check. Default is the maximum norm.
    line_search : {None, 'armijo' (default), 'wolfe'}, optional
        Which type of a line search to use to determine the step size in
        the direction given by the Jacobian approximation. Defaults to
        'armijo'.
    jac_options : dict, optional
        Options for the respective Jacobian approximation.

        alpha : float, optional
            initial guess for the jacobian is (-1/alpha).

*ExcitingMixing* options:

    nit : int, optional
        Number of iterations to make. If omitted (default), make as many
        as required to meet tolerances.
    disp : bool, optional
        Print status to stdout on every iteration.
    maxiter : int, optional
        Maximum number of iterations to make. If more are needed to
        meet convergence, `NoConvergence` is raised.
    ftol : float, optional
        Relative tolerance for the residual. If omitted, not used.
    fatol : float, optional
        Absolute tolerance (in max-norm) for the residual.
        If omitted, default is 6e-6.
    xtol : float, optional
        Relative minimum step size. If omitted, not used.
    xatol : float, optional
        Absolute minimum step size, as determined from the Jacobian
        approximation. If the step size is smaller than this, optimization
        is terminated as successful. If omitted, not used.
    tol_norm : function(vector) -> scalar, optional
        Norm to use in convergence check. Default is the maximum norm.
    line_search : {None, 'armijo' (default), 'wolfe'}, optional
        Which type of a line search to use to determine the step size in
        the direction given by the Jacobian approximation. Defaults to
        'armijo'.
    jac_options : dict, optional
        Options for the respective Jacobian approximation.

        alpha : float, optional
            Initial Jacobian approximation is (-1/alpha).
        alphamax : float, optional
            The entries of the diagonal Jacobian are kept in the range
            ``[alpha, alphamax]``.

*Krylov* options:

    nit : int, optional
        Number of iterations to make. If omitted (default), make as many
        as required to meet tolerances.
    disp : bool, optional
        Print status to stdout on every iteration.
    maxiter : int, optional
        Maximum number of iterations to make. If more are needed to
        meet convergence, `NoConvergence` is raised.
    ftol : float, optional
        Relative tolerance for the residual. If omitted, not used.
    fatol : float, optional
        Absolute tolerance (in max-norm) for the residual.
        If omitted, default is 6e-6.
    xtol : float, optional
        Relative minimum step size. If omitted, not used.
    xatol : float, optional
        Absolute minimum step size, as determined from the Jacobian
        approximation. If the step size is smaller than this, optimization
        is terminated as successful. If omitted, not used.
    tol_norm : function(vector) -> scalar, optional
        Norm to use in convergence check. Default is the maximum norm.
    line_search : {None, 'armijo' (default), 'wolfe'}, optional
        Which type of a line search to use to determine the step size in
        the direction given by the Jacobian approximation. Defaults to
        'armijo'.
    jac_options : dict, optional
        Options for the respective Jacobian approximation.

        rdiff : float, optional
            Relative step size to use in numerical differentiation.
        method : {'lgmres', 'gmres', 'bicgstab', 'cgs', 'minres'} or function
            Krylov method to use to approximate the Jacobian.
            Can be a string, or a function implementing the same
            interface as the iterative solvers in
            `scipy.sparse.linalg`.

            The default is `scipy.sparse.linalg.lgmres`.
        inner_M : LinearOperator or InverseJacobian
            Preconditioner for the inner Krylov iteration.
            Note that you can use also inverse Jacobians as (adaptive)
            preconditioners. For example,

            >>> jac = BroydenFirst()
            >>> kjac = KrylovJacobian(inner_M=jac.inverse).

            If the preconditioner has a method named 'update', it will
            be called as ``update(x, f)`` after each nonlinear step,
            with ``x`` giving the current point, and ``f`` the current
            function value.
        inner_tol, inner_maxiter, ...
            Parameters to pass on to the "inner" Krylov solver.
            See `scipy.sparse.linalg.gmres` for details.
        outer_k : int, optional
            Size of the subspace kept across LGMRES nonlinear
            iterations.

            See `scipy.sparse.linalg.lgmres` for details.

**linprog options**

*simplex* options:

    maxiter : int, optional
        Maximum number of iterations to make.

    tol : float, optional
        The tolerance which determines when the Phase 1 objective is
        sufficiently close to zero to be considered a basic feasible
        solution or when the Phase 2 objective coefficients are close
        enough to positive for the objective to be considered optimal.

    bland : bool, optional
        If True, choose pivots using Bland's rule.  In problems which
        fail to converge due to cycling, using Bland's rule can provide
        convergence at the expense of a less optimal path about the simplex.


<EOL>x_after_step = np.copy(self.x)<EOL>x_after_step = self.step_taking(x_after_step)<EOL>minres = self.minimizer(x_after_step)<EOL>x_after_quench = minres.x<EOL>energy_after_quench = minres.fun<EOL>if not minres.success:<EOL><INDENT>self.res.minimization_failures += <NUM_LIT:1><EOL>if self.disp:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT><DEDENT>if hasattr(minres, "<STR_LIT>"):<EOL><INDENT>self.res.nfev += minres.nfev<EOL><DEDENT>if hasattr(minres, "<STR_LIT>"):<EOL><INDENT>self.res.njev += minres.njev<EOL><DEDENT>if hasattr(minres, "<STR_LIT>"):<EOL><INDENT>self.res.nhev += minres.nhev<EOL><DEDENT>accept = True<EOL>for test in self.accept_tests:<EOL><INDENT>testres = test(f_new=energy_after_quench, x_new=x_after_quench,<EOL>f_old=self.energy, x_old=self.x)<EOL>if isinstance(testres, bool):<EOL><INDENT>if not testres:<EOL><INDENT>accept = False<EOL><DEDENT><DEDENT>elif isinstance(testres, str):<EOL><INDENT>if testres == "<STR_LIT>":<EOL><INDENT>accept = True<EOL>break<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>", type(testres))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>", type(testres))<EOL><DEDENT><DEDENT>if hasattr(self.step_taking, "<STR_LIT>"):<EOL><INDENT>self.step_taking.report(accept, f_new=energy_after_quench,<EOL>x_new=x_after_quench, f_old=self.energy,<EOL>x_old=self.x)<EOL><DEDENT>return x_after_quench, energy_after_quench, accept<EOL>

Do one monte carlo iteration

        Randomly displace the coordinates, minimize, and decide whether
        or not to accept the new coordinates.


self.nstep += <NUM_LIT:1><EOL>new_global_min = False<EOL>xtrial, energy_trial, accept = self._monte_carlo_step()<EOL>if accept:<EOL><INDENT>self.energy = energy_trial<EOL>self.x = np.copy(xtrial)<EOL>new_global_min = self.storage.update(self.x, self.energy)<EOL><DEDENT>if self.disp:<EOL><INDENT>self.print_report(energy_trial, accept)<EOL>if new_global_min:<EOL><INDENT>print("<STR_LIT>"<EOL>"<STR_LIT>" % (self.nstep, self.energy))<EOL><DEDENT><DEDENT>self.xtrial = xtrial<EOL>self.energy_trial = energy_trial<EOL>self.accept = accept<EOL>return new_global_min<EOL>

Do one cycle of the basinhopping algorithm


def print_report(self, energy_trial, accept):

xlowest, energy_lowest = self.storage.get_lowest()<EOL>print("<STR_LIT>"<EOL>"<STR_LIT>" % (self.nstep, self.energy, energy_trial,<EOL>accept, energy_lowest))<EOL>


if accept:<EOL><INDENT>self.naccept += <NUM_LIT:1><EOL><DEDENT>

called by basinhopping to report the result of the step


return bool(self.accept_reject(kwargs["<STR_LIT>"],<EOL>kwargs["<STR_LIT>"]))<EOL>


def root(fun, x0, args=(), method='<STR_LIT>', jac=None, tol=None, callback=None,<EOL>options=None):

if not isinstance(args, tuple):<EOL><INDENT>args = (args,)<EOL><DEDENT>meth = method.lower()<EOL>if options is None:<EOL><INDENT>options = {}<EOL><DEDENT>if callback is not None and meth in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>warn('<STR_LIT>' % method,<EOL>RuntimeWarning)<EOL><DEDENT>if not callable(jac) and meth in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>if bool(jac):<EOL><INDENT>fun = MemoizeJac(fun)<EOL>jac = fun.derivative<EOL><DEDENT>else:<EOL><INDENT>jac = None<EOL><DEDENT><DEDENT>if tol is not None:<EOL><INDENT>options = dict(options)<EOL>if meth in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>options.setdefault('<STR_LIT>', tol)<EOL><DEDENT>elif meth in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>options.setdefault('<STR_LIT>', tol)<EOL>options.setdefault('<STR_LIT>', np.inf)<EOL>options.setdefault('<STR_LIT>', np.inf)<EOL>options.setdefault('<STR_LIT>', np.inf)<EOL><DEDENT><DEDENT>if meth == '<STR_LIT>':<EOL><INDENT>sol = _root_hybr(fun, x0, args=args, jac=jac, **options)<EOL><DEDENT>elif meth == '<STR_LIT>':<EOL><INDENT>sol = _root_leastsq(fun, x0, args=args, jac=jac, **options)<EOL><DEDENT>elif meth in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>if jac is not None:<EOL><INDENT>warn('<STR_LIT>' % method,<EOL>RuntimeWarning)<EOL><DEDENT>sol = _root_nonlin_solve(fun, x0, args=args, jac=jac,<EOL>_method=meth, _callback=callback,<EOL>**options)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>' % method)<EOL><DEDENT>return sol<EOL>

Find a root of a vector function.

Parameters
----------
fun : callable
    A vector function to find a root of.
x0 : ndarray
    Initial guess.
args : tuple, optional
    Extra arguments passed to the objective function and its Jacobian.
method : str, optional
    Type of solver.  Should be one of

        - 'hybr'
        - 'lm'
        - 'broyden1'
        - 'broyden2'
        - 'anderson'
        - 'linearmixing'
        - 'diagbroyden'
        - 'excitingmixing'
        - 'krylov'

jac : bool or callable, optional
    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 `fun`. In
    this case, it must accept the same arguments as `fun`.
tol : float, optional
    Tolerance for termination. For detailed control, use solver-specific
    options.
callback : function, optional
    Optional callback function. It is called on every iteration as
    ``callback(x, f)`` where `x` is the current solution and `f`
    the corresponding residual. For all methods but 'hybr' and 'lm'.
options : dict, optional
    A dictionary of solver options. E.g. `xtol` or `maxiter`, see
    :obj:`show_options()` for details.

Returns
-------
sol : OptimizeResult
    The solution represented as a ``OptimizeResult`` object.
    Important attributes are: ``x`` the solution array, ``success`` a
    Boolean flag indicating if the algorithm exited successfully and
    ``message`` which describes the cause of the termination. See
    `OptimizeResult` for a description of other attributes.

See also
--------
show_options : Additional options accepted by the solvers

Notes
-----
This section describes the available solvers that can be selected by the
'method' parameter. The default method is *hybr*.

Method *hybr* uses a modification of the Powell hybrid method as
implemented in MINPACK [1]_.

Method *lm* solves the system of nonlinear equations in a least squares
sense using a modification of the Levenberg-Marquardt algorithm as
implemented in MINPACK [1]_.

Methods *broyden1*, *broyden2*, *anderson*, *linearmixing*,
*diagbroyden*, *excitingmixing*, *krylov* are inexact Newton methods,
with backtracking or full line searches [2]_. Each method corresponds
to a particular Jacobian approximations. See `nonlin` for details.

- Method *broyden1* uses Broyden's first Jacobian approximation, it is
  known as Broyden's good method.
- Method *broyden2* uses Broyden's second Jacobian approximation, it
  is known as Broyden's bad method.
- Method *anderson* uses (extended) Anderson mixing.
- Method *Krylov* uses Krylov approximation for inverse Jacobian. It
  is suitable for large-scale problem.
- Method *diagbroyden* uses diagonal Broyden Jacobian approximation.
- Method *linearmixing* uses a scalar Jacobian approximation.
- Method *excitingmixing* uses a tuned diagonal Jacobian
  approximation.

.. warning::

    The algorithms implemented for methods *diagbroyden*,
    *linearmixing* and *excitingmixing* may be useful for specific
    problems, but whether they will work may depend strongly on the
    problem.

.. versionadded:: 0.11.0

References
----------
.. [1] More, Jorge J., Burton S. Garbow, and Kenneth E. Hillstrom.
   1980. User Guide for MINPACK-1.
.. [2] C. T. Kelley. 1995. Iterative Methods for Linear and Nonlinear
    Equations. Society for Industrial and Applied Mathematics.
    <http://www.siam.org/books/kelley/>

Examples
--------
The following functions define a system of nonlinear equations and its
jacobian.

>>> def fun(x):
...     return [x[0]  + 0.5 * (x[0] - x[1])**3 - 1.0,
...             0.5 * (x[1] - x[0])**3 + x[1]]

>>> def jac(x):
...     return np.array([[1 + 1.5 * (x[0] - x[1])**2,
...                       -1.5 * (x[0] - x[1])**2],
...                      [-1.5 * (x[1] - x[0])**2,
...                       1 + 1.5 * (x[1] - x[0])**2]])

A solution can be obtained as follows.

>>> from scipy import optimize
>>> sol = optimize.root(fun, [0, 0], jac=jac, method='hybr')
>>> sol.x
array([ 0.8411639,  0.1588361])