code: def _compute_residual_and_jacobian(
      x: np.ndarray,
      y: np.ndarray,
      xd: np.ndarray,
      yd: np.ndarray,
      k1: float = 0.0,
      k2: float = 0.0,
      k3: float = 0.0,
      p1: float = 0.0,
      p2: float = 0.0,
  ) -> Tuple[
      np.ndarray,
      np.ndarray,
      np.ndarray,
      np.ndarray,
      np.ndarray,
      np.ndarray,
  ]:
      """Auxiliary function of radial_and_tangential_undistort()."""
      # let r(x, y) = x^2 + y^2;
      #     d(x, y) = 1 + k1 * r(x, y) + k2 * r(x, y) ^2 + k3 * r(x, y)^3;
      r = x * x + y * y
      d = 1.0 + r * (k1 + r * (k2 + k3 * r))
  
      # The perfect projection is:
      # xd = x * d(x, y) + 2 * p1 * x * y + p2 * (r(x, y) + 2 * x^2);
      # yd = y * d(x, y) + 2 * p2 * x * y + p1 * (r(x, y) + 2 * y^2);
      #
      # Let's define
      #
      # fx(x, y) = x * d(x, y) + 2 * p1 * x * y + p2 * (r(x, y) + 2 * x^2) - xd;
      # fy(x, y) = y * d(x, y) + 2 * p2 * x * y + p1 * (r(x, y) + 2 * y^2) - yd;
      #
      # We are looking for a solution that satisfies
      # fx(x, y) = fy(x, y) = 0;
      fx = d * x + 2 * p1 * x * y + p2 * (r + 2 * x * x) - xd
      fy = d * y + 2 * p2 * x * y + p1 * (r + 2 * y * y) - yd
  
      # Compute derivative of d over [x, y]
      d_r = k1 + r * (2.0 * k2 + 3.0 * k3 * r)
      d_x = 2.0 * x * d_r
      d_y = 2.0 * y * d_r
  
      # Compute derivative of fx over x and y.
      fx_x = d + d_x * x + 2.0 * p1 * y + 6.0 * p2 * x
      fx_y = d_y * x + 2.0 * p1 * x + 2.0 * p2 * y
  
      # Compute derivative of fy over x and y.
      fy_x = d_x * y + 2.0 * p2 * y + 2.0 * p1 * x
      fy_y = d + d_y * y + 2.0 * p2 * x + 6.0 * p1 * y
  
      return fx, fy, fx_x, fx_y, fy_x, fy_y
  
imports: import copy
  import json
  from typing import Tuple, Union, Optional
  import numpy
  from nerfies import gpath
  from nerfies import types
label_desc: Use image node_id 1 for calling a global function with example usage: _compute_residual_and_jacobian(x, y, xd, yd, k1, k2, k3, p1, p2) and returns: fx,  fy,  fx_x,  fx_y,  fy_x,  fy_y