@@ -81,11 +81,11 @@ function test_extension_expression(
8181 @test string (@expression (model, sin (x)^ 2.0 )) == " sin(x) ^ 2"
8282 @test string (@expression (model, 2 * sin (x)^ 2.0 )) == " 2 * (sin(x) ^ 2)"
8383 @test string (@expression (model, 1 + sin (x))) == " 1 + sin(x)"
84- @test string (@expression (model, 1 + 2 * sin (x))) == " 1 + ( 2 * sin(x) )"
84+ @test string (@expression (model, 1 + 2 * sin (x))) == " 1 + 2 * sin(x)"
8585 @test string (@expression (model, 2.0 * sin (x)^ 2 + cos (x) / x)) ==
86- " ( 2 * (sin(x) ^ 2)) + ( cos(x) / x) "
86+ " 2 * (sin(x) ^ 2) + cos(x) / x"
8787 @test string (@expression (model, 2.0 * sin (x)^ 2 - cos (x) / x)) ==
88- " ( 2 * (sin(x) ^ 2)) - ( cos(x) / x) "
88+ " 2 * (sin(x) ^ 2) - cos(x) / x"
8989 return
9090end
9191
@@ -162,7 +162,7 @@ function test_extension_latex(ModelType = Model, VariableRefType = VariableRef)
162162
163163 @expression (model, g, ifelse (x > 0 , sin (x), x + cos (x)^ 2 ))
164164 @test function_string (MIME (" text/latex" ), g) ==
165- raw " \t extsf{ifelse}\l eft({{x} > {0}}, {\t extsf{sin}\l eft({x}\r ight)}, {{x} + {\l eft( {\t extsf{cos}\l eft({x}\r ight)} ^ {2}\r ight) }}\r ight)"
165+ raw " \t extsf{ifelse}\l eft({{x} > {0}}, {\t extsf{sin}\l eft({x}\r ight)}, {{x} + {{\t extsf{cos}\l eft({x}\r ight)} ^ {2}}}\r ight)"
166166 return
167167end
168168
@@ -189,13 +189,12 @@ function test_extension_expression_addmul(
189189)
190190 model = ModelType ()
191191 @variable (model, x)
192- @test string (@expression (model, x + 3 * sin (x))) == " x + (3 * sin(x))"
193- @test string (@expression (model, 2 * x + 3 * sin (x))) ==
194- " (2 x) + (3 * sin(x))"
192+ @test string (@expression (model, x + 3 * sin (x))) == " x + 3 * sin(x)"
193+ @test string (@expression (model, 2 * x + 3 * sin (x))) == " (2 x) + 3 * sin(x)"
195194 @test string (@expression (model, x^ 2 + 3 * sin (x))) ==
196- " ($(x^ 2 ) ) + ( 3 * sin(x) )"
195+ " ($(x^ 2 ) ) + 3 * sin(x)"
197196 @test string (@expression (model, sin (x) + 3 * sin (x))) ==
198- " sin(x) + ( 3 * sin(x) )"
197+ " sin(x) + 3 * sin(x)"
199198 @test string (@expression (model, sin (x) + 3 * x)) == " sin(x) + (3 x)"
200199 @test string (@expression (model, sin (x) + 3 * x * x)) ==
201200 " sin(x) + (3 $(x^ 2 ) )"
@@ -209,12 +208,12 @@ function test_extension_expression_explicit_add_mul(
209208 model = ModelType ()
210209 @variable (model, x)
211210 f = sin (x)
212- @test string (MA. operate!! (MA. add_mul, 1 , 2 , f)) == " 1 + ( 2 * $f ) "
213- @test string (MA. operate!! (MA. add_mul, 1 , f, 2 )) == " 1 + ( $f * 2) "
214- @test string (MA. operate!! (MA. add_mul, 1 , f, f)) == " 1 + ( $f * $f ) "
215- @test string (MA. operate!! (MA. add_mul, f, 2 , f)) == " $f + ( 2 * $f ) "
216- @test string (MA. operate!! (MA. add_mul, f, f, 2 )) == " $f + ( $f * 2) "
217- @test string (MA. operate!! (MA. add_mul, f, f, f)) == " $f + ( $f * $f ) "
211+ @test string (MA. operate!! (MA. add_mul, 1 , 2 , f)) == " 1 + 2 * $f "
212+ @test string (MA. operate!! (MA. add_mul, 1 , f, 2 )) == " 1 + $f * 2"
213+ @test string (MA. operate!! (MA. add_mul, 1 , f, f)) == " 1 + $f * $f "
214+ @test string (MA. operate!! (MA. add_mul, f, 2 , f)) == " $f + 2 * $f "
215+ @test string (MA. operate!! (MA. add_mul, f, f, 2 )) == " $f + $f * 2"
216+ @test string (MA. operate!! (MA. add_mul, f, f, f)) == " $f + $f * $f "
218217 return
219218end
220219
@@ -224,13 +223,12 @@ function test_extension_expression_submul(
224223)
225224 model = ModelType ()
226225 @variable (model, x)
227- @test string (@expression (model, x - 3 * sin (x))) == " x - (3 * sin(x))"
228- @test string (@expression (model, 2 * x - 3 * sin (x))) ==
229- " (2 x) - (3 * sin(x))"
226+ @test string (@expression (model, x - 3 * sin (x))) == " x - 3 * sin(x)"
227+ @test string (@expression (model, 2 * x - 3 * sin (x))) == " (2 x) - 3 * sin(x)"
230228 @test string (@expression (model, x^ 2 - 3 * sin (x))) ==
231- " ($(x^ 2 ) ) - ( 3 * sin(x) )"
229+ " ($(x^ 2 ) ) - 3 * sin(x)"
232230 @test string (@expression (model, sin (x) - 3 * sin (x))) ==
233- " sin(x) - ( 3 * sin(x) )"
231+ " sin(x) - 3 * sin(x)"
234232 @test string (@expression (model, sin (x) - 3 * x)) == " sin(x) - (3 x)"
235233 @test string (@expression (model, sin (x) - 3 * x * x)) ==
236234 " sin(x) - (3 $(x^ 2 ) )"
@@ -247,7 +245,7 @@ function test_extension_aff_expr_convert(
247245 @test _to_string (AffExpr (0.0 )) == " +(0)"
248246 @test _to_string (AffExpr (1.0 )) == " +(1)"
249247 @test _to_string (x + 1 ) == " x + 1"
250- @test _to_string (2 x + 1 ) == " ( 2 * x) + 1"
248+ @test _to_string (2 x + 1 ) == " 2 * x + 1"
251249 @test _to_string (2 x) == " 2 * x"
252250 return
253251end
@@ -261,15 +259,15 @@ function test_extension_quad_expr_convert(
261259 _to_string (x) = string (convert (GenericNonlinearExpr{VariableRefType}, x))
262260 @test _to_string (QuadExpr (AffExpr (0.0 ))) == " +(0)"
263261 @test _to_string (QuadExpr (AffExpr (1.0 ))) == " +(1)"
264- @test _to_string (x^ 2 + 1 ) == " ( x * x) + 1"
265- @test _to_string (2 x^ 2 + 1 ) == " ( 2 * x * x) + 1"
262+ @test _to_string (x^ 2 + 1 ) == " x * x + 1"
263+ @test _to_string (2 x^ 2 + 1 ) == " 2 * x * x + 1"
266264 @test _to_string (2 x^ 2 ) == " 2 * x * x"
267- @test _to_string (x^ 2 + x + 1 ) == " x + ( x * x) + 1"
268- @test _to_string (2 x^ 2 + x + 1 ) == " x + ( 2 * x * x) + 1"
269- @test _to_string (2 x^ 2 + x) == " x + ( 2 * x * x) "
270- @test _to_string (x^ 2 + 2 x + 1 ) == " ( 2 * x) + ( x * x) + 1"
271- @test _to_string (2 x^ 2 + 2 x + 1 ) == " ( 2 * x) + ( 2 * x * x) + 1"
272- @test _to_string (2 x^ 2 + 2 x) == " ( 2 * x) + ( 2 * x * x) "
265+ @test _to_string (x^ 2 + x + 1 ) == " x + x * x + 1"
266+ @test _to_string (2 x^ 2 + x + 1 ) == " x + 2 * x * x + 1"
267+ @test _to_string (2 x^ 2 + x) == " x + 2 * x * x"
268+ @test _to_string (x^ 2 + 2 x + 1 ) == " 2 * x + x * x + 1"
269+ @test _to_string (2 x^ 2 + 2 x + 1 ) == " 2 * x + 2 * x * x + 1"
270+ @test _to_string (2 x^ 2 + 2 x) == " 2 * x + 2 * x * x"
273271 return
274272end
275273
@@ -458,7 +456,7 @@ function test_extension_expr_mle(
458456 sum ((data[i] - x)^ 2 for i in 1 : n) / (2 * y^ 2 )
459457 )
460458 @test string (obj) ==
461- " ( 2 * log(1 / (2 $(y^ 2 ) ))) - (( 4 $(x^ 2 ) - 30 x + 85) / (2 $(y^ 2 ) ))"
459+ " 2 * log(1 / (2 $(y^ 2 ) )) - (4 $(x^ 2 ) - 30 x + 85) / (2 $(y^ 2 ) )"
462460 return
463461end
464462
@@ -1081,11 +1079,11 @@ function test_printing_truncation()
10811079 @variable (model, x[1 : 100 ])
10821080 y = @expression (model, sum (sin .(x) .* 2 ))
10831081 @test occursin (
1084- " ( sin(x[30]) * 2) + [[...40 terms omitted...]] + ( sin(x[71]) * 2) " ,
1082+ " sin(x[30]) * 2 + [[...40 terms omitted...]] + sin(x[71]) * 2" ,
10851083 function_string (MIME (" text/plain" ), y),
10861084 )
10871085 @test occursin (
1088- " {\\ left({ \\ textsf{sin}\\ left({x_{30}}\\ right)} * {2}\\ right) + {[[\\ ldots\\ text{40 terms omitted}\\ ldots]]} + {\\ left( {\\ textsf{sin}\\ left({x_{71}}\\ right)} * {2}\\ right) }" ,
1086+ " {\\ textsf{sin}\\ left({x_{30}}\\ right)} * {2} + {[[\\ ldots\\ text{40 terms omitted}\\ ldots]]} + {{\\ textsf{sin}\\ left({x_{71}}\\ right)} * {2}}" ,
10891087 function_string (MIME (" text/latex" ), y),
10901088 )
10911089 return
@@ -1466,4 +1464,22 @@ function test_moi_function_no_owner_model()
14661464 return
14671465end
14681466
1467+ function test_extension_expression_bedmas_parentheses (
1468+ ModelType = Model,
1469+ VariableRefType = VariableRef,
1470+ )
1471+ model = ModelType ()
1472+ @variable (model, x)
1473+ @expression (model, y, sin (x))
1474+ @test string (@expression (model, y + 2 * y)) == " sin(x) + 2 * sin(x)"
1475+ @test string (@expression (model, y + 2 / y)) == " sin(x) + 2 / sin(x)"
1476+ @test string (@expression (model, y + 2 ^ y)) == " sin(x) + 2 ^ sin(x)"
1477+ @test string (@expression (model, y + 2 - y)) == " (sin(x) + 2) - sin(x)"
1478+ @test string (@expression (model, y - 2 * y)) == " sin(x) - 2 * sin(x)"
1479+ @test string (@expression (model, y - 2 / y)) == " sin(x) - 2 / sin(x)"
1480+ @test string (@expression (model, y - 2 ^ y)) == " sin(x) - 2 ^ sin(x)"
1481+ @test string (@expression (model, y - 2 + y)) == " (sin(x) - 2) + sin(x)"
1482+ return
1483+ end
1484+
14691485end # module
0 commit comments