code: def _map_payoff_to_sim_times(indices, payoff, num_samples):
      """Maps the swaption payoffs to short rate simulation times.
  
      Swaption payoffs are calculated on bermudan swaption's expiries. However, for
      the LSM algorithm, we need short rate simulations and swaption payoffs at
      the union of all exercise times in the batch of swaptions. This function
      takes the payoff of individual swaption at their respective exercise times
      and maps it to all simulation times. This is done by setting the payoff to
      -1 whenever the simulation time is not equal to the swaption exercise time.
  
      Args:
        indices: A `Tensor` of shape `batch_shape + num_exercise_times` containing
          the index of exercise time in the vector of simulation times.
        payoff: A real tensor of shape
          `[num_samples] + batch_shape + num_exercise_times` containing the
          exercise value of the underlying swap on each exercise time.
        num_samples: A scalar `Tensor` specifying the number of samples on which
          swaption payoff is computed.
  
      Returns:
        A tuple of `Tensors`. The first tensor is a integer `Tensor` of shape
        `[num_samples] + batch_shape + [num_simulation_times]` and contains `1`
        if the corresponding simulation time is one of the exercise times for the
        swaption. The second `Tensor` is a real `Tensor` of same shape and contains
        the exercise value of the swaption if the corresponding simulation time is
        an exercise time for the swaption or -1 otherwise.
      """
      indices = tf.expand_dims(indices, axis=0)
      indices = tf.repeat(indices, num_samples, axis=0)
      index_list = list()
      tensor_shape = np.array(indices.shape.as_list())
      output_shape = indices.shape.as_list()[:-1] + [
          tf.math.reduce_max(indices) + 1
      ]
      num_elements = np.prod(tensor_shape)
      for dim, _ in enumerate(tensor_shape[:-1]):
          idx = tf.range(0, tensor_shape[dim], dtype=indices.dtype)
          idx = tf.tile(
              tf.repeat(idx, np.prod(tensor_shape[dim + 1 :])),
              [np.prod(tensor_shape[:dim])],
          )
          index_list.append(idx)
  
      index_list.append(tf.reshape(indices, [-1]))
      # We need to transform `payoff` from the initial shape of
      # [num_samples, batch_shape, num_exercise_times] to a new `Tensor` with
      # shape = [num_samples, batch_shape, num_exercise_times] such that
      # payoff_new[..., indices] = payoff
      # We achieve this by first creating a `payoff_new` as a SparseTensor with
      # nonzero values at appropriate indices based on the payoff_new.shape and
      # then converting the sparse tenson to dense tensor.
      sparse_indices = tf.cast(
          tf.stack(index_list, axis=-1), dtype=np.int64
      )
      is_exercise_time = tf.sparse.to_dense(
          tf.sparse.SparseTensor(
              sparse_indices, tf.ones(shape=num_elements), output_shape
          ),
          validate_indices=False,
      )
      payoff = tf.sparse.to_dense(
          tf.sparse.SparseTensor(
              sparse_indices, tf.reshape(payoff, [-1]), output_shape
          ),
          validate_indices=False,
      )
      return is_exercise_time, payoff
  
imports: from typing import Callable, Union
  import numpy
  import tensorflow.compat.v2
  from tf_quant_finance import types
  from tf_quant_finance.math import pde
  from tf_quant_finance.math import random
  from tf_quant_finance.math.interpolation import linear
  from tf_quant_finance.math.root_search import brent
  from tf_quant_finance.models import utils
  from tf_quant_finance.models.hjm import swaption_util
  from tf_quant_finance.models.hull_white import one_factor
  from tf_quant_finance.models.hull_white import zero_coupon_bond_option
  from tf_quant_finance.models.longstaff_schwartz import lsm
label_desc: Use image node_id 7 for calling a global function with example usage: _map_payoff_to_sim_times(indices, payoff, num_samples) and returns: is_exercise_time,  payoff