Skip to content

Instantly share code, notes, and snippets.

@cmower
Created April 3, 2023 17:06
Show Gist options
  • Save cmower/dcc415799ad4e5f6e06a68b8f9e08410 to your computer and use it in GitHub Desktop.
Save cmower/dcc415799ad4e5f6e06a68b8f9e08410 to your computer and use it in GitHub Desktop.
Averaging Quaternions
import numpy as np
def quaternion_average(*quaternions):
"""Compute the average quaternion."""
# Markley, F. Landis, Yang Cheng, John Lucas Crassidis, and Yaakov Oshman.
# "Averaging quaternions." Journal of Guidance, Control, and Dynamics, 2007.
# https://ntrs.nasa.gov/citations/20070017872
Q = np.concatenate([q.flatten().reshape(-1, 1) for q in quaternions], axis=1)
evals, evecs = np.linalg.eig(Q @ Q.T)
return evecs[:, evals.argmax()]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment