Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Coverage is measured in two CI jobs (see .github/workflows/CI.yml), each of which uploads
# its own report: the Ubuntu job with the latest Julia, which runs every test item except
# those tagged `slow` (flag `fast`), and the `slow` job, which runs the rest (flag `slow`).
# The two jobs take different times to finish, and by default Codecov reports as soon as the
# first upload arrives. A report made from one upload alone shows much of the package as
# uncovered, so these settings make Codecov wait for both uploads before it sets the
# statuses or comments on a pull request. If the number of jobs that upload coverage
# changes, change these numbers to match.
codecov:
notify:
after_n_builds: 2
comment:
after_n_builds: 2
24 changes: 6 additions & 18 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ jobs:
# spend most of their time compiling the AD packages, run only on Ubuntu, because the AD
# code paths do not depend on the operating system. Coverage is uploaded only from the
# Ubuntu job with the latest Julia and the `slow` job, which together run the whole suite.
# The job with the latest pre-release of Julia is allowed to fail; it is there to show
# changes in Julia and LinearAlgebra, such as new methods that assume that the entries of
# a matrix commute, before they are released.
test:
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ github.event_name }}
runs-on: ${{ matrix.os }}
continue-on-error: ${{ matrix.version == 'pre' }}
if: >-
!contains(github.ref, 'refs/tags/')
&& !contains(github.event.head_commit.message, '[skip ci]')
Expand All @@ -42,6 +46,8 @@ jobs:
include:
- version: 'lts'
os: ubuntu-latest
- version: 'pre'
os: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: julia-actions/setup-julia@v3
Expand Down Expand Up @@ -86,24 +92,6 @@ jobs:
flags: slow
token: ${{ secrets.CODECOV_TOKEN }}

# The workspace test environment cannot be instantiated before Julia 1.9, so the oldest
# supported Julia runs a few smoke tests in a fresh environment instead.
compat16:
name: Smoke tests - Julia 1.6 - ubuntu-latest - ${{ github.event_name }}
runs-on: ubuntu-latest
if: >-
!contains(github.ref, 'refs/tags/')
&& !contains(github.event.head_commit.message, '[skip ci]')
&& !contains(github.event.head_commit.message, '[skip tests]')
steps:
- uses: actions/checkout@v7
- uses: julia-actions/setup-julia@v3
with:
version: '1.6'
- uses: julia-actions/cache@v3
- run: julia --project=compat16 -e 'using Pkg; Pkg.develop(path="."); Pkg.add(["ChainRulesCore", "ForwardDiff", "StaticArrays"])'
- run: julia --project=compat16 test/compat16_smoke.jl

# The docs job does not wait for the tests, so that the docs (and their previews on pull
# requests) can be checked even while some tests fail.
docs:
Expand Down
37 changes: 10 additions & 27 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Quaternionic"
uuid = "0756cd96-85bf-4b6f-a009-b5012ea7a443"
version = "4.5.0"
version = "5.0.0-dev"
authors = ["Michael Boyle <michael.oliver.boyle@gmail.com>"]

[workspace]
Expand All @@ -11,7 +11,6 @@ GenericLinearAlgebra = "14197337-ba66-59df-a3e3-ca00e7dcff7a"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

# ChainRules and Enzyme appear in [weakdeps] and [compat] partly to bound their versions:
Expand Down Expand Up @@ -42,37 +41,21 @@ QuaternionicReverseDiffExt = "ReverseDiff"
QuaternionicSymbolicsExt = "Symbolics"
QuaternionicZygoteExt = ["Zygote", "ChainRulesCore"]

# Every [weakdeps] entry is repeated here, so that Pkg before Julia 1.9, which does not
# know about [weakdeps], accepts their [compat] entries.
[extras]
ChainRules = "082447d4-558c-5d27-93f4-14fc19e9eca2"
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
DifferentiationInterface = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63"
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
FastDifferentiation = "eb9bf01b-bf85-4b60-bf87-ee5de06c00be"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
Mooncake = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6"
ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"

