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}}\):
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:
Rotations¶
Most often, 3-d rotations are represented as \(3\times3\) matrices
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\):
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:
where \(\boldsymbol{q}\) is a unit quaternion (i.e.: \(\boldsymbol{v}^2 = \boldsymbol{1}\)). The quaternion is related to the rotation axis and angle:
Switching Representations¶
Equatorial -> Matrix¶
Let’s write \(M = R_{\alpha}^z R_{-\delta}^y R_{\rho}^x\) in matrix form:
Matrix -> Equatorial¶
From Eq. (6) we can invert and get:
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:
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\):
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:
The next step depends on which entry in Eq. (9) is the largest:
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:
and we can set:
and after normalizing:
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.