Quaternions for Rotating, Scaling, and Translating Space June 11th, 2009
Patrick Stein

In earlier posts, I described how complex numbers can be used to rotate, scale, and translate the plane, how Clifford algebras can be used to rotate, scale, and translate the plane, and why I resorted to an awkward trick for the Clifford algebra rotations of the plane. In this post, I am going to explain what the quaternions are and describe how they can be used to represent a rotation in three-dimensional space.

What are the quaternions

Okay, remember how we got the complex numbers? We needed something that was the square root of negative one.

Now, imagine that you are Sir William Rowan Hamilton. The year is 1843. It is springtime. You know how to use the complex numbers to represent points in the plane. And, you know that when you do that, you can use complex numbers to rotate, scale, and translate the points. That’s all well and good, but you don’t live in a two-dimensional world. How are you going to do the same sort of thing with three-dimensional space? How are you going to multiply triples?

You spend months on this. If only you could say, How about I let there be another number that is different from i (and from -i) that has the same property that its square is negative one? You fight with this for months. You try to represent a point with coordinates (x,y,z) as x + yi + zj. But, nothing you come up with makes any sense.

Your kids are harassing you, Daddy, did you figure out how to multiply triples yet? You have to answer them every morning with a polite, No, not yet.

Then, you’re walking along the Royal Canal in Dublin. It’s mid-October already. My, how the year has flown by. Bam, it hits you. If you add a third number like i and j which is equal to i\cdot j, everything works out. You get so excited, that you carve your equations into a stone bridge over the canal:

i^2 = j^2 = k^2 = ijk = -1

Now, instead of trying to represent (x,y,z) as x + yi + zj, it is represented as xi + yj + zk. Immediately, this looks like an improvement. Before, things were not symmetric. Squaring something that was only in the x-direction was fundamentally different than squaring something that was only in the y- or z-direction because x^2 = x^2, but (yj)^2 = y^2j^2 = -y^2. Now, at least, there is no singled-out axis.

Let’s just explore the ijk = -1 part for a minute. If we multiply both sides (on the right) by -k, we see that: -ijk^2 = ij = k. If, instead, we multiply by -i on the left, we see that -i^2jk = jk = i. If we take that jk = i and multiply on the right by -k, we get -jk^2 = j = -ik. We’re going to show in a minute that -ik = ki. So, if take i, j, and k in that order and take two in a row (wrapping around if needed), the product is the next one: ij = k, jk = i, and ki =j.

So, how do we show that -ik = ki? Well, it’s pretty simple to show that kj = -i. We already know that ijk = -1. So, kj = -(ijk)(kj) = -ijk^2j. We know that k^2 = -1, so -ijk^2j = ij^2. And, we know that j^2 = -1, so ij^2 = -i. And, since we already know that jk = i, we have kj = -i = -jk. We can use this, then to manipulate -ik. -ik = (-jk)k = (kj)k = k(jk) = ki. We can do a similar thing to show that -ij = ji.

This should look familiar from the the Clifford algebras. In the Clifford algebras, e_1e_2 = -e_2e_1. Here, ij = -ji. It turns out that this is extra important in three-dimensions compared to two.

dice

In two dimensions, if you rotate by an angle \theta and then by and angle \phi, it is the same thing as if you rotate by the angle \phi and then by the angle \theta. In three-dimensions, you have more options. In addition to picking the angle, you can also pick the axis of rotation. (Actually, for later on, it may be helpful to think of picking the plane parallel to which the rotation will take place rather than thinking of it as an axis.) In the picture at the right, the die in the back is the original position of a die. The die on the left is the result of rotating the original die a quarter turn on its left face (orange arrow) and then a quarter turn on its right face (blue arrow). The die on the right is the result of rotating the original die around its right face (blue arrow) and then its left face (orange arrow). As you can see, the resulting positions are different.

[Ugh… I just realized the free model of a die that I downloaded is not an actual, legal die. If it were, the opposite faces would sum to seven. Clearly, however, the two and the five are on adjacent faces of this die, as are the three and four, and thus also the one and six. Erf. So, you can’t repeat this experiment on your desktop unless you fabricate your own die. Sorry.]

Like the Clifford algebras and the complex numbers, addition and subtraction are simply adding and subtracting (respectively, of course) the corresponding parts.

(a + bi + cj + dk) + (w + xi + yj + zk) = (a+w) + (b + x)i + (c + y)j + (d + z)k

Armed with the above identities, we have enough information to multiply together quaternions. Rather than do the full general case of (a + bi + cj + dk)\cdot(w + xi + yj + zk), I’m going to do a simpler case to give you the flavor without wasting a ton of space in this article beating a horse that needn’t be beaten that much. Let’s just try: (1 + 3i)\cdot(2i + 5j - k). We start with the distributive law, twice, and then combine like terms.

(1 + 3i) \cdot (2i + 5j - k)

(2i + 5j - k) + 3i \cdot (2i + 5j - k)

