In this post I’d like to talk about rotations in three-dimensional space. As you can imagine,
rotations are pretty important transformations! They commonly show up in physics as an example of
a symmetry transformation, and they
have practical applications in fields like computer graphics.
Like any linear transformation, rotations can be represented by matrices, and this provides the
best representation for actually computing with vectors, transformations, and suchlike. Various
formulas for rotation matrices are well-known and can be found in untold books, papers, and
websites, but how do you actually derive these formulas?
If you know a little trigonometry, you can work out the 2D rotation matrix formula by drawing a
diagram like this:
The rotation takes the vector (1,0) to (cosθ,sinθ) and the vector (0,1) to
(−sinθ,cosθ). This is just what we need, since in a matrix the first column is
just the output when you put in a unit vector along the x-axis; the second column is the
output for a unit vector along the y-axis, and so on. So the 2D rotation matrix is:
R2D(θ)=[cosθsinθ−sinθcosθ].
You can expand this 2×2 matrix into a 3×3 one and rearrange a few things to get
rotations around the x, y, and z axes. That’s all well and good, but
what if you want a rotation around some other axis, one that’s not lined up with x,
y, or z? Trying to figure out that formula directly with trigonometry
is probably hopeless!
One way to do it is to combine rotations together to achieve the desired result. We could do
something like: work out the polar coordinates of the axis we want to rotate around, then apply
some rotations around the y and z axes that take that axis onto the
x-axis. Then do an x-rotation by your desired angle, and then revert our
y and z rotations to get the axis back to where it was.
That will work, but it’s inelegant. Since this is a blog about theoretical stuff, I’m going to
talk about a more elegant way to do it, using a thing called an
infinitesimal generator!
The basic idea of this is to first work out the formula for an infinitesimal rotation - a rotation
by a very small angle. What’s the advantage of this? Everything’s simpler in infinitesimals,
because you’re allowed to simply ignore things that are “too small to matter”. Infinitesimal math
lets you make fantastically crude approximations and not worry about it because
eventually, you’re going to take the limit as the infinitesimal thing goes to zero, and in that
limit your approximation becomes exact! In this case (and I’m giving away a bit of the story
here), once we know the formula for an infinitesimal rotation, we’ll integrate it to get the
formula for a finite rotation.
Let’s return to the diagram above, but now consider a rotation by an infinitesimal angle dθ.
Imagine taking the Cartesian coordinate system and just barely starting to turn it
counter-clockwise. What happens to the point (1,0)? It goes straight up! Eventually, if you
keep rotating, it’ll start to curve over to the left…but since this is just an infinitesimal
rotation, it doesn’t have time to do that.
Surely, you say, it doesn’t go straight up? It must go a little to the left,
mustn’t it? Yes, it does…but this is one of those “too small to matter” things that you can just
ignore! The smaller dθ is, the closer to straight-up the point goes, and that justifies
treating it as if it just goes straight up all the time.
An analogous thought process about the point (0,1) reveals that it goes straight to the left,
neither up nor down (within our approximation). Here’s the diagram again, modified with these
observations:
As before, the effect of the rotation on these two points is all we need to get the matrix for an
infinitesimal rotation:
r2D(dθ)=[1dθ−dθ1].
(I’m using lowercase letters, e.g. r2D(dθ) for infinitesimal transformations, and
uppercase letters like R2D(θ) for finite ones.) Anyway, this looks a lot simpler than the
first matrix we found, and we didn’t need any trigonometry to work it out!
Conceptually, what we need to do next is accumulate the effect of many successive infinitesimal
rotations, to get a finite rotation. Technically, this is going to be accomplished by integration.
But before we can integrate anything, we need a differential equation, so we have to fiddle with
the formula a bit to get it into that shape. Let’s start with a vector u and apply the
infinitesimal rotation to it; that’ll cause u to change by some small vector du:
(In the second line there, I is the identity matrix.) Now we have a differential equation that
tells us how u changes with θ for very small values of θ. The matrix in the
last line, which I’m naming G2D, is the “infinitesimal generator” of 2D rotation! It’s a
matrix that converts a vector into its derivative with respect to infinitesimal rotations.
The Exponential Map
At this point, we need to take a short detour into the world of differential equations. We have a
differential equation in which the derivative of a vector, u, is equal to a matrix
multiplied by u itself. This looks very similar to a famous differential equation about
real numbers:
dxdy=ky,
whose solution is the exponential function, y(x)=y0ekx (where y0 is a constant of
integration, which equals y(0)). Working purely by analogy, we might hypothesize that the
solution to our original differential equation is:
u(θ)=R2D(θ)u0=eG2Dθu0.
Now, instead of the ordinary exponential function on real numbers, we have some kind of exponential
of a matrix, where e “raised to the power of” a matrix gives us back another matrix. Does
this even make sense? It turns out that it does! This “matrix exponential” is a mathematical
entity called the exponential map, a
generalization of the exponential function to a broad range of contexts. It obeys many of the same
rules as the familiar exponential function, so we can prove that it really does solve our
differential equation!
Now we just need to calculate it. One way of defining the ordinary exponential function is by its
Taylor series:
ex=1+x+2!x2+3!x3+⋯
The same Taylor series can be straightforwardly translated to one for the exponential map on
matrices! This implies that:
R2D(θ)=I+G2Dθ+G2D22!θ2+G2D33!θ3+⋯
We’ve made some progress. Instead of a differential equation, we now have an infinite series for
explicitly calculating a rotation matrix for any angle! We can almost split this into the
four individual matrix elements and sum up each series individually - but first we need to see what
the integer powers of G2D look like.
Let’s just calculate the first few powers and see what happens:
Woah! That’s interesting - the fifth power of G2D is the same as G2D itself.
This means that if we kept going, we’d just generate the same four matrices over and over again.
Note also that G2D3=−G2D, and G2D4=−G2D2. Not only do the matrices repeat
every four powers, they repeat with a sign change every two powers.
This is very similar to the behavior of a couple of other famous Taylor series!
sinxcosx=x−3!x3+5!x5−⋯=1−2!x2+4!x4−⋯
The Taylor series of the sine and cosine functions are the same as that of the exponential
function…except that they only include every two powers, and alternate with a sign change each
term, with sine taking the odd powers and cosine taking the even powers. (This is the basis of
Euler’s formula.)
If we regroup the terms in our series for R2D(θ) into odd and even, and use the fact that
the powers of G2D repeat with sign changes, we get:
This final matrix is the same as in the very first equation in this article, but now we’ve gotten
there through a completely different route - we first worked out the generator matrix for an
infinitesimal rotation, then we calculated the exponential of that matrix (multiplied by θ)
to get the matrix for a finite rotation!
Axis-Angle Rotations in 3D
We can apply the same approach to solve the problem I posed near the top of this article: to find
the matrix of a rotation about an arbitrary axis. Let’s identify this axis by a, a unit
vector pointing along the axis. Our goal is to find an expression for Ra(θ), the 3×3
matrix that rotates around a by an angle θ. As before, we’ll begin by considering an
infinitesimal rotation, and working out the generator Ga.
Let’s consider the action of a rotation around a by an infinitesimal angle dθ on an
arbitrary 3D vector u. What will happen to u? In the infinitesimal approximation,
it moves in a direction straight out of the plane containing a and u, as shown here:
We can express this using the vector cross product:
dθdu=a×u.
(Note that the length of the cross product vector is related to the lengths of the two
vectors being crossed. I leave it as an exercise for the reader to show that the length of
du is correct in the above equation!)
The cross product can also be expressed as a matrix. In this case, we need a matrix that, when
multiplied by u, gives us a×u. This will be Ga, the infinitesimal
generator for rotations around a.
a×u=0az−ay−az0axay−ax0u=Gau
It follows that Ra(θ)=eGaθ, i.e. the rotation matrix we’re trying to find is
the exponential of θ times the matrix Ga above! If you work out the first few powers of
Ga and use the fact that a is a unit vector (so ax2+ay2+az2=1), you’ll see
that it follows the same pattern that the powers of G2D did: they repeat every four powers,
and repeat with sign alternation every two, so that Ga3=−Ga and Ga4=−Ga2. We can
almost use the same odd/even term grouping as before to split the series into a part
proportional to cosθ, and a part proportional to sinθ. However, there is one
little bug to deal with. In the 2D version of this derivation, we implicitly used the fact that
G2D4=I, the identity matrix, when factoring −G2D2 out of the even terms. Here,
Ga4=I, so we have to do a little bit of a fixup.
And there it is! Whew! That was a lot of work, but now we have an explicit matrix for arbitrary
rotations around any axis in 3D space, and along the way we met a bunch of interesting concepts,
such as infinitesimal math, the exponential map, and Taylor series.
Rotations are truly a fascinating set of transformations, and in this post I’ve only just scratched
the surface of the interesting structure that they have. Rotations in any number of dimensions
form a mathematical entity called a Lie group,
and the fact that they can be produced by infinitesimal generators is intimately linked with the
special properties of Lie groups! But that’s a topic for a future post.