Skip to content

Commit c5b60cd

Browse files
committed
Remove use of implicit NLSolversBase.value and NLSolversBase.gradient
1 parent 07963b7 commit c5b60cd

File tree

4 files changed

+14
-39
lines changed

4 files changed

+14
-39
lines changed

src/LineSearches.jl

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ function make_ϕdϕ(df, x_new, x, s)
2929
x_new .= x .+ α.*s
3030

3131
# Evaluate ∇f(x+α*s)
32-
NLSolversBase.value_gradient!(df, x_new)
32+
f_x_new, g_x_new = NLSolversBase.value_gradient!(df, x_new)
3333

3434
# Calculate ϕ(a_i), ϕ'(a_i)
35-
NLSolversBase.value(df), real(dot(NLSolversBase.gradient(df), s))
35+
return f_x_new, real(dot(g_x_new, s))
3636
end
3737
ϕdϕ
3838
end
@@ -42,48 +42,18 @@ function make_ϕ_dϕ(df, x_new, x, s)
4242
x_new .= x .+ α.*s
4343

4444
# Evaluate ∇f(x+α*s)
45-
NLSolversBase.gradient!(df, x_new)
45+
g_x_new = NLSolversBase.gradient!(df, x_new)
4646

4747
# Calculate ϕ'(a_i)
48-
real(dot(NLSolversBase.gradient(df), s))
48+
return real(dot(g_x_new, s))
4949
end
5050
make_ϕ(df, x_new, x, s), dϕ
5151
end
5252
function make_ϕ_dϕ_ϕdϕ(df, x_new, x, s)
53-
function (α)
54-
# Move a distance of alpha in the direction of s
55-
x_new .= x .+ α.*s
56-
57-
# Evaluate f(x+α*s) and ∇f(x+α*s)
58-
NLSolversBase.gradient!(df, x_new)
59-
60-
# Calculate ϕ'(a_i)
61-
real(dot(NLSolversBase.gradient(df), s))
62-
end
63-
function ϕdϕ(α)
64-
# Move a distance of alpha in the direction of s
65-
x_new .= x .+ α.*s
66-
67-
# Evaluate ∇f(x+α*s)
68-
NLSolversBase.value_gradient!(df, x_new)
69-
70-
# Calculate ϕ'(a_i)
71-
NLSolversBase.value(df), real(dot(NLSolversBase.gradient(df), s))
72-
end
73-
make_ϕ(df, x_new, x, s), dϕ, ϕdϕ
53+
make_ϕ_dϕ(df, x_new, x, s)..., make_ϕdϕ(df, x_new, x, s)
7454
end
7555
function make_ϕ_ϕdϕ(df, x_new, x, s)
76-
function ϕdϕ(α)
77-
# Move a distance of alpha in the direction of s
78-
x_new .= x .+ α.*s
79-
80-
# Evaluate ∇f(x+α*s)
81-
NLSolversBase.value_gradient!(df, x_new)
82-
83-
# Calculate ϕ'(a_i)
84-
NLSolversBase.value(df), real(dot(NLSolversBase.gradient(df), s))
85-
end
86-
make_ϕ(df, x_new, x, s), ϕdϕ
56+
make_ϕ(df, x_new, x, s), make_ϕdϕ(df, x_new, x, s)
8757
end
8858

8959
include("types.jl")

src/initialguess.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ function (is::InitialQuadratic{T})(ls, state, phi_0, dphi_0, df) where T
7474
# If we're at the first iteration
7575
αguess = is.α0
7676
else
77-
αguess = 2 * (NLSolversBase.value(df) - state.f_x_previous) / dphi_0
77+
f_x_ls = NLSolversBase.value!(df, state.x_ls)
78+
αguess = 2 * (f_x_ls - state.f_x_previous) / dphi_0
7879
αguess = NaNMath.max(is.αmin, state.alpha*is.ρ, αguess)
7980
αguess = NaNMath.min(is.αmax, αguess)
8081
# if αguess ≈ 1, then make it 1 (Newton-type behaviour)
@@ -180,8 +181,8 @@ function (is::InitialHagerZhang)(ls::Tls, state, phi_0, dphi_0, df) where Tls
180181
# and the user has not provided an initial step size (is.α0 is NaN),
181182
# then we
182183
# pick the initial step size according to HZ #I0
183-
state.alpha = _hzI0(state.x, NLSolversBase.gradient(df),
184-
NLSolversBase.value(df),
184+
f_x, g_x = NLSolversBase.value_gradient!(df, state.x)
185+
state.alpha = _hzI0(state.x, g_x, f_x,
185186
is.αmax,
186187
convert(eltype(state.x), is.ψ0)) # Hack to deal with type instability between is{T} and state.x
187188
if Tls <: HagerZhang

test/arbitrary_precision.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ doublefloatstypes = [Double64, Double32, Double16]
9898
ls = HagerZhang{T}()
9999
state = getstate()
100100
state.f_x_previous = 2*phi_0
101+
state.x_ls = zero(x)
101102
is = InitialQuadratic{T}(snap2one=(convert(T, 0.9),convert(T, Inf)))
102103
is(ls, state, phi_0, dphi_0, df)
103104
@test !isnan(state.alpha)
@@ -107,6 +108,7 @@ doublefloatstypes = [Double64, Double32, Double16]
107108
ls = HagerZhang{T}()
108109
state = getstate()
109110
state.f_x_previous = 2*phi_0
111+
state.x_ls = zero(x)
110112
is = InitialQuadratic{T}(snap2one=(convert(T, 0.75),convert(T, Inf)))
111113
is(ls, state, phi_0, dphi_0, df)
112114
@test !isnan(state.alpha)

test/initial.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
ls = HagerZhang()
8989
state = getstate()
9090
state.f_x_previous = 2*phi_0
91+
state.x_ls = zero(x)
9192
is = InitialQuadratic(snap2one=(0.9,Inf))
9293
is(ls, state, phi_0, dphi_0, df)
9394
@test state.alpha == 1.0
@@ -97,6 +98,7 @@
9798
ls = HagerZhang()
9899
state = getstate()
99100
state.f_x_previous = 2*phi_0
101+
state.x_ls = zero(x)
100102
is = InitialQuadratic(snap2one=(0.75,Inf))
101103
is(ls, state, phi_0, dphi_0, df)
102104
@test state.alpha == 1.0

0 commit comments

Comments
 (0)