code: def _prepare_scalar_function(
      fun,
      x0,
      jac=None,
      args=(),
      bounds=None,
      epsilon=None,
      finite_diff_rel_step=None,
      hess=None,
  ):
      """
      Creates a ScalarFunction object for use with scalar minimizers
      (BFGS/LBFGSB/SLSQP/TNC/CG/etc).
  
      Parameters
      ----------
      fun : callable
          The objective function to be minimized.
  
              ``fun(x, *args) -> float``
  
          where ``x`` is an 1-D array with shape (n,) and ``args``
          is a tuple of the fixed parameters needed to completely
          specify the function.
      x0 : ndarray, shape (n,)
          Initial guess. Array of real elements of size (n,),
          where 'n' is the number of independent variables.
      jac : {callable,  '2-point', '3-point', 'cs', None}, optional
          Method for computing the gradient vector. If it is a callable, it
          should be a function that returns the gradient vector:
  
              ``jac(x, *args) -> array_like, shape (n,)``
  
          If one of `{'2-point', '3-point', 'cs'}` is selected then the gradient
          is calculated with a relative step for finite differences. If `None`,
          then two-point finite differences with an absolute step is used.
      args : tuple, optional
          Extra arguments passed to the objective function and its
          derivatives (`fun`, `jac` functions).
      bounds : sequence, optional
          Bounds on variables. 'new-style' bounds are required.
      eps : float or ndarray
          If `jac is None` the absolute step size used for numerical
          approximation of the jacobian via forward differences.
      finite_diff_rel_step : None or array_like, optional
          If `jac in ['2-point', '3-point', 'cs']` the relative step size to
          use for numerical approximation of the jacobian. The absolute step
          size is computed as ``h = rel_step * sign(x0) * max(1, abs(x0))``,
          possibly adjusted to fit into the bounds. For ``method='3-point'``
          the sign of `h` is ignored. If None (default) then step is selected
          automatically.
      hess : {callable,  '2-point', '3-point', 'cs', None}
          Computes the Hessian matrix. If it is callable, it should return the
          Hessian matrix:
  
              ``hess(x, *args) -> {LinearOperator, spmatrix, array}, (n, n)``
  
          Alternatively, the keywords {'2-point', '3-point', 'cs'} select a
          finite difference scheme for numerical estimation.
          Whenever the gradient is estimated via finite-differences, the Hessian
          cannot be estimated with options {'2-point', '3-point', 'cs'} and needs
          to be estimated using one of the quasi-Newton strategies.
  
      Returns
      -------
      sf : ScalarFunction
      """
      if callable(jac):
          grad = jac
      elif jac in FD_METHODS:
          # epsilon is set to None so that ScalarFunction is made to use
          # rel_step
          epsilon = None
          grad = jac
      else:
          # default (jac is None) is to do 2-point finite differences with
          # absolute step size. ScalarFunction has to be provided an
          # epsilon value that is not None to use absolute steps. This is
          # normally the case from most _minimize* methods.
          grad = "2-point"
          epsilon = epsilon
  
      if hess is None:
          # ScalarFunction requires something for hess, so we give a dummy
          # implementation here if nothing is provided, return a value of None
          # so that downstream minimisers halt. The results of `fun.hess`
          # should not be used.
          def hess(x, *args):
              return None
  
      if bounds is None:
          bounds = (-np.inf, np.inf)
  
      # ScalarFunction caches. Reuse of fun(x) during grad
      # calculation reduces overall function evaluations.
      sf = ScalarFunction(
          fun,
          x0,
          args,
          grad,
          hess,
          finite_diff_rel_step,
          bounds,
          epsilon=epsilon,
      )
  
      return sf
  
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 4 for calling a global function with example usage: _prepare_scalar_function(fun, x0, jac, args, bounds, epsilon, finite_diff_rel_step, hess) and returns: sf, None