1
- function simplify_constants (O:: Operation )
1
+ function simplify_constants (O:: Operation ,shorten_tree = true )
2
2
O_last = nothing
3
3
_O = O
4
4
while _O != O_last
5
5
O_last = _O
6
- _O = _simplify_constants (_O)
6
+ _O = _simplify_constants (_O,shorten_tree )
7
7
if typeof (_O) <: Operation
8
- _O = Operation (_O. op,simplify_constants .(_O. args))
8
+ _O = Operation (_O. op,simplify_constants .(_O. args,shorten_tree ))
9
9
end
10
10
end
11
11
_O
12
12
end
13
13
14
14
const TREE_SHRINK_OPS = [:* ,:+ ]
15
15
16
- function _simplify_constants (O)
16
+ function _simplify_constants (O,shorten_tree = true )
17
17
# Tree shrinking
18
- for cur_op in TREE_SHRINK_OPS
19
- if Symbol (O. op) == cur_op
20
- # Shrink tree
21
- if any (x-> typeof (x)<: Operation && Symbol (x. op) == cur_op ,O. args)
22
- idxs = find (x-> typeof (x)<: Operation && Symbol (x. op) == cur_op,O. args)
23
- keep_idxs = 1 : length (O. args) .∉ (idxs,)
24
- args = Vector{Expression}[O. args[i]. args for i in idxs]
25
- push! (args,O. args[keep_idxs])
26
- return Operation (O. op,vcat (args... ))
27
- # Collapse constants
28
- elseif length (find (x-> typeof (x)<: Variable && x. subtype == :Constant ,O. args)) > 1
29
- idxs = find (x-> typeof (x)<: Variable && x. subtype == :Constant ,O. args)
30
- other_idxs = 1 : length (O. args) .∉ (idxs,)
31
- if cur_op == :*
32
- new_var = Constant (prod (x-> x. value,O. args[idxs]))
33
- elseif cur_op == :+
34
- new_var = Constant (sum (x-> x. value,O. args[idxs]))
35
- end
36
- new_args = O. args[other_idxs]
37
- push! (new_args,new_var)
38
- if length (new_args) > 1
39
- return Operation (O. op,new_args)
40
- else
41
- return new_args[1 ]
42
- end
18
+ if shorten_tree
19
+ for cur_op in TREE_SHRINK_OPS
20
+ if Symbol (O. op) == cur_op
21
+ # Shrink tree
22
+ if any (x-> typeof (x)<: Operation && Symbol (x. op) == cur_op ,O. args)
23
+ idxs = find (x-> typeof (x)<: Operation && Symbol (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
+ # Collapse constants
29
+ elseif length (find (x-> typeof (x)<: Variable && x. subtype == :Constant ,O. args)) > 1
30
+ idxs = find (x-> typeof (x)<: Variable && x. subtype == :Constant ,O. args)
31
+ other_idxs = 1 : length (O. args) .∉ (idxs,)
32
+ if cur_op == :*
33
+ new_var = Constant (prod (x-> x. value,O. args[idxs]))
34
+ elseif cur_op == :+
35
+ new_var = Constant (sum (x-> x. value,O. args[idxs]))
36
+ end
37
+ new_args = O. args[other_idxs]
38
+ push! (new_args,new_var)
39
+ if length (new_args) > 1
40
+ return Operation (O. op,new_args)
41
+ else
42
+ return new_args[1 ]
43
+ end
44
+ end
43
45
end
44
46
end
45
47
end
@@ -63,9 +65,8 @@ function _simplify_constants(O)
63
65
else
64
66
return O
65
67
end
66
- elseif (Symbol (O. op) == :+ || Symbol (O. op) == :- ) &&
67
- any (x-> typeof (x)<: Variable && (isequal (x,Constant (0 )) ||
68
- isequal (x,Constant (- 0 ))),O. args)
68
+ elseif Symbol (O. op) == :+ && any (x-> typeof (x)<: Variable &&
69
+ (isequal (x,Constant (0 )) || isequal (x,Constant (- 0 ))),O. args)
69
70
# If there are Constant(0)s in a big `+` expression, get rid of them
70
71
idxs = find (x-> typeof (x)<: Variable && (isequal (x,Constant (0 )) || isequal (x,Constant (- 0 ))),O. args)
71
72
_O = Operation (O. op,O. args[1 : length (O. args) .∉ (idxs,)])
@@ -76,15 +77,18 @@ function _simplify_constants(O)
76
77
else
77
78
return O
78
79
end
80
+ #=
79
81
elseif O.op == identity
80
82
return O.args[1]
83
+ =#
81
84
elseif Symbol (O. op) == :- && length (O. args) == 1
82
85
return Operation (* ,Expression[- 1 ,O. args[1 ]])
83
86
else
84
87
return O
85
88
end
89
+ return O
86
90
end
87
- simplify_constants (x:: Variable ) = x
88
- _simplify_constants (x:: Variable ) = x
91
+ simplify_constants (x:: Variable ,y ) = x
92
+ _simplify_constants (x:: Variable ,y ) = x
89
93
90
94
export simplify_constants
0 commit comments