Quaternions

From GPWiki

Files:GUITutorial_warn.gif The Game Programming Wiki has moved! Files:GUITutorial_warn.gif

The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server.

However, the GPWiki forums are still active! Come say hello.

(This is a tutorial for the contest currently under construction -- there are currently unfixed errors!)

(Note: The author has temporarily suspended work on this tutorial. It will be finished shortly after the end of this week.)

Contents

A Journey into Quaternions

In this tutorial I hope to shed some light on the mystery shrouding these so-called quaternions which are all the buzz these days. I have rummaged around the internet in search of thorough, yet elementary, explanations of the quaternions, and have yet to find anything satisfactory. Certainly I have found many tutorials discussing quaternions, but they all suffer from one of the following fundamental flaws:

  • Superficiality – Quaternions are in actuality fairly simple concepts and this is probably why so many tutorials suffer from this problem. However, just because they are simple does not imply that they are simple to explain. Anyone who has not had formal mathematical training is going to find them thoroughly odd and curious, and so quaternions need to be explained very carefully from the fundamentals on which they are based all the way to their applications.
  • Technicalities – These tutorials are usually academic papers and thus suffer from extreme technicalities and formalisms that no one lacking a great degree of mathematical sophistication could follow without being painstakingly lost.
  • Length – Occasionally I will stumble upon a tutorial which starts out extremely well, is not very technical but explains the concepts simply from the ground up, and then swiftly cuts off short leaving you wanting more. I hope my tutorial will be akin to these, but without the abrupt cut-off!

Rather than donning the dry, academic style most math tutorials adopt, I have instead chosen to follow a lax and hopefully more interesting style. My reasoning is simple: although a formal style may be more appropriate for many advanced mathematics texts, this is not such a text; this is a tutorial meant to be accessible to readers of all levels, from simple high school algebra to undergraduate calculus.

Quaternions are heavily based on complex numbers (they are, after all, an extension of the field of complex numbers), and so I shall begin with an in-depth discussion of such. In fact, I have dedicated over half this tutorial to a thorough explanation of complex numbers, because their understanding is absolutely critical for truly understanding quaternions. If you feel you are very familiar with the complex numbers, then of course you may skip the following sections. I do, however, recommend you at least skim them, as there exists some information therein which is critical in the construction of the quaternions.

A Few Terms

There are a few terms which I will use liberally throughout this tutorial which you are likely to be unfamiliar with. There's nothing special about them, they are just rarely taught in the normal math classes most people will be taking:

  • Field - this is a mathematical set equipped with the operations of addition and multiplication, both of which adhere to all rules taught in a high school algebra class, i.e. reflexivity, existence of + and * identities, invertibility, commutativity, symmetricity, etc. As we will see, the real numbers and complex numbers both form fields, however the quaternions do not.
  • Algebra - this term refers to an algebra. For example, the quaternions we will construct later on form an algebra different from high school algebra. Despite what is taught in high school math classes, there are many algebras out there, each with different rules and structures. Formally, an algebra over a field F is called an F-algebra and is a vector space over F with a notion of multiplication. This formal definition doesn't really matter though, so no worries if you don't know what it means.

Introduction to Complex Numbers

Consider the seemingly innocent equation ax2 + bx + c = 0. At first glance, this just seems to be a normal, everyday quadratic equation, and it is. However, as normal as it may seem, there are surprisingly certain values for a, b and c which make in insoluble, i.e. there exists no real number x such that the equation is satisfied for certain values of a, b and c.

For example, (a,b,c) = (1,0,1) induces such a condition of insolubility, because this ordered-triple characterizes the equation x2 + 1 = 0, or equivalently x2 = - 1. The problem is obvious: there is no real number x such that when squared yields -1, or moreover any negative number at all! But what if we need to take the square root of negative one?

Perhaps we can simply pretend that there exists some number i, whose value we do not know, such that i2 = - 1. We can pretend that this number makes sense, even though we honestly don't understand it. With this, we can hold our breath, treat i as a variable, and continue on with our algebraic manipulations hoping that later on we might end up squaring the i thus getting plain old -1. It turns out that, by some stroke of fortune, this is precisely what happens in many situations.

