Numbers and Arithmetic

Quaternions, the Four-Dimensional Numbers Behind 3D Rotation

Learn the rules of quaternion arithmetic, work through a rotation by hand, and see why graphics and spacecraft software rely on them.

Key takeaways

  • A quaternion has one real part and three imaginary parts that obey i² = j² = k² = ijk = −1.
  • Quaternion multiplication is associative but not commutative, since ij = k while ji = −k.
  • A unit quaternion built from half the rotation angle rotates a vector v through the product q v q⁻¹.
  • Computer graphics, robotics and spacecraft attitude control use quaternions because they are compact and avoid gimbal lock.

Quaternions are numbers of the form a + bi + cj + dk, where a, b, c and d are real numbers and i, j and k are three different square roots of −1. Because each has four real components, they are called four-dimensional.

Their great practical use is describing rotations in ordinary three-dimensional space. A rotation about any axis by any angle can be packed into one quaternion, and combining rotations becomes a single multiplication. The price is that the order of multiplication matters.

Complex numbers handle rotation in a plane: multiplying by i turns a point through a right angle. The Irish mathematician William Rowan Hamilton wanted something similar for three-dimensional space.

He tried for years with "triplets" a + bi + cj and could not find a multiplication that behaved well. On 16 October 1843, while walking beside the Royal Canal in Dublin, he saw that he needed three imaginary units instead of two, and he cut the key formula into the stonework of Brougham Bridge:

i² = j² = k² = ijk = −1

The original carving has not survived, but a plaque marks the place. The insight required giving up a law that everyone had taken for granted, that a × b = b × a.

The multiplication rules

Everything follows from Hamilton's formula. Start with ijk = −1 and multiply on the right by k. Since k² = −1, you get −ij = −k, so ij = k.

Product Result Reversed order Result
i j k j i −k
j k i k j −i
k i j i k −j

A handy memory aid is the loop i → j → k → i. Multiplying two neighbors in the forward direction gives the third. Going backward gives the third with a minus sign.

Real numbers commute with everything. Otherwise you expand brackets as usual while keeping the order of the units.

(1 + 2i)(3j) = 3j + 6ij = 3j + 6k
(3j)(1 + 2i) = 3j + 6ji = 3j − 6k

The two answers differ, which shows that the failure of commutativity is not a technicality.

Conjugates, lengths and inverses

The conjugate of q = a + bi + cj + dk is q* = a − bi − cj − dk. Multiplying the two gives a real number:

q q* = a² + b² + c² + d²

The square root of that is the length, or norm, written |q|. It satisfies |pq| = |p| × |q|.

Every nonzero quaternion has an inverse, q⁻¹ = q*/|q|². So you can divide, as long as you say whether you are dividing on the left or the right. A quaternion with length 1 is called a unit quaternion, and for it the inverse is simply the conjugate.

For example, q = 1 + 2i + 2j + 4k has |q|² = 1 + 4 + 4 + 16 = 25, so |q| = 5 and q⁻¹ = (1 − 2i − 2j − 4k)/25.

Vectors hiding inside

A quaternion with zero real part, bi + cj + dk, can stand for the vector (b, c, d). Multiply two of these, u and v, and something familiar appears:

u v = −(u · v) + (u × v)

Here u · v is the dot product, a number that measures alignment, and u × v is the cross product, a vector perpendicular to both.

Later in the nineteenth century, Josiah Willard Gibbs and Oliver Heaviside separated these two pieces into the vector analysis that physics students learn today. For a while, supporters of the two approaches argued sharply about which was better. Vectors won in most textbooks, and quaternions later returned in computing. The words "scalar" and "vector" were both given currency by Hamilton's quaternion work.

How a quaternion rotates a vector

Suppose you want to rotate by an angle θ about an axis given by a unit vector n, written as a quaternion with no real part. Build

q = cos(θ/2) + sin(θ/2) n

Then a vector v is rotated to q v q⁻¹. The direction of turning follows the right-hand rule around n.

Rotating a vector with a quaternion
1 Choose axis and angle A unit vector n and an angle θ. 2 Halve the angle q = cos(θ/2) + sin(θ/2) n. 3 Invert For a unit quaternion, flip the sign of the vector part. 4 Sandwich Compute q v q⁻¹. The result is the rotated vector. Choose axis and angle A unit vector n and an angle θ. 1 Halve the angle q = cos(θ/2) + sin(θ/2) n. 2 Invert For a unit quaternion, flip the sign of the vector part. 3 Sandwich Compute q v q⁻¹. The result is the rotated vector. 4

A worked example

Rotate the x-axis direction, v = i, by 90° about the z-axis, n = k. Half the angle is 45°, and cos 45° = sin 45° = 1/√2.

q = (1 + k)/√2          q⁻¹ = (1 − k)/√2

q i q⁻¹ = ½ (1 + k) i (1 − k)
(1 + k) i = i + ki = i + j
(i + j)(1 − k) = i − ik + j − jk
               = i + j + j − i        since ik = −j and jk = i
               = 2j
½ × 2j = j

The x-axis direction has turned into the y-axis direction, exactly what a quarter turn about z should do.

Why half the angle? The vector is multiplied by q on one side and q⁻¹ on the other, and each contributes half of the turn. Using the full angle on one side alone would push vectors out of three-dimensional space into the fourth component.

Combining rotations

To rotate first by q₁ and then by q₂:

q₂ (q₁ v q₁⁻¹) q₂⁻¹ = (q₂ q₁) v (q₂ q₁)⁻¹

So the combined rotation is the single quaternion q₂q₁. Note the order: the first rotation sits on the right.

Non-commutativity now looks entirely reasonable. Take a book, turn it 90° about a vertical axis and then 90° about a horizontal one. Reset it and do the same turns in the opposite order. The book ends in a different position. Any number system that represents 3D rotations faithfully has to reflect that.

One curiosity: q and −q give the same rotation, because the two minus signs cancel in the sandwich. Every rotation has two quaternion names.

A second example with an unexpected axis

Rotations about slanted axes are where quaternions shine. Consider the axis pointing equally along x, y and z, so n = (i + j + k)/√3, and rotate by 120°.

Half the angle is 60°, with cos 60° = ½ and sin 60° = √3/2. Then

q = ½ + (√3/2)(i + j + k)/√3
  = (1 + i + j + k)/2

Check the length: four terms of ½, so |q|² = 4 × ¼ = 1. Now rotate v = i.

(1 + i + j + k) i = i − 1 − k + j      = −1 + i + j − k
(−1 + i + j − k)(1 − i − j − k):
   −1 × (...)  = −1 + i + j + k
    i × (...)  =  i + 1 − k + j
    j × (...)  =  j + k + 1 − i
   −k × (...)  = −k + j − i − 1
   total       =  4j
¼ × 4j = j

So x goes to y. Repeating the calculation shows that y goes to z and z goes to x. The 120° turn about this diagonal simply cycles the three axes, which you can confirm by holding a cube at opposite corners and turning it.

Why programmers prefer them

There are several common ways to store a rotation.

  • Euler angles use three successive angles. They are easy to read but suffer from gimbal lock, where two of the axes line up and one degree of freedom is temporarily lost.
  • Rotation matrices use nine numbers with constraints. Rounding errors can slowly distort them, and restoring the constraints takes some work.
  • Unit quaternions use four numbers with one constraint. After many multiplications you just rescale to length 1.

Quaternions also interpolate smoothly. A method called spherical linear interpolation moves from one orientation to another at a steady rate, which is useful for animating a camera or a character's joint. Flight software, drones, phone orientation sensors and game engines commonly use them.

Practical cautions

If you use a quaternion library, check its conventions before trusting results. Some store the components in the order (a, b, c, d) with the real part first, and others put the real part last. Some libraries use a different sign convention for the product, or interpret the rotation as turning the coordinate frame instead of the object. Mixing conventions gives answers that look plausible and are wrong.

A reliable test is the one worked above: rotate the x-axis by 90° about z and confirm that you get the y-axis. Also keep your quaternions at unit length, since a quaternion whose length has drifted no longer has its conjugate as its exact inverse.

Quaternions are not the best tool for everything. They represent rotation but not translation or scaling, so graphics systems often convert them to matrices when combining all three. If you want to see how matrices handle such transformations, the overview of linear algebra is a good next stop.

Where they sit among number systems

Frobenius proved in 1877 that if you want a finite-dimensional number system over the reals that is associative and allows division by every nonzero element, there are only three choices: the real numbers, the complex numbers and the quaternions. Hamilton's triplets could not exist.

If you also give up associativity, you can reach the eight-dimensional octonions. Systems with a length that multiplies, as in |pq| = |p||q|, occur only in dimensions 1, 2, 4 and 8.

That multiplying length has a number-theory payoff. It gives an identity showing that a product of two sums of four squares is again a sum of four squares, a key step toward Lagrange's four-square theorem.

Quaternions were also one of the first signs that algebra could be designed, with rules chosen to fit a purpose. That outlook grew into the study of rings, fields and related structures.

Written for 4Mathematics with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 38 articles in Numbers and Arithmetic →

Latest news on this topic