What Are Quaternions?
Quaternions are four-component numbers of the form q = a + bi + cj + dk, where a, b, c, d are real numbers and i, j, k are imaginary basis units. Sir William Hamilton introduced them in 1843 to describe rotations and transformations in three-dimensional space.
Unlike complex numbers (which have one imaginary unit), quaternions operate in a four-dimensional algebra where multiplication is non-commutative. This means q₁ × q₂ ≠ q₂ × q₁. Despite this quirk, quaternions form a division ring—every non-zero quaternion has a multiplicative inverse.
The key difference between quaternions and vectors: quaternions form an associative algebra, making them ideal for composing rotations without the singularities that plague Euler angles.
Quaternion Magnitude
The magnitude (or norm) of a quaternion measures its length in four-dimensional space. For a quaternion q = a + bi + cj + dk, compute it as:
|q| = √(a² + b² + c² + d²)
a— Real (scalar) componentb— Coefficient of the i basis unitc— Coefficient of the j basis unitd— Coefficient of the k basis unit
Core Quaternion Operations
Addition and subtraction are component-wise, like vector operations: add or subtract real parts together, i parts together, and so on. If q₁ = a + bi + cj + dk and q₂ = e + fi + gj + hk, then q₁ + q₂ = (a+e) + (b+f)i + (c+g)j + (d+h)k.
Multiplication requires care. Use the distributive property, then apply these rules: i² = j² = k² = −1, and ij = k, jk = i, ki = j, but ji = −k, kj = −i, ik = −j. The order matters.
Division of quaternion q₁ by q₂ is computed as q₁ ÷ q₂ = q₁ × q₂⁻¹, where q₂⁻¹ is the inverse of q₂.
Conjugate of q = a + bi + cj + dk flips the signs of imaginary parts: q* = a − bi − cj − dk.
Inverse (multiplicative) is q⁻¹ = q* / |q|², provided |q| ≠ 0.
Quaternions and 3D Rotation
A unit quaternion (magnitude = 1) encodes a rotation in three dimensions. Given a unit axis vector v_a = (x_a, y_a, z_a) and rotation angle θ, the rotation quaternion is:
q = cos(θ/2) + (x_a·i + y_a·j + z_a·k)·sin(θ/2)
To rotate a vector v = (x, y, z), convert it to pure quaternion form q_v = 0 + xi + yj + zk, apply the formula q_v' = q × q_v × q⁻¹, and extract the vector part: v' = (x', y', z').
This approach elegantly avoids gimbal lock and provides smooth interpolation between rotations (SLERP), making quaternions essential in robotics, aerospace, and game engines.
Practical Considerations
Watch for these common pitfalls when working with quaternions.
- Normalization for rotation — Only <strong>unit quaternions</strong> (magnitude exactly 1) represent pure rotations. Before using a quaternion for rotation calculations, normalize it by dividing each component by the magnitude. Non-unit quaternions will distort your geometry.
- Order matters in multiplication — Quaternion multiplication is non-commutative: <code>q₁ × q₂ ≠ q₂ × q₁</code>. When composing rotations, the order determines the final orientation. Reverse the order and you may rotate around the wrong axis or in the wrong direction.
- Angle halving in rotation formulas — Rotation quaternions use <code>θ/2</code>, not <code>θ</code> directly. This is a signature feature of quaternion algebra. Forgetting the factor of 2 will rotate your vector by twice the intended angle.
- Axis must be a unit vector — The rotation formula assumes your axis vector is normalized (length 1). If it isn't, compute the unit vector first: <code>u = v / |v|</code>. Using an unnormalized axis produces unexpected scaling alongside rotation.