Function Repository Resource:

RotationMatrixToQuaternion

Source Notebook

Convert a rotation matrix to an equivalent unit quaternion

Contributed by: Jan Mangaldan

ResourceFunction["RotationMatrixToQuaternion"][mat]

converts the 3×3 rotation matrix mat into an equivalent unit quaternion.

Details and Options

ResourceFunction["RotationMatrixToQuaternion"] works for exact as well as numerical real rotation matrices.
ResourceFunction["RotationMatrixToQuaternion"] returns results as ResourceFunction[“Quaternion”] objects.

Examples

Basic Examples (2) 

Generate a rotation matrix:

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

Convert to quaternion form:

In[2]:=
ResourceFunction["RotationMatrixToQuaternion"][rot]
Out[2]=

Scope (3) 

A real matrix:

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

Convert to quaternion form:

In[4]:=
ResourceFunction["RotationMatrixToQuaternion"][rot]
Out[4]=

An approximate MachinePrecision matrix:

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

An approximate arbitrary precision matrix:

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

Applications (4) 

Create a rotation matrix:

In[9]:=
rot = N[RotationMatrix[{{0, 0, 1}, {1, -1, 1}}]]
Out[9]=

Also define a vector to be rotated:

In[10]:=
vec = RandomVariate[NormalDistribution[], 3]
Out[10]=

Transform the vector using the rotation matrix representation:

In[11]:=
rot.vec
Out[11]=

Transform the vector using the quaternion representation of a rotation to get the same result:

In[12]:=
With[{uq = ResourceFunction["RotationMatrixToQuaternion"][rot]}, Rest[Apply[
   List, (uq ** Apply[ResourceFunction["Quaternion"], Prepend[vec, 0]]) ** Power[uq, -1]]]]
Out[12]=

Generate a matrix from a given set of Euler angles:

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

Convert to its quaternion representation:

In[14]:=
FullSimplify[ResourceFunction["RotationMatrixToQuaternion"][rot]]
Out[14]=

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

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

Convert to its quaternion representation:

In[16]:=
FullSimplify[ResourceFunction["RotationMatrixToQuaternion"][rot]]
Out[16]=

Generate a random rotation matrix:

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

Convert to its quaternion representation:

In[18]:=
FullSimplify[ResourceFunction["RotationMatrixToQuaternion"][rot]]
Out[18]=

Version History

  • 1.0.0 – 23 December 2020

Source Metadata

Related Resources

License Information