I suspect this is a bit basic for mathoverflow, seeing I'm still just an undergraduate 


I've been playing around with quaternions as means to eliminate the gimbal lock. From what I understand, one place the gimbal lock occurs is when you rotate $\frac{\pi}{2}$ around the y-axis. If I create two rotation matrices, $R\_{1}$ rotates first $\phi$ around x-axis and $\frac{\pi}{2}$ around the y-axis, while $R\_{2}$ rotates first $\frac{\pi}{2}$ around the y-axis and then $\theta$ around the z-axis.


$\begin{equation}
R\_{1} = R\_{z}(0) R\_{y}(\frac{\pi}{2}) R\_{x}(\phi) \\
 = \begin{bmatrix}
 0 & 0 & 1 \\
 0 & 1 & 0 \\
 -1 & 0 & 0
\end{bmatrix} \begin{bmatrix}
 1 & 0 & 0 \\
 0 & \cos(\phi) & -\sin(\phi) \\
 0 & \sin(\phi) & \cos(\phi)
\end{bmatrix} \\
 = \begin{bmatrix}
 0 & \sin(\phi) & \cos(\phi) \\
 0 & \cos(\phi) & -\sin(\phi) \\
 -1 & 0 & 0
\end{bmatrix},
\end{equation}$


$\begin{equation}
R\_{2} = R\_{z}(\theta) R\_{y}(\frac{\pi}{2}) R\_{x}(0) \\
 = \begin{bmatrix}
 \cos(\theta) & -\sin(\theta) & 0 \\
 \sin(\theta) & \cos(\theta) & 0 \\
 0 & 0 & 1 \\
\end{bmatrix}
\begin{bmatrix}
 0 & 0 & 1 \\
 0 & 1 & 0 \\
 -1 & 0 & 0
\end{bmatrix} \\
 = \begin{bmatrix}
 0 & -\sin(\theta) & \cos(\theta) \\
 0 & \cos(\theta) & \sin(\theta) \\
 -1 & 0 & 0
\end{bmatrix}.
\end{equation} $


Since $R\_{1} = R\_{2}^{-1} \Rightarrow R\_{1}(\theta) = R\_{2}(-\theta)$, we've lost a degree of freedom. Which is what I expect.


From what I understand, if I perform the same rotations using quaternions, I should be avoiding the gimbal lock?


$ Q\_{1} = Q\_{z}(0) \times Q\_{y}(\frac{\pi}{2}) \times Q\_{x}(\theta) = (1, 0, 0, 0) \times (\frac{1}{\sqrt(2)}, 0, \frac{1}{\sqrt(2)}, 0) \times (\cos\frac{\theta}{2}, \sin\frac{\theta}{2}, 0, 0)\\ = \frac{1}{\sqrt(2)}(\cos\frac{\theta}{2}, \sin\frac{\theta}{2}, \cos\frac{\theta}{2}, -\sin\frac{\theta}{2})$


$ Q\_{2} = Q\_{z}(\phi) \times Q\_{y}(\frac{\pi}{2}) \times Q\_{x}(0) = (\cos\frac{\phi}{2}, 0, 0, \sin\frac{\phi}{2}) \times (\frac{1}{\sqrt(2)}, 0, \frac{1}{\sqrt(2)}, 0) \times (1, 0, 0, 0) \\
= \frac{1}{\sqrt(2)}(\cos\frac{\phi}{2}, -\sin\frac{\phi}{2}, \cos\frac{\phi}{2}, \sin\frac{\phi}{2})$


By setting $\phi = -\theta$, $Q\_{2}$ becomes


$ Q\_{2} = \frac{1}{\sqrt(2)}(\cos\frac{-\theta}{2}, -\sin\frac{-\theta}{2}, \cos\frac{-\theta}{2}, \sin\frac{-\theta}{2})$ which due to trig properies becomes 
$ Q\_{2} = \frac{1}{\sqrt(2)}(\cos\frac{\theta}{2}, \sin\frac{\theta}{2}, \cos\frac{\theta}{2}, -\sin\frac{\theta}{2})$


Which means that $Q\_{1}$ and $Q\_{2}$ rotates around the same axis only in the oppsite direction, and we've lost a degree of freedom (??). Am I missing something fundamental?



The gimbal lock shows up in my quaternions
There's no paradox here: you did the same calculation in two different ways and got the same answer, as you should. The issue is how to think about gimbal lock.


How should you represent a rotation in three dimensions? You can try using Euler angles to represent it using three rotation angles, but there's something fishy about this. That naturally parametrizes a three-dimensional torus, but the rotation group is not a torus (rather, it's a projective space). It doesn't even have a torus as a covering space, but rather a 3-sphere. So the problem is that the naive coordinates just don't give the right topology, and therefore something must go wrong in degenerate cases to fix the topology. Gimbal lock is essentially a name for what goes wrong.


When people say quaternions avoid gimbal lock, they mean the unit quaternions naturally form a 3-sphere, so there are no topology issues and they give a beautiful double cover of the rotation group (via a very simple map). Keeping track of a unit quaternion is fundamentally a more natural way to describe a rotation than keeping track of three Euler angles.


On the other hand, if you describe your quaternion via Euler angles, then gimbal lock shows up again, not in the quaternions themselves but in your coordinate system for them. That's what you are seeing in your calculations: you are doing a standard calculation to see the effects of gimbal lock, and then redoing the same calculation using quaternions.


Some explanations of gimbal lock don't distinguish clearly between the underlying geometry/topology and the choice of coordinates, which has always annoyed me, since that's essential for understanding what's going on mathematically.