[compat]
ChainRules = "1.72.6"
ChainRulesCore = "1.15"
DifferentiationInterface = "0.7"
ChainRulesCore = "1.26"
DifferentiationInterface = "0.7.13"
Enzyme = "0.13.82"
FastDifferentiation = "0.4"
ForwardDiff = "0.10, 1"
GenericLinearAlgebra = "0.3.11, 0.4"
Latexify = "0.15, 0.16"
ForwardDiff = "1"
GenericLinearAlgebra = "0.3.15, 0.4"
Latexify = "0.16"
LinearAlgebra = "1, < 0.0.1"
Mooncake = "0.4.161, 0.5"
PrecompileTools = "1.2"
Random = "1, < 0.0.1"
Requires = "1"
ReverseDiff = "1.15"
StaticArrays = "1.8.1"
Symbolics = "0.1, 1, 2, 3, 4, 5, 6, 7"
ReverseDiff = "1.16"
StaticArrays = "1.9.8"
Symbolics = "7"
Zygote = "0.7.10"
julia = "1.6"
julia = "1.10"
116 changes: 52 additions & 64 deletions docs/src/differentiation.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,21 @@ directly.
| [FastDifferentiation](https://github.com/brianguenter/FastDifferentiation.jl) | Symbolic differentiation of expression graphs built from quaternions with `Node` components. | Arrays of expressions. Functions that branch on component values are not supported; see below. |
| [Symbolics](https://docs.sciml.ai/Symbolics/stable/) | Symbolic differentiation (for example, with `Symbolics.Differential`) of quaternions with `Num` components. | Symbolic expressions. A few functions that branch on component values, such as `sqrt`, are not supported. |

Every package in this table except FastDifferentiation differentiates `exp` and `log`
correctly, including at the points where their implementations switch to series
expansions or special formulas: the identity, quaternions with tiny or zero vector
part, quaternions near the negative real axis, and pure vectors. (The exceptions for
second derivatives with Mooncake are listed under [Known limitations](@ref).) For
FastDifferentiation, these functions raise an explanatory error.

The extensions for Zygote, Enzyme, Mooncake, and ReverseDiff are loaded only as package
extensions, so they require Julia 1.9 or later; the compatible versions of Zygote,
Enzyme, and Mooncake require Julia 1.10 or later. The ChainRulesCore, ForwardDiff,
FastDifferentiation, and Symbolics integrations also work on older versions of Julia.
Every package in this table except FastDifferentiation differentiates
`exp` and `log` correctly, including at the points where their
implementations switch to series expansions or special formulas: the
identity, quaternions with tiny or zero vector part, quaternions near
the negative real axis, and pure vectors. (The exceptions for second
derivatives with Mooncake are listed under [Known limitations](@ref).)
For FastDifferentiation, these functions raise an explanatory error.


## Tangent conventions

For a `Quaternion` argument, the tangent (and cotangent) is a `Quaternion` whose four
components are the four partial derivatives, as described in detail in the next
section. `Rotor` and `QuatVec` are subsets of the quaternions, and their tangents follow
from that fact:
For a `Quaternion` argument, the tangent (and cotangent) is a
`Quaternion` whose four components are the four partial derivatives,
as described in detail in the next section. `Rotor` and `QuatVec` are
subsets of the quaternions, and their tangents follow from that fact:

| Primal type | Tangent and cotangent type in the ChainRules rules | Notes |
|:------------|:------------------------------------------|:------|
Expand All @@ -80,36 +76,45 @@ from that fact:
| `AbstractQuaternion{<:Integer}` | `Quaternion{Float64}` or `QuatVec{Float64}` | As for `Integer` in ChainRulesCore. |
| A `Real` or `Complex` scalar receiving a quaternion cotangent | The scalar part of the cotangent | |

**`Rotor` arguments.** A `Rotor` is a unit quaternion, so the true tangent space at a
rotor ``R`` is the three-dimensional space ``\{R\, v : v \in \mathrm{QuatVec}\}``, which
consists of the quaternions orthogonal to ``R`` in ``\mathbb{R}^4``. The rules in this
package do not restrict cotangents to that space. Instead, a `Rotor` is differentiated
as the four real numbers it stores, and the gradient with respect to a `Rotor` argument
is the ambient gradient in ``\mathbb{R}^4`` of whatever the code computes from those
four numbers. Its component along ``R`` therefore depends on how the function is
written: for example, `abs(R)` is identically 1 for a `Rotor`, but `sqrt(R ⋅ R)` has a
nonzero gradient along ``R``. When only the part tangent to the unit sphere is
meaningful, project the gradient ``g`` onto the tangent space with ``g - (g \cdot R)\,
R``; the projections of the gradients of different implementations of the same function
agree. The gradient is returned as a `Quaternion` (or, with Enzyme, a `Rotor` holding
the raw components), never as a renormalized `Rotor`. Note that `abs` and `abs2` of a
`Rotor`-typed gradient return 1, whatever its components are, so read Enzyme's `Rotor`
gradients with `components(g)`.

**`QuatVec` arguments and outputs.** A `QuatVec` has zero scalar part, so tangents and
cotangents of `QuatVec`s have zero scalar part, and the scalar component of the input is
treated as a constant. The ChainRules rules return `QuatVec` cotangents for `QuatVec`
arguments. Enzyme and Mooncake differentiate the stored components structurally, so
the scalar slot of their raw gradient may be nonzero when the code reads it; that slot
should be ignored (for example, by using `vec(g)`).

**Complex components.** Quaternions with complex components (such as the [`Lorentz`](@ref)
transformations) follow ChainRules' convention for complex numbers: a pullback applies
the conjugate transpose of the Jacobian on ``\mathbb{C}^4``. Quaternion multiplication is
bilinear over ``\mathbb{C}``, so the pullback of ``x \mapsto a\, x\, b`` is ``\Delta
\mapsto a^\dagger\, \Delta\, b^\dagger``, where ``a^\dagger`` is the quaternion
conjugate of the componentwise complex conjugate of ``a``. For real components,
``a^\dagger = \bar{a}``, and this reduces to the real convention.
**`Rotor` arguments.** A `Rotor` is a unit quaternion, so the true
tangent space at a rotor ``R`` is the three-dimensional space ``\{R\,
v : v \in \mathrm{QuatVec}\}``, which consists of the quaternions
orthogonal to ``R`` in ``\mathbb{R}^4``. The rules in this package do
not restrict cotangents to that space. Instead, a `Rotor` is
differentiated as the four real numbers it stores, and the gradient
with respect to a `Rotor` argument is the ambient gradient in
``\mathbb{R}^4`` of whatever the code computes from those four
numbers. Its component along ``R`` therefore depends on how the
function is written: for example, `abs(R)` is identically 1 for a
`Rotor`, but `sqrt(sum(abs2, components(R)))` has a nonzero gradient
along ``R``. When only the part tangent to the unit sphere is
meaningful, project the gradient ``g`` onto the tangent space with
``g-(g \cdot R)\, R``, where ``g \cdot R`` is the dot product in
``\mathbb{R}^4``; the projections of the gradients of different
implementations of the same function agree. The gradient is returned
as a `Quaternion` (or, with Enzyme, a `Rotor` holding the raw
components), never as a renormalized `Rotor`. Note that `abs` and
`abs2` of a `Rotor`-typed gradient return 1, whatever its components
are, so read Enzyme's `Rotor` gradients with `components(g)`.

**`QuatVec` arguments and outputs.** A `QuatVec` has zero scalar
part, so tangents and cotangents of `QuatVec`s have zero scalar part,
and the scalar component of the input is treated as a constant. The
ChainRules rules return `QuatVec` cotangents for `QuatVec` arguments.
Enzyme and Mooncake differentiate the stored components structurally,
so the scalar slot of their raw gradient may be nonzero when the code
reads it; that slot should be ignored (for example, by using
`vec(g)`).

**Complex components.** Quaternions with complex components (such as
the [`Lorentz`](@ref) transformations) follow ChainRules' convention
for complex numbers: a pullback applies the conjugate transpose of the
Jacobian on ``\mathbb{C}^4``. Quaternion multiplication is bilinear
over ``\mathbb{C}``, so the pullback of ``x \mapsto a\, x\, b`` is
``\Delta \mapsto a^\dagger\, \Delta\, b^\dagger``, where ``a^\dagger``
is the quaternion conjugate of the componentwise complex conjugate of
``a``. For real components, ``a^\dagger = \bar{a}``, and this reduces
to the real convention.


## Simple generalization of complex differentiation
Expand Down Expand Up @@ -323,8 +328,8 @@ quaternion
Similarly, we can extend this with multiple arguments —
``\mathbb{R}``, ``\mathbb{H}``, or other — by appending those
arguments to the arguments of ``s``, ``t``, ``u``, and ``v``, and
similarly for multiple outputs. For example, a function
``\mathbb{R} \times \mathbb{H} \to \mathbb{H}`` would look like
similarly for multiple outputs. For example, a function ``\mathbb{R}
\times \mathbb{H} \to \mathbb{H}`` would look like
```math
f(\sigma, w, x, y, z) =
s(\sigma, w, x, y, z)
Expand Down Expand Up @@ -532,20 +537,3 @@ Quaternionic.value
Quaternionic.iszerovalue
```


## Explicit derivatives of `exp` and `log`

This package also provides some very explicit functions for computing
values and derivatives of `log` and `exp`. These are older, and may
be deprecated at some point in favor of AD. Because of the
simplifications that result from using the right types, these
derivatives are more strict about input types than the main functions
themselves: the derivatives of `exp` are defined only for `QuatVec`
arguments, and the derivatives of `log` are defined only for `Rotor`
arguments. Analytic derivatives of `slerp` and `squad` are described
in [Functions of time](@ref).

```@autodocs
Modules = [Quaternionic]
Pages = ["gradients_exp_log.jl"]
```
50 changes: 16 additions & 34 deletions docs/src/functions_of_time.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,49 +49,31 @@ which can also be called automatically by passing the corresponding
keywords to `slerp` and `squad`.

It can be very useful to compute the derivative of various functions
with respect to their arguments — for example, when computing angular
velocity of a `squad` interpolant.[^1] See [Differentiating by
quaternionic arguments](@ref) for more details. The value *and
derivative* of `slerp` can be simultaneously evaluated and
differentiated analytically with [`slerp∂slerp∂τ`](@ref) (or
automatically with `ForwardDiff`). While `squad` and its derivative
can be evaluated with [`squad∂squad∂t`](@ref), this is a relatively
low-level function; it is easier to use the relevant keyword arguments
to [`squad`](@ref).

[^1]: Essentially, we treat each quaternionic argument as a series of
four real arguments. For each input argument, the output is
generally a quaternion; interpreting those outputs as also being a
series of four real quantities, these derivatives could also be
thought of as [Jacobian
matrices](https://en.wikipedia.org/wiki/Jacobian_matrix_and_determinant)
of the relevant functions, though the actual return types are
collections of `Quaternion` objects.
with respect to their arguments — for example, when computing the
angular velocity of a `squad` interpolant. That angular velocity and
the time derivative of the interpolant are computed analytically when
the corresponding keyword arguments are passed to [`squad`](@ref).
Other derivatives, such as those of `slerp`, can be
computed by automatic differentiation; see [Differentiating by
quaternionic arguments](@ref) for details.

```@autodocs
Modules = [Quaternionic]
Pages = ["src/interpolation.jl", "src/gradients_interpolation.jl"]
Pages = ["src/interpolation.jl"]
Filter = f -> f !== Quaternionic.squad_control_points
```

Note that gradients can also be calculated automatically using
[`ForwardDiff.jl`](https://juliadiff.org/ForwardDiff.jl/).[^2] For
example, we could compute the derivative of `slerp` with respect to
the `y` component of the first input quaternion as
```julia
∂slerp∂q₁y(q₁, q₂, τ) = ForwardDiff.derivative(ϵ->slerp(q₁+ϵ*imy, q₂, τ), 0)
```
This is equal to
For example, with
[`ForwardDiff.jl`](https://juliadiff.org/ForwardDiff.jl/),[^1] the
derivatives of `slerp` with respect to `τ` and with respect to the `y`
component of the first input quaternion are
```julia
s, ∂s∂q₁, ∂s∂q₂, ∂s∂τ = slerp∂slerp(q₁, q₂, τ)
∂s∂q₁[3]
∂slerp∂τ(q₁, q₂, τ) = ForwardDiff.derivative(t -> slerp(q₁, q₂, t), τ)
∂slerp∂q₁y(q₁, q₂, τ) = ForwardDiff.derivative(ϵ -> slerp(q₁ + ϵ*imy, q₂, τ), 0)
```
though `slerp∂slerp` computes the value and all derivatives of `slerp`
simultaneously, and is at least as fast as most AD systems would be.
Nonetheless, it is useful to know that `ForwardDiff` can process
functions *involving* `Quaternionic` methods.
Each of these functions returns a `Quaternion`.

[^2]: Other AD systems are also supported, with some limitations that
[^1]: Other AD systems are also supported, with some limitations that
are described in [Differentiating by quaternionic
arguments](@ref). Please open [an
issue](https://github.com/moble/Quaternionic.jl/issues) (or pull
Expand Down
Loading