diff --git a/src/Utilities/functions.jl b/src/Utilities/functions.jl index 2603c899ad..6e28be93ca 100644 --- a/src/Utilities/functions.jl +++ b/src/Utilities/functions.jl @@ -1922,6 +1922,37 @@ function Base.:-(α::Number, f::MOI.SingleVariable) return operate(-, typeof(α), α, f) end +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.:-(::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.:*( + ::MOI.SingleVariable, + ::MOI.SingleVariable, + ::MOI.SingleVariable..., +) + 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 314482dcb0..7137fddbc8 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_throws ErrorException x + x + @test_throws ErrorException x - x + @test_throws ErrorException x * x + return +end + end # module TestFunctions.runtests()