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

Add tests for issue #3736 #3737

merged 5 commits into from
Apr 25, 2024

Conversation

odow
Copy link
Member

@odow odow commented Apr 24, 2024

Closes #3736

Needs release with jump-dev/MutableArithmetics.jl#283 before merging.

julia> 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
test_multiply_expr_MA_Zero (generic function with 1 method)

julia> @testset "" begin
           test_multiply_expr_MA_Zero()
       end
Test Summary: | Pass  Total  Time
              |   36     36  0.0s
Test.DefaultTestSet("", Any[], 36, false, false, true, 1.713934627962967e9, 1.713934627963343e9, false, "REPL[60]")

Copy link

codecov bot commented Apr 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.40%. Comparing base (509ab61) to head (04f0fc9).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #3737   +/-   ##
=======================================
  Coverage   98.40%   98.40%           
=======================================
  Files          43       43           
  Lines        5820     5820           
=======================================
  Hits         5727     5727           
  Misses         93       93           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

src/operators.jl Outdated Show resolved Hide resolved
@odow
Copy link
Member Author

odow commented Apr 24, 2024

Where are the methods?

I added because the stack trace from #3736 includes

  [1] _instantiate_zero(::Type{MutableArithmetics.Zero})
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:28
  [2] promote_operation_fallback(op::typeof(*), ::Type{QuadExpr}, ::Type{MutableArithmetics.Zero})
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:51
  [3] promote_operation(::typeof(*), ::Type, ::Type)
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:113
  [4] mutability(::Type, ::Function, ::Type, ::Type)
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:273
  [5] mutability(::QuadExpr, ::Function, ::QuadExpr, ::MutableArithmetics.Zero)
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:281
  [6] operate!!(::typeof(*), ::QuadExpr, ::MutableArithmetics.Zero)
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:616

This is going to additionally call zero(QuadExpr) to check whether the expression is mutable.

@blegat
Copy link
Member

blegat commented Apr 24, 2024

Then we should probably implement promote_operation instead ?

@odow
Copy link
Member Author

odow commented Apr 24, 2024

Changed to implement promote_operation

@blegat
Copy link
Member

blegat commented Apr 24, 2024

@odow
Copy link
Member Author

odow commented Apr 24, 2024

@odow odow merged commit 90fcfd0 into master Apr 25, 2024
11 checks passed
@odow odow deleted the od/add-ma-test branch April 25, 2024 03:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

MethodError with empty summations
2 participants