Skip to content

Commit df9394c

Browse files
committed
format
1 parent c288a3e commit df9394c

File tree

3 files changed

+44
-23
lines changed

3 files changed

+44
-23
lines changed

src/Utilities/functions.jl

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2226,29 +2226,7 @@ Returns the vector of scalar quadratic functions in the form of a
22262226
function vectorize(
22272227
funcs::AbstractVector{MOI.ScalarQuadraticFunction{T}},
22282228
) where {T}
2229-
num_affine_terms =
2230-
mapreduce(func -> number_of_affine_terms(T, func), +, funcs, init = 0)
2231-
num_quadratic_terms = mapreduce(
2232-
func -> number_of_quadratic_terms(T, func),
2233-
+,
2234-
funcs,
2235-
init = 0,
2236-
)
2237-
out_dim = mapreduce(func -> output_dim(T, func), +, funcs, init = 0)
2238-
affine_terms = Vector{MOI.VectorAffineTerm{T}}(undef, num_affine_terms)
2239-
quadratic_terms =
2240-
Vector{MOI.VectorQuadraticTerm{T}}(undef, num_quadratic_terms)
2241-
constant = zeros(T, out_dim)
2242-
fill_vector(affine_terms, T, fill_terms, number_of_affine_terms, funcs)
2243-
fill_vector(
2244-
quadratic_terms,
2245-
T,
2246-
fill_terms,
2247-
number_of_quadratic_terms,
2248-
funcs,
2249-
)
2250-
fill_vector(constant, T, fill_constant, output_dim, funcs)
2251-
return MOI.VectorQuadraticFunction(quadratic_terms, affine_terms, constant)
2229+
return MOI.VectorQuadraticFunction(funcs)
22522230
end
22532231

22542232
function vectorize(x::AbstractVector{MOI.ScalarNonlinearFunction})

src/functions.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,26 @@ struct VectorQuadraticFunction{T} <: AbstractVectorFunction
679679
constants::Vector{T}
680680
end
681681

682+
function VectorQuadraticFunction(
683+
rows::AbstractVector{ScalarQuadraticFunction{T}},
684+
) where {T}
685+
ret = VectorQuadraticFunction{T}(
686+
VectorQuadraticTerm{T}[],
687+
VectorAffineTerm{T}[],
688+
T[],
689+
)
690+
for (idx, f) in enumerate(rows)
691+
push!(ret.constants, f.constant)
692+
for term in f.quadratic_terms
693+
push!(ret.quadratic_terms, VectorQuadraticTerm(idx, term))
694+
end
695+
for term in f.affine_terms
696+
push!(ret.affine_terms, VectorAffineTerm(idx, term))
697+
end
698+
end
699+
return ret
700+
end
701+
682702
output_dimension(f::VectorQuadraticFunction) = length(f.constants)
683703

684704
constant(f::VectorQuadraticFunction) = f.constants

test/functions.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,29 @@ function test_functions_convert_ScalarQuadraticFunction()
129129
)
130130
end
131131

132+
function test_VectorQuadraticFunction_constructor()
133+
x = MOI.VariableIndex.(1:2)
134+
expr1 = dot(1.0 * x, x) + dot([2.0, 3.0], x) + 4.2
135+
expr2 = dot(3.0 * x, x) + dot([1.0, 3.0], x) + 1.2
136+
f = MOI.VectorQuadraticFunction([expr1, expr2])
137+
f_vec = MOI.Utilities.vectorize([expr1, expr2])
138+
@test f f_vec
139+
@test MOI.output_dimension(f) == 2
140+
@test f.constants == [4.2, 1.2]
141+
@test f.quadratic_terms == [
142+
MOI.VectorQuadraticTerm(1, MOI.ScalarQuadraticTerm(2.0, x[1], x[1])),
143+
MOI.VectorQuadraticTerm(1, MOI.ScalarQuadraticTerm(2.0, x[2], x[2])),
144+
MOI.VectorQuadraticTerm(2, MOI.ScalarQuadraticTerm(6.0, x[1], x[1])),
145+
MOI.VectorQuadraticTerm(2, MOI.ScalarQuadraticTerm(6.0, x[2], x[2])),
146+
]
147+
@test f.affine_terms == [
148+
MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(2.0, x[1])),
149+
MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(3.0, x[2])),
150+
MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(1.0, x[1])),
151+
MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(3.0, x[2])),
152+
]
153+
end
154+
132155
function test_isapprox_VectorOfVariables()
133156
x = MOI.VariableIndex(1)
134157
y = MOI.VariableIndex(2)

0 commit comments

Comments
 (0)