# A bug in ForwardDiff?

Consider this:

``````using ForwardDiff

function f!(F, x)
F[1] = (x[1]+3)*(x[2]^3-7)+18
F[2] = sin(x[2]*exp(x[1])-1)
return F
end

F = Array{Float64}(undef, 2)
x = [1, 1]
``````

Now let’s compute the Jacobian. The documentation, says:

``````ForwardDiff.jacobian(f!, y::AbstractArray, x::AbstractArray, cfg::JacobianConfig = JacobianConfig(f!, y, x), check=Val{true}())
``````

Return `J(f!)` evaluated at `x` , assuming `f!` is called as `f!(y, x)` where the result is stored in `y` .

since `f!(F, x)` works, then this should work:

``````∇f!(F, x) = ForwardDiff.jacobian!(f!, F, x)
``````

however

``````julia> ∇f!(F, x)
ERROR: MethodError: no method matching jacobian!(::typeof(f!), ::Vector{Float64}, ::Vector{Int64})
Closest candidates are:
jacobian!(::Union{AbstractArray, DiffResults.DiffResult}, ::Any, ::AbstractArray, ::AbstractArray) at /Users/amrods/.julia/packages/ForwardDiff/QOqCN/src/jacobian.jl:75
jacobian!(::Union{AbstractArray, DiffResults.DiffResult}, ::Any, ::AbstractArray, ::AbstractArray, ::ForwardDiff.JacobianConfig{T, V, N, D} where {V, N, D}) where T at /Users/amrods/.julia/packages/ForwardDiff/QOqCN/src/jacobian.jl:75
jacobian!(::Union{AbstractArray, DiffResults.DiffResult}, ::Any, ::AbstractArray, ::AbstractArray, ::ForwardDiff.JacobianConfig{T, V, N, D} where {V, N, D}, ::Val{CHK}) where {T, CHK} at /Users/amrods/.julia/packages/ForwardDiff/QOqCN/src/jacobian.jl:75
...
Stacktrace:
[1] ∇f!(F::Vector{Float64}, x::Vector{Int64})
@ Main ~/discourseQ4.jl:137
[2] top-level scope
@ REPL[1]:1
``````

Is that a bug in `ForwardDiff` or its documentation?

``````julia> ForwardDiff.jacobian(f!, F, x)
2×2 Matrix{Float64}:
-6.0       12.0
-0.399455  -0.399455
``````

The documentaiton said `ForwardDiff.jacobian(..)`, not `ForwardDiff.jacobian!(...)`.

1 Like

Didn’t see that `!`!!, I guess it’s time for me to take a break…