How Do Quaternions Work?

Posted on April 24, 2023 by Peter Shull

A quaternion is a 4 dimensional, hypercomplex number. What?! They have no intuitive meaning but are universally used for 3D orientations.


Quaternions are awesome because:

  1. they have no singularities

  2. successive rotations, interpolation, and extrapolation are easy

  3. computation time is fast.

The downside: individual quaternion elements have no physical meaning. More on that later, but first….

What is a quaternion?

A quaternion is simply a scalar and a vector combined together into a complex number with four elements of the form:

$$ \textbf{q} = q_0 + q_1\textbf{i} + q_2\textbf{j} + q_3\textbf{k}$$

For 3D representations and rotations, quaternions are always unit quaternions and thus:

$$ \sqrt{q_0^2 + q_1^2 + q_2^2 + q_3^2} = 1 $$

Quaternions are also hypercomplex, which means they have the following properties:

$$\textbf{i}^2 = \textbf{j}^2 = \textbf{k}^2 = \textbf{ijk} = -1 $$

History of Quaternions. On October 16, 1843, the Irish mathematician, William Hamilton, discovered quaternions while crossing the Broom Bridge in Dublin, Ireland. He was trying to extend standard complex numbers which could represent points in a plane such that complex numbers could also represent points in space. This etched stone plaque on the bridge commemorates his discovery of quaternions:

William Hamilton discovered quaternions. Little did he know that one day they would become one of the world’s most widely used methods for representing 3D orientations.

Now that we have a basic understanding of what quaternions are and where they came from….

Why are quaternions so popular?

Rotation matrices, Euler angles, angle-axis and quaternions can all be used to represent 3D orientations. While rotation matrices, Euler angles, and angle-axis are somewhat intuitive because their elements directly represent physical attributes, quaternion elements do not have any obvious physical meaning. However, quaternions are extremely popular for industrial and research applications, because they have no singularities, successive rotations and interpolation/extrapolation are simple and computation time is fast.

quaternions are extremely popular for industrial and research applications, because they have no singularities, successive rotations and interpolation/extrapolation are simple and computation time is fast.

Euler angles also compute quickly, but they contain singularities leading to gimbal lock in which one degree of freedom is lost, and it is not possible to adequately represent the all 3D orientations. It is difficult-to-impossible to compute successive rotations and interpolation/extrapolation with angle-axis and Euler angles. Rotation matrices have no singularities and successive rotations are straight-forward, but they are much slower to compute than the other 3 methods, because they contain 9 elements, while quaternions and angle-axis only contain 4 elements, and Euler angles only contain 3 elements. Interpolation/extrapolation is also extremely complex with rotation matrices. This table summarizes the pros and cons of each 3D orientation representation method and highlights why quaternions are so often used.

How are quaternions calculated?

The most straight-forward way to understand and compute quaternions is from the angle-axis 3D orientation representation. So, we present the math below for how this is done as well as conversion formulas from rotation matrices to quaternions and from Euler angles to quaternions.

angle-axis > quaternion

Given an angle-axis representation of a 3D orientation with a unit vector $\textbf{e} = \begin{bmatrix} e_x & e_y & e_z \end{bmatrix}$ indicating the axis of rotation and $\theta$ specifying the magnitude of rotation, the quaterion can be computed as: $$\textbf{q} = \begin{bmatrix} q_0 & q_1 & q_2 & q_3 \end{bmatrix}$$ $$q_0 = cos(\theta/2)$$ $$q_1 = e_x sin(\theta/2)$$ $$q_2 = e_y sin(\theta/2)$$ $$q_3 = e_z sin(\theta/2)$$

Angle-axis 3D representation

rotation matrix > quaternion

Given a 3D rotation matrix defined as:

$$ \textbf{R} = \begin{bmatrix} R_{11} & R_{12} & R_{13} \\ R_{21} & R_{22} & R_{23} \\ R_{31} & R_{32} & R_{33} \end{bmatrix}$$

the quaterion can be computed as: $$\textbf{q} = \begin{bmatrix} q_0 & q_1 & q_2 & q_3 \end{bmatrix}$$ $$q_0 = \frac{1}{2} \sqrt{1 + R_{11} + R_{22} + R_{33}}$$ $$q_1 = \frac{1}{4q_0} (R_{32} - R_{23})$$ $$q_2 = \frac{1}{4q_0} (R_{13} - R_{31})$$ $$q_3 = \frac{1}{4q_0} (R_{21} - R_{12})$$

Euler angles > quaternion

