JuliaDiff / ForwardDiff.jl

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

Support derivative(f, ::Complex) #675

Open dlfivefifty opened 12 months ago

dlfivefifty commented 12 months ago

I think the following should work:

julia> ForwardDiff.derivative(identity, 1+im)
ERROR: DimensionMismatch: derivative(f, x) expects that x is a real number (does not support Wirtinger derivatives). Separate real and imaginary parts of the input.
Stacktrace:
 [1] derivative(f::Function, x::Complex{Int64})
   @ ForwardDiff ~/.julia/packages/ForwardDiff/PcZ48/src/derivative.jl:73
 [2] top-level scope
   @ REPL[35]:1

Note that https://github.com/JuliaDiff/ForwardDiff.jl/issues?q=is%3Aissue+is%3Aclosed+complex supports Complex{<:Dual} so the implementation would be really straightforward.

I was surprised not to see a discussion on this. Would a PR adding automatic support for complex differentiation be merged?

ranocha commented 6 months ago

I also cam across this - it would be quite nice, also for classroom demonstration