We can actually take square roots of any negative number this way. For example, \sqrt{-4} = 2i because (2i)2 = (2i)(2i) = 4i2 = 4( - 1) = - 4. This is also a perfect example of how i ends up getting squared later on yielding a meaningful real number.

This number i came to be know as the imaginary unit because it only appeared intermittently during a more complicated calculation.

However, just because we have a method of overcoming these square roots of negative numbers does not mean that we understand this number i. What exactly is i? How can we understand it as well as we understand the real numbers?

The imaginary unit is actually surprisingly simple and intuitive to comprehend. Consider the real numbers and subsequently their mappings onto the real line. The real numbers correspond to distances left and right on the real a horizontal line. Real numbers are simply multiples of 1, so imaginary numbers are also by analogy multiples of i. Perhaps, then, imaginary numbers are distances up and down?

This is actually precisely what they are. In fact it was later realized that both real and imaginary numbers could be unified into the field of complex numbers, denoted C.

This unification is such that these so-called complex numbers are expressed as a + bi, where a and b are real numbers, and i = \sqrt{-1}. The reason this format works out so nicely is because all purely real numbers can be expressed as a + 0i = a and all purely imaginary numbers can be expressed as 0 + bi = bi. This means a few things to formal mathematics:

  • The complex numbers contain the real numbers as a proper subfield. This means that all real numbers can be written as complex numbers without changing anything about them.
  • The complex numbers form an algebraic closure over the real numbers. This is fancy abstract algebra jargon for saying that the complex numbers allow all polynomials formed with strictly real or complex coefficients to have solutions in the complex number field. This notion is made rigorous by the fundamental theorem of algebra.

However, as William Hamilton observed years ago, the form a + bi is not very sensible as we really can't form a precise notion of adding a purely real and purely imaginary number. Therefore, it was proposed that instead complex numbers be defined as ordered-pairs (a,b) and that strict rules be formulated for performing operations on these tuples. As we will see shortly though, there does exist an interpretation of complex numbers which allows the addition of purely real and purely imaginary numbers to become very much intuitive.

The Reality of Imaginary Numbers

Initially, mathematicians scoffed at the idea of an "imaginary number". For the most part they refused to acquaint themselves with it. Because of this, it took nearly 200 years for these complex numbers to be fully accepted by the professional mathematics community. The primary catalyst for this wide-spread acceptance was the gradual realization that imaginary numbers aren't imaginary at all! They are, in fact, no less real than is the number of pillows you sleep with. In the next few paragraphs, I hope to convince you of this point by telling you a story I have made up to help illustrate various concepts.

Travel back in time with me to ages long past, into a time in which mathematics is not even advanced beyond the natural numbers (1, 2, 3, ...). Imagine seeing a shepherd trying to manage his herd. A man comes to him and offers a great deal of money in return for seven sheep. The shepherd is then confronted with a fundamental mathematical problem: he wants the money, but does he have enough sheep so that he can afford to lose seven? How might he determine this?

He begins with counting his sheep. Suppose he counted s sheep. How many sheep, he then asked himself, would he have when he gave away seven? This shepherd happened to be a genius for his time, and solved the problem in an instant by visualizing the natural numbers. He ingeniously mapped them onto a line, and said that gaining x sheep was moving x units to the right, and that losing x sheep was moving x units to the left. So he started at s, moved seven units left and came to the remarkable conclusion that no natural number described the number of sheep that would remain! The problem was that he had started out with seven, and was subsequently going to give them all away!

He realized immediately that the number describing the sheep which would remain would correspond to the concept of "nothing" or "no quantity", but how could a number exist which specified "no quantity"?! After considerable deliberation on the matter, he came to the realization that there is no rule that says a number cannot specify "no quantity" and so he invented his own number. He called it zero and denoted it '0'. He didn't totally understand it, but nonetheless he was able to formalize rules that allowed other numbers to interact with it. He concluded that this number zero was no less real than were all these other natural numbers.

