Determine rotation vector from quaternion: Basic understanding how to use Quaternions in 3D rotation applications and IMU sensors results. It gives a simple definition of quaternions, and will see here how to convert back and forth between Quaternions, Rotational axis-angle representations, and rotation matrices operations into a single Quaternion.


Quaternions were discovered on 16 October 1843 by William Rowan Hamilton. He spent years trying to find a three dimensional number systems, but with no success, when he looked in 4 dimensions instead of 3 it worked.

Quaternions form an interesting algebra where each object contains 4 scalar variables (sometimes known as Euler Parameters not to be confused with Euler angles), these objects can be added and multiplied as a single unit in a similar way to the usual algebra of numbers. However, there is a difference, unlike the algebra of scalar numbers qa * qb is not necessarily equal to qb * qa (where qa and qb are quaternions). In mathematical terms, quaternion multiplication is not commutative.


Quaternions have 4 dimensions (each quaternion consists of 4 scalar numbers), one real dimension and 3 imaginary dimensions. Each of these imaginary dimensions has a unit value of the square root of -1, but they are different square roots of -1 all mutually perpendicular to each other, known as i,j and k. So a quaternion can be represented as follows:

a + i b + j c + k d

It may seem strange that there are 3 square roots of -1, but we have to remember that we are working in 4 dimensions so there are at least 3 ways to get round from +1 to -1. It is not very practical to try to draw 4 dimensions in 2 dimensions, but here is an attempt:

We have to be very careful with this picture of quaternions, it gives an intuative feel for how quaternions can represent rotations in 3D but it is misleading, we might think from this (as Hamilton did) that since i² =-1 that therefore i² represents a rotation of 180° and so ‘i’ represents a rotation of 90°. This is wrong! As we shall see below, to make it work porperly, we need to halve the angle so ‘i’ represents a rotation of 180° and i² represents a rotation of 360°. So +1 and -1 both represent the same rotation, this will be explained more fully later.


Strictly speaking, a quaternion is represented by four elements:

q = q0 + iq1 + jq2 + kq3             (1)

where q0, q1, q2 and q3 are real numbers, and i, j and k are mutually orthogonal imaginary unit vectors. The q0 term is referred to as the “real” component, and the remaining three terms are the “imaginary” components. In practice (and for the remainder of this paper), the imaginary notation is implied, and only the four coefficients are used to specify a quaternion, as in equation 2:

q = (q0, q1, q2, q3)                 (2)

Quaternions are a complicated subject. However, in this paper we will restrict ourselves to a subset of quaternions called rotation quaternions. Rotation quaternions are a mechanism for representing rotations in three dimensions, and can be used as an alternative to rotation matrices in 3D graphics and other applications. Using them requires no understanding of complex numbers.

Rotation quaternions are closely related to the axis-angle representation of rotation. We will therefore start with an explanation of the axis-angle representation, and then show how to convert to a quaternion.

Axis-Angle Representation of 3D Rotations

According to Euler’s rotation theorem, any 3D rotation (or sequence of rotations) can be specified using two parameters: a unit vector that defines an axis of rotation; and an angle θ describing the magnitude of the rotation about that axis. This is illustrated in Figure 2:

Axis-Angle illustration

Figure 2: Any 3D rotation can be specified by an axis of rotation and a rotation angle around that axis

An axis-angle rotation can therefore be represented by four numbers as in equation 3:

(θ, x̂   , ŷ   , ẑ    )                         (3)


(x̂   , ŷ   , ẑ   ) is a unit vector that defines the axis of rotation
θ is the amount of rotation around ( x̂   , ŷ   , ẑ   )

Convert Quaternion to Rotation Matrix

Given the rotation Quaternion q = (q0, q1, q2, q3), the corresponding rotation matrix is:


Or equivalently:


Both methods work for all valid unit rotation Quaternions, including the identity Quaternion.

Convert Rotation Matrix to Quaternion

Given the rotation matrix R:


We can find the equivalent quaternion using two steps.

Step 1: Find the magnitude of each quaternion component. This leaves the sign of each component undefined:



Step 2: To resolve the signs, find the largest of q0, q1, q2, q3 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.


The reason the sign is ambiguous is that 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 for all rotation quaternions, (q0, q1, q2, q3) and (−q0, −q1, −q2, −q3) produce identical rotations. To convert from a rotation matrix to a quaternion, we must arbitrarily pick one of the two possible answers as described in steps 1 and 2.


Properties of Quaternions

The following are some useful properties of quaternions. Up until now, this paper has discussed only rotation quaternions. However, rotation quaternions are only a subset of all possible quaternions, just as a rotation matrices are a subset of all possible 3×3 matrices. The following properties apply to all quaternions unless otherwise specified.

  1. The length (magnitude) of a quaternion is equation
  2. A quaternion is a “unit” quaternion if |q| = 1.
  3. All rotation quaternions must be unit quaternions.
  4. The quaternion q = (1, 0, 0, 0) is the identity quaternion. It represents no rotation. If q is an arbitrary quaternion and i is the identity quaternion, then qi = iq = q.
  5. The conjugate of a quaternion is q* = ( q0, −q1, −q2, −q3 )
  6. The inverse of a quaternion is equation. The product of a quaternion and its inverse is the identity quaternion: qq−1 = q−1q = (1, 0, 0, 0). Note that for this special case, quaternion multiplication is commutative.
  7. For rotation quaternions, the inverse equals the conjugate. So for rotation quaternions, q−1 = q* = ( q0, −q1, −q2, −q3 ).
  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. Quaternion multiplication is not commutative: abba



Ref. Links: