Skip to content

Commit a183f8c

Browse files
odowmatbesancon
andcommitted
Add row-wise constructors for VectorAffine and VectorQuadratic functions
Co-authored-by: Mathieu Besancon <matbesancon@users.noreply.github.com>
1 parent e50a5a4 commit a183f8c

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/Utilities/functions.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,6 +2217,12 @@ function vectorize(funcs::AbstractVector{MOI.ScalarAffineFunction{T}}) where {T}
22172217
return MOI.VectorAffineFunction(terms, constant)
22182218
end
22192219

2220+
function MOI.VectorAffineFunction(
2221+
funcs::AbstractVector{MOI.ScalarAffineFunction{T}},
2222+
) where {T}
2223+
return vectorize(funcs)
2224+
end
2225+
22202226
"""
22212227
vectorize(funcs::AbstractVector{MOI.ScalarQuadraticFunction{T}}) where T
22222228
@@ -2251,6 +2257,12 @@ function vectorize(
22512257
return MOI.VectorQuadraticFunction(quadratic_terms, affine_terms, constant)
22522258
end
22532259

2260+
function MOI.VectorQuadraticFunction(
2261+
funcs::AbstractVector{MOI.ScalarQuadraticFunction{T}},
2262+
) where {T}
2263+
return vectorize(funcs)
2264+
end
2265+
22542266
function vectorize(x::AbstractVector{MOI.ScalarNonlinearFunction})
22552267
# Explicitly construct the output vector here because don't know that `x`
22562268
# has a `convert` method to `Vector`.

test/Utilities/functions.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,6 +2179,44 @@ function test_deprecated_eval_term()
21792179
return
21802180
end
21812181

2182+
function test_VectorAffineFunction_row_constructor()
2183+
x = MOI.VariableIndex(1)
2184+
f = MOI.VectorAffineFunction([1.0 * x + 2.0, 3.0 * x + 4.0])
2185+
g = MOI.VectorAffineFunction(
2186+
MOI.VectorAffineTerm{Float64}[
2187+
MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(1.0, x)),
2188+
MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(3.0, x)),
2189+
],
2190+
[2.0, 4.0],
2191+
)
2192+
@test f g
2193+
return
2194+
end
2195+
2196+
function test_VectorQuadraticFunction_row_constructor()
2197+
x, y = MOI.VariableIndex.(1:2)
2198+
row1 = 1.0 * x * x + 1.0 * y * y + 2.0 * x + 3.0 * y + 4.2
2199+
row2 = 3.0 * x * x + 3.0 * y * y + 1.0 * x + 3.0 * y + 1.2
2200+
f = MOI.VectorQuadraticFunction([row1, row2])
2201+
@test f MOI.Utilities.vectorize([row1, row2])
2202+
@test f MOI.VectorQuadraticFunction(
2203+
[
2204+
MOI.VectorQuadraticTerm(1, MOI.ScalarQuadraticTerm(2.0, x, x)),
2205+
MOI.VectorQuadraticTerm(1, MOI.ScalarQuadraticTerm(2.0, y, y)),
2206+
MOI.VectorQuadraticTerm(2, MOI.ScalarQuadraticTerm(6.0, x, x)),
2207+
MOI.VectorQuadraticTerm(2, MOI.ScalarQuadraticTerm(6.0, y, y)),
2208+
],
2209+
[
2210+
MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(2.0, x)),
2211+
MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(3.0, y)),
2212+
MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(1.0, x)),
2213+
MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(3.0, y)),
2214+
],
2215+
[4.2, 1.2],
2216+
)
2217+
return
2218+
end
2219+
21822220
end # module
21832221

21842222
TestUtilitiesFunctions.runtests()

0 commit comments

Comments
 (0)