Rotations

Rotations by quaternions

A quaternion turns space: v↦qvq−1v \mapsto q v q^{-1}. With rational quaternions every rotation matrix, every composed rotation and every rotated point is exact, and no square root or sine is ever taken.

The rotation of a quaternion

Treat a vector (x,y,z)(x, y, z) as the quaternion xi+yj+zkxi + yj + zk. For a nonzero q=w+(ai+bj+ck)q = w + (a i + b j + c k), the map v↦qvq−1v \mapsto q v q^{-1} sends vectors to vectors and is a rotation about the axis (a,b,c)(a, b, c) by the angle θ\theta with

tan⁡θ2=a2+b2+c2w,cos⁡θ=w2−(a2+b2+c2)w2+(a2+b2+c2). \tan\frac{\theta}{2} = \frac{\sqrt{a^2 + b^2 + c^2}}{w}, \qquad \cos\theta = \frac{w^2 - (a^2 + b^2 + c^2)}{w^2 + (a^2 + b^2 + c^2)}.

Multiplying qq by a nonzero real number changes nothing, since it cancels against q−1q^{-1}. So qq does not have to have norm 1, and that is what keeps everything rational: a unit quaternion for a quarter turn needs 2\sqrt2, but 1+k1 + k is the same rotation.

import { quaternion, rotate, rotationMatrix, rotationOf, fromAxis } from "@ratmath/complex/cayley";

rotate("1 + k", [1, 0, 0]).map(String);          // ["0", "1", "0"]: a quarter turn about z

rotationMatrix(quaternion(1, 2, 3, 4));          // rows of Rationals:
//  -2/3   2/15  11/15
//   2/3  -1/3    2/3
//   1/3  14/15   2/15

String(fromAxis([1, 1, 1], 1));                  // "1 + i + j + k"
rotate(fromAxis([1, 1, 1], 1), [1, 0, 0]).map(String);   // ["0", "1", "0"]: x → y → z → x
String(rotationOf("1 + i + j + k").cos);         // "-1/2": a third of a turn

fromAxis(axis, t) is 1+t (axis)1 + t\,(\text{axis}), so for a unit axis tt is the tangent of half the angle: t=1t = 1 is a quarter turn, t=1/2t = 1/2 turns by the angle of the 3-4-5 triangle. rotationOf(q) returns the axis with cos⁡θ\cos\theta and tan⁡2(θ/2)\tan^2(\theta/2), both rational.

Rational rotation matrices

The matrix of v↦qvq−1v \mapsto q v q^{-1} has entries that are quadratic in w,a,b,cw, a, b, c divided by N(q)N(q), so a rational quaternion gives an orthogonal matrix with rational entries and determinant 1. The converse holds too: every rational rotation matrix comes from a rational quaternion, and fromRotationMatrix finds it with no square roots, returning the primitive one (whole numbers with no common factor).

import { fromRotationMatrix, primitive } from "@ratmath/complex/cayley";

String(fromRotationMatrix([[0, 0, 1], [1, 0, 0], [0, 1, 0]]));   // "1 + i + j + k"
String(fromRotationMatrix(rotationMatrix(quaternion(1, 2, 3, 4))));   // "1 + 2i + 3j + 4k"
String(primitive(quaternion("-1/2", 0, "3/4", 0)));               // "2 - 3j"
fromRotationMatrix([[1, 0, 0], [0, 1, 0], [0, 0, -1]]);           // throws: a reflection

This is the three-dimensional version of rational points on the circle. There, (m+ni)2/(m2+n2)(m + ni)^2/(m^2 + n^2) gives every rational rotation of the plane from a pair of whole numbers; here four whole numbers give every rational rotation of space, a fact Euler found while looking for exactly such matrices.

Composition

Doing bb and then aa is the product abab, because a(bvb−1)a−1=(ab)v(ab)−1a(bvb^{-1})a^{-1} = (ab)v(ab)^{-1}. Rotations of space do not commute and neither do quaternions.

const a = quaternion("1 + i"), b = quaternion("1 + j");   // quarter turns about x and about y
String(a.mul(b));                                // "1 + i + j + k": together, a third of a turn about (1, 1, 1)
rotate(a.mul(b), [1, 0, 0]).map(String);         // ["0", "1", "0"]
rotate(b.mul(a), [1, 0, 0]).map(String);         // ["0", "0", "-1"]: the other order is a different rotation

Composing many rotations as floating-point matrices drifts: the product slowly stops being orthogonal and has to be repaired. An exact quaternion product has nothing to repair. The coordinates do grow, and primitive removes any common factor.

Interpolation

interpolate(q0, q1, t) is (1−t)q0+tq1(1 - t)q_0 + t q_1, taking the shorter way round. Because scale does not matter, this straight line between two quaternions passes through the same rotations as the great arc of spherical interpolation, in the same order, just not at constant speed. Every point on it with rational tt is an exact rotation.

import { interpolate } from "@ratmath/complex/cayley";

["0", "1/4", "1/2", "3/4", "1"].map((t) => rotate(interpolate(1, "1 + k", t), [1, 0, 0]).join(", "));
// ["1, 0, 0", "15/17, 8/17, 0", "3/5, 4/5, 0", "7/25, 24/25, 0", "0, 1, 0"]

The points on the way from (1,0,0)(1, 0, 0) to (0,1,0)(0, 1, 0) are rational points of the circle: Pythagorean triples again.

Try itEdit the code and run it with @ratmath/complex and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs