# Quaternions and spatial rotation

Template:Cleanup-reorganize Unit quaternions, also known as versors, provide a convenient mathematical notation for representing orientations and rotations of objects in three dimensions. Compared to Euler angles they are simpler to compose and avoid the problem of gimbal lock. Compared to rotation matrices they are more numerically stable and may be more efficient. Quaternions have found their way into applications in computer graphics, computer vision, robotics, navigation, molecular dynamics, flight dynamics, and orbital mechanics of satellites.

When used to represent rotation, unit quaternions are also called rotation quaternions. When used to represent an orientation (rotation relative to a reference coordinate system), they are called orientation quaternions or attitude quaternions.

## Using quaternion rotations

In 3-dimensional space, according to Euler's rotation theorem, any rotation or sequence of rotations of a rigid body or coordinate system about a fixed point is equivalent to a single rotation by a given angle Template:Mvar about a fixed axis (called Euler axis) that runs through the fixed point. The Euler axis is typically represented by a unit vector Template:Mvec. Therefore, any rotation in three dimensions can be represented as a combination of a vector Template:Mvec and a scalar Template:Mvar. Quaternions give a simple way to encode this axis–angle representation in four numbers, and to apply the corresponding rotation to a position vector representing a point relative to the origin in R3.

An Euclidean vector such as (2, 3, 4) or (ax, ay, az) can be rewritten as 2 i + 3 j + 4 k or axi + ayj + azk, where i, j, k are unit vectors representing the three Cartesian axes. A rotation through an angle of Template:Mvar around the axis defined by a unit vector

${\vec {u}}=(u_{x},u_{y},u_{z})=u_{x}{\mathbf {i} }+u_{y}{\mathbf {j} }+u_{z}{\mathbf {k} }$ can be represented by a quaternion. This can be done using an extension of Euler's formula:

$\mathbf {q} =e^{{\frac {\theta }{2}}{(u_{x}\mathbf {i} +u_{y}\mathbf {j} +u_{z}\mathbf {k} )}}=\cos {\frac {\theta }{2}}+(u_{x}\mathbf {i} +u_{y}\mathbf {j} +u_{z}\mathbf {k} )\sin {\frac {\theta }{2}}$ It can be shown that the desired rotation can be applied to an ordinary vector ${\mathbf {p} }=(p_{x},p_{y},p_{z})=p_{x}{\mathbf {i} }+p_{y}{\mathbf {j} }+p_{z}{\mathbf {k} }$ in 3-dimensional space, considered as a quaternion with a real coordinate equal to zero, by evaluating the conjugation of p by q:

${\mathbf {p'} }={\mathbf {q} }{\mathbf {p} }{\mathbf {q} }^{-1}$ using the Hamilton product, where p′ = (px′, py′, pz′) is the new position vector of the point after the rotation.

This operation carries the set of all "pure" quaternions p (those with real part equal to zero) — which constitute a 3-dimensional space among the quaternions — into itself, by the desired rotation about the axis u, by the angle θ. (Each real quaternion is carried into itself by this operation. But for the purpose of rotations in 3-dimensional space, we ignore the real quaternions.)

The rotation is clockwise if our line of sight points in the same direction as Template:Mvec.

In this instance, q is a unit quaternion and

$\mathbf {q} ^{-1}=e^{-{\frac {\theta }{2}}{(u_{x}\mathbf {i} +u_{y}\mathbf {j} +u_{z}\mathbf {k} )}}=\cos {\frac {\theta }{2}}-(u_{x}\mathbf {i} +u_{y}\mathbf {j} +u_{z}\mathbf {k} )\sin {\frac {\theta }{2}}.$ It follows that conjugation by the product of two quaternions is the composition of conjugations by these quaternions: If p and q are unit quaternions, then rotation (conjugation) by pq is

${\mathbf {pq} }{\vec {v}}({\mathbf {pq} })^{-1}={\mathbf {pq} }{\vec {v}}{\mathbf {q} }^{-1}{\mathbf {p} }^{-1}={\mathbf {p} }({\mathbf {q} }{\vec {v}}{\mathbf {q} }^{-1}){\mathbf {p} }^{-1}$ ,

which is the same as rotating (conjugating) by q and then by p. The scalar component of the result is necessarily zero.

The quaternion inverse of a rotation is the opposite rotation, since ${\mathbf {q} }^{-1}({\mathbf {q} }{\vec {v}}{\mathbf {q} }^{-1}){\mathbf {q} }={\vec {v}}$ . The square of a quaternion rotation is a rotation by twice the angle around the same axis. More generally qn is a rotation by Template:Mvar times the angle around the same axis as q. This can be extended to arbitrary real Template:Mvar, allowing for smooth interpolation between spatial orientations; see Slerp.

Two rotation quaternions can be combined into one equivalent quaternion by the relation:

${\mathbf {q} }'={\mathbf {q} }_{2}{\mathbf {q} }_{1}$ in which q corresponds to the rotation q1 followed by the rotation q2. (Note that quaternion multiplication is not commutative.) Thus, an arbitrary number of rotations can be composed together and then applied as a single rotation.

### Example

#### The conjugation operation

Conjugating p by q refers to the operation pq p q−1.

Consider the rotation Template:Mvar around the axis ${\vec {v}}={\mathbf {i} }+{\mathbf {j} }+{\mathbf {k} }$ , with a rotation angle of 120°, or {{ safesubst:#invoke:Unsubst||$B=/3}} radians. $\alpha ={\frac {2\pi }{3}}$ The length of Template:Mvec is Template:Sqrt, the half angle is {{ safesubst:#invoke:Unsubst||$B=π/3}} (60°) with cosine {{ safesubst:#invoke:Unsubst||$B=1/2}}, (cos 60° = 0.5) and sine {{ safesubst:#invoke:Unsubst||$B=}}, (sin 60° ≈ 0.866). We are therefore dealing with a conjugation by the unit quaternion

${\begin{array}{lll}u&=&\cos {\frac {\alpha }{2}}+\sin {\frac {\alpha }{2}}\cdot {\frac {1}{\|{\vec {v}}\|}}{\vec {v}}\\&=&\cos {\frac {\pi }{3}}+\sin {\frac {\pi }{3}}\cdot {\frac {1}{\sqrt {3}}}{\vec {v}}\\&=&{\frac {1}{2}}+{\frac {\sqrt {3}}{2}}\cdot {\frac {1}{\sqrt {3}}}{\vec {v}}\\&=&{\frac {1}{2}}+{\frac {\sqrt {3}}{2}}\cdot {\frac {{\mathbf {i} }+{\mathbf {j} }+{\mathbf {k} }}{\sqrt {3}}}\\&=&{\frac {1+{\mathbf {i} }+{\mathbf {j} }+{\mathbf {k} }}{2}}\end{array}}$ If Template:Mvar is the rotation function,

$f(a{\mathbf {i} }+b{\mathbf {j} }+c{\mathbf {k} })=u(a{\mathbf {i} }+b{\mathbf {j} }+c{\mathbf {k} })u^{-1}$ It can be proved that the inverse of a unit quaternion is obtained simply by changing the sign of its imaginary components. As a consequence,

$u^{-1}={\frac {1-{\mathbf {i} }-{\mathbf {j} }-{\mathbf {k} }}{2}}$ and

$f(a{\mathbf {i} }+b{\mathbf {j} }+c{\mathbf {k} })={\frac {1+{\mathbf {i} }+{\mathbf {j} }+{\mathbf {k} }}{2}}(a{\mathbf {i} }+b{\mathbf {j} }+c{\mathbf {k} }){\frac {1-{\mathbf {i} }-{\mathbf {j} }-{\mathbf {k} }}{2}}$ This can be simplified, using the ordinary rules for quaternion arithmetic, to

$f(a{\mathbf {i} }+b{\mathbf {j} }+c{\mathbf {k} })=c{\mathbf {i} }+a{\mathbf {j} }+b{\mathbf {k} }$ As expected, the rotation corresponds to keeping a cube held fixed at one point, and rotating it 120° about the long diagonal through the fixed point (observe how the three axes are permuted cyclically).

#### Quaternion arithmetic in practice

Let's show how we reached the previous result. Let's develop the expression of Template:Mvar (in two stages), and apply the rules

{\begin{alignedat}{2}{\mathbf {ij} }&={\mathbf {k} },&{\mathbf {ji} }&={\mathbf {-k} },\\{\mathbf {jk} }&={\mathbf {i} },&{\mathbf {kj} }&={\mathbf {-i} },\\{\mathbf {ki} }&={\mathbf {j} },&{\mathbf {ik} }&={\mathbf {-j} },\\{\mathbf {i} }^{2}&={\mathbf {j} }^{2}&={\mathbf {k} }^{2}&=-1\end{alignedat}} It gives us:

which is the expected result. As we can see, such computations are relatively long and tedious if done manually; however, in a computer program, this amounts to calling the quaternion multiplication routine twice.

### Quaternion-derived rotation matrix

A quaternion rotation can be algebraically manipulated into a quaternion-derived rotation matrix. By simplifying the quaternion multiplications q p q*, they can be rewritten as a rotation matrix given an axis–angle representation:

${\begin{bmatrix}c+a_{x}^{2}(1-c)&a_{x}a_{y}(1-c)-a_{z}s&a_{x}a_{z}(1-c)+a_{y}s\\a_{y}a_{x}(1-c)+a_{z}s&c+a_{y}^{2}(1-c)&a_{y}a_{z}(1-c)-a_{x}s\\a_{z}a_{x}(1-c)-a_{y}s&a_{z}a_{y}(1-c)+a_{x}s&c+a_{z}^{2}(1-c)\end{bmatrix}}$ where Template:Mvar and Template:Mvar are shorthand for sin θ and cos θ, respectively. Although care should be taken (due to degeneracy as the quaternion approaches the identity quaternion (1) or the sine of the angle approaches zero) the axis and angle can be extracted via:

{\begin{aligned}\mathbf {q} &=q_{r}+q_{i}i+q_{j}j+q_{k}k\\\theta &=2\arccos q_{r}=2\arcsin {\sqrt {q_{i}^{2}+q_{j}^{2}+q_{k}^{2}}}\\(a_{x},a_{y},a_{z})&={\frac {1}{\sin {\tfrac {1}{2}}\theta }}(q_{i},q_{j},q_{k})\end{aligned}} Note that the Template:Mvar equality holds only when Template:Mvar is non-negative.

As with other schemes to apply rotations, the centre of rotation must be translated to the origin before the rotation is applied and translated back to its original position afterwards.

## Explanation

### Quaternions briefly

{{#invoke:main|main}} The complex numbers can be defined by introducing an abstract symbol i which satisfies the usual rules of algebra and additionally the rule i2 = −1. This is sufficient to reproduce all of the rules of complex number arithmetic: for example:

$(a+b{\mathbf {i} })(c+d{\mathbf {i} })=ac+ad{\mathbf {i} }+b{\mathbf {i} }c+b{\mathbf {i} }d{\mathbf {i} }=ac+ad{\mathbf {i} }+bc{\mathbf {i} }+bd{\mathbf {i} }^{2}=(ac-bd)+(bc+ad){\mathbf {i} }$ .

In the same way the quaternions can be defined by introducing abstract symbols i, j, k which satisfy the rules i2 = j2 = k2 = i j k = −1 and the usual algebraic rules except the commutative law of multiplication (a familiar example of such a noncommutative multiplication is matrix multiplication). From this all of the rules of quaternion arithmetic follow: for example, one can show that:

$(a+b{\mathbf {i} }+c{\mathbf {j} }+d{\mathbf {k} })(e+f{\mathbf {i} }+g{\mathbf {j} }+h{\mathbf {k} })=$ The imaginary part $b{\mathbf {i} }+c{\mathbf {j} }+d{\mathbf {k} }$ of a quaternion behaves like a vector ${\vec {v}}=(b,c,d)$ in three dimension vector space, and the real part Template:Mvar behaves like a scalar in R. When quaternions are used in geometry, it is more convenient to define them as a scalar plus a vector:

$a+b{\mathbf {i} }+c{\mathbf {j} }+d{\mathbf {k} }=a+{\vec {v}}$ .

Those who have studied vectors at school might find it strange to add a number to a vector, as they are objects of very different natures, or to multiply two vectors together, as this operation is usually undefined. However, if one remembers that it is a mere notation for the real and imaginary parts of a quaternion, it becomes more legitimate. In other words, the correct reasoning is the addition of two quaternions, one with zero vector/imaginary part, and another one with zero scalar/real part:

$a+{\vec {v}}=(a,{\vec {0}})+(0,{\vec {v}})$ .

### Performance comparisons

This section discusses the performance implications of using quaternions versus other methods (axis/angle or rotation matrices) to perform rotations in 3D.

#### Results

Storage requirements
Method Storage
Rotation matrix 9
Quaternion 4
Angle/axis 3*

* Note: angle/axis can be stored as 3 elements by multiplying the unit rotation axis by half of the rotation angle, forming the logarithm of the quaternion, at the cost of additional calculations.

Performance comparison of rotation chaining operations
Method # multiplies # add/subtracts total operations
Rotation matrices 27 18 45
Quaternions 16 12 28
Performance comparison of vector rotating operations
Method # multiplies # add/subtracts # sin/cos total operations
Rotation matrix 9 6 0 15
Quaternions 15 15 0 30
Angle/axis 23 16 2 41

#### Used methods

There are three basic approaches to rotating a vector Template:Vec:

1. Compute the matrix product of a 3 × 3 rotation matrix Template:Mvar and the original 3 × 1 column matrix representing Template:Vec. This requires 3 × (3 multiplications + 2 additions) = 9 multiplications and 6 additions, the most efficient method for rotating a vector.
2. A rotation can be represented by a unit-length quaternion q = (w, Template:Vec) with scalar (real) part Template:Mvar and vector (imaginary) part Template:Vec. The rotation can be applied to a 3D vector Template:Vec via the formula ${\vec {v}}_{\text{new}}={\vec {v}}+2{\vec {r}}\times ({\vec {r}}\times {\vec {v}}+w{\vec {v}})$ . This requires only 15 multiplications and 15 additions to evaluate (or 18 muls and 12 adds if the factor of 2 is done via multiplication.) This yields the same result as the less efficient but more compact formula ${\vec {v}}_{\text{new}}=q{\vec {v}}q^{-1}$ .
3. Use the angle/axis formula to convert an angle/axis to a rotation matrix Template:Mvar then multiplying with a vector. Converting the angle/axis to Template:Mvar using common subexpression elimination costs 14 multiplies, 2 function calls (sin, cos), and 10 add/subtracts; from item 1, rotating using Template:Mvar adds an additional 9 multiplications and 6 additions for a total of 23 multiplies, 16 add/subtracts, and 2 function calls (sin, cos).

## Pairs of unit quaternions as rotations in 4D space

A pair of unit quaternions zl and zr can represent any rotation in 4D space. Given a four dimensional vector Template:Vec, and pretending that it is a quaternion, we can rotate the vector Template:Vec like this:

$f({\vec {v}})={\mathbf {z} }_{\rm {l}}{\vec {v}}{\mathbf {z} }_{\rm {r}}={\begin{pmatrix}a_{\rm {l}}&-b_{\rm {l}}&-c_{\rm {l}}&-d_{\rm {l}}\\b_{\rm {l}}&a_{\rm {l}}&-d_{\rm {l}}&c_{\rm {l}}\\c_{\rm {l}}&d_{\rm {l}}&a_{\rm {l}}&-b_{\rm {l}}\\d_{\rm {l}}&-c_{\rm {l}}&b_{\rm {l}}&a_{\rm {l}}\end{pmatrix}}{\begin{pmatrix}a_{\rm {r}}&-b_{\rm {r}}&-c_{\rm {r}}&-d_{\rm {r}}\\b_{\rm {r}}&a_{\rm {r}}&d_{\rm {r}}&-c_{\rm {r}}\\c_{\rm {r}}&-d_{\rm {r}}&a_{\rm {r}}&b_{\rm {r}}\\d_{\rm {r}}&c_{\rm {r}}&-b_{\rm {r}}&a_{\rm {r}}\end{pmatrix}}{\begin{pmatrix}w\\x\\y\\z\end{pmatrix}}.$ The pair of matrices represents a rotation of ℝ4. Note that since $({\mathbf {z} }_{\rm {l}}{\vec {v}}){\mathbf {z} }_{\rm {r}}={\mathbf {z} }_{\rm {l}}({\vec {v}}{\mathbf {z} }_{\rm {r}})$ , the two matrices must commute. Therefore, there are two commuting subgroups of the set of four dimensional rotations. Arbitrary four dimensional rotations have 6 degrees of freedom, each matrix represents 3 of those 6 degrees of freedom.

Since the generators of the four-dimensional rotations can be represented by pairs of quaternions (as follows), all four-dimensional rotations can also be represented.

${\mathbf {z} }_{\rm {l}}{\vec {v}}{\mathbf {z} }_{\rm {r}}={\begin{pmatrix}1&-dt_{ab}&-dt_{ac}&-dt_{ad}\\dt_{ab}&1&-dt_{bc}&-dt_{bd}\\dt_{ac}&dt_{bc}&1&-dt_{cd}\\dt_{ad}&dt_{bd}&dt_{cd}&1\end{pmatrix}}{\begin{pmatrix}w\\x\\y\\z\end{pmatrix}}$ ${\mathbf {z} }_{\rm {l}}=1+{dt_{ab}+dt_{cd} \over 2}i+{dt_{ac}-dt_{bd} \over 2}j+{dt_{ad}+dt_{bc} \over 2}k$ ${\mathbf {z} }_{\rm {r}}=1+{dt_{ab}-dt_{cd} \over 2}i+{dt_{ac}+dt_{bd} \over 2}j+{dt_{ad}-dt_{bc} \over 2}k$ 