Quaternion Quick Facts

This is not meant to be a treatise on quaternions, but a summary relevant for the code implementation. It also assumes familiarity with linear algebra.

See also the Matrix and Quaternions FAQ for a practical compendium of equations for implementing matrix and quaternion operations.

Quaternions

The space of quaternions is a vector space generated by the basis \({\boldsymbol{1}, \boldsymbol{i}, \boldsymbol{j}, \boldsymbol{k}}\). In this space, quaternions are the sum of a scalar part generated by \({\boldsymbol{1}}\) and a vector component generated by \({\boldsymbol{i}, \boldsymbol{j}, \boldsymbol{k}}\).

A multiplication operation is defined over the space such that:

  • it is distributive with respect to the usual sum

  • the identity is the element \(\boldsymbol{1}\)

  • the products of the basis elements is given by \({\boldsymbol{i}, \boldsymbol{j}, \boldsymbol{k}}\):

(1)\[\begin{split}\begin{eqnarray} i^2 &=& j^2 = k^2 = -1 \\ i j &=& - j i = k \\ j k &=& - k j = i \\ k i &=& - i k = j \end{eqnarray}\end{split}\]

Example: The product of two pure vectors \(\boldsymbol{v} = \vec{v} = v_0 \boldsymbol{i} + v_1 \boldsymbol{j} + v_2 \boldsymbol{k}\) and \(\vec{u} = u_0 \boldsymbol{i} + u_1 \boldsymbol{j} + u_2 \boldsymbol{k}\) is:

\[\begin{split}\begin{eqnarray} \boldsymbol{v} \boldsymbol{u} &=& - (v_0 u_0 + v_1 u_1 + v_2 u_2)\boldsymbol{1} + (v_1 u_2 - v_2 u_1)\boldsymbol{i} + (v_2 u_0 - v_0 u_2)\boldsymbol{j} + (v_0 u_1 - v_1 u_0)\boldsymbol{k} \\ &=& - \vec{v} \cdot \vec{u} + \vec{v} \times \vec{u} \end{eqnarray}\end{split}\]

Rotations

Most often, 3-d rotations are represented as \(3\times3\) matrices

(2)\[\begin{split}\begin{eqnarray} M &=& \left[\begin{matrix} M_{00} & M_{01} & M_{02} \\ M_{10} & M_{11} & M_{12} \\ M_{20} & M_{21} & M_{22} \end{matrix}\right] \end{eqnarray}\end{split}\]

Some properties of rotations:

  • they are orthogonal: \(M^{-1} = M^T\) or \(M^T M = M M^T = \mathbb{1}\)

  • det(M) = 1

In what follows we use the following elemental (right-handed) rotations of \(\alpha\) around the axes \(R_{\alpha}^z\):

(3)\[\begin{split}\begin{eqnarray} R_{\alpha}^x &=& \left[\begin{matrix} 1 & 0 & 0 \\ 0 & \cos\alpha & -\sin\alpha \\ 0 & \sin\alpha & \cos\alpha \end{matrix}\right] \\ R_{\alpha}^y &=& \left[\begin{matrix} \cos\alpha & 0 & \sin\alpha \\ 0 & 1 & 0 \\ -\sin\alpha & 0 & \cos\alpha \end{matrix}\right] \\ R_{\alpha}^z &=& \left[\begin{matrix} \cos\alpha & -\sin\alpha & 0 \\ \sin\alpha & \cos\alpha & 0 \\ 0 & 0 & 1 \end{matrix}\right] \end{eqnarray}\end{split}\]

Angle-axis Representation

A 3-d rotation can be uniquely defined by a unit vector and an angle \((\vec{u}, \phi)\).

  • there is one and only one real-valued eigenvalue: \(\lambda = 1\).

  • the rotation axis is given by a unit eigenvector of M with eigenvalue \(\lambda = 1\).

  • the rotation angle is given by the trace of M: \(Tr(M) = 1 + 2 \cos \phi\).

Equatorial Representation

Any rotation can be decomposed as a sequence of three rotations around different cartesian axes. Using the equatorial coordinates related to a fixed intertial reference frame, this can be expressed as \(R_{\alpha}^z R_{-\delta}^y R_{\rho}^x\), where \({\rho}\), \({\delta}\) and \({\alpha}\) are roll, declination and right ascension respectively.

