From 7e4b3ff46f6fccddef6910e995d2e5509c6a4669 Mon Sep 17 00:00:00 2001 From: Mike Boyle Date: Tue, 17 Nov 2026 09:39:23 -0500 Subject: [PATCH] Fix derivatives through d of a Rotor that holds derivatives ForwardDiff and ReverseDiff failed with a MethodError for `rotor_value` on `d` of a `Rotor` whose components hold derivatives, through `d` itself and through a `dCalculator`, although the same unit quaternion given as a `Quaternion` worked. A lifting calculator of `d` views the caller's rotors through `LiftedValues`, whose indexing called a `rotor_value` defined only for `Quaternion`, and a `Rotor` is not a `Quaternion` in Quaternionic 5. * `.diffreview/`, the narrated reviewer's vocabulary directory, is ignored. * A new test item compares derivatives of `d` of a `Rotor` with those of the same `Quaternion`, through `d` and through a `dCalculator` of one rotor or two, under ForwardDiff and ReverseDiff. * `rotor_value` accepts a `Rotor` as well as a `Quaternion`, and returns a `Quaternion` of the values for both. * The changelog records the fix under a new 3.0.2 section. Co-Authored-By: Claude Opus 5.5 --- .gitignore | 3 +++ CHANGELOG.md | 13 ++++++++++ src/derivatives/lifting.jl | 5 ++-- test/derivatives.jl | 52 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 640400f2..e57ae084 100644 --- a/.gitignore +++ b/.gitignore @@ -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/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a2dd35f..88f8fb68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/derivatives/lifting.jl b/src/derivatives/lifting.jl index 5ceaded9..2d6b5653 100644 --- a/src/derivatives/lifting.jl +++ b/src/derivatives/lifting.jl @@ -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 diff --git a/test/derivatives.jl b/test/derivatives.jl index 195fea31..607e342a 100644 --- a/test/derivatives.jl +++ b/test/derivatives.jl @@ -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