Skip to content

Instantly share code, notes, and snippets.

@Nikolaj-K
Last active September 26, 2020 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nikolaj-K/907e0ea3cc958c483572950b746e0145 to your computer and use it in GitHub Desktop.
Save Nikolaj-K/907e0ea3cc958c483572950b746e0145 to your computer and use it in GitHub Desktop.
The virgin rotation matrix and the Chad Axis–angle representation
https://youtu.be/gBMdTSXhYsY
----
https://en.wikipedia.org/wiki/3D_rotation_group
https://en.wikipedia.org/wiki/Rotation_(mathematics)
https://en.wikipedia.org/wiki/Rotation_formalisms_in_three_dimensions
https://en.wikipedia.org/wiki/Rotation_matrix
https://en.wikipedia.org/wiki/Euler%27s_rotation_theorem
https://en.wikipedia.org/wiki/Axis%E2%80%93angle_representation
https://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula
https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
----
https://en.wikipedia.org/wiki/Matrix_exponential
https://en.wikipedia.org/wiki/Logarithm_of_a_matrix
$\exp(A) = \sum_{k=0}^\infty\frac{1}{k!}A^k = I + A + \frac{1}{2!}A^2 + \frac{1}{3!}A^3 + \cdots$
$\log(M)= \sum_{k=1}^\infty{\frac{(-1)^{k+1}}{k}}(M-I)^k =(M-I)-\frac{1}{2}(M-I)^2+\frac{1}{3}(M-I)^3\cdots$
Note that $\log$ might not converge in ${\mathbb R}^{n\times n}$, e.g. for $M =
\begin{bmatrix}
-7 & 0 \\
0 & 2 \\
\end{bmatrix}
$. As this is already true for $n=1$.
Consider $A$ the (Lie-)algebra element and $M$ a corresponding group element.
----
$\exp(\theta G) = I + \theta G + \frac{1}{2!}(\theta G)^2 + \frac{1}{3!}(\theta G)^3 + \cdots$
Wherever the series expansion of $\log$ is well defined, $\log(\exp(G))=\exp(\log(G))=G$.
$\dfrac{\mathrm{d}}{\mathrm{d}\theta}\left(\theta G\right)^n=G^n\cdot\dfrac{\mathrm{d}}{\mathrm{d}\theta}\theta^n=G^n\cdot n\cdot \theta^{n-1}=n\cdot G*\left(\theta G\right)^{n-1}$
$\dfrac{\mathrm{d}}{\mathrm{d}\theta}\exp(\theta G) = G + G^2\theta + \frac{1}{2!}G^3 \theta^2 + \cdots = G*\exp(\theta G)$
$G = G*\exp(0)=\dfrac{\mathrm{d}}{\mathrm{d}\theta}|_{\theta=0}\exp(\theta G)$
----
Example: Anti-symmetric $A$ in 2 dimensions (one independent coordinate). $A=\theta J$ where
$
J =
\begin{bmatrix}
0 & -1 \\
1 & 0 \\
\end{bmatrix}
$
$J^2 = \begin{bmatrix}
0 & -1 \\
1 & 0 \\
\end{bmatrix}*\begin{bmatrix}
0 & -1 \\
1 & 0 \\
\end{bmatrix}
=\begin{bmatrix}
-1 & 0 \\
0 & -1 \\
\end{bmatrix}\equiv -I_{2\times 2}
$
$J^3 = J * J^2 = -J$
$J^4 = J^2 * J^2 = (-I_{2\times 2})*(-I_{2\times 2}) = I_{2\times 2}$
$R_J(\theta):=\exp(\theta J) = I + \theta J - \frac{1}{2!}I_{2\times 2} - \frac{1}{3!}J + \cdots $
$= \cos\theta\cdot I_{2\times 2} + \sin\theta\cdot J
=
\begin{bmatrix}
\cos \theta & -\sin \theta \\
\sin \theta & \cos \theta \\
\end{bmatrix}
$
Explicit calculation of derivative:
$\dfrac{\mathrm{d}}{\mathrm{d}\theta}|_{\theta=0}R_J(\theta)=
\begin{bmatrix}
-\sin 0 & -\cos 0 \\
\cos 0 & -\sin 0 \\
\end{bmatrix}=
\begin{bmatrix}
0 & -1\\
1 & 0 \\
\end{bmatrix} = J$
For rotation matrices like this example (some special phenomena for those anti-symmetric generators): $J=R_J(\tfrac{\pi}{2})$
Indeed, $\dfrac{\mathrm{d}}{\mathrm{d}\theta}|_{\theta=0}\left(R_J(\theta)x\right)=R_J(\tfrac{\pi}{2})x$ makes sense:
A vector rotating $x$ being rotated around the origin has a tangent tangential to $x$ itself.
Moreover,
$\theta\dfrac{1}{\sin(\theta)}\dfrac{R_J(\theta)-R_J(\theta)^T}{2}=\theta\dfrac{1}{\sin(\theta)}\dfrac{1}{2}\begin{bmatrix}
0 & -2\sin \theta \\
2\sin \theta & 0 \\
\end{bmatrix} = \begin{bmatrix}
0 & -\theta \\
\theta & 0 \\
\end{bmatrix}=\theta\cdot J=\log(\exp(\theta\cdot J))=\log(R_J(\theta))$
----
Example: Anti-symmetric $A$ in 3 dimensions (three independent coordinates).
Let's not consider this example in full generality, but instead let $A=\theta\cdot Z$, where
$
Z =
\begin{bmatrix}
0 & -1 & 0\\
1 & 0 & 0\\
0 & 0 & 0\\
\end{bmatrix}
$
$R_Z(\theta):=\exp(\theta Z)=
\begin{bmatrix}
\cos\theta & -\sin\theta & 0\\
\sin\theta & \cos \theta & 0\\
0 & 0 & 1\\
\end{bmatrix}
$
----
In general, for an $R_A$, there are 3 independent dimensions (e.g. Euler angles, or rotation vector).
Consider $A:=\log(R_A)$ and $K_A=A/\theta_A$.
From $R_A^{-1}=R_A^T$ it follows that $A$ and $K_A$ are skew-symmetric.
Define
$k_A := \left(K_{A,{3,2}}, K_{A,{1,3}}, K_{A,{2,1}}\right)$,
with $\vert\vert k_A\vert\vert^2=k_A\cdot k_A=1$
as well as
$v_A := \theta_A\cdot k_A = \left(A_{3,2}, A_{1,3}, A_{2,1}\right)$.
Note that $K_A=[k_A\times]$.
It's then a theorem (Euler) that the rotation is about an angle $\theta_A$
and the rotation axis is parallel to $k_Z$ and this is a unit vector.
And in fact (without proof here),
$R_A=\begin{bmatrix}
\cos \theta_A +k_{A,x}^2 \left(1-\cos \theta_A\right) & k_{A,x} k_{A,y} \left(1-\cos \theta_A\right) - k_{A,z} \sin \theta_A & k_{A,x} k_{A,z} \left(1-\cos \theta_A\right) + k_{A,y} \sin \theta_A \\
k_{A,y} k_{A,x} \left(1-\cos \theta_A\right) + k_{A,z} \sin \theta_A & \cos \theta_A + k_{A,y}^2\left(1-\cos \theta_A\right) & k_{A,y} k_{A,z} \left(1-\cos \theta_A\right) - k_{A,x} \sin \theta_A \\
k_{A,z} k_{A,x} \left(1-\cos \theta_A\right) - k_{A,y} \sin \theta_A & k_{A,z} k_{A,y} \left(1-\cos \theta_A\right) + k_{A,x} \sin \theta_A & \cos \theta_A + k_{A,z}^2\left(1-\cos \theta_A\right)\end{bmatrix}
$
$= \cos \theta_A\cdot I_{3\times 3} + \sin \theta_A \begin{bmatrix}
0 & - k_{A,z} & k_{A,y} \\
k_{A,z} & 0 & - k_{A,x} \\
- k_{A,y} & k_{A,x} & 0
\end{bmatrix}
+
\left(1-\cos \theta_A\right)\begin{bmatrix}
k_{A,x}^2 & k_{A,x} k_{A,y} & k_{A,x} k_{A,z} \\
k_{A,y} k_{A,x} & k_{A,y}^2 & k_{A,y} k_{A,z} \\
k_{A,z} k_{A,x} & k_{A,z} k_{A,y} & k_{A,z}^2
\end{bmatrix}
$
$
= \cos \theta_A\cdot I_{3\times 3} + \sin\theta_A\cdot K_A + (1-\cos\theta)\,k\otimes k
$
https://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula
A form of which, using ${\mathrm tr}(k\otimes k)=k_A\cdot k_A=1$, we can also easily read off that
$\tfrac{1}{2}({\mathrm tr}(R_A)-1)=\cos\theta_A$
and
$\frac{1}{\sin\theta_A}\tfrac{1}{2}(R_A-R_A^T) = K_A$.
Sidenote:
Let $\{e^0,e^1,e^2,e^3\}$ be the four quaternion standard basis vectors and $k=\sum_{i=1}^3k_ie^i$.
For quaternion $q_A:=\theta\,k$ and thus a general quaternion $q_B=a\,e^0 + q_A$, we get
$q_R:=\exp(q_A)=\sum_{j=0}^\infty\frac{1}{k!}q_B^j=\cos|\theta| \cdot e^0 + \sin|\theta| \, k$
and then, indeed, $\exp(q_B)={\mathrm e}^{a}q_R$ (polar decomposition).
And the quaternions $q$ with $\vert q\vert=1$ (=zero $e^0$-component $a$) indeed induce rotations
in the expected way with $\theta_A$ and $k_A$.
What's special with the quaternion representation (and sometimes surely source of confusion),
is that the $q_A$ is up to first order the same of that of $q_R$.
(It's different than other representations in that $q_R$ is just as simple as $q_A$.)
Then, with the non-zero components of $q_A$ (components here written as $i,j,k$) and $q_r$,
$R_A = I_{3\times 3} + 2
\begin{bmatrix}
- (q_j^2 + q_k^2) & q_iq_j - q_kq_r & q_iq_k + q_jq_r\\
q_iq_j + q_kq_r & - (q_i^2 + q_k^2) & q_jq_k - q_iq_r\\
q_iq_k - q_jq_r & q_iq_r + q_jq_k & - (q_i^2 + q_j^2)
\end{bmatrix}$.
Also, without proof, every rotation decomposes as
$R_A=
\begin{bmatrix}
\cos \alpha & -\sin \alpha & 0 \\
\sin \alpha & \cos \alpha & 0 \\
0 & 0 & 1 \\
\end{bmatrix}_\text{yaw}*
\begin{bmatrix}
\cos \beta & 0 & \sin \beta \\
0 & 1 & 0 \\
-\sin \beta & 0 & \cos \beta \\
\end{bmatrix}_\text{pitch}*
\begin{bmatrix}
1 & 0 & 0 \\
0 & \cos \gamma & -\sin \gamma \\
0 & \sin \gamma & \cos \gamma \\
\end{bmatrix}_\text{roll}
$
----
Back to the example, e.g. take
$R_Z(\tfrac{\pi}{2})=
\begin{bmatrix}
0 & -1 & 0\\
1 & 0 & 0\\
0 & 0 & 1\\
\end{bmatrix}
$
implies
$A=\tfrac{\pi}{2}Z=
\begin{bmatrix}
0 & -\tfrac{\pi}{2} & 0\\
\tfrac{\pi}{2} & 0 & 0\\
0 & 0 & 0\\
\end{bmatrix}$
so that
$v_Z = \left(0, 0, \tfrac{\pi}{2}\right) = \tfrac{\pi}{2}\cdot\left(0, 0, 1\right)$
i.e., here $\theta = \tfrac{\pi}{2}$ and $k = e_Z$.
----
Let
$R_Y(\tfrac{\pi}{2})=
\begin{bmatrix}
0 & 0 & 1\\
0 & 1 & 0\\
-1 & 0 & 0\\
\end{bmatrix}
$
$R_Z(\tfrac{\pi}{2})*R_Y(\tfrac{\pi}{2})=
\begin{bmatrix}
0 & -1 & 0\\
1 & 0 & 0\\
0 & 0 & 1\\
\end{bmatrix}*
\begin{bmatrix}
0 & 0 & 1\\
0 & 1 & 0\\
-1 & 0 & 0\\
\end{bmatrix}=
\begin{bmatrix}
0 & -1 & 0\\
0 & 0 & 1\\
-1 & 0 & 0\\
\end{bmatrix}$
$A_{ZY}=\log(R_Y(\tfrac{\pi}{2})*R_Z(\tfrac{\pi}{2}))=\frac{2\pi}{3}\cdot\frac{1}{\sqrt{3}}
\begin{bmatrix}
0 & -1 & 1\\
1 & 0 & 1\\
-1 & -1 & 0\\
\end{bmatrix}
$
$v_{ZY}=\frac{2\pi}{3}\cdot\frac{1}{\sqrt{3}}\left(-1, 1, 1\right)$
MatrixLog[{{0,-1,0},{1,0,0},{0,0,1}}.{{0,0,1},{0,1,0},{-1,0,0}}]/(2Pi/(3Sqrt[3]))//Simplify
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment