-
-
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.
feat: ForwardDiff support in NonlinearSolveBase
- Loading branch information
Showing
8 changed files
with
136 additions
and
6 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
26 changes: 26 additions & 0 deletions
26
lib/BracketingNonlinearSolve/ext/BracketingNonlinearSolveForwardDiffExt.jl
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module BracketingNonlinearSolveForwardDiffExt | ||
|
||
using CommonSolve: CommonSolve | ||
using ForwardDiff: ForwardDiff, Dual | ||
using NonlinearSolveBase: nonlinearsolve_forwarddiff_solve, nonlinearsolve_dual_solution | ||
using SciMLBase: SciMLBase, IntervalNonlinearProblem | ||
|
||
using BracketingNonlinearSolve: Bisection, Brent, Alefeld, Falsi, ITP, Ridder | ||
|
||
for algT in (Bisection, Brent, Alefeld, Falsi, ITP, Ridder) | ||
@eval function CommonSolve.solve( | ||
prob::IntervalNonlinearProblem{ | ||
uType, iip, <:Union{<:Dual{T, V, P}, <:AbstractArray{<:Dual{T, V, P}}}}, | ||
alg::$(algT), | ||
args...; | ||
kwargs...) where {uType, iip, T, V, P} | ||
sol, partials = nonlinearsolve_forwarddiff_solve(prob, alg, args...; kwargs...) | ||
dual_soln = nonlinearsolve_dual_solution(sol.u, partials, prob.p) | ||
return SciMLBase.build_solution( | ||
prob, alg, dual_soln, sol.resid; sol.retcode, sol.stats, | ||
sol.original, left = Dual{T, V, P}(sol.left, partials), | ||
right = Dual{T, V, P}(sol.right, partials)) | ||
end | ||
end | ||
|
||
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
75 changes: 74 additions & 1 deletion
75
lib/NonlinearSolveBase/ext/NonlinearSolveBaseForwardDiffExt.jl
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,9 +1,82 @@ | ||
module NonlinearSolveBaseForwardDiffExt | ||
|
||
using CommonSolve: solve | ||
using FastClosures: @closure | ||
using ForwardDiff: ForwardDiff, Dual | ||
using NonlinearSolveBase: Utils | ||
using SciMLBase: SciMLBase, IntervalNonlinearProblem, NonlinearProblem, | ||
NonlinearLeastSquaresProblem, remake | ||
|
||
using NonlinearSolveBase: NonlinearSolveBase, ImmutableNonlinearProblem, Utils | ||
|
||
Utils.value(::Type{Dual{T, V, N}}) where {T, V, N} = V | ||
Utils.value(x::Dual) = Utils.value(ForwardDiff.value(x)) | ||
|
||
function NonlinearSolveBase.nonlinearsolve_forwarddiff_solve( | ||
prob::Union{IntervalNonlinearProblem, NonlinearProblem, ImmutableNonlinearProblem}, | ||
alg, args...; kwargs...) | ||
p = Utils.value(prob.p) | ||
if prob isa IntervalNonlinearProblem | ||
tspan = Utils.value.(prob.tspan) | ||
newprob = IntervalNonlinearProblem(prob.f, tspan, p; prob.kwargs...) | ||
else | ||
newprob = remake(prob; p, u0 = Utils.value(prob.u0)) | ||
end | ||
|
||
sol = solve(newprob, alg, args...; kwargs...) | ||
|
||
uu = sol.u | ||
Jₚ = nonlinearsolve_∂f_∂p(prob, prob.f, uu, p) | ||
Jᵤ = nonlinearsolve_∂f_∂u(prob, prob.f, uu, p) | ||
z = -Jᵤ \ Jₚ | ||
pp = prob.p | ||
sumfun = ((z, p),) -> map(Base.Fix2(*, ForwardDiff.partials(p)), z) | ||
|
||
if uu isa Number | ||
partials = sum(sumfun, zip(z, pp)) | ||
elseif p isa Number | ||
partials = sumfun((z, pp)) | ||
else | ||
partials = sum(sumfun, zip(eachcol(z), pp)) | ||
end | ||
|
||
return sol, partials | ||
end | ||
|
||
function nonlinearsolve_∂f_∂p(prob, f::F, u, p) where {F} | ||
if isinplace(prob) | ||
f = @closure p -> begin | ||
du = Utils.safe_similar(u, promote_type(eltype(u), eltype(p))) | ||
f(du, u, p) | ||
return du | ||
end | ||
else | ||
f = Base.Fix1(f, u) | ||
end | ||
if p isa Number | ||
return Utils.safe_reshape(ForwardDiff.derivative(f, p), :, 1) | ||
elseif u isa Number | ||
return Utils.safe_reshape(ForwardDiff.gradient(f, p), 1, :) | ||
else | ||
return ForwardDiff.jacobian(f, p) | ||
end | ||
end | ||
|
||
function nonlinearsolve_∂f_∂u(prob, f::F, u, p) where {F} | ||
if isinplace(prob) | ||
return ForwardDiff.jacobian( | ||
@closure((du, u)->f(du, u, p)), Utils.safe_similar(u), u) | ||
end | ||
return ForwardDiff.jacobian(Base.Fix2(f, p), u) | ||
end | ||
|
||
function NonlinearSolveBase.nonlinearsolve_dual_solution(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 NonlinearSolveBase.nonlinearsolve_dual_solution(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, Utils.restructure(u, partials))) | ||
end | ||
|
||
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
This file was deleted.
Oops, something went wrong.
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