Quaternions
From GPWiki
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.)
[edit] A Journey into QuaternionsIn 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:
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. [edit] A Few TermsThere 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:
[edit] Introduction to Complex NumbersConsider 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, 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
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. [edit] The Reality of Imaginary NumbersInitially, 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:
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:
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. [edit] Further Elaborations on the Complex NumbersConsider 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: 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
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. [edit] Complex Number NotationThus 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. [edit] Operations on Complex NumbersJust like real numbers, complex numbers have well-defined operations of addition, multiplications, their inverses, and unlike the real numbers, conjugation (shown above). [edit] Addition and SubtractionAddition 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. [edit] MultiplicationMultiplication 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). [edit] DivisionWhen 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,
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:
We can now determine an equation giving the quotient of two complex numbers zn = an + bni for n = 0,1:
In standard complex number form, this becomes:
[edit] Re-expressing Complex Numbers with Polar CoordinatesThis 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 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. [edit] The Euler FormulaThe 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. [edit] The Hidden Power of Complex NumbersHere we will finally see some utility produced by these complex numbers. Suppose we wish to multiply two complex numbers
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:
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). [edit] The QuaternionsI 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. [edit] The Basic PremiseAlthough 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. [edit] An Informal ConstructionThere 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:
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. [edit] RepresentationsWe 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 [edit] The Fundamental FormulaIt 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. [edit] OperationsQuaternion norm follows from the Pythagorean theorem and complex numbers:
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)
However, if q is of unit length (i.e., | q | = 1) then q - 1 = q * . [edit] 3D Rotations with QuaternionsFirst 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 Proof. First we re-express p using a mapping p(x,y,z) - > p'(0,x,y,z). Thus, p as a quaternion is [edit] Appendix I: Proof of the Euler FormulaThis 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
Rearranging, and exploiting the fact that i2 = - 1, we have:
Separating variables and integrating yields:
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. |
because
. The reason this format works out so nicely is because all purely real numbers can be expressed as 
.
.
is the complex argument, or angle between the segment formed by z and the real axis.
and
:
.
which we wish to rotate counter-clockwise by an angle
against the x-axis
.
, where
and
are functions giving respectively the real and imaginary parts of a complex number z.
, where
.
, where
, just as with complex numbers.
be a quaternion. Then
and by an angle
.
.
.
,


