code: def _grad(Q, X, Y, weights, metric="euclid"):
      """Gradient of loss function between class means."""
  
      if metric == "euclid":
          return np.einsum(
              "a,abc->bc", weights, -4 * (X - Q @ Y @ Q.T) @ Q @ Y
          )
  
      elif metric == "riemann":
          M = np.linalg.inv(X) @ Q @ Y @ Q.T
          eigvals, eigvecs = np.linalg.eig(M)
          logeigvals = np.expand_dims(np.log(eigvals), -2)
          logM = (eigvecs * logeigvals) @ np.linalg.inv(eigvecs)
          return np.einsum("a,abc->bc", weights, 4 * logM @ Q)
  
      else:
          raise ValueError(
              "RPA supports only 'euclid' and 'riemann' metrics."
          )
  
file_path: pyRiemann/pyriemann/transfer/_rotate.py
imports: import warnings
  import numpy
  from ..utils.distance import distance
  from ..utils.utils import check_weights
label_desc: Use image node_id 4 for calling a global function with example usage: _grad(Q, X, Y, weights, metric) and returns: np, np