After solving this problem, he continued to think on the man's offer. He decided to accept it, because the man was offering him enough money that he could buy back a sufficient amount of sheep yet still have a considerable amount of money left over. Unfortunately, greed got to the shepherd, and he carelessly bought a new hut, the cost of which was greater than the amount the man had given him! The one who sold him the hut had agreed to give the shepherd three years to pay him back.

The shepherd, having three years to think this over, realized that he had stumbled into another curious mathematical predicament. There existed, even with his addition of zero to the naturals, no number which denoted the amount of money he currently had! He knew that he owed m silver coins to the man who sold him the hut, but he did not have these coins yet.

He solved this problem just like he did his previous dilemma: by visualizing the numbers. His genius made a second debut when he reasoned that if he had n silver coins right now, that he must move rightwards on his number line m units until he had payed back the debt. Moreover, he used his remarkable insight to predict that when he had payed back the debt on the hut, he would be left with zero silver coins. He concluded that the number of silver coins he currently had was m units left of zero!

He called these numbers left of zero "negative numbers", because they essentially mirrored the "positive numbers", as he later called them.

Now, this process went on for quite a while, until the ingenious shepherd had conceived of rational numbers (because he had to, at one point, solve the equation 2x = 5 for the sake of economic stability), irrational numbers (because he was eventually confronted with solving the equation x2 = 2), and thus all the real numbers. Of all his numerous considerations, his favorite result was his geometric interpretation of multiplication:

  • Multiplying by a number greater than 1 scales numbers upwards, or equivalently stretches them out along the real number line.
  • Multiplying by a number less than 1 scales numbers downwards, or equivalently squashes them along the real number line.
  • Multiplying by -1 flips a number over into it's negative mirror on the opposite side of the real number line. This explains why a negative times a negative yields a positive.

I have designed this story meticulously to delineate a set of specific concepts. I'm hoping that putting these concepts into story form will help illustrate some bizarre notions, such as the fact that the concept of negative numbers is extremely abstract. You can't hold -5 silver coins in your hand after all! Certainly, however, the negative numbers are real, aren't they?

I want you to extract the following realizations from this story:

  • All numbers are extreme abstractions, even the naturals. For example, the quantity 3 + 5 is always 8, no matter if you are talking about apples, computers, door-knobs, dismembered limbs, or no material objects at all.
  • The concept of zero is very different from all other numbers. All numbers describe a quantity or degree, whereas zero describes a non-quantifiable concept something akin to "nothingness" or "absence of quantity". As abstract and non-tangible as this is, the reality of the zero concept is irrefutable. Moreover, there exist strict rules which allow zero to interact seamlessly with all the other numbers.
  • Negative numbers are so much an abstraction that they do not even exist in reality! You simply can't look up to the sky and exclaim, "Look, there are negative seven birds flying over there!" without inducing some uncertainty about your sanity! Even so, I doubt that anyone would attest to the unreality of the negative numbers.
  • By working with numbers visually and mapping them onto geometric primitives we can determine fundamental properties, relationships and explanations which are utterly ambiguous or mysterious in an purely algebraic sense. As was said above, the property that a negative times a negative yields a positive is somewhat mysterious in a strict mathematical way, but when interpreted geometrically it suddenly makes wonderful sense.
  • All the aforementioned applies to imaginary and complex numbers (and even quaternions as we will see later on). Adding a negative number is geometrically equivalent to going leftwards on the real number line. Adding an imaginary number is geometrically equivalent to going upwards!

I'm hoping your conclusion after considering these things will be something along the lines of a mathematical "Eureka!" moment wherein you realize that imaginary numbers simply aren't imaginary at all. If I had my way, real numbers would be called "linear numbers" because they lie strictly on the horizontal axis, imaginary numbers would be called "lateral numbers" because they lie on lines at different latitudes than the reals, and the complex numbers would be called "plane numbers" because they can exist anywhere on the complex plane.

Further Elaborations on the Complex Numbers

Consider the final point made in the previous section: adding an imaginary number is geometrically equivalent to going upwards. Now attempt to determine the consequences of this in terms of the complex numbers. After considerable deliberation you should arrive at the conclusion that complex numbers are precise specifications of points in a 2-dimensional plane!

