Greetings! Listed below is a small piece of code that composes QuadGK and ForwardDiff and that fails. Including the resulting method error message for the function extract_derivative(). I have two questions regarding this code.
1/ Is there a way to see that the quadqk() call results in the function u(x) = x - 0.5 other then by sampling the output u(x)?
2/ How do I make the ForwardDiff.derivative() call work (i.e. avoid the method error)?
Thx!
using QuadGK
using ForwardDiff
# define two input integrand
integrand(x,y) = x - y
# compute integral by quadrature over second input - results in u(x) = x - 0.5
u(x) = quadgk(y -> integrand(x,y), 0, 1)
# compute derivative - should result in du/dx = 1
# toy model ForwardDiff.derivative(x -> x^2, 1) works just fine
# alternative attempts ForwardDiff.derivative(x -> u(x), 1) or
# ForwardDiff.derivative(x -> u(x), 1) result in same method error
ForwardDiff.derivative(u, 1)
ERROR: MethodError: no method matching extract_derivative(::Type{ForwardDiff.Tag{var"#9#10", Int64}}, ::typeof(u))
Closest candidates are:
extract_derivative(::Type{T}, ::ForwardDiff.Dual) where T at ~/.julia/packages/ForwardDiff/PcZ48/src/derivative.jl:84
extract_derivative(::Type{T}, ::Real) where T at ~/.julia/packages/ForwardDiff/PcZ48/src/derivative.jl:82
extract_derivative(::Type{T}, ::AbstractArray) where T at ~/.julia/packages/ForwardDiff/PcZ48/src/derivative.jl:85
…
Stacktrace:
[1] derivative(f::var"#9#10", x::Int64)
@ ForwardDiff ~/.julia/packages/ForwardDiff/PcZ48/src/derivative.jl:14
[2] top-level scope
@ REPL[27]:1