Skip to content

Commit c8d1c41

Browse files
committed
simplify internal_itp
1 parent d7528a4 commit c8d1c41

File tree

1 file changed

+35
-54
lines changed

1 file changed

+35
-54
lines changed

Diff for: src/internal_itp.jl

+35-54
Original file line numberDiff line numberDiff line change
@@ -36,78 +36,59 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem{IP, Tuple{T, T}}, alg::I
3636
ϵ = eps(T)
3737
if iszero(fl)
3838
return SciMLBase.build_solution(prob, alg, left, fl;
39-
retcode = ReturnCode.ExactSolutionLeft, left = left,
40-
right = right)
39+
retcode = ReturnCode.ExactSolutionLeft, left, right)
4140
elseif iszero(fr)
4241
return SciMLBase.build_solution(prob, alg, right, fr;
43-
retcode = ReturnCode.ExactSolutionRight, left = left,
44-
right = right)
42+
retcode = ReturnCode.ExactSolutionRight, left, right)
4543
end
46-
#defining variables/cache
47-
k1 = T(alg.k1)
4844
k2 = T(alg.k2)
45+
span = abs(right - left)
46+
k1 = T(alg.alg_k1)
4947
n0 = T(alg.n0)
50-
n_h = ceil(log2(abs(right - left) / (2 * ϵ)))
51-
mid = (left + right) / 2
52-
x_f = (fr * left - fl * right) / (fr - fl)
53-
xt = left
54-
xp = left
55-
r = zero(left) #minmax radius
56-
δ = zero(left) # truncation error
57-
σ = 1.0
58-
ϵ_s = ϵ * 2^(n_h + n0)
59-
i = 0 #iteration
60-
while i <= maxiters
61-
#mid = (left + right) / 2
48+
n_h = exponent(span / (2 * ϵ))
49+
ϵ_s = ϵ * exp2(n_h + n0)
50+
T0 = zero(fl)
51+
52+
i = 1
53+
while i maxiters
54+
stats.nsteps += 1
6255
span = abs(right - left)
56+
mid = (left + right) / 2
6357
r = ϵ_s - (span / 2)
64-
δ = k1 * (span^k2)
6558

66-
## Interpolation step ##
67-
x_f = left + (right - left) * (fl / (fl - fr))
59+
x_f = left + span * (fl / (fl - fr)) # Interpolation Step
6860

69-
## Truncation step ##
70-
σ = sign(mid - x_f)
71-
if δ <= abs(mid - x_f)
72-
xt = x_f +* δ)
73-
else
74-
xt = mid
75-
end
61+
δ = max(k1 * span^k2, eps(x_f))
62+
diff = mid - x_f
7663

77-
## Projection step ##
78-
if abs(xt - mid) <= r
79-
xp = xt
80-
else
81-
xp = mid -* r)
82-
end
64+
xt = ifelse abs(diff), x_f + copysign(δ, diff), mid) # Truncation Step
8365

84-
## Update ##
85-
tmin, tmax = minmax(left, right)
86-
xp >= tmax && (xp = prevfloat(tmax))
87-
xp <= tmin && (xp = nextfloat(tmin))
66+
xp = ifelse(abs(xt - mid) r, xt, mid - copysign(r, diff)) # Projection Step
67+
if span < 2ϵ
68+
return SciMLBase.build_solution(
69+
prob, alg, xt, f(xt); retcode = ReturnCode.Success, left, right
70+
)
71+
end
8872
yp = f(xp)
73+
stats.nf += 1
8974
yps = yp * sign(fr)
90-
if yps > 0
91-
right = xp
92-
fr = yp
93-
elseif yps < 0
94-
left = xp
95-
fl = yp
75+
if yps > T0
76+
right, fr = xp, yp
77+
elseif yps < T0
78+
left, fl = xp, yp
9679
else
97-
left = prevfloat_tdir(xp, prob.tspan...)
98-
right = xp
99-
return SciMLBase.build_solution(prob, alg, left, f(left);
100-
retcode = ReturnCode.Success, left = left,
101-
right = right)
80+
return SciMLBase.build_solution(
81+
prob, alg, xp, yps; retcode = ReturnCode.Success, left, right
82+
)
10283
end
84+
10385
i += 1
104-
mid = (left + right) / 2
10586
ϵ_s /= 2
10687

107-
if nextfloat_tdir(left, prob.tspan...) == right
108-
return SciMLBase.build_solution(prob, alg, left, fl;
109-
retcode = ReturnCode.FloatingPointLimit, left = left,
110-
right = right)
88+
if nextfloat_tdir(left, left, right) == right
89+
return SciMLBase.build_solution(
90+
prob, alg, right, fr; retcode = ReturnCode.FloatingPointLimit, left, right
91+
)
11192
end
11293
end
11394
return SciMLBase.build_solution(prob, alg, left, fl; retcode = ReturnCode.MaxIters,

0 commit comments

Comments
 (0)