es

This section is divided into a number of subsections, links to which are:

Rotations

2D Rotations

3D Rotations

Euler Rotation Theorem

3D Rotations via Shearing

Compositions

Cayley Decomposition

 

This section demonstrates how to use quaternions for practical applications. You suppose to build intuition about how to use them as building blocks to solve engineering and geometry problems. Applications include computer graphics, attitude systems for air and spacecraft, biomechanics, and structural chemistry models. You may have read that quaternions have advantages over Euler angles to represent orientations, including preventing gimbal lock, and advantages over rotation matrices, such as more efficient computation, and compact form.

There are two conventions used in quaternion computations: The original one created by Hamilton, and one popularized by NASA's Jet Propulson Library (JPL). They sometimes go by the names Hamilton quaternions and JPL quaternions. The key difference between the two conventions lies in the relation between the three imaginary bases. In Hamilton convention, ijk = −1, while JPL defines ijk = 1. This section exclusively uses Hamilton quaternions.

Up until now we have learned that a rotation in ℝ³ about an axis through the origin can be represented by a 3 × 3 orthogonal matrix with determinant 1. However, the matrix representation seems redundant because only four of its nine elements are independent. Also the geometric interpretation of such a matrix is not clear until we carry out several steps of calculation to extract the rotation axis and angle. Furthermore, to compose two rotations, we need to compute the product of the two corresponding matrices, which requires twenty-seven multiplications and eighteen additions. It turns out that quaternions are very efficient in presenting rotations in ℝ³.

Quaternions

William Hamilton
We will now delve into a subject, which at first may seem quite unrelated to rotation problems. This story begins in the early 19th century, when the great Irish mathematician William Rowan Hamilton (1805--1865) was searching for a generalization of the complex number system well before the theory of vector spaces had been fully developed. Sir Hamilton was an Irish physicist and mathematician then at the height of his renown. Early on, he understood that the complex numbers served as a model for 2-dimensional real space, a setting for elementary physics. Three-space is an even better setting for physics so Hamilton was after finding a model analogous to ℂ that would provide an algebraic framework for modeling 3-space. After a years-long search, he discovered the quaternions, designated ℍ in Hamilton’s honor.

A legend is that in 1843, Hamilton was walking with his wife Helen at the Royal Irish Academy when he was suddenly struck by the idea of adding a fourth dimension in order to multiply triples. Excited by this breakthrough, as the couple passed the Broome Bridge of the Royal Canal, he carved the newfound theory of quaternions. You can recall a similar breakthrough of complex numbers discovery by the Italian mathematician and gambler Gerolamo Cardano (1501--1576), who achieved it when solving cubic equation.

As a vector space over ℝ, the quaternion space ℍ is 4-dimensional, which means that every its element is a 4-tuple: q = (q₀, q₁, q₂, q₃) with four real entries. However, ℍ is not isomorphic to ℝ4 because it has different algebraic structure. The quaternion number system admits multiplication between its elements and it is isomorphic to the direct sum ℝ⊕ℝ³. After many failed attempts, Hamilton finally came up with the idea of using three imaginary symbols i, j, and k that together with "1" form a basis in ℍ. Quaternions are generally represented in the form

\begin{equation} \label{EqQuart.1} \mathbf{q} = a + b\,{\bf i} + c\,{\bf j} + d\,{\bf k} \end{equation}
where it is assumed that the real number 𝑎 is multiplied by the identity quaternion:    1 = (1, 0, 0, 0), which does produce no rotation. Although unit 4-tuples i = (0, 1, 0, 0), j = (0, 0, 1, 0), and k = (0, 0, 0, 1) are elements of ℍ, it is customary consider these vectors as familiar unit vectors i = (1, 0, 0), j = (0, 1, 0), and k = (0, 0, 1) in ℝ³, which is embedded into ℝ4. These basis symbols 1, i, j, k behave as follows:
\begin{equation} \label{EqQuart.2} \mathbf{i}^2 = \mathbf{j}^2 = \mathbf{k}^2 = \mathbf{ijk} = -1 \quad \mbox{and} \quad \mathbf{ij} = \mathbf{k}, \ \mathbf{jk} = \mathbf{i}, \ \mathbf{ki} = \mathbf{j} . \end{equation}
Combining these relations, it follows that ji = −k, kj = −i and ik = −j. The skew symmetry of multiplication (e.g., ij = −ji) was actually a major leap, since multiplication systems up to that time had been commutative.) This recipe is captured in Figure bellow. Taking the product of two successive elements in the clockwise direction, we get the third element. When going counterclockwise, we introduce a minus sign.

Roger
figure 10.2 from Dilong, page 266
Product basis elements.
    Hamilton's multiplication table
The set of quaternions is a 4-dimensional vector space over ℝ with basis {1, i, j, k} where 1 is in ℝ and i, j, k are mutually orthogonal imaginary unit vectors (or symbols) with the multiplication property \eqref{EqQuart.2}. The set of all quaternions is denoted by \[ \mathbb{H} = \left\{ a + b\mathbf{i} + c \mathbf{j} + d \mathbf{k} \ : \ a, b, c, d \in \mathbb{R} \right\} . \]

Similar to the case of complex numbers, every quaternion is designated as

\[ \mathbf{q} = a + b\,\mathbf{i} + c\, \mathbf{j} + d\, \mathbf{k} = a + \left( b\,\mathbf{i} + c\, \mathbf{j} + d\, \mathbf{k} \right), \]
So a quaternion q is a direct sum of two components, a scalar 𝑎 and a three-dimensional vector u(b, c, d). Since ℍ is isomorphic to the direct sum ℝ⊕ℝ³ as a vector space, it is convenient to represent a quaternion as q = (𝑎 u), where 𝑎 is the real part of q and u = (b, c, d) is a vector in 3-space, forming a pure quaternion. It is to be understood that 𝑎, b, c, d are all real. The real part of q = 𝑎 + bi + cj + dk in ℍ is 𝑎 and the pure quaternion part is bi + cj + dk. If 𝑎 = 0, then q is a pure quaternion.

Then we define the following quantities.

Conjugate:    q* = (𝑎 ; −u), where u = (b, c, d) ∈ ℝ³.
Modulus: \( \displaystyle \quad \| \mathbf{q} \| = \sqrt{a^2 + b^2 + c^2 + d^2} = \sqrt{a^2 + \left( \mathbf{w} \bullet \mathbf{w} \right)} = \sqrt{a^2 + \| {\bf w}\|^2} . \quad \)
Unit Quaternion:    q = (𝑎, b, c, d) is said to be a unit quaternion if ∥q∥ = 1. A unit quaternion is also referred to as the rotation quaternion.

Since qq* = 𝑎² + b² + c² + d² is a nonnegative real number, we can define the modulus of q in ℍ by

\[ \| \mathbf{q} \| = +\sqrt{{\bf q}\,{\bf q}^{\ast}} = +\sqrt{{\bf q}^{\ast} {\bf q}} = \sqrt{a^2 + b^2 + c^2 + d^2} . \]

Quaternions can be written in a matrix form, Since there are 48 distinct matrix representations of the same quaternion q = 𝑎 + bi + cj + dk, we present one of them:

\begin{align*} \begin{bmatrix} a&d&-b&-c \\ -d&a& c& -b \\ b&-c & a & -d \\ c&b&d&a \end{bmatrix} &= a \begin{bmatrix} 1&0&0&0 \\ 0&1&0&0 \\ 0&0&1&0 \\ 0&0&0&1 \end{bmatrix} + b \begin{bmatrix} 0&0&1&0 \\ 0&0&0&-1 \\ 1&0&0&0 \\ 0&1&0&0 \end{bmatrix} \\ & \quad + c \begin{bmatrix} 0&0&0&-1 \\0&0&1&0 \\ 0&-1&0&0 \\ 1&0&0&0 \end{bmatrix} + d \begin{bmatrix} 0&1&0&0 \\ -1&0&0&0 \\ 0&0&0&-1 \\ 0&0&1&0 \end{bmatrix} . \end{align*}

Quaternion Multiplication

We define multiplication in ℍ using (1), the distributive law, and the usual rules of arithmetic in ℝ. Letting the chips fall where they may from there, we find that multiplication in ℍ is more exciting than multiplication in ℂ. For example, consider two quaternions q = (s, u) and p = (t, v):

