---
summary: The rotations as unit quaternions with the stereographic chart, global and full with a declared volume so that they carry the normal, the rigid motions SE(3) and SE₂(3) as their semidirect products with the left-translated chart, and the usual maps of all three, each a composite of the core's operations.
depends_on: [retraction, product-manifold, chart-volume, derivatives-in-charts, archive/core/math, archive/measures/math, archive/normal/math]
katex_macros:
    '\Exp': '\operatorname{Exp}'
    '\Log': '\operatorname{Log}'
    '\rv': '\operatorname{rv}'
    '\atantwo': '\operatorname{atan2}'
---

# Mathematics

## The problem

skoro is meant for models whose states are attitudes, poses and extended poses, a pose with a
velocity, and whose measurements are of them. The only curved type it has is the sphere of the
core's tests. This entry brings back the rotations $SO(3)$ and the rigid motions $SE(3)$ and
$SE_2(3)$, as embedded types declared on the core's public interface, so that each may be an
estimated state or an observable, and carry [the normal](/archive/normal/math.md).

Three earlier positions shape them. A type that carries the normal has a declared volume
([the core's measures](/archive/measures/math.md)) and a global chart
([why global charts](/archive/normal/math.md#why-global-charts)). A type's operations are composites of
the core's primitives wherever they can be, and fused operations, with kernels of their own,
only where they must (charter, position 11). And a type's chart is part of the meaning of every
normal on it, so it is chosen once, here. The `archive` branch held the rotations as $3 \times 3$
matrices with the exponential chart, and gave their exponential, logarithm and composition fused
kernels for each backend; its rigid motions were a rotation matrix and a position. This document
works out which representation, which chart and which measure each type takes now, and the maps
between them. It assumes no frame of the earth: frames are the user's to compose from these.

## Setting

A quaternion is $q = (w, u)$ with $w \in \R$ and $u \in \R^3$, stored as the vector $(w, u)$ of
$\R^4$. The product is

$$
	(w_1, u_1) \otimes (w_2, u_2) = \bigl(w_1 w_2 - u_1 \cdot u_2,\ w_1 u_2 + w_2 u_1 + u_1 \times u_2\bigr), @tag(product)
$$

the conjugate $\bar q = (w, -u)$, and $\operatorname{vec}(q) = u$, $w(q) = w$. The unit quaternions
$S^3 = \{q : \lvert q \rvert = 1\}$ are a group under $\otimes$, with identity $1 = (1, 0)$ and
inverse $\bar q$, and a compact Lie group of dimension $3$, whose Haar measure is invariant under
translation on either side. Each unit quaternion is a rotation,

$$
	\pi(q) = (w^2 - \lvert u \rvert^2)\, I + 2\, u u\transpose + 2 w\, [u]_\times , @tag(rotation)
$$

the rotation by the angle $\theta$ about $a$ when $q = (\cos \tfrac\theta2, \sin \tfrac\theta2\,
a)$, and $\pi : S^3 \to SO(3)$ is a homomorphism, two to one, with $\pi(q) = \pi(-q)$. Its action
on a vector is $\pi(q)\, x = x + 2 w\, (u \times x) + 2\, u \times (u \times x)$.

The rotations' tangent coordinates in this document are the rotation vector in the frame of the
point, $\pi(q \oplus \tangent) = \pi(q) (I + [\tangent]_\times) + O(\lvert \tangent \rvert^2)$:
a perturbation on the right, in the body frame.

## Matrices or quaternions

The rotations can be stored as the matrices $SO(3) \subset \R^{3 \times 3}$ or as the unit
quaternions $S^3 \subset \R^4$, and the core asks two different things of either.

**A chart.** For the normal, the chart at each point must be global and full. On the matrices the
Cayley chart is, with log-volume $-2 \log(1 + \lvert \tangent \rvert^2 / 4)$
([the note's table](/math/chart-volume.md#the-usual-charts)). On the unit quaternions the
stereographic chart is ([%full](statement:chart), below). Either serves the normal exactly.

**A way into the type.** A model computes rotations as well as holding them: a composition, an
inverse, a rotation built from a rate. The core reaches an embedded type from Euclidean values in
two ways: the retraction from a base point, valid in that point's chart, and the type's projection
$P$, a smooth map onto the type that is the identity on it
([%full](statement:archive/core/math/embedded)). A composition of two rotations written as a
retraction from one of them, $R_1 R_2 = R_1 \oplus (R_2 \ominus I)$, fails wherever $R_2$ is a
half-turn, which a model meets whenever a sensor is mounted upside down. A composition needs the
projection. On the unit quaternions it is the normalization $P(w) = w / \lvert w \rvert$, a
composite smooth away from $0$, and a composition is $P(q_1 \otimes q_2)$. On the matrices it is the
polar factor $X (X\transpose X)^{-1/2}$, which has no closed form in the core's primitives that is
smooth at the rotations themselves: the eigenvalues of $X\transpose X$ coincide there, and a
formula through them divides by their differences. Nor can it pass through the quaternions:

> **Proposition (The rotations have no continuous choice of quaternion).** There is no continuous map $s : SO(3) \to S^3$ with $\pi \circ s = \operatorname{id}$.

<Proof>

Suppose $s$ is one. Its image $s(SO(3))$ is compact, and so is $-s(SO(3))$, since $q \mapsto -q$ is
continuous. They are disjoint, since $s(R) = -s(R')$ gives $R = \pi(s(R)) = \pi(-s(R')) = R'$ and
then $s(R) = -s(R)$, which no unit quaternion is. And they cover $S^3$, since a $q$ with
$\pi(q) = R$ is $s(R)$ or $-s(R)$. So $S^3$ is the union of two disjoint nonempty closed sets, which
contradicts its connectedness.

</Proof>

Every formula that reads a quaternion off a matrix is therefore discontinuous somewhere, and a
projection of matrices through one is too. The matrices would need the composition, or the
projection, as a fused operation, with kernels for each backend and their tests on an adapter. The
unit quaternions need none: storage, chart, projection and composition are all composites.

> [!DECISION] The rotations are stored as unit quaternions
> The type is $S^3$, a double cover of the rotations, whose chart, projection, composition and
> action are composites of the core's primitives, and on which the normal is exact. What it costs
> is the double cover itself, which [The double cover](#the-double-cover) works out: a law on $S^3$
> is a law on the rotations through $\pi$, and an observed quaternion must be given on the side of
> its prediction. Neither is a burden in practice: a concentrated law on $S^3$ is a law on the
> rotations to within a negligible mass, and a sensor's quaternion is aligned once, on the host.

## The unit quaternions

The type $S^3$ has storage $4$, dimension $3$, and the retraction, inverse and projection

$$
	q \oplus \tangent = q \otimes \frac{\bigl(1 - s,\ \tangent / 2\bigr)}{1 + s}, \quad s = \tfrac1{16} \lvert \tangent \rvert^2;
	\qquad
	p \ominus q = \frac{4\, \operatorname{vec}(\bar q \otimes p)}{1 + w(\bar q \otimes p)};
	\qquad
	P(x) = \frac{x}{\lvert x \rvert} . @tag(quaternion-chart)
$$

> **Proposition (The stereographic chart of the unit quaternions).** For $q \in S^3$ and $\tangent \in \R^3$, with $s = \lvert \tangent \rvert^2 / 16$, let
>
> $$
> 	q \oplus \tangent = q \otimes \delta(\tangent), \qquad
> 	\delta(\tangent) = \frac{\bigl(1 - s,\ \tangent / 2\bigr)}{1 + s} .
> $$
>
> Then $\tangent \mapsto q \oplus \tangent$ is a diffeomorphism of $\R^3$ onto $S^3 \setminus \{-q\}$,
> whose inverse is $p \mapsto 4\, \operatorname{vec}(\bar q \otimes p) / (1 + w(\bar q \otimes p))$;
> $\delta(\tangent)$ is the rotation by the angle $\theta = 4 \arctan(\lvert \tangent \rvert / 4)$
> about $\tangent$; and $\pi(q \oplus \tangent) = \pi(q)\,\bigl(I + [\tangent]_\times\bigr) +
> O(\lvert \tangent \rvert^2)$, so that $\tangent$ is, to first order, the rotation vector of the
> perturbation in the frame of $q$.

<Proof>

Write $\delta = \delta(\tangent)$. Its norm is $((1 - s)^2 + 4s)^{1/2} / (1 + s) = 1$, so
$q \otimes \delta$ is a unit quaternion. With $w(\delta) = (1 - s)/(1 + s)$ and
$\lvert \operatorname{vec}(\delta) \rvert = (\lvert \tangent \rvert / 2)/(1 + s)$, $\delta$ is the
rotation by $\theta$ about $\tangent$ with $\cos \tfrac\theta2 = (1-s)/(1+s)$ and $\sin
\tfrac\theta2 = (\lvert \tangent \rvert / 2)/(1+s)$, so $\tan \tfrac\theta4 = \sin \tfrac\theta2 /
(1 + \cos \tfrac\theta2) = \lvert \tangent \rvert / 4$. As $\lvert \tangent \rvert$ runs over
$[0, \infty)$, $\theta$ runs over $[0, 2\pi)$, so $\delta$ runs over every unit quaternion but $-1$,
once each; $\delta$ is the inverse of the stereographic projection of $S^3$ from $-1$ onto the
hyperplane $w = 0$, at $\tangent / 4$. Left multiplication by $q$ carries $S^3 \setminus \{-1\}$ to
$S^3 \setminus \{-q\}$. The inverse follows from $\delta = \bar q \otimes p$ and $\operatorname{vec}(
\delta) / (1 + w(\delta)) = \tangent / 4$, which $s$ cancels from. Last, $\delta(\tangent) = (1,
\tangent / 2) + O(\lvert \tangent \rvert^2)$, and [](eq:rotation) at it is $I + [\tangent]_\times +
O(\lvert \tangent \rvert^2)$; $\pi$ is a homomorphism.

</Proof>

The chart's coordinate of a rotation by $\theta$ has the length $4 \tan(\theta / 4) = \theta +
\theta^3 / 48 + O(\theta^5)$, closer to the exponential coordinate $\theta$ than the Cayley
chart's $2 \tan(\theta/2) = \theta + \theta^3 / 12 + \cdots$. These are the *modified Rodrigues
parameters* of attitude estimation, scaled by $4$ so that they agree with the rotation vector to
first order. The chart is the left translation of the chart at the identity,
$q \oplus \tangent = q \otimes (1 \oplus \tangent)$, and since $q \otimes (w, u) \otimes \bar q =
(w, \pi(q)\, u)$ for a unit $q$, and $\delta$ depends on $\tangent$ only through its direction and
$\lvert \tangent \rvert$, which $\pi(q)$ preserves,

$$
	q \oplus \tangent = (1 \oplus \pi(q)\, \tangent) \otimes q @tag(conjugation)
$$

exactly, not only to first order: a perturbation $\tangent$ in the frame of the point is the
perturbation $\pi(q)\, \tangent$ in the frame the point is expressed in. So the stereographic chart
on the left, $\tangent \mapsto (1 \oplus \tangent) \otimes q$, is the same chart as this one, its
coordinates rotated by $\pi(q)$, and a law given with its noise on the left is a law on the right
with the covariance $\pi(q)\transpose \Sigma\, \pi(q)$.

> **Proposition (The volume of the stereographic chart).** Let $\nu$ be the Haar measure of $S^3$ normalized so that the chart of
> [%full](statement:chart) has volume $1$ at its centre, which is $8$ times the surface measure of
> $S^3 \subset \R^4$. The chart at every $q$ is global and full, and its log-volume is
>
> $$
> 	\lambda_q(\tangent) = -3 \log\Bigl(1 + \tfrac{1}{16} \lvert \tangent \rvert^2\Bigr) .
> $$

<Proof>

The chart at $q$ is the left translation of the chart at $1$, and $\nu$ is invariant under left
translation, so the volume is that of the chart at $1$
([%full](statement:math/chart-volume/group)). In polar coordinates, with $\theta$ the angle of a
rotation and $a$ its axis, Haar measure on $S^3$ is $4 \sin^2(\theta/2)\, \dd \theta\, \dd a$ for
$\theta \in [0, 2\pi)$, normalized as the exponential chart, whose volume at $\tangent$ is
$\operatorname{sinc}^2(\lvert \tangent \rvert / 2)$
([](eq:math/chart-volume/so3-exp)) and which on $S^3$ is a diffeomorphism of the ball of radius
$2\pi$. With $r = 4 \tan(\theta/4)$, $\dd \theta = \dd r / (1 + r^2/16)$ and $\sin(\theta/2) =
(r/2)/(1 + r^2/16)$, so the measure is $r^2\, \dd r\, \dd a / (1 + r^2/16)^3$, while Lebesgue
measure in $\tangent$ is $r^2\, \dd r\, \dd a$. The chart's derivative at $0$ is $\tangent \mapsto q
\otimes (0, \tangent/2)$, whose columns have length $\tfrac12$, so its volume at $0$ against surface
measure is $\tfrac18$, and $\nu$ is $8$ times surface measure
([%full](statement:math/chart-volume/normalized)). The chart is global by [%full](statement:chart)
and full since it misses one point.

</Proof>

The log-volume is rational in $\lvert \tangent \rvert^2$, smooth as computed everywhere, and needs
no guard at $\tangent = 0$. The total mass of $\nu$ is $8 \cdot 2\pi^2 = 16\pi^2$.

## The double cover

A normal on $S^3$ is a law on the unit quaternions, and through $\pi$ a law on the rotations.

> **Proposition (A law on the unit quaternions as a law on the rotations).** Let $\randomPoint$ be a random point of $S^3$ with density $\density$ with respect to $\nu$, and
> $\nu_{SO(3)} = \tfrac12\, \pi_* \nu$. Then $\pi(\randomPoint)$ has, with respect to
> $\nu_{SO(3)}$, the density $R \mapsto \density(q) + \density(-q)$, $q$ either preimage of $R$. If
> $\randomPoint = \mu \oplus \varepsilon$ with $\varepsilon$ in $\R^3$, then $\randomPoint$ lies on
> the far side of $\mu$, $\langle \randomPoint, \mu \rangle < 0$, exactly when
> $\lvert \varepsilon \rvert > 4$; and the two points $\pm q$ over one rotation have chart coordinates
> at $\mu$ whose lengths multiply to $16$.

<Proof>

The map $q \mapsto -q$ is the translation by $-1$, which preserves $\nu$, and $\pi^{-1}(A) = B \cup
(-B)$ for a measurable $B$ meeting each fibre once, such as the quaternions over $A$ with
$w > 0$, or $w = 0$ and a first nonzero coordinate of $u$ positive. So
$\Pr(\pi(\randomPoint) \in A) = \int_B (\density(q) + \density(-q))\, \dd \nu(q)$, and $\pi$
carries $\nu$ on $B$ to $\tfrac12 \pi_* \nu = \nu_{SO(3)}$ on $A$. For the second claim,
$\langle \mu \oplus \varepsilon, \mu \rangle = w(\delta(\varepsilon)) = (1 - s)/(1 + s)$, negative
exactly when $s > 1$. For the third, the coordinates of $q$ and $-q$ at $\mu$ have the angles
$\theta$ and $2\pi - \theta$, and $4 \tan(\theta/4) \cdot 4 \tan((2\pi - \theta)/4) = 16$.

</Proof>

So the normal on $S^3$ puts the mass $\Pr(\lvert \varepsilon \rvert > 4)$ on the far side of its
mean, at most $\Pr(\chi_3 > 4/\sigma)$ for $\sigma^2$ the largest eigenvalue of $\Sigma$: below
$10^{-13}$ at $\sigma = 0.5$ rad, about $10^{-3}$ at $1$ rad. As a law on the rotations it has the
density $\density(q) + \density(-q)$, in which the second term is negligible for a concentrated
noise and the first is the normal's term. For a broad one, such as a prior on an attitude about
which little is known, it is the sum that is the law on the rotations, and the term on $S^3$ is the
law on its double cover.

Three consequences follow for a model.

- **Generation is unaffected.** A draw $\mu \oplus \varepsilon$ is a unit quaternion near $\mu$,
  and every value computed from it through $\pi$, a rotated vector or a matrix, is the rotation's.
- **An observed quaternion must be given on the side of its prediction.** A sensor's quaternion
  has an arbitrary sign: $z$ and $-z$ are one rotation, but two points of $S^3$, and by the third
  claim one of them has a chart coordinate at the prediction of length at least $4$, where the
  normal's density is negligible. A measurement $z$ predicted by $h(\point)$ is aligned before it is
  observed, $z \leftarrow \operatorname{sign}\langle z, \hat h \rangle\, z$, at a value $\hat h$ of
  the prediction near the estimate: the point of $S^3$ that is the measured rotation on the
  prediction's side. Likelihoods that read the rotation only through $\pi$, a measured direction
  $\pi(q)\, x$ for one, need no alignment.
- **A posterior on $S^3$ has a mirror.** Every term that reads its quaternion through $\pi$ alone
  takes the same value at $q$ and at $-q$; a normal on $S^3$ does not, and breaks the symmetry. A
  model whose attitude enters through $\pi$ alone has a posterior with two mirrored modes, one for
  each sign, and an estimator converges to the one on the side it starts from, which is the same
  rotation.

## Maps of the rotations

A model computes with rotations as well as estimating them: it composes them, rotates vectors by
them, integrates rates into them, reads angles off them, and builds them from the forms in which
sensors and people give them. Each map below is a composite of the core's primitives and
[selection](/archive/core/math.md#selection), and its derivatives in charts are those of its parts. The
primitives have no square root, absolute value or inverse sine, and divide only through
logarithms, so the composites write a norm as $\lvert x \rvert = e^{\frac12 \log \sum_i x_i^2}$,
defined for $x \ne 0$, a reciprocal as $1/a = e^{-\log a}$, for $a > 0$, every inverse
trigonometric function through $\atantwo$, and an absolute value or a sign as a selection. Each
map is of one of three kinds, and the kind says what a model may ask of its derivatives:

- **smooth as written**: products, sums, sines and cosines of the storage, and reciprocals and norms
  of quantities positive on the map's domain, with derivatives of every order the core takes;
- **smooth, with a removable singularity**: smooth on its domain, but its closed form divides by an
  angle that vanishes at the identity; computed as the core's
  [smooth as computed](/archive/core/math.md#quantities) asks, by a series below a cutoff, with
  derivatives of every order the core takes, at the identity too;
- **a discretization and a selection**: where no continuous map exists, by
  [%full](statement:no-section), by [%full](statement:euler-singular), or by the jump of a
  rotation vector at a half-turn, the composite
  selects between smooth formulas by a discretization of its input; its derivatives are those of the
  selected formula, and the jump has none.

### The group

| map | of | to | as | kind |
| --- | --- | --- | --- | --- |
| identity | | $S^3$ | the constant $(1, 0)$ | smooth |
| composition | $S^3 \times S^3$ | $S^3$ | $P(q_1 \otimes q_2)$, [](eq:product) | smooth |
| inverse | $S^3$ | $S^3$ | $P(\bar q)$ | smooth |
| relative rotation | $S^3 \times S^3$ | $S^3$ | $P(\bar q_1 \otimes q_2)$ | smooth |
| action | $S^3 \times \R^3$ | $\R^3$ | $x + 2 w\, (u \times x) + 2\, u \times (u \times x)$ | smooth |
| inverse action | $S^3 \times \R^3$ | $\R^3$ | $x - 2 w\, (u \times x) + 2\, u \times (u \times x)$ | smooth |
| matrix | $S^3$ | $\R^{3 \times 3}$ | $\pi(q)$, [](eq:rotation) | smooth |
| adjoint | $S^3$ | $\R^{3 \times 3}$ | $\pi(q)$ | smooth |
| hat | $\R^3$ | $\R^{3 \times 3}$ | $[\omega]_\times$ | smooth |
| vee | $\R^{3 \times 3}$ | $\R^3$ | $\tfrac12 (A_{32} - A_{23},\ A_{13} - A_{31},\ A_{21} - A_{12})$ | smooth |

The composition, the inverse and the relative rotation are maps into the type, written in storage
and projected ([](eq:archive/core/math/map-into)); the product of two unit quaternions, and the
conjugate of one, are unit quaternions already, and the projection only returns what rounding
carried off the sphere. The relative rotation $\bar q_1 \otimes q_2$ is the rotation that carries
the frame of $q_1$ to that of $q_2$, expressed in the frame of $q_1$, so that $q_1 \otimes (\bar q_1
\otimes q_2) = q_2$. The inverse action is the action of $\bar q$, the transpose $\pi(q)\transpose
x$, which takes a vector given in the outer frame into the frame of $q$; both actions apply column
by column to a $3 \times n$ matrix of vectors, and a covariance is carried by $\pi(q)\, \Sigma\,
\pi(q)\transpose$, a product of the matrix with itself. The adjoint is the matrix itself on the
rotations, and its meaning is [](eq:conjugation): it carries a perturbation in the frame of the
point to the outer frame. The hat is the skew matrix with $[\omega]_\times x = \omega \times x$, and
the vee is its inverse on skew matrices and, on any other, the vector of its skew part.

### The exponential and the logarithm

The exponential takes a rotation vector to the rotation about it by its length:

$$
	\Exp(\omega) = \Bigl(\cos \tfrac\theta2,\ \frac{\sin(\theta/2)}{\theta}\, \omega\Bigr),
	\qquad \theta = \lvert \omega \rvert . @tag(exp)
$$

It is a diffeomorphism of the open ball of radius $2\pi$ onto $S^3 \setminus \{-1\}$, and every
$\omega$ is a rotation by $\theta$ modulo $2\pi$. It is how a rate becomes a rotation: an attitude
turning at the constant body rate $\omega$ for a time $\Delta t$ moves from $q_k$ to $q_{k+1} = q_k
\otimes \Exp(\omega\, \Delta t)$, exactly. Its inverse on $S^3 \setminus \{-1\}$ is the
logarithm,

$$
	\Log(q) = \frac{2 \atantwo(n, w)}{n}\, u, \qquad n = \lvert u \rvert, @tag(log)
$$

whose rotation vector has the length $2 \atantwo(n, w) \in [0, 2\pi)$, smooth on $S^3 \setminus
\{-1\}$ and undefined at $-1$, the one point the exponential's ball does not reach. The logarithm
is a map of the unit quaternions, and $\Log(-q) \ne \Log(q)$. The rotation's own logarithm, the
rotation vector of the shorter turn,

$$
	\rv(q) = \Log\bigl(s(q)\, q\bigr), \qquad s(q) = \begin{cases} 1 & w \ge 0, \\ -1 & w < 0, \end{cases} @tag(rotation-vector)
$$

is a map of the rotation, $\rv(-q) = \rv(q)$, with length in $[0, \pi]$, and a discretization: at a
half-turn, $w = 0$, it jumps from $\pi a$ to $-\pi a$, two rotation vectors of one rotation. The
logarithm is what inverts the exponential, and the derivative of either is the other's inverse; the
rotation vector is what a model reports as the error of an attitude, or reads as the rate between two
attitudes, $\omega \approx \rv(\bar q_k \otimes q_{k+1}) / \Delta t$.

The left and right Jacobians of the exponential are the matrices with $\Exp(\omega + \varepsilon) =
\Exp(J_l(\omega)\, \varepsilon) \otimes \Exp(\omega) = \Exp(\omega) \otimes \Exp(J_r(\omega)\,
\varepsilon)$ to first order in $\varepsilon$. They are

$$
	J_l(\omega) = I + A\, [\omega]_\times + B\, [\omega]_\times^2, \quad
	J_r(\omega) = J_l(-\omega) = I - A\, [\omega]_\times + B\, [\omega]_\times^2, \quad
	A = \frac{1 - \cos \theta}{\theta^2}, \quad B = \frac{\theta - \sin \theta}{\theta^3}, @tag(jacobians)
$$

and their inverses, for $\theta < 2\pi$,

$$
	J_l(\omega)^{-1} = I - \tfrac12 [\omega]_\times + C\, [\omega]_\times^2, \quad
	J_r(\omega)^{-1} = I + \tfrac12 [\omega]_\times + C\, [\omega]_\times^2, \quad
	C = \frac{1}{\theta^2}\Bigl(1 - \frac\theta2 \cot \frac\theta2\Bigr). @tag(jacobian-inverses)
$$

Each is a polynomial in $[\omega]_\times$, so the inverses are checked by multiplying out with
$[\omega]_\times^3 = -\theta^2 [\omega]_\times$, and $J_r(\omega) = J_l(\omega)\transpose$, since
$[\omega]_\times$ is skew. skoro does not need them to differentiate: a model's derivatives are
computed by passes, in the charts of the types, and those are the Jacobians of the textbooks.

> **Proposition (The derivatives in these charts are the exponential coordinates').** Let $\map$ be a smooth map between the types of this document and Euclidean spaces. Its derivative
> in charts [](eq:math/retraction/directional), read in the charts of this document, equals
> its Jacobian read in exponential coordinates on the same side, $\point \Exp(\tangent)$. In
> particular the derivative of $\Exp : \R^3 \to S^3$ at $\omega$ is the right Jacobian $J_r(\omega)$,
> and that of $\Log$ at $q$ is $J_r(\Log q)^{-1}$. On the unit quaternions the chart and the
> exponential agree to second order, $1 \oplus \tangent = \Exp(\tangent) + O(\lvert \tangent
> \rvert^3)$, so the second derivatives of a map of unit quaternions at the centres of the charts agree
> too; on the rigid motions they agree to first order only.

<Proof>

The derivative in charts of $\map$ at $\point$ is the Jacobian at $0$ of $\tangent \mapsto
Q_\otherPoint(\map(R_\point(\tangent)))$, with $R_\point$ the chart at $\point$ and $Q_\otherPoint$
its inverse at $\otherPoint = \map(\point)$, and by the chain rule it depends on $R_\point$ only
through its differential at $0$ and on $Q_\otherPoint$ only through its differential at
$\otherPoint$, which is the inverse of $R_\otherPoint$'s at $0$. On $S^3$, $1 \oplus \tangent =
\bigl(1 - \tfrac18 \lvert \tangent \rvert^2,\ \tfrac12 \tangent\bigr) + O(\lvert \tangent
\rvert^3)$ from $(1 - s)/(1 + s) = 1 - 2s + O(s^2)$, and $\Exp(\tangent)$ has the same expansion from
$\cos \tfrac\theta2 = 1 - \tfrac18 \theta^2 + O(\theta^4)$ and $\sin(\theta/2)/\theta = \tfrac12 +
O(\theta^2)$; both charts are left translations of these, so they agree at every point to second
order, and so do their inverses. The local representatives of $\map$ in the two charts then agree
to second order at $0$, and so do their first and second derivatives there. On the rigid motions
the chart is $(q \oplus \phi,\ p + \pi(q)\, \rho)$ and the exponential coordinates are $(q \Exp
\phi,\ p + \pi(q)\, J_l(\phi)\, \rho)$, by [](eq:motion-exp) below, and $J_l(\phi)\, \rho = \rho +
\tfrac12 \phi \times \rho + O(\lvert \phi \rvert^2 \lvert \rho \rvert)$: they agree to first order,
and differ at second order by the term $\tfrac12 \phi \times \rho$. Last, $\Exp(\omega + \varepsilon)
\ominus \Exp(\omega)$ is the chart coordinate at $1$ of $\Exp(\omega)^{-1} \otimes \Exp(\omega +
\varepsilon) = \Exp(J_r(\omega)\, \varepsilon) + O(\lvert \varepsilon \rvert^2)$, which is
$J_r(\omega)\, \varepsilon + O(\lvert \varepsilon \rvert^2)$; and $\Log$ is the inverse of $\Exp$.

</Proof>

The right Jacobian is still a map of its own, for the values a model computes with it rather than
for its derivatives: the left Jacobian is the $V$ of the rigid motions' exponential below, and a
model that propagates a covariance through a rate by hand, as the preintegration of an inertial
unit's samples does, writes $J_r$ into its formula.

### Removable singularities

The exponential, the logarithm, the Jacobians and their inverses are smooth at the identity, and
their closed forms divide by $\theta$ there, or by $n$. Each is computed as the core's
[smooth as computed](/archive/core/math.md#quantities) asks of a composite: a discretization of the
argument below a cutoff selects a truncated series of each coefficient, and further selections give
each formula a harmless argument on the rows where the other is chosen (the closed form where the
series is, and the series, if it can be undefined, where the closed form is), so that no derivative
of a formula that is not selected is ever undefined. The coefficients, each a function
of $\theta^2$, or for the logarithm of $n^2$ and $w$, are:

| coefficient | closed form | series | used by |
| --- | --- | --- | --- |
| $a$ | $\cos \tfrac\theta2$ | $\sum_k \frac{(-1)^k \theta^{2k}}{4^k (2k)!} = 1 - \frac{\theta^2}{8} + \frac{\theta^4}{384} - \cdots$ | [](eq:exp) |
| $b$ | $\sin(\theta/2)/\theta$ | $\sum_k \frac{(-1)^k \theta^{2k}}{2^{2k+1} (2k+1)!} = \frac12 - \frac{\theta^2}{48} + \frac{\theta^4}{3840} - \cdots$ | [](eq:exp) |
| $A$ | $(1 - \cos \theta)/\theta^2$ | $\sum_k \frac{(-1)^k \theta^{2k}}{(2k+2)!} = \frac12 - \frac{\theta^2}{24} + \frac{\theta^4}{720} - \cdots$ | [](eq:jacobians) |
| $B$ | $(\theta - \sin \theta)/\theta^3$ | $\sum_k \frac{(-1)^k \theta^{2k}}{(2k+3)!} = \frac16 - \frac{\theta^2}{120} + \frac{\theta^4}{5040} - \cdots$ | [](eq:jacobians) |
| $C$ | $\bigl(1 - \frac\theta2 \cot \frac\theta2\bigr)/\theta^2$ | $\frac1{12} + \frac{\theta^2}{720} + \frac{\theta^4}{30240} + \cdots$ | [](eq:jacobian-inverses) |
| $L$ | $2 \atantwo(n, w)/n$ | $\frac2w \sum_k \frac{(-1)^k x^k}{2k+1} = \frac2w \bigl(1 - \frac x3 + \frac{x^2}5 - \cdots\bigr)$, $x = n^2/w^2$ | [](eq:log) |

The series of $C$ is that of $\tfrac\theta2 \cot \tfrac\theta2 = 1 - \tfrac{\theta^2}{12} -
\tfrac{\theta^4}{720} - \tfrac{\theta^6}{30240} - \cdots$, and that of $L$ is $\atantwo(n, w) / n =
\arctan(n/w)/n$ for $w > 0$, expanded in $x$. The logarithm's series holds near the identity only,
so its discretization is $n^2 < c\, w^2$ and $w > 0$, which needs no division, and the series is
given $w = 1$ on the rows where it is not chosen, since its $1/w$ is undefined at $w \le 0$; near
$-1$ the closed form is selected, and it is the logarithm's cut, not a removable singularity. Its argument keeps
$w$: on the sphere $w = (1 - n^2)^{1/2}$, but a series in $n^2$ alone would have no derivative in
$w$, and the second derivatives along the sphere, which move $w$ at second order, would be wrong.
The closed forms' own arguments are $\theta = e^{\frac12 \log \theta^2}$ and $n = e^{\frac12 \log
n^2}$, and $\cot \tfrac\theta2 = \cos \tfrac\theta2\, e^{-\log \sin(\theta/2)}$, positive in its
logarithm for $0 < \theta < 2\pi$.

Each series keeps at least the terms through $\theta^2$, so that the derivatives of second order at
the identity are exact, and in practice as many as make its truncation error at the cutoff smaller
than the closed form's rounding there. Where to put each cutoff and how many terms to keep, at
either precision, is settled by the implementation against measured errors on both sides of the
cutoff; the archive's exponential, with its terms through $\theta^8$ and its cutoffs at $\theta^2
\approx 1.37$ in single precision and $0.025$ in double, is where that starts.

### Interpolation and distance

The geodesic from $q_1$ to $q_2$, at the parameter $t \in \R$, is

$$
	q_1 \otimes \Exp\bigl(t \Log(\bar q_1 \otimes q_2)\bigr), @tag(slerp)
$$

which runs at a constant rate along the great circle of $S^3$ through $q_1$ and $q_2$, reaching
$q_2$ at $t = 1$ by the arc that avoids $-q_1$, and is smooth wherever $q_2 \ne -q_1$. With $\rv$ in
place of $\Log$ it is the rotation's interpolation, along the shorter of the two turns from one
attitude to the other, and a discretization where the relative rotation is a half-turn and the two
turns are equally short. A model interpolating an attitude between two estimates means the second;
the first is the curve of the unit quaternions, and the two agree whenever $\langle q_1, q_2 \rangle
\ge 0$.

The angle between two rotations, the length of the shorter turn from one to the other and the
distance between them on the rotations, is

$$
	\angle(q_1, q_2) = \lvert \rv(\bar q_1 \otimes q_2) \rvert = 2 \atantwo\bigl(\lvert
	\operatorname{vec}(r) \rvert,\ \lvert w(r) \rvert\bigr) \in [0, \pi], \qquad r = \bar q_1 \otimes
	q_2 . @tag(angle)
$$

It is the same at $\pm q_1$ and $\pm q_2$, and smooth where $0 < \angle < \pi$: at $0$ it is a norm,
with no derivative, and at $\pi$ a maximum, where $\lvert w(r) \rvert$ turns. Its square,
$\lvert \rv(r) \rvert^2$, is smooth at $0$, and is what a cost on an attitude's error is written
with.

### Constructions and conversions

Rotations reach a model as an axis and an angle, as a pair of directions, as Euler angles and as
matrices, and leave it in the same forms.

**Elementary rotations and axis–angle.** The rotation by $\theta$ about the coordinate axis $e$ is
$q_e(\theta) = (\cos \tfrac\theta2,\ \sin \tfrac\theta2\, e)$, and about any axis $a \ne 0$ it is
$(\cos \tfrac\theta2,\ \sin \tfrac\theta2\, a / \lvert a \rvert)$, smooth in both. The way back is
the logarithm split into its length and its direction, $\theta = 2 \atantwo(n, w)$ and $a = u / n$;
the direction is undefined at the identity, where the composite selects a fixed axis, and is a
discretization there. A model differentiating through an axis and an angle is better served by the
rotation vector, which has no such point.

**From two directions.** The rotation of least angle carrying the direction of $a$ to that of $b$ is

$$
	P\bigl(\lvert a \rvert \lvert b \rvert + a \cdot b,\ a \times b\bigr), @tag(two-vectors)
$$

since for unit vectors at the angle $\theta$ about the axis $n$ the argument is $(1 + \cos \theta,
\sin \theta\, n) = 2 \cos \tfrac\theta2 \bigl(\cos \tfrac\theta2, \sin \tfrac\theta2\, n\bigr)$.
It is smooth for $a, b \ne 0$ except where $b$ is opposite to $a$: there the argument vanishes, every
half-turn about an axis perpendicular to $a$ is a rotation of least angle, and the map is undefined.
It is how a measured direction, of gravity or of a magnetic field, is turned into an attitude that
explains it.

**Euler angles.** A sequence of axes $(i, j, k)$ with $i \ne j \ne k$ is *proper* when $k = i$ and
of *Tait–Bryan* type when the three are distinct, six sequences of each. The rotation by the angles
$(\alpha, \beta, \gamma)$ about them, each about the axis as the previous rotations left it
(*intrinsic*), is

$$
	q = q_i(\alpha) \otimes q_j(\beta) \otimes q_k(\gamma), @tag(euler)
$$

smooth in the angles, and its rotation about the fixed axes in the same order (*extrinsic*) is
$q_k(\gamma) \otimes q_j(\beta) \otimes q_i(\alpha)$, the intrinsic rotation of the reversed sequence.
Roll, pitch and yaw $(\phi, \vartheta, \psi)$ in the usual aerospace convention are the intrinsic
$(z, y, x)$ angles $(\psi, \vartheta, \phi)$. The way back is a composite for every sequence:

> **Proposition (The Euler angles of a unit quaternion).** Let $(i, j, k)$ be axes with $i \ne j \ne k$, let $l$ be the axis other than $i$ and $j$, and let
> $\epsilon = \pm 1$ with $e_i \times e_j = \epsilon\, e_l$. Write $q_e(\theta) = (\cos \tfrac\theta2,
> \sin \tfrac\theta2\, e)$.
>
> 1. *Proper angles*, $k = i$. If $q = q_i(\alpha) \otimes q_j(\beta) \otimes q_i(\gamma)$ with
>    $\beta \in [0, \pi]$, then, with $\sigma = (\alpha + \gamma)/2$ and $\delta = (\alpha - \gamma)/2$,
>
>    $$
>    	(w,\ u_i) = \cos \tfrac\beta2\, (\cos \sigma,\ \sin \sigma), \qquad
>    	(u_j,\ \epsilon\, u_l) = \sin \tfrac\beta2\, (\cos \delta,\ \sin \delta) .
>    $$
>
>    So for $\beta \in (0, \pi)$ the angles are recovered, modulo $2\pi$, from any positive multiple
>    of $q$ or of $-q$ as $\beta = 2 \operatorname{atan2}\bigl(\lvert (u_j, u_l) \rvert,\ \lvert (w,
>    u_i) \rvert\bigr)$, $\alpha = \sigma + \delta$ and $\gamma = \sigma - \delta$, where $\sigma =
>    \operatorname{atan2}(u_i, w)$ and $\delta = \operatorname{atan2}(\epsilon\, u_l, u_j)$.
>
> 2. *Tait–Bryan angles*, $k = l$. If $q = q_i(\alpha) \otimes q_j(\beta) \otimes q_k(\gamma)$ with
>    $\beta \in [-\tfrac\pi2, \tfrac\pi2]$, then $q \otimes q_j(\tfrac\pi2)$ has the proper angles
>    $(\alpha,\ \beta + \tfrac\pi2,\ -\epsilon \gamma)$ about $(i, j, i)$, and is a positive multiple of
>    $(w - u_j,\ u_i - \epsilon u_k,\ u_j + w,\ u_k + \epsilon u_i)$, listed as the coordinates
>    $(w, u_i, u_j, u_k)$.

<Proof>

With $c_\theta = \cos \tfrac\theta2$ and $s_\theta = \sin \tfrac\theta2$, and $e_j \times e_i =
-\epsilon\, e_l$, $e_l \times e_i = \epsilon\, e_j$, the product [](eq:product) gives $q_i(\alpha)
\otimes q_j(\beta) = (c_\alpha c_\beta,\ s_\alpha c_\beta\, e_i + c_\alpha s_\beta\, e_j + \epsilon
s_\alpha s_\beta\, e_l)$, and its product with $q_i(\gamma)$ has

$$
	w = c_\beta (c_\alpha c_\gamma - s_\alpha s_\gamma), \quad
	u_i = c_\beta (c_\alpha s_\gamma + s_\alpha c_\gamma), \quad
	u_j = s_\beta (c_\alpha c_\gamma + s_\alpha s_\gamma), \quad
	u_l = \epsilon\, s_\beta (s_\alpha c_\gamma - c_\alpha s_\gamma),
$$

which are the stated forms by the addition formulas. For $\beta \in (0, \pi)$ both $c_\beta$ and
$s_\beta$ are positive, so $\lvert (w, u_i) \rvert = \lambda c_\beta$ and $\lvert (u_j, u_l) \rvert
= \lambda s_\beta$ for $q$ scaled by $\lambda > 0$, and the $\atantwo$s return $\beta / 2$, and
$\sigma$ and $\delta$ modulo $2\pi$; for $-q$ they return $\sigma + \pi$ and $\delta + \pi$ modulo
$2\pi$, which change $\alpha$ and $\gamma$ by multiples of $2\pi$. For the second claim, let $r =
q_j(\tfrac\pi2)$. Since $\pi(r)$ turns $e_i$ about $e_j$ to $e_j \times e_i = -\epsilon e_k$,
$q_k(\gamma) = r \otimes q_i(-\epsilon \gamma) \otimes \bar r$, and so $q \otimes r = q_i(\alpha)
\otimes q_j(\beta + \tfrac\pi2) \otimes q_i(-\epsilon\gamma)$, with $\beta + \tfrac\pi2 \in [0, \pi]$.
Last, $q \otimes (1, e_j) = (w - u_j,\ u + w\, e_j + u \times e_j)$ is $\sqrt 2\, q \otimes r$, and
$u \times e_j = \epsilon u_i\, e_k - \epsilon u_k\, e_i$.

</Proof>

So the Euler angles of a unit quaternion are, for a proper sequence, those of [%full](statement:euler)
read off $q$, and for a Tait–Bryan sequence those read off the linear rearrangement of $q$ it
gives, with $\beta$ lowered by $\tfrac\pi2$ and $\gamma$ multiplied by $-\epsilon$; for an extrinsic
sequence, those of the reversed sequence, in reverse order. The rotation is smooth in its angles
everywhere, but the angles are not smooth in the rotation everywhere, and no choice of formula
makes them so:

> **Proposition (No continuous Euler angles).** Fix a sequence of axes $(i, j, k)$, and call a rotation *at the lock* when its $\beta$ is $0$ or
> $\pi$ for a proper sequence, $\pm\tfrac\pi2$ for a Tait–Bryan one.
>
> 1. Away from the lock, $\alpha$ and $\gamma$ are smooth maps of the rotation into the circle
>    $\R / 2\pi\Z$, but neither is represented by a continuous real-valued map on any set of
>    rotations containing the loop $t \mapsto \pi\bigl(q_i(t) \otimes q_j(\beta) \otimes
>    q_k(\gamma)\bigr)$, $t \in [0, 2\pi]$, for $\gamma$ the loop in its own angle, with $\beta$ off
>    the lock.
> 2. At a rotation at the lock, neither $\alpha$ nor $\gamma$ has a limit, even in the circle. For a
>    proper sequence, $\alpha + \gamma$ has one at $\beta = 0$ and $\alpha - \gamma$ at $\beta = \pi$;
>    for a Tait–Bryan sequence, $\alpha - \epsilon \gamma$ has one at $\beta = -\tfrac\pi2$ and
>    $\alpha + \epsilon \gamma$ at $\beta = \tfrac\pi2$, with $\epsilon$ as in
>    the proposition on the Euler angles of a unit quaternion.

<Proof>

For the first claim, let $\tilde\alpha$ be continuous and real-valued, equal to $\alpha$ modulo
$2\pi$ on the loop. Off the lock the angles are unique modulo $2\pi$ by [%full](statement:euler),
so the loop's rotation at $t$ has $\alpha = t$ modulo $2\pi$, and $\tilde\alpha(t) - t$ is
continuous with values in $2\pi\Z$, hence constant. So $\tilde\alpha$ is larger by $2\pi$ at $t =
2\pi$ than at $t = 0$, but the loop closes there, since $q_i(2\pi) = -1$ and $\pi(-q) = \pi(q)$. The
same holds of $\gamma$ on its loop. For the second, take a proper sequence. As $\beta \to 0$,
$q_i(\alpha) \otimes q_j(\beta) \otimes q_i(\theta - \alpha)$ tends to $q_i(\theta)$ for every
$\alpha$, and for $\beta \in (0, \pi)$ its angles are $\alpha$ and $\theta - \alpha$ modulo $2\pi$;
so the rotations near $\pi(q_i(\theta))$ take every $\alpha$ and every $\gamma$, while $\alpha +
\gamma = \theta$ on all of them. At $\beta = \pi$ the same holds of $q_i(\alpha) \otimes q_j(\beta)
\otimes q_i(\alpha - \theta)$, which tends to $q_i(\theta) \otimes q_j(\pi)$ for every $\alpha$,
since $q_j(\pi) \otimes q_i(\varphi) = q_i(-\varphi) \otimes q_j(\pi)$, a half-turn about $e_j$
reversing $e_i$; there $\alpha - \gamma = \theta$. A Tait–Bryan sequence's angles are, by
[%full](statement:euler), the proper angles $(\alpha, \beta + \tfrac\pi2, -\epsilon\gamma)$ of $q
\otimes q_j(\tfrac\pi2)$, a diffeomorphism of the rotations, and its lock is theirs.

</Proof>

The way back therefore has two kinds of singularity, and the composite treats them differently.

**The wrap.** Away from the lock, $\alpha$ and $\gamma$ are smooth as angles, points of the circle,
and are returned as real numbers in $(-\pi, \pi]$, as $\atantwo(\sin \alpha, \cos \alpha)$, so that
$q$ and $-q$ give the same angles. By [%full](statement:euler-singular) a real-valued angle must be
cut somewhere, and this convention cuts it where it is $\pi$: crossing there, it jumps by $2\pi$,
from $\pi$ to just above $-\pi$. The jump is in the value alone. $\atantwo(a, b)$ jumps across its
half-line $a = 0$, $b < 0$, but its derivatives, $(b, -a) / (a^2 + b^2)$ by
[](eq:archive/core/math/atan2), and all of their own, are continuous across it, so the angles'
derivatives of every order are the same on either side of the wrap: those of the angle on the
circle. What a jump in value breaks is a difference of values: a finite difference straddling the
wrap, and a residual. Half-turns about the coordinate axes, common test values, lie on it: a
half-turn about $x$ has the $(z, y, x)$ angles $(0, 0, \pi)$, and rounding puts its $\gamma$ at
either end of the interval. A residual between an angle $a$ read off a model and an angle $b$ it is
compared with is taken on the circle,

$$
	a \ominus b = \atantwo\bigl(\sin(a - b),\ \cos(a - b)\bigr), @tag(angle-residual)
$$

which moves the cut to a residual of $\pm\pi$, as far as an angle can be from the one it is compared
with, and has the derivatives of $a - b$ everywhere else. With $b$ a reference angle, such as the
previous estimate along a trajectory, $b + (a \ominus b)$ is the angle in $(b - \pi, b + \pi]$,
continuous along any path that stays within a half-turn of $b$.

**The lock.** At gimbal lock one of the two norms of [%full](statement:euler) vanishes, and by
[%full](statement:euler-singular) only $\alpha + \gamma$ or $\alpha - \gamma$ is determined there: no
convention makes the angles continuous at the lock, and the composite chooses $\gamma = 0$, which
[](eq:euler) maps back to the rotation exactly. It selects it by a discretization of the smaller
squared norm against a fraction $c$ of $\lvert q \rvert^2$. On the rows selected, $\beta$ is taken at
the lock rather than computed, $\alpha = 2\sigma$ when $\lvert (u_j, u_l) \rvert$ vanishes and
$\alpha = 2\delta$ when $\lvert (w, u_i) \rvert$ does, and the vanishing norm and the $\atantwo$ it
feeds are given a harmless argument, since a norm has no derivative at $0$. A row is selected when
$\sin \tfrac\beta2 \le \sqrt c$, or $\cos \tfrac\beta2 \le \sqrt c$, and there the convention's
rotation differs from $q$ by a conjugate of $q_j(\beta)$, or of $q_j(\pi - \beta)$: by the angle
$\beta$, or $\pi - \beta$, at most $2 \arcsin \sqrt c$. So $c$ is chosen to make that angle smaller
than the rounding of either precision, and the selection changes the round trip of no rotation that
rounding can tell from the lock. On the selected rows the derivatives are those of the selected
formulas: none for $\beta$ and $\gamma$, and for $\alpha$ those of $2\sigma$ or $2\delta$, which are
the derivatives of $\alpha + \gamma$ or $\alpha - \gamma$, the one combination that has them.

Off the lock the angles are smooth, but not uniformly. $\delta$ is the angle of the vector $(u_j,
\epsilon u_l)$ of length $\sin \tfrac\beta2$, and $\sigma$ that of $(w, u_i)$ of length $\cos
\tfrac\beta2$, so the first derivatives of $\alpha$ and $\gamma$ grow like $1 / \sin \tfrac\beta2$ as
$\beta \to 0$ and $1 / \cos \tfrac\beta2$ as $\beta \to \pi$, and their second derivatives like the
squares; $\beta$, twice the angle between those two lengths, has bounded first derivatives that turn
with the direction of approach, and second derivatives that grow in the same way. The same factor
amplifies a rounding of $q$'s coordinates into $\alpha$ and $\gamma$ near the lock, while $\alpha \pm
\gamma$, read off the larger norm, stays accurate: that is the condition of the angles, not an error
of the composite. Euler angles are therefore a way into and out of a model, and its noise is put in
the charts instead.

**From a matrix.** By [%full](statement:no-section) no continuous map reads a quaternion off every
rotation matrix, and the usual reading is a selection between four, each a linear map of the matrix
$X$ projected:

$$
	\begin{aligned}
	c_0(X) &= (1 + X_{11} + X_{22} + X_{33},\ X_{32} - X_{23},\ X_{13} - X_{31},\ X_{21} - X_{12}), \\
	c_1(X) &= (X_{32} - X_{23},\ 1 + X_{11} - X_{22} - X_{33},\ X_{12} + X_{21},\ X_{13} + X_{31}), \\
	c_2(X) &= (X_{13} - X_{31},\ X_{12} + X_{21},\ 1 - X_{11} + X_{22} - X_{33},\ X_{23} + X_{32}), \\
	c_3(X) &= (X_{21} - X_{12},\ X_{13} + X_{31},\ X_{23} + X_{32},\ 1 - X_{11} - X_{22} + X_{33}),
	\end{aligned}
	\qquad
	q = P\bigl(c_m(X)\bigr), @tag(from-matrix)
$$

with $m$ the index of the largest of the four diagonal entries $c_m(X)_m$. At $X = \pi(q)$,
[](eq:rotation) gives $c_m(X) = 4 q_m\, q$, writing $q = (q_0, q_1, q_2, q_3)$, so $P(c_m(X)) =
\operatorname{sign}(q_m)\, q$: the quaternion over $X$ whose $m$-th coordinate is positive. Since
$c_m(X)_m = 4 q_m^2$ and the largest of the $q_m^2$ is at least $\tfrac14$, the selected argument has
norm at least $2$. The composite selects the argument before it projects, so that the projection
never meets one of the small arguments it did not choose. Within each choice of $m$ the map is
smooth; where the choice changes, the result may change sign. A matrix that is only near a rotation
gives a nearby unit quaternion, though not that of the nearest rotation. A quaternion read off a
measured matrix this way is aligned with its prediction like any other.

**Alignment.** The alignment of $z$ with $r$, $\operatorname{sign}\langle z, r \rangle\, z$ by
selection, is the point over $z$'s rotation on the side of $r$, and with $r = 1$ the one with $w \ge
0$. It is a discretization, applied on the host to an observed quaternion before it is observed, as
[The double cover](#the-double-cover) says, or to a quaternion before it is reported.

## The rigid motions

A rigid motion is a rotation and a translation, and an extended pose a rotation, a velocity and a
position. As groups, with the unit quaternions in place of the rotations, they are the semidirect
products $S^3 \ltimes \R^3$, a double cover of $SE(3)$, and $S^3 \ltimes (\R^3 \times \R^3)$, a
double cover of $SE_2(3)$, with the products of [%full](statement:motions), stored as $(q, p)$, of
storage $7$ and dimension $6$, and $(q, v, p)$, of storage $10$ and dimension $9$.

> **Proposition (The charts of the rigid motions).** On $S^3 \ltimes \R^3$, with the product $(q_1, p_1)(q_2, p_2) = (q_1 \otimes q_2,\ p_1 + \pi(q_1)
> p_2)$, let
>
> $$
> 	(q, p) \oplus (\phi, \rho) = \bigl(q \oplus \phi,\ p + \pi(q)\, \rho\bigr),
> $$
>
> and on $S^3 \ltimes (\R^3 \times \R^3)$, with $(q_1, v_1, p_1)(q_2, v_2, p_2) = (q_1 \otimes q_2,\
> v_1 + \pi(q_1) v_2,\ p_1 + \pi(q_1) p_2)$, let
>
> $$
> 	(q, v, p) \oplus (\phi, \eta, \rho) = \bigl(q \oplus \phi,\ v + \pi(q)\, \eta,\ p + \pi(q)\, \rho\bigr).
> $$
>
> Each is the left translation by the point of the chart at the identity, is global and full, has the
> inverse $\bigl(\phi, \rho\bigr) = \bigl((q' \ominus q),\ \pi(q)\transpose (p' - p)\bigr)$ (and likewise
> for $v$), and has, against the product $\nu \times$ Lebesgue, which is a Haar measure of the group,
> the log-volume $\lambda(\phi, \cdot) = -3 \log(1 + \lvert \phi \rvert^2 / 16)$ of its rotation
> alone.

<Proof>

The product of $(q, p)$ and $(1 \oplus \phi, \rho)$ is $(q \otimes (1 \oplus \phi), p + \pi(q)
\rho)$, the chart, and likewise for the extended pose. For fixed $(q, p)$ the chart is the product
of the rotation's chart, a diffeomorphism of $\R^3$ onto $S^3 \setminus \{-q\}$, and the affine map
$\rho \mapsto p + \pi(q) \rho$ of $\R^3$ onto itself, so it is global, full, and inverted as
stated. Since $\pi(q)$ is a rotation, $\nu \times$ Lebesgue measure is invariant under the group's
left and right translations, so it is a Haar measure and the volume of the chart is the same at
every point ([%full](statement:math/chart-volume/group)); at the identity it is the product of the
rotation's chart's volume and $\lvert \det \pi(1) \rvert = 1$.

</Proof>

The chart is not the groups' exponential [](eq:motion-exp), which couples the translation to the
rotation through the left Jacobian; the two agree to first order, by [%full](statement:first-order).
Its translation coordinates are in the frame of the point: the noise $\rho$ of a pose's normal moves
the position along the pose's own axes, as the noise of an odometer, an inertial unit or a scan
matcher is given. Since the chart is a left translation, the normal's
[law of a translated chart](/archive/normal/math.md#without-a-volume-or-a-global-chart) holds on these
groups, as on the rotations.

A translation with its noise in a fixed frame is a different model, the product of $S^3$ with
$\R^3$, whose chart moves the rotation and the position independently. The core has products
already: $S^3 \times \R^3$ is a product of spaces, global and full when its factors are, with the
log-volume of its rotation alone, and needs no type of its own.

> [!DECISION] Two charts for a pose, one of them a type
> A pose whose noise is given in its own frame is a rigid motion, $S^3 \ltimes \R^3$, with the
> left-translated chart of [%full](statement:motions); a pose whose position's noise is given in a
> fixed frame is the product $S^3 \times \R^3$ the core already has. The motions' types offer the
> first chart alone, and a model moves a value between the two by the maps below, which are the
> identity on storage.

### Maps of the rigid motions

The maps of the rigid motions are their group's, each a composite projecting its rotation, with
$R = \pi(q)$ and tangent coordinates ordered rotation first, $(\phi, \rho)$:

| map | of | to | as | kind |
| --- | --- | --- | --- | --- |
| identity | | $SE(3)$ | $(1, 0)$ | smooth |
| composition | $SE(3) \times SE(3)$ | $SE(3)$ | $(P(q_1 \otimes q_2),\ p_1 + R_1 p_2)$ | smooth |
| inverse | $SE(3)$ | $SE(3)$ | $(P(\bar q),\ -R\transpose p)$ | smooth |
| relative motion | $SE(3) \times SE(3)$ | $SE(3)$ | $(P(\bar q_1 \otimes q_2),\ R_1\transpose (p_2 - p_1))$ | smooth |
| action on a point | $SE(3) \times \R^3$ | $\R^3$ | $R x + p$ | smooth |
| action on a direction | $SE(3) \times \R^3$ | $\R^3$ | $R x$ | smooth |
| inverse action | $SE(3) \times \R^3$ | $\R^3$ | $R\transpose (x - p)$ | smooth |
| rotation, position | $SE(3)$ | $S^3$, $\R^3$ | $P(q)$, $p$ | smooth |
| from parts | $S^3 \times \R^3$ | $SE(3)$ | $(P(q), p)$ | smooth |
| matrix | $SE(3)$ | $\R^{4 \times 4}$ | $\begin{pmatrix} R & p \\ 0 & 1 \end{pmatrix}$ | smooth |
| adjoint | $SE(3)$ | $\R^{6 \times 6}$ | [](eq:motion-adjoint) | smooth |
| exponential | $\R^6$ | $SE(3)$ | [](eq:motion-exp) | removable |
| Jacobians | $\R^6$ | $\R^{6 \times 6}$ | [](eq:motion-jacobians) | removable |
| logarithm | $SE(3)$ | $\R^6$ | [](eq:motion-log) | removable |
| interpolation | $SE(3) \times SE(3) \times \R$ | $SE(3)$ | [](eq:motion-slerp) | removable |

*From parts* is a map from the product $S^3 \times \R^3$ to the motion, and *rotation, position*
the way back: they are the identity on storage, and a change of chart, whose derivative is
$\operatorname{diag}(I, R\transpose)$ from the product's chart to the motion's, since the product's
coordinates $(\phi, \rho)$ reach $(q \oplus \phi,\ p + \rho)$, whose coordinates in the motion's
chart are $(\phi,\ R\transpose \rho)$. The matrix is the homogeneous
one, so that composition is its product and the action on a point is its product with $(x, 1)$.

The adjoint is the conjugation of the group's Lie algebra, $T \Exp(\xi)\, T^{-1} = \Exp(\operatorname{Ad}(T)\,
\xi)$, and carries a perturbation in the frame of the motion to the outer frame, $T \oplus \xi = (1
\oplus \operatorname{Ad}(T)\, \xi)\, T + O(\lvert \xi \rvert^2)$; in the order $(\phi, \rho)$ it
is

$$
	\operatorname{Ad}(q, p) = \begin{pmatrix} R & 0 \\ [p]_\times R & R \end{pmatrix}, @tag(motion-adjoint)
$$

which many references write with the blocks the other way about, their translation first. Unlike
[](eq:conjugation) on the rotations, the relation holds to first order only, since the chart is not
the exponential.

The exponential is the motion at a constant body twist $(\phi, \rho)$, rotating at the rate $\phi$
while translating at the body velocity $\rho$, for unit time, and the logarithm its inverse:

$$
	\Exp(\phi, \rho) = \bigl(\Exp \phi,\ J_l(\phi)\, \rho\bigr), @tag(motion-exp)
$$

$$
	\Log(q, p) = \bigl(\phi,\ J_l(\phi)^{-1} p\bigr), \qquad \phi = \Log q, @tag(motion-log)
$$

the logarithm defined for $q \ne -1$, with $J_l$ and its inverse of [](eq:jacobians) and
[](eq:jacobian-inverses), whose series serve here too. The motions' Jacobians, with $\Exp(\xi +
\varepsilon) = \Exp(\mathcal J_l(\xi)\, \varepsilon)\, \Exp(\xi) = \Exp(\xi)\, \Exp(\mathcal
J_r(\xi)\, \varepsilon)$ to first order, are in the order $(\phi, \rho)$

$$
	\mathcal J_l(\phi, \rho) = \begin{pmatrix} J_l(\phi) & 0 \\ Q(\phi, \rho) & J_l(\phi) \end{pmatrix},
	\qquad
	\mathcal J_r(\phi, \rho) = \mathcal J_l(-\phi, -\rho),
	\qquad
	\mathcal J_l^{-1} = \begin{pmatrix} J_l^{-1} & 0 \\ -J_l^{-1} Q\, J_l^{-1} & J_l^{-1} \end{pmatrix}, @tag(motion-jacobians)
$$

with $\Phi = [\phi]_\times$, $\Psi = [\rho]_\times$ and

$$
	Q(\phi, \rho) = \tfrac12 \Psi + B\, (\Phi \Psi + \Psi \Phi + \Phi \Psi \Phi)
	+ D\, (\Phi^2 \Psi + \Psi \Phi^2 - 3\, \Phi \Psi \Phi)
	+ E\, (\Phi \Psi \Phi^2 + \Phi^2 \Psi \Phi),
	\quad D = \frac{\theta^2 + 2 \cos \theta - 2}{2 \theta^4},
	\quad E = \frac{2\theta - 3 \sin \theta + \theta \cos \theta}{2 \theta^5}, @tag(motion-q)
$$

$B$ the coefficient of [](eq:jacobians). This is the block of the textbooks written translation first,
moved below the diagonal by the order of the coordinates here. Its coefficients are smooth with
removable singularities at $\theta = 0$, with the series

$$
	D = \sum_k \frac{(-1)^k \theta^{2k}}{(2k+4)!} = \frac1{24} - \frac{\theta^2}{720} + \cdots,
	\qquad
	E = \sum_k \frac{(-1)^k (k+1)\, \theta^{2k}}{(2k+5)!} = \frac1{120} - \frac{\theta^2}{2520} + \cdots,
$$

computed as the rotations' are. By [%full](statement:first-order), $\mathcal J_r$ is the derivative
of [](eq:motion-exp) that a pass computes, and like the rotations' it is a map for the values a
model computes with it. A pose moving at constant body rates
$(\omega, v)$ for a time $\Delta t$ moves from $T_k$ to $T_{k+1} = T_k\, \Exp(\omega\, \Delta t,\ v\,
\Delta t)$, exactly. With $\rv$ in place of $\Log$, the logarithm is the motion's along the shorter
turn, a discretization at half-turns as on the rotations.

Two interpolations are in use, and both are offered:

$$
	T_1\, \Exp\bigl(t \Log(T_1^{-1} T_2)\bigr)
	\qquad\text{and}\qquad
	\bigl(q_1 \otimes \Exp(t \Log(\bar q_1 \otimes q_2)),\ (1 - t)\, p_1 + t\, p_2\bigr) . @tag(motion-slerp)
$$

The first is the screw motion at a constant twist, the one-parameter subgroup through $T_1^{-1}
T_2$ carried to $T_1$, and the second
interpolates the rotation along its geodesic and the position along a straight line, the geodesic
of the product. Each comes, like [](eq:slerp), with $\rv$ in place of $\Log$ for the shorter turn.

The extended poses have the same maps with the velocity carried as the position is: the composition
$(P(q_1 \otimes q_2),\ v_1 + R_1 v_2,\ p_1 + R_1 p_2)$, the inverse $(P(\bar q),\ -R\transpose v,\
-R\transpose p)$, the relative motion likewise, the parts and the pose $(q, p)$ among them, the
$5 \times 5$ matrix with the columns $(R, v, p)$ over $\begin{pmatrix} 0 & 1 & 0 \\ 0 & 0 & 1
\end{pmatrix}$, the exponential $(\Exp \phi,\ J_l(\phi)\, \eta,\ J_l(\phi)\, \rho)$ and its logarithm,
the left Jacobian with the blocks $J_l(\phi)$ on its diagonal and $Q(\phi, \eta)$ and $Q(\phi, \rho)$
below it in its first column, and in the order $(\phi, \eta, \rho)$ the adjoint

$$
	\operatorname{Ad}(q, v, p) = \begin{pmatrix} R & 0 & 0 \\ [v]_\times R & R & 0 \\ [p]_\times R & 0 & R \end{pmatrix} . @tag(extended-adjoint)
$$

Their action on points is that of their pose. An
inertial unit's step, which turns the attitude by its rate, adds the rotated specific force and a
gravity to the velocity, and the velocity to the position, is a composite of these maps that the
model writes with its own gravity, since no frame of the earth is assumed.

## The normal on these types

Every chart of this document is global and full with a declared volume, so the normal applies as
[its mathematics](/archive/normal/math.md) defines it, its density and its sampler exact. The figures its
[Properties](/archive/normal/math.md#properties) state for a chart follow from the log-volume, whose
negative is $\tfrac{3}{16} \lvert \tangent \rvert^2 + O(\lvert \tangent \rvert^4)$, with the
Hessian $\tfrac38 I$:

- a measurement of a rotation weighs on the state as a Gaussian of its residual would with the
  precision $\Lambda - \tfrac38 I$ on the rotation's block, a change of $0.4\,\%$ for a noise of
  $0.1$ rad;
- the mean is a mode of the density when $\sigma^2 < \tfrac83$, a standard deviation under
  $1.6$ rad;
- Gauss–Newton with the log-volume's gradient converges at a rate within
  $\kappa = \tfrac38 \sigma^2$ of the residuals' own.

The expected information of the normal's root is exact in the scale's block and of second order in
the blocks of the point and the mean, as on any curved type.

## What the tests check

- **The types' boundaries**: the core's check of an embedded type on $S^3$, $SE(3)$ and $SE_2(3)$,
  with each declared volume checked against the area formula.
- **The chart**: [%full](statement:chart)'s inverse on the whole image, the angle $4
  \arctan(\lvert \tangent \rvert / 4)$, the first-order rotation vector against [](eq:rotation), and
  [](eq:conjugation).
- **The groups**: associativity, identities and inverses of the compositions, and the relative
  rotations and motions against them; $\pi$ a homomorphism, the same at $q$ and $-q$; the actions
  homomorphisms, the inverse actions their inverses; the homogeneous matrices homomorphisms; hat and
  vee inverse on skew matrices; the adjoints against $T \Exp(\xi)\, T^{-1} = \Exp(\operatorname{Ad}(T)\,
  \xi)$, and [](eq:motion-adjoint) against the first-order relation with the chart.
- **The exponentials and logarithms**: each logarithm the inverse of its exponential on the ball of
  radius $2\pi$, and $\rv$ against $\Log$ on either side of a half-turn; the Jacobians against
  [%full](statement:first-order), as the derivative of $\Exp$ computed by a pass, on the rotations and on both
  motions, and their inverses by multiplication; every coefficient of [Removable singularities](#removable-singularities),
  and $D$ and $E$, against its closed form, in extended precision, on both sides of its cutoff, at both precisions,
  with first and second derivatives at the identity checked against the series'.
- **The constructions**: [](eq:two-vectors) carrying $a$ to $b$; [%full](statement:euler) by round
  trips through [](eq:euler) for all twelve sequences, intrinsic and extrinsic, away from the lock
  and the wrap, and at the lock the convention $\gamma = 0$ reproducing the rotation, on rows exactly
  at it and within the threshold of it; [](eq:from-matrix) returning
  $\pm q$ from $\pi(q)$ in each of its four branches; alignment and the interpolations at their
  endpoints and at a constant rate.
- **The discretizations**: each one's value on both sides of its jump, and its derivatives those of
  the formula selected, finite where the core's passes are taken; at the wrap of the Euler angles,
  values $2\pi$ apart on either side and derivatives that agree across it, with the angles' finite
  differences checked only at rows farther from the wrap than their step; and
  [](eq:angle-residual) continuous across the wrap of either angle.
- **The normal**: its density integrating to one through the chart of $S^3$ and of $SE(3)$, its
  draws agreeing with quadratures, and [%full](statement:cover)'s law on the rotations, by
  quadrature over $SO(3)$ of $\density(q) + \density(-q)$.

The core's acceptance test stays where it is, on the sphere its tests declare. That sphere is to be
replaced by the library's own spheres when an entry brings them; this entry does not revise the
core.

## In skoro's terms

- **Three embedded types**, declared from the core's public interface: the unit quaternions, of
  storage $4$ and dimension $3$; the rigid motions, $7$ and $6$; the extended poses, $10$ and $9$.
  Each declares its retraction, inverse and projection as composites, and a global volume, the
  log-volume of its rotation's chart.
- **Their tangent coordinates** are the rotation vector in the body frame, then the translation,
  and for the extended pose the velocity before the position, in the body frame; every adjoint and
  Jacobian is written in that order.
- **Their maps** are composites of the core's primitives and selection: the groups' operations,
  with compositions and inverses into the types by projection; actions, matrices, adjoints and
  the rotations' and the motions' Jacobians into Euclidean values; exponentials into the types and logarithms out of them; the
  interpolations and the angle; and the constructions from and to axes and angles, pairs of
  directions, Euler angles and matrices. Each is smooth, smooth with a removable singularity
  computed by series, or a stated discretization.
- **Their derivatives** are the passes' over those composites, and are the textbooks' Jacobians,
  by [%full](statement:first-order); no derivative is written by hand.
- **A quantity of these types** may be scored by the normal, observed and simulated; an observed
  quaternion is aligned with its prediction by its caller. A pose with its noise in a fixed frame
  is a quantity of the product $S^3 \times \R^3$.
- **No earth frame** is assumed: a frame is a rotation or a motion the user composes, and a gravity
  a constant the user's model holds.

## Open questions

None remain.

## Revisions

**2026-11-03.** Reopened after the build: the Euler angles are said to be smooth away from gimbal
lock, but returning $\alpha$ and $\gamma$ to $(-\pi, \pi]$ makes them jump by $2\pi$ wherever
either crosses $\pm\pi$, which the derivative checks found at the half-turns about the coordinate
axes. This revision treats the Euler angles at all of their singularities. A new proposition,
[%full](statement:euler-singular), shows that neither singularity can be removed: a
real-valued angle must be cut somewhere, and at the lock no convention is continuous. The text now
says that the wrap is a jump of the value alone, the derivatives agreeing across it; gives the
residual on the circle, [](eq:angle-residual), by which a model compares angles across the wrap or
keeps them continuous along a path; bounds what the lock's threshold costs, and so how it is
chosen; and says how the angles' derivatives and their rounding grow near the lock. The tests
check the wrap and the residual, and the lock within its threshold as well as exactly at it.
