# Quaternions and spatial rotation

(Redirected from Quaternion rotation)

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,[1], orbital mechanics of satellites[2] and crystallographic texture analysis.[3]

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

A visualization of a rotation represented by an Euler axis and angle.

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 θ about a fixed axis (called Euler axis) that runs through the fixed point. The Euler axis is typically represented by a unit vector u. Therefore, any rotation in three dimensions can be represented as a combination of a vector u and a scalar θ. 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 θ 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 u.

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{p q} \vec{v} (\mathbf{p q})^{-1} = \mathbf{p q} \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 n times the angle around the same axis as q. This can be extended to arbitrary real n, 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

A rotation of 120° around the first diagonal permutes i, j, and k cyclically.

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

Consider the rotation f around the axis $\vec{v} = \mathbf{i} + \mathbf{j} + \mathbf{k}$, with a rotation angle of 120°, or /3 radians.

$\alpha = \frac{2 \pi}{3}$
pq p for q = 1 + i + j + k/2 on the unit 3-sphere. Note this one-sided (namely, left) multiplication yields a 60° rotation of quaternions

The length of v is 3, the half angle is π/3 (60°) with cosine 1/2, (cos 60° = 0.5) and sine 3/2, (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 f 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 f (in two stages), and apply the rules

\begin{alignat}{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{alignat}

It gives us:

$\begin{array}{lll} 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} \\ &=& \frac{1}{4} ( (a\mathbf{i} + b\mathbf{j} + c\mathbf{k}) +(- a + b\mathbf{k} - c\mathbf{j}) + (-a\mathbf{k} - b +c\mathbf{i}) + (a\mathbf{j} - b\mathbf{i} - c))\\ && (1 - \mathbf{i} - \mathbf{j} - \mathbf{k})\\ &=& \frac{1}{4} ( (-a - b - c) + (a - b+ c) \mathbf{i} + (a + b - c) \mathbf{j} + (-a + b + c) \mathbf{k})\\ && (1 - \mathbf{i} - \mathbf{j} - \mathbf{k})\\ &=& \frac{1}{4} ( ( (-a - b - c) + (a - b + c) \mathbf{i} + (a + b - c) \mathbf{j} + (-a + b + c) \mathbf{k})\\ &&+ ( (a + b + c) \mathbf{i} + (a - b + c) + (a + b - c) \mathbf{k} + (a - b - c) \mathbf{j})\\ &&+ ( (a + b + c) \mathbf{j} + (-a + b - c) \mathbf{k} + (a + b - c) + (-a + b + c) \mathbf{i})\\ &&+ ( (a + b + c) \mathbf{k} + (a - b + c) \mathbf{j} + (-a - b + c) \mathbf{i} + (-a + b + c))\\ &=& \frac{1}{4} ( ( (-a - b - c) + (a - b + c) + (a + b - c) + (-a + b + c) )\\ &&+ ( (a - b + c) + (a + b + c) + (-a + b + c) + (-a - b + c) ) \mathbf{i}\\ &&+ ( (a + b - c) + (a - b - c) + (a + b + c) + (a - b + c) ) \mathbf{j}\\ &&+ ( (-a + b + c) + (a + b - c) + (-a + b - c) + (a + b + c) ) \mathbf{k})\\ &=& \frac{1}{4} (0 + 4c \mathbf{i} + 4a \mathbf{j} + 4b \mathbf{k})\\ &=&c\mathbf{i} + a\mathbf{j} + b\mathbf{k} \end{array}$

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 s and c 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{align} \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{align}

Note that the θ equality holds only when qr 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

Main article: Quaternions

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}) =$

$(ae - bf - cg - dh) + (af + be + ch - dg) \mathbf{i} + (ag - bh + ce + df) \mathbf{j} + (ah + bg - cf + de) \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 a 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})$.

We can express quaternion multiplication in the modern language of vector cross and dot products (which were actually inspired by the quaternions in the first place[citation needed]). In place of the rules i2 = j2 = k2 = ijk = −1 we have the quaternion multiplication rule:

$\vec{v} \vec{w} = \vec{v} \times \vec{w} - \vec{v} \cdot \vec{w},$

where:

• $\vec{v} \vec{w}$ is the resulting quaternion,
• $\vec{v} \times \vec{w}$ is vector cross product (a vector),
• $\vec{v} \cdot \vec{w}$ is vector scalar product (a scalar).

Quaternion multiplication is noncommutative (because of the cross product, which anti-commutes), while scalar–scalar and scalar–vector multiplications commute. From these rules it follows immediately that (see details):

$(s + \vec{v}) (t + \vec{w}) = (s t - \vec{v} \cdot \vec{w}) + (s \vec{w} + t \vec{v} + \vec{v} \times \vec{w})$.

The (left and right) multiplicative inverse or reciprocal of a nonzero quaternion is given by the conjugate-to-norm ratio (see details):

$(s + \vec{v})^{-1} = \frac{(s + \vec{v})^*}{\lVert s + \vec{v} \rVert^2} = \frac{s - \vec{v}}{s^2 + \lVert \vec{v} \rVert^2}$,

as can be verified by direct calculation.

### Proof of the quaternion rotation identity

Let $\vec{u}$ be a unit vector (the rotation axis) and let $q = \cos \frac{\alpha}{2} + \vec{u} \sin \frac{\alpha}{2}$. Our goal is to show that

