code: def _minimize_powell(
      func,
      x0,
      args=(),
      callback=None,
      bounds=None,
      xtol=1e-4,
      ftol=1e-4,
      maxiter=None,
      maxfev=None,
      disp=False,
      direc=None,
      return_all=False,
      **unknown_options,
  ):
      """
      Minimization of scalar function of one or more variables using the
      modified Powell algorithm.
  
      Options
      -------
      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, maxfev : int
          Maximum allowed number of iterations and function evaluations.
          Will default to ``N*1000``, where ``N`` is the number of
          variables, if neither `maxiter` or `maxfev` is set. If both
          `maxiter` and `maxfev` are set, minimization will stop at the
          first reached.
      direc : ndarray
          Initial set of direction vectors for the Powell method.
      return_all : bool, optional
          Set to True to return a list of the best solution at each of the
          iterations.
      bounds : `Bounds`
          If bounds are not provided, then an unbounded line search will be used.
          If bounds are provided and the initial guess is within the bounds, then
          every function evaluation throughout the minimization procedure will be
          within the bounds. If bounds are provided, the initial guess is outside
          the bounds, and `direc` is full rank (or left to default), then some
          function evaluations during the first iteration may be outside the
          bounds, but every function evaluation after the first iteration will be
          within the bounds. If `direc` is not full rank, then some parameters may
          not be optimized and the solution is not guaranteed to be within the
          bounds.
      return_all : bool, optional
          Set to True to return a list of the best solution at each of the
          iterations.
      """
      _check_unknown_options(unknown_options)
      maxfun = maxfev
      retall = return_all
      # we need to use a mutable object here that we can update in the
      # wrapper function
      fcalls, func = _wrap_function(func, args)
      x = asarray(x0).flatten()
      if retall:
          allvecs = [x]
      N = len(x)
      # If neither are set, then set both to default
      if maxiter is None and maxfun is None:
          maxiter = N * 1000
          maxfun = N * 1000
      elif maxiter is None:
          # Convert remaining Nones, to np.inf, unless the other is np.inf, in
          # which case use the default to avoid unbounded iteration
          if maxfun == np.inf:
              maxiter = N * 1000
          else:
              maxiter = np.inf
      elif maxfun is None:
          if maxiter == np.inf:
              maxfun = N * 1000
          else:
              maxfun = np.inf
  
      if direc is None:
          direc = eye(N, dtype=float)
      else:
          direc = asarray(direc, dtype=float)
          if np.linalg.matrix_rank(direc) != direc.shape[0]:
              warnings.warn(
                  "direc input is not full rank, some parameters may "
                  "not be optimized",
                  OptimizeWarning,
                  3,
              )
  
      if bounds is None:
          # don't make these arrays of all +/- inf. because
          # _linesearch_powell will do an unnecessary check of all the elements.
          # just keep them None, _linesearch_powell will not have to check
          # all the elements.
          lower_bound, upper_bound = None, None
      else:
          # bounds is standardized in _minimize.py.
          lower_bound, upper_bound = bounds.lb, bounds.ub
          if np.any(lower_bound > x0) or np.any(x0 > upper_bound):
              warnings.warn(
                  "Initial guess is not within the specified bounds",
                  OptimizeWarning,
                  3,
              )
  
      fval = squeeze(func(x))
      x1 = x.copy()
      iter = 0
      ilist = list(range(N))
      while True:
          fx = fval
          bigind = 0
          delta = 0.0
          for i in ilist:
              direc1 = direc[i]
              fx2 = fval
              fval, x, direc1 = _linesearch_powell(
                  func,
                  x,
                  direc1,
                  tol=xtol * 100,
                  lower_bound=lower_bound,
                  upper_bound=upper_bound,
                  fval=fval,
              )
              if (fx2 - fval) > delta:
                  delta = fx2 - fval
                  bigind = i
          iter += 1
          if callback is not None:
              callback(x)
          if retall:
              allvecs.append(x)
          bnd = ftol * (np.abs(fx) + np.abs(fval)) + 1e-20
          if 2.0 * (fx - fval) <= bnd:
              break
          if fcalls[0] >= maxfun:
              break
          if iter >= maxiter:
              break
          if np.isnan(fx) and np.isnan(fval):
              # Ended up in a nan-region: bail out
              break
  
          # Construct the extrapolated point
          direc1 = x - x1
          x2 = 2 * x - x1
          x1 = x.copy()
          fx2 = squeeze(func(x2))
  
          if fx > fx2:
              t = 2.0 * (fx + fx2 - 2.0 * fval)
              temp = fx - fval - delta
              t *= temp * temp
              temp = fx - fx2
              t -= delta * temp * temp
              if t < 0.0:
                  fval, x, direc1 = _linesearch_powell(
                      func,
                      x,
                      direc1,
                      tol=xtol * 100,
                      lower_bound=lower_bound,
                      upper_bound=upper_bound,
                      fval=fval,
                  )
                  if np.any(direc1):
                      direc[bigind] = direc[-1]
                      direc[-1] = direc1
  
      warnflag = 0
      # out of bounds is more urgent than exceeding function evals or iters,
      # but I don't want to cause inconsistencies by changing the
      # established warning flags for maxfev and maxiter, so the out of bounds
      # warning flag becomes 3, but is checked for first.
      if bounds and (
          np.any(lower_bound > x) or np.any(x > upper_bound)
      ):
          warnflag = 4
          msg = _status_message["out_of_bounds"]
      elif fcalls[0] >= maxfun:
          warnflag = 1
          msg = _status_message["maxfev"]
          if disp:
              print("Warning: " + msg)
      elif iter >= maxiter:
          warnflag = 2
          msg = _status_message["maxiter"]
          if disp:
              print("Warning: " + msg)
      elif np.isnan(fval) or np.isnan(x).any():
          warnflag = 3
          msg = _status_message["nan"]
          if disp:
              print("Warning: " + msg)
      else:
          msg = _status_message["success"]
          if disp:
              print(msg)
              print("         Current function value: %f" % fval)
              print("         Iterations: %d" % iter)
              print("         Function evaluations: %d" % fcalls[0])
  
      result = OptimizeResult(
          fun=fval,
          direc=direc,
          nit=iter,
          nfev=fcalls[0],
          status=warnflag,
          success=(warnflag == 0),
          message=msg,
          x=x,
      )
      if retall:
          result["allvecs"] = allvecs
      return result
  
imports: import warnings
  import sys
  from numpy import atleast_1d, eye, argmin, zeros, shape, squeeze, asarray, sqrt, Inf, asfarray, isinf
  import numpy
  from .linesearch import line_search_wolfe1, line_search_wolfe2, line_search_wolfe2, LineSearchWarning
  from ._numdiff import approx_derivative
  from scipy._lib._util import getfullargspec_no_self
  from scipy._lib._util import MapWrapper
  from scipy.optimize._differentiable_functions import ScalarFunction, FD_METHODS
label_desc: Use image node_id 34 for calling a global function with example usage: _minimize_powell(func, x0, args, callback, bounds, xtol, ftol, maxiter, maxfev, disp, direc, return_all) and returns: result