Quaternion Maths

Written by Paul Bourke
June 2001


Most students of algebra are familiar with complex numbers, that is, numbers that have two components called real and imaginary that can often be interpreted as 2 dimensions. A complex number is normally written as a + ib where i2 = -1 and a and b are two real values quantities. This idea can be extended to higher dimensions but it turns out that 4 components have useful properties. These are called quaternions and are attributed to Sir William Rowan Hamilton who published a major analysis in 1844 called "On a Species of Imaginary Quantities Connected with a Theory of Quaternions" in the Proceedings of the Royal Irish Academ (2, pp 424-434)

In this discussion we will write a quaternion "Q" as

Q = r + a i + b j + c k

Where "r" might be considered the real component and "a", "b", "c" real valued. The 4 vector (r,a,b,c) might be considered to be a vector in the 4D quaternion space.

When performing operations on complex numbers whenever one encounters i2 then one knows that is equal to the simpler -1. There are similar but slightly more complicated relationships between i,j,k in quaternion space. They are as follows:

i2 = j2 = k2 = -1
i j = k j k = i k i = j
j i = -k k j = -i i k = -j

Note that the order in which i,j,k appears in an expression is important. Also note that there is no linear relationship between i,j,k.

Addition

Addition (or subtraction) of two quaternions Q1 = r1 + a1 i + b1 j + c1 k and Q2 = r2 + a2 i + b2 j + c2 k is performed as follows.

Q1 + Q2 = r1+r2 + (a1+a2) i + (b1+b2) j + (c1 + c2) k
Congujate

The congujate of Q = Q* = r - a i - b j - c k.

Multiplication

Multiplication of two quaternions is somewhat involved but follows directly from the relationships above.

Q1 Q2 = [ r1 r2 - a1 a2 - b1 b2 - c1 c2 ] +
[ r1 a2 + a1 r2 + b1 c2 - c1 b2 ] i +
[ r1 b2 + b1 r2 + c1 a2 - a1 c2 ] j +
[ r1 c2 + c1 r2 + a1 b2 - b1 a2 ] k

Note that quaternion multiplication is not commutative, that is, Q1 Q2 is NOT the same as Q2 Q1

Length (modulus)

The length (magnitude) of a quaternion is the familiar coordinate length in 4 dimensional space.

|Q| = sqrt( Q Q* )

where Q* is the congujate (see later) which expands to

|Q| = sqrt(r2 + a2 + b2 + c2)

and

|Q1 Q2| = |Q1| |Q2|

Inverse

The inverse of a quaternion Q-1 such that Q Q-1 = 1 is given by

  r - a i - b j - c k
Q-1 = ------------------------
  |Q|2

The inverse of a normalised quaternion is simply the congujate, otherwise the magnitude of the inverse is 1/|Q|. So the above expression normalises the quaternion and then scales by 1/|Q|.

Division

Division of Q1 by Q2 is as follows

Q1   Q1 (2 r2 - Q2)
-------- = -------------------
Q2   |Q2|2
Exponential

If m = sqrt(a2 + b2 + c2) and v is the unit vector (a,b,c) / m then the exponential of the quaternion Q is

exp(Q) = exp(r) [ cos(m), v sin(m) ]
Polar Coordinates

The equivalent to polar coordinates in quaternion space are

theta1 is known as the amplitude of the quaternion, theta2 and theta3 are the latitude (or co-latitude) and longitude respectively. The representative point of a quaternion is the normalised vector (a,b,c), that is, where (a,b,c) intersects the unit sphere centered at the origin.

Rotation of a vector about another vector

To rotate a 3D vector "p" by angle theta about a (unit) axis "r" one forms the quaternion

Q1 = (0,px,py,pz)

and the rotation quaternion

Q2 = (cos(theta/2), rx sin(theta/2), ry sin(theta/2), rz sin(theta/2)).

The rotated vector is the last three components of the quaternion

Q3 = Q2 Q1 Q2*

It is easy to see that rotation in the opposite direction (-theta) can be achieved by reversing the order of the multiplication.

Q3 = Q2* Q1 Q2

Note also that the quaternian Q2 is of unit magnitude, and needs to be in order to be a valid rotation.

Converting a quaternion to a matrix

Given a quaternion rotation the corresponding 3x3 rotation matrix M is given by

M =
1 - 2 b2 - 2 c2 2 a b - 2 r c 2 a c + 2 r b
2 a b + 2 r c 1 - 2 a2 - 2 c2 2 b c - 2 r c
2 a c - 2 r b 2 b c + 2 r c 1 - 2 a2 - 2 b2