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) component
  • b — Coefficient of the i basis unit
  • c — Coefficient of the j basis unit
  • d — 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.

  1. 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.
  2. 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.
  3. 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.
  4. 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.

Frequently Asked Questions

Can quaternions represent all 3D rotations?

Yes. Every rotation around an arbitrary axis passing through the origin maps to exactly two unit quaternions (q and −q). This two-to-one mapping is actually beneficial: it prevents ambiguity when interpolating between rotations. For axes not through the origin, combine quaternion rotation with vector translation to achieve the desired transformation.

What is quaternion conjugation and when do I use it?

The conjugate of <code>q = a + bi + cj + dk</code> is <code>q* = a − bi − cj − dk</code>. For unit quaternions, conjugation is equivalent to taking the inverse. Use conjugation when you need to reverse a rotation or compute <code>q⁻¹ = q* / |q|²</code>. It's computationally cheaper than full inverse calculation for normalized quaternions.

Why would I use quaternions instead of rotation matrices?

Quaternions use only four numbers versus nine for a 3×3 rotation matrix, reducing memory and computation. Quaternion multiplication is faster than matrix multiplication. Most importantly, quaternions sidestep gimbal lock entirely and enable smooth interpolation (SLERP) between orientations. Matrix-based rotations struggle with interpolation and singularities at certain angles.

How do I convert between Euler angles and quaternions?

This requires a bit of trigonometry. Given roll (φ), pitch (θ), yaw (ψ) in radians, compute: <code>w = cos(φ/2)cos(θ/2)cos(ψ/2) + sin(φ/2)sin(θ/2)sin(ψ/2)</code>, and similar formulae for x, y, z components. The reverse conversion involves arctangent functions. Many graphics libraries provide built-in conversion functions; use them rather than deriving formulas yourself to avoid sign and order errors.

Is a quaternion with zero magnitude valid?

No. A zero quaternion (all components equal to zero) has magnitude 0 and no multiplicative inverse. It cannot represent a rotation. In practical applications, always verify that your quaternion is non-zero before attempting division or computing its inverse. Normalized quaternions used for rotation should always have magnitude exactly 1.

How do quaternion rotations handle gimbal lock?

Gimbal lock occurs with Euler angles when two rotation axes align, collapsing from three degrees of freedom to two. Quaternions avoid this entirely because they don't decompose rotation into sequential axis-aligned steps. Instead, they represent the full rotation as a single four-dimensional entity, eliminating the singularities that plague Euler angles.

More math calculators (see all)