From 4355014291a528e46e7f0f1083acfeffe2d78ee8 Mon Sep 17 00:00:00 2001 From: odow Date: Thu, 26 Aug 2021 16:53:56 +1200 Subject: [PATCH 1/2] Add operator fallbacks for pure SingleVariable operations --- src/Utilities/functions.jl | 17 +++++++++++++++++ test/Utilities/functions.jl | 13 +++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/Utilities/functions.jl b/src/Utilities/functions.jl index 2603c899ad..8c8392c5d8 100644 --- a/src/Utilities/functions.jl +++ b/src/Utilities/functions.jl @@ -1922,6 +1922,23 @@ function Base.:-(α::Number, f::MOI.SingleVariable) return operate(-, typeof(α), α, f) end +# SingleVariable defaults to Float64 +function Base.:+(arg::MOI.SingleVariable, args::MOI.SingleVariable...) + return operate(+, Float64, arg, args...) +end + +function Base.:-(arg::MOI.SingleVariable, args::MOI.SingleVariable...) + return operate(-, Float64, arg, args...) +end + +function Base.:*( + arg::MOI.SingleVariable, + arg2::MOI.SingleVariable, + args::MOI.SingleVariable..., +) + return operate(*, Float64, arg, arg2, args...) +end + # Vector +/- ############################################################################### diff --git a/test/Utilities/functions.jl b/test/Utilities/functions.jl index 314482dcb0..8ff4dfc12d 100644 --- a/test/Utilities/functions.jl +++ b/test/Utilities/functions.jl @@ -1781,6 +1781,19 @@ function test_zero_with_output_dimension() return end +""" + test_SingleVariable_operators() + +Test the three Base.:(op) fallbacks for pure SingleVariable operations. +""" +function test_SingleVariable_operators() + x = MOI.SingleVariable(MOI.VariableIndex(1)) + @test x + x ≈ 1.0 * x + 1.0 * x + @test x - x ≈ 1.0 * x - 1.0 * x + @test x * x ≈ 1.0 * x * 1.0 * x + return +end + end # module TestFunctions.runtests() From 8efc46f5fb6bbb7fff43802abe2cd2a135699e26 Mon Sep 17 00:00:00 2001 From: odow Date: Fri, 27 Aug 2021 10:02:01 +1200 Subject: [PATCH 2/2] Throw error instead --- src/Utilities/functions.jl | 32 +++++++++++++++++++++++--------- test/Utilities/functions.jl | 6 +++--- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/Utilities/functions.jl b/src/Utilities/functions.jl index 8c8392c5d8..6e28be93ca 100644 --- a/src/Utilities/functions.jl +++ b/src/Utilities/functions.jl @@ -1922,21 +1922,35 @@ function Base.:-(α::Number, f::MOI.SingleVariable) return operate(-, typeof(α), α, f) end -# SingleVariable defaults to Float64 -function Base.:+(arg::MOI.SingleVariable, args::MOI.SingleVariable...) - return operate(+, Float64, arg, args...) +function Base.:+(::MOI.SingleVariable, ::MOI.SingleVariable...) + return error( + "Unable to add SingleVariables together because no coefficient type " * + "is specified. Instead of `x + y`, convert one of the terms to a " * + "`ScalarAffineFunction` first by left-multiplying by `one(T)` where " * + "`T` is the coefficient type For example: `1.0 * x + y`.", + ) end -function Base.:-(arg::MOI.SingleVariable, args::MOI.SingleVariable...) - return operate(-, Float64, arg, args...) +function Base.:-(::MOI.SingleVariable, ::MOI.SingleVariable...) + return error( + "Unable to subtract SingleVariables together because no coefficient " * + "type is specified. Instead of `x - y`, convert one of the terms to a " * + "`ScalarAffineFunction` first by left-multiplying by `one(T)` where " * + "`T` is the coefficient type For example: `1.0 * x - y`.", + ) end function Base.:*( - arg::MOI.SingleVariable, - arg2::MOI.SingleVariable, - args::MOI.SingleVariable..., + ::MOI.SingleVariable, + ::MOI.SingleVariable, + ::MOI.SingleVariable..., ) - return operate(*, Float64, arg, arg2, args...) + return error( + "Unable to multiply SingleVariables together because no coefficient " * + "type is specified. Instead of `x * y`, convert one of the terms to a " * + "`ScalarAffineFunction` first by left-multiplying by `one(T)` where " * + "`T` is the coefficient type For example: `1.0 * x * y`.", + ) end # Vector +/- diff --git a/test/Utilities/functions.jl b/test/Utilities/functions.jl index 8ff4dfc12d..7137fddbc8 100644 --- a/test/Utilities/functions.jl +++ b/test/Utilities/functions.jl @@ -1788,9 +1788,9 @@ Test the three Base.:(op) fallbacks for pure SingleVariable operations. """ function test_SingleVariable_operators() x = MOI.SingleVariable(MOI.VariableIndex(1)) - @test x + x ≈ 1.0 * x + 1.0 * x - @test x - x ≈ 1.0 * x - 1.0 * x - @test x * x ≈ 1.0 * x * 1.0 * x + @test_throws ErrorException x + x + @test_throws ErrorException x - x + @test_throws ErrorException x * x return end