(2i + 5j - k + 6i^2 + 15ij - 3ik

2i + 5j - k - 6 + 15k + 3ki

2i + 5j - k - 6 + 15k + 3j

-6 + 2i + 8j + 14k

There is one other notation we’re going to need below. In the complex numbers, we used the notation \overline{z} to mean the number z with the sign of its imaginary part reversed. So, if z = x + yi, then \overline{z} = x - yi. With the quaternions, we are going to use \overline{q} to mean the number q with the sing of all of its imaginary parts reversed. So, if q = a + bi + cj + dk, then \overline{q} = a - bi - cj - dk.

Transforming Space

Say you have some list of points in three-dimensional space: (x_1,y_1,z_1), (x_2,y_2,z_2), \ldots, (x_n,y_n,z_n). We are going to represent those as pure quaternions: q_1 = x_1 i + y_1 j + z_1 k, q_2 = x_2 i + y_2 j + z_2 k, \ldots, q_n = x_ni + y_nj + z_nk.

If we want to translate them all by two units along the x-axis, three units along the y-axis and five units along the z-axis, we can set t = 2i + 3j + 5k and then add t to each of our points: q_\alpha^\prime = q_\alpha + t.

If we want to scale three-space out from the origin by a factor of seven, we can set s = 7 and multiply each point by s: q_\alpha^\prime = s \cdot q_\alpha^\prime.

Let’s say now, that we want to rotate our points parallel to some plane through the origin by an angle \theta. The first thing we need to find is some way to describe that plane. In three-dimensions, we can simply use the surface normal of the plane. It may make more sense to you to think of this as the axis of rotation. In two- and three-dimensions, that will be fine. In higher numbers of dimensions, this will just confuse things. Let’s say our normal starts at the origin and goes through the point (a,b,c). To make things nice, let’s also assume it is of unit length: a^2 + b^2 + c^2 = 1. We can represent our rotation as r = \cos\frac{\theta}{2} + \sin\frac{\theta}{2}\left( ai + bj + ck \right). Then, we can rotate our points by multiplying on the left by r and on the right by \overline{r}: q_\alpha^\prime = rq_\alpha \overline{r}.

By way of an example, let us suppose we are going to rotate the point xi + yj + zk by an angle \theta, parallel to the xy-plane. We will expect that our z-coordinate remains the same, but that the other two coordinates change. First, let us let \alpha = \cos\frac{\theta}{2} and \beta = \sin\frac{\theta}{2}. The normal to the xy-plane is just the z-axis. So, our r is going to be \alpha + \beta k. This leaves us to multiply out (and it’s going to get messy):

(\alpha + \beta k)(xi + yj + zk)(\alpha - \beta k)

(\alpha + \beta k)(\alpha xi + \alpha yj + \alpha zk - \beta x ik - \beta y jk - \beta z k^2)

(\alpha + \beta k)\left( \beta z + (\alpha x - \beta y)i + (\alpha y + \beta x)j + \alpha zk \right)

\alpha\beta z + \alpha(\alpha x - \beta y)i + \alpha(\alpha y + \beta x)j + \alpha^2 z k \\ \qquad + \beta^2 z k + \beta(\alpha x - \beta y)ki + \beta(\alpha y + \beta x)kj + \alpha\beta z k^2

\alpha\beta z + (\alpha^2 x - \alpha\beta y)i + (\alpha^2 y - \alpha\beta x)j + \alpha^2 z k \\ \qquad + \beta^2 z k + (\alpha\beta x - \beta^2 y)j - (\alpha\beta y + \beta^2 x)i - \alpha\beta z

Notice how the \alpha\beta z and the -\alpha\beta z cancel out. Also, notice how \alpha^2 + \beta^2 = 1. So, we can simplify to an extent like this:

(\alpha^2 x - \alpha\beta y)i + (\alpha^2 y + \alpha\beta x)j + z k \\ \qquad + (\alpha\beta x - \beta^2 y)j + (-\alpha\beta y - \beta^2 x)i

[(\alpha^2 - \beta^2)x - 2\alpha\beta y]i + [2\alpha\beta x + (\alpha^2 - \beta^2)]j + z

If we go back to the double angle identites, we find that \alpha^2 - \beta^2 = \cos\theta while 2\alpha\beta = \sin\theta. So, our final formula is:

(\cos\theta x - \sin\theta y) i + (sin\theta x + \cos\theta y) j + zk

Denouement

I know that in the complex number case and in the Clifford algebra case, I used \overline{r}zr. In this case, I used rq\overline{r} (swapping the roles of r and \overline{r}). I could have used either in the complex number case since the complex numbers are commutative: ab = ba for all complex numbers a and b. Thus, \overline{r}zr = z\overline{r}r = zr\overline{r} = rz\overline{r}. I probably should have used rz\overline{r} for the complex case and still used \overline{r}zr for the Clifford algebra case.

There is a technical reason for the difference. This may sail right over your head if you haven’t done much hardcore linear algebra. But, when we are using the complex numbers or the quaternions to represent rotations, we are really jamming a linear form into a vector and pretending like they’re the same thing. We are glossing over the difference between a vector space and its dual space by just pretending they are the same. We can get away with that in the complex numbers and the quaternions, but we could not get away with it in the Clifford algebra case. This is because the three-dimensional bivectors make up the dual space of the vectors. So, when we’re using three-dimensional bivectors, we are explicitly using the dual space instead of taking advantage of the isomorphism between the dual space and the original vectors. When we get to four dimensions or more, the rotations aren’t even in the dual space.

For a more English, less Math, version of the above, look at it this way. Really, a rotation is something fundamentally different than a vector. We lucked out with the complex numbers and the quaternions in that we were still able to represent rotations with something like a vector (though in the quaternion case, it was a vector plus a scalar). The luck in the quaternion case isn’t perfect, it is off by a few signs here and there. In more dimensions, we won’t be so lucky. In three dimensions, there are three degrees of freedom for a rotation. In four dimensions, there are six degrees of freedom. We are never going to jam those six degrees of freedom into our puny four-dimensional vector.

Next time

Next time, we will do these transformations of three-dimensional space using Clifford algebras instead of quaternions.

l