code: def fmin(
      func,
      x0,
      args=(),
      xtol=1e-4,
      ftol=1e-4,
      maxiter=None,
      maxfun=None,
      full_output=0,
      disp=1,
      retall=0,
      callback=None,
  ):
      """Minimize a function using the downhill simplex algorithm.
  
      :Parameters:
  
        func : the Python function or method to be minimized.
        x0 : ndarray - the initial guess.
        args : extra arguments for func.
        callback : an optional user-supplied function to call after each
                    iteration.  It is called as callback(xk), where xk is the
                    current parameter vector.
  
      :Returns: (xopt, {fopt, iter, funcalls, warnflag})
  
        xopt : ndarray
          minimizer of function
        fopt : number
          value of function at minimum: fopt = func(xopt)
        iter : number
          number of iterations
        funcalls : number
          number of function calls
        warnflag : number
          Integer warning flag:
                    1 : 'Maximum number of function evaluations.'
                    2 : 'Maximum number of iterations.'
        allvecs : Python list
          a list of solutions at each iteration
  
      :OtherParameters:
  
        xtol : number
          acceptable relative error in xopt for convergence.
        ftol : number
          acceptable relative error in func(xopt) for convergence.
        maxiter : number
          the maximum number of iterations to perform.
        maxfun : number
          the maximum number of function evaluations.
        full_output : number
          non-zero if fval and warnflag outputs are desired.
        disp : number
          non-zero to print convergence messages.
        retall : number
          non-zero to return list of solutions at each iteration
  
      :SeeAlso:
  
        fmin, fmin_powell, fmin_cg,
               fmin_bfgs, fmin_ncg -- multivariate local optimizers
        leastsq -- nonlinear least squares minimizer
  
        fmin_l_bfgs_b, fmin_tnc,
               fmin_cobyla -- constrained multivariate optimizers
  
        anneal, brute -- global optimizers
  
        fminbound, brent, golden, bracket -- local scalar minimizers
  
        fsolve -- n-dimenstional root-finding
  
        brentq, brenth, ridder, bisect, newton -- one-dimensional root-finding
  
        fixed_point -- scalar fixed-point finder
  
      Notes
  
      -----------
  
        Uses a Nelder-Mead simplex algorithm to find the minimum of function
        of one or more variables.
      """
      fcalls, func = wrap_function(func, args)
      x0 = asfarray(x0).flatten()
      N = len(x0)
      rank = len(x0.shape)
      if not -1 < rank < 2:
          raise ValueError(
              "Initial guess must be a scalar or rank-1 sequence."
          )
      if maxiter is None:
          maxiter = N * 200
      if maxfun is None:
          maxfun = N * 200
  
      rho = 1
      chi = 2
      psi = 0.5
      sigma = 0.5
      one2np1 = range(1, N + 1)
  
      if rank == 0:
          sim = numpy.zeros((N + 1,), dtype=x0.dtype)
      else:
          sim = numpy.zeros((N + 1, N), dtype=x0.dtype)
      fsim = numpy.zeros((N + 1,), float)
      sim[0] = x0
      if retall:
          allvecs = [sim[0]]
      fsim[0] = func(x0)
      nonzdelt = 0.05
      zdelt = 0.00025
      for k in range(0, N):
          y = numpy.array(x0, copy=True)
          if y[k] != 0:
              y[k] = (1 + nonzdelt) * y[k]
          else:
              y[k] = zdelt
  
          sim[k + 1] = y
          f = func(y)
          fsim[k + 1] = f
  
      ind = numpy.argsort(fsim)
      fsim = numpy.take(fsim, ind, 0)
      # sort so sim[0,:] has the lowest function value
      sim = numpy.take(sim, ind, 0)
  
      iterations = 1
  
      while fcalls[0] < maxfun and iterations < maxiter:
          if (
              max(numpy.ravel(abs(sim[1:] - sim[0]))) <= xtol
              and max(abs(fsim[0] - fsim[1:])) <= ftol
          ):
              break
  
          xbar = numpy.add.reduce(sim[:-1], 0) / N
          xr = (1 + rho) * xbar - rho * sim[-1]
          fxr = func(xr)
          doshrink = 0
  
          if fxr < fsim[0]:
              xe = (1 + rho * chi) * xbar - rho * chi * sim[-1]
              fxe = func(xe)
  
              if fxe < fxr:
                  sim[-1] = xe
                  fsim[-1] = fxe
              else:
                  sim[-1] = xr
                  fsim[-1] = fxr
          else:  # fsim[0] <= fxr
              if fxr < fsim[-2]:
                  sim[-1] = xr
                  fsim[-1] = fxr
              else:  # fxr >= fsim[-2]
                  # Perform contraction
                  if fxr < fsim[-1]:
                      xc = (1 + psi * rho) * xbar - psi * rho * sim[-1]
                      fxc = func(xc)
  
                      if fxc <= fxr:
                          sim[-1] = xc
                          fsim[-1] = fxc
                      else:
                          doshrink = 1
                  else:
                      # Perform an inside contraction
                      xcc = (1 - psi) * xbar + psi * sim[-1]
                      fxcc = func(xcc)
  
                      if fxcc < fsim[-1]:
                          sim[-1] = xcc
                          fsim[-1] = fxcc
                      else:
                          doshrink = 1
  
                  if doshrink:
                      for j in one2np1:
                          sim[j] = sim[0] + sigma * (sim[j] - sim[0])
                          fsim[j] = func(sim[j])
  
          ind = numpy.argsort(fsim)
          sim = numpy.take(sim, ind, 0)
          fsim = numpy.take(fsim, ind, 0)
          if callback is not None:
              callback(sim[0])
          iterations += 1
          if retall:
              allvecs.append(sim[0])
  
      x = sim[0]
      fval = min(fsim)
      warnflag = 0
  
      if fcalls[0] >= maxfun:
          warnflag = 1
          if disp:
              print(
                  "Warning: Maximum number of function evaluations has "
                  "been exceeded."
              )
      elif iterations >= maxiter:
          warnflag = 2
          if disp:
              print(
                  "Warning: Maximum number of iterations has been exceeded"
              )
      else:
          if disp:
              print("Optimization terminated successfully.")
              print("         Current function value: %s" % fval)
              print("         Iterations: %d" % iterations)
              print("         Function evaluations: %d" % fcalls[0])
  
      if full_output:
          retlist = x, fval, iterations, fcalls[0], warnflag
          if retall:
              retlist += (allvecs,)
      else:
          retlist = x
          if retall:
              retlist = (x, allvecs)
  
      return retlist
  
imports: import numpy
  from numpy import atleast_1d, eye, mgrid, argmin, zeros, shape, empty, squeeze, isscalar, vectorize, asarray, absolute, sqrt, Inf, asfarray, isinf
  from mystic import linesearch
  from collections.abc import Callable
  import builtins
label_desc: Use image node_id 9 for calling a global function with example usage: fmin(func, x0, args, xtol, ftol, maxiter, maxfun, full_output, disp, retall, callback) and returns: retlist