code: def _adjust_scheme_to_bounds(x0, h, num_steps, scheme, lb, ub):
      """Adjust final difference scheme to the presence of bounds.
  
      Parameters
      ----------
      x0 : ndarray, shape (n,)
          Point at which we wish to estimate derivative.
      h : ndarray, shape (n,)
          Desired absolute finite difference steps.
      num_steps : int
          Number of `h` steps in one direction required to implement finite
          difference scheme. For example, 2 means that we need to evaluate
          f(x0 + 2 * h) or f(x0 - 2 * h)
      scheme : {'1-sided', '2-sided'}
          Whether steps in one or both directions are required. In other
          words '1-sided' applies to forward and backward schemes, '2-sided'
          applies to center schemes.
      lb : ndarray, shape (n,)
          Lower bounds on independent variables.
      ub : ndarray, shape (n,)
          Upper bounds on independent variables.
  
      Returns
      -------
      h_adjusted : ndarray, shape (n,)
          Adjusted absolute step sizes. Step size decreases only if a sign flip
          or switching to one-sided scheme doesn't allow to take a full step.
      use_one_sided : ndarray of bool, shape (n,)
          Whether to switch to one-sided scheme. Informative only for
          ``scheme='2-sided'``.
      """
      if scheme == "1-sided":
          use_one_sided = np.ones_like(h, dtype=bool)
      elif scheme == "2-sided":
          h = np.abs(h)
          use_one_sided = np.zeros_like(h, dtype=bool)
      else:
          raise ValueError("`scheme` must be '1-sided' or '2-sided'.")
  
      if np.all((lb == -np.inf) & (ub == np.inf)):
          return h, use_one_sided
  
      h_total = h * num_steps
      h_adjusted = h.copy()
  
      lower_dist = x0 - lb
      upper_dist = ub - x0
  
      if scheme == "1-sided":
          x = x0 + h_total
          violated = (x < lb) | (x > ub)
          fitting = np.abs(h_total) <= np.maximum(
              lower_dist, upper_dist
          )
          h_adjusted[violated & fitting] *= -1
  
          forward = (upper_dist >= lower_dist) & ~fitting
          h_adjusted[forward] = upper_dist[forward] / num_steps
          backward = (upper_dist < lower_dist) & ~fitting
          h_adjusted[backward] = -lower_dist[backward] / num_steps
      elif scheme == "2-sided":
          central = (lower_dist >= h_total) & (upper_dist >= h_total)
  
          forward = (upper_dist >= lower_dist) & ~central
          h_adjusted[forward] = np.minimum(
              h[forward], 0.5 * upper_dist[forward] / num_steps
          )
          use_one_sided[forward] = True
  
          backward = (upper_dist < lower_dist) & ~central
          h_adjusted[backward] = -np.minimum(
              h[backward], 0.5 * lower_dist[backward] / num_steps
          )
          use_one_sided[backward] = True
  
          min_dist = np.minimum(upper_dist, lower_dist) / num_steps
          adjusted_central = ~central & (np.abs(h_adjusted) <= min_dist)
          h_adjusted[adjusted_central] = min_dist[adjusted_central]
          use_one_sided[adjusted_central] = False
  
      return h_adjusted, use_one_sided
  
file_path: scipy/scipy/optimize/_numdiff.py
imports: import functools
  import numpy
  from numpy.linalg import norm
  from scipy.sparse.linalg import LinearOperator
  from ..sparse import issparse, csc_matrix, csr_matrix, coo_matrix, find
  from ._group_columns import group_dense, group_sparse
  from scipy._lib._array_api import atleast_nd, array_namespace
label_desc: Use image node_id 1 for calling a global function with example usage: _adjust_scheme_to_bounds(x0, h, num_steps, scheme, lb, ub) and returns: h_adjusted,  use_one_sided, h,  use_one_sided