Skip to content

Commit 0bb5d9b

Browse files
geoffroylecontedpo
authored andcommitted
commit suggestions
1 parent 6beffff commit 0bb5d9b

File tree

3 files changed

+18
-26
lines changed

3 files changed

+18
-26
lines changed

src/QuadraticModels.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import NLPModels:
2828
jtprod
2929
import NLPModelsModifiers: SlackModel, slack_meta
3030

31+
import Base.convert
32+
3133
export AbstractQuadraticModel, QuadraticModel, presolve, postsolve!
3234

3335
include("linalg_utils.jl")

src/qpmodel.jl

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ end
1414

1515
isdense(data::QPData{T, S, M1, M2}) where {T, S, M1, M2} = M1 <: DenseMatrix || M2 <: DenseMatrix
1616

17-
function convertQPData_toCOO(data::QPData{T, S, M1, M2}) where {T, S, M1 <: AbstractMatrix, M2 <: AbstractMatrix}
18-
(M1 <: SparseMatrixCOO) && (M2 <: SparseMatrixCOO) && return data
17+
function Base.convert(::Type{QPData{T, S, MCOO, MCOO}}, data::QPData{T, S, M1, M2}) where {T, S, M1 <: AbstractMatrix, M2 <: AbstractMatrix, MCOO <: SparseMatrixCOO{T}}
1918
HCOO = (M1 <: SparseMatrixCOO) ? data.H : SparseMatrixCOO(data.H)
2019
ACOO = (M2 <: SparseMatrixCOO) ? data.A : SparseMatrixCOO(data.A)
2120
return QPData(data.c0, data.c, HCOO, ACOO)
2221
end
22+
Base.convert(::Type{QPData{T, S, MCOO, MCOO}}, data::QPData{T, S, M1, M2}) where {T, S, M1 <: SparseMatrixCOO, M2 <: SparseMatrixCOO, MCOO <: SparseMatrixCOO{T}} = data
2323

2424
abstract type AbstractQuadraticModel{T, S} <: AbstractNLPModel{T, S} end
2525

@@ -262,25 +262,21 @@ end
262262
# TODO: Better hess_op
263263

264264
function NLPModels.hess_structure!(
265-
qp::QuadraticModel,
265+
qp::QuadraticModel{T, S, M1},
266266
rows::AbstractVector{<:Integer},
267267
cols::AbstractVector{<:Integer},
268-
)
269-
typeof(qp.data.H) <: SparseMatrixCOO ||
270-
error("hess_structure! should be used only if H is a SparseMatrixCOO")
268+
) where {T, S, M1 <: SparseMatrixCOO}
271269
rows .= qp.data.H.rows
272270
cols .= qp.data.H.cols
273271
return rows, cols
274272
end
275273

276274
function NLPModels.hess_coord!(
277-
qp::QuadraticModel{T},
275+
qp::QuadraticModel{T, S, M1},
278276
x::AbstractVector{T},
279277
vals::AbstractVector{T};
280278
obj_weight::Real = one(eltype(x)),
281-
) where {T}
282-
typeof(qp.data.H) <: SparseMatrixCOO ||
283-
error("hess_coord! should be used only if H is a SparseMatrixCOO")
279+
) where {T, S, M1 <: SparseMatrixCOO}
284280
NLPModels.increment!(qp, :neval_hess)
285281
vals .= obj_weight * qp.data.H.vals
286282
return vals
@@ -295,20 +291,20 @@ NLPModels.hess_coord!(
295291
) = hess_coord!(qp, x, vals, obj_weight = obj_weight)
296292

297293
function NLPModels.jac_structure!(
298-
qp::QuadraticModel,
294+
qp::QuadraticModel{T, S, M1, M2},
299295
rows::AbstractVector{<:Integer},
300296
cols::AbstractVector{<:Integer},
301-
)
302-
typeof(qp.data.A) <: SparseMatrixCOO ||
303-
error("jac_structure! should be used only if A is a SparseMatrixCOO")
297+
) where {T, S, M1, M2 <: SparseMatrixCOO}
304298
rows .= qp.data.A.rows
305299
cols .= qp.data.A.cols
306300
return rows, cols
307301
end
308302

309-
function NLPModels.jac_coord!(qp::QuadraticModel, x::AbstractVector, vals::AbstractVector)
310-
typeof(qp.data.A) <: SparseMatrixCOO ||
311-
error("jac_coord! should be used only if A is a SparseMatrixCOO")
303+
function NLPModels.jac_coord!(
304+
qp::QuadraticModel{T, S, M1, M2},
305+
x::AbstractVector,
306+
vals::AbstractVector
307+
) where {T, S, M1, M2 <: SparseMatrixCOO}
312308
NLPModels.increment!(qp, :neval_jac)
313309
vals .= qp.data.A.vals
314310
return vals
@@ -434,14 +430,13 @@ function slackdata(data::QPData{T}, meta::NLPModelMeta{T}, ns::Int) where {T}
434430
)
435431
end
436432

437-
function NLPModelsModifiers.SlackModel(qp::AbstractQuadraticModel, name = qp.meta.name * "-slack")
433+
function NLPModelsModifiers.SlackModel(qp::AbstractQuadraticModel{T, S}, name = qp.meta.name * "-slack") where{T, S}
438434
qp.meta.ncon == length(qp.meta.jfix) && return qp
439435
nfix = length(qp.meta.jfix)
440436
ns = qp.meta.ncon - nfix
441-
T = eltype(qp.data.c)
442437

443438
if isdense(qp.data) # convert to QPDataCOO first
444-
dataCOO = convertQPData_toCOO(qp.data)
439+
dataCOO = convert(QPData{T, S, SparseMatrixCOO{T, Int}, SparseMatrixCOO{T, Int}}, qp.data)
445440
data = slackdata(dataCOO, qp.meta, ns)
446441
else
447442
data = slackdata(qp.data, qp.meta, ns)

test/runtests.jl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,7 @@ end
111111
name = "QM1",
112112
coo_matrices = false,
113113
)
114-
115-
@test_throws Exception hess_structure(qpdense)
116-
@test_throws Exception hess_coord(qpdense)
117-
@test_throws Exception jac_structure(qpdense)
118-
@test_throws Exception jac_coord(qpdense)
119-
114+
120115
smdense = SlackModel(qpdense)
121116
testSM(smdense)
122117
end

0 commit comments

Comments
 (0)