diff --git a/Project.toml b/Project.toml index 5e8e34c1a4b..109722c1e2c 100644 --- a/Project.toml +++ b/Project.toml @@ -13,7 +13,7 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" [compat] MathOptInterface = "1.7" -MutableArithmetics = "1" +MutableArithmetics = "1.1" OrderedCollections = "1" julia = "1.6" diff --git a/docs/src/manual/expressions.md b/docs/src/manual/expressions.md index 8b5fc78ff86..906bba30dda 100644 --- a/docs/src/manual/expressions.md +++ b/docs/src/manual/expressions.md @@ -182,7 +182,7 @@ ex = @expression(model, x^2 + 2 * x * y + y^2 + x + y - 1) # output -x² + 2 y*x + y² + x + y - 1 +x² + 2 x*y + y² + x + y - 1 ``` ### Operator overloading diff --git a/src/complement.jl b/src/complement.jl index 0f8c782c8c3..67e352c40fb 100644 --- a/src/complement.jl +++ b/src/complement.jl @@ -70,6 +70,6 @@ function parse_constraint_call( F, x, ) - f, parse_code = _MA.rewrite(F) + f, parse_code = _MA.rewrite(F; move_factors_into_sums = false) return parse_code, :(_build_complements_constraint($errorf, $f, $(esc(x)))) end diff --git a/src/macros.jl b/src/macros.jl index a1ecf169fae..0bfbe412490 100644 --- a/src/macros.jl +++ b/src/macros.jl @@ -391,9 +391,9 @@ function parse_constraint_head( "`ub >= expr >= lb` are supported.", ) end - new_aff, parse_aff = _MA.rewrite(aff) - new_lb, parse_lb = _MA.rewrite(lb) - new_ub, parse_ub = _MA.rewrite(ub) + new_aff, parse_aff = _MA.rewrite(aff; move_factors_into_sums = false) + new_lb, parse_lb = _MA.rewrite(lb; move_factors_into_sums = false) + new_ub, parse_ub = _MA.rewrite(ub; move_factors_into_sums = false) parse_code = quote $parse_aff $parse_lb @@ -474,7 +474,7 @@ function parse_constraint_call( func, set, ) - f, parse_code = _MA.rewrite(func) + f, parse_code = _MA.rewrite(func; move_factors_into_sums = false) build_call = if vectorized :(build_constraint.($_error, _desparsify($f), Ref($(esc(set))))) else @@ -509,7 +509,7 @@ function parse_constraint_call( ) func = vectorized ? :($lhs .- $rhs) : :($lhs - $rhs) set = operator_to_set(_error, operator) - f, parse_code = _MA.rewrite(func) + f, parse_code = _MA.rewrite(func; move_factors_into_sums = false) # `_functionize` deals with the pathological case where the `lhs` is a # `VariableRef` and the `rhs` is a summation with no terms. f = :(_functionize($f)) @@ -1287,7 +1287,7 @@ macro objective(model, args...) end sense, x = args sense_expr = _moi_sense(_error, sense) - newaff, parsecode = _MA.rewrite(x) + newaff, parsecode = _MA.rewrite(x; move_factors_into_sums = false) code = quote $parsecode # Don't leak a `_MA.Zero` if the objective expression is an empty @@ -1353,7 +1353,7 @@ macro expression(args...) "different name for the index.", ) end - code = _MA.rewrite_and_return(x) + code = _MA.rewrite_and_return(x; move_factors_into_sums = false) code = quote # Don't leak a `_MA.Zero` if the expression is an empty summation, or # other structure that returns `_MA.Zero()`. diff --git a/test/macros.jl b/test/macros.jl index af23b48ec48..32cd0c3ba40 100644 --- a/test/macros.jl +++ b/test/macros.jl @@ -653,7 +653,7 @@ end function test_Error_on_unexpected_comparison() m = Model() @variable(m, x) - @test_macro_throws ErrorException @expression(m, x <= 1) + @test_throws ErrorException @expression(m, x <= 1) return end @@ -665,8 +665,7 @@ function test_Warn_on_unexpected_assignment() # Julia v1.3 onwards # ERROR: MethodError: no method matching getindex(::VariableRef; i=1) @test_throws Union{ErrorException,MethodError} x[i = 1] - # err = ErrorException("Unexpected assignment in expression `x[i=1]`.") - @test_macro_throws ErrorException @constraint(m, x[i = 1] <= 1) + @test_throws MethodError @constraint(m, x[i = 1] <= 1) return end diff --git a/test/print.jl b/test/print.jl index 6c74596551e..1feb7efc896 100644 --- a/test/print.jl +++ b/test/print.jl @@ -195,7 +195,7 @@ function test_printing_expressions() "x_{1}\\times y_{2,2} + x_{2}\\times y_{2,2} + z$ijulia_sq + 3 x_{1} + 3 x_{2} - 1", ) - ex = @expression(mod, -z * x[1] - x[1] * z + x[1] * x[2] + 0 * z^2) + ex = @expression(mod, -z * x[1] - z * x[1] + x[1] * x[2] + 0 * z^2) io_test(MIME("text/plain"), ex, "-2 z*x[1] + x[1]*x[2]") io_test(MIME("text/latex"), ex, "-2 z\\times x_{1} + x_{1}\\times x_{2}")