1
1
struct Bisection <: AbstractBracketingAlgorithm
2
+ exact_left:: Bool
3
+ exact_right:: Bool
4
+ end
5
+
6
+ function Bisection (;exact_left= false , exact_right= false )
7
+ Bisection (exact_left, exact_right)
2
8
end
3
9
4
10
mutable struct BisectionCache{uType}
@@ -95,7 +101,7 @@ function perform_step!(solver, alg::Bisection, cache)
95
101
fl * fr > fzero && error (" Bracket became non-containing in between iterations. This could mean that "
96
102
+ " input function crosses the x axis multiple times. Bisection is not the right method to solve this." )
97
103
98
- mid = (left + right) / 2.0
104
+ mid = (left + right) / 2
99
105
100
106
if left == mid || right == mid
101
107
solver. force_stop = true
@@ -106,9 +112,22 @@ function perform_step!(solver, alg::Bisection, cache)
106
112
fm = f (mid, p)
107
113
108
114
if iszero (fm)
109
- cache. state = 1
110
- cache. right = mid
111
- cache. left = mid
115
+ if alg. exact_left
116
+ cache. state = 1
117
+ cache. right = mid
118
+ cache. left = mid
119
+ elseif alg. exact_right
120
+ solver. left = prevfloat_tdir (mid, left, right)
121
+ sync_residuals! (solver)
122
+ cache. state = 2
123
+ cache. left = mid
124
+ else
125
+ solver. left = prevfloat_tdir (mid, left, right)
126
+ solver. right = nextfloat_tdir (mid, left, right)
127
+ sync_residuals! (solver)
128
+ solver. force_stop = true
129
+ return
130
+ end
112
131
else
113
132
if sign (fm) == sign (fl)
114
133
solver. left = mid
@@ -119,11 +138,18 @@ function perform_step!(solver, alg::Bisection, cache)
119
138
end
120
139
end
121
140
elseif cache. state == 1
122
- mid = (left + cache. right) / 2.0
141
+ mid = (left + cache. right) / 2
123
142
124
143
if cache. right == mid || left == mid
125
- cache. state = 2
126
- return
144
+ if alg. exact_right
145
+ cache. state = 2
146
+ return
147
+ else
148
+ solver. right = nextfloat_tdir (mid, left, right)
149
+ sync_residuals! (solver)
150
+ solver. force_stop = true
151
+ return
152
+ end
127
153
end
128
154
129
155
fm = f (mid, p)
@@ -135,7 +161,7 @@ function perform_step!(solver, alg::Bisection, cache)
135
161
solver. fl = fm
136
162
end
137
163
else
138
- mid = (cache. left + right) / 2.0
164
+ mid = (cache. left + right) / 2
139
165
140
166
if right == mid || cache. left == mid
141
167
solver. force_stop = true
0 commit comments