Skip to content
Closed
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
9 changes: 1 addition & 8 deletions src/Utilities/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2207,14 +2207,7 @@ Returns the vector of scalar affine functions in the form of a
`MOI.VectorAffineFunction{T}`.
"""
function vectorize(funcs::AbstractVector{MOI.ScalarAffineFunction{T}}) where {T}
nterms =
mapreduce(func -> number_of_affine_terms(T, func), +, funcs, init = 0)
out_dim = mapreduce(func -> output_dim(T, func), +, funcs, init = 0)
terms = Vector{MOI.VectorAffineTerm{T}}(undef, nterms)
constant = zeros(T, out_dim)
fill_vector(terms, T, fill_terms, number_of_affine_terms, funcs)
fill_vector(constant, T, fill_constant, output_dim, funcs)
return MOI.VectorAffineFunction(terms, constant)
return MOI.VectorAffineFunction(funcs)
end

"""
Expand Down
13 changes: 13 additions & 0 deletions src/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,19 @@ function VectorAffineFunction{T}(f::VectorOfVariables) where {T}
return VectorAffineFunction(terms, constants)
end

function VectorAffineFunction(
rows::AbstractVector{ScalarAffineFunction{T}},
) where {T}
ret = VectorAffineFunction{T}(VectorAffineTerm{T}[], T[])
for (row, f) in enumerate(rows)
push!(ret.constants, f.constant)
for term in f.terms
push!(ret.terms, VectorAffineTerm(row, term))
end
end
return ret
end

"""
VectorQuadraticTerm{T}(
output_index::Int64,
Expand Down
14 changes: 14 additions & 0 deletions test/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,20 @@ function test_functions_convert_ScalarQuadraticFunction()
)
end

function test_vectoraffinefunction_creation()
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_isapprox_VectorOfVariables()
x = MOI.VariableIndex(1)
y = MOI.VariableIndex(2)
Expand Down
Loading