Hello,
I was successfully using the following block of code which uses ForwardDiff.gradient on a function, that has a Nonlinearsolve.NonlinearLeastSquaresProblem. It was giving out correct gradients (and jacobians) until recently, and now, it no longer works. I would really appreciate any help in figuring out why this is happening.
Here is a MWE
using ForwardDiff, LinearAlgebras
import NonlinearSolve as NLS
l1 = 10.0;
l2 = 5.0;
function objective!(F,u,p)
th1 = u[1];
th2 = u[2];
x = p[1]; y = p[2];
F[1] = l1*cos(th1)+l2*cos(th1+th2)-x;
F[2] = l1*sin(th1)+l2*sin(th1+th2)-y;
return F
end
function cog_pos(pos)
T = eltype(pos)
u0 = T[0.0; 0.0]; # initial guess
prob2 = NLS.NonlinearLeastSquaresProblem(objective!, u0, pos)
sol = NLS.solve(prob2)
th1 = sol.u[1];
th2 = sol.u[2];
cog_pos_x = 0.5*l1*cos(th1);
return cog_pos_x
end
p = [5.0; 7.0];
ForwardDiff.jacobian(cog_pos,p)
I get a very large MethodError: no method matching Float64, which I am not sharing here. The stacktrace is as follows:
Stacktrace:
[1] convert(::Type{Float64}, x::ForwardDiff.Dual{ForwardDiff.Tag{NonlinearSolveBaseForwardDiffExt.var"#6#7"{NonlinearSolveBase.var"#nlls_generate_vjp_function##8#nlls_generate_vjp_function##9"{typeof(objective!), SciMLBase.NonlinearSolution{Float64, 1, Vector{Float64}, Vector{Float64}, SciMLBase.NonlinearLeastSquaresProblem{Vector{Float64}, true, Vector{Float64}, SciMLBase.NonlinearFunction{true, SciMLBase.AutoSpecialize, NonlinearSolveBase.AutoSpecializeCallable{FunctionWrappersWrappers.FunctionWrappersWrapper{Tuple{FunctionWrappers.FunctionWrapper{Nothing, Tuple{Vector{Float64}, Vector{Float64}, Vector{Float64}}}, FunctionWrappers.FunctionWrapper{Nothing, Tuple{Vector{ForwardDiff.Dual{ForwardDiff.Tag{NonlinearSolveBase.NonlinearSolveTag, Float64}, Float64, 1}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{NonlinearSolveBase.NonlinearSolveTag, Float64}, Float64, 1}}, Vector{Float64}}}, FunctionWrappers.FunctionWrapper{Nothing, Tuple{Vector{ForwardDiff.Dual{ForwardDiff.Tag{NonlinearSolveBase.NonlinearSolveTag, Float64}, Float64, 1}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{NonlinearSolveBase.NonlinearSolveTag, Float64}, Float64, 1}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{NonlinearSolveBase.NonlinearSolveTag, Float64}, Float64, 1}}}}, FunctionWrappers.FunctionWrapper{Nothing, Tuple{Vector{ForwardDiff.Dual{ForwardDiff.Tag{NonlinearSolveBase.NonlinearSolveTag, Float64}, Float64, 1}}, Vector{Float64}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{NonlinearSolveBase.NonlinearSolveTag, Float64}, Float64, 1}}}}}, FunctionWrappersWrappers.AllowNonIsBits, FunctionWrappersWrappers.SingleCacheStorage}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing, Nothing}, @Kwargs{}, Nothing, Nothing}, NonlinearSolveBase.NonlinearSolvePolyAlgorithm{Val{6}, Tuple{NonlinearSolveFirstOrder.GeneralizedFirstOrderAlgorithm{Missing, Missing, NonlinearSolveBase.NewtonDescent{Nothing}, Nothing, Nothing, Nothing, Nothing, Val{false}}, NonlinearSolveFirstOrder.GeneralizedFirstOrderAlgorithm{Missing, NonlinearSolveFirstOrder.LevenbergMarquardtTrustRegion{Float64}, NonlinearSolveBase.DampedNewtonDescent{Nothing, Float64, NonlinearSolveFirstOrder.LevenbergMarquardtDampingFunction{Float64, Float64, Float64}}, Nothing, Nothing, Nothing, Nothing, Val{true}}, NonlinearSolveFirstOrder.GeneralizedFirstOrderAlgorithm{Missing, NonlinearSolveFirstOrder.GenericTrustRegionScheme{NonlinearSolveFirstOrder.RadiusUpdateSchemes.__Simple, Rational{Int64}, Rational{Int64}, Rational{Int64}, Rational{Int64}, Rational{Int64}, Rational{Int64}, Rational{Int64}}, NonlinearSolveBase.Dogleg{NonlinearSolveBase.NewtonDescent{Nothing}, NonlinearSolveBase.SteepestDescent{Nothing}}, Nothing, Nothing, Nothing, Nothing, Val{false}}, NonlinearSolveFirstOrder.GeneralizedFirstOrderAlgorithm{LineSearch.BackTracking{Nothing, Float64, Float64, Float64, Val{3}, Float64, Bool}, Missing, NonlinearSolveBase.NewtonDescent{Nothing}, Nothing, Nothing, Nothing, Nothing, Val{false}}, NonlinearSolveFirstOrder.GeneralizedFirstOrderAlgorithm{Missing, NonlinearSolveFirstOrder.GenericTrustRegionScheme{NonlinearSolveFirstOrder.RadiusUpdateSchemes.__Bastin, Rational{Int64}, Rational{Int64}, Rational{Int64}, Rational{Int64}, Rational{Int64}, Rational{Int64}, Rational{Int64}}, NonlinearSolveBase.Dogleg{NonlinearSolveBase.NewtonDescent{Nothing}, NonlinearSolveBase.SteepestDescent{Nothing}}, Nothing, Nothing, Nothing, Nothing, Val{false}}, NonlinearSolveFirstOrder.GeneralizedFirstOrderAlgorithm{Missing, NonlinearSolveFirstOrder.LevenbergMarquardtTrustRegion{Float64}, NonlinearSolveBase.GeodesicAcceleration{NonlinearSolveBase.DampedNewtonDescent{Nothing, Float64, NonlinearSolveFirstOrder.LevenbergMarquardtDampingFunction{Float64, Float64, Float64}}, Float64, Float64}, Nothing, Nothing, Nothing, Nothing, Val{true}}}}, Any, Nothing, SciMLBase.NLStats, NonlinearSolveBase.NonlinearSolveTrace{Val{false}, Val{false}, Nothing, NonlinearSolveBase.NonlinearSolveTracing{Val{:minimal}}, SciMLBase.NonlinearLeastSquaresProblem{Vector{Float64}, true, Vector{Float64}, SciMLBase.NonlinearFunction{true, SciMLBase.AutoSpecialize, NonlinearSolveBase.AutoSpecializeCallable{FunctionWrappersWrappers.FunctionWrappersWrapper{Tuple{FunctionWrappers.FunctionWrapper{Nothing, Tuple{Vector{Float64}, Vector{Float64}, Vector{Float64}}}, FunctionWrappers.FunctionWrapper{Nothing, Tuple{Vector{ForwardDiff.Dual{ForwardDiff.Tag{NonlinearSolveBase.NonlinearSolveTag, Float64}, Float64, 1}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{NonlinearSolveBase.NonlinearSolveTag, Float64}, Float64, 1}}, Vector{Float64}}}, FunctionWrappers.FunctionWrapper{Nothing, Tuple{Vector{ForwardDiff.Dual{ForwardDiff.Tag{NonlinearSolveBase.NonlinearSolveTag, Float64}, Float64, 1}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{NonlinearSolveBase.NonlinearSolveTag, Float64}, Float64, 1}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{NonlinearSolveBase.NonlinearSolveTag, Float64}, Float64, 1}}}}, FunctionWrappers.FunctionWrapper{Nothing, Tuple{Vector{ForwardDiff.Dual{ForwardDiff.Tag{NonlinearSolveBase.NonlinearSolveTag, Float64}, Float64, 1}}, Vector{Float64}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{NonlinearSolveBase.NonlinearSolveTag, Float64}, Float64, 1}}}}}, FunctionWrappersWrappers.AllowNonIsBits, FunctionWrappersWrappers.SingleCacheStorage}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing, Nothing}, @Kwargs{}, Nothing, Nothing}}}, ADTypes.AutoForwardDiff{nothing, Nothing}, Nothing}, Vector{Float64}}, Float64}, Float64, 2})
@ Base .\number.jl:7
[2] setindex!
@ .\array.jl:985 [inlined]
[3] macro expansion
@ .\broadcast.jl:995 [inlined]
[4] macro expansion
@ .\simdloop.jl:77 [inlined]
[5] copyto!
@ .\broadcast.jl:994 [inlined]
[6] copyto!
...
@ ForwardDiff C:\Users\user\.julia\packages\ForwardDiff\z3jRk\src\jacobian.jl:19
[40] jacobian(f::typeof(cog_pos), x::Vector{Float64})
@ ForwardDiff C:\Users\user\.julia\packages\ForwardDiff\z3jRk\src\jacobian.jl:19
[41] top-level scope
@ c:\Users\user\Documents\final chapter julia report\jl_notebook_cell_df34fa98e69747e1a8f8a730347b8e2f_X21sZmlsZQ==.jl:1
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...
The output of Pkg.status() is
[26cc04aa] FiniteDifferences v0.12.33
[f6369f11] ForwardDiff v1.3.3
[e9467ef8] GLMakie v0.13.10
[ee78f7c6] Makie v0.24.10
[8913a72c] NonlinearSolve v4.19.0
[1ed8b502] SciMLSensitivity v7.106.0
[e88e6eb3] Zygote v0.7.10
[37e2e46d] LinearAlgebra v1.12.0