Skip to content

Commit 95d9357

Browse files
committed
[Nonlinear] parse x*x as x^2 in Nonlinear.Model
1 parent 4a8af79 commit 95d9357

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

src/Nonlinear/parse.jl

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -345,18 +345,40 @@ function parse_expression(
345345
x::MOI.ScalarQuadraticTerm,
346346
parent_index::Int,
347347
)
348-
id_mul = data.operators.multivariate_operator_to_id[:*]
349-
push!(expr.nodes, Node(NODE_CALL_MULTIVARIATE, id_mul, parent_index))
350-
mul_parent = length(expr.nodes)
351-
coef = x.coefficient
348+
# There are four cases:
349+
# (1): 1 * x * x --> ^(x, 2)
350+
# (2): a * x * x --> *(a, ^(x, 2))
351+
# (3): a * x * y --> *(a, x, y)
352+
# (4): 1 * x * y --> *(x, y)
352353
if x.variable_1 == x.variable_2
353-
coef /= 2
354-
end
355-
if !isone(coef)
356-
parse_expression(data, expr, coef, mul_parent)
354+
# Case (1): 1 * x * x --> ^(x, 2)
355+
# Case (2): a * x * x --> *(a, ^(x, 2))
356+
coef = x.coefficient / 2
357+
parent = parent_index
358+
if !isone(coef)
359+
id = data.operators.multivariate_operator_to_id[:*]
360+
push!(expr.nodes, Node(NODE_CALL_MULTIVARIATE, id, parent_index))
361+
parent = length(expr.nodes)
362+
parse_expression(data, expr, coef, parent)
363+
parent
364+
end
365+
id = data.operators.multivariate_operator_to_id[:^]
366+
push!(expr.nodes, Node(NODE_CALL_MULTIVARIATE, id, parent))
367+
pow_parent = length(expr.nodes)
368+
parse_expression(data, expr, x.variable_1, pow_parent)
369+
parse_expression(data, expr, 2, pow_parent)
370+
else
371+
# Case (3): a * x * y --> *(a, x, y)
372+
# Case (4): 1 * x * y --> *(x, y)
373+
id_mul = data.operators.multivariate_operator_to_id[:*]
374+
push!(expr.nodes, Node(NODE_CALL_MULTIVARIATE, id_mul, parent_index))
375+
mul_parent = length(expr.nodes)
376+
if !isone(x.coefficient)
377+
parse_expression(data, expr, x.coefficient, mul_parent)
378+
end
379+
parse_expression(data, expr, x.variable_1, mul_parent)
380+
parse_expression(data, expr, x.variable_2, mul_parent)
357381
end
358-
parse_expression(data, expr, x.variable_1, mul_parent)
359-
parse_expression(data, expr, x.variable_2, mul_parent)
360382
return
361383
end
362384

test/Nonlinear/Nonlinear.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,13 +1167,13 @@ function test_scalar_nonlinear_function_parse_scalarquadraticfunction()
11671167
MOI.ScalarQuadraticFunction(qterms, terms, 0.0) => :(0.0),
11681168
MOI.ScalarQuadraticFunction(qterms, terms, 1.0) => :(1.0),
11691169
MOI.ScalarQuadraticFunction(qterms, [aterm], 0.0) => :(2.0 * $x),
1170-
(1.0 * x * x + 1.0 * x + 1.0) => :($x * $x + $x + 1),
1171-
(1.0 * x * x + 1.0 * x) => :($x * $x + $x),
1172-
(1.0 * x * x + 2.0 * x) => :($x * $x + 2.0 * $x),
1173-
(2.0 * x * x + 2.0 * x) => :(2.0 * $x * $x + 2.0 * $x),
1174-
(1.0 * x * x) => :($x * $x),
1170+
(1.0 * x * x + 1.0 * x + 1.0) => :($x^2 + $x + 1),
1171+
(1.0 * x * x + 1.0 * x) => :($x^2 + $x),
1172+
(1.0 * x * x + 2.0 * x) => :($x^2 + 2.0 * $x),
1173+
(2.0 * x * x + 2.0 * x) => :(2.0 * $x^2 + 2.0 * $x),
1174+
(1.0 * x * x) => :($x^2),
11751175
(1.5 * x * x + 2.5 * x * y + 3.5 * x + 2.0) =>
1176-
:(1.5 * $x * $x + 2.5 * $x * $y + 3.5 * $x + 2.0),
1176+
:(1.5 * $x^2 + 2.5 * $x * $y + 3.5 * $x + 2.0),
11771177
)
11781178
nlp_model = MOI.Nonlinear.Model()
11791179
f1 = MOI.Nonlinear.add_expression(nlp_model, f)

0 commit comments

Comments
 (0)