code: def _minimize_bfgs(
      fun,
      x0,
      args=(),
      jac=None,
      callback=None,
      gtol=1e-5,
      norm=Inf,
      eps=_epsilon,
      maxiter=None,
      disp=False,
      return_all=False,
      **unknown_options,
  ):
      """
      Minimization of scalar function of one or more variables using the
      BFGS algorithm.
  
      Options
      -------
      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.
  
      """
      _check_unknown_options(unknown_options)
      f = fun
      fprime = jac
      epsilon = eps
      retall = return_all
  
      x0 = asarray(x0).flatten()
      if x0.ndim == 0:
          x0.shape = (1,)
      if maxiter is None:
          maxiter = len(x0) * 200
      func_calls, f = wrap_function(f, args)
      if fprime is None:
          grad_calls, myfprime = wrap_function(
              approx_fprime, (f, epsilon)
          )
      else:
          grad_calls, myfprime = wrap_function(fprime, args)
      gfk = myfprime(x0)
      k = 0
      N = len(x0)
      I = numpy.eye(N, dtype=int)
      Hk = I
  
      # Sets the initial step guess to dx ~ 1
      old_fval = f(x0)
      old_old_fval = old_fval + np.linalg.norm(gfk) / 2
  
      xk = x0
      if retall:
          allvecs = [x0]
      warnflag = 0
      gnorm = vecnorm(gfk, ord=norm)
      while (gnorm > gtol) and (k < maxiter):
          pk = -numpy.dot(Hk, gfk)
          try:
              (
                  alpha_k,
                  fc,
                  gc,
                  old_fval,
                  old_old_fval,
                  gfkp1,
              ) = _line_search_wolfe12(
                  f,
                  myfprime,
                  xk,
                  pk,
                  gfk,
                  old_fval,
                  old_old_fval,
                  amin=1e-100,
                  amax=1e100,
              )
          except _LineSearchError:
              # Line search failed to find a better solution.
              warnflag = 2
              break
  
          xkp1 = xk + alpha_k * pk
          if retall:
              allvecs.append(xkp1)
          sk = xkp1 - xk
          xk = xkp1
          if gfkp1 is None:
              gfkp1 = myfprime(xkp1)
  
          yk = gfkp1 - gfk
          gfk = gfkp1
          if callback is not None:
              callback(xk)
          k += 1
          gnorm = vecnorm(gfk, ord=norm)
          if gnorm <= gtol:
              break
  
          if not numpy.isfinite(old_fval):
              # We correctly found +-Inf as optimal value, or something went
              # wrong.
              warnflag = 2
              break
  
          try:  # this was handled in numeric, let it remaines for more safety
              rhok = 1.0 / (numpy.dot(yk, sk))
          except ZeroDivisionError:
              rhok = 1000.0
              if disp:
                  print(
                      "Divide-by-zero encountered: rhok assumed large"
                  )
          if isinf(rhok):  # this is patch for numpy
              rhok = 1000.0
              if disp:
                  print(
                      "Divide-by-zero encountered: rhok assumed large"
                  )
          A1 = I - sk[:, numpy.newaxis] * yk[numpy.newaxis, :] * rhok
          A2 = I - yk[:, numpy.newaxis] * sk[numpy.newaxis, :] * rhok
          Hk = numpy.dot(A1, numpy.dot(Hk, A2)) + (
              rhok * sk[:, numpy.newaxis] * sk[numpy.newaxis, :]
          )
  
      fval = old_fval
      if np.isnan(fval):
          # This can happen if the first call to f returned NaN;
          # the loop is then never entered.
          warnflag = 2
  
      if warnflag == 2:
          msg = _status_message["pr_loss"]
      elif k >= maxiter:
          warnflag = 1
          msg = _status_message["maxiter"]
      else:
          msg = _status_message["success"]
  
      if disp:
          print("%s%s" % ("Warning: " if warnflag != 0 else "", msg))
          print("         Current function value: %f" % fval)
          print("         Iterations: %d" % k)
          print("         Function evaluations: %d" % func_calls[0])
          print("         Gradient evaluations: %d" % grad_calls[0])
  
      result = OptimizeResult(
          fun=fval,
          jac=gfk,
          hess_inv=Hk,
          nfev=func_calls[0],
          njev=grad_calls[0],
          status=warnflag,
          success=(warnflag == 0),
          message=msg,
          x=xk,
          nit=k,
      )
      if retall:
          result["allvecs"] = allvecs
      return result
  
imports: from __future__ import division, print_function, absolute_import
  import warnings
  import sys
  import numpy
  from scipy._lib.six import callable, xrange
  from numpy import atleast_1d, eye, mgrid, argmin, zeros, shape, squeeze, vectorize, asarray, sqrt, Inf, asfarray, isinf
  import numpy
  from .linesearch import line_search_wolfe1, line_search_wolfe2, line_search_wolfe2, LineSearchWarning
  from scipy._lib._util import getargspec_no_self
label_desc: Use image node_id 17 for calling a global function with example usage: _minimize_bfgs(fun, x0, args, jac, callback, gtol, norm, eps, maxiter, disp, return_all) and returns: result