$\vec{v'} = q \vec{v} q^{-1} = \left( \cos \frac{\alpha}{2} + \vec{u} \sin \frac{\alpha}{2} \right) \, \vec{v} \, \left( \cos \frac{\alpha}{2} - \vec{u} \sin \frac{\alpha}{2} \right)$

yields the vector $\vec{v}$ rotated by an angle $\alpha$ around the axis $\vec{u}$. Expanding out, we have

$\begin{array}{lll} \vec{v'} &=& \vec{v} \cos^2 \frac{\alpha}{2} + (\vec{u}\vec{v} - \vec{v}\vec{u}) \sin \frac{\alpha}{2} \cos \frac{\alpha}{2} - \vec{u}\vec{v}\vec{u} \sin^2 \frac{\alpha}{2} \\ &=& \vec{v} \cos^2 \frac{\alpha}{2} + 2 (\vec{u} \times \vec{v}) \sin \frac{\alpha}{2} \cos \frac{\alpha}{2} - (\vec{v} (\vec{u} \cdot \vec{u}) - 2 \vec{u} (\vec{u} \cdot \vec{v})) \sin^2 \frac{\alpha}{2} \\ &=& \vec{v} (\cos^2 \frac{\alpha}{2} - \sin^2 \frac{\alpha}{2}) + (\vec{u} \times \vec{v}) (2 \sin \frac{\alpha}{2} \cos \frac{\alpha}{2}) + \vec{u} (\vec{u} \cdot \vec{v}) (2 \sin^2 \frac{\alpha}{2}) \\ &=& \vec{v} \cos \alpha + (\vec{u} \times \vec{v}) \sin \alpha + \vec{u} (\vec{u} \cdot \vec{v}) (1 - \cos \alpha) \\ &=& (\vec{v} - \vec{u} (\vec{u} \cdot \vec{v})) \cos \alpha + (\vec{u} \times \vec{v}) \sin \alpha + \vec{u} (\vec{u} \cdot \vec{v}) \\ &=& \vec{v}_{\bot} \cos \alpha + (\vec{u} \times \vec{v}) \sin \alpha + \vec{v}_{\|} \end{array}$

where $\vec{v}_{\bot}$ and $\vec{v}_{\|}$ are the components of v perpendicular and parallel to u respectively. This is the formula of a rotation by α around the u axis.

## Quaternion rotation operations

A very formal explanation of the properties used in this section is given by Altman.[4]

### The hypersphere of rotations

Main article: Rotation group SO(3)

#### Visualizing the space of rotations

Unit quaternions represent the group of Euclidean rotations in three dimensions in a very straightforward way. The correspondence between rotations and quaternions can be understood by first visualizing the space of rotations itself.

Two rotations by different angles and different axes in the space of rotations. The length of the vector is related to the magnitude of the rotation.

In order to visualize the space of rotations, it helps to consider a simpler case. Any rotation in three dimensions can be described by a rotation by some angle about some axis; for our purposes, we will use an axis vector to establish handedness for our angle. Consider the special case in which the axis of rotation lies in the xy plane. We can then specify the axis of one of these rotations by a point on a circle through which the vector crosses, and we can select the radius of the circle to denote the angle of rotation. Similarly, a rotation whose axis of rotation lies in the xy plane can be described as a point on a sphere of fixed radius in three dimensions. Beginning at the north pole of a sphere in three-dimensional space, we specify the point at the north pole to be the identity rotation (a zero angle rotation). Just as in the case of the identity rotation, no axis of rotation is defined, and the angle of rotation (zero) is irrelevant. A rotation having a very small rotation angle can be specified by a slice through the sphere parallel to the xy plane and very near the north pole. The circle defined by this slice will be very small, corresponding to the small angle of the rotation. As the rotation angles become larger, the slice moves in the negative z direction, and the circles become larger until the equator of the sphere is reached, which will correspond to a rotation angle of 180 degrees. Continuing southward, the radii of the circles now become smaller (corresponding to the absolute value of the angle of the rotation considered as a negative number). Finally, as the south pole is reached, the circles shrink once more to the identity rotation, which is also specified as the point at the south pole.

Notice that a number of characteristics of such rotations and their representations can be seen by this visualization. The space of rotations is continuous, each rotation has a neighborhood of rotations which are nearly the same, and this neighborhood becomes flat as the neighborhood shrinks. Also, each rotation is actually represented by two antipodal points on the sphere, which are at opposite ends of a line through the center of the sphere. This reflects the fact that each rotation can be represented as a rotation about some axis, or, equivalently, as a negative rotation about an axis pointing in the opposite direction (a so-called double cover). The "latitude" of a circle representing a particular rotation angle will be half of the angle represented by that rotation, since as the point is moved from the north to south pole, the latitude ranges from zero to 180 degrees, while the angle of rotation ranges from 0 to 360 degrees. (the "longitude" of a point then represents a particular axis of rotation.) Note however that this set of rotations is not closed under composition. Two successive rotations with axes in the xy plane will not necessarily give a rotation whose axis lies in the xy plane, and thus cannot be represented as a point on the sphere. This will not be the case with a general rotation in 3-space, in which rotations do form a closed set under composition.

The sphere of rotations for the rotations that have a "horizontal" axis (in the xy plane).

This visualization can be extended to a general rotation in 3-dimensional space. The identity rotation is a point, and a small angle of rotation about some axis can be represented as a point on a sphere with a small radius. As the angle of rotation grows, the sphere grows, until the angle of rotation reaches 180 degrees, at which point the sphere begins to shrink, becoming a point as the angle approaches 360 degrees (or zero degrees from the negative direction). This set of expanding and contracting spheres represents a hypersphere in four dimensional space (a 3-sphere). Just as in the simpler example above, each rotation represented as a point on the hypersphere is matched by its antipodal point on that hypersphere. The "latitude" on the hypersphere will be half of the corresponding angle of rotation, and the neighborhood of any point will become "flatter" (i.e. be represented by a 3-D Euclidean space of points) as the neighborhood shrinks. This behavior is matched by the set of unit quaternions: A general quaternion represents a point in a four dimensional space, but constraining it to have unit magnitude yields a three-dimensional space equivalent to the surface of a hypersphere. The magnitude of the unit quaternion will be unity, corresponding to a hypersphere of unit radius. The vector part of a unit quaternion represents the radius of the 2-sphere corresponding to the axis of rotation, and its magnitude is the cosine of half the angle of rotation. Each rotation is represented by two unit quaternions of opposite sign, and, as in the space of rotations in three dimensions, the quaternion product of two unit quaternions will yield a unit quaternion. Also, the space of unit quaternions is "flat" in any infinitesimal neighborhood of a given unit quaternion.

#### Parameterizing the space of rotations

We can parameterize the surface of a sphere with two coordinates, such as latitude and longitude. But latitude and longitude are ill-behaved (degenerate) at the north and south poles, though the poles are not intrinsically different from any other points on the sphere. At the poles (latitudes +90° and −90°), the longitude becomes meaningless.

It can be shown that no two-parameter coordinate system can avoid such degeneracy. We can avoid such problems by embedding the sphere in three-dimensional space and parameterizing it with three Cartesian coordinates (w, x, y), placing the north pole at (w, x, y) = (1, 0, 0), the south pole at (w, x, y) = (−1, 0, 0), and the equator at w = 0, x2 + y2 = 1. Points on the sphere satisfy the constraint w2 + x2 + y2 = 1, so we still have just two degrees of freedom though there are three coordinates. A point (w, x, y) on the sphere represents a rotation in the ordinary space around the horizontal axis directed by the vector (x, y, 0) by an angle $\alpha= 2 \cos^{-1} w = 2 \sin^{-1} \sqrt{x^2+y^2}$.

In the same way the hyperspherical space of 3D rotations can be parameterized by three angles (Euler angles), but any such parameterization is degenerate at some points on the hypersphere, leading to the problem of gimbal lock. We can avoid this by using four Euclidean coordinates w, x, y, z, with w2 + x2 + y2 + z2 = 1. The point (w, x, y, z) represents a rotation around the axis directed by the vector (x, y, z) by an angle $\alpha = 2 \cos^{-1} w = 2 \sin^{-1} \sqrt{x^2+y^2+z^2}.$

## Explaining quaternions' properties with rotations

### Non-commutativity

The multiplication of quaternions is non-commutative. This fact explains how the pq p q−1 formula can work at all, having q q−1 = 1 by definition. Since the multiplication of unit quaternions corresponds to the composition of three-dimensional rotations, this property can be made intuitive by showing that three-dimensional rotations are not commutative in general.

Set two books next to each other. Rotate one of them 90 degrees clockwise around the z axis, then flip it 180 degrees around the x axis. Take the other book, flip it 180° around x axis first, and 90° clockwise around z later. The two books do not end up parallel. This shows that, in general, the composition of two different rotations around two distinct spatial axes will not commute.

### Orientation

The vector cross product, used to define the axis–angle representation, does confer an orientation ("handedness") to space: in a three-dimensional vector space, the three vectors in the equation a × b = c will always form a right-handed set (or a left-handed set, depending on how the cross product is defined), thus fixing an orientation in the vector space. Alternatively, the dependence on orientation is expressed in referring to such u that specifies a rotation as to axial vectors. In quaternionic formalism the choice of an orientation of the space corresponds to order of multiplication: ij = k but ji = −k. If one reverses the orientation, then the formula above becomes pq−1p q, i.e., a unit q is replaced with the conjugate quaternion – the same behaviour as of axial vectors.

## Comparison with other representations of rotations

The representation of a rotation as a quaternion (4 numbers) is more compact than the representation as an orthogonal matrix (9 numbers). Furthermore, for a given axis and angle, one can easily construct the corresponding quaternion, and conversely, for a given quaternion one can easily read off the axis and the angle. Both of these are much harder with matrices or Euler angles.

In video games and other applications, one is often interested in “smooth rotations”, meaning that the scene should slowly rotate and not in a single step. This can be accomplished by choosing a curve such as the spherical linear interpolation in the quaternions, with one endpoint being the identity transformation 1 (or some other initial rotation) and the other being the intended final rotation. This is more problematic with other representations of rotations.

When composing several rotations on a computer, rounding errors necessarily accumulate. A quaternion that’s slightly off still represents a rotation after being normalised: a matrix that’s slightly off may not be orthogonal anymore and is harder to convert back to a proper orthogonal matrix.

Quaternions also avoid a phenomenon called gimbal lock which can result when, for example in pitch/yaw/roll rotational systems, the pitch is rotated 90° up or down, so that yaw and roll then correspond to the same motion, and a degree of freedom of rotation is lost. In a gimbal-based aerospace inertial navigation system, for instance, this could have disastrous results if the aircraft is in a steep dive or ascent.

### Conversion to and from the matrix representation

#### From a quaternion to an orthogonal matrix

The orthogonal matrix corresponding to a rotation by the unit quaternion z = a + bi + cj + dk (with | z | = 1) when post-multiplying with a column vector is given by

$R = \begin{pmatrix} a^2+b^2-c^2-d^2&2bc-2ad &2bd+2ac \\ 2bc+2ad &a^2-b^2+c^2-d^2&2cd-2ab \\ 2bd-2ac &2cd+2ab &a^2-b^2-c^2+d^2\\ \end{pmatrix}.$

This rotation matrix is used on vector w as w_rotated = R*w. The quaternion representation of this rotation is given by:

[0, w_rotated^T] = z [0, w^T] z*

where z* is the conjugate of the quaternion z, given by z^* = a -bi - cj - dk

Also, quaternion multiplication is defined as (assuming a and b are quaternions, like z above):

ab = [a_0*b_0 - dotproduct(a_vec,b_vec); a_0*b_vec + b_0*a_vec + crossproduct(a_vec,b_vec)]

where the order a,b is important since the cross product of two vectors is not commutative.

#### From an orthogonal matrix to a quaternion

One must be careful when converting a rotation matrix to a quaternion, as several straightforward methods tend to be unstable when the trace (sum of the diagonal elements) of the rotation matrix is zero or very small. For a stable method of converting an orthogonal matrix to a quaternion, see Rotation matrix#Quaternion.

#### Fitting quaternions

The above section described how to recover a quaternion q from a 3×3 rotation matrix Q. Suppose, however, that we have some matrix Q that is not a pure rotation—due to round-off errors, for example—and we wish to find the quaternion q that most accurately represents Q. In that case we construct a symmetric 4×4 matrix

$K = \frac13 \begin{bmatrix} Q_{xx}-Q_{yy}-Q_{zz} & Q_{yx}+Q_{xy} & Q_{zx}+Q_{xz} & Q_{yz}-Q_{zy} \\ Q_{yx}+Q_{xy} & Q_{yy}-Q_{xx}-Q_{zz} & Q_{zy}+Q_{yz} & Q_{zx}-Q_{xz} \\ Q_{zx}+Q_{xz} & Q_{zy}+Q_{yz} & Q_{zz}-Q_{xx}-Q_{yy} & Q_{xy}-Q_{yx} \\ Q_{yz}-Q_{zy} & Q_{zx}-Q_{xz} & Q_{xy}-Q_{yx} & Q_{xx}+Q_{yy}+Q_{zz} \end{bmatrix} ,$

and find the eigenvector (x, y, z, w) corresponding to the largest eigenvalue (that value will be 1 if and only if Q is a pure rotation). The quaternion so obtained will correspond to the rotation closest to the original matrix Q[dubious ][5]

### 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 v:

1. Compute the matrix product of a 3 × 3 rotation matrix R and the original 3 × 1 column matrix representing v. 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, r) with scalar (real) part w and vector (imaginary) part r. The rotation can be applied to a 3D vector v 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 R then multiplying with a vector. Converting the angle/axis to R using common subexpression elimination costs 14 multiplies, 2 function calls (sin, cos), and 10 add/subtracts; from item 1, rotating using R 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 v, and pretending that it is a quaternion, we can rotate the vector v 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$