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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ __pycache__/
# generated files, so a hand-written page must not be placed in either of them.
docs/src/30-conventions/10-comparisons/
docs/src/30-conventions/20-calculations/

# The vocabulary of the narrated reviewer (diff_reviewer), which is kept out of the history
.diffreview/
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Changelog

## 3.0.2

### Fixed

* ForwardDiff and ReverseDiff work through ``d`` of a `Rotor` whose
components hold derivatives, both through `d` itself and through a
`dCalculator` of one rotor or several, whether the rotors are given
when the calculator is made, by `set_β!`, or by `recurrence!`. This
used to fail with a `MethodError` for `rotor_value`, although the
same unit quaternion given as a `Quaternion` worked, as did ``𝔇``
and the harmonics of a `Rotor`. ReverseDiff was affected only
through `dCalculator`, since `d` itself has a rule of its own there.

## 3.0.1

### Fixed
Expand Down
5 changes: 3 additions & 2 deletions src/derivatives/lifting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ value_type(::Type{T}) where {T<:Real} = T
recurrence_type(::Type{T}) where {T<:Real} = value_type(T) === T ? T : recurrence_type(value_type(T))

# The value of a number that holds derivatives, and the number itself otherwise; the
# rotation of those values; the number of directions in which a number of type `T` holds
# rotation of those values, as a `Quaternion` whether the rotation is given as a `Rotor` or
# as a `Quaternion`; the number of directions in which a number of type `T` holds
# derivatives; the tangents of a quaternion in each of those directions, as a tuple of
# tuples of four components, and those of an angle, as a tuple of numbers; and the function
# that assembles an element of type `Complex{T}` or `T` from a value and the tuple of its
# derivatives in those directions. An extension defines all of these for its number type.
real_value(x::Real) = x
rotor_value(q::Quaternion{T}) where {T} =
rotor_value(q::Union{Rotor{T}, Quaternion{T}}) where {T} =
Quaternion{value_type(T)}(real_value(q[1]), real_value(q[2]), real_value(q[3]), real_value(q[4]))
function ndirections end
function rotor_tangents end
Expand Down
52 changes: 52 additions & 0 deletions test/derivatives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,58 @@ end
end


@testitem "Derivatives: d of a Rotor that holds derivatives" setup=[DerivativeTools] tags=[:derivatives] begin
import ReverseDiff
import SphericalFunctions: recurrence!, set_β!

# The derivatives of `d` of a `Rotor` whose components hold derivatives are those of `d`
# of the same unit quaternion given as a `Quaternion`, through `d` itself and through a
# `dCalculator` of one rotor or a batch of them, whether the rotors are given when the
# calculator is made, by `set_β!`, or by `recurrence!`. ReverseDiff is tested on the
# calculators, which its tracked rotors reach as ForwardDiff's dual rotors do; on `d` it
# has a rule of its own.
blocks(S) = reduce(vcat, [vec(Array(b)) for b ∈ values(S)])
looped(c) = reduce(vcat, [vec(Array(b)) for (_, b) ∈ c])
# The rotor data of `v`: one rotor, or a vector of them, each from four components
data(rotor, v) = length(v) == 4 ? rotor(v) : [rotor(v, k) for k ∈ 1:(length(v) ÷ 4)]
# Other rotor data of the same type, from which a calculator is made before `v` is set
other(rotor, v) = data(rotor, v[mod1.(eachindex(v) .+ 1, length(v))])
function made_then_set(rotor, v, n)
c = dCalculator(other(rotor, v), n)
set_β!(c, data(rotor, v))
looped(c)
end
entry_points = [
"d" => (rotor, v, n) -> blocks(d(rotor(v), n)),
"dCalculator" => (rotor, v, n) -> looped(dCalculator(data(rotor, v), n)),
"set_β!" => made_then_set,
"recurrence!" => (rotor, v, n) ->
vec(Array(recurrence!(dCalculator(other(rotor, v), n), data(rotor, v), n))),
]
unit(v) = v ./ sqrt(sum(abs2, v))
one_rotor = unit([0.3, 0.4, -0.5, 0.6])
two_rotors = [one_rotor; unit([0.5, -0.2, 0.7, 0.1])]
for n ∈ (4, 7//2), (name, f) ∈ entry_points, v ∈ (one_rotor, two_rotors)
name == "d" && length(v) > 4 && continue
J = ForwardDiff.jacobian(w -> f(quaternionof, w, n), v)
@test ForwardDiff.jacobian(w -> f(rotorof, w, n), v) == J
name == "d" && continue
@test within(ReverseDiff.jacobian(w -> f(rotorof, w, n), v), J; rtol=16eps())
end

# The normalizing constructor `Rotor(v...)`, applied to components that are not those
# of a unit quaternion
v = [0.3, 0.4, -0.5, 0.6]
for n ∈ (4, 7//2)
@test within(
ForwardDiff.jacobian(w -> blocks(d(Rotor(w...), n)), v),
ForwardDiff.jacobian(w -> blocks(d(Quaternion(unit(w)...), n)), v);
rtol=16eps()
)
end
end


@testitem "Derivatives: first order, through calculators" setup=[ExplicitWignerMatrices, DerivativeTools] tags=[:derivatives] begin
using DifferentiationInterface: AutoForwardDiff, AutoReverseDiff, AutoEnzyme,
AutoMooncake, AutoMooncakeForward
Expand Down
Loading