JuliaDiff / ForwardDiff.jl

Forward Mode Automatic Differentiation for Julia
Other
892 stars 145 forks source link

incorrect 2nd derivative of complex exponential #653

Open stevengj opened 1 year ago

stevengj commented 1 year ago

This, from discourse, looks like a bug:

julia> p(s) = exp(1im*(2π*s - π/2));

julia> p′(s) = ForwardDiff.derivative(p, s);

julia> p″(s) = ForwardDiff.derivative(p′, s);

julia> p″(0.2499999999999)
-39.47841760435743 + 2.4807694081270598e-11im

julia> p″(0.25)
0.0 + 0.0im

I don't think mere floating-point error can explain why p″(0.25) is suddenly zero — looks like a misplaced conditional or something. Note that the correct answer for p″(0.25) is exactly -(2π)² ≈ -39.47841760435743.

(The first derivative p′ looks okay.)

mikmoore commented 1 year ago

(Very slightly) reduced example:

julia> p(s) = exp(1im*s);

julia> p′(s) = ForwardDiff.derivative(p, s);

julia> p″(s) = ForwardDiff.derivative(p′, s);

julia> p″(prevfloat(0.0))
-1.0 + 5.0e-324im

julia> p″(0.0)
0.0 + 0.0im

x^(1im*s) appears to exhibit this problem for any x. expm1(1im*s) has this problem. exp2(1im*s) and exp10(1im*s) are mysteriously correct.

mcabbott commented 1 year ago

I believe this is one more case fixed by #481:

julia> p″(0.2499999999999)
-39.47841760435743 + 2.4807694081270598e-11im

julia> p″(0.25)
-39.47841760435743 + 0.0im

(jl_aRIFuW) pkg> st ForwardDiff
Status `/private/var/folders/yq/4p2zwd614y59gszh7y9ypyhh0000gn/T/jl_aRIFuW/Project.toml`
  [f6369f11] ForwardDiff v0.11.0-DEV `https://github.com/JuliaDiff/ForwardDiff.jl.git#master`