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.

**Introduction**

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*=*q*_{0}+*iq*_{1}+*jq*_{2}+*kq*_{3 }(1)where *q*_{0}, *q*_{1}, *q*_{2} and *q*_{3} are real numbers, and ** i**,

**and**

*j***are mutually orthogonal imaginary unit vectors. The**

*k**q*

_{0}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*= (*q*_{0},*q*_{1},*q*_{2},*q*_{3}) (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.

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

** 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)where:

**) is a unit vector that defines the axis of rotation**

*(x̂ , ŷ , ẑ**θ*is the amount of rotation around

*( x̂ , ŷ , ẑ )*

## Convert Quaternion to Rotation Matrix

Given the rotation Quaternion ** q = (q_{0}, q_{1}, q_{2}, q_{3})**, 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

*q*

_{0},

*q*

_{1},

*q*

_{2},

*q*

_{3}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, (*q*_{0}, *q*_{1}, *q*_{2}, *q*_{3}) and (−*q*_{0}, −*q*_{1}, −*q*_{2}, −*q*_{3}) 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.

- The length (magnitude) of a quaternion is
- A quaternion is a “unit” quaternion if |
*q*| = 1. - All rotation quaternions must be unit quaternions.
- The quaternion
= (1, 0, 0, 0) is the**q**. It represents no rotation. If**identity quaternion**is an arbitrary quaternion and**q**is the identity quaternion, then**i**=**qi**=**iq**.**q** - The
of a quaternion is q**conjugate**^{*}= (*q*_{0}, −*q*_{1}, −*q*_{2}, −*q*_{3}) - The inverse of a quaternion is . The product of a quaternion and its inverse is the identity quaternion:
=**qq**^{−1}**q**^{−1}= (1, 0, 0, 0). Note that for this special case, quaternion multiplication is commutative.**q** - For rotation quaternions, the inverse equals the conjugate. So for rotation quaternions,
=**q**^{−1}= (**q**^{*}*q*)._{0}, −q_{1}, −q_{2}, −q_{3} - 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
, then rotating it again using**q**or**q**^{−1}will return it to its original location.**q**^{*} - 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
is a rotation quaternion, then**q**and −**q**will produce the same rotation.**q** - A rotation of
followed by a rotation of**q**_{a}can be combined into the single rotation**q**_{b}=**q**_{c}**q**_{b}. This can be extended to an arbitrary number of rotations. Note that the order matters (because quaternion multiplication is not commutative).**q**_{a} - Quaternion multiplication is associative: (
)*ab*=*c*(*a*)*bc* - Quaternion multiplication is not commutative:
≠*ab**ba*

Ref. Links:[1] http://danceswithcode.net/engineeringnotes/quaternions/quaternions.html

[2] http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/index.htm

[3] https://in.mathworks.com/help/aeroblks/quaternionstorotationangles.html

[4] http://www.cs.unc.edu/~tracker/media/pdf/SIGGRAPH2001_CoursePack_08.pdf