Skip to main content
In the plane, one imaginary unit is enough for rotation. As shown in the complex-numbers section, multiplication by eiθe^{i\theta} turns a point through θ\theta. A three-dimensional rotation also needs an axis. Quaternions extend complex numbers to represent both the angle and the axis. Robot software often uses them because four numbers store a rotation compactly and make composition inexpensive. This presentation follows appendix B.3 of Lynch and Park (Kevin M. Lynch, 2017) and the discussion of the group S3S^3 by Solà, Deray and Atchuthan (Solà et al., 2018).

Three imaginary units

A quaternion consists of one real part and three imaginary parts: q=w+x i+y j+z k,q = w + x\,i + y\,j + z\,k , Each imaginary unit squares to −1-1: i2=j2=k2=ijk=−1.i^2 = j^2 = k^2 = ijk = -1 . These rules give the pairwise products ij=kij = k, jk=ijk = i and ki=jki = j, along with ji=−kji = -k, kj=−ikj = -i and ik=−jik = -j. Reversing the order changes the sign. Quaternion multiplication does not commute, just as rotations about different axes do not commute. Complex multiplication cannot represent this behavior, but quaternion multiplication can. Following Lynch and Park, this section places the real part first and writes q=(w,x,y,z)q = (w, x, y, z). The real part ww is the scalar part, and (x,y,z)(x, y, z) is the vector part. If w=0w = 0, the quaternion is pure. Pure quaternions represent ordinary 3-D vectors.

A unit axis behaves like i

Let the unit vector u=(ux,uy,uz)u = (u_x, u_y, u_z) specify the rotation axis. Write it as the pure quaternion u^=ux i+uy j+uz k.\hat u = u_x\,i + u_y\,j + u_z\,k . When you square it, the mixed terms cancel in pairs because ij=−jiij = -ji. The remaining terms give u^2=−(ux2+uy2+uz2)=−1.\hat u^2 = -\left(u_x^2 + u_y^2 + u_z^2\right) = -1 . A unit axis squares to −1-1, as ii does. You can extend the planar formula eiθ=cos⁡θ+isin⁡θe^{i\theta} = \cos\theta + i\sin\theta to any axis. A rotation by θ\theta about the axis uu has the unit quaternion q=cos⁡θ2+u^ sin⁡θ2that is,q=(cos⁡θ2,  u sin⁡θ2)q = \cos\frac{\theta}{2} + \hat u\,\sin\frac{\theta}{2} \qquad\text{that is,}\qquad q = \Big(\cos\frac{\theta}{2},\; u\,\sin\frac{\theta}{2}\Big) (Lynch and Park eq. B.9). The sum of the squared components is cos⁡2θ2+sin⁡2θ2=1\cos^2\frac{\theta}{2} + \sin^2\frac{\theta}{2} = 1, hence the term unit quaternion. The rotation formula below explains the half angle.

Rotating a vector

To rotate the vector p=(px,py,pz)p = (p_x, p_y, p_z), first represent it by the pure quaternion pxi+pyj+pzkp_x i + p_y j + p_z k. Then multiply it on both sides: p′=q p q∗,q∗=w−x i−y j−z k.p' = q\, p\, q^* , \qquad q^* = w - x\,i - y\,j - z\,k . The conjugate q∗q^* reverses the sign of the vector part, much as complex conjugation reverses the sign of iyiy. For a unit quaternion, the conjugate is also the inverse, q∗q=1q^* q = 1. Multiplication by qq on the left and its conjugate on the right doubles the angle held in qq. The quaternion must therefore store θ/2\theta/2. Consider a quarter turn. Rotate p=(1,0,0)p = (1, 0, 0) by 90∘90^\circ about the zz axis. In this case, q=cos⁡45∘+ksin⁡45∘=12(1+k)q = \cos 45^\circ + k \sin 45^\circ = \frac{1}{\sqrt 2}(1 + k), and direct multiplication gives q i q∗=12(1+k) i (1−k)=12(i+ki)(1−k)=12(i+j)(1−k)=12(i−ik+j−jk)=12(i+j+j−i)=j.q\, i\, q^* = \tfrac{1}{2}(1 + k)\, i\, (1 - k) = \tfrac{1}{2}(i + ki)(1 - k) = \tfrac{1}{2}(i + j)(1 - k) = \tfrac{1}{2}(i - ik + j - jk) = \tfrac{1}{2}(i + j + j - i) = j . The calculation maps (1,0,0)(1, 0, 0) to (0,1,0)(0, 1, 0), a quarter turn about zz.

The double cover

Replace qq by −q-q. In the product p′=(−q) p (−q)∗p' = (-q)\,p\,(-q)^*, the two minus signs cancel. Thus qq and −q-q produce the same rotation. Every rotation corresponds to these two unit quaternions. Unit quaternions satisfy w2+x2+y2+z2=1w^2 + x^2 + y^2 + z^2 = 1. These points form the 3-sphere S3S^3, a sphere in four dimensions. They also form a group under quaternion multiplication. Since qq and −q-q represent one rotation, S3S^3 covers SO(3) twice. You obtain SO(3) from S3S^3 by identifying every pair of opposite points. Solà et al. therefore call S3S^3 a double cover of SO(3). Both groups have the same tangent space, so angular velocities, small rotations and uncertainties have the same form in either group. The double cover affects numerical work. When a filter or controller compares two quaternions, it should treat qq and −q-q as equal. Interpolation between orientations should follow the shorter path. This may require changing the sign of one quaternion first.

Using quaternions

Composition. If you apply q1q_1 first and q2q_2 second, the combined rotation is q2q1q_2 q_1. For q=(q0,q1,q2,q3)q = (q_0, q_1, q_2, q_3) and p=(p0,p1,p2,p3)p = (p_0, p_1, p_2, p_3), the components of qpqp are qp=[q0p0−q1p1−q2p2−q3p3q0p1+p0q1+q2p3−q3p2q0p2+p0q2−q1p3+q3p1q0p3+p0q3+q1p2−q2p1]qp = \begin{bmatrix} q_0 p_0 - q_1 p_1 - q_2 p_2 - q_3 p_3 \\ q_0 p_1 + p_0 q_1 + q_2 p_3 - q_3 p_2 \\ q_0 p_2 + p_0 q_2 - q_1 p_3 + q_3 p_1 \\ q_0 p_3 + p_0 q_3 + q_1 p_2 - q_2 p_1 \end{bmatrix} (Lynch and Park eq. B.15). This quaternion product corresponds to the matrix product RqRpR_q R_p. To a rotation matrix. The unit quaternion (q0,q1,q2,q3)(q_0, q_1, q_2, q_3) produces the matrix R=[q02+q12−q22−q322(q1q2−q0q3)2(q0q2+q1q3)2(q0q3+q1q2)q02−q12+q22−q322(q2q3−q0q1)2(q1q3−q0q2)2(q0q1+q2q3)q02−q12−q22+q32]R = \begin{bmatrix} q_0^2 + q_1^2 - q_2^2 - q_3^2 & 2(q_1 q_2 - q_0 q_3) & 2(q_0 q_2 + q_1 q_3) \\ 2(q_0 q_3 + q_1 q_2) & q_0^2 - q_1^2 + q_2^2 - q_3^2 & 2(q_2 q_3 - q_0 q_1) \\ 2(q_1 q_3 - q_0 q_2) & 2(q_0 q_1 + q_2 q_3) & q_0^2 - q_1^2 - q_2^2 + q_3^2 \end{bmatrix} (Lynch and Park eq. B.12). Each entry is quadratic in qq, so replacing qq by −q-q leaves RR unchanged. Cost. Composing two rotation matrices requires 27 multiplications and 18 additions. Composing two quaternions requires 16 multiplications and 12 additions. You can correct small numerical errors by rescaling qq to length 1. A rotation matrix that has drifted must be re-orthogonalized. Compared with other representations.
Storage order. Different systems use different component orders. Lynch and Park place the scalar first, (w,x,y,z)(w, x, y, z), as this section does. Newton and Warp and ROS 2 use (x,y,z,w)(x, y, z, w), with the vector part first and scalar last. Check both conventions before passing a quaternion between systems.

References

  • Kevin M. Lynch, F. (2017). Modern Robotics.
  • Solà, J., Deray, J., Atchuthan, D. (2018). A micro Lie theory for state estimation in robotics. arXiv [cs.RO].

Found an error on this page? Report an issue.