1
1
function SciMLBase. solve (prob:: NonlinearProblem{<:Number} , alg:: NewtonRaphson , args... ; xatol = nothing , xrtol = nothing , maxiters = 1000 , kwargs... )
2
2
f = Base. Fix2 (prob. f, prob. p)
3
3
x = float (prob. u0)
4
+ fx = float (prob. u0)
4
5
T = typeof (x)
5
6
atol = xatol != = nothing ? xatol : oneunit (T) * (eps (one (T)))^ (4 // 5 )
6
7
rtol = xrtol != = nothing ? xrtol : eps (one (T))^ (4 // 5 )
@@ -13,15 +14,15 @@ function SciMLBase.solve(prob::NonlinearProblem{<:Number}, alg::NewtonRaphson, a
13
14
fx = f (x)
14
15
dfx = FiniteDiff. finite_difference_derivative (f, x, alg. diff_type, eltype (x), fx)
15
16
end
16
- iszero (fx) && return NewtonSolution ( x, DEFAULT)
17
+ iszero (fx) && return SciMLBase . build_solution (prob, alg, x, fx; retcode = Symbol ( DEFAULT) )
17
18
Δx = dfx \ fx
18
19
x -= Δx
19
20
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) )
21
22
end
22
23
xo = x
23
24
end
24
- return NewtonSolution ( x, MAXITERS_EXCEED)
25
+ return SciMLBase . build_solution (prob, alg, x, fx; retcode = Symbol ( MAXITERS_EXCEED) )
25
26
end
26
27
27
28
function scalar_nlsolve_ad (prob, alg, args... ; kwargs... )
@@ -32,7 +33,7 @@ function scalar_nlsolve_ad(prob, alg, args...; kwargs...)
32
33
newprob = NonlinearProblem (f, u0, p; prob. kwargs... )
33
34
sol = solve (newprob, alg, args... ; kwargs... )
34
35
35
- uu = getsolution ( sol)
36
+ uu = sol. u
36
37
if p isa Number
37
38
f_p = ForwardDiff. derivative (Base. Fix1 (f, uu), p)
38
39
else
50
51
51
52
function SciMLBase. solve (prob:: NonlinearProblem{<:Number, iip, <:Dual{T,V,P}} , alg:: NewtonRaphson , args... ; kwargs... ) where {iip, T, V, P}
52
53
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
+
54
56
end
55
57
function SciMLBase. solve (prob:: NonlinearProblem{<:Number, iip, <:AbstractArray{<:Dual{T,V,P}}} , alg:: NewtonRaphson , args... ; kwargs... ) where {iip, T, V, P}
56
58
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)
58
60
end
59
61
60
62
# avoid ambiguities
61
63
for Alg in [Bisection]
62
64
@eval function SciMLBase. solve (prob:: NonlinearProblem{uType, iip, <:Dual{T,V,P}} , alg:: $Alg , args... ; kwargs... ) where {uType, iip, T, V, P}
63
65
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)
65
68
end
66
69
@eval function SciMLBase. solve (prob:: NonlinearProblem{uType, iip, <:AbstractArray{<:Dual{T,V,P}}} , alg:: $Alg , args... ; kwargs... ) where {uType, iip, T, V, P}
67
70
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)
69
73
end
70
74
end
71
75
72
- function SciMLBase. solve (prob:: NonlinearProblem , :: Bisection , args... ; maxiters = 1000 , kwargs... )
76
+ function SciMLBase. solve (prob:: NonlinearProblem , alg :: Bisection , args... ; maxiters = 1000 , kwargs... )
73
77
f = Base. Fix2 (prob. f, prob. p)
74
78
left, right = prob. u0
75
79
fl, fr = f (left), f (right)
76
80
77
81
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 )
79
83
end
80
84
81
85
i = 1
82
86
if ! iszero (fr)
83
87
while i < maxiters
84
88
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 )
86
90
fm = f (mid)
87
91
if iszero (fm)
88
92
right = mid
@@ -101,7 +105,7 @@ function SciMLBase.solve(prob::NonlinearProblem, ::Bisection, args...; maxiters
101
105
102
106
while i < maxiters
103
107
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 )
105
109
fm = f (mid)
106
110
if iszero (fm)
107
111
right = mid
@@ -113,23 +117,23 @@ function SciMLBase.solve(prob::NonlinearProblem, ::Bisection, args...; maxiters
113
117
i += 1
114
118
end
115
119
116
- return BracketingSolution (left, right, MAXITERS_EXCEED)
120
+ return SciMLBase . build_solution (prob, alg, left, fl; retcode = Symbol ( MAXITERS_EXCEED), left = left, right = right )
117
121
end
118
122
119
- function SciMLBase. solve (prob:: NonlinearProblem , :: Falsi , args... ; maxiters = 1000 , kwargs... )
123
+ function SciMLBase. solve (prob:: NonlinearProblem , alg :: Falsi , args... ; maxiters = 1000 , kwargs... )
120
124
f = Base. Fix2 (prob. f, prob. p)
121
125
left, right = prob. u0
122
126
fl, fr = f (left), f (right)
123
127
124
128
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 )
126
130
end
127
131
128
132
i = 1
129
133
if ! iszero (fr)
130
134
while i < maxiters
131
135
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 )
133
137
end
134
138
mid = (fr * left - fl * right) / (fr - fl)
135
139
for i in 1 : 10
@@ -156,7 +160,7 @@ function SciMLBase.solve(prob::NonlinearProblem, ::Falsi, args...; maxiters = 10
156
160
157
161
while i < maxiters
158
162
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 )
160
164
fm = f (mid)
161
165
if iszero (fm)
162
166
right = mid
@@ -171,5 +175,5 @@ function SciMLBase.solve(prob::NonlinearProblem, ::Falsi, args...; maxiters = 10
171
175
i += 1
172
176
end
173
177
174
- return BracketingSolution (left, right, MAXITERS_EXCEED)
178
+ return SciMLBase . build_solution (prob, alg, left, fl; retcode = Symbol ( MAXITERS_EXCEED), left = left, right = right )
175
179
end
0 commit comments