Skip to content

Commit 69880f2

Browse files
fix: do not drop zeros when constructing prototype buffers
1 parent 8248e3f commit 69880f2

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

lib/OptimizationBase/ext/OptimizationMTKExt.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function OptimizationBase.instantiate_function(
3131
hess = (H, θ, args...) -> f.hess(H, θ, mtkprob.p, args...)
3232

3333
hv = function (H, θ, v, args...)
34-
res = (eltype(θ)).(f.hess_prototype)
34+
res = similar(f.hess_prototype, eltype(θ))
3535
hess(res, θ, args...)
3636
H .= res * v
3737
end
@@ -83,7 +83,7 @@ function OptimizationBase.instantiate_function(
8383
hess = (H, θ, args...) -> f.hess(H, θ, mtkprob.p, args...)
8484

8585
hv = function (H, θ, v, args...)
86-
res = (eltype(θ)).(f.hess_prototype)
86+
res = similar(f.hess_prototype, eltype(θ))
8787
hess(res, θ, args...)
8888
H .= res * v
8989
end

lib/OptimizationMOI/src/nlp.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,17 @@ function MOIOptimizationNLPCache(prob::OptimizationProblem,
123123
end
124124
T = eltype(prob.u0)
125125
n = length(prob.u0)
126-
127126
J = if isnothing(f.cons_jac_prototype)
128127
zeros(T, num_cons, n)
129128
else
130-
convert.(T, f.cons_jac_prototype)
129+
similar(f.cons_jac_prototype, T)
131130
end
132131
lagh = !isnothing(f.lag_hess_prototype)
132+
133133
H = if lagh # lag hessian takes precedence
134-
convert.(T, f.lag_hess_prototype)
134+
similar(f.lag_hess_prototype, T)
135135
elseif !isnothing(f.hess_prototype)
136-
convert.(T, f.hess_prototype)
136+
similar(f.hess_prototype, T)
137137
else
138138
zeros(T, n, n)
139139
end
@@ -142,7 +142,7 @@ function MOIOptimizationNLPCache(prob::OptimizationProblem,
142142
elseif isnothing(f.cons_hess_prototype)
143143
Matrix{T}[zeros(T, n, n) for i in 1:num_cons]
144144
else
145-
[convert.(T, f.cons_hess_prototype[i]) for i in 1:num_cons]
145+
[similar(f.cons_hess_prototype[i], T) for i in 1:num_cons]
146146
end
147147
lcons = prob.lcons === nothing ? fill(T(-Inf), num_cons) : prob.lcons
148148
ucons = prob.ucons === nothing ? fill(T(Inf), num_cons) : prob.ucons

0 commit comments

Comments
 (0)