From 183448a182c5e78fe42b6aaba395c7e4bda8c781 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 17 Jan 2019 17:32:15 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20zero=20for=20functions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Utilities/functions.jl | 8 ++++++++ test/Utilities/functions.jl | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/Utilities/functions.jl b/src/Utilities/functions.jl index 81895d1947..c8091464b4 100644 --- a/src/Utilities/functions.jl +++ b/src/Utilities/functions.jl @@ -547,6 +547,14 @@ end # Arithmetic +function Base.zero(::Type{MOI.ScalarAffineFunction{T}}) where T + return MOI.ScalarAffineFunction(MOI.ScalarAffineTerm{T}[], zero(T)) +end +function Base.zero(::Type{MOI.ScalarQuadraticFunction{T}}) where T + return MOI.ScalarQuadraticFunction(MOI.ScalarAffineTerm{T}[], + MOI.ScalarQuadraticTerm{T}[], zero(T)) +end + """ operate(op::Function, ::Type{T}, args::Union{T, MOI.AbstractFunction}...)::MOI.AbstractFunction where T diff --git a/test/Utilities/functions.jl b/test/Utilities/functions.jl index 8590286787..bf71cb4bf1 100644 --- a/test/Utilities/functions.jl +++ b/test/Utilities/functions.jl @@ -155,6 +155,10 @@ end @testset "Scalar" begin @testset "Affine" begin + @testset "zero" begin + f = @inferred MOIU.zero(MOI.ScalarAffineFunction{Float64}) + @test MOIU.isapprox_zero(f, 1e-16) + end @testset "promote_operation" begin @test MOIU.promote_operation(+, Float64, MOI.SingleVariable, MOI.SingleVariable) == MOI.ScalarAffineFunction{Float64} @@ -225,6 +229,10 @@ end end @testset "Quadratic" begin + @testset "zero" begin + f = @inferred MOIU.zero(MOI.ScalarQuadraticFunction{Float64}) + @test MOIU.isapprox_zero(f, 1e-16) + end @testset "promote_operation" begin @test MOIU.promote_operation(+, Int, MOI.ScalarQuadraticFunction{Int},