@@ -2,22 +2,10 @@ module SimplifyEquationModule
2
2
3
3
import .. EquationModule: Node, copy_node
4
4
import .. OperatorEnumModule: AbstractOperatorEnum
5
- import .. UtilsModule: isbad, isgood, @generate_idmap , @use_idmap
5
+ import .. UtilsModule: isbad, isgood
6
6
7
7
# Simplify tree
8
- function combine_operators (
9
- tree:: Node{T} , operators:: AbstractOperatorEnum ; preserve_topology:: Bool = false
10
- ) where {T}
11
- if preserve_topology
12
- @use_idmap (_combine_operators (tree, operators), IdDict {Node{T},Node{T}} ())
13
- else
14
- _combine_operators (tree, operators)
15
- end
16
- end
17
-
18
- @generate_idmap tree function _combine_operators (
19
- tree:: Node{T} , operators
20
- ):: Node{T} where {T}
8
+ function combine_operators (tree:: Node{T} , operators:: AbstractOperatorEnum ) where {T}
21
9
# NOTE: (const (+*-) const) already accounted for. Call simplify_tree before.
22
10
# ((const + var) + const) => (const + var)
23
11
# ((const * var) * const) => (const * var)
27
15
if tree. degree == 0
28
16
return tree
29
17
elseif tree. degree == 1
30
- tree. l = _combine_operators (tree. l, operators)
18
+ tree. l = combine_operators (tree. l, operators)
31
19
elseif tree. degree == 2
32
- tree. l = _combine_operators (tree. l, operators)
33
- tree. r = _combine_operators (tree. r, operators)
20
+ tree. l = combine_operators (tree. l, operators)
21
+ tree. r = combine_operators (tree. r, operators)
34
22
end
35
23
36
24
top_level_constant = tree. degree == 2 && (tree. l. constant || tree. r. constant)
108
96
end
109
97
110
98
# Simplify tree
111
- function simplify_tree (
112
- tree:: Node{T} , operators:: AbstractOperatorEnum ; preserve_topology:: Bool = false
113
- ) where {T}
114
- if preserve_topology
115
- @use_idmap (_simplify_tree (tree, operators), IdDict {Node{T},Node{T}} ())
116
- else
117
- _simplify_tree (tree, operators)
118
- end
119
- end
120
-
121
- @generate_idmap tree function _simplify_tree (tree:: Node{T} , operators):: Node{T} where {T}
99
+ function simplify_tree (tree:: Node{T} , operators:: AbstractOperatorEnum ) where {T}
122
100
if tree. degree == 1
123
- tree. l = _simplify_tree (tree. l, operators)
101
+ tree. l = simplify_tree (tree. l, operators)
124
102
if tree. l. degree == 0 && tree. l. constant
125
103
l = tree. l. val:: T
126
104
if isgood (l)
132
110
end
133
111
end
134
112
elseif tree. degree == 2
135
- tree. l = _simplify_tree (tree. l, operators)
136
- tree. r = _simplify_tree (tree. r, operators)
113
+ tree. l = simplify_tree (tree. l, operators)
114
+ tree. r = simplify_tree (tree. r, operators)
137
115
constantsBelow = (
138
116
tree. l. degree == 0 && tree. l. constant && tree. r. degree == 0 && tree. r. constant
139
117
)
0 commit comments