Skip to content

Commit 8affba4

Browse files
authored
Avoid similar and use copy. (#129)
1 parent f7b6cbc commit 8affba4

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

src/objective_types/oncedifferentiable.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function OnceDifferentiable(f, x_seed::AbstractArray, F::AbstractArray, DF::Abst
104104
# symbol interface.
105105
fdtype = finitediff_fdtype(autodiff)
106106
# Apparently only the third input is aliased.
107-
j_finitediff_cache = FiniteDiff.JacobianCache(similar(x_seed), similar(F), similar(F), fdtype)
107+
j_finitediff_cache = FiniteDiff.JacobianCache(copy(x_seed), copy(F), copy(F), fdtype)
108108
if autodiff == :finiteforward
109109
# These copies can be done away with if we add a keyword for
110110
# reusing arrays instead for overwriting them.
@@ -140,7 +140,7 @@ function OnceDifferentiable(f, x_seed::AbstractArray, F::AbstractArray, DF::Abst
140140
F
141141
end
142142
function j_finitediff!(J, x)
143-
F_cache = similar(F)
143+
F_cache = copy(F)
144144
fj_finitediff!(F_cache, J, x)
145145
end
146146

src/objective_types/twicedifferentiable.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function TwiceDifferentiable(f, g, fg, h, x::TX, F::T = real(zero(eltype(x))), G
2323
h! = h!_from_h(h, F, inplace)
2424

2525
TwiceDifferentiable{T,TG,TH,TX}(f, g!, fg!, h!,
26-
copy(F), similar(G), copy(H),
26+
copy(F), copy(G), copy(H),
2727
x_f, x_df, x_h,
2828
[0,], [0,], [0,])
2929
end
@@ -33,13 +33,13 @@ function TwiceDifferentiable(f, g, h,
3333
F::Real = real(zero(eltype(x))),
3434
G = alloc_DF(x, F),
3535
H = alloc_H(x, F); inplace = true) where {TX}
36-
3736
g! = df!_from_df(g, F, inplace)
3837
h! = h!_from_h(h, F, inplace)
3938

4039
fg! = make_fdf(x, F, f, g!)
40+
x_f, x_df, x_h = x_of_nans(x), x_of_nans(x), x_of_nans(x)
4141

42-
return TwiceDifferentiable(f, g!, fg!, h!, x, F, G, H)
42+
return TwiceDifferentiable(f, g!, fg!, h!, F, G, H, x_f, x_df, x_h, [0,], [0,], [0,])
4343
end
4444

4545

@@ -65,7 +65,7 @@ function TwiceDifferentiable(f, g,
6565
end
6666

6767
elseif is_forwarddiff(autodiff)
68-
hcfg = ForwardDiff.HessianConfig(f, similar(x_seed))
68+
hcfg = ForwardDiff.HessianConfig(f, copy(x_seed))
6969
h! = (out, x) -> ForwardDiff.hessian!(out, f, x, hcfg)
7070
else
7171
error("The autodiff value $(autodiff) is not supported. Use :finite or :forward.")
@@ -90,7 +90,7 @@ function TwiceDifferentiable(d::OnceDifferentiable, x_seed::AbstractVector{T} =
9090
return
9191
end
9292
elseif is_forwarddiff(autodiff)
93-
hcfg = ForwardDiff.HessianConfig(d.f, similar(gradient(d)))
93+
hcfg = ForwardDiff.HessianConfig(d.f, copy(gradient(d)))
9494
h! = (out, x) -> ForwardDiff.hessian!(out, d.f, x, hcfg)
9595
else
9696
error("The autodiff value $(autodiff) is not supported. Use :finite or :forward.")

src/objective_types/twicedifferentiablehv.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ mutable struct TwiceDifferentiableHV{T,TDF,THv,TX} <: AbstractObjective
1616
end
1717

1818
# compatibility with old constructor
19-
function TwiceDifferentiableHV(f, fdf, h, x::TX, F::T, G::TG = similar(x), H::THv = similar(x)) where {T, TG, THv, TX}
19+
function TwiceDifferentiableHV(f, fdf, h, x::TX, F::T, G::TG = copy(x), H::THv = copy(x)) where {T, TG, THv, TX}
2020
x_f, x_df, x_hv, v_hv = x_of_nans(x), x_of_nans(x), x_of_nans(x), x_of_nans(x)
2121
TwiceDifferentiableHV{T,TG, THv, TX}(f, fdf, h,
22-
copy(F), similar(G), copy(H),
22+
copy(F), copy(G), copy(H),
2323
x_f, x_df, x_hv, v_hv,
2424
[0,], [0,], [0,])
2525
end

0 commit comments

Comments
 (0)