Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/Utilities/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 +/-
###############################################################################

Expand Down
13 changes: 13 additions & 0 deletions test/Utilities/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()