Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for issue #3736 #3737

Merged
merged 5 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions src/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -568,3 +568,9 @@ function Base.complex(
)
return r + im * i
end

# These methods are used to provide an efficient implementation for a common
# case like `x^2 * sum(f for i in 1:0)`, which lowers to
# `_MA.operate!!(*, x^2, _MA.Zero())`
_MA.operate!!(::typeof(*), ::AbstractJuMPScalar, ::_MA.Zero) = _MA.Zero()
odow marked this conversation as resolved.
Show resolved Hide resolved
_MA.operate!!(::typeof(*), ::_MA.Zero, ::AbstractJuMPScalar) = _MA.Zero()
33 changes: 33 additions & 0 deletions test/test_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -519,4 +519,37 @@ function test_aff_expr_complex_HermitianPSDCone()
return
end

function test_multiply_expr_MA_Zero()
model = Model()
@variable(model, x)
for f in (x, x^2, sin(x))
@test @expression(model, f * sum(x for i in 1:0)) == 0.0
@test @expression(model, sum(x for i in 1:0) * f) == 0.0
@test @expression(model, -f * sum(x for i in 1:0)) == 0.0
@test @expression(model, sum(x for i in 1:0) * -f) == 0.0
@test @expression(model, (f + f) * sum(x for i in 1:0)) == 0.0
@test @expression(model, sum(x for i in 1:0) * (f + f)) == 0.0

@test isequal_canonical(@expression(model, f + sum(x for i in 1:0)), f)
@test isequal_canonical(@expression(model, sum(x for i in 1:0) + f), f)
@test isequal_canonical(
@expression(model, -f + sum(x for i in 1:0)),
-1.0 * f, # Needed for f = sin(x)
)
@test isequal_canonical(
@expression(model, sum(x for i in 1:0) + -f),
-1.0 * f, # Needed for f = sin(x)
)
@test isequal_canonical(
@expression(model, (f + f) + sum(x for i in 1:0)),
f + f,
)
@test isequal_canonical(
@expression(model, sum(x for i in 1:0) + (f + f)),
f + f,
)
end
return
end

end # TestExpr
Loading