code: def _scalar_optimization_loop(
      work,
      callback,
      shape,
      maxiter,
      func,
      args,
      dtype,
      pre_func_eval,
      post_func_eval,
      check_termination,
      post_termination_check,
      customize_result,
      res_work_pairs,
  ):
      """Main loop of a vectorized scalar optimization algorithm
  
      Parameters
      ----------
      work : OptimizeResult
          All variables that need to be retained between iterations. Must
          contain attributes `nit`, `nfev`, and `success`
      callback : callable
          User-specified callback function
      shape : tuple of ints
          The shape of all output arrays
      maxiter :
          Maximum number of iterations of the algorithm
      func : callable
          The user-specified callable that is being optimized or solved
      args : tuple
          Additional positional arguments to be passed to `func`.
      dtype : NumPy dtype
          The common dtype of all abscissae and function values
      pre_func_eval : callable
          A function that accepts `work` and returns `x`, the active elements
          of `x` at which `func` will be evaluated. May modify attributes
          of `work` with any algorithmic steps that need to happen
           at the beginning of an iteration, before `func` is evaluated,
      post_func_eval : callable
          A function that accepts `x`, `func(x)`, and `work`. May modify
          attributes of `work` with any algorithmic steps that need to happen
           in the middle of an iteration, after `func` is evaluated but before
           the termination check.
      check_termination : callable
          A function that accepts `work` and returns `stop`, a boolean array
          indicating which of the active elements have met a termination
          condition.
      post_termination_check : callable
          A function that accepts `work`. May modify `work` with any algorithmic
          steps that need to happen after the termination check and before the
          end of the iteration.
      customize_result : callable
          A function that accepts `res` and `shape` and returns `shape`. May
          modify `res` (in-place) according to preferences (e.g. rearrange
          elements between attributes) and modify `shape` if needed.
      res_work_pairs : list of (str, str)
          Identifies correspondence between attributes of `res` and attributes
          of `work`; i.e., attributes of active elements of `work` will be
          copied to the appropriate indices of `res` when appropriate. The order
          determines the order in which OptimizeResult attributes will be
          pretty-printed.
  
      Returns
      -------
      res : OptimizeResult
          The final result object
  
      Notes
      -----
      Besides providing structure, this framework provides several important
      services for a vectorized optimization algorithm.
  
      - It handles common tasks involving iteration count, function evaluation
        count, a user-specified callback, and associated termination conditions.
      - It compresses the attributes of `work` to eliminate unnecessary
        computation on elements that have already converged.
  
      """
      cb_terminate = False
  
      # Initialize the result object and active element index array
      n_elements = int(np.prod(shape))
      active = np.arange(n_elements)  # in-progress element indices
      res_dict = {
          i: np.zeros(n_elements, dtype=dtype)
          for i, j in res_work_pairs
      }
      res_dict["success"] = np.zeros(n_elements, dtype=bool)
      res_dict["status"] = np.full(n_elements, _EINPROGRESS)
      res_dict["nit"] = np.zeros(n_elements, dtype=int)
      res_dict["nfev"] = np.zeros(n_elements, dtype=int)
      res = OptimizeResult(res_dict)
      work.args = args
  
      active = _scalar_optimization_check_termination(
          work, res, res_work_pairs, active, check_termination
      )
  
      if callback is not None:
          temp = _scalar_optimization_prepare_result(
              work, res, res_work_pairs, active, shape, customize_result
          )
          if _call_callback_maybe_halt(callback, temp):
              cb_terminate = True
  
      while (
          work.nit < maxiter
          and active.size
          and not cb_terminate
          and n_elements
      ):
          x = pre_func_eval(work)
  
          if work.args and work.args[0].ndim != x.ndim:
              # `x` always starts as 1D. If the SciPy function that uses
              # _scalar_optimization_loop added dimensions to `x`, we need to
              # add them to the elements of `args`.
              dims = np.arange(x.ndim, dtype=np.int64)
              work.args = [
                  np.expand_dims(arg, tuple(dims[arg.ndim :]))
                  for arg in work.args
              ]
  
          f = func(x, *work.args)
          f = np.asarray(f, dtype=dtype)
          work.nfev += 1 if x.ndim == 1 else x.shape[-1]
  
          post_func_eval(x, f, work)
  
          work.nit += 1
          active = _scalar_optimization_check_termination(
              work, res, res_work_pairs, active, check_termination
          )
  
          if callback is not None:
              temp = _scalar_optimization_prepare_result(
                  work,
                  res,
                  res_work_pairs,
                  active,
                  shape,
                  customize_result,
              )
              if _call_callback_maybe_halt(callback, temp):
                  cb_terminate = True
                  break
          if active.size == 0:
              break
  
          post_termination_check(work)
  
      work.status[:] = _ECALLBACK if cb_terminate else _ECONVERR
      return _scalar_optimization_prepare_result(
          work, res, res_work_pairs, active, shape, customize_result
      )
  
imports: import warnings
  from collections import namedtuple
  import operator
  from .None import _zeros
  from ._optimize import OptimizeResult, _call_callback_maybe_halt
  import numpy
label_desc: Use image node_id 21 for calling a global function with example usage: _scalar_optimization_loop(work, callback, shape, maxiter, func, args, dtype, pre_func_eval, post_func_eval, check_termination, post_termination_check, customize_result, res_work_pairs) and returns: _scalar_optimization_prepare_result