Skip to content

Commit edd89a9

Browse files
fix A default value when not provided in the constructor (#112)
* fix A default value when not provided in the constructor * tests default A value
1 parent 310c5c4 commit edd89a9

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/qpmodel.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,14 @@ function QuadraticModel(
148148
)
149149
end
150150

151+
similar_empty_matrix(H::AbstractMatrix{T}, n::Integer) where {T} = similar(H, 0, n)
152+
similar_empty_matrix(::SparseMatrixCOO{T, I}, n::Integer) where {T, I} = SparseMatrixCOO(0, n, I[], I[], T[])
153+
similar_empty_matrix(::AbstractLinearOperator{T}, n::Integer) where {T} = opZeros(T, 0, n)
154+
151155
function QuadraticModel(
152156
c::S,
153157
H::Union{AbstractMatrix{T}, AbstractLinearOperator{T}};
154-
A::Union{AbstractMatrix, AbstractLinearOperator} = similar(c, 0, length(c)),
158+
A::Union{AbstractMatrix, AbstractLinearOperator} = similar_empty_matrix(H, length(c)),
155159
lcon::S = S(undef, 0),
156160
ucon::S = S(undef, 0),
157161
lvar::S = fill!(S(undef, length(c)), T(-Inf)),

test/runtests.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,26 @@ end
207207
@test jtprod(SMLO, x, y) jtprod(SM, x, y)
208208
@test objgrad(SMLO, x)[1] objgrad(SM, x)[1]
209209
@test objgrad(SMLO, x)[2] objgrad(SM, x)[2]
210+
211+
# tests default A value
212+
qp = QuadraticModel(
213+
c,
214+
SparseMatrixCOO(H.data),
215+
lvar = lvar,
216+
uvar = uvar,
217+
c0 = 0.0,
218+
name = "QM",
219+
)
220+
qpLO = QuadraticModel(
221+
c,
222+
LinearOperator(H),
223+
lvar = lvar,
224+
uvar = uvar,
225+
c0 = 0.0,
226+
name = "QMLO",
227+
)
228+
@test qp.data.A isa SparseMatrixCOO
229+
@test qpLO.data.A isa AbstractLinearOperator
210230
end
211231

212232
@testset "struct and coord CSC" begin

0 commit comments

Comments
 (0)