@@ -4,7 +4,8 @@ $(TYPEDSIGNATURES)
44Generate `NonlinearSystem`, dependent variables, and parameters from an `NonlinearProblem`.
55"""
66function modelingtoolkitize (
7- prob:: NonlinearProblem ; u_names = nothing , p_names = nothing , kwargs... )
7+ prob:: Union{NonlinearProblem, NonlinearLeastSquaresProblem} ;
8+ u_names = nothing , p_names = nothing , kwargs... )
89 p = prob. p
910 has_p = ! (p isa Union{DiffEqBase. NullParameters, Nothing})
1011
@@ -37,13 +38,22 @@ function modelingtoolkitize(
3738 end
3839
3940 if DiffEqBase. isinplace (prob)
40- rhs = ArrayInterface. restructure (prob. u0, similar (vars, Num))
41- prob. f (rhs, vars, params)
41+ if prob isa NonlinearLeastSquaresProblem
42+ rhs = ArrayInterface. restructure (
43+ prob. f. resid_prototype, similar (prob. f. resid_prototype, Num))
44+ prob. f (rhs, vars, params)
45+ eqs = vcat ([0.0 ~ rhs[i] for i in 1 : length (prob. f. resid_prototype)]. .. )
46+ else
47+ rhs = ArrayInterface. restructure (prob. u0, similar (vars, Num))
48+ prob. f (rhs, vars, params)
49+ eqs = vcat ([0.0 ~ rhs[i] for i in 1 : length (rhs)]. .. )
50+ end
51+
4252 else
4353 rhs = prob. f (vars, params)
54+ out_def = prob. f (prob. u0, prob. p)
55+ eqs = vcat ([0.0 ~ rhs[i] for i in 1 : length (out_def)]. .. )
4456 end
45- out_def = prob. f (prob. u0, prob. p)
46- eqs = vcat ([0.0 ~ rhs[i] for i in 1 : length (out_def)]. .. )
4757
4858 sts = vec (collect (vars))
4959 _params = params
0 commit comments