11function SciMLBase. solve (prob:: NonlinearProblem{<:Number} , alg:: NewtonRaphson , args... ; xatol = nothing , xrtol = nothing , maxiters = 1000 , kwargs... )
22 f = Base. Fix2 (prob. f, prob. p)
33 x = float (prob. u0)
4+ fx = float (prob. u0)
45 T = typeof (x)
56 atol = xatol != = nothing ? xatol : oneunit (T) * (eps (one (T)))^ (4 // 5 )
67 rtol = xrtol != = nothing ? xrtol : eps (one (T))^ (4 // 5 )
@@ -13,15 +14,15 @@ function SciMLBase.solve(prob::NonlinearProblem{<:Number}, alg::NewtonRaphson, a
1314 fx = f (x)
1415 dfx = FiniteDiff. finite_difference_derivative (f, x, alg. diff_type, eltype (x), fx)
1516 end
16- iszero (fx) && return NewtonSolution ( x, DEFAULT)
17+ iszero (fx) && return SciMLBase . build_solution (prob, alg, x, fx; retcode = Symbol ( DEFAULT) )
1718 Δx = dfx \ fx
1819 x -= Δx
1920 if isapprox (x, xo, atol= atol, rtol= rtol)
20- return NewtonSolution ( x, DEFAULT)
21+ return SciMLBase . build_solution (prob, alg, x, fx; retcode = Symbol ( DEFAULT) )
2122 end
2223 xo = x
2324 end
24- return NewtonSolution ( x, MAXITERS_EXCEED)
25+ return SciMLBase . build_solution (prob, alg, x, fx; retcode = Symbol ( MAXITERS_EXCEED) )
2526end
2627
2728function scalar_nlsolve_ad (prob, alg, args... ; kwargs... )
@@ -32,7 +33,7 @@ function scalar_nlsolve_ad(prob, alg, args...; kwargs...)
3233 newprob = NonlinearProblem (f, u0, p; prob. kwargs... )
3334 sol = solve (newprob, alg, args... ; kwargs... )
3435
35- uu = getsolution ( sol)
36+ uu = sol. u
3637 if p isa Number
3738 f_p = ForwardDiff. derivative (Base. Fix1 (f, uu), p)
3839 else
5051
5152function SciMLBase. solve (prob:: NonlinearProblem{<:Number, iip, <:Dual{T,V,P}} , alg:: NewtonRaphson , args... ; kwargs... ) where {iip, T, V, P}
5253 sol, partials = scalar_nlsolve_ad (prob, alg, args... ; kwargs... )
53- return NewtonSolution (Dual {T,V,P} (sol. u, partials), sol. retcode)
54+ return SciMLBase. build_solution (prob, alg, Dual {T,V,P} (sol. u, partials), sol. resid; retcode= sol. retcode)
55+
5456end
5557function SciMLBase. solve (prob:: NonlinearProblem{<:Number, iip, <:AbstractArray{<:Dual{T,V,P}}} , alg:: NewtonRaphson , args... ; kwargs... ) where {iip, T, V, P}
5658 sol, partials = scalar_nlsolve_ad (prob, alg, args... ; kwargs... )
57- return NewtonSolution ( Dual {T,V,P} (sol. u, partials), sol. retcode)
59+ return SciMLBase . build_solution (prob, alg, Dual {T,V,P} (sol. u, partials), sol . resid; retcode = sol. retcode)
5860end
5961
6062# avoid ambiguities
6163for Alg in [Bisection]
6264 @eval function SciMLBase. solve (prob:: NonlinearProblem{uType, iip, <:Dual{T,V,P}} , alg:: $Alg , args... ; kwargs... ) where {uType, iip, T, V, P}
6365 sol, partials = scalar_nlsolve_ad (prob, alg, args... ; kwargs... )
64- return BracketingSolution (Dual {T,V,P} (sol. left, partials), Dual {T,V,P} (sol. right, partials), sol. retcode)
66+ return SciMLBase. build_solution (prob, alg, Dual {T,V,P} (sol. u, partials), sol. resid; retcode= sol. retcode,left = Dual {T,V,P} (sol. left, partials), right = Dual {T,V,P} (sol. right, partials))
67+ # return BracketingSolution(Dual{T,V,P}(sol.left, partials), Dual{T,V,P}(sol.right, partials), sol.retcode, sol.resid)
6568 end
6669 @eval function SciMLBase. solve (prob:: NonlinearProblem{uType, iip, <:AbstractArray{<:Dual{T,V,P}}} , alg:: $Alg , args... ; kwargs... ) where {uType, iip, T, V, P}
6770 sol, partials = scalar_nlsolve_ad (prob, alg, args... ; kwargs... )
68- return BracketingSolution (Dual {T,V,P} (sol. left, partials), Dual {T,V,P} (sol. right, partials), sol. retcode)
71+ return SciMLBase. build_solution (prob, alg, Dual {T,V,P} (sol. u, partials), sol. resid; retcode= sol. retcode,left = Dual {T,V,P} (sol. left, partials), right = Dual {T,V,P} (sol. right, partials))
72+ # return BracketingSolution(Dual{T,V,P}(sol.left, partials), Dual{T,V,P}(sol.right, partials), sol.retcode, sol.resid)
6973 end
7074end
7175
72- function SciMLBase. solve (prob:: NonlinearProblem , :: Bisection , args... ; maxiters = 1000 , kwargs... )
76+ function SciMLBase. solve (prob:: NonlinearProblem , alg :: Bisection , args... ; maxiters = 1000 , kwargs... )
7377 f = Base. Fix2 (prob. f, prob. p)
7478 left, right = prob. u0
7579 fl, fr = f (left), f (right)
7680
7781 if iszero (fl)
78- return BracketingSolution (left, right, EXACT_SOLUTION_LEFT)
82+ return SciMLBase . build_solution (prob, alg, left, fl; retcode = Symbol ( EXACT_SOLUTION_LEFT), left = left, right = right )
7983 end
8084
8185 i = 1
8286 if ! iszero (fr)
8387 while i < maxiters
8488 mid = (left + right) / 2
85- (mid == left || mid == right) && return BracketingSolution (left, right, FLOATING_POINT_LIMIT)
89+ (mid == left || mid == right) && return SciMLBase . build_solution (prob, alg, left, fl; retcode = Symbol ( FLOATING_POINT_LIMIT), left = left, right = right )
8690 fm = f (mid)
8791 if iszero (fm)
8892 right = mid
@@ -101,7 +105,7 @@ function SciMLBase.solve(prob::NonlinearProblem, ::Bisection, args...; maxiters
101105
102106 while i < maxiters
103107 mid = (left + right) / 2
104- (mid == left || mid == right) && return BracketingSolution (left, right, FLOATING_POINT_LIMIT)
108+ (mid == left || mid == right) && return SciMLBase . build_solution (prob, alg, left, fl; retcode = Symbol ( FLOATING_POINT_LIMIT), left = left, right = right )
105109 fm = f (mid)
106110 if iszero (fm)
107111 right = mid
@@ -113,23 +117,23 @@ function SciMLBase.solve(prob::NonlinearProblem, ::Bisection, args...; maxiters
113117 i += 1
114118 end
115119
116- return BracketingSolution (left, right, MAXITERS_EXCEED)
120+ return SciMLBase . build_solution (prob, alg, left, fl; retcode = Symbol ( MAXITERS_EXCEED), left = left, right = right )
117121end
118122
119- function SciMLBase. solve (prob:: NonlinearProblem , :: Falsi , args... ; maxiters = 1000 , kwargs... )
123+ function SciMLBase. solve (prob:: NonlinearProblem , alg :: Falsi , args... ; maxiters = 1000 , kwargs... )
120124 f = Base. Fix2 (prob. f, prob. p)
121125 left, right = prob. u0
122126 fl, fr = f (left), f (right)
123127
124128 if iszero (fl)
125- return BracketingSolution (left, right, EXACT_SOLUTION_LEFT)
129+ return SciMLBase . build_solution (prob, alg, left, fl; retcode = Symbol ( EXACT_SOLUTION_LEFT), left = left, right = right )
126130 end
127131
128132 i = 1
129133 if ! iszero (fr)
130134 while i < maxiters
131135 if nextfloat_tdir (left, prob. u0... ) == right
132- return BracketingSolution (left, right, FLOATING_POINT_LIMIT)
136+ return SciMLBase . build_solution (prob, alg, left, fl; retcode = Symbol ( FLOATING_POINT_LIMIT), left = left, right = right )
133137 end
134138 mid = (fr * left - fl * right) / (fr - fl)
135139 for i in 1 : 10
@@ -156,7 +160,7 @@ function SciMLBase.solve(prob::NonlinearProblem, ::Falsi, args...; maxiters = 10
156160
157161 while i < maxiters
158162 mid = (left + right) / 2
159- (mid == left || mid == right) && return BracketingSolution (left, right, FLOATING_POINT_LIMIT)
163+ (mid == left || mid == right) && return SciMLBase . build_solution (prob, alg, left, fl; retcode = Symbol ( FLOATING_POINT_LIMIT), left = left, right = right )
160164 fm = f (mid)
161165 if iszero (fm)
162166 right = mid
@@ -171,5 +175,5 @@ function SciMLBase.solve(prob::NonlinearProblem, ::Falsi, args...; maxiters = 10
171175 i += 1
172176 end
173177
174- return BracketingSolution (left, right, MAXITERS_EXCEED)
178+ return SciMLBase . build_solution (prob, alg, left, fl; retcode = Symbol ( MAXITERS_EXCEED), left = left, right = right )
175179end
0 commit comments