code: def _adesi_whaley(
      *,
      sigma,
      x,
      t,
      r,
      d,
      s,
      is_call_options,
      max_iterations,
      tolerance,
      dtype,
  ):
      """Computes American option prices using the Baron-Adesi Whaley formula."""
  
      # The naming convention will align variables with the variables named in
      # reference [1], but made lower case, and differentiating between put and
      # call option values with the suffix _put and _call.
      # [1] https://deriscope.com/docs/Barone_Adesi_Whaley_1987.pdf
      sign = tf.where(
          is_call_options,
          tf.constant(1, dtype=dtype),
          tf.constant(-1, dtype=dtype),
      )
  
      q2, a2, s_crit, converged, failed = _adesi_whaley_critical_values(
          sigma=sigma,
          x=x,
          t=t,
          r=r,
          d=d,
          sign=sign,
          is_call_options=is_call_options,
          max_iterations=max_iterations,
          tolerance=tolerance,
          dtype=dtype,
      )
  
      eu_prices = vanilla_prices.option_price(
          volatilities=sigma,
          strikes=x,
          expiries=t,
          spots=s,
          discount_rates=r,
          dividend_rates=d,
          is_call_options=is_call_options,
          dtype=dtype,
      )
  
      # The divisive condition is different for put and call options
      condition = tf.where(is_call_options, s < s_crit, s > s_crit)
  
      # Distinguish the case of zero expiry.
      american_prices = tf.where(
          t > 0,
          tf.where(
              condition,
              eu_prices + a2 * (s / s_crit) ** q2,
              (s - x) * sign,
          ),
          tf.where(
              is_call_options,
              tf.math.maximum(s - x, 0),
              tf.math.maximum(x - s, 0),
          ),
      )
  
      return american_prices, converged, failed
  
imports: from typing import Tuple
  import numpy
  import tensorflow.compat.v2
  from tf_quant_finance import types
  from tf_quant_finance.black_scholes import vanilla_prices
  from tf_quant_finance.math import gradient
  from tf_quant_finance.math import integration
  from tf_quant_finance.math.root_search import newton
label_desc: Use image node_id 2 for calling a global function with example usage: _adesi_whaley() and returns: american_prices,  converged,  failed