@@ -111,18 +111,19 @@ function (ls::HagerZhang)(ϕ, ϕdϕ,
111
111
@unpack delta, sigma, alphamax, rho, epsilon, gamma,
112
112
linesearchmax, psi3, display, mayterminate = ls
113
113
114
+ zeroT = convert (T, 0 )
114
115
115
116
if ! (isfinite (phi_0) && isfinite (dphi_0))
116
117
throw (ArgumentError (" Value and slope at step length = 0 must be finite." ))
117
118
end
118
- if dphi_0 >= T ( 0 )
119
+ if dphi_0 >= zeroT
119
120
throw (ArgumentError (" Search direction is not a direction of descent." ))
120
121
end
121
122
122
123
# Prevent values of x_new = x+αs that are likely to make
123
124
# ϕ(x_new) infinite
124
125
iterfinitemax:: Int = ceil (Int, - log2 (eps (T)))
125
- alphas = [T ( 0 ) ] # for bisection
126
+ alphas = [zeroT ] # for bisection
126
127
values = [phi_0]
127
128
slopes = [dphi_0]
128
129
if display & LINESEARCH > 0
@@ -131,7 +132,7 @@ function (ls::HagerZhang)(ϕ, ϕdϕ,
131
132
132
133
133
134
phi_lim = phi_0 + epsilon * abs (phi_0)
134
- @assert c > T ( 0 )
135
+ @assert c > zeroT
135
136
@assert isfinite (c) && c <= alphamax
136
137
phi_c, dphi_c = ϕdϕ (c)
137
138
iterfinite = 1
@@ -142,9 +143,9 @@ function (ls::HagerZhang)(ϕ, ϕdϕ,
142
143
phi_c, dphi_c = ϕdϕ (c)
143
144
end
144
145
if ! (isfinite (phi_c) && isfinite (dphi_c))
145
- warn (" Failed to achieve finite new evaluation point, using alpha=0" )
146
+ @ warn (" Failed to achieve finite new evaluation point, using alpha=0" )
146
147
mayterminate[] = false # reset in case another initial guess is used next
147
- return T ( 0.0 ) , ϕ (T ( 0.0 ) ) # phi_0
148
+ return zeroT , ϕ (zeroT ) # phi_0
148
149
end
149
150
push! (alphas, c)
150
151
push! (values, phi_c)
@@ -175,7 +176,7 @@ function (ls::HagerZhang)(ϕ, ϕdϕ,
175
176
" , phi_c = " , phi_c,
176
177
" , dphi_c = " , dphi_c)
177
178
end
178
- if dphi_c >= T ( 0 )
179
+ if dphi_c >= zeroT
179
180
# We've reached the upward slope, so we have b; examine
180
181
# previous values to find a
181
182
ib = length (alphas)
@@ -191,7 +192,7 @@ function (ls::HagerZhang)(ϕ, ϕdϕ,
191
192
# have crested over the peak. Use bisection.
192
193
ib = length (alphas)
193
194
ia = ib - 1
194
- if c ≉ alphas[ib] || slopes[ib] >= T ( 0 )
195
+ if c ≉ alphas[ib] || slopes[ib] >= zeroT
195
196
error (" c = " , c)
196
197
end
197
198
# ia, ib = bisect(phi, lsr, ia, ib, phi_lim) # TODO : Pass options
@@ -226,7 +227,7 @@ function (ls::HagerZhang)(ϕ, ϕdϕ,
226
227
if ! (isfinite (phi_c) && isfinite (dphi_c))
227
228
mayterminate[] = false # reset in case another initial guess is used next
228
229
return cold, ϕ (cold)
229
- elseif dphi_c < T ( 0 ) && c == alphamax
230
+ elseif dphi_c < zeroT && c == alphamax
230
231
# We're on the edge of the allowed region, and the
231
232
# value is still decreasing. This can be due to
232
233
# roundoff error in barrier penalties, a barrier
@@ -352,7 +353,8 @@ function secant2!(ϕdϕ,
352
353
dphi_a = slopes[ia]
353
354
dphi_b = slopes[ib]
354
355
T = eltype (slopes)
355
- if ! (dphi_a < T (0 ) && dphi_b >= T (0 ))
356
+ zeroT = convert (T, 0 )
357
+ if ! (dphi_a < zeroT && dphi_b >= zeroT)
356
358
error (string (" Search direction is not a direction of descent; " ,
357
359
" this error may indicate that user-provided derivatives are inaccurate. " ,
358
360
@sprintf " (dphi_a = %f; dphi_b = %f)" dphi_a dphi_b))
@@ -436,10 +438,11 @@ function update!(ϕdϕ,
436
438
a = alphas[ia]
437
439
b = alphas[ib]
438
440
T = eltype (slopes)
441
+ zeroT = convert (T, 0 )
439
442
# Debugging (HZ, eq. 4.4):
440
- @assert slopes[ia] < T ( 0 )
443
+ @assert slopes[ia] < zeroT
441
444
@assert values[ia] <= phi_lim
442
- @assert slopes[ib] >= T ( 0 )
445
+ @assert slopes[ib] >= zeroT
443
446
@assert b > a
444
447
c = alphas[ic]
445
448
phi_c = values[ic]
@@ -456,7 +459,7 @@ function update!(ϕdϕ,
456
459
if c < a || c > b
457
460
return ia, ib # , 0, 0 # it's out of the bracketing interval
458
461
end
459
- if dphi_c >= T ( 0 )
462
+ if dphi_c >= zeroT
460
463
return ia, ic # , 0, 0 # replace b with a closer point
461
464
end
462
465
# We know dphi_c < 0. However, phi may not be monotonic between a
@@ -485,9 +488,10 @@ function bisect!(ϕdϕ,
485
488
a = alphas[ia]
486
489
b = alphas[ib]
487
490
# Debugging (HZ, conditions shown following U3)
488
- @assert slopes[ia] < T (0 )
491
+ zeroT = convert (T, 0 )
492
+ @assert slopes[ia] < zeroT
489
493
@assert values[ia] <= phi_lim
490
- @assert slopes[ib] < T ( 0 ) # otherwise we wouldn't be here
494
+ @assert slopes[ib] < zeroT # otherwise we wouldn't be here
491
495
@assert values[ib] > phi_lim
492
496
@assert b > a
493
497
while b - a > eps (b)
@@ -503,7 +507,7 @@ function bisect!(ϕdϕ,
503
507
push! (slopes, gphi)
504
508
505
509
id = length (alphas)
506
- if gphi >= T ( 0 )
510
+ if gphi >= zeroT
507
511
return ia, id # replace b, return
508
512
end
509
513
if phi_d <= phi_lim
0 commit comments