Extract the angle of rotation from a unit quaternion

Sorry for boring you my friends before the spring vacation. I am haunted by a simple problem of how to extract rotation angle from a unit quaternion.

Suppose $a$ is a unit quaternion which takes the form of: $$a = \left( \cos \left(\frac{\phi}{2} \right),\sin \left(\frac{\phi}{2}\right)\cdot \overrightarrow{n}\right)$$

$\phi$ is the angle of rotation and $\overrightarrow{n}$ is the axis of rotation.

I have seen in the books, the popular approach to extract the angle of rotation is via the inverse function of $sin$ or $cos$, but the result of inverse function remains valid and unique when the rotation is positive and small. Thus, I wonder if there is another approach in which the extracted angle could be extended in both directions (positive or negative) and in the scope of large rotation.

Thank you in advance for taking a look and giving a hint.

• What about $atan2(y,x)$? – crbah Apr 8 '16 at 11:26
• You can make the parentheses adjust to their content by using \left and \right. You can get $\sin$ and $\cos$ in the right font using \sin and \cos, and for operators that don't have a command of their own, there's \operatorname{name}. Here's a reference and tutorial for MathJax. – joriki Apr 8 '16 at 11:27
• Thank you for your advice. Sorry, I didn't clarify that the axis of rotation is unknown too. Thus, maybe, we could not apply $atan2$. – Zihan Shen Apr 8 '16 at 11:29
• I didn't get your point. Let's say you have $a = (a_1, a_2, a_4,a_4) = a_1 + a_2 \cdot i + a_3 \cdot j+ a_4 \cdot k$. Since the vector $n$ is a unit vector, we obtain $\sqrt{a_2^2 + a_3^2 + a_4^2} = \sin{\frac{\theta}{2}}$. Thus in the end we have $(a_1, \sqrt{a_2^2 + a_3^2 + a_4^2} )$ as $\cos$ and $\sin$ values of the angle. – crbah Apr 8 '16 at 11:55
• As the other commenters have said, atan2 with @corbah's components will work. I would also like to point out that $\theta\in [0,\pi]$ by definition, reducing the number of possible inverses for the trig functions. – SZN Jun 4 '16 at 1:33

This answer is mostly a compilation of the comments by crbah and SZN, but It's easier to see here. I almost had overlooked the comment.

$\phi = 2 \cdot \tan^{-1}\left(\frac{\sin{\left(\frac{\phi}{2}\right)}}{\cos{\left(\frac{\phi}{2}\right)}}\right)$
This is enough because $\phi$ has by definition only a range $\phi\in[0,\pi]$. If the angle were larger, we could use a rotation in the opposite direction instead. I believe this would be simply done by inverting the signs of all elements of the rotation axis $\vec{n}$ in your definition $$a = \left( \cos \left(\frac{\phi}{2} \right),\sin \left(\frac{\phi}{2}\right)\cdot \overrightarrow{n}\right)$$
$\cos{\left(\phi\right)}$ is only ambiguous if the angle $\phi$ is allowed to be negative, so I think it should be enough to use $\cos^{-1}{ \left(\cos{\left(\frac{\phi}{2}\right)}\right)}$ though. I don't know, that seems confusing to me, because $\tan^{-1} \in\left]-\frac{\pi}{2},\frac{\pi}{2}\right[$ while $\cos^{-1}\in\left[-\frac{\pi}{2},\frac{\pi}{2}\right]$.
If the sign of $\sin{\left(\frac{\phi}{2}\right)}$ is positive, we use $\phi=2\cdot cos^{-1}{\left(\cos{\frac{\phi}{2}}\right)}$ and if the sign of the sine is negative, we take the negative of this $\phi$ instead. (You can think about this by picturing the unit circle.)