Quaternion Representation

A rotation can be represented by a quaternion operator:

(4)\[L_q(\boldsymbol{v}) = \boldsymbol{q}^* \boldsymbol{v} \boldsymbol{q},\]

where \(\boldsymbol{q}\) is a unit quaternion (i.e.: \(\boldsymbol{v}^2 = \boldsymbol{1}\)). The quaternion is related to the rotation axis and angle:

(5)\[\boldsymbol{q} = \cos \frac{\phi}{2} + \sin \frac{\phi}{2}\vec{u}\]

Switching Representations

Equatorial -> Matrix

Let’s write \(M = R_{\alpha}^z R_{-\delta}^y R_{\rho}^x\) in matrix form:

(6)\[\begin{split}\begin{eqnarray} M &=& \left[\begin{matrix}\cos{\alpha} & - \sin{\alpha} & 0\\\sin{\alpha} & \cos{\alpha} & 0\\0 & 0 & 1\end{matrix}\right] \left[\begin{matrix}\cos{\delta} & 0 & - \sin{\delta}\\0 & 1 & 0\\ \sin{\delta} & 0 & \cos{\delta}\end{matrix}\right] \left[\begin{matrix}1 & 0 & 0\\0 & \cos{\rho} & - \sin{\rho}\\0 & \sin{\rho} & \cos{\rho}\end{matrix}\right]\\ &=& \left[\begin{matrix}\cos{\alpha} \cos{\delta} & - \sin{\alpha} \cos{\rho} - \sin{\delta} \sin{\rho} \cos{\alpha} & \sin{\alpha} \sin{\rho} - \sin{\delta} \cos{\alpha} \cos{\rho}\\\sin{\alpha} \cos{\delta} & - \sin{\alpha} \sin{\delta} \sin{\rho} + \cos{\alpha} \cos{\rho} & - \sin{\alpha} \sin{\delta} \cos{\rho} - \sin{\rho} \cos{\alpha}\\ \sin{\delta} & \sin{\rho} \cos{\delta} & \cos{\delta} \cos{\rho}\end{matrix}\right] \end{eqnarray}\end{split}\]

Matrix -> Equatorial

From Eq. (6) we can invert and get:

\[\begin{split}\begin{eqnarray} \tan \alpha &=& \frac{M_{10}}{M_{00}} \\ \tan \rho &=& \frac{M_{21}}{M_{22}} \\ \sin \delta &=& - M_{20} \end{eqnarray}\end{split}\]

Quaternion -> Matrix

Expanding Eq. (4) using the multiplication rules in Eq. (1), and then grouping factors of \(v_*\), we can write this in matrix form:

(7)\[\begin{split}\begin{eqnarray} M &=& \left[\begin{matrix} 1 - 2(q_1^2 + q_2^2) & 2(q_0 q_1 - q_2 q_3) & 2(q_0 q_2 + q_1 q_3) \\2(q_0 q_1 + q_2 q_3) &1 - 2(q_0^2 + q_2^2) &2(q_1 q_2 - q_0 q_3) \\2(q_0 q_2 - q_1 q_3) &2(q_1 q_2 + q_0 q_3) &1 - 2(q_0^2 + q_1^2)\end{matrix}\right] \end{eqnarray}\end{split}\]

For practical reasons, some times we want to express the transform in terms of a quaternion with norm \(\left\| \boldsymbol{q} \right\| \neq 1\), only requiring that \(\left\| \boldsymbol{q} \right\| \neq 0\):

(8)\[\begin{split}\begin{eqnarray} M &=& \frac{1}{\left\| q \right\|^2} \left[\begin{matrix} \left\| q \right\|^2 - 2(q_1^2 + q_2^2) & 2(q_0 q_1 - q_2 q_3) & 2(q_0 q_2 + q_1 q_3) \\ 2(q_0 q_1 + q_2 q_3) & \left\| q \right\|^2 - 2(q_0^2 + q_2^2) &2(q_1 q_2 - q_0 q_3) \\ 2(q_0 q_2 - q_1 q_3) & 2(q_1 q_2 + q_0 q_3) & \left\| q \right\|^2 - 2(q_0^2 + q_1^2) \end{matrix}\right] \end{eqnarray}\end{split}\]

Matrix -> Quaternion

This is a bit more involved. The idea is to invert from Eq. (8). From the diagonal elements we can already get the squares of the quaternion components:

(9)\[\begin{split}S = \frac{\left\| q \right\|^2}{4} \left[\begin{matrix} 1 + M_{00} - M_{11} - M_{22} \\ 1 - M_{00} + M_{11} - M_{22} \\ 1 - M_{00} - M_{11} + M_{22} \\ 1 + M_{00} + M_{11} + M_{22} \end{matrix}\right] = \left[\begin{matrix} q_0^2 \\ q_1^2 \\ q_2^2 \\ q_3^2 \end{matrix}\right],\end{split}\]

The next step depends on which entry in Eq. (9) is the largest:

(10)\[\begin{split}\begin{eqnarray} 0 &\rightarrow& \left[ \sqrt{S_0}, \frac{(M_{01} + M_{10})}{4 \sqrt{S_0}}, \frac{(M_{02} + M_{20})}{4 \sqrt{S_0}}, \frac{(M_{21} - M_{12})}{4 \sqrt{S_0}} \right] \\ 1 &\rightarrow& \left[ \frac{(M_{01} + M_{10})}{4 \sqrt{S_1}}, \sqrt{S_1}, \frac{(M_{12} + M_{21})}{4 \sqrt{S_1}}, \frac{(M_{02} - M_{20})}{4 \sqrt{S_1}} \right] \\ 2 &\rightarrow& \left[ \frac{(M_{20} + M_{02})}{4 \sqrt{S_2}}, \frac{(M_{12} + M_{21})}{4 \sqrt{S_2}}, \sqrt{S_2}, \frac{(M_{10} - M_{01})}{4 \sqrt{S_2}} \right] \\ 3 &\rightarrow& \left[ \frac{(M_{21} - M_{12})}{4 \sqrt{S_3}}, \frac{(M_{02} - M_{20})}{4 \sqrt{S_3}}, \frac{(M_{10} - M_{01})}{4 \sqrt{S_3}}, \sqrt{S_3} \right] \end{eqnarray}\end{split}\]

Note that the denominator is always \(4 \sqrt{S_i}\), so we always choose the case with the largest denominator in order to minimize round-off errors.

Derivation. As an example, we derive one of the cases in Eq. (10). The rest are similar. The idea is to scale the quaternion so its largest component is equal to 1. In other words, if the i-th entry in Eq. (9) is the largest, then multiply q by a factor \(1/S_i\) to make \(q_i = 1\). The magnitude of the resulting quaternion is \(\left\| \boldsymbol{q} \right\| = 1/q_i\).

If \(S_1 = 1 - M_{00} + M_{11} - M_{22}\) is the largest, we scale q by \(1/\sqrt{S_1}\). The matrix in Eq. (8) then takes a simpler form:

\[\begin{split}\begin{eqnarray} M &=& S_1 \left[\begin{matrix} 1/S_1 - 2(1 + q_2^2) & 2(q_0 - q_2 q_3) & 2(q_0 q_2 + q_3) \\ 2(q_0 + q_2 q_3) & 1/S_1 - 2(q_0^2 + q_2^2) &2(q_2 - q_0 q_3) \\ 2(q_0 q_2 - q_3) & 2(q_2 + q_0 q_3) & 1/S_1 - 2(q_0^2 + 1)\end{matrix}\right] \end{eqnarray}\end{split}\]

and we can set:

\[\begin{split}\begin{eqnarray} q_0 &=& \frac{(M_{01} + M_{10})}{4 S_1} \\ q_1 &=& 1 \\ q_2 &=& \frac{(M_{12} + M_{21})}{4 S_1} \\ q_3 &=& \frac{(M_{02} - M_{20})}{4 S_1} \\ \left\| q \right\| &=& \frac{1}{\sqrt{S_1}} \end{eqnarray}\end{split}\]

and after normalizing:

\[q = \left[ \frac{(M_{01} + M_{10})}{4 \sqrt{S_1}}, \sqrt{S_1}, \frac{(M_{12} + M_{21})}{4 \sqrt{S_1}}, \frac{(M_{02} - M_{20})}{4 \sqrt{S_1}} \right]\]

Equatorial -> Quaternion

This code first transforms from equatorial to matrix, and then from matrix to quaternion.

Quaternion -> Equatorial

Transform from quaternion to matrix, and then from matrix to equatorial.