Dual{::ForwardDiff...} type error when JuMP tries autodiff User-defined Function

The problem is that typing the elements of the vector as Real is not a concrete type.

Instead of

function foo(x)
    y = Vector{Real}(undef, 2)
    y[1] = x
    y[2] = 2 * x
    return sum(y)
end

Type the elements of the vector based on the input arguments

function foo(x::T) where {T <: Real}
    y = Vector{T}(undef, 2)
    y[1] = x
    y[2] = 2 * x
    return sum(y)
end
2 Likes