### Quaternion Math

• At first there were numbers, they were used to count objects like stones or apples or whatever (~ 9000 b.c.)
• Then people thought, that there must be some “nothing”, so clever people invented the most important number, called “zero”
• After that people played around and asked whether there could be numbers smaller than 0, and they invented negative numbers.
• Then the communists came and wanted to divide everything, and that’s how rational numbers were invented (a/b, a and b numbers.)
• Then some mathematics asked for the solution of x*x=2. He did not find a number, he found only an approximation (1.41…), so the clever guy just wrote sqrt(2), and voila! The irrational numbers were invented.
• Ok, so far so good. All numbers were invented. All numbers? No! We forgot the mathematicians! They searched and searched, and suddenly somebody asked why there should not be a solution to x*x=-1. They told the number to be written as i, and so the complex numbers were invented. Unfortunately the good old number line was dense (i.e. full), there was no place for these strange “complex” numbers (just as the name implies ). So they placed them below and above the real numbers: And even nowadays they form the complex number plane, each complex number consistring of a real part and an imaginary part.
• And then, as if this were not enough, in 1843 Sir William R. Hamilton was trying to find a way to rotate a 3D vector by multiplying it with another one. After 15 years of hard work, he stumbled upon the idea of using a 4D notation. He called these numbers quaternions.

A Quaternion q is a quadruple of real numbers and can be defined as q=[s,v] where s is a scalar and v is a 3D vector.

One can express the vector v in terms of its components:

q = [s + xi + vj + zk], where s is a real number called the scalar part.
The three imaginary parts are xi, yj, zk. The coefficients x, y, z in the imaginary terms are the real numbers.

Now how are the rules for mathematical operations with quaternion numbers?

• Two Quaternions are equal if and only if their corresponding terms are equal.
• The following rules apply when multiplying Quaternions:
• i2 = j2 = k2 = -1
• ij = k = -ji    [i.e. Quaternion multiplication is not commutative!]

it therefore follows that

• jk = i = -kj, ki = -ik = j,
• ji = -ij = -k, kj = -1, ik = -j

Given two Quaternions q1 and q2:

q1 = [s1, v1] = [s1 + x1i+y1j + z1k]

q2 = [s2, v2] = [s2 + x2i+y2j + z2k]

The product of q1q2 is:

q1q2 = [(s1s2 - x1x2 - y1y2 - z1z2) + (s1x2 + s2x1 + y1z2 - y2z1)i + (s1y2 + s2y1 + z1x2 - z2x1)j + (s1z2 + s2z1 + x1y2 - x2y1)k]

Fortunately this can be written using dot and cross products

q1q2 = [(s1s2 - s1.v2),   s1v2 + s2v1 + (v1   x   v2)]

For any Quaternion q = [s + xi + yj + zk] = (s,v):

The inverse Quaternion is:

q = [s - xi - yj - zk] / |q|2 = (s,-v) / |q|2

Where |q| is the magnitude or modulus of q given by:

|q| = sqrt(s2 + x2 + y2 + z2)

Also, |q|2 = (s2 + v ยท v)