Quaternions and spatial rotation


s, 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 compact, more numerically stable, and more efficient. Quaternions have applications in computer graphics, computer vision, robotics, navigation, molecular dynamics, flight dynamics, orbital mechanics of satellites and crystallographic texture analysis.
When used to represent rotation, unit quaternions are also called rotation quaternions as they represent the 3D rotation group. When used to represent an orientation, they are called orientation quaternions or attitude quaternions. The equation for spatial rotations can be summarized for radians about a unit axis as the Quaternion where and.

Introduction

Quaternions

The complex numbers can be defined by introducing an abstract symbol which satisfies the usual rules of algebra and additionally the rule. This is sufficient to reproduce all of the rules of complex number arithmetic: for example:
In the same way the quaternions can be defined by introducing abstract symbols,, which satisfy the rules and the usual algebraic rules except the commutative law of multiplication. From this all of the rules of quaternion arithmetic follow, such as the rules on multiplication of quaternion basis elements. Using these rules, one can show that:
The imaginary part of a quaternion behaves like a vector in three dimension vector space, and the real part behaves like a scalar in. When quaternions are used in geometry, it is more convenient to define them as a scalar plus a vector:
Some 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:
We can express quaternion multiplication in the modern language of vector cross and dot products. When multiplying the vector/imaginary parts, in place of the rules we have the quaternion multiplication rule:
where:
Quaternion multiplication is noncommutative, while scalar–scalar and scalar–vector multiplications commute. From these rules it follows immediately that :
The multiplicative inverse or reciprocal of a nonzero quaternion is given by the conjugate-to-norm ratio :
as can be verified by direct calculation.

Proof of the quaternion rotation identity

Let be a unit vector and let. Our goal is to show that
yields the vector rotated by an angle around the axis. Expanding out, we have
where and are the components of perpendicular and parallel to respectively. This is the formula of a rotation by around the axis.

Using quaternion as 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 about a fixed axis that runs through the fixed point. The Euler axis is typically represented by a unit vector . Therefore, any rotation in three dimensions can be represented as a combination of a vector and a scalar. Quaternions give a simple way to encode this axis–angle representation in four numbers, and can be used to apply the corresponding rotation to a position vector, representing a point relative to the origin in R3.
A Euclidean vector such as or can be rewritten as or, where,, are unit vectors representing the three Cartesian axes. A rotation through an angle of around the axis defined by a unit vector
can be represented by a quaternion. This can be done using an extension of Euler's formula:
It can be shown that the desired rotation can be applied to an ordinary vector in 3-dimensional space, considered as a quaternion with a real coordinate equal to zero, by evaluating the [|conjugation] of by :
using the Hamilton product, where is the new position vector of the point after the rotation. In a programmatic implementation, this is achieved by constructing a quaternion whose vector part is and real part equals zero and then performing the quaternion multiplication. The vector part of the resulting quaternion is the desired vector.
Mathematically, this operation carries the set of all "pure" quaternions —which constitute a 3-dimensional space among the quaternions—into itself, by the desired rotation about the axis u, by the angle θ.
The rotation is clockwise if our line of sight points in the same direction as.
In this instance, is a unit quaternion and
It follows that conjugation by the product of two quaternions is the composition of conjugations by these quaternions: If and are unit quaternions, then rotation by is
which is the same as rotating by and then by . The scalar component of the result is necessarily zero.
The quaternion inverse of a rotation is the opposite rotation, since. The square of a quaternion rotation is a rotation by twice the angle around the same axis. More generally is a rotation by times the angle around the same axis as. This can be extended to arbitrary real, allowing for smooth interpolation between spatial orientations; see Slerp.
Two rotation quaternions can be combined into one equivalent quaternion by the relation:
in which corresponds to the rotation followed by the rotation. Thus, an arbitrary number of rotations can be composed together and then applied as a single rotation.

Example

The conjugation operation

