Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow u0 to be duals #477

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "NonlinearSolve"
uuid = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
authors = ["SciML"]
version = "3.15.1"
version = "3.15.2"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
50 changes: 27 additions & 23 deletions src/internal/forward_diff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,33 @@
import SimpleNonlinearSolve: __nlsolve_ad, __nlsolve_dual_soln, __nlsolve_∂f_∂p,
__nlsolve_∂f_∂u

function SciMLBase.solve(
prob::NonlinearProblem{<:Union{Number, <:AbstractArray}, iip,
<:Union{<:Dual{T, V, P}, <:AbstractArray{<:Dual{T, V, P}}}},
alg::Union{Nothing, AbstractNonlinearAlgorithm},
args...;
kwargs...) where {T, V, P, iip}
sol, partials = __nlsolve_ad(prob, alg, args...; kwargs...)
dual_soln = __nlsolve_dual_soln(sol.u, partials, prob.p)
return SciMLBase.build_solution(
prob, alg, dual_soln, sol.resid; sol.retcode, sol.stats, sol.original)
for (uType, pType) in [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary? This seems pretty ugly

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ambiguity prevention

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason this doesn't overload __init and __solve like `OrdinaryDiffEq does?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was asking myself the same question today morning

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you need to so that you promote first.

(Union{<:Number, <:AbstractArray}, Union{<:Dual, <:AbstractArray{<:Dual}}),
(Union{<:Dual, <:AbstractArray{<:Dual}}, Union{<:Dual, <:AbstractArray{<:Dual}}),
(Union{<:Dual, <:AbstractArray{<:Dual}}, Union{<:Number, <:AbstractArray})
]
@eval begin
function SciMLBase.solve(
prob::NonlinearProblem{<:$(uType), iip, <:$(pType)},
alg::Union{Nothing, AbstractNonlinearAlgorithm},
args...; kwargs...) where {iip}
sol, partials = __nlsolve_ad(prob, alg, args...; kwargs...)
dual_soln = __nlsolve_dual_soln(sol.u, partials, prob.p)
return SciMLBase.build_solution(
prob, alg, dual_soln, sol.resid; sol.retcode, sol.stats, sol.original)
end

function SciMLBase.init(
prob::NonlinearProblem{<:$(uType), iip, <:$(pType)},
alg::Union{Nothing, AbstractNonlinearAlgorithm},
args...; kwargs...) where {iip}
p = __value(prob.p)
newprob = NonlinearProblem(prob.f, __value(prob.u0), p; prob.kwargs...)
cache = init(newprob, alg, args...; kwargs...)
return NonlinearSolveForwardDiffCache(
cache, newprob, alg, prob.p, p, ForwardDiff.partials(prob.p))
end
end
end

@concrete mutable struct NonlinearSolveForwardDiffCache
Expand All @@ -35,19 +52,6 @@ function reinit_cache!(cache::NonlinearSolveForwardDiffCache;
return cache
end

function SciMLBase.init(
prob::NonlinearProblem{<:Union{Number, <:AbstractArray}, iip,
<:Union{<:Dual{T, V, P}, <:AbstractArray{<:Dual{T, V, P}}}},
alg::Union{Nothing, AbstractNonlinearAlgorithm},
args...;
kwargs...) where {T, V, P, iip}
p = __value(prob.p)
newprob = NonlinearProblem(prob.f, __value(prob.u0), p; prob.kwargs...)
cache = init(newprob, alg, args...; kwargs...)
return NonlinearSolveForwardDiffCache(
cache, newprob, alg, prob.p, p, ForwardDiff.partials(prob.p))
end

function SciMLBase.solve!(cache::NonlinearSolveForwardDiffCache)
sol = solve!(cache.cache)
prob = cache.prob
Expand Down
Loading