Kenton Hamaluik

Quaternions as Four-Dimensional Complex Numbers

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 you 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\langle x, y, z \rangle 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 xx and yy dimensions. In the complex plane, the imaginary dimension is given the label ii, where:

i^2=1\hat{i}^2 = -1

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:

v=ax^+by^\vec{v} = a \hat{x} + b \hat{y}

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

x=a+bi^\vec{x} = a + b \hat{i}

Where aa represents the real part of the vector and bb 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\cos and sin\sin transforms in the two dimensions:

R(θ)=cos(θ)x^+sin(θ)y^R(\theta) = \cos(\theta)\hat{x} + \sin(\theta)\hat{y}

Similarly, rotations in the complex plane can be represented as the combinations of cos\cos and sin\sin transforms in the two complex dimensions:

R(θ)=cos(θ)+i^sin(θ)R(\theta) = \cos\left(\theta\right) + \hat{i}\sin\left(\theta\right)

Because of some neat math with complex numbers (including i2=1i^2 = -1 formula above) which I won’t repeat here, this can be reduced to:

R(θ)=ei^θR(\theta) = e^{\hat{i} \theta}

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 j2j2 and k2k2 dimensions to cast the vector into, defining the directions according to Hamilton’s formula:

i^2=j^2=k^2=i^j^k^=1\hat{i}^2 = \hat{j}^2 = \hat{k}^2 = \hat{i}\hat{j}\hat{k} = -1

A quaternion can then be written as:

q=w+xi^2+yj^2+zk^2\vec{q} = w + x\hat{i}^2 + y\hat{j}^2 + z\hat{k}^2

Or, more commonly:

q=<w,x,y,z>\vec{q} = \left<w, x, y, z\right>

Where ww corresponds to the real dimension and xx, yy, and zz 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”:

q_real=w\vec{q}\_{real} = w

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

q_pure=xi^+yj^+zk^\vec{q}\_{pure} = x\hat{i} + y\hat{j} + z\hat{k}

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

q=q_real+q_pure=(w)+(xi^+yj^+zk^)\begin{aligned} \vec{q} &= \vec{q}\_{real} + \vec{q}\_{pure} \\ &= \left(w\right) + \left(x\hat{i} + y\hat{j} + z\hat{k}\right) \end{aligned}

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:

q=w+<x,y,z><i^,j^,k^>=w+ui\begin{aligned} \vec{q} &= w + \left<x, y, z\right> \cdot \left<\hat{i}, \hat{j}, \hat{k}\right> \\ &= w + \vec{u} \cdot \vec{i} \end{aligned}

Look familiar?

Using the same multiplication formula as before, we get:

R(θ)=e(ui)θR(\theta) = e^{(\vec{u} \cdot \vec{i})\theta}

or:

R(θ)=cos(θ)+(ui)sin(θ)R(\theta) = \cos(\theta) + (\vec{u} \cdot \vec{i})\sin(\theta)

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:

p=qp=<w,u><0,p>=<up,wp+u×p>\begin{aligned} \vec{p}' &= q\vec{p} \\ &= \left<w, \vec{u}\right>\left<0, \vec{p}\right> \\ &= \left<-\vec{u}\cdot\vec{p}, w\vec{p} + \vec{u}\times\vec{p}\right> \end{aligned}

Now, if the quaternion represents a rotation as defined above, the result should represent a rotated version of the vector pp. Note that we essentially converted pp to a “pure” quaternion, so we would expect pp' 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 qq:

p=qpq1\vec{p}' = q\vec{p}q^{-1}

By adding this multiplication in, the resulting pp' quaternion will be pure quaternion, with the complex parts representing the vector pp rotated by the quaternion qq. There’s a catch however: since you effectively multiplied the vector twice (once by qq and once by the inverse of qq), the resulting vector gets rotated by , meaning to rotate the vector only by θ, you need to construct qq 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:

q(θ)=cos(θ)+sin(θ)i^+sin(θ)j^+sin(θ)k^q(\theta) = \cos(\theta) + \sin(\theta)\hat{i} + \sin(\theta)\hat{j} + \sin(\theta)\hat{k}

However, this suffers from the issue mentioned above, so we actually want to construct it as so:

q(θ)=cos(θ2)+sin(θ2)i^+sin(θ2)j^+sin(θ2)k^q\left(\theta\right) = \cos\left(\frac{\theta}{2}\right) + \sin\left(\frac{\theta}{2}\right)\hat{i} + \sin\left(\frac{\theta}{2}\right)\hat{j} + \sin\left(\frac{\theta}{2}\right)\hat{k}

The resulting quaterion qq 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:

q_1q_2=<q_1,wq_2,wq_1q_2,q_1,wq_2+q_2,wq_1+q_1×q_2>\vec{q}\_1 \vec{q}\_2 = \left<q\_{1,w}q\_{2,w} - \vec{q\_1}\cdot\vec{q\_2}, q\_{1,w}\vec{q}\_2 + q\_{2,w}\vec{q}\_1 + \vec{q}\_1 \times \vec{q\_2}\right>
Calculating the Inverse of a Quaternion

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

q1=qq2q^{-1} = \frac{q^*}{\left|q\right|^2}

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

q=q_wq_xi^q_yj^q_zk^=<q_w,ui^>\begin{aligned} q^* &= q\_w - q\_x\hat{i} - q\_y\hat{j} - q\_z\hat{k} \\ &= \left<q\_w, -\vec{u}\hat{i}\right> \end{aligned}

Conclusions

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