code: def _brent(
      objective_fn,
      left_bracket,
      right_bracket,
      value_at_left_bracket=None,
      value_at_right_bracket=None,
      absolute_root_tolerance=2e-7,
      relative_root_tolerance=None,
      function_tolerance=2e-7,
      max_iterations=100,
      stopping_policy_fn=None,
      validate_args=False,
      name=None,
  ):
      r"""Finds root(s) of a function of a single variable using Brent's method.
  
      [Brent's method](https://en.wikipedia.org/wiki/Brent%27s_method) is a
      root-finding algorithm combining the bisection method, the secant method and
      extrapolation. Like bisection it is guaranteed to converge towards a root if
      one exists, but that convergence is superlinear and on par with less reliable
      methods.
  
      This implementation is a translation of the algorithm described in the
      [original article](https://academic.oup.com/comjnl/article/14/4/422/325237).
  
      Args:
        objective_fn: Python callable for which roots are searched. It must be a
          callable of a single `Tensor` parameter and return a `Tensor` of the same
          shape and dtype as `left_bracket`.
        left_bracket: `Tensor` or Python float representing the first starting
          points. The function will search for roots between each pair of points
          defined by `left_bracket` and `right_bracket`. The shape of `left_bracket`
          should match that of the input to `objective_fn`.
        right_bracket: `Tensor` of the same shape and dtype as `left_bracket` or
          Python float representing the second starting points. The function will
          search for roots between each pair of points defined by `left_bracket` and
          `right_bracket`. This argument must have the same shape as `left_bracket`.
        value_at_left_bracket: Optional `Tensor` or Python float representing the
          value of `objective_fn` at `left_bracket`. If specified, this argument
          must have the same shape as `left_bracket`. If not specified, the value
          will be evaluated during the search.
          Default value: None.
        value_at_right_bracket: Optional `Tensor` or Python float representing the
          value of `objective_fn` at `right_bracket`. If specified, this argument
          must have the same shape as `right_bracket`. If not specified, the value
          will be evaluated during the search.
          Default value: None.
        absolute_root_tolerance: Optional `Tensor` representing the absolute
          tolerance for estimated roots, with the total tolerance being calculated
          as `(absolute_root_tolerance + relative_root_tolerance * |root|) / 2`. If
          specified, this argument must be positive, broadcast with the shape of
          `left_bracket` and have the same dtype.
          Default value: `2e-7`.
        relative_root_tolerance: Optional `Tensor` representing the relative
          tolerance for estimated roots, with the total tolerance being calculated
          as `(absolute_root_tolerance + relative_root_tolerance * |root|) / 2`. If
          specified, this argument must be positive, broadcast with the shape of
          `left_bracket` and have the same dtype.
          Default value: `None` which translates to `4 *
            numpy.finfo(left_bracket.dtype.as_numpy_dtype).eps`.
        function_tolerance: Optional `Tensor` representing the tolerance used to
          check for roots. If the absolute value of `objective_fn` is smaller than
          or equal to `function_tolerance` at a given estimate, then that estimate
          is considered a root for the function. If specified, this argument must
          broadcast with the shape of `left_bracket` and have the same dtype. Set to
          zero to match Brent's original algorithm and to continue the search until
          an exact root is found.
          Default value: `2e-7`.
        max_iterations: Optional `Tensor` of an integral dtype or Python integer
          specifying the maximum number of steps to perform for each initial point.
          Must broadcast with the shape of `left_bracket`. If an element is set to
          zero, the function will not search for any root for the corresponding
          points in `left_bracket` and `right_bracket`. Instead, it will return the
          best estimate from the inputs.
          Default value: `100`.
        stopping_policy_fn: Python `callable` controlling the algorithm termination.
          It must be a callable accepting a `Tensor` of booleans with the shape of
          `left_bracket` (each denoting whether the search is finished for each
          starting point), and returning a scalar boolean `Tensor` (indicating
          whether the overall search should stop). Typical values are
          `tf.reduce_all` (which returns only when the search is finished for all
          pairs of points), and `tf.reduce_any` (which returns as soon as the search
          is finished for any pair of points).
          Default value: `None` which translates to `tf.reduce_all`.
        validate_args: Python `bool` indicating whether to validate arguments such
          as `left_bracket`, `right_bracket`, `absolute_root_tolerance`,
          `relative_root_tolerance`, `function_tolerance`, and `max_iterations`.
          Default value: `False`.
        name: Python `str` name prefixed to ops created by this function.
  
      Returns:
        brent_results: A Python object containing the following attributes:
          estimated_root: `Tensor` containing the best estimate explored. If the
            search was successful within the specified tolerance, this estimate is
            a root of the objective function.
          objective_at_estimated_root: `Tensor` containing the value of the
            objective function at `estimated_root`. If the search was successful
            within the specified tolerance, then this is close to 0. It has the
            same dtype and shape as `estimated_root`.
          num_iterations: `Tensor` containing the number of iterations performed.
            It has the same dtype as `max_iterations` and shape as `estimated_root`.
          converged: Scalar boolean `Tensor` indicating whether `estimated_root` is
            a root within the tolerance specified for the search. It has the same
            shape as `estimated_root`.
  
      Raises:
        ValueError: if the `stopping_policy_fn` is not callable.
      """
  
      with tf.name_scope(name or "brent_root"):
          state, params, constants = _prepare_brent_args(
              objective_fn,
              left_bracket,
              right_bracket,
              value_at_left_bracket,
              value_at_right_bracket,
              absolute_root_tolerance,
              relative_root_tolerance,
              function_tolerance,
              max_iterations,
              stopping_policy_fn,
          )
  
          assertions = []
          if validate_args:
              assertions += [
                  tf.Assert(
                      tf.reduce_all(
                          state.value_at_last_estimate
                          * state.value_at_best_estimate
                          <= constants.zero_value
                      ),
                      [
                          state.value_at_last_estimate,
                          state.value_at_best_estimate,
                      ],
                  ),
                  tf.Assert(
                      tf.reduce_all(
                          params.absolute_root_tolerance
                          > constants.zero
                      ),
                      [params.absolute_root_tolerance],
                  ),
                  tf.Assert(
                      tf.reduce_all(
                          params.relative_root_tolerance
                          > constants.zero
                      ),
                      [params.relative_root_tolerance],
                  ),
                  tf.Assert(
                      tf.reduce_all(
                          params.function_tolerance >= constants.zero
                      ),
                      [params.function_tolerance],
                  ),
                  tf.Assert(
                      tf.reduce_all(
                          params.max_iterations >= state.num_iterations
                      ),
                      [params.max_iterations],
                  ),
              ]
  
          with tf.compat.v1.control_dependencies(assertions):
              result = tf.while_loop(
                  # Negate `_should_stop` to determine if the search should continue.
                  # This means, in particular, that tf.reduce_*all* will return only
                  # when the search is finished for *all* starting points.
                  lambda loop_vars: ~_should_stop(
                      loop_vars, params.stopping_policy_fn
                  ),
                  lambda state: _brent_loop_body(
                      state, params, constants
                  ),
                  loop_vars=[state],
                  maximum_iterations=max_iterations,
              )
  
      state = result[0]
      converged = (
          tf.math.abs(state.value_at_best_estimate)
          <= function_tolerance
      )
  
      return BrentResults(
          estimated_root=state.best_estimate,
          objective_at_estimated_root=state.value_at_best_estimate,
          num_iterations=state.num_iterations,
          converged=converged,
      )
  
file_path: tf-quant-finance/tf_quant_finance/math/root_search/brent.py
imports: from typing import Callable
  import tensorflow.compat.v2
  from tf_quant_finance import types
  from tf_quant_finance import utils
  from tf_quant_finance.math.root_search import utils
label_desc: Use image node_id 7 for calling a global function with example usage: _brent(objective_fn, left_bracket, right_bracket, value_at_left_bracket, value_at_right_bracket, absolute_root_tolerance, relative_root_tolerance, function_tolerance, max_iterations, stopping_policy_fn, validate_args, name) and returns: BrentResults