**edit: I have solved the problem of the sign ambiguity. see** 28 Sept 2008.

I was going to call this “rotations 2”, but I decided to put the key computations in the name.

## from rotation matrix to axis and angle of rotation

Having gotten the rotation (attitude) matrix for mars coordinates here, can we find its axis of rotation? (not of mars, of the attitude matrix!)

Sure, that’s just the eigenvector with eigenvalue 1!

Every 3D rotation has an eigenvalue of 1: there is a line in space which is left fixed under the rotation. Any vector on that line is an eigenvector. It has eigenvalue 1 because the line is not being stretched or compressed: nothing has been done to it. The other two eigenvalues are complex, and so are their eigenvectors, because there are no other subspaces which are left fixed by the rotation.

When I ask Mathematica® to find the eigenvalues and eigenvectors of the mars rotation matrix, the eigenvector with eigenvalue 1 is:

That’s very nearly the z-axis, as it should be.

There’s just one little problem. The negative of that eigenvector – the negative z-axis – is every bit as good an answer. Any multiple of it is every bit as good an answer. We know the rotation axis, but we don’t know the direction of it. What we really know is the line in space which is left fixed by the rotation.

Call the answer we got “the positive eigenvector” (because its largest component is positive).

What about the angle? Well, there is some coordinate system in which any given rotation is a rotation exactly about the z-axis, and the trace of a (3D) rotation matrix is . And the trace is invariant under similarity transformations (because it’s the sum of the eigenvalues, and they are invariant under similarity transformations).

Too convoluted? The trace of any rotation matrix is invariant under similarity transform because it’s the sum of the eigenvalues; therefore it has the value which we would obtain by taking the axis of rotation as a coordinate axis.

So we compute the trace of the mars rotation matrix, set it equal to ,

and solve for the angle…

Here we have another problem. There are two possible answers, but only one of them is right – and I know of no simple way to tell which it is. **edit: yes I do.** 28 Sept 2008. We have the original rotation, so we can see what it does to a test vector; we have two candidates for axis-angle, and we could work them both out to see which is right.

We don’t actually have 4 distinct answers for axis and angle, but only 2 distinct answers: taking one eigenvector and one angle is the same as taking the other eigenvector and the other angle.

In this case, we know that the negative angle is correct for the eigenvector we chose: we know the mars coordinate system is rotated about something close to the z-axis thru approximately 335° = -25°. If we choose the other unit eigenvector, then the positive angle would be correct.

Let me be clear about that. The attitude matrix for mars’ coordinates is

Either from its construction or by inspection, it is very nearly a rotation thru a small negative angle about the +z-axis, and the “positive eigenvector”

is nearly the z-axis; so we should go with the negative angle, too. Here it is in radians… -0.428857 and in degrees… -24.5717.

But in general, we won’t be able to say, it’s almost a rotation about one of the axes. What then? Well, what if we could compute a rotation matrix from a given axis and angle? That would be a wonderful in general, and in this case it would let us construct the 2 possible answers and see which one matches the original. (In fact, it will let me construct the 4 possible answers and confirm that only 2 are distinct, but one would rarely do that.)

Before we do that, however, **we should ask if we need it. We do, after all, have the original rotation matrix, for any computational needs.** Do we need consistent signs for the axis and angle, or would it suffice to say that the axis of rotation is given by any vector parallel or anti-parallel to

and that the rotation is very nearly 25°, sign unknown? Maybe it would suffice, maybe not.

Let’s suppose that, for some reason, we need to know the sense of the rotation; we have to know whether the angle is 25° or -25° with the positive eigenvector.

## from axis and angle to rotation matrix

Can we construct a matrix for a rotation given the axis of rotation and the angle?

No problem. The following recipe shows up in many different forms. Here’s one. Write the axis of rotation as a unit vector. (that’s what I forget when I rush into this calculation: to make it a unit vector.)

hang on: what kind of rotation? because I wrote my rotation matrices for CCW rotations of coordinate axes, i.e. for CW rotations of vectors, I assume that I have been given a vector for the axis of rotation, and I have been given an angle which is to rotate test vectors CW.

