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

- i
^{2}= j^{2}= k^{2}= -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

- i

Given two Quaternions q_{1} and q_{2}:

q_{1} = [s_{1}, v_{1}] = [s_{1} + x_{1}i+y_{1}j + z_{1}k]

q_{2} = [s_{2}, v_{2}] = [s_{2} + x_{2}i+y_{2}j + z_{2}k]

The product of q_{1}q_{2} is:

q_{1}q_{2} = [(s_{1}s_{2} - x_{1}x_{2} - y_{1}y_{2} - z_{1}z_{2}) + (s_{1}x_{2} + s_{2}x_{1} + y_{1}z_{2} - y_{2}z_{1})i + (s_{1}y_{2} + s_{2}y_{1} + z_{1}x_{2} - z_{2}x_{1})j + (s_{1}z_{2} + s_{2}z_{1} + x_{1}y_{2} - x_{2}y_{1})k]

Fortunately this can be written using dot and cross products

q_{1}q_{2} = [(s_{1}s_{2} - s_{1}.v_{2}), s_{1}v_{2} + s_{2}v_{1} + (v_{1} x v_{2})]

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(s^{2} + x^{2} + y^{2} + z^{2})

Also, |q|^{2} = (s^{2} + v ยท v)