Skip to content

Quaternions

Definition

A quaternion H\mathbb{H} is an extended number system for complex numbers. It is represented as a four-element vector:

q=[q0q1q2q3]=[q0q1:3] \mathbf{q} = \begin{bmatrix} q_0 \\ q_1 \\ q_2 \\ q_3 \end{bmatrix} = \begin{bmatrix} q_0 \\ \mathbf{q}_{1:3} \end{bmatrix}

where q0q_0 is the real scalar part and q1,q2,q3q_1, q_2, q_3 are the real components of the quaternion vector part.

A unit quaternion can be used to represent the attitude of a rigid body or a rotation transformation. The rotation axis is encoded in the vector part and the rotation angle in the scalar part, through the relationship:

q=[cos ⁣(D2)cos(A)sin ⁣(D2)cos(B)sin ⁣(D2)cos(C)sin ⁣(D2)] \mathbf{q} = \begin{bmatrix} \cos\!\left(\frac{D}{2}\right) \\ \cos(A)\sin\!\left(\frac{D}{2}\right) \\ \cos(B)\sin\!\left(\frac{D}{2}\right) \\ \cos(C)\sin\!\left(\frac{D}{2}\right) \end{bmatrix}

where AA, BB, CC are the angles between the rotation axis vector and the xx, yy, zz axes respectively, and DD is the rotation angle around the rotation axis vector.

The unit constraint requires q2=q02+q12+q22+q32=1|\mathbf{q}|^2 = q_0^2 + q_1^2 + q_2^2 + q_3^2 = 1.

Quaternion Transformations

With rotations, reversing the axis of rotation or reversing the angle of rotation reverses the transformation. Reversing both at the same time leaves the total transformation unchanged. This gives four related quaternion transformations:

q:FaFbq:FaFb \mathbf{q} : \mathcal{F}^a \rightarrow \mathcal{F}^b \qquad -\mathbf{q} : \mathcal{F}^a \rightarrow \mathcal{F}^b qˉ:FaFbqˉ:FaFb \bar{\mathbf{q}} : \mathcal{F}^a \leftarrow \mathcal{F}^b \qquad -\bar{\mathbf{q}} : \mathcal{F}^a \leftarrow \mathcal{F}^b

Both q\mathbf{q} and q-\mathbf{q} represent the same physical rotation. This double cover of SO(3)SO(3) is an inherent property of unit quaternions.

Vector Rotation

Consider a vector xR3\mathbf{x} \in \mathbb{R}^3, with xa\mathbf{x}^a being the vector in frame Fa\mathcal{F}^a and xb\mathbf{x}^b being the same vector in frame Fb\mathcal{F}^b. The rotation is applied using the sandwich product:

[0xb]=q[0xa]q1 \begin{bmatrix} 0 \\ \mathbf{x}^b \end{bmatrix} = \mathbf{q} \cdot \begin{bmatrix} 0 \\ \mathbf{x}^a \end{bmatrix} \cdot \mathbf{q}^{-1}

where q\mathbf{q} is the unit quaternion representing the rotation between Fa\mathcal{F}^a and Fb\mathcal{F}^b.

Quaternion to DCM

The quaternion rotation relationship can be expanded to give the equivalent DCM in terms of quaternion components:

R(q)=[q02+q12q22q322q1q2+2q0q32q1q32q0q22q1q22q0q3q02q12+q22q322q2q3+2q0q12q1q3+2q0q22q2q32q0q1q02q12q22+q32] \mathbf{R}(\mathbf{q}) = \begin{bmatrix} q_0^2+q_1^2-q_2^2-q_3^2 & 2q_1q_2+2q_0q_3 & 2q_1q_3-2q_0q_2 \\ 2q_1q_2-2q_0q_3 & q_0^2-q_1^2+q_2^2-q_3^2 & 2q_2q_3+2q_0q_1 \\ 2q_1q_3+2q_0q_2 & 2q_2q_3-2q_0q_1 & q_0^2-q_1^2-q_2^2+q_3^2 \end{bmatrix}

DCM to Quaternion

The reverse mapping is more involved. Inspection of the DCM yields four candidate mappings:

q0(R)=12[1+r11+r22+r33(r23r32)/1+r11+r22+r33(r31r13)/1+r11+r22+r33(r12r21)/1+r11+r22+r33] \mathbf{q}_0(\mathbf{R}) = \frac{1}{2} \begin{bmatrix} \sqrt{1+r_{11}+r_{22}+r_{33}} \\ (r_{23}-r_{32})\,/\,\sqrt{1+r_{11}+r_{22}+r_{33}} \\ (r_{31}-r_{13})\,/\,\sqrt{1+r_{11}+r_{22}+r_{33}} \\ (r_{12}-r_{21})\,/\,\sqrt{1+r_{11}+r_{22}+r_{33}} \end{bmatrix} q1(R)=12[(r23r32)/1+r11r22r331+r11r22r33(r12+r21)/1+r11r22r33(r31+r13)/1+r11r22r33] \mathbf{q}_1(\mathbf{R}) = \frac{1}{2} \begin{bmatrix} (r_{23}-r_{32})\,/\,\sqrt{1+r_{11}-r_{22}-r_{33}} \\ \sqrt{1+r_{11}-r_{22}-r_{33}} \\ (r_{12}+r_{21})\,/\,\sqrt{1+r_{11}-r_{22}-r_{33}} \\ (r_{31}+r_{13})\,/\,\sqrt{1+r_{11}-r_{22}-r_{33}} \end{bmatrix} q2(R)=12[(r31r13)/1r11+r22r33(r12+r21)/1r11+r22r331r11+r22r33(r23+r32)/1r11+r22r33] \mathbf{q}_2(\mathbf{R}) = \frac{1}{2} \begin{bmatrix} (r_{31}-r_{13})\,/\,\sqrt{1-r_{11}+r_{22}-r_{33}} \\ (r_{12}+r_{21})\,/\,\sqrt{1-r_{11}+r_{22}-r_{33}} \\ \sqrt{1-r_{11}+r_{22}-r_{33}} \\ (r_{23}+r_{32})\,/\,\sqrt{1-r_{11}+r_{22}-r_{33}} \end{bmatrix} q3(R)=12[(r12r21)/1r11r22+r33(r31+r13)/1r11r22+r33(r23+r32)/1r11r22+r331r11r22+r33] \mathbf{q}_3(\mathbf{R}) = \frac{1}{2} \begin{bmatrix} (r_{12}-r_{21})\,/\,\sqrt{1-r_{11}-r_{22}+r_{33}} \\ (r_{31}+r_{13})\,/\,\sqrt{1-r_{11}-r_{22}+r_{33}} \\ (r_{23}+r_{32})\,/\,\sqrt{1-r_{11}-r_{22}+r_{33}} \\ \sqrt{1-r_{11}-r_{22}+r_{33}} \end{bmatrix}

The correct mapping to use is selected based on which expression gives a non-zero denominator:

