# Combining two rotations

I'm working on a project, where I have to perform rotations of a point which is on the surface of a sphere of radius 1, around the center of the sphere.

In order to do so, I have a function, let's call it $U$, that is parametrized by 3 angles (real numbers) $\theta$, $\phi$ and $\lambda$, and that performs the following operations on its input: it performs a rotation on $z$ of $\phi$ radians, then a rotation over $y$ of $\theta$ radians, and finally a last rotation, again on $z$, of $\lambda$ radians.

In short: $U(\theta, \phi, \lambda) := R_z(\phi) R_y(\theta) R_z(\lambda)$.

Now here is my question: if I successively apply U with angles $\theta_1, \phi_1, \lambda_1$, and then with other angles $\theta_2, \phi_2, \lambda_2$, is there a method to find which angles $\theta, \phi, \lambda$ would have led to the same rotation in only one application of the function, and if so, how ?

• By $R_z(\phi)$ do you mean $R_x(\phi)$? – Arnaud Mortier Aug 5 '18 at 17:23
• Yes, you can find a solution, but that might not be unique. – Andrei Aug 5 '18 at 17:31
• Arnaud Mortier: No, it really is $R_z(\phi)$. I am taking that $U$ function from a paper, and have to stick with it. – 4rzael Aug 5 '18 at 19:00
• And @Andrei: The non-uniqueness of the result is not a problem (a deterministic solution to the problem would be preferable, in this case, though) – 4rzael Aug 5 '18 at 19:06

You can express rotations as matrices. Their combination is found by matrix multiplication.

it performs a rotation on $z$ of $\phi$ radians, then a rotation over $y$ of $\theta$ radians, and finally a last rotation, again on $z$, of $\lambda$ radians.

we have:

\begin{align} U &= R_z(\lambda) \, R_y(\theta) \, R_z(\phi) \\ &= \begin{pmatrix} \cos\lambda & -\sin\lambda & 0 \\ \sin\lambda & \cos\lambda & 0 \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} \cos\theta & 0 & -\sin\theta \\ 0 & 1 & 0 \\ \sin\theta & 0 & \cos\theta \end{pmatrix} \begin{pmatrix} \cos\phi & -\sin\phi & 0 \\ \sin\phi & \cos\phi & 0 \\ 0 & 0 & 1 \end{pmatrix} \\ &= \begin{pmatrix} \cos\lambda & -\sin\lambda & 0 \\ \sin\lambda & \cos\lambda & 0 \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} \cos\theta \cos\phi & -\cos\theta \sin\phi & -\sin\theta \\ \sin\phi & \cos\phi & 0 \\ \sin\theta \cos\phi & -\sin\theta \sin\phi & \cos\theta \end{pmatrix} \\ &= \begin{pmatrix} \cos\lambda \cos\theta \cos\phi - \sin\lambda \sin\phi & -\cos\lambda \cos\theta \sin\phi - \sin\lambda \cos\phi & -\cos\lambda \sin\theta \\ \sin\lambda \cos\theta \cos\phi + \cos\lambda \sin\phi & -\sin\lambda \cos\theta \sin\phi + \cos\lambda \cos\phi & -\sin\lambda \sin\theta \\ \sin\theta \cos\phi & -\sin\theta \sin\phi & \cos\theta \end{pmatrix} \end{align} This results in a single $3 \times 3$ matrix.

If you have two of those transformations $U_1$ and $U_2$ then the composition is given by the matrix product $$U_2 U_1$$ One would have to calculate the resulting matrices and then check if it is possible to easily read the angles from the result.

• I already did the matrix transformation, but have no clue how to read the angles from the resulting matrix – 4rzael Aug 5 '18 at 19:01
• You can calculate $U_2 U_1$ for your given parameter sets and then try to fit that to a new matrix $U$. E.g. the last element is $\cos \theta$ and you should get the resulting $\theta=\arccos U_{33}$. Then use e.g. $U_{23}$ and $U_{32}$ to recover the resulting $\lambda$ and $\phi$. It is possible that there are several solutions. – mvw Aug 5 '18 at 19:08
• Note that you have a sign issue. In the rotation matrix around $y$ axis the $+\sin$ is in the upper corner. – Andrei Aug 5 '18 at 19:19
• @Andrei: You could be right, if I compare to this. No I idea what 4rzael uses. I would suggest to check the calculation anyways. :-) – mvw Aug 5 '18 at 19:22
• @mvw That's exactly what I was looking at :) – Andrei Aug 5 '18 at 19:23

Start from the rotation matrix in terms of Euler angles. $$R(\phi,\theta,\lambda)=\begin{pmatrix}R_{11}& R_{12} &R_{13}\\R_{21}& R_{22} &R_{23}\\R_{31}& R_{32} &R_{33}\end{pmatrix}$$

First calculate the inner most angle. If $R_{31}$ and $R_{32}$ are both zero, use $\lambda=0$. Otherwise $$\tan\lambda=\frac{R_{32}}{-R_{31}}$$ You can use the $\arctan2$ function $$\lambda=\arctan2 (R_{32},-R_{31})$$

Now calculate $$R'(\phi,\theta)=R(\phi,\theta,\lambda)R^{-1}_z(\lambda)=R(\phi,\theta,\lambda)R_z(-\lambda)$$

If you do the calculations right, you get $\phi=\arctan2(-R'_{12},R'_{22})$ and $\theta=\arctan2(-R'_{31},R'_{33})$

A lot of details about this subject are in the Wikipedia article Euler angles. It seems to me that Euler angles are not a good way to parametrize 3D rotations. As an alternative, you can use quaternions as in the Wikipedia article Quaternion and spatial rotations and just multiply quaternions in order to perform 3D rotations. An ambiguity is that a quaternion and its negative give the same rotation.