# What are quaternions and how are they used to represent spacecraft dynamics?

The title says it all. Quaternions are widely used to represent the orientation of a spacecraft. Why is that, and how do quaternions compare to other alternatives?

• I wonder if the footnote at the bottom of this page applies: github.com/moble/quaternion/blob/master/README.md – uhoh Dec 13 '16 at 7:41
• @uhoh -- Do an exact Google search for "euler angles are evil" (exact means search with the quotes). You'll get lots of hits. – David Hammen Dec 14 '16 at 12:35

What are quaternions and how are they used in spacecraft dynamics?

## Background

Newtonian mechanics says we live in a universe with three spatial dimensions, and a universal time that is the independent variable, in which we can describe translation and rotation. Relativity theory says we live in a universe with three space-like dimensions and one time-like dimension that are interweaved. This answer ignores relativistic aspects.

With this assumption, describing translation is easy, at least compared to rotation. An object's center of mass has a position in some frame of reference (preferably inertial) that changes over time. Gravitation, drag, and other interactions such as firing the translational thrusters change this value as a function of time. Simulate these interactions and you have a three degree of freedom (3DOF) simulation.

What makes 3DOF modeling so easy is that it is a commutative space. Walk north ten meters and then east five meters and you'll arrive at a certain spot. Alternatively, walking east five meters and the north ten meters will bring you to that same spot. Translation in Euclidean space commutes. Rotation in three dimensional space is not commutative. Pick up a book and rotate it about an axis parallel to the book's spine and then rotate the book about an axis perpendicular to the book's face. Do these actions differently (first rotate about an axis perpendicular to the book's face and then about it's spine) and you'll get a different end result.

## Rotations in three-dimensional space

As rotations in three-dimensional space do not commute, whatever they are, they are not vectors. Commutativity is an essential quality of being a vector. Since rotations aren't vectors, how to represent them? It turns out that there are a many different ways to represent rotations in three dimensional space. Euler was one of the first to investigate this issue, realizing a number of key concepts. One is that a rigid body rotating in three dimensional space can always be described as instantaneously rotating about a single axis. This concept is unique to 3D space, and is also key as to why quaternions (unit quaternions) are useful for describing rotations. I'll address this a bit later.

