Spatial rotations in three dimensions can be parametrized using both Euler angles and unit quaternions. This article explains how to convert between the two representations. Actually this simple use of "quaternions" was first presented by Euler some seventy years earlier than Hamilton to solve the problem of magic squares. For this reason the dynamics community commonly refers to quaternions in this application as "Euler parameters".
Contents 
A unit quaternion can be described as:
We can associate a quaternion with a rotation around an axis by the following expression
where α is a simple rotation angle (the value in radians of the angle of rotation) and cos(β_{x}), cos(β_{y}) and cos(β_{z}) are the "direction cosines" locating the axis of rotation (Euler's Theorem).
The orthogonal matrix (postmultiplying a column vector) corresponding to a clockwise/lefthanded rotation by the unit quaternion q = q_{0} + iq_{1} + jq_{2} + kq_{3} is given by the inhomogeneous expression
or equivalently, by the homogeneous expression
If q_{0} + iq_{1} + jq_{2} + kq_{3} is not a unit quaternion then the homogeneous form is still a scalar multiple of a rotation matrix, while the inhomogeneous form is in general no longer an orthogonal matrix. This is why in numerical work the homogeneous form is to be preferred if distortion is to be avoided.
The orthogonal matrix (postmultiplying a column vector) corresponding to a clockwise/lefthanded rotation with Euler angles φ,θ,ψ, with xyz convention, is given by:
By combining the quaternion representations of the Euler rotations we get
For Euler angles we get:
arctan and arcsin have a result between Pi/2 and Pi/2. With three rotation between Pi/2 and Pi/2 you can't have all the orientation. You need to replace the arctan by atan2 to generate all the orientation.
Similarly for Euler angles, we use the TaitBryan angles (in terms of flight dynamics):
where the Xaxis points forward, Yaxis to the right and Zaxis downward and in the example to follow the rotation occurs in the order yaw, pitch, roll (about bodyfixed axes). Nevertheless, it is not easy to find a matrix expression with TaitBryan angles because its final expression depends on how the rotations are applied.
One must be aware of singularities in the Euler angle parametrization when the pitch approaches 90° (north/south pole). These cases must be handled specially. The common name for this situation is gimbal lock.
Code to handle the singularities is derived on this site: www.euclideanspace.com
