The quaternion number system is an extension of the complex number system. By introducing three "imaginary" numbers Loading..., Loading... and Loading..., William Hamilton was able to devise a number system capable of representing rotations in 3-dimensional space, analogous to the ability of complex numbers to represent rotation and scaling in the 2d plane.
The book "A History of Vector Analysis" tells the full historical tale, from Hamilton's discovery of quaternions up through modern vector analysis. This namespace is mostly concerned with the implementation and manipulation of quaternions, but I expect more writing will appear in the library on this fascinating topic.
This namespace begins with an implementation of a [[Quaternion]] type along with a number of accessors and constructors. Next comes a suite of arithmetic functions, followed by transcendental functions like [[exp]], [[log]], [[sin]] etc.
Next comes an API for describing rotations in various ways, and converting between quaternions and other representations of rotations, like 3D and 4D matrices, Euler angles and more.
Finally, the namespace ends by installing the [[Quaternion]] type into the generic system. Quaternions are compatible with complex numbers and real numbers, and interact with them by casting them up to [[Quaternion]] instances.
This implementation was inspired by a number of excellent Quaternion libraries:
For more reading, see:
The [[Quaternion]] type is a container for the four coefficients $(a, b, c, d)$ of a quaternion of the form Loading..., along with an optional metadata entry m
. (The coefficient fields are named r
, i
, j
and k
after the associated imaginary.)
The type implements a number of protocols, designed to achieve the following goals:
Quaternions should be seq
-able into a sequence of the coefficient entries, but also act like vectors with a fixed count of 4, capable of supporting rseq
, indexed lookups and efficient reductions.
Unlike a vector, if a [[Quaternion]] has function entries, applying the quaternion as a function should apply each component to the arguments and return a new quaternion with the results as the new coefficients.
The D
operator should work on quaternions, so derivatives of functions that return quaternions work well.
All [[emmy.generic]] functions should work well.
The following functions provide access to specific components, or coefficients, of a quaternion q
, as well as other named things you might like to extract, like the quaternion's vector or real components.
These functions apply specifically to quaternions with function coefficients.
NOTE: the definition for [[partial-derivative]] comes from quaternion.scm
in the original scmutils library. This pdf and Wikipedia seem to state that the derivative of, say, a quaternion-valued function with respect to a quaternion should not be defined this way. If you run into trouble, study these sources and please correct this issue! Or if this implementation is correct, please add some exposition and delete the comment.
The next section implements quaternion arithmetic. These implementations are installed into the generic arithmetic system at the bottom of the namespace.
The story of quaternions, from their discovery to their modern day usage, is tightly connected to their utility in describing rotations in 3-dimensional space.
The following section provides a number of utilities for building quaternions that represent rotations, and moving quaternions and other representations of rotations in 3d and 4d space.
Because equality is a symmetric relation, these methods arrange their arguments so that the quaternion is passed into [[eq]] first.