@@ -16,35 +16,35 @@ const TREE_SHRINK_OPS = [*, +]
16
16
function _simplify_constants (O,shorten_tree = true )
17
17
# Tree shrinking
18
18
if shorten_tree
19
- for cur_op in TREE_SHRINK_OPS
20
- if O. op == cur_op
21
- # Shrink tree
22
- if any (x -> is_operation (x) && x. op == cur_op, O. args)
23
- idxs = findall (x -> is_operation (x) && x. op == cur_op, O. args)
24
- keep_idxs = 1 : length (O. args) .∉ (idxs,)
25
- args = Vector{Expression}[O. args[i]. args for i in idxs]
26
- push! (args,O. args[keep_idxs])
27
- return Operation (O. op, vcat (args... ))
28
- end
29
- # Collapse constants
30
- idxs = findall (is_constant, O. args)
31
- if length (idxs) > 1
32
- other_idxs = 1 : length (O. args) .∉ (idxs,)
33
- if cur_op == (* )
34
- new_var = Constant (prod (get, O. args[idxs]))
35
- elseif cur_op == (+ )
36
- new_var = Constant (sum (get, O. args[idxs]))
37
- end
38
- new_args = O. args[other_idxs]
39
- push! (new_args,new_var)
40
- if length (new_args) > 1
41
- return Operation (O. op,new_args)
42
- else
43
- return new_args[1 ]
44
- end
45
- end
19
+ for cur_op in TREE_SHRINK_OPS
20
+ if O. op == cur_op
21
+ # Shrink tree
22
+ if any (x -> is_operation (x) && x. op == cur_op, O. args)
23
+ idxs = findall (x -> is_operation (x) && x. op == cur_op, O. args)
24
+ keep_idxs = 1 : length (O. args) .∉ (idxs,)
25
+ args = Vector{Expression}[O. args[i]. args for i in idxs]
26
+ push! (args,O. args[keep_idxs])
27
+ return Operation (O. op, vcat (args... ))
28
+ end
29
+ # Collapse constants
30
+ idxs = findall (is_constant, O. args)
31
+ if length (idxs) > 1
32
+ other_idxs = 1 : length (O. args) .∉ (idxs,)
33
+ if cur_op == (* )
34
+ new_var = Constant (prod (get, O. args[idxs]))
35
+ elseif cur_op == (+ )
36
+ new_var = Constant (sum (get, O. args[idxs]))
37
+ end
38
+ new_args = O. args[other_idxs]
39
+ push! (new_args,new_var)
40
+ if length (new_args) > 1
41
+ return Operation (O. op,new_args)
42
+ else
43
+ return new_args[1 ]
44
+ end
45
+ end
46
+ end
46
47
end
47
- end
48
48
end
49
49
50
50
if O. op == (* )
0 commit comments