He also realized that the orientation of a planet, or of a rigid body in general, can be described in terms of three rotations. Euler used a sequence of three rotations: A rotation about the Z axis, then another about the once-rotated X axis, and then yet another about the twice-rotated Z axis. There's nothing special about this canonical Z-X'-Z'' sequence. All that is needed is that the first and third rotations be about the same body-fixed axis and that the middle element be about a body-fixed axis orthogonal to the other axis. A related sequence is to use three orthogonal body-fixed axes. This choice results in (for example) a roll-pitch-yaw sequence. There are six classical Euler sequences (e.g., Z-X'-Z''), plus six Tait-Bryan sequences (e.g., roll-pitch-yaw). Yet another variation on this theme is to rotate about a fixed set of axes. This leads to the extrinsic Euler sequences. All together, there are 24 different things that can be called Euler rotation sequences.

Another way to represent rotations is via a proper orthogonal matrix. An orthogonal matrix is an NxN matrix in which each row (or each column) is a unit vector and is orthogonal to all of the other rows (or columns). This representation is generic; it applies to any N-dimensional cartesian space. The 3x3 representation is of special interest because we live in a 3D universe. A key problem with an NxN matrix used to represent orientation is that rotations in N dimensional space have $(N^2-N)/2$ degrees of freedom while the matrix has $N^2$ elements. Over half of the elements of the matrix are redundant. (This problem of redundancy also applies to quaternions. Rotations in 3D space have three degrees of freedom. Quaternions have four elements.)

Yet another way to represent orientation / rotation in 3D space is via Euler's single axis theorem. No matter how weirdly a rigid body is oriented, there always exists an axis combined with a rotation by some angle about that axis that can be used to represent the orientation of the body in question with respect to some given frame of reference. This axis/angle representation is very close to a quaternionic representation. Given an unit vector $\hat u$ and an angle $\theta$, the quaternion whose scalar part is $\cos(\theta/2)$ and whose vector part is either $\hat u \sin(\theta/2)$ or $-\hat u \sin(\theta/2)$ captures all of the information needed to represent the orientation, completely avoiding the singularity issues associated with Euler sequences, and almost avoiding the over-specification issue associated with matrices.

## WTF are quaternions?

The above begs the question. I wrote about quaternions before discussing what quaternions are. William Rowan Hamilton struggled to find an extension of the complex numbers, where $i=\sqrt{-1}$. He finally had an insight while taking a walk with his wife and invented the quaternions. To ensure he wouldn't forget this incredible insight, he carved some graffiti on a bridge:

$$i^2=j^2=k^2=ijk=-1$$ This seemingly simple graffiti is incredibly complex (pun intended). Complex numbers have two elements, one real and one imaginary part. Negative one has two square roots in the complex numbers. Quaternions have four elements, one real part and three imaginary parts. Negative one has an uncountably many square roots in the quaternions.

There is a natural progression from the quaternions, discovered post-Hamilton. The octonions have eight elements, one of which is real part and the other seven are (somehow) imaginary. The sedenions have sixteen elements, one of which is real and the other fifteen are imaginary, and so on, with powers of two. The problem is that each step up the ladder drops a key mathematical concept. Quaternions don't commute (multiplicatively), octonions don't even associate. Sedenions and higher are generally rather worthless.

## Quaternion mathematics

Quaternions have two key binary operators, addition and multiplication, plus additive and multiplicative identity elements, additive inverses, and except for zero, multiplicative inverses. Addition works analogously to addition of a pair of four dimensional Cartesian vectors. Multiplication is where quaternions get tricky. From Hamilton's graffiti, one can deduce (for example) that $ij=k$ but that $ji=-k$. Quaternion multiplication is noncommutative. If one views quaternions as comprising a real scalar part and a vectorial imaginary part, the product of two quaternions $(a+\vec b)(c+\vec d)$ is ($ac-\vec b \cdot \vec d) + (a\vec d + c\vec b + \vec b \times \vec d)$.

This gives quaternions just the right amount of messiness needed to describe the rotational difference between two frames of reference in three-dimensional space. In particular, given some quaternion $q$ and a vector $v$ viewed as a pure imaginary quaternion, the quaternion operation $q\,v\,q^{-1}$ is another pure imaginary quaternion. This operation rotates the vector $v$ in three dimensional space. So does the operation $q^{-1}\,v\,q$.

## Unit quaternions

Because we're dealing with inverses, it makes sense to scale those quaternions so they are unitary. The inverse of a quaternion is its conjugate divided by the square of the magnitude. For a unit quaternion $q$, its inverse and quaternion conjugate are one and the same: $q^{-1} = q^\ast$ for all unit quaternions $q$.

Another reason to use unit quaternions is that they form the right kind of mathematical structure needed to represent rotations in 3D space. The unit quaternions are a mathematical group, as are rotations in 3D space. Every unit quaternion represents some rotation in 3D space, and every rotation in 3D space can be represented by a unit quaternion. Actually, every rotation in 3D space can be represented by two unit quaternions. Multiply a unit quaternion by -1 and you'll get another unit quaternion that represents the same rotation as the first one. Unit quaternions form a double cover on rotations in 3D space.

## How are quaternions used to represent rotations in 3D space?

This is the key question. The answer is that there is a very simple relation between the single axis representation of a rotation and a unit quaternion. The scalar part (aka real part) of this quaternion is $\cos(\theta/2)$, where $\theta$ is the single axis rotation angle, and the imaginary part is either $\hat u \sin(\theta/2)$ or $-\hat u \sin(\theta/2)$, where $\hat u$ is the unit vector along the axis of rotation. Whether one uses a plus or minus sign is completely arbitrary. This of course results in a number of arguments over "the right way to do it." There is no right way. Both approaches are equally valid.

## What are some of the gotchas?

While quaternions aren't evil as are Euler angles, there are issues associated with their use.

1. Left versus right quaternions.
There are two ways to use unit quaternions to represent a rotation or transformation in 3D space: $q v q^{\ast}$ or $q^{\ast} v q$. Both approaches are equally valid mathematically; the only difference is whether the unconjugated quaternion is on the precedes or follows the vector to be transformed or rotated.
2. Transformation versus rotation quaternions. Quaternions can be used to represent the physical rotation of a vector in 3D space and to represent the transformation of the same vector from one coordinate system to another. These turn out to be conjugate operations.
3. Order of the elements.
Quaternions have four elements, a scalar real part and a vectorial imaginary part. Which should go first, the real or imaginary part, when storing or exchanging them with someone else? The answer is that there is no right answer.
4. How to numerically integrate them.
The above three are representational issues only. This final issue is complex enough to be handled separately.

The first issues mean that when working with someone else (e.g., a joint integrated simulation), one had better get the nomenclature the two teams straightened out. The odds are strong that the two groups will not be using the same representations, and the odds are also strong that neither team will change their internal scheme. Fortunately, the conversion from one to another is easily handled. There will be lots of finger-pointing it's not handled.

## How to numerically integrate unit quaternions?

There are a number of reasons one may need to integrate a unit quaternion over time. A non-zero angular velocity changes a spacecraft's orientation. I use left transformation unit quaternions in my work. The time derivative of such a quaternion is $\dot q_{I\to B} = -\frac 1 2 \omega \, q_{I\to B}$ where $q_{I\to B}$ is the inertial to spacecraft body left transformation quaternion and $\omega$ is the spacecraft's angular velocity expressed in the body frame coordinates. Numerical integration inevitably involves steps along the lines of $x(t+\Delta t) = x(t) + \dot x(t)\Delta t$.

This is mathematically invalid for unit quaternions; the unit quaternions do not have an addition operator. It is valid for quaternions in general, but the result is inevitably a non-unit quaternion. One simple expedient is to normalize the result. This works,a bit, for very small steps. There has been a huge amount of work on geometric integrators over the last two decades, in particular, Lie group integrators. The unit quaternions are a fairly simple Lie group. The tangent space is the pure imaginary quaternions. This is the space in which angular velocities live. The pure imaginary quaternions form an algebra, a Lie algebra in particular. All of the work done on Lie group integration techniques applies directly to the unit quaternions.

• There is more to come. There are books on this subject. I don't want to write a book, but doing this subject justice requires quite a bit more than a few paragraphs. – David Hammen Dec 12 '16 at 18:29
• To any editors: My answer (so far) is long, and is therefore almost assuredly chock-full of grammatical errors. Please feel free to correct. – David Hammen Dec 12 '16 at 18:33
• A brief, how could one convert a quartenion to the familiar pitch-yaw-roll? – SF. Dec 12 '16 at 19:52
• I like this answer. But what about some words regarding gimbal lock? Gimbal lock is one of the most important reasons to prefer quaternions over 3x3 matrices in a lot of computations and models. – Polygnome Dec 12 '16 at 22:34
• @Polygnome -- Those words are forthcoming, Gimbal lock is one of the many reasons why Euler angles (in all 48 incantations) bite bullets. Quaternions do not suffer gimbal lock. – David Hammen Dec 12 '16 at 22:36