Conjugating by refers to the operation.
Consider the rotation around the axis, with a rotation angle of 120°, or radians.
. Note this one-sided multiplication yields a 60° rotation of quaternions
The length of is, the half angle is with cosine, and sine,. We are therefore dealing with a conjugation by the unit quaternion
If is the rotation function,
It can be proven that the inverse of a unit quaternion is obtained simply by changing the sign of its imaginary components. As a consequence,
and
This can be simplified, using the ordinary rules for quaternion arithmetic, to
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.

Quaternion-derived rotation matrix

A quaternion rotation can be algebraically manipulated into a matrix rotation, where R is the rotation matrix given by:
Here and if is a unit quaternion,
This can be obtained by using vector calculus and linear algebra if we express and as scalar and vector parts and use the formula for the multiplication operation in the equation. If we write as, as and as, where, our equation turns into. By using the formula for multiplication of two quaternions that are expressed as scalar and vector parts,
this equation can be rewritten as
where denotes the outer product, is the identity matrix and is the transformation matrix that when multiplied from the left with a vector gives the cross product.
Since, we can identify as, which upon expansion should result in the expression written in matrix form above.

Recovering the axis-angle representation

The expression rotates any vector quaternion around an axis given by the vector by the angle, where and depends on the quaternion.
and can be found from the following equations:
where is the two-argument arctangent. Care should be taken when the quaternion approaches a scalar, since due to degeneracy the axis of an identity rotation is not well-defined.

The composition of spatial rotations

A benefit of the quaternion formulation of the composition of two rotations RB and RA is that it yields directly the rotation axis and angle of the composite rotation RC=RBRA.
Let the quaternion associated with a spatial rotation R be constructed from its rotation axis S with the rotation angle φ around this axis. The associated quaternion is given by,
Then the composition of the rotation RB with RA is the rotation RC=RBRA with rotation axis and angle defined by the product of the quaternions
that is
Expand this product to obtain
Divide both sides of this equation by the identity, which is the law of cosines on a sphere,
and compute
or compute
This is Rodrigues formula for the axis of a composite rotation defined in terms of the axes of the two rotations. He derived this formula in 1840.
The three rotation axes A, B, and C form a spherical triangle and the dihedral angles between the planes formed by the sides of this triangle are defined by the rotation angles. Hamilton presented the component form of these equations showing that the quaternion product computes the third vertex of a spherical triangle from two given vertices and their associated arc-lengths, which is also defines an algebra for points in Elliptic geometry.

Differentiation with respect to the rotation quaternion

The rotated quaternion needs to be differentiated with respect to the rotating quaternion, when the rotation is estimated from numerical optimization. The estimation of rotation angle is an essential procedure in 3D object registration or camera calibration. The derivative can be represented using the Matrix Calculus notation.
A derivation can be found in.

Quaternion rotation operations

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

The hypersphere of rotations

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.
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. Just as in the case of the identity rotation, no axis of rotation is defined, and the angle of rotation 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. 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. 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. 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.
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. This set of expanding and contracting spheres represents a hypersphere in four dimensional space. 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" 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 at the north and south poles, though the poles are not intrinsically different from any other points on the sphere. At the poles, 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, placing the north pole at, the south pole at, and the equator at,. Points on the sphere satisfy the constraint, so we still have just two degrees of freedom though there are three coordinates. A point on the sphere represents a rotation in the ordinary space around the horizontal axis directed by the vector by an angle.
In the same way the hyperspherical space of 3D rotations can be parameterized by three 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, with. The point represents a rotation around the axis directed by the vector by an angle

Explaining quaternions' properties with rotations

Non-commutativity

The multiplication of quaternions is non-commutative. This fact explains how the formula can work at all, having 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 to space: in a three-dimensional vector space, the three vectors in the equation will always form a right-handed set, thus fixing an orientation in the vector space. Alternatively, the dependence on orientation is expressed in referring to such that specifies a rotation as to axial vectors. In quaternionic formalism the choice of an orientation of the space corresponds to order of multiplication: but. If one reverses the orientation, then the formula above becomes, i.e., a unit is replaced with the conjugate quaternion – the same behaviour as of axial vectors.

Comparison with other representations of rotations

Advantages of quaternions

The representation of a rotation as a quaternion is more compact than the representation as an orthogonal matrix. 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 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 is slightly off still represents a rotation after being normalised: a matrix that is slightly off may not be orthogonal any more 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 when post-multiplying with a column vector is given by
This rotation matrix is used on vector as. The quaternion representation of this rotation is given by:
where is the conjugate of the quaternion, given by
Also, quaternion multiplication is defined as :
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 of the rotation matrix is zero or very small. For a stable method of converting an orthogonal matrix to a quaternion, see the Quaternion subsection of the Rotation matrix article.

Fitting quaternions

The above section described how to recover a quaternion from a rotation matrix. Suppose, however, that we have some matrix that is not a pure rotation—due to round-off errors, for example—and we wish to find the quaternion that most accurately represents. In that case we construct a symmetric matrix
and find the eigenvector corresponding to the largest eigenvalue. The quaternion so obtained will correspond to the rotation closest to the original matrix .

Performance comparisons

This section discusses the performance implications of using quaternions versus other methods to perform rotations in 3D.

Results

MethodStorage
Rotation matrix9
Quaternion3 or 4
Angle/axis3 or 4

Only three of the quaternion components are independent, as a rotation is represented by a unit quaternion. For further calculation one usually needs all four elements, so all calculations would suffer additional expense from recovering the fourth component. Likewise, angle/axis can be stored in a three-component vector by multiplying the unit direction by the angle, but this comes at additional computational cost when using it for calculations.
Method# multiplies# add/subtractstotal operations
Rotation matrices271845
Quaternions161228

Method# multiplies# add/subtracts# sin/costotal operations
Rotation matrix96015
Quaternions *Without intermediate matrix1515030
Quaternions *With intermediate matrix2118039
Angle/axisWithout intermediate matrix1813230 + 3
Angle/axisWith intermediate matrix2116237 + 2

* Note: Quaternions can be implicitly converted to a rotation-like matrix, which levels the following vectors rotating cost with the rotation matrix method

Used methods

There are three basic approaches to rotating a vector :
  1. Compute the matrix product of a 3 × 3 rotation matrix and the original 3 × 1 column matrix representing. This requires 3 × = 9 multiplications and 6 additions, the most efficient method for rotating a vector.
  2. A rotation can be represented by a unit-length quaternion with scalar part and vector part. The rotation can be applied to a 3D vector via the formula. This requires only 15 multiplications and 15 additions to evaluate This formula, originally thought to be used with axis/angle notation, can also be applied to quaternion notation. This yields the same result as the less efficient but more compact formula of quaternion multiplication.
  3. Use the angle/axis formula to convert an angle/axis to a rotation matrix then multiplying with a vector, or, similarly, use a formula to convert quaternion notation to a rotation matrix, then multiplying with a vector. Converting the angle/axis to costs 12 multiplies, 2 function calls, and 10 add/subtracts; from item 1, rotating using adds an additional 9 multiplications and 6 additions for a total of 21 multiplies, 16 add/subtracts, and 2 function calls. Converting a quaternion to costs 12 multiplies and 12 add/subtracts; from item 1, rotating using adds an additional 9 multiplications and 6 additions for a total of 21 multiplies and 18 add/subtracts.
Method# multiplies# add/subtracts# sin/costotal operations
Rotation matrix9n6n015n
Quaternions *Without intermediate matrix15n15n030n
Quaternions *With intermediate matrix9n + 126n + 12015n + 24
Angle/axisWithout intermediate matrix18n12n + 1230n + 3
Angle/axisWith intermediate matrix9n + 126n + 10215n + 24

Pairs of unit quaternions as rotations in 4D space

A pair of unit quaternions and can represent any rotation in 4D space. Given a four dimensional vector, and assuming that it is a quaternion, we can rotate the vector like this:
The pair of matrices represents a rotation of ℝ4. Note that since, 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, all four-dimensional rotations can also be represented.