From 9cc76a216b4e372a44a60715d02214183482d78b Mon Sep 17 00:00:00 2001 From: Mike Boyle Date: Mon, 28 Sep 2026 12:11:30 -0400 Subject: [PATCH] Fix derivatives of rotor powers, slerp, and distance2 --- src/distance.jl | 11 +++++++- src/math.jl | 30 ++++++++++++++++------ test/nested_forwarddiff.jl | 51 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 8 deletions(-) diff --git a/src/distance.jl b/src/distance.jl index 50b41505..3eb83ccd 100644 --- a/src/distance.jl +++ b/src/distance.jl @@ -38,5 +38,14 @@ distance2(q₁::Rotor, q₂::Rotor) = _abs2_small_vec_log(q₁ / q₂) @inline function _abs2_small_vec_log(q::Rotor{T}) where {T} # Like `min(abs2(log(q)), abs2(log(-q)))`, but assumes the norm of `q` is 1 - atan(absvec(q), abs(q[1]))^2 + x = abs2vec(q) / q[1]^2 + if x ≤ ∜(eps(typeof(x))) / 2 + # Near the identity, `absvec` is the square root of a tiny number, whose derivatives + # are huge and lose all accuracy when they cancel, or are NaN when it is exactly 0. + # This series for `atan(√x)^2` in `x` is smooth instead; its relative truncation + # error is about `x⁴/3`, which is below `eps` for this range of `x`. + x * evalpoly(x, (1, -2//3, 23//45, -44//105)) + else + atan(absvec(q), abs(q[1]))^2 + end end diff --git a/src/math.jl b/src/math.jl index 08385b21..eecd0ba1 100644 --- a/src/math.jl +++ b/src/math.jl @@ -550,15 +550,31 @@ function Base.:^(q::Quaternion, s::Number) end function Base.:^(q::Rotor, s::Number) q = float(q) + x = abs2vec(q) / q[1]^2 + if q[1] > 0 && x ≤ eps(typeof(x))^(1//5) + # Near the identity, `absvec(q)` is the square root of a tiny number, whose + # derivatives are huge and lose all accuracy when they cancel, or are NaN when it is + # exactly 0. Instead, we write log(q) = A 𝐯/q[1], where A = atan(√x)/√x, and q^s = + # exp(f 𝐯) with f = s A/q[1], using series in `x` and in y = |f 𝐯|² that are + # smooth at the identity. Five terms suffice for a truncation error below `eps`. + A = evalpoly(x, (1, -1//3, 1//5, -1//7, 1//9)) + f = s * A / q[1] + y = f^2 * abs2vec(q) + c, sincu = if y ≤ eps(typeof(y))^(1//5) + evalpoly(y, (1, -1//2, 1//24, -1//720, 1//40320)), + evalpoly(y, (1, -1//6, 1//120, -1//5040, 1//362880)) + else + # Only reached for large `s`, so that √y is not small + a = √y + cos(a), sin(a) / a + end + return Rotor{typeof(c)}([c, sincu*f*q[2], sincu*f*q[3], sincu*f*q[4]]) + end absolutevec = absvec(q) if absolutevec ≤ eps(typeof(absolutevec)) - if q[1] < 0 - # log(q) ≈ π𝐤 - sin_πs, cos_πs = sincospi(oftype(absolutevec, s)) - return Rotor{basetype(q)}([cos_πs, 0, 0, sin_πs]) - end - # log(q) ≈ 0 - return one(q) + # log(q) ≈ π𝐤, because q ≈ -1 + sin_πs, cos_πs = sincospi(oftype(absolutevec, s)) + return Rotor{basetype(q)}([cos_πs, 0, 0, sin_πs]) end f1 = s * atan(absolutevec, q[1]) sin_f1, cos_f1 = sincos(f1) diff --git a/test/nested_forwarddiff.jl b/test/nested_forwarddiff.jl index eef83fa6..50955e88 100644 --- a/test/nested_forwarddiff.jl +++ b/test/nested_forwarddiff.jl @@ -121,6 +121,57 @@ end ) end +@testitem "nested ForwardDiff: Rotor powers and slerp" tags=[:validation, :fast] setup=[NestedForwardDiff] begin + using ForwardDiff + using .NestedForwardDiff: compare_derivatives + + n̂ = normalize(quatvec(1.0, -2.0, 3.0)) + nx, ny, nz = vec(n̂) + R₀ = rotor(0.4, -0.2, 0.7, 0.5) + # A rotation by `θ` about `n̂`, written without branches, for use in reference results + ref(θ) = Quaternion(cos(θ/2), sin(θ/2)*nx, sin(θ/2)*ny, sin(θ/2)*nz) + + # At t = 0, `exp(t * n̂ / 2)` is exactly the identity, where `R ^ s` takes a special branch + for s ∈ (0.3, 2.5, -0.7) + @test compare_derivatives( + t -> components(exp(t * n̂ / 2) ^ s), + t -> components(ref(s*t)), + ) + end + # slerp(R₀, R, τ) = (R / R₀)^τ R₀, so it reaches the same branch when R = R₀ + for τ ∈ (0.3, 0.8), Rₐ ∈ (one(RotorF64), R₀) + @test compare_derivatives( + t -> components(slerp(Rₐ, exp(t * n̂ / 2) * Rₐ, τ)), + t -> components(ref(τ*t) * Rₐ), + ) + end + # The Jacobian with respect to all four components at the identity: normalization + # removes the scalar direction, and the power scales the vector directions by `s` + J = ForwardDiff.jacobian(u -> components(rotor(u...) ^ 0.3), [1.0, 0.0, 0.0, 0.0]) + @test J ≈ [0 0 0 0; 0 0.3 0 0; 0 0 0.3 0; 0 0 0 0.3] atol=1e-14 +end + +@testitem "nested ForwardDiff: distance2" tags=[:validation, :fast] setup=[NestedForwardDiff] begin + using ForwardDiff + using .NestedForwardDiff: compare_derivatives + + n̂ = normalize(quatvec(1.0, -2.0, 3.0)) + R₀ = rotor(0.4, -0.2, 0.7, 0.5) + # Rotors separated by a rotation through `t` are a distance `t/2` apart. At t = 0 with + # Rₐ = 1, the quotient inside `distance2` is exactly the identity. + for Rₐ ∈ (one(RotorF64), R₀) + @test compare_derivatives( + t -> [distance2(exp(t * n̂ / 2) * Rₐ, Rₐ)], + t -> [(t/2)^2], + ) + end + # The gradient at the minimum is zero + for Rₐ ∈ (one(RotorF64), R₀) + g = ForwardDiff.gradient(u -> distance2(rotor(u...), Rₐ), collect(components(Rₐ))) + @test g ≈ zeros(4) atol=1e-14 + end +end + @testitem "nested ForwardDiff: iszerovalue on complex" tags=[:unit, :fast] begin using ForwardDiff import Quaternionic: iszerovalue