@@ -2,6 +2,13 @@ include("test_params.jl")
2
2
using DynamicExpressions, Test
3
3
import SymbolicUtils: simplify, Symbolic
4
4
import Random: MersenneTwister
5
+ import Base: ≈
6
+
7
+ function Base.:≈ (a:: String , b:: String )
8
+ a = replace (a, r" \s +" => " " )
9
+ b = replace (b, r" \s +" => " " )
10
+ return a == b
11
+ end
5
12
6
13
simplify_tree = DynamicExpressions. SimplifyEquationModule. simplify_tree
7
14
combine_operators = DynamicExpressions. SimplifyEquationModule. combine_operators
@@ -33,10 +40,10 @@ tree = convert(Node, eqn2, operators)
33
40
# that SymbolicUtils does not convert it to a power:
34
41
tree = Node (" x1" ) * Node (" x1" )
35
42
eqn = convert (Symbolic, tree, operators)
36
- @test repr (eqn) == " x1*x1"
43
+ @test repr (eqn) ≈ " x1*x1"
37
44
# Test converting back:
38
45
tree_copy = convert (Node, eqn, operators)
39
- @test repr (tree_copy) == " (x1 * x1)"
46
+ @test repr (tree_copy) ≈ " (x1* x1)"
40
47
41
48
# Let's test a much more complex function,
42
49
# with custom operators, and unary operators:
@@ -105,42 +112,42 @@ x1, x2, x3 = [Node(; feature=i) for i in 1:3]
105
112
106
113
# unary operator applied to constant => constant:
107
114
tree = Node (1 , Node (; val= 0.0 ))
108
- @test repr (tree) == " cos(0.0)"
109
- @test repr (simplify_tree (tree, operators)) == " 1.0"
115
+ @test repr (tree) ≈ " cos(0.0)"
116
+ @test repr (simplify_tree (tree, operators)) ≈ " 1.0"
110
117
111
118
# except when the result is a NaN, then we don't change it:
112
119
tree = Node (1 , Node (; val= NaN ))
113
- @test repr (tree) == " cos(NaN)"
114
- @test repr (simplify_tree (tree, operators)) == " cos(NaN)"
120
+ @test repr (tree) ≈ " cos(NaN)"
121
+ @test repr (simplify_tree (tree, operators)) ≈ " cos(NaN)"
115
122
116
123
# the same as above, but inside a binary tree.
117
124
tree =
118
125
Node (1 , Node (1 , Node (; val= 0.1 ), Node (; val= 0.2 )) + Node (; val= 0.2 )) + Node (; val= 2.0 )
119
- @test repr (tree) == " (cos((0.1 + 0.2) + 0.2) + 2.0)"
120
- @test repr (combine_operators (tree, operators)) == " (cos(0.4 + 0.1) + 2.0)"
126
+ @test repr (tree) ≈ " (cos((0.1 + 0.2) + 0.2) + 2.0)"
127
+ @test repr (combine_operators (tree, operators)) ≈ " (cos(0.4 + 0.1) + 2.0)"
121
128
122
129
# left is constant:
123
130
tree = Node (; val= 0.5 ) + (Node (; val= 0.2 ) + x1)
124
- @test repr (tree) == " (0.5 + (0.2 + x1))"
125
- @test repr (combine_operators (tree, operators)) == " (x1 + 0.7)"
131
+ @test repr (tree) ≈ " (0.5 + (0.2 + x1))"
132
+ @test repr (combine_operators (tree, operators)) ≈ " (x1 + 0.7)"
126
133
127
134
# (const - (const - var)) => (var - const)
128
135
tree = Node (2 , Node (; val= 0.5 ), Node (; val= 0.2 ) - x1)
129
- @test repr (tree) == " (0.5 - (0.2 - x1))"
130
- @test repr (combine_operators (tree, operators)) == " (x1 - -0.3)"
136
+ @test repr (tree) ≈ " (0.5 - (0.2 - x1))"
137
+ @test repr (combine_operators (tree, operators)) ≈ " (x1 - -0.3)"
131
138
132
139
# ((const - var) - const) => (const - var)
133
140
tree = Node (2 , Node (; val= 0.5 ) - x1, Node (; val= 0.2 ))
134
- @test repr (tree) == " ((0.5 - x1) - 0.2)"
135
- @test repr (combine_operators (tree, operators)) == " (0.3 - x1)"
141
+ @test repr (tree) ≈ " ((0.5 - x1) - 0.2)"
142
+ @test repr (combine_operators (tree, operators)) ≈ " (0.3 - x1)"
136
143
137
144
# (const - (var - const)) => (const - var)
138
145
tree = Node (2 , Node (; val= 0.5 ), x1 - Node (; val= 0.2 ))
139
- @test repr (tree) == " (0.5 - (x1 - 0.2))"
140
- @test repr (combine_operators (tree, operators)) == " (0.7 - x1)"
146
+ @test repr (tree) ≈ " (0.5 - (x1 - 0.2))"
147
+ @test repr (combine_operators (tree, operators)) ≈ " (0.7 - x1)"
141
148
142
149
# ((var - const) - const) => (var - const)
143
150
tree = ((x1 - 0.2 ) - 0.6 )
144
- @test repr (tree) == " ((x1 - 0.2) - 0.6)"
145
- @test repr (combine_operators (tree, operators)) == " (x1 - 0.8)"
151
+ @test repr (tree) ≈ " ((x1 - 0.2) - 0.6)"
152
+ @test repr (combine_operators (tree, operators)) ≈ " (x1 - 0.8)"
146
153
# ##############################################################################
0 commit comments