code: def safe_cospx_div_cosx(
      theta: type_alias.TensorLike,
      factor: type_alias.TensorLike,
      eps: Optional[type_alias.Float] = None,
      name: str = "safe_cospx_div_cosx",
  ) -> tf.Tensor:
      """Calculates cos(factor * theta)/cos(theta) safely.
  
      The term `cos(factor * theta)/cos(theta)` has periodic edge cases with
      division by zero problems, and also zero / zero, e.g. when factor is equal to
      1.0 and `theta` is `(n + 1/2)pi`. This function adds signed eps to the angles
      in both the nominator and the denominator to ensure safety, and returns the
      correct values in all edge cases.
  
      Note:
        In the following, A1 to An are optional batch dimensions, which must be
        broadcast compatible.
  
      Args:
        theta: A tensor of shape `[A1, ..., An]`, representing angles in radians.
        factor: A `float` or a tensor of shape `[A1, ..., An]`.
        eps: A `float`, used to perturb the angle. If left as `None`, its value is
          automatically determined from the `dtype` of `theta`.
        name: A name for this op. Defaults to 'safe_cospx_div_cosx'.
  
      Raises:
        InvalidArgumentError: If tf-graphics debug flag is set and division returns
          `NaN` or `Inf` values.
  
      Returns:
        A tensor of shape `[A1, ..., An]` containing the resulting values.
      """
      with tf.name_scope(name):
          theta = tf.convert_to_tensor(value=theta)
          factor = tf.convert_to_tensor(value=factor, dtype=theta.dtype)
          if eps is None:
              eps = asserts.select_eps_for_division(theta.dtype)
          eps = tf.convert_to_tensor(value=eps, dtype=theta.dtype)
  
          # eps will be multiplied with factor next, which can make it zero.
          # Therefore we multiply eps with min(1/factor, 1e10), which can handle
          # factors as small as 1e-10 correctly, while preventing a division by zero.
          eps *= tf.clip_by_value(1.0 / factor, 1.0, 1e10)
          sign = nonzero_sign(
              0.5 * np.pi - (theta - 0.5 * np.pi) % np.pi
          )
          theta += sign * eps
          div = tf.cos(factor * theta) / tf.cos(theta)
          return asserts.assert_no_infs_or_nans(div)
  
file_path: graphics/tensorflow_graphics/util/safe_ops.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  from typing import Optional
  import numpy
  import tensorflow
  from tensorflow_graphics.util import asserts
  from tensorflow_graphics.util import type_alias
label_desc: Use image node_id 2 for calling a global function with example usage: safe_cospx_div_cosx(theta, factor, eps, name) and returns: asserts