The explanation for this property is simple. If a complex number is of the form a + bi, then a corresponds to a point along the horizontal real line, and the addition of bi corresponds to a movement upwards or downwards (dependent on the sign of a). Thus all purely imaginary numbers map onto the so-called imaginary axis, or the vertical axis perpendicular to the horizontal real axis. Moreover, observe that any complex number therefore forms a right triangle:

Files:ComplexPlane.png

This means we can compute the "length" or more precisely absolute value or complex norm of a complex number by trivial application of the Pythagorean theorem. For z = a + bi, we have

|z| = \sqrt{a^2 + b^2}

Mathematicians have furthermore defined the complex conjugate of z = a + bi to be z * = a - bi. The utility of this definition is not immediately obvious; just be patient and you will see soon enough it's indispensability.

Also realize that i and -i are both square roots of -1, just as 5 and -5 are both square roots of 25.

Complex Number Notation

Thus far I have mentioned several notations for complex numbers, namely the expanded form a + bi and the tuple form (a,b). The former notation is more useful for deriving various formulae, while the latter is clearer. I will be using both of these throughout the remainder of this tutorial.

Operations on Complex Numbers

Just like real numbers, complex numbers have well-defined operations of addition, multiplications, their inverses, and unlike the real numbers, conjugation (shown above).

Addition and Subtraction

Addition and subtraction are easy. If we write complex numbers zn = an + bni = (an,bn) for n = 0,1, then they simply proceed component-wise as shown:

z0 + z1 = (a0,b0) + (a1,b1) = (a0 + a1,b0 + b1) = (a0 + a1) + (b0 + b1)i

and

z0 - z1 = (a0,b0) - (a1,b1) = (a0 - a1,b0 - b1) = (a0 - a1) + (b0 - b1)i.

The additive identity is (0,0) because

(a + bi) + (0 + 0i) = a + bi.

Multiplication

Multiplication is the tricky part. The difficulty arises in the need to avoid introducing too many incompatibilities with division. For example, multiplication can not proceed component-wise as do addition and subtraction because any complex number for which either the real or imaginary parts (a and b, respectively) are 0 make impossible their fulfillment of the role of divisor (because division by zero is still disallowed). This means a more elaborate multiplication scheme is needed.

The solution eventually determined by mathematicians was to let multiplication of complex numbers zn = an + bni for n = 0,1 behave exactly like polynomial multiplication, that is:

z0z1 = (a0 + b0i)(a1 + b1i) = a0a1 + a0b1i + a1b0i + b0b1i2.

Observing that i2 = - 1 and factoring gives:

(a0 + b0i)(a1 + b1i) = a0a1 + (a0b1 + a1b0)i + (b0b1)( - 1) = (a0a1 - b0b1) + (a0b1 + a1b0)i

The multiplicative identity of the complex numbers is (1,0) because

(a + bi)(1 + 0i) = 1a + 0ai + 1bi + 0bi2 = a + bi

For the remainder of this article, the multiplicative identity will simply be denoted 1 rather than (1,0).

Division

When devising schemes for performing division in alternate number systems (in this case, the complex numbers), we must consider that division is equivalent to multiplication by the multiplicative inverse. First, however, we must establish the existence of such an object.

If such a multiplicative inverse existed, then we would have, for some complex number z, zz - 1 = 1. Consider that z - 1 exists. Then we have

(zz - 1)z * = z *

or, by associativity and commutativity,

z - 1(zz * ) = z * .

Therefore,

z^{-1} = \frac{z^*}{zz^*}

However,

zz * = (a + bi)(a - bi) = a2 - abi + abi - b2i2 = a2 + b2 = | z | 2.

Thus, we conclude with a formula for the multiplicative inverse of z:

z^{-1} = \frac{z^*}{|z|^2}

We can now determine an equation giving the quotient of two complex numbers zn = an + bni for n = 0,1:

