@@ -2,6 +2,13 @@ include("test_params.jl")
22using DynamicExpressions, Test
33import SymbolicUtils: simplify, Symbolic
44import 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
512
613simplify_tree = DynamicExpressions. SimplifyEquationModule. simplify_tree
714combine_operators = DynamicExpressions. SimplifyEquationModule. combine_operators
@@ -33,10 +40,10 @@ tree = convert(Node, eqn2, operators)
3340# that SymbolicUtils does not convert it to a power:
3441tree = Node (" x1" ) * Node (" x1" )
3542eqn = convert (Symbolic, tree, operators)
36- @test repr (eqn) == " x1*x1"
43+ @test repr (eqn) ≈ " x1*x1"
3744# Test converting back:
3845tree_copy = convert (Node, eqn, operators)
39- @test repr (tree_copy) == " (x1 * x1)"
46+ @test repr (tree_copy) ≈ " (x1* x1)"
4047
4148# Let's test a much more complex function,
4249# with custom operators, and unary operators:
@@ -105,42 +112,42 @@ x1, x2, x3 = [Node(; feature=i) for i in 1:3]
105112
106113# unary operator applied to constant => constant:
107114tree = 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"
110117
111118# except when the result is a NaN, then we don't change it:
112119tree = 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)"
115122
116123# the same as above, but inside a binary tree.
117124tree =
118125 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)"
121128
122129# left is constant:
123130tree = 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)"
126133
127134# (const - (const - var)) => (var - const)
128135tree = 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)"
131138
132139# ((const - var) - const) => (const - var)
133140tree = 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)"
136143
137144# (const - (var - const)) => (const - var)
138145tree = 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)"
141148
142149# ((var - const) - const) => (var - const)
143150tree = ((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)"
146153# ##############################################################################
0 commit comments