code: def select_step(x, J_h, diag_h, g_h, p, p_h, d, Delta, lb, ub, theta):
      """Select the best step according to Trust Region Reflective algorithm."""
      if in_bounds(x + p, lb, ub):
          p_value = evaluate_quadratic(J_h, g_h, p_h, diag=diag_h)
          return p, p_h, -p_value
  
      p_stride, hits = step_size_to_bound(x, p, lb, ub)
  
      # Compute the reflected direction.
      r_h = np.copy(p_h)
      r_h[hits.astype(bool)] *= -1
      r = d * r_h
  
      # Restrict trust-region step, such that it hits the bound.
      p *= p_stride
      p_h *= p_stride
      x_on_bound = x + p
  
      # Reflected direction will cross first either feasible region or trust
      # region boundary.
      _, to_tr = intersect_trust_region(p_h, r_h, Delta)
      to_bound, _ = step_size_to_bound(x_on_bound, r, lb, ub)
  
      # Find lower and upper bounds on a step size along the reflected
      # direction, considering the strict feasibility requirement. There is no
      # single correct way to do that, the chosen approach seems to work best
      # on test problems.
      r_stride = min(to_bound, to_tr)
      if r_stride > 0:
          r_stride_l = (1 - theta) * p_stride / r_stride
          if r_stride == to_bound:
              r_stride_u = theta * to_bound
          else:
              r_stride_u = to_tr
      else:
          r_stride_l = 0
          r_stride_u = -1
  
      # Check if reflection step is available.
      if r_stride_l <= r_stride_u:
          a, b, c = build_quadratic_1d(
              J_h, g_h, r_h, s0=p_h, diag=diag_h
          )
          r_stride, r_value = minimize_quadratic_1d(
              a, b, r_stride_l, r_stride_u, c=c
          )
          r_h *= r_stride
          r_h += p_h
          r = r_h * d
      else:
          r_value = np.inf
  
      # Now correct p_h to make it strictly interior.
      p *= theta
      p_h *= theta
      p_value = evaluate_quadratic(J_h, g_h, p_h, diag=diag_h)
  
      ag_h = -g_h
      ag = d * ag_h
  
      to_tr = Delta / norm(ag_h)
      to_bound, _ = step_size_to_bound(x, ag, lb, ub)
      if to_bound < to_tr:
          ag_stride = theta * to_bound
      else:
          ag_stride = to_tr
  
      a, b = build_quadratic_1d(J_h, g_h, ag_h, diag=diag_h)
      ag_stride, ag_value = minimize_quadratic_1d(a, b, 0, ag_stride)
      ag_h *= ag_stride
      ag *= ag_stride
  
      if p_value < r_value and p_value < ag_value:
          return p, p_h, -p_value
      elif r_value < p_value and r_value < ag_value:
          return r, r_h, -r_value
      else:
          return ag, ag_h, -ag_value
  
file_path: catboost/contrib/python/scipy/py2/scipy/optimize/_lsq/trf.py
imports: from __future__ import division, print_function, absolute_import
  import numpy
  from numpy.linalg import norm
  from scipy.linalg import svd, qr
  from scipy.sparse.linalg import lsmr
  from scipy.optimize import OptimizeResult
  from scipy._lib.six import string_types
  from .common import step_size_to_bound, find_active_constraints, in_bounds, make_strictly_feasible, intersect_trust_region, solve_lsq_trust_region, solve_trust_region_2d, minimize_quadratic_1d, build_quadratic_1d, evaluate_quadratic, right_multiplied_operator, regularized_lsq_operator, CL_scaling_vector, compute_grad, compute_jac_scale, check_termination, update_tr_radius, scale_for_robust_loss_function, print_header_nonlinear, print_iteration_nonlinear
label_desc: Use image node_id 2 for calling a global function with example usage: select_step(x, J_h, diag_h, g_h, p, p_h, d, Delta, lb, ub, theta) and returns: p,  p_h,  unknown, p,  p_h,  unknown, r,  r_h,  unknown, ag,  ag_h,  unknown