Rotations by quaternions
A quaternion turns space: . 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 as the quaternion . For a nonzero , the map sends vectors to vectors and is a rotation about the axis by the angle with
Multiplying by a nonzero real number changes nothing, since it cancels against . So does not have to have norm 1, and that is what keeps everything rational: a unit quaternion for a quarter turn needs , but 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 , so for a unit axis is the tangent of half the angle: is a quarter turn, turns by the angle of the 3-4-5 triangle. rotationOf(q) returns the axis with and , both rational.
Rational rotation matrices
The matrix of has entries that are quadratic in divided by , 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, 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 and then is the product , because . 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 , 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 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 to are rational points of the circle: Pythagorean triples again.
@ratmath/complex and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.