Euler angles are rotations about the body frame or the global fixed frame, sometimes referred to as intrinsic and extrinsic rotations, respectively.

Given the BodyZYX Euler angles ($\alpha, \beta, \gamma$) [z-y-x body frame rotations], the quaternion is computed as:

$$\textbf{q} = \begin{bmatrix} q_0 & q_1 & q_2 & q_3 \end{bmatrix}$$ $$q_0 = cos\frac{\alpha}{2} cos\frac{\beta}{2} cos\frac{\gamma}{2} + sin\frac{\alpha}{2} sin\frac{\beta}{2} sin\frac{\gamma}{2}$$ $$q_1 = cos\frac{\alpha}{2} cos\frac{\beta}{2} sin\frac{\gamma}{2} - sin\frac{\alpha}{2} sin\frac{\beta}{2} cos\frac{\gamma}{2}$$ $$q_2 = cos\frac{\alpha}{2} sin\frac{\beta}{2} cos\frac{\gamma}{2} + sin\frac{\alpha}{2} cos\frac{\beta}{2} sin\frac{\gamma}{2}$$ $$q_3 = sin\frac{\alpha}{2} cos\frac{\beta}{2} cos\frac{\gamma}{2} - cos\frac{\alpha}{2} sin\frac{\beta}{2} sin\frac{\gamma}{2}$$ $$ $$

Given the GlobalZXZ Euler angles ($\gamma, \beta, \alpha$) [z-x-z fixed frame rotations], the quaternion is computed as:

$$\textbf{q} = \begin{bmatrix} q_0 & q_1 & q_2 & q_3 \end{bmatrix}$$ $$q_0 = cos\frac{\alpha+\gamma}{2} cos\frac{\beta}{2}$$ $$q_1 = cos\frac{-\alpha+\gamma}{2} sin\frac{\beta}{2}$$ $$q_2 = sin\frac{-\alpha+\gamma}{2} sin\frac{\beta}{2}$$ $$q_3 = sin\frac{\alpha+\gamma}{2} cos\frac{\beta}{2}$$

How can quaternions rotate a vector?

To rotate the original vector $\mathbf{v_a}$ by the quaternion $\mathbf{q}$ to get the rotated vector $\mathbf{v_b}$, perform the following quaternion multiplication:

$$ \mathbf{v_b} = \mathbf{q}^{-1} \mathbf{v_a} \mathbf{q}$$

where,

$$ \mathbf{v_a} = \begin{bmatrix} 0 & v_{a1} & v_{a2} & v_{a3} \end{bmatrix}$$

$$ \mathbf{v_b} = \begin{bmatrix} 0 & v_{b1} & v_{b2} & v_{b3} \end{bmatrix}$$

$$\mathbf{q} = \begin{bmatrix} q_0 & q_1 & q_2 & q_3 \end{bmatrix}$$

$$\mathbf{q}^{-1} = \begin{bmatrix} q_0 & -q_1 & -q_2 & -q_3 \end{bmatrix}$$

$\mathbf{v_a}$ and $\mathbf{v_b}$ are vectors in quaternion form with a 0 filler for the first element, and $\mathbf{q}^{-1}$ is the inverse of $\mathbf{q}$.

Quaternion multiplication is associative, so the above can be computed as either $\mathbf{v_b} = (\mathbf{q}^{-1} \mathbf{v_a}) \mathbf{q}$ or $\mathbf{v_b} = \mathbf{q}^{-1} (\mathbf{v_a} \mathbf{q})$

 

And, quaternion multiplication is defined as:

$$\mathbf{a} = \mathbf{bc}$$

$$\begin{bmatrix} a_0 & a_1 & a_2 & a_3 \end{bmatrix} = \begin{bmatrix} b_0 & b_1 & b_2 & b_3 \end{bmatrix} \otimes \begin{bmatrix} c_0 & c_1 & c_2 & c_3 \end{bmatrix}$$

where,

$$a_0 = (b_0 c_0 - b_1 c_1 - b_2 c_2 - b_3 c_3)$$

$$a_1 = (b_0 c_1 + b_1 c_0 - b_2 c_3 + b_3 c_2)$$

$$a_2 = (b_0 c_2 + b_1 c_3 + b_2 c_0 - b_3 c_1)$$

$$a_3 = (b_0 c_3 - b_1 c_2 + b_2 c_1 + b_3 c_0)$$

Previous
Previous

Why You Should Switch to Python from Matlab

Next
Next

How Does IMU Sensor Fusion Work?