Error using ForwardDiff

Hi, I am new to ForwardDiff. However, I get an error which i cant understand why occurs.

function get_rho(a)
  return sqrt(2.0*a+1.0)/(a+1.0)
end
@time ForwardDiff.gradient(a ->get_rho(a), -0.43)

From the code above I get the following error:

MethodError: Cannot convert an object of type Float64 to an object of type ForwardDiff.GradientConfig
This may have arisen from a call to the constructor ForwardDiff.GradientConfig(…),
since type constructors fall back to convert methods.
gradient(::##23#24, ::Float64) at gradient.jl:6
include_string(::String, ::String) at loading.jl:515
include_string(::String, ::String, ::Int64) at eval.jl:30
include_string(::Module, ::String, ::String, ::Int64, ::Vararg{Int64,N} where N) at eval.jl:34
(::Atom.##49#52{String,Int64,String})() at eval.jl:50
withpath(::Atom.##49#52{String,Int64,String}, ::String) at utils.jl:30
withpath(::Function, ::String) at eval.jl:38
macro expansion at eval.jl:49 [inlined]
(::Atom.##48#51{Dict{String,Any}})() at task.jl:80

Any help with this, will be highly appreciated :slight_smile:

gradient is for multivariate functions. You can use derivative. Also, the anonymous function is unnecessary here.
This works:

julia> @time ForwardDiff.derivative(get_rho, -0.43)
  0.000007 seconds (5 allocations: 176 bytes)
3.537160173048399
2 Likes