-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #203 from avik-pal/ap/cleanup
Towards a cleaner and more maintainable internals of NonlinearSolve.jl
- Loading branch information
Showing
18 changed files
with
1,081 additions
and
2,124 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,2 +1,3 @@ | ||
style = "sciml" | ||
format_markdown = true | ||
format_markdown = true | ||
annotate_untyped_fields_with_any = false |
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
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
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 |
---|---|---|
|
@@ -25,3 +25,4 @@ Manifest.toml | |
docs/src/assets/Project.toml | ||
|
||
.vscode | ||
wip |
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
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
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
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,44 +1,63 @@ | ||
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),) -> map(zᵢ -> zᵢ * ForwardDiff.partials(p), z) | ||
if uu isa Number | ||
partials = sum(sumfun, zip(z_arr, pp)) | ||
elseif p isa Number | ||
partials = sumfun((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, StaticArraysCore.SVector}, | ||
iip, | ||
<:Dual{T, V, P}}, | ||
alg::AbstractNewtonAlgorithm, | ||
args...; kwargs...) where {iip, T, V, P} | ||
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; | ||
retcode = 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, StaticArraysCore.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; | ||
retcode = 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) | ||
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 |
Oops, something went wrong.