code: def _construct_contribution_of_mixed_term(
      outer_coeff,
      inner_coeff,
      coord_grid,
      value_grid,
      dim1,
      dim2,
      batch_rank,
      lower_trim_indices,
      upper_trim_indices,
      has_default_lower_boundary,
      has_default_upper_boundary,
      n_dims,
  ):
      """Constructs contribution of a mixed derivative term."""
      if outer_coeff is None and inner_coeff is None:
          return None
      delta_dim1 = _get_grid_delta(coord_grid, dim1)
      delta_dim2 = _get_grid_delta(coord_grid, dim2)
  
      outer_coeff = _prepare_pde_coeff(outer_coeff, value_grid)
      inner_coeff = _prepare_pde_coeff(inner_coeff, value_grid)
  
      # The contribution of d2V/dx_dim1 dx_dim2 is
      # mixed_coeff / (4 * delta_dim1 * delta_dim2), but there is also
      # d2V/dx_dim2 dx_dim1, so the contribution is doubled.
      # Also, the minus is because of moving to the rhs.
      contrib = -1 / (2 * delta_dim1 * delta_dim2)
  
      # If the boundary condition along dimension `dim1` or `dim2` is default, we
      # need to update trimmings for that direction to remove the boundary and later
      # pad mixed term contributions with zeros (as we assume that the mixed terms
      # are zero on the boundary).
      (
          trimmed_lower_indices,
          trimmed_upper_indices,
          paddings,
      ) = _remove_default_boundary(
          lower_trim_indices,
          upper_trim_indices,
          has_default_lower_boundary,
          has_default_upper_boundary,
          batch_rank,
          (dim1, dim2),
          n_dims,
      )
  
      # Function to add zeros to the default boundary as mixed term on the default
      # boundary is assumed to be zero
      append_zeros_fn = lambda x: tf.pad(x, paddings)
  
      if outer_coeff is not None:
          outer_coeff = _trim_boundaries(
              outer_coeff,
              batch_rank,
              lower_trim_indices=trimmed_lower_indices,
              upper_trim_indices=trimmed_upper_indices,
          )
          contrib *= outer_coeff
      if inner_coeff is None:
          contrib = append_zeros_fn(contrib)
          return contrib, -contrib, -contrib, contrib
  
      def create_trimming_shifts(dim1_shift, dim2_shift):
          # See _trim_boundaries. We need to apply shifts to dimensions dim1 and
          # dim2.
          shifts = [0] * n_dims
          shifts[dim1] = dim1_shift
          shifts[dim2] = dim2_shift
          return shifts
  
      # When there are inner coefficients in mixed terms, the contributions of four
      # diagonally placed points are significantly different. Below we use indices
      # "p" and "m" to denote shifts by +1 and -1 in grid indices on the
      # (dim1, dim2) plane. E.g. if the current point has grid indices (k, l),
      # contrib_pm is the contribution of the point (k + 1, l - 1).
      contrib_pp = contrib * _trim_boundaries(
          inner_coeff,
          from_dim=batch_rank,
          shifts=create_trimming_shifts(1, 1),
          lower_trim_indices=trimmed_lower_indices,
          upper_trim_indices=trimmed_upper_indices,
      )
      contrib_pm = -contrib * _trim_boundaries(
          inner_coeff,
          from_dim=batch_rank,
          shifts=create_trimming_shifts(1, -1),
          lower_trim_indices=trimmed_lower_indices,
          upper_trim_indices=trimmed_upper_indices,
      )
      contrib_mp = -contrib * _trim_boundaries(
          inner_coeff,
          from_dim=batch_rank,
          shifts=create_trimming_shifts(-1, 1),
          lower_trim_indices=trimmed_lower_indices,
          upper_trim_indices=trimmed_upper_indices,
      )
      contrib_mm = contrib * _trim_boundaries(
          inner_coeff,
          from_dim=batch_rank,
          shifts=create_trimming_shifts(-1, -1),
          lower_trim_indices=trimmed_lower_indices,
          upper_trim_indices=trimmed_upper_indices,
      )
      (contrib_pp, contrib_pm, contrib_mp, contrib_mm) = map(
          append_zeros_fn,
          (contrib_pp, contrib_pm, contrib_mp, contrib_mm),
      )
      return contrib_pp, contrib_pm, contrib_mp, contrib_mm
  
imports: import tensorflow.compat.v2
  from tf_quant_finance import utils
label_desc: Use image node_id 4 for calling a global function with example usage: _construct_contribution_of_mixed_term(outer_coeff, inner_coeff, coord_grid, value_grid, dim1, dim2, batch_rank, lower_trim_indices, upper_trim_indices, has_default_lower_boundary, has_default_upper_boundary, n_dims) and returns: contrib_pp,  contrib_pm,  contrib_mp,  contrib_mm, None, contrib,  unknown,  unknown,  contrib, shifts