From df9394c71fd9151f3a2311d84d0ad00186e225bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathieu=20Besan=C3=A7on?= Date: Mon, 17 Feb 2025 11:17:59 +0100 Subject: [PATCH 1/5] format --- src/Utilities/functions.jl | 24 +----------------------- src/functions.jl | 20 ++++++++++++++++++++ test/functions.jl | 23 +++++++++++++++++++++++ 3 files changed, 44 insertions(+), 23 deletions(-) diff --git a/src/Utilities/functions.jl b/src/Utilities/functions.jl index 2d558f92e4..ed9884088b 100644 --- a/src/Utilities/functions.jl +++ b/src/Utilities/functions.jl @@ -2226,29 +2226,7 @@ Returns the vector of scalar quadratic functions in the form of a function vectorize( funcs::AbstractVector{MOI.ScalarQuadraticFunction{T}}, ) where {T} - num_affine_terms = - mapreduce(func -> number_of_affine_terms(T, func), +, funcs, init = 0) - num_quadratic_terms = mapreduce( - func -> number_of_quadratic_terms(T, func), - +, - funcs, - init = 0, - ) - out_dim = mapreduce(func -> output_dim(T, func), +, funcs, init = 0) - affine_terms = Vector{MOI.VectorAffineTerm{T}}(undef, num_affine_terms) - quadratic_terms = - Vector{MOI.VectorQuadraticTerm{T}}(undef, num_quadratic_terms) - constant = zeros(T, out_dim) - fill_vector(affine_terms, T, fill_terms, number_of_affine_terms, funcs) - fill_vector( - quadratic_terms, - T, - fill_terms, - number_of_quadratic_terms, - funcs, - ) - fill_vector(constant, T, fill_constant, output_dim, funcs) - return MOI.VectorQuadraticFunction(quadratic_terms, affine_terms, constant) + return MOI.VectorQuadraticFunction(funcs) end function vectorize(x::AbstractVector{MOI.ScalarNonlinearFunction}) diff --git a/src/functions.jl b/src/functions.jl index b6193e0192..f99e33b2a1 100644 --- a/src/functions.jl +++ b/src/functions.jl @@ -679,6 +679,26 @@ struct VectorQuadraticFunction{T} <: AbstractVectorFunction constants::Vector{T} end +function VectorQuadraticFunction( + rows::AbstractVector{ScalarQuadraticFunction{T}}, +) where {T} + ret = VectorQuadraticFunction{T}( + VectorQuadraticTerm{T}[], + VectorAffineTerm{T}[], + T[], + ) + for (idx, f) in enumerate(rows) + push!(ret.constants, f.constant) + for term in f.quadratic_terms + push!(ret.quadratic_terms, VectorQuadraticTerm(idx, term)) + end + for term in f.affine_terms + push!(ret.affine_terms, VectorAffineTerm(idx, term)) + end + end + return ret +end + output_dimension(f::VectorQuadraticFunction) = length(f.constants) constant(f::VectorQuadraticFunction) = f.constants diff --git a/test/functions.jl b/test/functions.jl index d6143d161b..a633051ebf 100644 --- a/test/functions.jl +++ b/test/functions.jl @@ -129,6 +129,29 @@ function test_functions_convert_ScalarQuadraticFunction() ) end +function test_VectorQuadraticFunction_constructor() + x = MOI.VariableIndex.(1:2) + expr1 = dot(1.0 * x, x) + dot([2.0, 3.0], x) + 4.2 + expr2 = dot(3.0 * x, x) + dot([1.0, 3.0], x) + 1.2 + f = MOI.VectorQuadraticFunction([expr1, expr2]) + f_vec = MOI.Utilities.vectorize([expr1, expr2]) + @test f ≈ f_vec + @test MOI.output_dimension(f) == 2 + @test f.constants == [4.2, 1.2] + @test f.quadratic_terms == [ + MOI.VectorQuadraticTerm(1, MOI.ScalarQuadraticTerm(2.0, x[1], x[1])), + MOI.VectorQuadraticTerm(1, MOI.ScalarQuadraticTerm(2.0, x[2], x[2])), + MOI.VectorQuadraticTerm(2, MOI.ScalarQuadraticTerm(6.0, x[1], x[1])), + MOI.VectorQuadraticTerm(2, MOI.ScalarQuadraticTerm(6.0, x[2], x[2])), + ] + @test f.affine_terms == [ + MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(2.0, x[1])), + MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(3.0, x[2])), + MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(1.0, x[1])), + MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(3.0, x[2])), + ] +end + function test_isapprox_VectorOfVariables() x = MOI.VariableIndex(1) y = MOI.VariableIndex(2) From eb5286fe6ab97687031f8fffefa058e495a334a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathieu=20Besan=C3=A7on?= Date: Mon, 17 Feb 2025 11:24:16 +0100 Subject: [PATCH 2/5] import LA --- test/functions.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/functions.jl b/test/functions.jl index a633051ebf..312972025a 100644 --- a/test/functions.jl +++ b/test/functions.jl @@ -7,6 +7,8 @@ module TestFunctions using Test +using LinearAlgebra + import MathOptInterface as MOI """ From cb482729cbb2100b96ba5bb5941fa0c398525776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathieu=20Besan=C3=A7on?= Date: Mon, 17 Feb 2025 17:57:39 +0100 Subject: [PATCH 3/5] Update test/functions.jl Co-authored-by: Joaquim --- test/functions.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/functions.jl b/test/functions.jl index 312972025a..f120dcefc6 100644 --- a/test/functions.jl +++ b/test/functions.jl @@ -7,7 +7,7 @@ module TestFunctions using Test -using LinearAlgebra +import LinearAlgebra import MathOptInterface as MOI From 19c9b76a6cd00cd8e0629ac26e5b68190d95b2df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathieu=20Besan=C3=A7on?= Date: Mon, 17 Feb 2025 17:57:44 +0100 Subject: [PATCH 4/5] Update test/functions.jl Co-authored-by: Joaquim --- test/functions.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/functions.jl b/test/functions.jl index f120dcefc6..43b11efd62 100644 --- a/test/functions.jl +++ b/test/functions.jl @@ -133,8 +133,8 @@ end function test_VectorQuadraticFunction_constructor() x = MOI.VariableIndex.(1:2) - expr1 = dot(1.0 * x, x) + dot([2.0, 3.0], x) + 4.2 - expr2 = dot(3.0 * x, x) + dot([1.0, 3.0], x) + 1.2 + expr1 = LinearAlgebra.dot(1.0 * x, x) + LinearAlgebra.dot([2.0, 3.0], x) + 4.2 + expr2 = LinearAlgebra.dot(3.0 * x, x) + LinearAlgebra.dot([1.0, 3.0], x) + 1.2 f = MOI.VectorQuadraticFunction([expr1, expr2]) f_vec = MOI.Utilities.vectorize([expr1, expr2]) @test f ≈ f_vec From 1a2b4af80c1a01b24dec960295304e61920f363b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathieu=20Besan=C3=A7on?= Date: Mon, 17 Feb 2025 18:00:33 +0100 Subject: [PATCH 5/5] formatting --- test/functions.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/functions.jl b/test/functions.jl index 43b11efd62..e3cd3e3c83 100644 --- a/test/functions.jl +++ b/test/functions.jl @@ -133,8 +133,10 @@ end function test_VectorQuadraticFunction_constructor() x = MOI.VariableIndex.(1:2) - expr1 = LinearAlgebra.dot(1.0 * x, x) + LinearAlgebra.dot([2.0, 3.0], x) + 4.2 - expr2 = LinearAlgebra.dot(3.0 * x, x) + LinearAlgebra.dot([1.0, 3.0], x) + 1.2 + expr1 = + LinearAlgebra.dot(1.0 * x, x) + LinearAlgebra.dot([2.0, 3.0], x) + 4.2 + expr2 = + LinearAlgebra.dot(3.0 * x, x) + LinearAlgebra.dot([1.0, 3.0], x) + 1.2 f = MOI.VectorQuadraticFunction([expr1, expr2]) f_vec = MOI.Utilities.vectorize([expr1, expr2]) @test f ≈ f_vec