\frac{z_0}{z_1} = z_0(\frac{z_1^*}{|z_1|^2}) = \frac{z_0z_1^*}{|z_1|^2} = \frac{(a_0 + b_0i)(a_1 - b_1i)}{a_1^2 + b_1^2} = \frac{a_0a_1 - a_0b_1i + a_1b_0i - b_0b_1i^2}{a_1^2 + b_1^2} = \frac{a_0a_1 + (a_1b_0 - a_0b_1)i + b_0b_1}{a_1^2 + b_1^2} = \frac{(a_0a_1 + b_0b_1) + (a_1b_0 - a_0b_1)i}{a_1^2 + b_1^2}.

In standard complex number form, this becomes:

\frac{a_0+b_0i}{a_1+b_1i} = \frac{a_0a_1 + b_0b_1}{a_1^2 + b_1^2} + \frac{a_1b_0 - a_0b_1}{a_1^2 + b_1^2}i.

Re-expressing Complex Numbers with Polar Coordinates

This section requires that you have understanding of polar coordinates. Because I do not want to clutter this tutorial with discussion of such, I refer you to this Wikipedia article. The coordinate system we are interested in is circular polar coordinates.

Given a complex number z = (a,b) expressed in usual form, we can re-express this by exploiting polar coordinates:

z = (a,b) = (rcosθ,rsinθ)

where r = | z | is the complex norm or absolute value of z and \theta = \arctan{\frac{b}{a}} is the complex argument, or angle between the segment formed by z and the real axis.

You may easily verify this formula with elementary trigonometry and reference to the diagram of the complex plane above. It is important to know this conversion formula as complex numbers are written like this very often in math articles and texts. It is also the basis of what I'm going to show you next.

The Euler Formula

The Euler formula relates the exponential function to complex numbers and trigonometry. If you do not remember, or were never aware, the exponential function is defined as the inverse of the natural logarithm lnx:

expx = ex

The Euler formula is

eix = cosx + isinx

where i is the imaginary unit discussed far above.

This means, by the polar coordinate formulation above, we can interpret x as the complex argument and use the Euler formula to find a elegant expression of all complex numbers relating the exponential function, integrals, complex numbers and trigonometry:

z = | z | (cosθ + isinθ) = | z | eiθ.

Using the Euler formula is not by any means necessary for what follows, however it does clean up the equations a bit and so I prefer it over raw polar coordinates. It also allows us to bypass some trigonometry.

I have provided a simple proof of the correctness of the Euler formula as Appendix I to this tutorial.

The Hidden Power of Complex Numbers

Here we will finally see some utility produced by these complex numbers.

Suppose we wish to multiply two complex numbers z_0 = |z_0|e^{i\theta_0} and z_1 = |z_1|e^{i\theta_1}:

z_0z_1 = (|z_0|e^{i\theta_0})(|z_1|e^{i\theta_1}) = |z_0||z_1|e^{i\theta_0 + i\theta_1} = |z_0||z_1|e^{i(\theta_0 + \theta_1)}.

What is the result? The complex norm has been scaled by | z1 | and the angle increased by θ1, meaning complex multiplication induces simultaneous scaling and rotation!

We can already exploit complex numbers to perform rotations in 2D by using this algorithm:

  • Start with a vector \vec v = (x,y) which we wish to rotate counter-clockwise by an angle θr.
  • Determine the angle of \vec v against the x-axis \theta_{\vec v} = \arctan{\frac{\vec v_y}{\vec v_x}}.
  • The final vector then is \vec f = (\Re{(|\vec v|e^{i(\theta_{\vec v} + \theta_r)})},\Im{(|\vec v|e^{i(\theta_{\vec v} + \theta_r)})}), where \Re{(z)} and \Im{(z)} are functions giving respectively the real and imaginary parts of a complex number z.

Of course, with 2D we could use the much simpler algorithm of converting to polar coordinates, adding the angle by which to rotate, and converting back thereby avoiding complex arithmetic entirely (this entire algorithm can easily be put into a single equation, in fact).

The Quaternions

