-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
48 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,61 @@ | ||
function scalar_nlsolve_ad(prob, alg, args...; kwargs...) | ||
f = prob.f | ||
p = value(prob.p) | ||
|
||
u0 = value(prob.u0) | ||
newprob = NonlinearProblem(f, u0, p; prob.kwargs...) | ||
|
||
sol = solve(newprob, alg, args...; kwargs...) | ||
|
||
uu = sol.u | ||
if p isa Number | ||
f_p = ForwardDiff.derivative(Base.Fix1(f, uu), p) | ||
else | ||
f_p = ForwardDiff.gradient(Base.Fix1(f, uu), p) | ||
end | ||
f_p = scalar_nlsolve_∂f_∂p(f, uu, p) | ||
f_x = scalar_nlsolve_∂f_∂u(f, uu, p) | ||
|
||
z_arr = -inv(f_x) * f_p | ||
|
||
f_x = ForwardDiff.derivative(Base.Fix2(f, p), uu) | ||
pp = prob.p | ||
sumfun = let f_x′ = -f_x | ||
((fp, p),) -> (fp / f_x′) * ForwardDiff.partials(p) | ||
sumfun = ((z, p),) -> [zᵢ * ForwardDiff.partials(p) for zᵢ in z] | ||
if uu isa Number | ||
partials = sum(sumfun, zip(z_arr, pp)) | ||
else | ||
partials = sum(sumfun, zip(eachcol(z_arr), pp)) | ||
end | ||
partials = sum(sumfun, zip(f_p, pp)) | ||
|
||
return sol, partials | ||
end | ||
|
||
function SciMLBase.solve(prob::NonlinearProblem{<:Union{Number, SVector}, iip, | ||
<:Dual{T, V, P}}, alg::AbstractNewtonAlgorithm, args...; | ||
function SciMLBase.solve(prob::NonlinearProblem{<:Union{Number, SVector, <:AbstractArray}, | ||
iip, <:Dual{T, V, P}}, alg::AbstractNewtonAlgorithm, args...; | ||
kwargs...) where {iip, T, V, P} | ||
sol, partials = scalar_nlsolve_ad(prob, alg, args...; kwargs...) | ||
return SciMLBase.build_solution(prob, alg, Dual{T, V, P}(sol.u, partials), sol.resid; | ||
sol.retcode) | ||
dual_soln = scalar_nlsolve_dual_soln(sol.u, partials, prob.p) | ||
return SciMLBase.build_solution(prob, alg, dual_soln, sol.resid; sol.retcode) | ||
end | ||
|
||
function SciMLBase.solve(prob::NonlinearProblem{<:Union{Number, SVector}, iip, | ||
<:AbstractArray{<:Dual{T, V, P}}}, alg::AbstractNewtonAlgorithm, args...; | ||
function SciMLBase.solve(prob::NonlinearProblem{<:Union{Number, SVector, <:AbstractArray}, | ||
iip, <:AbstractArray{<:Dual{T, V, P}}}, alg::AbstractNewtonAlgorithm, args...; | ||
kwargs...) where {iip, T, V, P} | ||
sol, partials = scalar_nlsolve_ad(prob, alg, args...; kwargs...) | ||
return SciMLBase.build_solution(prob, alg, Dual{T, V, P}(sol.u, partials), sol.resid; | ||
sol.retcode) | ||
dual_soln = scalar_nlsolve_dual_soln(sol.u, partials, prob.p) | ||
return SciMLBase.build_solution(prob, alg, dual_soln, sol.resid; sol.retcode) | ||
end | ||
|
||
function scalar_nlsolve_∂f_∂p(f, u, p) | ||
ff = p isa Number ? ForwardDiff.derivative : | ||
(u isa Number ? ForwardDiff.gradient : ForwardDiff.jacobian) | ||
return ff(Base.Fix1(f, u), p) | ||
end | ||
|
||
function scalar_nlsolve_∂f_∂u(f, u, p) | ||
ff = u isa Number ? ForwardDiff.derivative : ForwardDiff.jacobian | ||
return ff(Base.Fix2(f, p), u) | ||
end | ||
|
||
function scalar_nlsolve_dual_soln(u::Number, partials, | ||
::Union{<:AbstractArray{<:Dual{T, V, P}}, Dual{T, V, P}}) where {T, V, P} | ||
return Dual{T, V, P}(u, partials[1]) | ||
end | ||
|
||
function scalar_nlsolve_dual_soln(u::AbstractArray, partials, | ||
::Union{<:AbstractArray{<:Dual{T, V, P}}, Dual{T, V, P}}) where {T, V, P} | ||
return map(((uᵢ, pᵢ),) -> Dual{T, V, P}(uᵢ, pᵢ), zip(u, partials)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters