Although I have a pretty solid background in math (especially vectors, matrices, and even tensors), I’ve always somewhat struggled with *quaternions*. Most sources focus on quaternions as some tool for performing rotations in three-dimensions while avoiding gimbal lock. Which is true, they *are* that, but they’re also more. After reading several articles about quaternions over the past several days, quaternions finally clicked and made sense! I’ll try to share that insight with here here, though be warned that my description may be just as confusing (if not more so) than anywhere else.

In short, once I really understood that quaternions are simply four-dimensional complex numbers, understanding their creation and use became a lot simpler. Quaternions are basically just four-dimensional vectors, who’s orthonormal basis lies in some weird four-dimensional existence. That sounds like a mouthful, and to be honest, it kind of is. Let’s take a step back and look at complex numbers. Actually, before that, let’s look at orthonormal bases.

## Orthonormal Bases

If you don’t know what an orthonormal basis is, that’s probably just because you don’t know their name. To quote wikipedia:

In mathematics, particularly linear algebra, an orthonormal basis for an inner product space V with finite dimension is a basis for V whose vectors are orthonormal, that is, they are all unit vectors and orthogonal to each other.

That is to say, an orthonormal basis is a set of vectors which are **all** perpendicular to each other. You almost assuredly know one such basis: the `<x, y, z>`

coordinate system (also called the “Cartesian coordinate system”). Essentially each component of the basis represents a different dimension. There are many other orthonormal bases, for example: 2D Cartesian coordinates, polar coordinates (2D), cylindrical coordinates (3D), and spherical coordinates (3D) to name a few.

As it turns out, complex numbers *also* form an orthonormal basis. However, instead of representing physical dimensions, complex numbers represent a complex plane composed of *real* and *imaginary* components representing real and imaginary dimensions.

## Complex Numbers

Complex numbers are just two-dimensional vectors which are composed of both *real* and *imaginary* dimensions. In the 2D Cartesian coordinate system, vectors are composed of the `x`

and `y`

dimensions. In the complex plane, the imaginary dimension is given the label `i`

, where:

Which is an important identity to know, however we don’t really need to use it often. Where in the Cartesian plane you might write a vector as such:

In the complex plane, you might write a vector as:

Where `a`

represents the *real* part of the vector and `b`

represents the *imaginary* part.

### Rotating with Complex Numbers

When rotating a vector in Cartesian coordinates, you can represent the rotation as a combination of `cos`

and `sin`

transforms in the two dimensions:

Similarly, rotations in the complex plane can be represented as the combinations of `cos`

and `sin`

transforms in the two complex dimensions:

Because of some neat math with complex numbers (including `i² = -1`

formula above) which I won’t repeat here, this can be reduced to:

## Quaternions as Four-Dimensional Complex Numbers

Now that we have an understanding of complex numbers in two dimensions, it’s pretty straightforward to extend the concept into the four dimensions necessary for quaternions—essentially all we do is define `j²`

and `k²`

dimensions to cast the vector into, defining the directions according to Hamilton’s formula:

A quaternion can then be written as:

Or, more commonly:

Where `w`

corresponds to the *real* dimension and `x`

, `y`

, and `z`

correspond to the three *imaginary* dimensions.

And *that’s it*. That’s all quaternions really are.

Of course, quaternions are useful for all sorts of things, owing to some more neat math.

### Real and Pure Quaternions

If a quaternion’s imaginary components are all equal to zero, then the quaternion is said to be “real”:

Alternatively, if a quaternion’s real component is equal to zero, then the quaternion is said to be “pure”:

Note that any quaternion can be expressed as the sum of its “real” and “pure” parts:

### Rotations Using Quaternions

Since quaternions are composed of a single *real* component and three orthogonal *imaginary* components, they can be written similarly to vectors in our 2D complex plane:

Look familiar?

Using the same multiplication formula as before, we get:

or:

By multiplying a vector by a quaternion (noting that to satisfy the math, we must use a four-dimensional vector, which we can set to be our three-dimensional vector with the fourth element set to 0), we get another quaternion:

Now, if the quaternion represents a rotation as defined above, the result should represent a rotated version of the vector `p`

. Note that we essentially converted `p`

to a “pure” quaternion, so we would expect `p'`

to be a pure quaternion as well, from which we could extract the rotated vector. Somewhat unfortunately, this isn’t the case for all but a few very specific circumstances. Most of the time, the result will be a mixed quaternion (meaning it will have *both* real and pure components), and the pure portion of it will not represent the origin vector (it will be longer). Fortunately, this can easily by solved by following the multiplication up by another multiplication—this time, by the inverse of `q`

:

By adding this multiplication in, the resulting `p'`

quaternion will be **pure** quaternion, with the complex parts representing the vector `p`

rotated by the quaternion `q`

. There’s a catch however: since you effectively multiplied the vector twice (once by `q`

and once by the inverse of `q`

), the resulting vector gets rotated by `2θ`

, meaning to rotate the vector only by `θ`

, you need to construct `q`

as if it was rotated by `0.5 θ`

.

#### Constructing a Quaternion as a Rotation

Remembering the formula for `R(θ)`

from before, we can construct a rotation quaternion as such:

However, this suffers from the `2θ`

issue mentioned above, so we actually want to construct it as so:

The resulting quaterion `q`

can now be used to rotate a vector in three dimensions! Not so shabby, eh?

To actually implement the rotation however, you’ll need a couple more formulas—namely how to multiply quaternions, and how to calculate the inverse of a quaternion.

##### Multiplying Quaternions

The derivation of multiplying quaternions is fairly straightforward, if somewhat tedious. To save on tedium, I’ll just give you the result here:

##### Calculating the Inverse of a Quaternion

The inverse of a quaternion is given via the following formula:

Where `q*`

represents the conjugate of the quaternion, and is calculated as such:

## Conclusions

- Quaternions are just vectors (in four dimensions)
- The four dimensions are:
*real*`i`

`j`

`k`

- 3D vectors can be written as a quaternion, where the
`x-y-z`

components of the vector map to the`i-j-k`

components of the quaternion - Quaternions can be used to rotate other quaternions using a couple of simple formula

For full in-depth discussion about quaternions, as well as to check out the sources I used for this, please check these places out: