I'm not sure this question is well posed in the sense that the answer is non-unique. A simple example would be r=RotationMatrix[1,{1,0,0}]=RotationMatrix[-1,{-1,0,0}] are the same. So which one are you intending to find?
–
bill sAug 6 '13 at 13:08

The one where the angle is in [0,2 pi]. I think it is well posed as angle and axis are directly linked and you can provide a unique answer with a perfectly reasonable additional assumption.
–
DanvilAug 6 '13 at 13:43

Actually there was an answer earlier which was just fine but it was deleted?
–
DanvilAug 6 '13 at 13:46

@Danvil it was deleted because it was incorrect, so he deleted it. It was incorrect, as it worked only for the example you provided and could not be generalized.
–
rcollyerAug 6 '13 at 14:42

1

@Danvil I'm thankful I noticed the wink. It would have been a bad day, otherwise. :P
–
rcollyerAug 6 '13 at 16:38

4 Answers
4

There is no need to use Eigensystem or Eigenvectors to find the axis of a rotation matrix. Instead, you can read the axis vector components off directly from the skew-symmetric matrix

$$a \equiv R^T-R$$

In three dimensions (which is assumed in the question), applying this matrix to a vector is equivalent to applying a cross product with a vector made up of the three independent components of $a$:

{1, -1, 1}Extract[a, {{3, 2}, {3, 1}, {2, 1}}]

This one-line method of finding the axis is applied in the following function. To get the angle of rotation, I construct two vectors ovec, nvec perpendicular to the axis and to each other, to find the cosine and sine of the angle using the Dot product (could equally have used Projection). To get a first vector ovec that is not parallel to the axis, I permute the components of the axis vector using the fact that

In the test, I have to account for the fact that if the function axisAngle defined the axis vector with the opposite sign as the random test vector, I have to reverse the sign of the rotation angle. This is what the factor Dot[v, axis] does.

Explanation of how the axis results from a skew-symmetric matrix

If $\vec{v}$ is the axis of rotation matrix $R$, then we have both $R\vec{v} = \vec{v}$ and $R^T\vec{v} = \vec{v}$ because $R^T$ is just the inverse rotation. Therefore, with $a \equiv R^T-R$ as above, we get

$$a \vec{v} = \vec{0}$$

Now the skew-symmetric property $a^T = -a$, which can be seen from its definition, means there are exactly three independent matrix element in $a$. They can be arranged in the form of a 3D vector $\vec{w}$ which must have the property $a \vec{w} = 0$. This vector is obtained in the Extract line above. In fact, $a \vec{x} = \vec{w}\times \vec{x}$ for all $\vec{x}$, and hence if $a \vec{x} = 0$ then $\vec{x}\parallel\vec{w}$. Therefore, the vector $\vec{v}$ is also parallel to $\vec{w}$, and the latter is a valid representation of the rotation axis.

Edit 2: speed considerations

Since the algorithm above involves only elementary operations that can be compiled, it makes sense that a practical application of this approach would use Compile. Then the function could be defined as follows (keeping the return values arranged as above):

This is more than an order of magnitude faster than the un-compiled version. I removed Orthogonalize from the code because I didn't find it in the list of compilable functions.

Note that Eigensystem is not in that list, either.

Edit 3

The first version of axisAngle demonstrated the basic math, but the compiled version axisAngle1 (together with the re-defined axisAngle as a wrapper) is faster. One thing that was missing was the correct treatment of the edge case where the rotation is by exactly $\pi$ in angle.

I added that fix only to the compiled version (axisAngle1) because I think that's the more practical version anyway. The trivial case of zero rotation angle was already included in the earlier version.

To explain the added code, first note that for angle $\pi$ you can't read off the axis from $R^T - R$ because the resulting matrix vanishes. To get around this singular case, we can use the geometric fact that a rotation by $\pi$ is equivalent to an inversion in the plane perpendicular to the rotation axis given by the unit vector $\vec{n}$. Therefore, if we form the sum of a vector $\vec{v}$ and its $\pi$-rotated counterpart, the components transverse to the rotation axis cancel and the result is always parallel to the axis. In matrix form,

Since this holds for all vectors, it is a matrix identity. The right-hand side contains a matrix $\vec{n}\vec{n}^T$ which must have at least one row that's nonzero. This row is proportional to $\vec{n}^T$, so you can read of the axis vector directly from $(R+1)$, again without any eigenvalue computations.

Beautiful! (+1) My hamfisted approach to this has been an opportunity to learn.
–
ubpdqnAug 6 '13 at 22:28

Note (as you have outlined) you could also obtain the axis from NullSpace[m-Transpose[m]]
–
ubpdqnAug 7 '13 at 4:17

@ubpdqn True, but that takes more computation... as I said, you can read the axis off literally from the matrix a itself.
–
JensAug 7 '13 at 5:15

Actually for cases $\theta=0$ and $\theta=\pi$ the $R-R^T$ is a zero matrix and the axis is thus maps to {{0,0,0},0) in your code. The test set always skirts these "points". The eigensystem allows extraction of the eigenvector associated with eigenvalue 1). I agree with lack of simplicity or elegance using the eigensystem. I found it a lazy way to deal with these anomalous cases: integer*$\pi$. I think my code works now. Passes your test (up orientation of axis issue) and the "extreme" cases.
–
ubpdqnAug 7 '13 at 5:29

Maybe the easiest way to deal with the isotropic cases ($\pm$IdentityMatrix[3]) would be to append the rule axis /. {0, 0, 0} -> {0, 0, 1} to the result. The choice of rotation axis is arbitrary in that case and I'd fix it to be the z axis.
–
JensAug 7 '13 at 5:41

Presentations also has an EulerAngles routine that will return the two sets of Euler angles for any of the possible sequence of rotations specified by strings. For example, here are the two sets of Euler angles of the example for two different rotation sequences.

It doesn't seem to work with a rotation of $\pi$ about the {0,0,1}.
–
rcollyerAug 6 '13 at 14:39

@rcollyer, that is to be expected because the rotation group $SO(3)$ cannot be covered by a single chart. Each chart will have its cases that are problematic. I used this parametrization and it worked for the OP's question. Still using this and going with sols = NSolve[eqns[[2]] == 0, t] we get $t=0$ for your case. The technique I used for the axis of rotation should be fine for all cases, however.
–
Suba ThomasAug 6 '13 at 14:55

True, but I think it is amenable to decomposition into the generators of rotation, i.e. the rotations should have eigenvalues of the form $\pmatrix{e^{i \theta} & 1 & e^{-i \theta}}$, or for real rotations $\pmatrix{\cos\theta + i \sin\theta & 1 & \cos\theta - i \sin\theta}$. Thus, the angle is easily recoverable. As to the direction, I have not yet looked through your code.
–
rcollyerAug 6 '13 at 15:05

Thanks for pointing that out. With your approach the angle of rotation is ambiguous for the OP's question, i.e we don't know if it is +20 or -20, whereas with the parametrization I used only + 20 satisfied all conditions. Probably quaternions will give a unique correct solution, but I do not have the material before me now.
–
Suba ThomasAug 6 '13 at 15:20

Well you can choose the angle to be between $0$ and $2 \pi$, to fix the branch.
–
rcollyerAug 6 '13 at 15:25

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.