Skip to content

Commit

Permalink
Change MutableArithmetics.rewrite to move_factors_into_sums=false
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Nov 23, 2022
1 parent f57b13f commit 8310930
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[compat]
MathOptInterface = "1.7"
MutableArithmetics = "1"
MutableArithmetics = "1.1"
OrderedCollections = "1"
julia = "1.6"

Expand Down
2 changes: 1 addition & 1 deletion docs/src/manual/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/complement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 7 additions & 7 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()`.
Expand Down
5 changes: 2 additions & 3 deletions test/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down

0 comments on commit 8310930

Please sign in to comment.