docs: def apply_grad_zmat_tensor(grad_C, construction_table, cart_dist):
      """Apply the gradient for transformation to Zmatrix space onto cart_dist.
  
      Args:
          grad_C (:class:`numpy.ndarray`): A ``(3, n, n, 3)`` array.
              The mathematical details of the index layout is explained in
              :meth:`~chemcoord.Cartesian.get_grad_zmat()`.
          construction_table (pandas.DataFrame): Explained in
              :meth:`~chemcoord.Cartesian.get_construction_table()`.
          cart_dist (:class:`~chemcoord.Cartesian`):
              Distortions in cartesian space.
  
      Returns:
          :class:`Zmat`: Distortions in Zmatrix space.
      """
      if (construction_table.index != cart_dist.index).any():
          message = "construction_table and cart_dist must use the same index"
          raise ValueError(message)
      X_dist = cart_dist.loc[:, ['x', 'y', 'z']].values.T
      C_dist = np.tensordot(grad_C, X_dist, axes=([3, 2], [0, 1])).T
      if C_dist.dtype == np.dtype('i8'):
          C_dist = C_dist.astype('f8')
      try:
          C_dist[:, [1, 2]] = np.rad2deg(C_dist[:, [1, 2]])
      except AttributeError:
          C_dist[:, [1, 2]] = sympy.deg(C_dist[:, [1, 2]])
  
      from chemcoord.internal_coordinates.zmat_class_main import Zmat
      cols = ['atom', 'b', 'bond', 'a', 'angle', 'd', 'dihedral']
      dtypes = ['O', 'i8', 'f8', 'i8', 'f8', 'i8', 'f8']
      new = pd.DataFrame(data=np.zeros((len(construction_table), 7)),
                         index=cart_dist.index, columns=cols, dtype='f8')
      new = new.astype(dict(zip(cols, dtypes)))
      new.loc[:, ['b', 'a', 'd']] = construction_table
      new.loc[:, 'atom'] = cart_dist.loc[:, 'atom']
      new.loc[:, ['bond', 'angle', 'dihedral']] = C_dist
      return Zmat(new, _metadata={'last_valid_cartesian': cart_dist})