Quaternionic change of basis
02 Dec 2016A rotation can be described in two equivalent ways: as a change of basis or as a linear transformation (see dynamical pictures for cool examples from quantum mechanics). Rotations of bases are usually described using rotation matrices, whereas the preferred way to describe rotations of vectors is by using unit quaternions (a.k.a. versors). This raises a natural question: “Can one describe rotations of bases using quaternions and is it any better than using rotation matrices?” The answer is in both cases yes (thanks to Rudolf Lioutikov for convincing me of that), and moreover, the formulas for vector and versor coordinate transformations based on unit quaternions are truly elegant and simple.
Using matrices
To get started, let’s recall the matrix formulas that relate coordinates of vectors and rotation matrices in different bases. Assume there are two coordinate frames with a common origin: a base frame $B$ and a free frame $F$, and let $T$ be the transformation matrix containing the basis vectors of $F$ in basis $B$ as columns.
To get an intuition of how the formula for transformation of rotations works, imagine that $R$ stands for “get beer”, $T$ for “close fridge”, and $T^{-1}$ for “open fridge”. Beer is awaiting in a fridge, but you only know how to “get beer when fridge open”, which corresponds to $Q’$. What you want is to “get beer when fridge closed”, i.e., $Q$. The formula above provides a sequence of actions (from right to left) you need to take to “get beer when fridge closed”. Read it out loud: “Get beer when fridge closed = open fridge, get beer when fridge open, close fridge”. I think Hilbert would rejoice in this explanation.
Using quaternions
Unit quaternions rotate vectors by conjugation $v \mapsto q v q^{-1}$. Since change of basis is nothing else but a rotation of basis vectors, we can rotate each basis vector separately to describe the full basis rotation. Let versor $t$ represent rotation of basis $B$ into basis $F$, i.e., if $\left\{ e_1, e_2, e_3 \right\}$ are the basis vectors of $B$ and $\left\{ e’_1, e’_2, e’_3 \right\}$ are the basis vectors of $F$, then $e’_i = t e_i t^{-1}$ for $i \in \{ 1, 2, 3 \}$.
Formula \eqref{quat_vec} is straightforward to derive. Without loss of generality, we can identify basis $B$ with the standard basis in $\mathbb{R}^3$. Then $T = \begin{pmatrix}e’_1 & e’_2 & e’_3\end{pmatrix}$ and \eqref{mat_vec} can be written as $\xi = e’_i \xi’_i$ (summation assumed). Substituting $e’_i = t e_i t^{-1}$ and pulling $\xi’_i$ towards $e_i$, we obtain $\xi = t \left( e_i \xi’_i \right) t^{-1} = t \xi’ t^{-1}$.
Some confusion may arise at this point because formulas on Wikipedia have primes on the left-hand side. That is because they use active transformations whereas we use passive ones. See active vs passive transformations for more details.
To arrive at \eqref{quat_quat}, recall the derivation with matrices \begin{equation*} \left. \begin{array}{l} \left. \begin{array}{l} \eta = A \xi \newline \xi = T \xi^\prime \end{array} \right\} \Rightarrow \eta = A T \xi^\prime \newline \left. \begin{array}{l} \eta = T \eta^\prime \newline \eta^\prime = A^\prime \xi^\prime \end{array} \right\} \Rightarrow \eta = T A^\prime \xi^\prime \end{array} \right\} \Rightarrow A = T A^\prime T^{-1} \end{equation*} and convince yourself that the same derivation also works with quaternions.
Implementation
We can save quite some computation on Formula \eqref{quat_quat} if we notice that the scalar part of a unit quaternion is invariant under change of basis. Indeed, if $q’ = \left(\cos(\theta/2), u\sin(\theta/2)\right)$ with a unit vector $u \in \mathbb{R}^3$, the scalar part of $q’$ corresponds to the rotation angle around axis $u$. Rotation of a basis rotates $u$ but it cannot affect $\theta$, therefore we can simplify computation by just copying the scalar part of the quaternion and rotating the vector part by conjugation. Thanks to Ralf Grosse-Kunstleve for pointing that out.
Conclusion
Quaternions are fast, beautiful, and singularity-free. Use them for a great good!