## axis and angle of rotation

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:

$\left(\begin{array}{lll} -0.0361149 & -0.0667194 & 0.997118\end{array}\right)$

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 $2 cos \theta + 1$. 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 $2 cos \theta + 1$ 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 $2 cos \theta + 1$,

$2.81888 = 2 \cos (\theta )+1$

and solve for the angle…

$\{\{\theta \to -0.428857\},\{\theta \to 0.428857\}\}$

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

$\left(\begin{array}{lll} 0.90956 & -0.414415 & -0.0310051 \\ 0.414851 & 0.909845 & 0.00899314 \\ 0.0244829 & -0.0210423 & 0.999479\end{array}\right)$

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”

$\left(\begin{array}{lll} -0.0361149 & -0.0667194 & 0.997118\end{array}\right)$

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

$\left(\begin{array}{lll} -0.0361149 & -0.0667194 & 0.997118\end{array}\right)$

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

$N = \left(\begin{array}{lll} 0 & c & -b \\ -c & 0 & a \\ b & -a & 0\end{array}\right)$

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

$R = I + sin \theta\ N + (1-cos \theta) (N^2)$,

where I is a 3×3 identity matrix.

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

$N = \left(\begin{array}{lll} 0 & 1 & 0 \\ -1 & 0 & 0 \\ 0 & 0 & 0\end{array}\right)$

We get R

$R = \left(\begin{array}{lll} \cos (\theta ) & \sin (\theta ) & 0 \\ -\sin (\theta ) & \cos (\theta ) & 0 \\ 0 & 0 & 1\end{array}\right)$
=

$\left(\begin{array}{lll} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1\end{array}\right)$ + $\left(\begin{array}{lll} 0 & \sin (\theta ) & 0 \\ -\sin (\theta ) & 0 & 0 \\ 0 & 0 & 0\end{array}\right)$

+ $\left(\begin{array}{lll} \cos (\theta )-1 & 0 & 0 \\ 0 & \cos (\theta )-1 & 0 \\ 0 & 0 & 0\end{array}\right)$

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

$\left(\begin{array}{lll} \cos (\theta ) & -\sin (\theta ) & 0 \\ \sin (\theta ) & \cos (\theta ) & 0 \\ 0 & 0 & 1\end{array}\right)$

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

$\left(\begin{array}{lll} -0.0361149 & -0.0667194 & 0.997118\end{array}\right)$

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

I create N:

$N = \left(\begin{array}{lll} 0 & 0.997118 & 0.0667194 \\ -0.997118 & 0 & -0.0361149 \\ -0.0667194 & 0.0361149 & 0\end{array}\right)$

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

$R =\left(\begin{array}{lll} 0.90956 & -0.414415 & -0.0310051 \\ 0.414851 & 0.909845 & 0.00899314 \\ 0.0244829 & -0.0210423 & 0.999479\end{array}\right)$

=

$\left(\begin{array}{lll} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1\end{array}\right)$ + $\left(\begin{array}{lll} 0 & -0.414633 & -0.027744 \\ 0.414633 & 0 & 0.0150177 \\ 0.027744 & -0.0150177 & 0\end{array}\right)$

+ $\left(\begin{array}{lll} -0.0904401 & 0.000218206 & -0.00326108 \\ 0.000218206 & -0.0901551 & -0.00602457 \\ -0.00326108 & -0.00602457 & -0.000521231\end{array}\right)$

And yes, that is the mars rotation matrix.

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

$N = \left(\begin{array}{lll} 0 & 0.997118 & 0.0667194 \\ -0.997118 & 0 & -0.0361149 \\ -0.0667194 & 0.0361149 & 0\end{array}\right)$

We get the rotation matrix

$\left(\begin{array}{lll} 0.90956 & 0.414851 & 0.0244829 \\ -0.414415 & 0.909845 & -0.0210423 \\ -0.0310051 & 0.00899314 & 0.999479\end{array}\right)$

=

$\left(\begin{array}{lll} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1\end{array}\right)$ + $\left(\begin{array}{lll} 0 & 0.414633 & 0.027744 \\ -0.414633 & 0 & -0.0150177 \\ -0.027744 & 0.0150177 & 0\end{array}\right)$

+ $\left(\begin{array}{lll} -0.0904401 & 0.000218206 & -0.00326108 \\ 0.000218206 & -0.0901551 & -0.00602457 \\ -0.00326108 & -0.00602457 & -0.000521231\end{array}\right)$

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.

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):

$N = \left(\begin{array}{lll} 0 & c & -b \\ -c & 0 & a \\ b & -a & 0\end{array}\right)$

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

$R = I + sin \theta\ N + (1-cos \theta) (N^2)$,

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…

$\left(\begin{array}{lll} c y-b z & a z-c x & b x-a y\end{array}\right)$

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

$\left(\begin{array}{lll} b z-c y & c x-a z & a y-b x\end{array}\right)$

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:

$exp(A) = I + A + \frac{A^2}{2} + \frac{A^3}{3!} + \cdot\ \cdot\ \cdot$

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 $N \theta$. That is, the formula

$R = I + sin \theta\ N + (1-cos \theta) (N^2)$

is the summation of the infinite series $exp( N \theta)$.

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

$\left(\begin{array}{lll} 0.90956 & 0.414851 & 0.0244829 \\ -0.414415 & 0.909845 & -0.0210423 \\ -0.0310051 & 0.00899314 & 0.999479\end{array}\right)$

and that is not the right answer.

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

$\left(\begin{array}{lll} 0.90956 & -0.414415 & -0.0310051 \\ 0.414851 & 0.909845 & 0.00899314 \\ 0.0244829 & -0.0210423 & 0.999479\end{array}\right)$

and that is, indeed, the mars rotation matrix.

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

$dRx = \left(\begin{array}{lll} 0 & 0 & 0 \\ 0 & 0 & 1 \\ 0 & -1 & 0\end{array}\right)$

the derivative of $Ry(\theta)$ at 0:

$dRy = \left(\begin{array}{lll} 0 & 0 & -1 \\ 0 & 0 & 0 \\ 1 & 0 & 0\end{array}\right)$

And the derivative of $Rz(\theta)$ at 0:

$dRz = \left(\begin{array}{lll} 0 & 1 & 0 \\ -1 & 0 & 0 \\ 0 & 0 & 0\end{array}\right)$

The set of skew-symmetric 3×3 matrices is a vector space, and those matrices are a basis. Physicists multiply them by $d\theta$ 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

$\left(\begin{array}{lll} 0 & c & -b \\ -c & 0 & a \\ b & -a & 0\end{array}\right)$

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 $N \theta$ gives me a rotation about N thru $\theta$.

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.)

### 10 Responses to “axis and angle of rotation”

1. Arman Says:

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.

2. rip Says:

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.

3. Arman Says:

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.

4. rip Says:

hi arman,

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

5. Araman Says:

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.

6. rip Says:

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

7. Stefan Says:

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!

8. rip Says:

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.

9. www.wowbeats.com Says:

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!

10. 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.