Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using unit quaternions to represent rotations.

Given rotation angles X, Y and Z, I can construct 3 quaternions that rotate around each unit axis. Qx = ( sin(X/2), cos(X/2), 0, 0 ) Qy = ( sin(Y/2), 0, cos(Y/2), 0 ) Qz = ( sin(Z/2), 0, 0, cos(Z/2) )

The final oreintation is calculated by Q = Qx * Qy * Qz;

I need to go in the opposite direction. I know Q, how do I calculate the angles X, Y and Z?

share|cite|improve this question
The problem looks underdetermined to me... – J. M. Sep 13 '11 at 11:07
Indeed, regardless of quaternions and as commented above, in the form given the problem is certainly underdetermined (as is well known, cp. Eulerian angles), e.g. already just taking $X:=Y:=Z:=\pi$ gives you $Qx=Qy=Qz=1_{\mathbb H}$ (trying to make sense of the notation, and what's the factor $\frac{1}{2}$ good for ?), which is also related to so-called "gimbal lock" - check Wiki for starters ! Kind regards - Stephan F. Kroneck. – bonnbaki Sep 13 '11 at 12:11
(ctd.) As far as I recall, one advantage of using quaternions for describing rotations is to avoid precisely this phenomenon (you're losing this by sticking to your angles). Kind regards - Stephan F. Kroneck. – bonnbaki Sep 13 '11 at 12:18
I'm driving a robot arm, which needs Euler angles to drive each of it's 3 degrees of freedom. Don't get me wrong, I love quaternions and wouldn't convert to angles if I didn't have to. However thanks for your input! – Hannesh Sep 14 '11 at 8:55
up vote 1 down vote accepted

As has already been written in the comments, there are singular points at which this inverse is ill-defined, and the point of using quaternions is to avoid these sorts of problems. In case you can't avoid this inversion (e.g. because you're using other people's code), here's an idea for how you might go about it:

With $\alpha=X/2$, $\beta=Y/2$, $\gamma=Z/2$, the quaternion $Q$ comes out as

$$ \begin{pmatrix} \cos\alpha\cos\beta\cos\gamma-\sin\alpha\sin\beta\sin\gamma\\ \sin\alpha\cos\beta\cos\gamma+\cos\alpha\sin\beta\sin\gamma\\ \cos\alpha\sin\beta\cos\gamma-\sin\alpha\cos\beta\sin\gamma\\ \sin\alpha\sin\beta\cos\gamma+\cos\alpha\cos\beta\sin\gamma\\ \end{pmatrix} \;. $$

Forming squares and combining them in various ways to simplify the trigonometric functions yields

$$ \begin{eqnarray} q_0^2-q_1^2-q_2^2+q_3^2&=&\cos X\cos Y\;,\\ q_0^2+q_1^2-q_2^2-q_3^2&=&\cos Y\cos Z\;,\\ q_0^2-q_1^2+q_2^2-q_3^2&=&\cos X\cos Z-\sin X\sin Y\sin Z \;. \end{eqnarray} $$

If you take $\sin X\sin Y\sin Z$ to one side in the last equation and square, you can express the squared sines by squared cosines and then substitute $\cos X$ and $\cos Z$ from the first two equations. This yields a cubic equation for $\cos^2 Y$. You'll still have to deal with all the usual issues about the signs and ranges of the angles, but this should at least solve the algebraic part of the problem. (Regarding the problem of equivalent angles, see also How can I find equivalent Euler angles?.)

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.