(quat1, quat2)
return (qmat @ quat2[..., None])[..., 0]
Computes the Hamilton product of two quaternions.

  Any number of leading batch dimensions is supported.

  Args:
    quat1: A quaternion [w, i, j, k].
    quat2: A quaternion [w, i, j, k].

  Returns:
    The quaternion product quat1 * quat2.
Computes the Hamilton product of two quaternions.
Computes the Hamilton product of two quaternions .
def quat_mul(quat1, quat2):
  """Computes the Hamilton product of two quaternions.

  Any number of leading batch dimensions is supported.

  Args:
    quat1: A quaternion [w, i, j, k].
    quat2: A quaternion [w, i, j, k].

  Returns:
    The quaternion product quat1 * quat2.
  """
  # Construct a (..., 4, 4) matrix to multiply with quat2 as shown below.
  qmat = quat1[..., _qmat_idx] * _qmat_sign

  # Compute the batched Hamilton product:
  # |w1 -i1 -j1 -k1|   |w2|   |w1w2 - i1i2 - j1j2 - k1k2|
  # |i1  w1 -k1  j1| . |i2| = |w1i2 + i1w2 + j1k2 - k1j2|
  # |j1  k1  w1 -i1|   |j2|   |w1j2 - i1k2 + j1w2 + k1i2|
  # |k1 -j1  i1  w1|   |k2|   |w1k2 + i1j2 - j1i2 + k1w2|
  return (qmat @ quat2[..., None])[..., 0]
def quat_mul ( quat1 , quat2 ) : # Construct a (..., 4, 4) matrix to multiply with quat2 as shown below. qmat = quat1 [ ... , _qmat_idx ] * _qmat_sign # Compute the batched Hamilton product: # |w1 -i1 -j1 -k1|   |w2|   |w1w2 - i1i2 - j1j2 - k1k2| # |i1  w1 -k1  j1| . |i2| = |w1i2 + i1w2 + j1k2 - k1j2| # |j1  k1  w1 -i1|   |j2|   |w1j2 - i1k2 + j1w2 + k1i2| # |k1 -j1  i1  w1|   |k2|   |w1k2 + i1j2 - j1i2 + k1w2| return ( qmat @ quat2 [ ... , None ] ) [ ... , 0 ]