|
| 1 | +module NonlinearSolveMINPACKExt |
| 2 | + |
| 3 | +using NonlinearSolve, SciMLBase |
| 4 | +using MINPACK |
| 5 | + |
| 6 | +function SciMLBase.__solve(prob::Union{NonlinearProblem{uType, iip}, |
| 7 | + NonlinearLeastSquaresProblem{uType, iip}}, alg::CMINPACK, args...; |
| 8 | + abstol = 1e-6, maxiters = 100000, alias_u0::Bool = false, |
| 9 | + kwargs...) where {uType, iip} |
| 10 | + if prob.u0 isa Number |
| 11 | + u0 = [prob.u0] |
| 12 | + else |
| 13 | + u0 = NonlinearSolve.__maybe_unaliased(prob.u0, alias_u0) |
| 14 | + end |
| 15 | + |
| 16 | + sizeu = size(prob.u0) |
| 17 | + p = prob.p |
| 18 | + |
| 19 | + # unwrapping alg params |
| 20 | + show_trace = alg.show_trace |
| 21 | + tracing = alg.tracing |
| 22 | + io = alg.io |
| 23 | + |
| 24 | + if !iip && prob.u0 isa Number |
| 25 | + f! = (du, u) -> (du .= prob.f(first(u), p); Cint(0)) |
| 26 | + elseif !iip && prob.u0 isa Vector{Float64} |
| 27 | + f! = (du, u) -> (du .= prob.f(u, p); Cint(0)) |
| 28 | + elseif !iip && prob.u0 isa AbstractArray |
| 29 | + f! = (du, u) -> (du .= vec(prob.f(reshape(u, sizeu), p)); Cint(0)) |
| 30 | + elseif prob.u0 isa Vector{Float64} |
| 31 | + f! = (du, u) -> prob.f(du, u, p) |
| 32 | + else # Then it's an in-place function on an abstract array |
| 33 | + f! = (du, u) -> (prob.f(reshape(du, sizeu), reshape(u, sizeu), p); du = vec(du); 0) |
| 34 | + end |
| 35 | + |
| 36 | + u = zero(u0) |
| 37 | + resid = NonlinearSolve.evaluate_f(prob, u) |
| 38 | + m = length(resid) |
| 39 | + |
| 40 | + method = ifelse(alg.method === :auto, |
| 41 | + ifelse(prob isa NonlinearLeastSquaresProblem, :lm, :hydr), alg.method) |
| 42 | + |
| 43 | + if SciMLBase.has_jac(prob.f) |
| 44 | + if !iip && prob.u0 isa Number |
| 45 | + g! = (du, u) -> (du .= prob.f.jac(first(u), p); Cint(0)) |
| 46 | + elseif !iip && prob.u0 isa Vector{Float64} |
| 47 | + g! = (du, u) -> (du .= prob.f.jac(u, p); Cint(0)) |
| 48 | + elseif !iip && prob.u0 isa AbstractArray |
| 49 | + g! = (du, u) -> (du .= vec(prob.f.jac(reshape(u, sizeu), p)); Cint(0)) |
| 50 | + elseif prob.u0 isa Vector{Float64} |
| 51 | + g! = (du, u) -> prob.f.jac(du, u, p) |
| 52 | + else # Then it's an in-place function on an abstract array |
| 53 | + g! = function (du, u) |
| 54 | + prob.f.jac(reshape(du, sizeu), reshape(u, sizeu), p) |
| 55 | + du = vec(du) |
| 56 | + return CInt(0) |
| 57 | + end |
| 58 | + end |
| 59 | + original = MINPACK.fsolve(f!, g!, u0, m; tol = abstol, show_trace, tracing, method, |
| 60 | + iterations = maxiters, kwargs...) |
| 61 | + else |
| 62 | + original = MINPACK.fsolve(f!, u0, m; tol = abstol, show_trace, tracing, method, |
| 63 | + iterations = maxiters, kwargs...) |
| 64 | + end |
| 65 | + |
| 66 | + u = reshape(original.x, size(u)) |
| 67 | + resid = original.f |
| 68 | + retcode = original.converged ? ReturnCode.Success : ReturnCode.Failure |
| 69 | + |
| 70 | + return SciMLBase.build_solution(prob, alg, u, resid; retcode, original) |
| 71 | +end |
| 72 | + |
| 73 | +end |
0 commit comments