# Problem when reconstructing rotation from Euler angles

I have a rotation matrix R:

R=[[-0.22247682, -0.32863132, -0.91788099],
[-0.01426572, -0.94027818,  0.34010798],
[-0.9748336 ,  0.08876037,  0.20450194]]


and I want to decompose this into three rotations, $R_Z(\alpha)R_X(\beta)R_Z(\gamma)$, where I consider clockwise rotations. The rotation matrix in terms of these Euler angles is then given by

$R=$ \begin{bmatrix} \cos\alpha\;\cos\gamma\;-\;\sin\alpha\;\cos\beta\;\sin\gamma & -\sin\gamma\;\cos\alpha\;-\;\sin\alpha\;\cos\beta\;\cos\gamma\; & \sin\alpha\;\sin\beta \\ \sin\alpha\;\cos\gamma\;+\;\sin\gamma\;\cos\alpha\;\cos\beta\; & -\sin\alpha\;\sin\gamma\;+\;\cos\alpha\;\cos\beta\;\cos\gamma & -\cos\alpha\;\sin\beta\\ \sin\beta\;\sin\gamma & \sin\beta\;\cos\gamma & \cos\beta\\ \end{bmatrix}

Then, I calculate $\beta=\arccos(R_{33})=1.36$, $\alpha=\arctan(-R_{13}/R_{23})=1.22$, and $\gamma=\arctan(R_{31}/R_{32})=-1.48$. As a double check, I reconstruct the matrix R from these Euler angles, and find the following:

R'=[[ 0.22247682,  0.32863132, -0.91788099],
[ 0.01426572,  0.94027818,  0.34010798],
[ 0.9748336 , -0.08876037,  0.20450194]])


I double checked everything, but I cannot find what the problem is. What could I be missing?

Edit: It turns out the transpose problem was because my python code had some indexation problem. The sign issues still persist, however. I updated the matrix R' above.

• are you sure of consistency for CW/CCW direction ? – G Cab Jul 13 '17 at 21:17
• @GCab I think so, I derived the formula to double check. But in any case, the solution of the Euler angles come from the matrix I wrote above. – sodiumnitrate Jul 13 '17 at 21:18
• $R'=-R^T$, which suggests that you got something backwards in your computation. – amd Jul 13 '17 at 21:38
• Use ${\rm atan2}(y,x)$ function instead. – ja72 Jul 14 '17 at 1:15
• @ja72 As defined in C/C++? What does it do differently than numpy's arctan? – sodiumnitrate Jul 14 '17 at 1:30

I have confirmed the rotation matrix expression, but I would extract the angles a little differently than you.

• $\gamma = {\rm atan2}(R_{31},\,R_{32}) = -1.4799948857740634$
• $\beta = {\rm atan2}( \sqrt{R_{31}^2 +R_{32}^2 }, \,R_{33} ) = 1.3648414591977873$
• $\alpha = {\rm atan2}( R_{13},\, -R_{23} ) = -1.9256476493782756$

where atan2(dy,dx) is the arc tangent covering the entire unit circle.

The recustruction is thus

R = RZ(1.2159450042115176)*RX(1.3648414591977873)*RZ(-1.4799948857740634) $\checkmark$

1) Your calculation for the angles is correct, apart from the indetermination in the signs.

From $\cos\beta$ you can deduct $\pm \beta$. From $\tan\alpha$ you can deduct $(\alpha, \alpha -\pi,\alpha +\pi)$, and same for $\gamma$.

Then you shall check the assumptions with the other elements of the starting matrix.

Taking $\beta=1.36$ then $\cos\beta$ and $\sin\beta$ are positive. From $R[1,3]$ and $R[2,3]$ then follows that $\cos\alpha$ and$\sin\alpha$ shall be both negative: so $\alpha=1.22-\pi$.

For $\gamma=-1.48$ the signs of $R[3,1]$ and $R[3,2]$ are correct.

2) The elemental rotation matrices, defined according to this Wikipedia article therefore pre-multiplying, counter-clockwise and right hand in particular, are $${\bf R}_{\,{\bf x}} (\alpha ) = \left( {\matrix{ 1 & 0 & 0 \cr 0 & {\cos \alpha } & { - \sin \alpha } \cr 0 & {\sin \alpha } & {\cos \alpha } \cr } } \right)\quad {\bf R}_{\,{\bf y}} (\beta ) = \left( {\matrix{ {\cos \beta } & 0 & {\sin \beta } \cr 0 & 1 & 0 \cr { - \sin \beta } & 0 & {\cos \beta } \cr } } \right)\quad {\bf R}_{\,{\bf z}} (\gamma ) = \left( {\matrix{ {\cos \gamma } & { - \sin \gamma } & 0 \cr {\sin \gamma } & {\cos \gamma } & 0 \cr 0 & 0 & 1 \cr } } \right)$$

3) Applying these matrices and the corrected angles, I get that $${\bf R}_{\,{\bf z}} (\alpha )\;{\bf R}_{\,{\bf x}} (\beta )\;{\bf R}_{\,{\bf z}} (\gamma )$$ correctly returns the starting matrix.

The result you are obtaining, apart from signs, is the transposed of the original matrix, meaning that probably you are not using the correct elemental matrices

• Thanks. Shouldn't there be two cases: $\{\alpha,\alpha+\pi\}$? – sodiumnitrate Jul 14 '17 at 1:10
• @sodiumnitrate: it depends on the convention adopted for angle range: I am considering the $-\pi < \alpha \le \pi$. By the way did you discover why you are getting $\approx$ transpose ? – G Cab Jul 14 '17 at 14:13
• Shouldn't $\cos(\alpha-\pi)=\cos(\alpha+\pi)$ because $\cos(x)$ is $2\pi$-periodic? The transpose was an indexation error in my python code where I was assigning the matrix elements. – sodiumnitrate Jul 14 '17 at 21:07
• @sodiumnitrate: of course $\cos(\alpha-\pi)=\cos(\alpha+\pi)$. Now $\arccos(\alpha)$ is (usually) restituted with $0 \le \alpha \le \pi$ right ? If $\cos(\alpha)=-1/2$ and $\sin(\alpha)<0$ what angle do you take , $4/3 \pi$ or $-2/3 \pi$ ? as told above I am following the convention $-\pi < \alpha \le \pi$ – G Cab Jul 14 '17 at 23:59