# Homogeneous transformation matrix (7)

Kinematics & Universal Numerics
July 28, 2019

## Conversion between rotation matrix and its axis-angle representation.

In the past two blogs, we have shown how to convert a rotation in the axis-angle representation $({\mathbf{d}},\theta)$ into the matrix representation. We summarize the steps here:

• Find $\phi\in[0,\pi],\,\psi\in(-\pi,\pi]$ that parametrize a normalized (unit) vector ${\mathbf{d}}$:

 ${\mathbf{d}}=\begin{bmatrix}d_{x}\\ d_{y}\\ d_{z}\end{bmatrix}=\begin{bmatrix}s_{\phi}c_{\psi}\\ s_{\phi}s_{\psi}\\ c_{\phi}\end{bmatrix}.$
• Use $\phi,\,\psi$ to construct

 $\displaystyle{\mathbf{S}}_{0}=\begin{bmatrix}s_{\psi}&c_{\phi}c_{\psi}&s_{\phi% }c_{\psi}\\ -c_{\psi}&c_{\phi}s_{\psi}&s_{\phi}s_{\psi}\\ &-s_{\phi}&c_{\phi}\end{bmatrix}.$ (1)
• The rotation matrix is hence

 $\displaystyle{\mathbf{R}}={\mathbf{S}}_{0}\begin{bmatrix}c_{\theta}&-s_{\theta% }\\ s_{\theta}&c_{\theta}\\ &&1\end{bmatrix}{\mathbf{S}}_{0}^{T}.$ (2)

Conversely, given a rotation matrix ${\mathbf{R}}=(r_{ij})$, how can we find its axis-angle representation $({\mathbf{d}},\theta)$? Assume $\lambda_{1},\,\lambda_{2},\,\lambda_{3}$ are the three eigenvalues of ${\mathbf{R}}$. Then for each eigenvalue of ${\mathbf{R}}$, $i=1,2,3$,

 $\displaystyle 0$ $\displaystyle=\det({\mathbf{R}}-\lambda_{i}{\mathbf{I}})$ $\displaystyle=\det({\mathbf{S}}_{0}\cdot\text{Rot}_{z}({\theta})\cdot{\mathbf{% S}}_{0}^{T}-\lambda_{i}{\mathbf{S}}_{0}{\mathbf{S}}_{0}^{T})$ $\displaystyle=\det\left({\mathbf{S}}_{0}\cdot(\text{Rot}_{z}({\theta})-\lambda% _{i}{\mathbf{I}})\cdot{\mathbf{S}}_{0}^{T}\right)$ $\displaystyle=\det({\mathbf{S}}_{0})\cdot\det(\text{Rot}_{z}({\theta})-\lambda% _{i}{\mathbf{I}})\cdot\det({\mathbf{S}}_{0}^{T})$ $\displaystyle=1\cdot\det(\text{Rot}_{z}({\theta})-\lambda_{i}{\mathbf{I}})\cdot 1$ $\displaystyle=\det(\text{Rot}_{z}({\theta})-\lambda_{i}{\mathbf{I}}),$

which means $\lambda_{i}$ is also an eigenvalue of $\text{Rot}_{z}({\theta})$. In other words, ${\mathbf{R}}$ is obtained by performing a similarity transform ${\mathbf{S}}_{0}$ on $\text{Rot}_{z}({\theta})$, so matrices ${\mathbf{R}}$ and $\text{Rot}_{z}({\theta})$ have the same eigenvalues.

The sum of eigenvalues of an $n\times n$ matrix ${\mathbf{A}}=(a_{ij})$ equals its matrix trace, that is the sum of diagonal elements of ${\mathbf{A}}$:

 $\text{tr}({\mathbf{A}})=\sum_{i=1}^{n}a_{ii}.$

Having the same eigenvalues, matrices ${\mathbf{R}}$ and $\text{Rot}_{z}({\theta})$ also have the same trace

 $r_{11}+r_{22}+r_{33}=\text{tr}({\mathbf{R}})=\lambda_{1}+\lambda_{2}+\lambda_{% 3}=\text{tr}(\text{Rot}_{z}({\theta}))=2c_{\theta}+1.$

Since entries of ${\mathbf{R}}$ are given, we can calculate $\theta$ by

 $\displaystyle\theta=\arccos\frac{r_{11}+r_{22}+r_{33}-1}{2}.$ (3)

To derive the rotation axis ${\mathbf{d}}={\mathbf{S}}_{0}{\mathbf{e}}_{1,3}$, we may first try to eliminate one of the two trigonometric terms $s_{\theta}$ and $c_{\theta}$. In our first attempt, we get rid of $c_{\theta}$ by taking the difference of ${\mathbf{R}}$ and its transpose:

 $\displaystyle{\mathbf{R}}-{\mathbf{R}}^{T}$ $\displaystyle={\mathbf{S}}_{0}\begin{bmatrix}c_{\theta}&-s_{\theta}\\ s_{\theta}&c_{\theta}\\ &&1\end{bmatrix}{\mathbf{S}}_{0}^{T}-{\mathbf{S}}_{0}\begin{bmatrix}c_{-\theta% }&-s_{-\theta}\\ s_{-\theta}&c_{-\theta}\\ &&1\end{bmatrix}{\mathbf{S}}_{0}^{T}$ $\displaystyle=2\cdot{\mathbf{S}}_{0}\begin{bmatrix}&-s_{\theta}\\ s_{\theta}\\ &&0\end{bmatrix}{\mathbf{S}}_{0}^{T}$ $\displaystyle=2s_{\theta}\cdot{\mathbf{S}}_{0}\begin{bmatrix}&-1\\ 1\\ &&0\end{bmatrix}{\mathbf{S}}_{0}^{T}.$ (4)