\begin{align*} \mathbf{q} &= (s, {\bf u}) = s + u_x\mathbf{i} + u_y\mathbf{j} + u_z {\bf k} , \\ \mathbf{p} &= (t, {\bf v}) = t + v_x\mathbf{i} + v_y\mathbf{j} + v_z {\bf k} \end{align*}
If we multiply these two together, we’ll get lots of cross-product terms, such as (uxi)(vyj), but we can simplify these by using Hamilton’s rules. That is, (uxi)(vyj) = uxvy(ij) = uxvyk. If we do this, simplify, and collect common terms, we get a very messy formula involving 16 different terms. The derivation is left as an exercise. However, lazy people like me prefer to use a computer algebra system. Fortunately, Mathematica has a dedicated command (upon implementation of the package):
<< Quaternions`
Quaternion[a, b, c, d]

The formula for quaternion multiplication can be expressed somewhat succinctly in the following form (Sir Hamilton could not find a way to define multiplication):

Theorem 1: If f : ℍ ⤑ ℝ ⊕ ℝ³ is given by \[ f\left( a + b\mathbf{i} + c\mathbf{j} + d\mathbf{k} \right) = \left( a : \mathbf{v} \right) , \quad\mbox{with} \quad \mathbf{v} = \left( b, c, d \right) \in \mathbb{R}^3 , \] then f is an isomorphism of vector spaces. Moreover, if q₁, q₂ are in ℍ with f(q₁) = (𝑎₁ ; v₁) and f(q₂) = (𝑎₂ ; v₂), then \begin{equation} \label{EqQuart.3} f \left( {\bf q}_1 {\bf q}_2 \right) = \left( a_1 a_2 - \left( {\bf v}_1 \bullet {\bf v}_2 \right) \, ; \, a_1 {\bf v}_2 + a_2 {\bf v}_2 + \left( {\bf v}_1 \times {\bf v}_2 \right) \right) , \end{equation} where "•" is the Euclidean dot product on ℝ³ and "×" is the cross-product on ℝ³.
Note that the above expression is in the quaternion scalar-vector form. The first term st − (uv) evaluates to a scalar (recalling that the dot product returns a scalar), and the second term (sv + tu + u × v) is a sum of three vectors, and so is a vector in ℝ³. It can be shown that quaternion multiplication is associative, but not commutative.    
Example 1: Suppose the two vectors are given as follows \[ \begin{split} \mathbf{p} &= \begin{bmatrix} 1& 2& 3& 4 \end{bmatrix} = 1 + 2\,{\bf i} + 3\,{\bf j} + 4\,{\bf k} , \\ \mathbf{q} &= \begin{bmatrix} 5& 4& 3& 2 \end{bmatrix} = 5 + 4\,{\bf i} + 3\,{\bf j} + 2\,{\bf k} . \end{split} \] We single out their vector parts u = (2, 3, 4) and v = (4, 3, 2) and calculate their inner and cross products: \[ \begin{split} \mathbf{u} \bullet \mathbf{v} &= 25, \\ \mathbf{u} \times \mathbf{v} &= \begin{pmatrix} -6 & 12 & -6 \end{pmatrix} . \end{split} \]
u = {2, 3, 4}; v = {4, 3, 2}; u . v
25
Cross[u, v]
{-6, 12, -6}
Then quaternion product becomes \begin{align*} \mathbf{p}\,\mathbf{q} &= 5 - 25 + \mathbf{u} + 5\,\mathbf{v} + \mathbf{u} \times \mathbf{v} \\ &= \end{align*}    ■
End of Example 1

A product of two quaternions is again a quaternion (which we write as a column):

\[ \mathbf{p}\, \mathbf{q} = \begin{pmatrix} p_0 q_0 - p_x q_x - p_y q_y - p_z q_z \\ p_x q_0 + p_0 q_x - p_z q_y + p_y q_z \\ p_y q_0 + p_z q_x + p_0 q_y - p_x q_z \\ p_z q_0 - p_y q_x + p_x q_y + p_0 q_z \end{pmatrix} . \]
We can also express this as a matrix/vector product, which is useful for computer implementation: if 𝒓 = (r₀, r₁, r₂, r₃) = 𝒑 𝒒, then
\[ \mathbf{r} = \begin{pmatrix} r_0 \\ r_1 \\ r_2 \\ r_3 \end{pmatrix} = \begin{bmatrix} p_0 & - p_x & -p_y & -p_z \\ p_x & p_0 & - p_z & p_y \\ p_y & p_z & p_0 & - p_x \\ p_z & -p_y & p_x & p_0 \end{bmatrix} \begin{pmatrix} q_0 \\ q_x \\ q_y \\ q_z \end{pmatrix} . \]
We can even express quaternions themselves as 4x4 matrices and have all of the normal notation carry over:
\[ \begin{bmatrix} r_0 & - r_x & -r_y & -r_z \\ r_x & r_0 & - r_z & r_y \\ r_y & r_z & r_0 & - r_x \\ r_z & -r_y & r_x & r_0 \end{bmatrix} = \begin{bmatrix} p_0 & - p_x & -p_y & -p_z \\ p_x & p_0 & - p_z & p_y \\ p_y & p_z & p_0 & - p_x \\ p_z & -p_y & p_x & p_0 \end{bmatrix} \begin{bmatrix} q_0 & - q_x & -q_y & -q_z \\ q_x & q_0 & - q_z & q_y \\ q_y & q_z & q_0 & - q_x \\ q_z & -q_y & q_x & q_0 \end{bmatrix} \]
Q = {{q0, -q1, -q2, -q3}, {q1, q0, -q3, q2}, {q2, q3, q0, -q1}, {q3, -q2, q1, q0}};
P = {{p0, -p1, -p2, -p3}, {p1, p0, -p3, p2}, {p2, p3, p0, -p1}, {p3, -p2, p1, p0}};
Q1 = {{q0, q1, q2, q3}, {-q1, q0, q3, -q2}, {-q2, -q3, q0, q1}, {-q3, q2, -q1, q0}};
Simplify[Q1 . P . Q]/. p0 -> 0
{{0, -2 (-p3 q0 q2 + p2 q1 q2 + p2 q0 q3 + p3 q1 q3) + p1 (-q0^2 - q1^2 + q2^2 + q3^2), -2 (p3 q0 q1 + p1 q1 q2 - p1 q0 q3 + p3 q2 q3) + p2 (-q0^2 + q1^2 - q2^2 + q3^2), -2 (-p2 q0 q1 + p1 q0 q2 + p1 q1 q3 + p2 q2 q3) + p3 (-q0^2 + q1^2 + q2^2 - q3^2)}, {2 (-p3 q0 q2 + p2 q1 q2 + p2 q0 q3 + p3 q1 q3) + p1 (q0^2 + q1^2 - q2^2 - q3^2), 0, -2 (-p2 q0 q1 + p1 q0 q2 + p1 q1 q3 + p2 q2 q3) + p3 (-q0^2 + q1^2 + q2^2 - q3^2), 2 (p3 q0 q1 + p1 q1 q2 - p1 q0 q3 + p3 q2 q3) + p2 (q0^2 - q1^2 + q2^2 - q3^2)}, {2 (p3 q0 q1 + p1 q1 q2 - p1 q0 q3 + p3 q2 q3) + p2 (q0^2 - q1^2 + q2^2 - q3^2), 2 (-p2 q0 q1 + p1 q0 q2 + p1 q1 q3 + p2 q2 q3) + p3 (q0^2 - q1^2 - q2^2 + q3^2), 0, -2 (-p3 q0 q2 + p2 q1 q2 + p2 q0 q3 + p3 q1 q3) + p1 (-q0^2 - q1^2 + q2^2 + q3^2)}, {2 (-p2 q0 q1 + p1 q0 q2 + p1 q1 q3 + p2 q2 q3) + p3 (q0^2 - q1^2 - q2^2 + q3^2), -2 (p3 q0 q1 + p1 q1 q2 - p1 q0 q3 + p3 q2 q3) + p2 (-q0^2 + q1^2 - q2^2 + q3^2), 2 (-p3 q0 q2 + p2 q1 q2 + p2 q0 q3 + p3 q1 q3) + p1 (q0^2 + q1^2 - q2^2 - q3^2), 0}}

Since ℍ is not a field, ℍ² is not a vector space. In treatments of matrix groups for mature audiences, though, matrices over ℍ are embraced as part of the family of linear operators. Though we will not delve into this, it is worth a moment to look at an actual 2 × 2 matrix over ℍ to see what transpires when our scalars come from ℍ.

Quaternion Multiplication and 3-d Rotations

So far, everything we have said about quaternions seems to be purely abstract algebraic manipulations. We will show that in fact, they provide a natural way to encode 3-dimensional rotations. First, let us define a multiplicative inverse for every nonzero quaternion:

\[ \mathbf{q}^{-1} = \frac{1}{\| {\bf q} \|^2}\,\mathbf{q}^{\ast} . \]
To verify this identity, just multiply both sides by q,
\[ \mathbf{q}\,\mathbf{q}^{-1} = \mathbf{q}\,\frac{1}{\| {\bf q} \|^2}\,\mathbf{q}^{\ast} = \frac{1}{\| {\bf q} \|^2}\,\mathbf{q}\,\mathbf{q}^{\ast} = \frac{1}{\| {\bf q} \|^2}\,\| \mathbf{q} \|^2 = 1. \]
Certainly, we have the inclusion ℝ ⊆ ℂ ⊆ ℍ. As a set with two binary operations that satisfy all the field axioms except commutativity of multiplication, ℍ is a skew-field.

Observe that if q is a unit quaternion---we consider only such quaternions---then it follows that q−1 = q*. As you might have guessed, our objective will be to show that there is a relation between rotating vectors and multiplying quaternions. In order to apply this insight, we need first to show how to represent rotations as quaternions and 3-dimensional vectors as quaternions.

Since quaternions provide a concise method of representing the automorphisms of three- and four-dimensional spaces, it becomes beneficial to use them in practical applications. Ken Shoemake was probably the first (1985) who discovered the connection between rotations and quaternions. For this reason, quaternions are used in computer graphics, control theory, robotics, signal processing, attitude control, physics, bioinformatics, and orbital mechanics. For example, it is common for spacecraft attitude-control systems to be commanded in terms of quaternions. Tomb Raider (1996) is often cited as the first mass-market computer game to have used quaternions to achieve smooth 3D rotation.

When we build a rotation in ℝ³, we need an angle Θ and the axis of rotation, which we specify by a unit vector along it, n = (nx, ny, nz) with \( \displaystyle \quad n_x^2 + n_y^2 + n_z^2 = 1 . \quad \) These four parameters (Θ and three components of n) uniquely identify both, a rotation by angle Θ along the axis n and the quaternion.

Let us consider a unit quaternion q = q₀ + u only. Then q0² + ‖u‖² = 1 implies that there must exist some angle θ such that

\begin{align*} \cos^2 \theta &= q_0^2 , \\ \sin^2 \theta &= \| \mathbf{u} \|^2 . \end{align*}
In fact, there exists a unique θ ∈ [0, π] such that cos θ = q₀ and sin θ = ‖u‖. The unit quaternion q = q₀ + u can now be written in terms of the angle θ and the unit vector w = u/∥u∥:
\[ \mathbf{q} = \cos\theta + \mathbf{w}\,\sin\theta = \cos\theta + \sin\theta \left( x, y, z \right) \qquad \left( \| \mathbf{q} \| = 1 \right) . \]
Then
\[ \mathbf{q}^2 = \cos \left( 2\,\theta \right) + \sin\left( 2\,\theta \right) \left( x, y, z \right) \qquad \left( \| \mathbf{q} \| = 1 \right) \]
and
\[ \mathbf{q}^3 = \cos \left( 3\,\theta \right) + \sin\left( 3\,\theta \right) \left( x, y, z \right) \qquad \left( \| \mathbf{q} \| = 1 \right) . \]
Using the unit quaternion q = q₀ + u ∈ U(1,ℍ) with ∥q∥ = q0² + ‖u‖² = 1, we define a linear operator on ℝ³ that acts on vector v ∈ ℝ³ by considering it as a pure quaternion (0, v):
\begin{align} \label{EqQuart.4} L_q ({\bf v}) &= {\bf q} \left( 0; \mathbf{v} \right) {\bf q}^{\ast} = {\bf q} \left( 0; \mathbf{v} \right) {\bf q}^{-1} \\ &= \left( q_0^2 - \| {\bf u} \|^2 \right) \mathbf{v} + 2 \left( \mathbf{u} \bullet \mathbf{v} \right) \mathbf{u} + 2 q_0 \left( \mathbf{u} \times \mathbf{v} \right) . \notag \end{align}
Here we make two observations. First, the quaternion operator \eqref{EqQuart.4} does not change the length of the vector v:
\[ \| L_q ({\bf v}) \| = \| {\bf q} \left( 0; \mathbf{v} \right) {\bf q}^{\ast} \| = \| {\bf q} \| \,\| {\bf v} \| \,\|{\bf q}^{\ast} \| = \| {\bf q} \| \, \| {\bf q}^{-1} \| \, \| {\bf v} \| = \| {\bf v} \| . \]
Second, the direction of v, if along u, is left unchanged by the operator Lq. To verify this, we let v = ku and have
\begin{align*} {\bf q}\left( 0; {\bf v} \right) {\bf q}^{\ast} &={\bf q}\left( 0; k\,{\bf u} \right) {\bf q}^{\ast} \\ &= \left( q_0^2 - \| \mathbf{u} \|^2 \right) \left( k\,{\bf u} \right) + 2 \left( \mathbf{u} \bullet k\,{\bf u} \right) \mathbf{u} + 2 q_0 \left( \mathbf{u} \times k\,\mathbf{u} \right) \\ &= k \left( q_0^2 - \| \mathbf{u} \|^2 \right) {\bf u} = k\,{\bf u} . \end{align*}
These two observations make us guess that the operator Lq acts like a rotation about q. This is made precise by the next theorem.
Theorem 2 (Shoemake, 1985): Let P(x, y, z) be an arbitrary point in three-dimensional space ℝ³, represented as a pure quaternion (0, v), where v = (x, y, z). For any unit quaternion \[ \mathbf{q} = q_0 + {\bf u} = \cos\theta + \mathbf{w}\,\sin\theta \qquad \left( \| \mathbf{q} \|^2 = q_0^2 + \| \mathbf{u} \|^2 = \| \mathbf{w} \|^2 = 1 \right) , \] the action of the operator \begin{equation} \label{EqQuat.5} L_q \left( \mathbf{v} \right) = {\bf q}\left( 0; \mathbf{v} \right) {\bf q}^{\ast} = {\bf q}\left( 0; \mathbf{v} \right) {\bf q}^{-1} \end{equation} on v ∈ ℝ³ is equivalent to a passive rotation of the vector (and point P) through an angle 2θ about u as the axis of rotation.
Given a vector v ∈ ℝ³, we decompose it as v = a + n, where a = u is the component along the vector u and n = u is the component normal to u. Then we show that under the operator Lq, vector a is invariant, while n is rotated about w through an angle 2θ. Since the operator is linear, this shows that the image qvq* is indeed interpreted as a rotation of v about w through an angle 2θ.

We know from an early reasoning that a is invariant under Lq. So let us focus on the effect of Lq on the orthogonal component n. We have \begin{align*} L_q (\mathbf{n}) &= \left( q_0^2 - \| \mathbf{u} \|^2 \right) {\bf n} + 2 \left( \mathbf{u} \bullet {\bf n} \right) \mathbf{u} + 2 q_0 \left( \mathbf{u} \times \mathbf{n} \right) \\ &= \left( q_0^2 - \| \mathbf{u} \|^2 \right) {\bf n} + 2 q_0 \left( \mathbf{u} \times \mathbf{n} \right) \\ &= \left( q_0^2 - \| \mathbf{u} \|^2 \right) {\bf n} + 2 q_0 \| \mathbf{u} \| \left( \mathbf{w} \times \mathbf{n} \right) , \end{align*} where in the last step above we introduced w = u/‖u‖. Denote n = w × n. So the last equation becomes \[ L_q (0; \mathbf{n}) = \left( q_0^2 - \| \mathbf{u} \|^2 \right) {\bf n} + 2 q_0 \| \mathbf{u} \| \,\mathbf{n}_{\perp} . \] Note that n and n have the same length: \[ \| \mathbf{n}_{\perp} \| = \| \mathbf{n} \times \mathbf{w} \| = \| \mathbf{n} \| \, \| \mathbf{w} \| \,\sin\frac{\pi}{2} = \| \mathbf{n} \| . \] Finally, we rewrite Lq(v) into the form \begin{align*} L_q (\mathbf{v} ) &= \left( \cos^2 \theta - \sin^2 \theta \right) \mathbf{n} + \left( 2\cos\theta\,\sin\theta \right) \mathbf{n}_{\perp} \\ &= \left( \cos\, 2\theta \right) \mathbf{n} + \left( \sin \,2\theta \right) \mathbf{n}_{\perp} . \end{align*} Namely, the resulting vector is a rotation of n through an angle 2θ in the plane defined by n and n. See the figure below. This vector is clearly orthogonal to the rotation axis.

see page 5 of https://graphics.stanford.edu/courses/cs348a-17-winter/Papers/quaternion.pdf

We substitute the unit quaternion form q = q₀ + u into (3) to obtain the resulting vector from rotating a vector v about the axis u through 2θ: \begin{align*} L_q \left( \mathbf{v} \right) &= \left( \cos^2 \theta - \sin^2 \theta \right) \mathbf{v} + 2 \left( \mathbf{w}\,\sin\theta \bullet \mathbf{v} \right) \mathbf{w}\,\sin\theta + 2 \cos \theta \left( \mathbf{w}\,\sin\theta \times \mathbf{v} \right) \\ &= \cos (2\theta )\,\mathbf{v} + \left( 1 - \cos 2\theta \right) \left(\mathbf{w} \bullet \mathbf{v} \right) \mathbf{w} + \sin (2\theta ) \left( \mathbf{w} \times \mathbf{v} \right) . \end{align*}

   
Example 2: The first problem of using quaternions in rotations must be simple and transparent. Let us consider a rotation about an axis defined by (1, 1, −1) through an angle of π/3. We define a unit vector \[ \mathbf{u} = \frac{1}{\sqrt{3}} \left( 1, 1, -1 \right) . \] Let the rotation angle θ = π/3. The quaternion q defines the rotation is then given as \begin{align*} \mathbf{q} &= \cos\left( \frac{\theta}{2} \right) + \mathbf{u}\,\sin \left( \frac{\theta}{2} \right) = \cos \frac{\pi}{6} + \mathbf{u}\,\sin \frac{\pi}{6} \\ &= \frac{\sqrt{3}}{2} + \frac{1}{\sqrt{3}} \left( 1, 1, -1 \right) \frac{1}{2} \\ &= \frac{\sqrt{3}}{2} + \frac{\sqrt{3}}{6} \,{\bf i} + \frac{\sqrt{3}}{6} \,{\bf j} - \frac{\sqrt{3}}{6} \,{\bf k} . \end{align*} Let us compute the effect of rotation on the basis vector i = (1, 0, 0). \begin{align*} L_q ({\bf i}) &= \mathbf{q} \left( 0; \mathbf{i} \right) \mathbf{q}^{\ast} \\ &= 3 \left( \frac{1}{2} + \frac{1}{6}\,{\bf i} + \frac{1}{6}\,{\bf j} - \frac{1}{6}\,{\bf k} \right) \left( {\bf i} \right) \left( \frac{1}{2} -\frac{1}{6}\,{\bf i} - \frac{1}{6}\,{\bf j} + \frac{1}{6}\,{\bf k} \right) \\ &= 3 \left( \frac{5}{18} \,{\bf i} - \frac{1}{6} \,{\bf k} \right) \\ &= \frac{5}{6} \,{\bf i} - \frac{1}{2} \,{\bf k} . \end{align*}

Using formula (4), we obtain \begin{align*} L_q ({\bf j}) &= \mathbf{q} \left( 0; \mathbf{j} \right) \mathbf{q}^{\ast} \\ &= \end{align*} and \begin{align*} L_q ({\bf k}) &= \mathbf{q} \left( 0; \mathbf{j} \right) \mathbf{q}^{\ast} \\ &= \end{align*}

Roger
   ■
End of Example 2
   
Example 3: We are going to determine the image of an arbitrary point P(x, y, z) under the rotation by an angle of 2θ = 60° (= π/3 in radians) about an axis in the xy-plane that is inclined at an angle of 45° (= π/4) to the positive x-axis.

The unit vector u in the direction of the axis of rotation is \[ \mathbf{u} = \cos \left( \frac{\pi}{4} \right) {\bf i} + \sin \left( \frac{\pi}{4} \right) {\bf j} = \frac{1}{\sqrt{2}}\, {\bf i} + \frac{1}{\sqrt{2}}\, {\bf j} . \] To find the image of p = xi + yj + zk under the rotation, we calculate q p q*, where q is the quaternion \[ \mathbf{q} = \cos\theta + \sin\theta\,\mathbf{u} \] and θ is the half angle of rotation (30° in this case). The resulting quaternion—if we did the calculation right—would have no constant term and therefore we can interpret it as a vector. That vector gives us the answer.

We have \[ \mathbf{q} = \cos\left( \frac{\pi}{6} \right) + \sin \left( \frac{\pi}{6} \right) \mathbf{u} = \frac{\sqrt{3}}{2} + \frac{1}{2} \left( \frac{\sqrt{2}}{2}\,{\bf i} + \frac{\sqrt{2}}{2}\,{\bf j} \right) \] Since q is by construction a unit quaternion, its inverse is its conjugate: \( \displaystyle \quad \mathbf{q}^{-1} = {\bf q}^{\ast} = \frac{\sqrt{3}}{2} - \frac{\sqrt{2}}{4}\,{\bf i} - \frac{\sqrt{2}}{4}\,{\bf j} . \quad \) Now, computing qp in the routine way, we obtain \begin{align*} {\bf q}\,{\bf p} &= \left( \frac{\sqrt{3}}{2} + \frac{\sqrt{2}}{4}\,{\bf i} + \frac{\sqrt{2}}{4}\,{\bf j} \right) \left( x{\bf i} + y{\bf j} + z{\bf k} \right) \\ &= x \left( \frac{\sqrt{3}}{2}\,{\bf i} - \frac{\sqrt{2}}{4} - \frac{\sqrt{2}}{4}\,{\bf k} \right) + y \left( - \frac{\sqrt{2}}{4} + \frac{\sqrt{3}}{2}\,{\bf j} + \frac{\sqrt{2}}{4}\,{\bf k} \right) \\ & \quad + z \left( \frac{\sqrt{3}}{2}\,{\bf k} - \frac{\sqrt{2}}{4}\,{\bf j} + \frac{\sqrt{2}}{4}\,{\bf i} \right) . \end{align*} Then another long but routine computation give \begin{align*} {\bf q}\,{\bf p}\,{\bf q}^{\ast} &= x \left( - \frac{\sqrt{2}}{4} + \frac{\sqrt{3}}{2}\,{\bf i} - \frac{\sqrt{2}}{4}\,{\bf k} \right) \left( \frac{\sqrt{3}}{2} - \frac{\sqrt{2}}{4}\,{\bf i} - \frac{\sqrt{2}}{4}\,{\bf j} \right) \\ & \quad + y \left( - \frac{\sqrt{2}}{4} + \frac{\sqrt{3}}{2}\,{\bf j} + \frac{\sqrt{2}}{4}\,{\bf k} \right) \left( \frac{\sqrt{3}}{2} - \frac{\sqrt{2}}{4}\,{\bf i} - \frac{\sqrt{2}}{4}\,{\bf j} \right) \\ & \quad + z \left( \frac{\sqrt{3}}{2}\,{\bf k} - \frac{\sqrt{2}}{4}\,{\bf j} + \frac{\sqrt{2}}{4}\,{\bf i} \right) \left( \frac{\sqrt{3}}{2} - \frac{\sqrt{2}}{4}\,{\bf i} - \frac{\sqrt{2}}{4}\,{\bf j} \right) \\ &= x \left( \frac{\sqrt{6}}{8} - \frac{\sqrt{6}}{8} + \frac{1}{8}\,{\bf i} + \frac{1}{8}\,{\bf j} + \frac{3}{4}\,{\bf i} - \frac{\sqrt{6}}{8}\,{\bf k} + \frac{1}{8}\,{\bf j} \right) \end{align*}

rot = 4*RotationMatrix[Pi/3, {1/Sqrt[2], 1/Sqrt[2], 0}]
\( \displaystyle \quad \begin{pmatrix} 3& 1& \sqrt{6} \\ 1& 3& -\sqrt{6} \\ -\sqrt{6}& \sqrt{6}& 2 \end{pmatrix} \)
but
rot = 4*RotationMatrix[Pi/3, {1/Sqrt[2], 1/Sqrt[2], 0}]
\( \displaystyle \quad \begin{pmatrix} 2 + \sqrt{3} & 2 - \sqrt{3}& \sqrt{2} \\ 2 - \sqrt{3}& 2 + \sqrt{3}& -\sqrt{2} \\ -\sqrt{2}& \sqrt{2}& 2 \sqrt{3} \end{pmatrix} \)
   ■
End of Example 3

Given a unit quaternion q = [q₀, q₁, q₂, q₃] = q₀ + qi + qj + qk with ∥q∥ = 1, the corresponding passive rotation matrix is

\begin{equation} \label{EqQuat.6} \mathbf{R}_q = \begin{bmatrix} 1 - 2 q_2^2 - 2 q_3^2 & 2 q_1 q_2 - 2 q_0 q_3 & 2q_1 q_3 + 2 q_0 q_2 \\ 2 q_1 q_2 + 2 q_0 q_3 & 1 - 2q_1^2 - 2q_3^2 & 2 q_2 q_3 - 2 q_0 q_1 \\ 2 q_1 q_3 - 2 q_0 q_2 & 1 q_2 q_3 + 2 q_0 q_1 & 1 - 2 q_1^2 - 2 q_2^2 \end{bmatrix} \end{equation}
So Lq(v) = q(0; v)q* = Rqv defines a passive rotation. Similarly, we get q*(0; v)q = Rq*v defines an active rotation, where ????
\begin{equation} \label{EqQuat.7} \mathbf{R}_{q^{\ast}} = \begin{bmatrix} 1 - 2 q_2^2 - 2 q_3^2 & -2 q_1 q_2 - 2 q_0 q_3 & -2q_1 q_3 + 2 q_0 q_2 \\ 2 q_1 q_2 + 2 q_0 q_3 & 1 - 2q_1^2 - 2q_3^2 & 2 q_2 q_3 - 2 q_0 q_1 \\ 2 q_1 q_3 - 2 q_0 q_2 & 1 q_2 q_3 + 2 q_0 q_1 & 1 - 2 q_1^2 - 2 q_2^2 \end{bmatrix} \end{equation}
because ????
\begin{align} \label{EqQuart.8} L_{q^{\ast}} \left( {\bf v}\right) &= {\bf q}^{\ast} \left( 0; {\bf v} \right) {\bf q} = {\bf q}^{-1} \left( 0; {\bf v} \right) {\bf q} \\ &= \left( q_0^2 - \| {\bf u} \|^2 \right) \mathbf{v} + 2 \left( \mathbf{u} \bullet \mathbf{v} \right) \mathbf{u} + 2 q_0 \left( \mathbf{u} \times \mathbf{v} \right) . \notag \end{align}
Roger
We compute the multiplication of quarternions to change a coordinate frame: \[ \mathbf{q}\,{\bf v}\,{\bf q}^{\ast} = \left( q_0 + q_1 {\bf i} + q_2 {\bf j} + q_3 {\bf k} \right) \left( x{\bf i} + y{\bf j} + z{\bf k} \right) \left( q_0 - q_1 {\bf i} - q_2 {\bf j} - q_3 {\bf k} \right) \] Multiply the first two factors, we get \begin{align*} {\bf v}\,{\bf q}^{\ast} &= \left( x{\bf i} + y{\bf j} + z{\bf k} \right) \left( q_0 - q_1 {\bf i} - q_2 {\bf j} - q_3 {\bf k} \right) \\ &= q_0 x{\bf i} - x{\bf i} q_1 {\bf i} - x{\bf i} q_2 {\bf j} - x{\bf i} q_3 {\bf k} \\ & \quad + y{\bf j} q_0 - y{\bf j} q_1 {\bf i} - y{\bf j} q_2 {\bf j} - y{\bf j}q_3 {\bf k} \\ & \quad + z{\bf k}q_0 - z{\bf k}q_1 {\bf i} - z{\bf k}q_2 {\bf j} - z{\bf k}q_3 {\bf k} \\ &= q_0 x{\bf i} + x\, q_1 - x\, q_2 {\bf k} + x q_3 {\bf j} \\ & \quad + y{\bf j} q_0 + y{\bf k} q_1 + y\, q_2 - y\,q_3 {\bf i} \\ & \quad + z{\bf k}q_0 - z{\bf j}q_1 + z{\bf i}q_2 + z q_3 \\ &= x\, q_1 + y\, q_2 + z q_3 + {\bf i} \left( q_0 x - q_3 y + q_2 z \right) \\ & \quad + {\bf j} \left( q_3 x + q_0 y - q_1 z \right) + {\bf k} \left( - q_2 x + q_1 y + q_0 z \right) . \end{align*} Now multiply this expression on the right by q, which leads to \begin{align*} {\bf q}\,{\bf v}\,{\bf q}^{\ast} &= \left( q_0 + q_1 {\bf i} + q_2 {\bf j} + q_3 {\bf k} \right) \left( x\, q_1 + y\, q_2 + z q_3 \right) \\ & \quad + \left( q_0 + q_1 {\bf i} + q_2 {\bf j} + q_3 {\bf k} \right) {\bf i} \left( q_0 x - q_3 y + q_2 z \right) \\ & \quad + \left( q_0 + q_1 {\bf i} + q_2 {\bf j} + q_3 {\bf k} \right) {\bf j} \left( q_3 x + q_0 y - q_1 z \right) \\ & \quad + \left( q_0 + q_1 {\bf i} + q_2 {\bf j} + q_3 {\bf k} \right) {\bf k} \left( - q_2 x + q_1 y + q_0 z \right) \\ &= q_0 \left( x\, q_1 + y\, q_2 + z q_3 \right) + \left( q_1 {\bf i} + q_2 {\bf j} + q_3 {\bf k} \right) \left( x\, q_1 + y\, q_2 + z q_3 \right) \\ & \quad + \left( q_0 {\bf i} - q_1 - q_2 {\bf k} + q_3 {\bf j} \right) \left( q_0 x - q_3 y + q_2 z \right) \\ & \quad + \left( q_0 {\bf j} + q_1 {\bf k} - q_2 - q_3 {\bf i} \right) \left( q_3 x + q_0 y - q_1 z \right) \\ & \quad + \left( q_0 {\bf k} - q_1 {\bf j} + q_2 {\bf i} - q_3 \right) \left( - q_2 x + q_1 y + q_0 z \right) . \end{align*} Collecting terms gives the required result.
   
Example 4: Consider a rotation about an axis defined by (1, −1, 3) through an angle of 2π/3. About this axis, the basis vectors i, j, and k generate the same cone when rotated through 2π. We define a unit vector \[ \mathbf{w} = \frac{1}{\sqrt{11}} \left( 1, \ -1, \ 3 \right) . \] Let the rotation angle 2θ = 2π/3. The quaternion q defines the rotation is then given as \begin{align*} \mathbf{q} &= \cos\theta + \mathbf{w}\,\sin\theta \\ &= \frac{\sqrt{3}}{2} + \left( \frac{1}{2}\,\mathbf{i} - \frac{1}{2}\,\mathbf{j} + \frac{3}{2}\,\mathbf{k} \right) \frac{1}{\sqrt{11}} . \end{align*} Let us compute the effect of rotation on the basis vector i = (1, 0, 0). \begin{align*} L_q \left( \mathbf{i} \right) &= \mathbf{v}_1 = \cos (2\theta )\,\mathbf{i} + \left( 1 - \cos 2\theta \right) \left(\mathbf{w} \bullet \mathbf{i} \right) \mathbf{w} + \sin (2\theta ) \left( \mathbf{w} \times \mathbf{i} \right) \\ &= \end{align*}
Cos[2*Pi/3]
   ■
End of Example 4
Corollary 1: For any unit quaternion \[ \mathbf{q} = q_0 + \mathbf{u} = \cos \left( \frac{\theta}{2} \right) + \mathbf{w} \, \sin \left( \frac{\theta}{2} \right) , \qquad \| \mathbf{q} \| = \| \mathbf{w} \| = 1, \] and for any vector v ∈ ℝ³ the action of the operator Lq*(v) = q*(0; v)q on vector v is a rotation of the coordinate frame about the axis u or w through an angle θ while v is not rotated.

The quaternion operator Lq*(v) = q*(0; v)q may be interpreted as a coordinate frame rotation with respect to the (fixed) space of points. Equivalently, the operator Lq* actively rotates the vector v with respect to the coordinate frame through an angle −θ about u. The quaternion operator Lq(v) = q(0; v)q* may be interpreted as a point or vector rotation with respect to the (fixed) coordinate frame.

Corollary 2: Every three-dimensional rotation is the action of some unit quaternion.
Observe that the combination of rotation by unit quaternion q₁ followed by q₂ is given by q = qq₁, since
\[ \mathbf{q}_2 \left( \mathbf{q}_1 \mathbf{v}\,\mathbf{q}_1^{\ast} \right) \mathbf{q}_2^{\ast} = \left( \mathbf{q}_2 \mathbf{q}_1 \right) \mathbf{v} \left( \mathbf{q}_2 \mathbf{q}_1 \right)^{\ast} . \]
   
Example 5:    ■
End of Example 5

Let us consider the rotation around an axis n = (nx, ny, nz) by an angle 2𝜃, represented by a quaternion:

\[ \mathbf{q} = \begin{bmatrix} \cos\theta & n_x \sin\theta & n_y \sin\theta & n_z \sin\theta \end{bmatrix} , \qquad n_x^2 + n_y^2 + n_z^2 = 1. \]
If we rotate around this axis by an additional 360°, we get
\[ \mathbf{p} = \begin{bmatrix} \cos\theta & -n_x \sin\theta & -n_y \sin\theta & -n_z \sin\theta \end{bmatrix} . \]
This is a different quaternion, but it represents the same rotation; we’ve just rotated an extra 360°. In fact, we see the following: Every rotation – an angle around some axis – is represented not by one, but by two quaternions. In this way, we say that the unit quaternions double-cover the space of rotations. But why do quaternions have to double-cover rotations?

Properties of Quaternion

The set of all quaternions is denoted by ℍ and its subset of all unit (rotation) quaternions is denoted by U(1, ℍ). This group is isomorphic to the special unitary group:

\[ U(1, \mathbb{H}) \cong SU(2) = \left\{ {\bf R} \in \mathbb{C}^{2\times 2} \ : \ \mathbf{R}\,\mathbf{R}^{\ast} = \mathbf{I} \right\} . \]
Homomorphism U(1, ℍ) ↦ SO(3) = { B ∈ ℝ3×3 : R R* = I } to the orthogonal group is not an isomorphism because it is double covering.

The following are some useful properties of quaternions. Up until now, we discussed only rotation quaternions (of unit length), denoted by U(1, ℍ). However, rotation quaternions are only a subset of all possible quaternions, just as rotation matrices are a subset of all possible 3x3 matrices. The following properties apply to all quaternions q = (q₀, q₁, q₂, q₃) ∈ ℍ unless otherwise specified.

  1. The length (magnitude) of a quaternion is its norm:    ∥q∥ = \( \displaystyle \sqrt{\mathbf{q}\,\mathbf{q}^{\ast}} = \sqrt{\mathbf{q}^{\ast}\mathbf{q}} = \sqrt{q_0^2 + q_1^2 + q_2^2 + 1_3^2} . \)
  2. A quaternion is called a "unit" quaternion if    ∥q∥ = 1.
  3. All rotation quaternions must be of unit norm.
  4. The quaternion q = (1, 0, 0, 0) is the identity quaternion, which we denote by 1. It represents no rotation. If q is an arbitrary quaternion and 1 is the identity quaternion, then q 1 = 1 q = q.
  5. The conjugate of a quaternion q = (q₀, q₁, q₂, q₃) is    q* = (q₀, −q₁, −q₂, −q₃). Also q*)* = q.
  6. The inverse of a quaternion q = (q₀, q₁, q₂, q₃) is    \( \displaystyle \mathbf{1}^{-1} = \frac{1}{\| {\bf q} \|}\,\mathbf{q}^{\ast} . \)    The product of a quaternion and its inverse is the identity quaternion:    \( \displaystyle \mathbf{q}^{-1} \mathbf{q} = \mathbf{q} \,{\bf q}^{-1} = \mathbf{1} = (1, 0, 0, 0, 0) . \)    Note that for this special case, quaternion multiplication is commutative.
  7. For rotation quaternions q ∈ U(1, ℍ), the inverse equals the conjugate. So for rotation quaternions,    \( \displaystyle \mathbf{q}^{-1} = \mathbf{q}^{\ast} = (1, 0, 0, 0). \)
  8. Inverting or conjugating a rotation quaternion has the effect of reversing the axis of rotation, which modifies it to rotate in the opposite direction from the original. That is, if a point is rotated to a new position using q, then rotating it again using q−1 or q* will return it to its original location.
  9. Any given rotation has two possible quaternion representations. If one is known, the other can be found by taking the negative of all four terms. This has the effect of reversing both the rotation angle and the axis of rotation. So if q is a rotation quaternion, then q and −q will produce the same rotation.
  10. A rotation of qa followed by a rotation of qb can be combined into the single rotation qc = qbqa. This can be extended to an arbitrary number of rotations. Note that the order matters (because quaternion multiplication is not commutative).
  11. Quaternion multiplication is associative: (ab)c = a(bc).
  12. Generally speaking, quaternion multiplication is not commutative: abba.

From a matrix of angles to a quaternion

Given a rotation matrix R, we can compute the quaternion that represents the same rotation. First, we compute the trace, the sum of the elements on the main diagonal of the matrix:
\[ \mbox{Trace} (\mathbf{R} ) = 3 - 4 \left( q_1^2 + q_2^2 + q_3^2 \right) = 4\,q_0^2 -1 \]
because the quaternion q is assumed to have aunit magnitude. We can solve this equation for q₀:
\[ \left\vert q_0 \right\vert = + \sqrt{\frac{r_{1,1} + r_{2,2} + r_{3,3} +1}{4}} . \]
Once we have q₀, we can obtain q₁ from the first entry r1,1 of matrixR:
\[ r_{1,1} = 1 - 2\, q_2^2 - q_3^2 = 2\,q_0^2 + 2\,q_1^2 -1 = 2\,\frac{\mbox{Trace}({\bf R}) +1}{4} + 2\,q_1^2 -1 . \]
So
\[ \left\vert q_1 \right\vert = \sqrt{\frac{r_{1,1}}{2} + \frac{1 - \mbox{Trace}({\bf R})}{4}} = \sqrt{\frac{1 + r_{1,1} - r_{2,2} - r_{3,3}}{4}} . \]
Similarly, q₂ and q₃ can be computed from two other diagonal entries of rotation matrix:
\[ \left\vert q_2 \right\vert = \sqrt{\frac{r_{2,2}}{2} + \frac{1 - \mbox{Trace}({\bf R})}{4}} = \sqrt{\frac{1 - r_{1,1} + r_{2,2} - r_{3,3}}{4}} , \]
\[ \left\vert q_3 \right\vert = \sqrt{\frac{r_{3,3}}{2} + \frac{1 - \mbox{Trace}({\bf R})}{4}} = \sqrt{\frac{1 - r_{1,1} - r_{2,2} + r_{3,3}}{4}} . \]

To resolve the signs, find the largest of q₀, q₁q1, q₂, q₃ and assume its sign is positive. Then compute the remaining components as shown in the table below. Taking the largest magnitude avoids division by small numbers, which would reduce numerical accuracy.

If q₀ is largest: If q₁ is largest: If q₂ is largest: If q₃ is largest:
\( \displaystyle \quad q_1 = \frac{r_{3,2} - r_{2,3}}{4\,q_0} \quad\) \( \displaystyle \quad q_0 = \frac{r_{3,2} - r_{2,3}}{4\,q_1} \quad\) \( \displaystyle \quad q_0 = \frac{r_{1,3} - r_{3,1}}{4\,q_2} \quad\) \( \displaystyle \quad q_1 = \frac{r_{2,1} - r_{1,2}}{4\,q_3} \quad\)
\( \displaystyle \quad q_2 = \frac{r_{1,3} - r_{3,1}}{4\,q_0} \quad\) \( \displaystyle \quad q_2 = \frac{r_{2,1} + r_{1,2}}{4\,q_1} \quad\) \( \displaystyle \quad q_1 = \frac{r_{1,3} + r_{3,1}}{4\,q_2} \quad\) \( \displaystyle \quad q_1 = \frac{r_{1,3} + r_{3,1}}{4\,q_3} \quad\)
\( \displaystyle \quad q_3 = \frac{r_{2,1} - r_{1,2}}{4\,q_0} \quad\) \( \displaystyle \quad q_3 = \frac{r_{1,3} + r_{3,1}}{4\,q_1} \quad\) \( \displaystyle \quad q_3 = \frac{r_{2,3} + r_{3,2}}{4\,q_2} \quad\) \( \displaystyle \quad q_2 = \frac{r_{2,3} + r_{3,2}}{4\,q_3} \quad\)

   
Example 6:    ■
End of Example 6

From Euler angles to a quaternion

Given a rotation by an angle, the corresponding quaternion can be computed. Consider the rotation ψ around the z-axis:
\[ \mathbf{R}_z (\psi ) = \begin{bmatrix} \cos\psi & -\sin\psi & 0 \\ \sin\psi & \cos\psi & 0 \\ 0&0&1 \end{bmatrix} . \]
From the formulas in the previous section and using that Trace(R) = 2 cos ψ + 1:
\[ \left\vert q_0 \right\vert = \sqrt{\frac{2\,\cos\psi + 1+1}{4}} = \sqrt{\frac{1 + \cos\psi}{2}} = \cos \left( \frac{\psi}{2} \right) , \]
and
\[ \left\vert q_1 \right\vert = \left\vert q_2 \right\vert = \sqrt{\frac{\cos\psi}{2} + \frac{1 - (2\,\cos\psi +1)}{4}} = 0, \]
\[ \left\vert q_3 \right\vert = \sqrt{\frac{1}{2} + \frac{}{4}} = \sqrt{\frac{1 - \cos\psi}{2}} = \sin \left( \frac{\psi}{2} \right) . \]
Therefore, the corresponding quaternion becomes
\[ \mathbf{q}_{\psi} = \cos \left( \frac{\psi}{2} \right) + {\bf k}\,\sin \left( \frac{\psi}{2} \right) . \]
Similarly, we can compute the quaternions corresponding to rotations around the y- and x-axes:
\[ \mathbf{q}_{\theta} = \cos \left( \frac{\theta}{2} \right) + {\bf j}\,\sin \left( \frac{\theta}{2} \right) , \]
\[ \mathbf{q}_{\phi} = \cos \left( \frac{\phi}{2} \right) + {\bf i}\,\sin \left( \frac{\phi}{2} \right) . \]

Now suppose that we know three Euler angles, ϕ, θ, and ψ. We can convert from Euler's angles to quaternion q = (q₀, q₁, q₂, q₃) as follows:

\begin{align*} q_0 &= c \left( \frac{\phi}{2} \right) c \left( \frac{\theta}{2} \right) c\left( \frac{\psi}{2} \right) + s\left( \frac{\phi}{2} \right) s \left( \frac{\theta}{2} \right) s\left( \frac{\psi}{2} \right) , \\ q_1 &= s \left( \frac{\phi}{2} \right) c \left( \frac{\theta}{2} \right) c\left( \frac{\psi}{2} \right) - c\left( \frac{\phi}{2} \right) s \left( \frac{\theta}{2} \right) s\left( \frac{\psi}{2} \right) , \\ q_2 &= c \left( \frac{\phi}{2} \right) s \left( \frac{\theta}{2} \right) c\left( \frac{\psi}{2} \right) + s\left( \frac{\phi}{2} \right) c \left( \frac{\theta}{2} \right) s\left( \frac{\psi}{2} \right) , \\ q_3 &= c \left( \frac{\phi}{2} \right) c \left( \frac{\theta}{2} \right) s\left( \frac{\psi}{2} \right) - s\left( \frac{\phi}{2} \right) s \left( \frac{\theta}{2} \right) c \left( \frac{\psi}{2} \right) , \end{align*}
where

ϕ = roll angle,
θ = pitch angle,
ψ = yaw angle,
c() = cosine function,
s() = sine function.

These equations work for all values of Euler angle, including the condition of gimbal lock, where the pitch angle equals +90° or −90°.    

Example 7: Consider a rotation of +90° around the z-axis. It takes the point (1, 0, 0) to (0, 1, 0). The rotation matrix is: \[ \mathbf{R}(\psi = 90^{\circ}) = \begin{bmatrix} \cos 90^{\circ} & - \sin 90^{\circ} &0 \\ \sin 90^{\circ} & \cos 90^{\circ} & 0 \\ 0&0&1 \end{bmatrix} = \begin{bmatrix} 0&-1&0 \\ 1&0&0 \\ 0&0&1 \end{bmatrix} . \] Using formulas from the previous subsection together with trace(R) = 1, we obtain \begin{align*} \left\vert q_0 \right\vert &= \sqrt{\frac{\mbox{Trace}(\mathbf{R}) + 1}{4}} = \sqrt{\frac{1}{2}} = \frac{\sqrt{2}}{2} , \\ \left\vert q_1 \right\vert &= \sqrt{\frac{r_{1,1}}{2} + \frac{1 - \mbox{Trace}({\bf R})}{4}} = 0, \\ \left\vert q_2 \right\vert &= \sqrt{\frac{r_{2,2}}{2} + \frac{1 - \mbox{Trace}({\bf R})}{4}} = 0, \\ \left\vert q_3 \right\vert &= \sqrt{\frac{r_{3,3}}{2} + \frac{1 - \mbox{Trace}({\bf R})}{4}} = \sqrt{\frac{1}{2}} = \frac{\sqrt{2}}{2} . \end{align*}    ■
End of Example 7

Convert Quaternion to Euler Angles

The previous subsection allows us to determine Euler's angles from quaternions:
\begin{align*} \mbox{roll} = \phi &= \arctan \left( \frac{2 \left( q_0 q_1 + q_2 q_3 \right)}{q_0^2 - q_1^2 - q_2^2 + q_3^2} \right) , \\ \mbox{pitch} = \theta &= \arcsin \left( 2 \left( q_0 q_2 - q_1 q_3 \right) \right) , \\ \mbox{yaw} = \psi &= \arctan \left( \frac{2 \left( q_0 q_3 + q_1 q_2 \right)}{q_0^2 + q_1^2 -q_2^2 -q_3^2} \right) . \end{align*}
   
Example 8:    ■
End of Example 8
   
Example 9: This example follows the article
Quaternions and Rotations by Yan-Bin Jia.

An important problem in model-based recognition is to find the transformation of a set of data points that yields the best match of these points against a shape model. The process is often referred to as data registration. The data points are typically measured on a real object by range sensors, touch sensors, etc., and given in Cartesian coordinates. The quality of a match is often described as the total squared distance from the data points to the model. When multiple shape models are possible, the one that results in the least total distance is then recognized as the shape of the object.

Quaternions are very effective in solving the above least-squares-based registration problem. Let us begin with a formulation of the problem in 3D. Let {p₁, p₂, … , pn} be a set of data points. We assume that p₁, p₂, … , pn are to be matched against the points q₁, q₂, … , qn on a shape model. Namely, the correspondences between the data points and those on the model have been predetermined. Then the problem is to find a rotation, represented by an orthogonal matrix R with det(R) = 1, and a translation b as the solution to the following minimization: \[ \min_{{\bf R}, {\bf b}} \sum_{i=1}^n \| {\bf R}\,{\bf p}_i +{\bf b} - {\bf q}_i \|^2 . \tag{9.1} \] We begin by computing the centroids of the two sets of points: \begin{align*} \overline{\bf p} &= \frac{1}{n} \sum_{i=1}^n {\bf p}_i , \\ \overline{\bf q} &= \frac{1}{n} \sum_{i=1}^n {\bf q}_i . \end{align*} The relative coordinates of all the points to their centroids are obtained as, for 1 ≤ in, \begin{align*} {\bf p}'_i &= {\bf p}_i - \overline{\bf p} , \\ {\bf q}'_i &= {\bf q}_i - \overline{\bf q} . \end{align*} Clearly, we have \begin{align*} \sum_{i=1}^n {\bf p}'_i &= \sum_{i=1}^n {\bf p}_i - n\,\overline{\bf p} = \sum_{i=1}^n {\bf p}_i - n\cdot \frac{1}{n} \sum_{i=1}^n {\bf p}_i = {\bf 0}, \\ \sum_{i=1}^n {\bf q}'_i &= \sum_{i=1}^n {\bf q}_i - n\,\overline{\bf q} = \sum_{i=1}^n {\bf q}_i - n\cdot \frac{1}{n} \sum_{i=1}^n {\bf q}_i = {\bf 0} . \end{align*} Let us rewrite the objective function in (9.1) in terms of data \begin{align*} I &= \sum_{i=1}^n \| {\bf R}\,{\bf p}_i +{\bf b} - {\bf q}_i \|^2 \\ &= \sum_{i=1}^n \| {\bf R}\,{\bf p}'_i - {\bf q}'_i + {\bf R}\,\overline{\bf p} + {\bf b} - \overline{\bf q} \|^2 \\ &= \sum_{i=1}^n \left( {\bf R}\,{\bf p}'_i - {\bf q}'_i + {\bf R}\,\overline{\bf p} - \overline{\bf q} + {\bf b} \right) \bullet \left( {\bf R}\,{\bf p}'_i - {\bf q}'_i + {\bf R}\,\overline{\bf p} - \overline{\bf q} + {\bf b} \right) \\ &= \sum_{i=1}^n \| {\bf R}\,{\bf p}'_i - {\bf q}'_i \|^2 + \left( 2 \sum_{i=1}^n \left( {\bf R}\,{\bf p}'_i - {\bf q}'_i \right) \right) \bullet \left( {\bf R}\,\overline{\bf p} - \overline{\bf q} + {\bf b} \right) + n \| {\bf R}\,\overline{\bf p} - \overline{\bf q} + {\bf b} \|^2 \\ &= \sum_{i=1}^n \| {\bf R}\,{\bf p}'_i - {\bf q}'_i \|^2 + 2 \left( {\bf R} \sum_{i=1}^n {\bf p}'_i - \sum_{i=1}^n {\bf q}'_i \right) \bullet \left( {\bf R}\,\overline{\bf p} - \overline{\bf q} + {\bf b} \right) + n \| {\bf R}\,\overline{\bf p} - \overline{\bf q} + {\bf b} \|^2 \\ &= \sum_{i=1}^n \| {\bf R}\,{\bf p}'_i - {\bf q}'_i \|^2 + n \| {\bf R}\,\overline{\bf p} - \overline{\bf q} + {\bf b} \|^2 \end{align*} The minimizing translation b should make the second term in the last equation above zero, yielding: \[ \mathbf{b} = \overline{\bf q} - {\bf R}\,\overline{\bg p} . \tag{9.2} \] Thus, we have decomposed the problem of data registration into two phases: the first of which determines its optimal translation, as given by equation (9.2), and the second of which determines the optimal rotation of the set {pi}. Note that every point pi is transformed into R(pi \( \displaystyle - \overline{\bf p}) + \overline{\bf q} \quad \) before matching against qi. Equivalently, to find the best match of the two point sets {pi} and {qi}, we first translate {pi} to let their centroid coincide with that of {qi}, and then rotate about the common centroid.

By the reasoning so far, the optimal rotation can be solved from the formulation below: \[ \min_{R} \sum_{i=1}^n \| {\bf R}\,{\bf p}'_i - {\bf q}'_i \|^2 . \tag{9.3} \] Here we present an exact solution to (9.3) as described in [B. K. P. Horn. Closed-form solution of absolute orientation using unit quaternions. Journal of Optical Society of America A, 4(4):629–642, 1987.] using quaternions. An equivalent quaternion-based solution is given in [O. D. Faugeras and M. Hebert. The representation, recognition, and locating of 3-D objects. International Journal of Robotics Research, 5(3):27–52, 1986.]. The version of matching two curves (or surfaces), also assuming pointwise correspondences, is solved exactly in [J. T. Schwartz and M. Sharir. Identification of partially obscured objects in two and three dimensions by matching noisy characteristic curves. International Journal of Robotics Research, 6(2):29–44, 1987.] in a somewhat similar manner without the use of quaternions.

First, we rewrite the summation in (9.3) as follows \begin{align*} \sum_{i=1}^n \| {\bf R}\,{\bf p}'_i - {\bf q}'_i \|^2 &= \sum_{i=1}^n \left( {\bf R}\,{\bf p}'_i \bullet {\bf R}\,{\bf p}'_i \right) -2 \sum_{i=1}^n \left( {\bf R}\,{\bf p}'_i \bullet {\bf q}'_i \right) + \sum_{i=1}^n {\bf q}'_i \bullet {\bf q}'_i \\ &= \sum_{i=1}^n \left( \| {\bf p}'_i \|^2 + \| {\bf q}'_i \|^2 \right) -2 \sum_{i=1}^n {\bf R}\,{\bf p}'_i \bullet {\bf q}'_i . \end{align*} The first summand in the last equation above does not depend on the rotation, so we need only minimize the second summand. Equivalently, this can be done through a maximization: \[ \max_{R} \sum_{i=1}^n {\bf R}\,{\bf p}'_i \bullet {\bf q}'_i . \] The rotation matrix R has nine entries, only four of which are independent due to the orthogonality and unit determinant of R. Instead, we represent rotations using unit quaternions. Essentially, we find the unit quaternion q that maximizes \[ \sum_{i=1}^n \left( {\bf q}\,{\bf p}'_i {\bf q}^{\ast} \right)\bullet {\bf q}'_i . \tag{9.4} \] Here we view quaternions as vectors in ℝ4. Let q = (q₀, q₁, q₂, q₃)T and q* = (q₀, −q₁, −q₂, −q₃)T. Also, the points p′₁, … , p'n and q′₁, … , q'n are viewed as 4-tuples with pi = (0; pi1, pi2, pi3)T and qi = (0; qi1, qi2, qi3)T by a slight abuse of notation.

Applying the definition of quaternion product, it is not difficult to show that \[ \left( {\bf q}\,{\bf p}'_i {\bf q}^{\ast} \right)\bullet {\bf q}'_i = \left( {\bf q}\,{\bf p}'_i \right) \bullet \left( {\bf q}'_i {\bf q} \right) . \tag{9.5} \] Next, we intend to rewrite the summands in (9.4) as matrix products. For this purpose, we define matrices \[ {\bf P}_i = \begin{bmatrix} 0& -p'_{i1} & -p'_{i2} & -p'_{i3} \\ p'_{i1} & 0 & p'_{i3} & -p'_{i2} \\ p'_{i2} & -p'_{i3} & 0 & p'_{i1} \\ p'_{i3} & p'_{i2} & -p'_{i1} & 0 \end{bmatrix} \quad\mbox{and}\quad {\bf Q}_i = \begin{bmatrix} 0 & -1'_{i1} & -q'_{i2} & -q'_{i3} \\ q'_{i1} & 0 & -q'_{i3} & q'_{i2} \\ q'_{i2} & q'_{i3} & 0 & -q'_{i1} \\ q'_{i3} & -q'_{i2} & q'_{i1} & 0 \end{bmatrix} \] For 1 ≤ in. Then the quaternion products qpi and qiq are equivalent to the matrix products Piq and Qiq. We thus have \begin{align*} \sum_{i=1}^n \left( {\bf q}\,{\bf p}'_i {\bf q}^{\ast} \right)\bullet {\bf q}'_i &= \sum_{i=1}^n \left( {\bf q}\,{\bf p}'_i \right) \bullet \left( {\bf q}'_{i} {\bf q} \right) \\ &= \sum_{i=1}^n \left( {\bf P}_i {\bf q} \right) \bullet \left( {\bf Q}_i {\bf q} \right) \\ &= \sum_{i=1}^n {\bf q}^{\mathrm T} {\bf P}^{\mathrm T}_i {\bf Q}_i {\bf q} \\ &= {\bf q}^{\mathrm T} \left( {\bf P}^{\mathrm T}_i {\bf Q}_i \right) {\bf q} . \end{align*} It is easy to verify that each matrix PTiQi is symmetric, so is the 4 × 4 matrix \[ \mathbf{M} = \sum_{i=1}^n {\bf P}^{\mathrm T}_i {\bf Q}_i . \] Thus, M has real eigenvalues only, say, λ₁, λ₂, λ₃, λ₄ with λ₁ ≥ λ₂ ≥ λ₃ ≥ λ₄. Let v₁, v₂, v₃, v₄ be the corresponding orthogonal unit eigenvectors. Eigenvectors corresponding to different eigenval- ues must be orthogonal to each other. Multiple eigenvectors corresponding to the same eigenvalue are chosen to be orthogonal to each other. The quaternion q is a linear combination of these eigenvectors: \[ \mathbf{q} = \alpha_1 {\bf v}_1 + \alpha_2 {\bf v}_2 + \alpha_3 {\bf v}_3 + \alpha_4 {\bf v}_4 . \] Therefore, we have \begin{align*} \mathbf{q}^{\mathrm T} \mathbf{M}\,{\bf q} &= \left( \alpha_1 {\bf v}_1 + \alpha_2 {\bf v}_2 + \alpha_3 {\bf v}_3 + \alpha_4 {\bf v}_4 \right)^{\mathrm T} \left( \alpha_1 {\bf v}_1 + \alpha_2 {\bf v}_2 + \alpha_3 {\bf v}_3 + \alpha_4 {\bf v}_4 \right) \\ &= \left( \alpha_1 {\bf v}_1 + \alpha_2 {\bf v}_2 + \alpha_3 {\bf v}_3 + \alpha_4 {\bf v}_4 \right) \bullet \left( \lambda_1\alpha_1 {\bf v}_1 + \lambda_1 \alpha_2 {\bf v}_2 + \lambda_3 \alpha_3 {\bf v}_3 + \lambda_4 \alpha_4 {\bf v}_4 \right) \\ &= \lambda_1 \alpha_1^2 + \lambda_2 \alpha_2^2 + \lambda_3 \alpha_3^2 + \lambda_4 \alpha_4^2 . \end{align*} The product qTMq achieves its maximum when α₁ = 1 and α₂ = α₃ = α₄ = 0. Therefore, the unit quaternion q that maximizes (14) is the eigenvector that corresponds to the largest eigenvalue of the matrix M. It describes the optimal rotation for (12), i.e, for data registration.

When the corresponding points q1, … , qn are unknown, a well-known method called the Iterative Closest Point (ICP) [P. J. Besl and N. D. McKay. A method for registration of 3-D shapes. IEEE Transactions on pattern analysis and machine intelligence, 14(2):239–256, 1992.] solves the registration problem. Given a set of data points {p₁, … , pn}, the ICP algorithm finds the initial corresponding points q(0)1, … , q(0)n as the closest points on the surface model to p(0)1 = p₁, … , p(0)n = pn, respectively. Then it applies the introduced quaternion- based method to determine the rotation and translation that best match {p(0)n} with {q(0)i}. The second iteration applies the just found transformation to every p(0)i, obtaining p(1)i p(1) i , and then determines its new corresponding point q(1)i on the model as the closest point to p(1)i. Recompute the best rotation and translation using quaternions, and so on. The algorithm stops when the change in the new transformation becomes small enough.    ■

End of Example 9


  1. Altmann, S.L., Rotations, Quaternions, and Double Groups, Dover Publications; Illustrated edition (November 3, 2005)
  2. Altmann, S.L., Hamilton, Rodrigues, and the quaternion scandal, Mathematics Magazine 62(5) 291–308 (1989).
  3. Bickford, N., Why Do the Unit Quaternions Double-Cover the Space of Rotations?
  4. ten Bosch, M., Let's remove Quaternions from every 3D Engine,
  5. Conway, J.H., On Quaternions and Octonions: Their Geometry, Arithmetic, and Symmetry, A K Peters/CRC Press; 1st edition (January 23, 2003)
  6. Dai, J.S., Euler–Rodrigues formula variations, quaternion conjugation and intrinsic connections, Mechanism and Machine Theory, 2015, pp. 144--152.
  7. Dunn, F. and Parberry, I. (2002). 3D math primer for graphics and game development. Plano, Tex.: Wordware Pub.
  8. Foley, James D.; van Dam, Andries; Feiner, Steven K.; Hughes, John F. (1991), Computer Graphics: Principles and Practice (2nd ed.), Reading: Addison-Wesley, ISBN 0-201-12110-7
  9. Hanson, A.J., Visualizing Quaternions: Belt-Trick Documentation, Indiana University.
  10. Hamilton, W. R., On a new species of imaginary quantities connected with a theory of quaternions, Proceedings of the Royal Irish Academy 2 424-434 (1844).
  11. Hanson, A.J., Visualizing Quaternions (The Morgan Kaufmann Series in Interactive 3D Technology) 1st Edition, 2006.
  12. Kuipers, J.B., Quaternions and Rotation Sequences. Princeton University Press, 1999.
  13. Matrices and Linear Transformations
  14. Pujol, J., Hamilton, Rodrigues, Gauss, quaternions, and rotations: A historical reassessment, Communications in Mathematical Analysis 13(2) 1-14 (2012).
  15. Rogers, D.F., Adams, J. A., Mathematical Elements for Computer Graphics, McGraw-Hill Science/Engineering/Math, 1989.
  16. Sanderson, G., Visualizing quaternionsquaternions, Technology by Ben Eater.
  17. Shoemake, K., Animating Rotation with Quaternion Curves, San Francisco. Ju;y 22--26, ACM, vol. 19, N.3, 1985.
  18. Watt, A., 3D Computer Graphics, Addison-Wesley; 3rd edition, 1999.