Function Repository Resource:

AxisAngle

Source Notebook

Generate the axis-angle representation of a three-dimensional rotation matrix

Contributed by: Jan Mangaldan

ResourceFunction["AxisAngle"][mat]

gives the axis-angle representation of a 3D rotation matrix mat.

Details and Options

ResourceFunction["AxisAngle"] returns results in an inert form Inactive[RotationMatrix][…].
ResourceFunction["AxisAngle"] works for exact as well as numerical real rotation matrices.

Examples

Basic Examples (3) 

Generate a rotation matrix from its axis-angle representation:

In[1]:=
rot = RotationMatrix[ArcTan[4/3], {2/7, 3/7, 6/7}]
Out[1]=

Reconstitute the axis-angle representation from the matrix:

In[2]:=
aa = ResourceFunction["AxisAngle"][rot]
Out[2]=

Use Activate to see the matrix again:

In[3]:=
Activate[aa]
Out[3]=

Scope (3) 

A real matrix:

In[4]:=
rot = {{413/1053, -64/81, 496/1053}, {896/1053, 41/81, 148/1053}, {-368/1053, 28/81, 917/1053}}
Out[4]=

Its axis-angle:

In[5]:=
ResourceFunction["AxisAngle"][rot]
Out[5]=

An approximate MachinePrecision matrix:

In[6]:=
rot = N[RotationMatrix[Pi/5, {3, 1, 2}]]
Out[6]=

Its axis-angle:

In[7]:=
ResourceFunction["AxisAngle"][rot]
Out[7]=

An approximate arbitrary precision matrix:

In[8]:=
rot = N[RotationMatrix[Pi/3, {2, 1, 4}], 25]
Out[8]=

Its axis-angle:

In[9]:=
ResourceFunction["AxisAngle"][rot]
Out[9]=

Applications (3) 

Generate a matrix from a given set of Euler angles:

In[10]:=
rot = EulerMatrix[{-Pi/6, Pi/3, -Pi/2}]
Out[10]=

Convert to its axis-angle representation:

In[11]:=
nro = FullSimplify[ResourceFunction["AxisAngle"][rot]]
Out[11]=

Verify that they give the same rotation matrix:

In[12]:=
RootReduce[Activate[nro] == rot]
Out[12]=

Generate a matrix from a given set of roll-pitch-yaw angles:

In[13]:=
rot = RollPitchYawMatrix[{Pi/4, -Pi/4, Pi/4}]
Out[13]=

Convert to its axis-angle representation:

In[14]:=
nro = FullSimplify[ResourceFunction["AxisAngle"][rot]]
Out[14]=

Verify that they give the same rotation matrix:

In[15]:=
RootReduce[Activate[nro] == rot]
Out[15]=

Generate a random rotation matrix:

In[16]:=
rot = (#/Det[#]) &[RandomVariate[CircularRealMatrixDistribution[3]]]
Out[16]=

Convert to its axis-angle representation:

In[17]:=
ResourceFunction["AxisAngle"][rot]
Out[17]=

Properties and Relations (1) 

AxisAngle is effectively the inverse of RotationMatrix:

In[18]:=
\[Theta] = ArcTan[3/4];
v = {6/7, 3/7, 2/7};
In[19]:=
rot = RotationMatrix[\[Theta], v]
Out[19]=
In[20]:=
ResourceFunction["AxisAngle"][rot]
Out[20]=

Possible Issues (1) 

For singular rotation matrices, the choice of axis returned is arbitrary:

In[21]:=
rot = IdentityMatrix[3]
Out[21]=
In[22]:=
ResourceFunction["AxisAngle"][rot]
Out[22]=
In[23]:=
rot = RotationMatrix[Pi, {4/9, -7/9, -4/9}]
Out[23]=
In[24]:=
ResourceFunction["AxisAngle"][rot]
Out[24]=

Neat Examples (2) 

Generate two random unit vectors:

In[25]:=
{v1, v2} = RandomPoint[Sphere[], 2]
Out[25]=

Find the axis-angle representation of the matrix that transforms one vector to the other:

In[26]:=
aa = ResourceFunction["AxisAngle"][RotationMatrix[{v1, v2}]]
Out[26]=

Verify the result:

In[27]:=
VectorAngle[v1, v2] == First[aa]
Out[27]=
In[28]:=
Normalize[Cross[v1, v2]] == Last[aa]
Out[28]=
In[29]:=
Norm[Activate[aa] . v1 - v2] // Chop
Out[29]=

Here are two polygons:

In[30]:=
poly1 = Polygon[{{0, 1, 0}, {0, 0, 1}, {1, 0, 0}}];
poly2 = Polygon[{{1.9, 3., 3.}, {3., 3.777, 3.7}, {3., 3.77, 2.22}}];

Use FindGeometricTransform to find a rigid transformation between the two:

In[31]:=
{err, tf} = FindGeometricTransform[poly1, poly2, TransformationClass -> "Rigid"]
Out[31]=

Extract the rotation matrix:

In[32]:=
rot = Drop[TransformationMatrix[tf], -1, -1]
Out[32]=

Convert the rotation matrix to its axis-angle representation:

In[33]:=
ResourceFunction["AxisAngle"][rot]
Out[33]=

Version History

  • 1.0.0 – 14 October 2019

Source Metadata

Author Notes

This submission is based on this original implementation on Stack Exchange: https://mathematica.stackexchange.com/a/136500.
AxisAngle uses the "Pixar method" to generate the orthogonal vectors needed to apply the Euler fixed point theorem for computing the axis-angle representation.

License Information