Skip to content

Commit 9d8a931

Browse files
committed
Fix format
1 parent 23cb0da commit 9d8a931

File tree

3 files changed

+46
-26
lines changed

3 files changed

+46
-26
lines changed

src/Nonlinear/parse.jl

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,26 @@ function _extract_subexpression!(expr::Expression, root::Int)
4040
n = length(expr.nodes)
4141
# The whole subexpression is continuous in the tape
4242
first_out = findfirst((root+1):n) do i
43-
expr.nodes[i].parent < root
43+
return expr.nodes[i].parent < root
4444
end
4545
if isnothing(first_out)
4646
I = root:n
4747
else
48-
I = root .+ (0:(first_out - 1))
48+
I = root .+ (0:(first_out-1))
4949
end
5050
first_value = findfirst(I) do i
51-
expr.nodes[i].type == NODE_VALUE
51+
return expr.nodes[i].type == NODE_VALUE
5252
end
5353
if isnothing(first_value)
5454
V = nothing
5555
else
5656
last_value = findlast(I) do i
57-
expr.nodes[i].type == NODE_VALUE
57+
return expr.nodes[i].type == NODE_VALUE
5858
end
5959
V = expr.nodes[I[first_value]].index:expr.nodes[I[last_value]].index
6060
end
6161
if !isnothing(first_out)
62-
for i in last(I)+1:n
62+
for i in (last(I)+1):n
6363
node = expr.nodes[i]
6464
index = node.index
6565
if node.type == NODE_VALUE && !isnothing(V)
@@ -80,7 +80,8 @@ end
8080
function _extract_subexpression!(data::Model, expr::Expression, root::Int)
8181
parent = expr.nodes[root].parent
8282
I, V = _extract_subexpression!(expr, root)
83-
subexpr = Expression(expr.nodes[I], isnothing(V) ? Float64[] : expr.values[V])
83+
subexpr =
84+
Expression(expr.nodes[I], isnothing(V) ? Float64[] : expr.values[V])
8485
push!(data.expressions, subexpr)
8586
index = ExpressionIndex(length(data.expressions))
8687
expr.nodes[root] = Node(NODE_SUBEXPRESSION, index.value, parent)
@@ -119,7 +120,8 @@ function parse_expression(
119120
_parent_node = stack[i][1]
120121
if _parent_node > first(I)
121122
@assert _parent_node > last(I)
122-
stack[i] = (_parent_node - length(I) + 1, stack[i][2])
123+
stack[i] =
124+
(_parent_node - length(I) + 1, stack[i][2])
123125
end
124126
end
125127
end
@@ -128,15 +130,27 @@ function parse_expression(
128130
__expr, __node = val
129131
if _expr === __expr && __node > first(I)
130132
@assert __node > last(I)
131-
data.cache[key] = (__expr, __node - length(I) + 1)
133+
data.cache[key] =
134+
(__expr, __node - length(I) + 1)
132135
end
133136
end
134137
end
135138
data.cache[arg] = subexpr
136139
end
137-
parse_expression(data, expr, subexpr::ExpressionIndex, parent_node)
140+
parse_expression(
141+
data,
142+
expr,
143+
subexpr::ExpressionIndex,
144+
parent_node,
145+
)
138146
else
139-
_parse_without_recursion_inner(stack, data, expr, arg, parent_node)
147+
_parse_without_recursion_inner(
148+
stack,
149+
data,
150+
expr,
151+
arg,
152+
parent_node,
153+
)
140154
data.cache[arg] = (expr, length(expr.nodes))
141155
end
142156
else

src/Nonlinear/types.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@ mutable struct Model
167167
# This is a private field, used only to increment the ConstraintIndex.
168168
last_constraint_index::Int64
169169
# This is a private field, used to detect common subexpressions.
170-
cache::Dict{MOI.ScalarNonlinearFunction,Union{ExpressionIndex,Tuple{Expression,Int}}}
170+
cache::Dict{
171+
MOI.ScalarNonlinearFunction,
172+
Union{ExpressionIndex,Tuple{Expression,Int}},
173+
}
171174
function Model()
172175
return new(
173176
nothing,
@@ -176,7 +179,10 @@ mutable struct Model
176179
Float64[],
177180
OperatorRegistry(),
178181
0,
179-
Dict{MOI.ScalarNonlinearFunction,Union{ExpressionIndex,Tuple{Expression,Int}}}(),
182+
Dict{
183+
MOI.ScalarNonlinearFunction,
184+
Union{ExpressionIndex,Tuple{Expression,Int}},
185+
}(),
180186
)
181187
end
182188
end

test/Nonlinear/Nonlinear.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,27 +1498,27 @@ function test_extract_subexpression()
14981498
# Test that the objective function gets rewritten as we reuse `h`
14991499
# Also test that we don't change the parents in the stack of `h`
15001500
# by creating a long stack
1501-
prod = MOI.ScalarNonlinearFunction(
1502-
:*,
1503-
[h, x],
1504-
)
1505-
sum = MOI.ScalarNonlinearFunction(
1506-
:*,
1507-
[x, x, x, x, prod],
1508-
)
1501+
prod = MOI.ScalarNonlinearFunction(:*, [h, x])
1502+
sum = MOI.ScalarNonlinearFunction(:*, [x, x, x, x, prod])
15091503
expr = Nonlinear.parse_expression(model, sum)
15101504
@test isempty(model.objective.values)
15111505
@test model.objective.nodes == [
15121506
Nonlinear.Node(Nonlinear.NODE_CALL_MULTIVARIATE, 1, -1),
15131507
Nonlinear.Node(Nonlinear.NODE_SUBEXPRESSION, 1, 1),
15141508
Nonlinear.Node(Nonlinear.NODE_SUBEXPRESSION, 2, 1),
15151509
]
1516-
@test model.expressions == [expected_sub, Nonlinear.Expression([
1517-
Nonlinear.Node(Nonlinear.NODE_CALL_MULTIVARIATE, 3, 1),
1518-
Nonlinear.Node(Nonlinear.NODE_VALUE, 1, 3),
1519-
Nonlinear.Node(Nonlinear.NODE_SUBEXPRESSION, 1, 3),
1520-
Nonlinear.Node(Nonlinear.NODE_VALUE, 2, 3),
1521-
], [2.0, 1.0])]
1510+
@test model.expressions == [
1511+
expected_sub,
1512+
Nonlinear.Expression(
1513+
[
1514+
Nonlinear.Node(Nonlinear.NODE_CALL_MULTIVARIATE, 3, 1),
1515+
Nonlinear.Node(Nonlinear.NODE_VALUE, 1, 3),
1516+
Nonlinear.Node(Nonlinear.NODE_SUBEXPRESSION, 1, 3),
1517+
Nonlinear.Node(Nonlinear.NODE_VALUE, 2, 3),
1518+
],
1519+
[2.0, 1.0],
1520+
),
1521+
]
15221522
@test isempty(expr.values)
15231523
@test expr.nodes == [
15241524
Nonlinear.Node(Nonlinear.NODE_CALL_MULTIVARIATE, 3, -1),

0 commit comments

Comments
 (0)