diff --git a/src/math.jl b/src/math.jl index f7b356b..0cfcd20 100644 --- a/src/math.jl +++ b/src/math.jl @@ -193,15 +193,25 @@ rotor(0.7071067811865476 + 0.7071067811865475š¢ + 0.0š£ + 0.0š¤) ``` """ function Base.exp(q::Quaternion{T}) where {T} - a = absvec(q) + a = abs2vec(q) e = exp(q[1]) - esinc = e * _sincu(a) - Quaternion{typeof(esinc)}(e*cos(a), esinc*q[2], esinc*q[3], esinc*q[4]) + if iszero(a) + # Take this a little seriously, to obtain accurate ForwardDiff derivative + Rotor{typeof(e)}(e*(1 - a/2 + a^2/24), e*vāƒ—[2], e*vāƒ—[3], e*vāƒ—[4]) + else + esinc = e * _sincu(āˆša) + Quaternion{typeof(esinc)}(e*cos(āˆša), esinc*q[2], esinc*q[3], esinc*q[4]) + end end function Base.exp(vāƒ—::QuatVec{T}) where {T} - a = absvec(vāƒ—) - sinc = _sincu(a) - Rotor{typeof(sinc)}(cos(a), sinc*vāƒ—[2], sinc*vāƒ—[3], sinc*vāƒ—[4]) + a = abs2vec(vāƒ—) + if iszero(a) + # Take this a little seriously, to obtain accurate ForwardDiff derivative + Rotor{typeof(a)}(1 - a/2 + a^2/24, vāƒ—[2], vāƒ—[3], vāƒ—[4]) + else + sinc = _sincu(āˆša) + Rotor{typeof(sinc)}(cos(āˆša), sinc*vāƒ—[2], sinc*vāƒ—[3], sinc*vāƒ—[4]) + end end @doc raw"""