q(R)={q0(R)if r22>r33,  r11>r22,  r11>r33q1(R)if r22<r33,  r11>r22,  r11>r33q2(R)if r22>r33,  r11<r22,  r11<r33q3(R)if r22<r33,  r11<r22,  r11<r33 \mathbf{q}(\mathbf{R}) = \begin{cases} \mathbf{q}_0(\mathbf{R}) & \text{if } r_{22} > -r_{33},\; r_{11} > -r_{22},\; r_{11} > -r_{33} \\ \mathbf{q}_1(\mathbf{R}) & \text{if } r_{22} < -r_{33},\; r_{11} > \phantom{-}r_{22},\; r_{11} > \phantom{-}r_{33} \\ \mathbf{q}_2(\mathbf{R}) & \text{if } r_{22} > \phantom{-}r_{33},\; r_{11} < \phantom{-}r_{22},\; r_{11} < -r_{33} \\ \mathbf{q}_3(\mathbf{R}) & \text{if } r_{22} < \phantom{-}r_{33},\; r_{11} < -r_{22},\; r_{11} < \phantom{-}r_{33} \end{cases}

Euler Angles to Quaternion

The conversion from Euler angles to a unit quaternion follows the same approach as generating a DCM from Euler angles — chain successive single-axis quaternion rotations:

q(Φijk)=qi(ϕ)qj(θ)qk(ψ) \mathbf{q}(\mathbf{\Phi}_{ijk}) = \mathbf{q}_i(\phi) \otimes \mathbf{q}_j(\theta) \otimes \mathbf{q}_k(\psi)

The single-axis quaternion rotations are:

qx(θ)=[cos(θ/2)sin(θ/2)00]qy(θ)=[cos(θ/2)0sin(θ/2)0]qz(θ)=[cos(θ/2)00sin(θ/2)] \mathbf{q}_x(\theta) = \begin{bmatrix} \cos(\theta/2) \\ \sin(\theta/2) \\ 0 \\ 0 \end{bmatrix} \qquad \mathbf{q}_y(\theta) = \begin{bmatrix} \cos(\theta/2) \\ 0 \\ \sin(\theta/2) \\ 0 \end{bmatrix} \qquad \mathbf{q}_z(\theta) = \begin{bmatrix} \cos(\theta/2) \\ 0 \\ 0 \\ \sin(\theta/2) \end{bmatrix}

For the XYZ sequence this gives:

q(Φxyz)=[cϕ/2cθ/2cψ/2+sϕ/2sθ/2sψ/2sϕ/2cθ/2cψ/2cϕ/2sθ/2sψ/2cϕ/2sθ/2cψ/2+sϕ/2cθ/2sψ/2cϕ/2cθ/2sψ/2sϕ/2sθ/2cψ/2] \mathbf{q}(\mathbf{\Phi}_{xyz}) = \begin{bmatrix} c_{\phi/2}\, c_{\theta/2}\, c_{\psi/2} + s_{\phi/2}\, s_{\theta/2}\, s_{\psi/2} \\ s_{\phi/2}\, c_{\theta/2}\, c_{\psi/2} - c_{\phi/2}\, s_{\theta/2}\, s_{\psi/2} \\ c_{\phi/2}\, s_{\theta/2}\, c_{\psi/2} + s_{\phi/2}\, c_{\theta/2}\, s_{\psi/2} \\ c_{\phi/2}\, c_{\theta/2}\, s_{\psi/2} - s_{\phi/2}\, s_{\theta/2}\, c_{\psi/2} \end{bmatrix}

where c()=cos()c_{(\cdot)} = \cos(\cdot) and s()=sin()s_{(\cdot)} = \sin(\cdot).

Quaternion to Euler Angles

The conversion to XYZ Euler angles from a unit quaternion is found by substituting the quaternion-to-DCM conversion into the DCM-to-Euler conversion:

Φxyz(q)=[arctan2 ⁣(2q2q3+2q0q1,  q02q12q22+q32)arcsin ⁣(2q1q32q0q2)arctan2 ⁣(2q1q2+2q0q3,  q02+q12q22q32)] \mathbf{\Phi}_{xyz}(\mathbf{q}) = \begin{bmatrix} \text{arctan2}\!\left(2q_2q_3 + 2q_0q_1,\; q_0^2 - q_1^2 - q_2^2 + q_3^2\right) \\ -\arcsin\!\left(2q_1q_3 - 2q_0q_2\right) \\ \text{arctan2}\!\left(2q_1q_2 + 2q_0q_3,\; q_0^2 + q_1^2 - q_2^2 - q_3^2\right) \end{bmatrix}

Properties Summary

Property Value
Parameters 4
Constraints 1 (unit norm)
Singularities None
Unique representation No — q\mathbf{q} and q-\mathbf{q} are equivalent
Inverse Conjugate qˉ\bar{\mathbf{q}}
Preferred use Propagation, integration, filtering