We claim this is exactly $2s_{\theta}\cdot{\mathbf{d}}_{\times}$, where “$(\cdot)_{\times}$” is an operator on a directional vector ${\mathbf{d}}$ such that

 ${\mathbf{d}}_{\times}=\begin{bmatrix}d_{x}\\ d_{y}\\ d_{z}\end{bmatrix}_{\times}:=\begin{bmatrix}&-d_{z}&d_{y}\\ d_{z}&&-d_{x}\\ -d_{y}&d_{x}\end{bmatrix}.$

Then the cross product can be also written as

 ${\mathbf{d}}\times{\mathbf{v}}={\mathbf{d}}_{\times}{\mathbf{v}}.$

To show (4) is indeed $2s_{\theta}\cdot{\mathbf{d}}_{\times}$, we first write (using subscripts in column major form)

 ${\mathbf{S}}_{0}=[{\mathbf{s}}_{1},\,{\mathbf{s}}_{2},\,{\mathbf{d}}]:=\begin{% bmatrix}s_{1,1}&s_{2,1}&d_{x}\\ s_{1,2}&s_{2,2}&d_{y}\\ s_{1,3}&s_{2,3}&d_{z}\end{bmatrix},$

and the matrix product becomes

 $\displaystyle{\mathbf{S}}_{0}\begin{bmatrix}&-1\\ 1\\ &&0\end{bmatrix}{\mathbf{S}}_{0}^{T}=[{\mathbf{s}}_{2},\,-{\mathbf{s}}_{1},\,{% \mathbf{0}}]\begin{bmatrix}{\mathbf{s}}_{1}^{T}\\ {\mathbf{s}}_{2}^{T}\\ {\mathbf{d}}^{T}\end{bmatrix}={\mathbf{s}}_{2}{\mathbf{s}}_{1}^{T}-{\mathbf{s}% }_{1}{\mathbf{s}}_{2}^{T}={\mathbf{d}}_{\times}.$

The last step can be verified by writing ${\mathbf{s}}_{2}{\mathbf{s}}_{1}^{T}-{\mathbf{s}}_{1}{\mathbf{s}}_{2}^{T}$ into the full $3\times 3$ matrix, for example, on position (1,2),

 $s_{2,1}s_{1,2}-s_{1,1}s_{2,2}=-d_{z}.$

So far we have shown

 $\displaystyle{\mathbf{R}}-{\mathbf{R}}^{T}=2s_{\theta}{\mathbf{d}}_{\times}=2s% _{\theta}\begin{bmatrix}&-d_{z}&d_{y}\\ d_{z}&&-d_{x}\\ -d_{y}&d_{x}\end{bmatrix}.$ (5)

We have three cases to discuss:

1. Case 1: $\theta\neq 0$ and $\theta\neq\pi$. This is the regular case: $s_{\theta}\neq 0$. Getting ${\mathbf{d}}$ is by simply matching both hand sides in (5):

 $\displaystyle{\mathbf{d}}=\begin{bmatrix}d_{x}\\ d_{y}\\ d_{z}\end{bmatrix}=\frac{1}{2s_{\theta}}\begin{bmatrix}r_{32}-r_{23}\\ r_{13}-r_{31}\\ r_{21}-r_{12}\end{bmatrix},$ (6)

where ${\mathbf{R}}=(r_{ij})$.

2. Case 2: $\theta=0$. Then the rotation is trivial so the rotation axis can be arbitrary. In such a case we choose the default ${\mathbf{d}}=\begin{bmatrix}0\\ 0\\ 1\end{bmatrix}$ in our convention.

3. Case 3: $\theta=\pi$. This occurs only when $(\text{tr}({\mathbf{R}})-1)/2=s_{\theta}=\sin\pi=-1$, so $\text{tr}({\mathbf{R}})=r_{11}+r_{22}+r_{33}=-1$. In this case, ${\mathbf{R}}={\mathbf{R}}^{T}$, so we cannot use (6) to determine ${\mathbf{d}}$. By plugging in ${\mathbf{S}}_{0}$ from (1) and $\theta=\pi$ in (2), we obtain

 ${\mathbf{R}}=\begin{bmatrix}-2s_{\phi}^{2}s_{\psi}^{2}-c_{2\phi}&s_{\phi}^{2}s% _{2\psi}&c_{\psi}s_{2\phi}\\ s_{\phi}^{2}s_{2\psi}&2s_{\phi}^{2}s_{\psi}^{2}-1&s_{\psi}s_{2\phi}\\ c_{\psi}s_{2\phi}&s_{\psi}s_{2\phi}&c_{2\phi}\end{bmatrix}.$

Then

 $\displaystyle\phi$ $\displaystyle=\frac{1}{2}\arccos r_{33}\in[0,\pi/2]$ $\displaystyle\psi$ $\displaystyle=\left\{\begin{array}[]{rl}\text{arbitrary}&\text{if \phi=0, so% }{\mathbf{d}}=\begin{bmatrix}0\\ 0\\ 1\end{bmatrix}\\ \displaystyle{\sqrt{\frac{r_{22}+1}{2}}}&\text{if \phi=\pi/2, so }{\mathbf{d% }}=\begin{bmatrix}c_{\psi}\\ s_{\psi}\\ 0\end{bmatrix}\\ \text{atan2}(r_{23},r_{13})&\text{if s_{2\phi}\neq 0, so \phi\neq 0 and % \phi\neq\pi/2}.\end{array}\right.$

We remind ourselves that

 ${\mathbf{d}}=\begin{bmatrix}s_{\phi}c_{\psi}\\ s_{\phi}s_{\psi}\\ c_{\phi}\end{bmatrix}.$