@@ -2,22 +2,10 @@ module SimplifyEquationModule
22
33import .. EquationModule: Node, copy_node
44import .. OperatorEnumModule: AbstractOperatorEnum
5- import .. UtilsModule: isbad, isgood, @generate_idmap , @use_idmap
5+ import .. UtilsModule: isbad, isgood
66
77# 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}
219 # NOTE: (const (+*-) const) already accounted for. Call simplify_tree before.
2210 # ((const + var) + const) => (const + var)
2311 # ((const * var) * const) => (const * var)
2715 if tree. degree == 0
2816 return tree
2917 elseif tree. degree == 1
30- tree. l = _combine_operators (tree. l, operators)
18+ tree. l = combine_operators (tree. l, operators)
3119 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)
3422 end
3523
3624 top_level_constant = tree. degree == 2 && (tree. l. constant || tree. r. constant)
10896end
10997
11098# 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}
122100 if tree. degree == 1
123- tree. l = _simplify_tree (tree. l, operators)
101+ tree. l = simplify_tree (tree. l, operators)
124102 if tree. l. degree == 0 && tree. l. constant
125103 l = tree. l. val:: T
126104 if isgood (l)
132110 end
133111 end
134112 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)
137115 constantsBelow = (
138116 tree. l. degree == 0 && tree. l. constant && tree. r. degree == 0 && tree. r. constant
139117 )
0 commit comments