Skip to content

Add row-wise constructors for VectorAffine and VectorQuadratic functions #2636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Utilities/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2217,6 +2217,12 @@
return MOI.VectorAffineFunction(terms, constant)
end

function MOI.VectorAffineFunction(

Check warning on line 2220 in src/Utilities/functions.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/functions.jl#L2220

Added line #L2220 was not covered by tests
funcs::AbstractVector{MOI.ScalarAffineFunction{T}},
) where {T}
return vectorize(funcs)
end

"""
vectorize(funcs::AbstractVector{MOI.ScalarQuadraticFunction{T}}) where T

Expand Down Expand Up @@ -2251,6 +2257,12 @@
return MOI.VectorQuadraticFunction(quadratic_terms, affine_terms, constant)
end

function MOI.VectorQuadraticFunction(

Check warning on line 2260 in src/Utilities/functions.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/functions.jl#L2260

Added line #L2260 was not covered by tests
funcs::AbstractVector{MOI.ScalarQuadraticFunction{T}},
) where {T}
return vectorize(funcs)
end

function vectorize(x::AbstractVector{MOI.ScalarNonlinearFunction})
# Explicitly construct the output vector here because don't know that `x`
# has a `convert` method to `Vector`.
Expand Down
38 changes: 38 additions & 0 deletions test/Utilities/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2179,6 +2179,44 @@ function test_deprecated_eval_term()
return
end

function test_VectorAffineFunction_row_constructor()
x = MOI.VariableIndex(1)
f = MOI.VectorAffineFunction([1.0 * x + 2.0, 3.0 * x + 4.0])
g = MOI.VectorAffineFunction(
MOI.VectorAffineTerm{Float64}[
MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(1.0, x)),
MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(3.0, x)),
],
[2.0, 4.0],
)
@test f ≈ g
return
end

function test_VectorQuadraticFunction_row_constructor()
x, y = MOI.VariableIndex.(1:2)
row1 = 1.0 * x * x + 1.0 * y * y + 2.0 * x + 3.0 * y + 4.2
row2 = 3.0 * x * x + 3.0 * y * y + 1.0 * x + 3.0 * y + 1.2
f = MOI.VectorQuadraticFunction([row1, row2])
@test f ≈ MOI.Utilities.vectorize([row1, row2])
@test f ≈ MOI.VectorQuadraticFunction(
[
MOI.VectorQuadraticTerm(1, MOI.ScalarQuadraticTerm(2.0, x, x)),
MOI.VectorQuadraticTerm(1, MOI.ScalarQuadraticTerm(2.0, y, y)),
MOI.VectorQuadraticTerm(2, MOI.ScalarQuadraticTerm(6.0, x, x)),
MOI.VectorQuadraticTerm(2, MOI.ScalarQuadraticTerm(6.0, y, y)),
],
[
MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(2.0, x)),
MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(3.0, y)),
MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(1.0, x)),
MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(3.0, y)),
],
[4.2, 1.2],
)
return
end

end # module

TestUtilitiesFunctions.runtests()
Loading