code: def _append_boundary(
      dim,
      batch_rank,
      boundary_conditions,
      has_default_lower_boundary,
      has_default_upper_boundary,
      lower_trim_indices,
      upper_trim_indices,
      coord_grid,
      value_grid_in,
      current_value_grid_out,
      t,
  ):
      """Calculates and appends boundaries orthogonal to `dim`."""
      # E.g. for n_dims = 3, and dim = 1, the expected input grid shape is
      # (b, nx, ny-2, nz-2), and the output shape is (b, nx, ny, nz-2).
      # No update is done for the default boundary condition.
  
      def _reshape_fn(bound_coeff):
          # Say the grid shape is (b, nz, ny-2, nx-2), and dim = 1: we have already
          # restored the z-boundaries and now are restoring the y-boundaries.
          # The boundary condition coefficients are expected to have the shape
          # (b, nz, nx). We need to:
          # - Trim the boundaries which we haven't yet restored: nx -> nx-2.
          # - Expand dimension batch_rank+dim=2, because broadcasting won't always
          # do this correctly in subsequent computations.
          return _reshape_boundary_conds(
              bound_coeff,
              trim_from=batch_rank + dim,
              expand_dim_at=batch_rank + dim,
              # Skip all the trim indices which have already been updated
              lower_trim_indices=lower_trim_indices[dim + 1 :],
              upper_trim_indices=upper_trim_indices[dim + 1 :],
          )
  
      delta = _get_grid_delta(coord_grid, dim)
      if has_default_lower_boundary[dim]:
          # No update for the default BC
          first_value = None
      else:
          # Robin BC
          lower_value_first = _slice(
              current_value_grid_out, batch_rank + dim, 0, 1
          )
          lower_value_second = _slice(
              current_value_grid_out, batch_rank + dim, 1, 2
          )
          alpha_l, beta_l, gamma_l = boundary_conditions[dim][0](
              t, coord_grid
          )
          alpha_l, beta_l, gamma_l = (
              _prepare_boundary_conditions(
                  b, value_grid_in, batch_rank, dim
              )
              for b in (alpha_l, beta_l, gamma_l)
          )
          alpha_l, beta_l, gamma_l = map(
              _reshape_fn, (alpha_l, beta_l, gamma_l)
          )
          xi1, xi2, eta = _discretize_boundary_conditions(
              delta, delta, alpha_l, beta_l, gamma_l
          )
          first_value = (
              xi1 * lower_value_first + xi2 * lower_value_second + eta
          )
      if has_default_upper_boundary[dim]:
          # No update for the default BC
          last_value = None
      else:
          # Robin BC
          upper_value_first = _slice(
              current_value_grid_out, batch_rank + dim, -1, 0
          )
          upper_value_second = _slice(
              current_value_grid_out, batch_rank + dim, -2, -1
          )
          alpha_u, beta_u, gamma_u = boundary_conditions[dim][1](
              t, coord_grid
          )
          alpha_u, beta_u, gamma_u = (
              _prepare_boundary_conditions(
                  b, value_grid_in, batch_rank, dim
              )
              for b in (alpha_u, beta_u, gamma_u)
          )
          alpha_u, beta_u, gamma_u = map(
              _reshape_fn, (alpha_u, beta_u, gamma_u)
          )
          xi1, xi2, eta = _discretize_boundary_conditions(
              delta, delta, alpha_u, beta_u, gamma_u
          )
          last_value = (
              xi1 * upper_value_first + xi2 * upper_value_second + eta
          )
      return _append_first_and_last(
          first_value,
          current_value_grid_out,
          last_value,
          axis=batch_rank + dim,
      )
  
imports: import tensorflow.compat.v2
  from tf_quant_finance import utils
label_desc: Use image node_id 10 for calling a global function with example usage: _append_boundary(dim, batch_rank, boundary_conditions, has_default_lower_boundary, has_default_upper_boundary, lower_trim_indices, upper_trim_indices, coord_grid, value_grid_in, current_value_grid_out, t) and returns: _append_first_and_last, _reshape_boundary_conds