I believe we have sufficiently covered complex numbers to move onto quaternions. If you are interested into delving further into complex math, you might find the field of mathematics called complex analysis interesting. It is mainly consisted of the study of complex functions (that is, maps from a subset of R to the complex plane C) and their derivatives.

A word before moving onwards: quaternions are very weird. They are likely to be completely unlike any mathematical objects you've ever studied before. To get you pumped to go on reading, and so you can see what I mean by weird, consider that if a and b are quaternions then ab = - ba.

The Basic Premise

Although quaternions were originally conceived of as simply curious mathematical objects by William Hamilton in the 19th century, they have extensive applications in modern computer graphics. We have already seen how complex numbers allow us to perform rotations in 2-dimensional space. The basic idea is to extend the complex numbers so that they allow us to perform rotations in 3-dimensional space. This extension is known as the set of quaternions, or quaternionic numbers.

An Informal Construction

There exist a variety of methods for constructing the curious algebra which is the quaternions. One of the most popular is the Cayley-Dickson construction. Actually, the Cayley-Dickson construction produces a sequence of algebras over the field of real numbers, each algebra having twice the dimension of it's predecessor. However, this formal construction is not what interests us because it does not provide much in the way of intuitive understanding.

Rather, we will construct the quaternions informally by using just our human intuition. This is very simple to do.

Consider our goal: to extend complex numbers to allow rotations in 3D in the same manner in which 2D rotations are done with complex numbers (i.e., multiplication induces rotation). Suppose I told you to rotate the point (5,13,9) by 45o. Could you comply with my demand?

You could not, for this simple reason: I did not specify the axis about which to rotate! 3D complicates rotation because we now have an infinity of axes about which to rotate, whereas in 2D you have the safe assumption that rotation is about the "invisible" axis shooting through the paper precisely at the origin. Hence, to specify a rotation in 3D we need this information encoded somehow in our quaternion:

  • The angle by which to rotate (a single scalar).
  • The axis about which to rotate (a 3-vector).
  • A scaling factor. This is the norm of the rotation axis vector and thus it is implicit in the quaternion so it takes up no further space.

Thus, in total we need four scalars to properly represent rotations in 3D. This question now poses itself: how do we represent this information?

We simply do to the complex numbers to add information what we did to the real numbers to add information: we add in some square roots of -1. In four-dimensional space, we will have the real axis, the i axis, the j axis, and the k axis, where i, j, and k are all distinct square roots of -1.

Thus quaternions may be written q = a + bi + cj + dk. You may interpret i, j, and k as basis vectors if you wish to help with understanding; however, as will be shown next, they technically are not vectors.

You now likely find yourself thinking back to my curious statement above that if a and b are quaternions then we have ab = - ba. Let us justify this property of anticommutativity.

We have already stated that we desire for quaternion multiplication to somehow induce rotations, whether it be with single or multiple multiplications, and whether order does or does not matter.

However, it can easily be verified that rotation is non-commutative, that is, performing rotation A and then rotation B is not always the same as doing B and then A. For example, rotating 90 degrees about the x-axis and then 45 degrees about the y-axis is not the same as rotating 45 degrees about the y-axis and then 90 about the x-axis.

Thus, we have that quaternion multiplication is necessarily non-commutative. For formal mathematics, this means quaternions form a division ring, that is a non-commutative field or skew field. We shall show rigorously why anticommutativity in specific prevails with quaternions rather than the generalized noncommutativity shortly.

Before proceeding however, let me point out that your entire life you have been using an anticommutative mathematical operation: subtraction. This is so because we have a - b = - (b - a), where a and b are real numbers.

Representations

We have already shown that quaternions may be written in the form q = a + bi + cj + dk. However, just as with complex numbers (and for similar reasons), quaternions may also be written as 2-tuples q = (w, \vec v), where w is the real part and \vec v is the vector part or imaginary part. This representation is often much easier to work with, as we will see.

The Fundamental Formula

It is said that as William Rowan Hamilton was walking with his wife along the Royal Canal in Dublin on October 16, 1843, the following fundamental formula elegantly describing the properties of quaternions occurred to him, whereupon he carved it onto the side of the bridge:

i2 = j2 = k2 = ijk = - 1.