if our given axis is not a unit vector, “make it so”, suppose our unit vector is (a, b, c).

here’s what we do. We recast that vector as a specific skew-symmetric matrix. That is, we define N

Then there is a magical formula: the rotation matrix R is given by

,

where I is a 3×3 identity matrix.

Let me illustrate that for a rotation about the z axis (0,0,1). N becomes…

We get R

=

+

+

which is my CCW rotation of the coordinate axes.

As usual, there is a sign convention: where are the negative signs in N? If you see a version of this formula with all the signs switched in N, you would end up with

for your rotation about the z-axis. And if you wanted a positive angle to correspond to a CCW rotation of a vector (instead of to a CCW rotation of the axes), that rotation matrix would be right.

I did confirm privately that I recover my answers for Ry and Rx, rotations about the y and x axes, too.

## selecting the answer from the candidates

Now that we know how to get from axis-angle to a rotation matrix, we can test our two candidates for the mars rotation.

I want to create two matrices using the positive eigenvector and the two angle solutions. First I want to make sure that the positive eigenvector

is a unit vector. (yes, it is.)

I create N:

Then I get the rotation matrix using the negative angle -0.428857:

=

+

+

And yes, that is the mars rotation matrix.

Next, try the positive angle (with the same, “positive”, eigenvector). we set N

We get the rotation matrix

=

+

+

and that is not the mars rotation matrix. We have confirmed that the positive eigenvector and the negative angle are the axis-angle representation of the mars rotation matrix.

In addition, I have privately confirmed that if we take the negative eigenvector, and the two angle solutions, we get the same two rotation matrices; this time the negative eigenvector and positive angle reproduce the mars rotation matrix.

So, we now know two magical things:

- how to extract the axis and angle of rotation from a 3D rotation matrix;
- how to build a 3D rotation matrix given the axis and angle of rotation.

The only problem is that we get extra candidates for the answers for the axis and angle of rotation. If it matters, we eliminate the wrong answer the same way we dealt with extra square roots in high school: plug each solution back in to the original equations. In this case, reconstruct two distinct rotation matrices from the possible answers, and see which one matches the original.

## casually: comments and connections

I am finding it all too easy to get hung up here on precise details. To heck with it. Let me try to be interesting rather than exact.

**I have chosen this for my sign convention for the matrix N** corresponding to a unit vector (a, b, c):

because I want positive angles to be CW rotations of vectors. I suspect that most presentations of this formula use the opposite convention, so that positive angles are CCW rotations of vectors. (at the risk of boring you to death: it isn’t enough to say that a positive angle is, for example, a CW rotation; you must say which is rotated CW: a vector or a coordinate axis.)

There are versions of the formula

,

**using a unit vector and dot and cross products**. How does a cross product get in there? That’s what the matrix N does, although my sign makes it correspond to the negative cross-product.

if I apply the matrix N to a vector (x,y,z), I get…

whereas if I take the cross product of (a,b,c) with the vector (x,y,z), i.e. (a,b,c) x (x,y,z), I get

That is, I get the negative answer. OK, I did that by changing the convention for the sign of a rotation matrix. The skew symmetric matrix N can be viewed as a stand-in for the cross product.

Now, i’m going to show you another piece of magic. It’s called the **matrix exponential**, and for a matrix A it’s defined by a power series that corresponds to the power for the exponential of a real or complex variable:

It is very well behaved: that series converges for any square matrix A. We will talk more about this real soon, when we get back to quantum mechanics, but what I want to show you is that the rotation matrix is the matrix exponential of . That is, the formula

is the summation of the infinite series .

Now, we had four possible choices for but they lead to two possible rotation matrices. First, the positive eigenvector and the positive angle, I ask for MatrixExp[N ] and get:

and that is not the right answer.

then, using the positive eigenvector and the negative angle, I ask for MatrixExp[N ] and get:

and that is, indeed, the mars rotation matrix.

There’s another connection, **the inverse operation to the matrix exponential.** Take the rotation about the z-axis, differentiate wrt , and set to zero. (find the derivative of at 0.) we get

the derivative of at 0:

And the derivative of at 0:

The set of skew-symmetric 3×3 matrices is a vector space, and those matrices are a basis. Physicists multiply them by and call them infinitesimal rotations. OK – so long as we realize they’re not rotations.

Now, take our unit vector (a, b, c) and compute

a dRx + b dRy + c dRz

and that gives us

which is our skew matrix N.

The signs are consistent because I got the infinitesimal rotations from my rotation matrices. This gives me a way of finding N rather than looking it up in my notes.

Let me emphasize that connection.

- for rotation matrices about the coordinate axes…
- I can construct a set of skew-symmetric matrices by computing derivatives at 0,
- and from them I can construct a matrix N for an axis of rotation;
- then the matrix exponential of gives me a rotation about N thru .

The matrix exponential appears to be inverse to taking derivatives at 0.

yup.

And to put it in more complicated terms, namely **lie groups and lie algebras:**

- our rotations are elements of a lie group;
- the skew symmetric matrices are elements of its lie algebra;
- the lie algebra is the tangent space at the origin of the lie group;
- the matrix exponential maps from the lie algebra to the lie group.

But we can go a long way just treating the matrix exponential as a useful operator, without knowing about lie groups and lie algebras. when we come to study them, we will already be comfortable with the matrix exponential. (And one reason for studying matrix lie groups before lie groups in general is the existence of the matrix exponential.)

August 21, 2008 at 12:20 pm

I faced with the problem of finding exponential coordinate of two coordinate systems. I read your article and I found that very helpful. But I have a question and I would be obligued if you help me.

I want to know that does this method of finding axis and angle of rotation (which is so close to finding exponential coordinates) works for all rotation matrices?

I mean if I don’t know that my rotation matrix have (or have not) any fixed axis of rotation, what solution would be found by this method?

Thanks.

August 22, 2008 at 7:32 am

hi arman,

yes, this method of finding angle and axis of rotation works for all 3×3 rotation matrices (i.e. 3×3 orthogonal with determinant +1). every 3×3 rotation matrix has an axis of rotation, specified by the eigenvector whose eigenvalue is +1.

if you’re asking, what if the axis of rotation is a function of time, all this will do for you is find the instantaneous axis of rotation at one instant of time. i haven’t proved that, but i believe it.

August 22, 2008 at 12:16 pm

Hi,

Thanks a lot.

I had found an equivalent method in a book “mathematical introduction to robotics”, page 30, which could’nt be used for symmetric rotation matrices.

But with your method I can find solution of my problem of finding exponential coordinates.

Thanks a lot.

August 22, 2008 at 1:21 pm

hi arman,

glad it helped.

is there a “best book” on the mathematics of robotics?

August 24, 2008 at 12:34 am

Hi,

I didn’t see more than two or three books in field of robotics but I think this book is a good one:

“A mathematical introduction to robotics

Richard M. Murray”

you can find this book in “google book”.

Thanks.

August 24, 2008 at 7:13 am

thanks, arman, i’ll take a look at it.

October 8, 2008 at 11:56 am

I enjoyed your blog-post, thanks! I am teaching this work in an introductory course on linear algebra, and am trying to simplify the derivation of e^(A theta) from first principles. Your page is one of the very few that explore the relationships between the rotation matrix and the cross-product matrix; kudos!

October 9, 2008 at 8:24 am

Thanks, Stefan, for your kind words.

I can tell from the blog stats that this specific post is my most popular, but I appreciate getting a personal comment from one of its many readers.

April 12, 2013 at 11:56 am

Hi! I could have sworn I’ve visited this web site before but after looking at some of the articles I realized it’s new

to me. Regardless, I’m definitely happy I discovered it and I’ll be bookmarking it and checking back regularly!

July 16, 2013 at 7:33 pm

Now, once she’s had one g-spot climax, is the best time to give her multiple orgasms. If you wait until you hit the bedroom you miss out on a wonderful opportunity to have the ultimate sexual experience. Again, work on getting them each individually,and then put together a volcanic combination.