Skip to content

Commit dd6299d

Browse files
Merge pull request #1127 from oscardssmith/os/simplify-itp
simplify internal_itp
2 parents 0fea6eb + f121a90 commit dd6299d

File tree

1 file changed

+35
-58
lines changed

1 file changed

+35
-58
lines changed

src/internal_itp.jl

Lines changed: 35 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ end
2020
simpler dependencies.
2121
"""
2222
struct InternalITP
23-
k1::Float64
24-
k2::Float64
23+
scaled_k1::Float64
2524
n0::Int
2625
end
2726

28-
InternalITP() = InternalITP(0.007, 1.5, 10)
27+
InternalITP() = InternalITP(0.2, 10)
2928

3029
function SciMLBase.solve(prob::IntervalNonlinearProblem{IP, Tuple{T, T}}, alg::InternalITP,
3130
args...;
@@ -36,78 +35,56 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem{IP, Tuple{T, T}}, alg::I
3635
ϵ = eps(T)
3736
if iszero(fl)
3837
return SciMLBase.build_solution(prob, alg, left, fl;
39-
retcode = ReturnCode.ExactSolutionLeft, left = left,
40-
right = right)
38+
retcode = ReturnCode.ExactSolutionLeft, left, right)
4139
elseif iszero(fr)
4240
return SciMLBase.build_solution(prob, alg, right, fr;
43-
retcode = ReturnCode.ExactSolutionRight, left = left,
44-
right = right)
41+
retcode = ReturnCode.ExactSolutionRight, left, right)
4542
end
46-
#defining variables/cache
47-
k1 = T(alg.k1)
48-
k2 = T(alg.k2)
43+
span = abs(right - left)
44+
k1 = T(alg.scaled_k1)/span
4945
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
46+
n_h = exponent(span / (2 * ϵ))
47+
ϵ_s = ϵ * exp2(n_h + n0)
48+
T0 = zero(fl)
49+
50+
i = 1
51+
while i maxiters
6252
span = abs(right - left)
53+
mid = (left + right) / 2
6354
r = ϵ_s - (span / 2)
64-
δ = k1 * (span^k2)
6555

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

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
58+
δ = max(k1 * span^2, eps(x_f))
59+
diff = mid - x_f
7660

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

84-
## Update ##
85-
tmin, tmax = minmax(left, right)
86-
xp >= tmax && (xp = prevfloat(tmax))
87-
xp <= tmin && (xp = nextfloat(tmin))
63+
xp = ifelse(abs(xt - mid) r, xt, mid - copysign(r, diff)) # Projection Step
64+
if span < 2ϵ
65+
return SciMLBase.build_solution(
66+
prob, alg, xt, f(xt); retcode = ReturnCode.Success, left, right
67+
)
68+
end
8869
yp = f(xp)
8970
yps = yp * sign(fr)
90-
if yps > 0
91-
right = xp
92-
fr = yp
93-
elseif yps < 0
94-
left = xp
95-
fl = yp
71+
if yps > T0
72+
right, fr = xp, yp
73+
elseif yps < T0
74+
left, fl = xp, yp
9675
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)
76+
return SciMLBase.build_solution(
77+
prob, alg, xp, yps; retcode = ReturnCode.Success, left, right
78+
)
10279
end
80+
10381
i += 1
104-
mid = (left + right) / 2
10582
ϵ_s /= 2
10683

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)
84+
if nextfloat_tdir(left, left, right) == right
85+
return SciMLBase.build_solution(
86+
prob, alg, right, fr; retcode = ReturnCode.FloatingPointLimit, left, right
87+
)
11188
end
11289
end
11390
return SciMLBase.build_solution(prob, alg, left, fl; retcode = ReturnCode.MaxIters,

0 commit comments

Comments
 (0)