This equation is both mysterious and powerful; it defines the entire multiplication table of quaternions.

The key concept to keep in your mind when considering this equation is the anticommutativity of quaternions shown above: for quaternions a and b, ab = - ba. Do not forget this when reading what follows.

Since ijk = - 1, right-multiplying by k gives

ijkk = - k,

ij( - 1) = - k,

ij = k.

Alternatively, left-multiplying by i gives

iijk = - i,

( - 1)jk = - i,

jk = i.

Multiplying this last equation by j from the left yields

jjk = ji,

- k = ji.

Continuing on in this fashion allows the entire multiplication table to be derived.

Operations

Quaternion norm follows from the Pythagorean theorem and complex numbers:

|q| = \sqrt{a^2 + b^2 + c^2 + d^2}.

Addition and subtraction are easily defined and simply proceed component-wise, just as with complex numbers and vectors.

Multiplication, as always, is the difficult part. However, we may simply follow in the foot-steps of complex numbers and define multiplication as behaving algebraically just like polynomial multiplication:

ab = (a1b1 - a2b2 - a3b3 - a4b4) + (a1b2 + a2b1 + a3b4 - a4b3)i + (a1b3 - a2b4 + a3b1 + a4b2)j + (a1b4 + a2b3 - a3b2 + a4b1)k

where qi is the ith component of quaternion q.

We may derive a multiplicative inverse for the quaternions similarly to how we did such with the complex numbers. First, however, let us define the quaternionic conjugate:

q * = a - bi - cj - dk.

We then have that, if the multiplicative inverse is q - 1, qq - 1 = 1. Therefore, (qq - 1)q * = q * . We know that qq - 1 = 1 by definition, hence commutativity applies in this situation and we may proceed as with the complex numbers:

(q - 1q)q * = q * ,

q - 1(qq * ) = q * , (by associativity, which we have said nothing about being invalid with quaternions)

q^{-1} = \frac{q^*}{qq^*}, where qq * = a2 + b2 + c2 + d2 = | q | 2. Hence, q^{-1} = \frac{q^*}{|q|^2}, just as with complex numbers.

However, if q is of unit length (i.e., | q | = 1) then q - 1 = q * .

3D Rotations with Quaternions

First I should note this: I have subtly mislead you thus far concerning rotations with quaternion multiplication. I have intentionally made it sound simpler and more straight-forward than it really is with the purpose of letting you instead focus on other things. Now, however, I can hide the facts no more; utilizing quaternion multiplication for rotation is unfortunately slightly less trivial than you were probably expecting, as the following theorem shows:

Theorem. Let p be a point in three-dimensional space, represented as a 3-vector, and let q = (\cos{\frac{\theta}{2}}, \vec n\sin{\frac{\theta}{2}}) be a quaternion. Then qpq - 1 = p' rotates p to p', such that the rotation was about the axis specified by the unit 3-vector \vec n and by an angle θ.

Proof. First we re-express p using a mapping p(x,y,z) - > p'(0,x,y,z). Thus, p as a quaternion is p_q = (0, \vec p).

Appendix I: Proof of the Euler Formula

This proof is provided in the case of a mathematically inclined and curious reader. The proof is surprisingly elementary and requires nothing more than high school calculus to understand.

Let z = cosx + isinx be a complex number. Then

\frac{dz}{dx} = -\sin{x} + i\cos{x}.

Rearranging, and exploiting the fact that i2 = - 1, we have:

\frac{dz}{dx} = i^2\sin{x} + i\cos{x} = i(i\sin{x} + \cos{x}) = iz.

Separating variables and integrating yields:

\int\frac{1}{z}dz = \int i \,dx,

lnz = ix + C

where C is the constant of integration.

Next, we prove that C = 0. Observe that substitution of x = 0 gives:

lnz = C.

However,

z = cosx + isinx = cos0 + isin0 = 1,

Thus,

ln1 = C,

C = 0.

Taking the exponential gives the final part of the proof.

lnz = ix,

elnz = eix,

z = eix,

eix = cosx + isinx

which completes the proof.