1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 | using System;
|
---|
22 | using System.Globalization;
|
---|
23 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
24 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tests {
|
---|
27 |
|
---|
28 | [TestClass()]
|
---|
29 | public class SymbolicDataAnalysisExpressionTreeSimplifierTest {
|
---|
30 |
|
---|
31 | [TestMethod]
|
---|
32 | [TestCategory("Problems.DataAnalysis")]
|
---|
33 | [TestProperty("Time", "short")]
|
---|
34 | public void SimplifierAxiomsTest() {
|
---|
35 | SymbolicExpressionImporter importer = new SymbolicExpressionImporter();
|
---|
36 | SymbolicExpressionTreeStringFormatter formatter = new SymbolicExpressionTreeStringFormatter();
|
---|
37 | #region single argument arithmetics
|
---|
38 |
|
---|
39 | AssertEqualAfterSimplification("(+ 1.0)", "1.0");
|
---|
40 | AssertEqualAfterSimplification("(- 1.0)", "-1.0");
|
---|
41 | AssertEqualAfterSimplification("(- (variable 2.0 a))", "(variable -2.0 a)");
|
---|
42 | AssertEqualAfterSimplification("(* 2.0)", "2.0");
|
---|
43 | AssertEqualAfterSimplification("(* (variable 2.0 a))", "(variable 2.0 a)");
|
---|
44 | AssertEqualAfterSimplification("(/ 2.0)", "0.5");
|
---|
45 | AssertEqualAfterSimplification("(/ (variable 2.0 a))", "(/ 1.0 (variable 2.0 a))");
|
---|
46 | #endregion
|
---|
47 |
|
---|
48 | #region aggregation of constants into factors
|
---|
49 | AssertEqualAfterSimplification("(* 2.0 (variable 2.0 a))", "(variable 4.0 a)");
|
---|
50 | AssertEqualAfterSimplification("(/ (variable 2.0 a) 2.0)", "(variable 1.0 a)");
|
---|
51 | AssertEqualAfterSimplification("(/ (variable 2.0 a) (* 2.0 2.0))", "(variable 0.5 a)");
|
---|
52 | #endregion
|
---|
53 |
|
---|
54 | #region constant and variable folding
|
---|
55 | AssertEqualAfterSimplification("(+ 1.0 2.0)", "3.0");
|
---|
56 | AssertEqualAfterSimplification("(+ (variable 2.0 a) (variable 2.0 a))", "(variable 4.0 a)");
|
---|
57 | AssertEqualAfterSimplification("(- (variable 2.0 a) (variable 1.0 a))", "(variable 1.0 a)");
|
---|
58 | AssertEqualAfterSimplification("(* (variable 2.0 a) (variable 2.0 a))", "(* (* (variable 1.0 a) (variable 1.0 a)) 4.0)");
|
---|
59 | AssertEqualAfterSimplification("(/ (variable 1.0 a) (variable 2.0 a))", "0.5");
|
---|
60 | #endregion
|
---|
61 |
|
---|
62 | #region logarithm rules
|
---|
63 |
|
---|
64 | // cancellation
|
---|
65 | AssertEqualAfterSimplification("(log (exp (variable 2.0 a)))", "(variable 2.0 a)");
|
---|
66 | // must not transform logs in this way as we do not know wether both variables are positive
|
---|
67 | AssertEqualAfterSimplification("(log (* (variable 1.0 a) (variable 1.0 b)))", "(log (* (variable 1.0 a) (variable 1.0 b)))");
|
---|
68 | // must not transform logs in this way as we do not know wether both variables are positive
|
---|
69 | AssertEqualAfterSimplification("(log (/ (variable 1.0 a) (variable 1.0 b)))", "(log (/ (variable 1.0 a) (variable 1.0 b)))");
|
---|
70 | #endregion
|
---|
71 |
|
---|
72 | #region exponentiation rules
|
---|
73 | // cancellation
|
---|
74 | AssertEqualAfterSimplification("(exp (log (variable 2.0 a)))", "(variable 2.0 a)");
|
---|
75 | // exp transformation
|
---|
76 | AssertEqualAfterSimplification("(exp (+ (variable 2.0 a) (variable 3.0 b)))", "(* (exp (variable 2.0 a)) (exp (variable 3.0 b)))");
|
---|
77 | // exp transformation
|
---|
78 | AssertEqualAfterSimplification("(exp (- (variable 2.0 a) (variable 3.0 b)))", "(* (exp (variable 2.0 a)) (exp (variable -3.0 b)))");
|
---|
79 | // exp transformation
|
---|
80 | AssertEqualAfterSimplification("(exp (- (variable 2.0 a) (* (variable 3.0 b) (variable 4.0 c))))", "(* (exp (variable 2.0 a)) (exp (* (variable 1.0 b) (variable 1.0 c) -12.0)))");
|
---|
81 | // exp transformation
|
---|
82 | AssertEqualAfterSimplification("(exp (- (variable 2.0 a) (* (variable 3.0 b) (cos (variable 4.0 c)))))", "(* (exp (variable 2.0 a)) (exp (* (variable 1.0 b) (cos (variable 4.0 c)) -3.0)))");
|
---|
83 | #endregion
|
---|
84 |
|
---|
85 | #region power rules
|
---|
86 |
|
---|
87 | // cancellation
|
---|
88 | AssertEqualAfterSimplification("(pow (variable 2.0 a) 0.0)", "1.0");
|
---|
89 | // fixed point
|
---|
90 | AssertEqualAfterSimplification("(pow (variable 2.0 a) 1.0)", "(variable 2.0 a)");
|
---|
91 | // inversion fixed point
|
---|
92 | AssertEqualAfterSimplification("(pow (variable 2.0 a) -1.0)", "(/ 1.0 (variable 2.0 a))");
|
---|
93 | // inversion
|
---|
94 | AssertEqualAfterSimplification("(pow (variable 2.0 a) -2.0)", "(/ 1.0 (pow (variable 2.0 a) 2.0))");
|
---|
95 | // constant folding
|
---|
96 | AssertEqualAfterSimplification("(pow 3.0 2.0)", "9.0");
|
---|
97 | #endregion
|
---|
98 |
|
---|
99 | #region root rules
|
---|
100 | // cancellation
|
---|
101 | AssertEqualAfterSimplification("(root (variable 2.0 a) 0.0)", "NaN");
|
---|
102 | // fixed point
|
---|
103 | AssertEqualAfterSimplification("(root (variable 2.0 a) 1.0)", "(variable 2.0 a)");
|
---|
104 | // inversion fixed point
|
---|
105 | AssertEqualAfterSimplification("(root (variable 2.0 a) -1.0)", "(/ 1.0 (variable 2.0 a))");
|
---|
106 | // inversion
|
---|
107 | AssertEqualAfterSimplification("(root (variable 2.0 a) -2.0)", "(/ 1.0 (root (variable 2.0 a) 2.0))");
|
---|
108 | // constant folding
|
---|
109 | AssertEqualAfterSimplification("(root 9.0 2.0)", "3.0");
|
---|
110 | #endregion
|
---|
111 |
|
---|
112 | #region boolean operations
|
---|
113 | // always true and
|
---|
114 | AssertEqualAfterSimplification("(and 1.0 2.0)", "1.0");
|
---|
115 | // always false and
|
---|
116 | AssertEqualAfterSimplification("(and 1.0 -2.0)", "-1.0");
|
---|
117 | // always true or
|
---|
118 | AssertEqualAfterSimplification("(or -1.0 2.0)", "1.0");
|
---|
119 | // always false or
|
---|
120 | AssertEqualAfterSimplification("(or -1.0 -2.0)", "-1.0");
|
---|
121 | // constant not
|
---|
122 | AssertEqualAfterSimplification("(not -2.0)", "1.0");
|
---|
123 | // constant not
|
---|
124 | AssertEqualAfterSimplification("(not 2.0)", "-1.0");
|
---|
125 | // constant not
|
---|
126 | AssertEqualAfterSimplification("(not 0.0)", "1.0");
|
---|
127 | // nested nots
|
---|
128 | AssertEqualAfterSimplification("(not (not 1.0))", "1.0");
|
---|
129 | // not of non-Boolean argument
|
---|
130 | AssertEqualAfterSimplification("(not (variable 1.0 a))", "(not (> (variable 1.0 a) 0.0))");
|
---|
131 | // not Boolean argument
|
---|
132 | AssertEqualAfterSimplification("(not (and (> (variable 1.0 a) 0.0) (> (variable 1.0 a) 0.0)))", "(not (and (> (variable 1.0 a) 0.0) (> (variable 1.0 a) 0.0)))");
|
---|
133 | #endregion
|
---|
134 |
|
---|
135 | #region conditionals
|
---|
136 | // always false
|
---|
137 | AssertEqualAfterSimplification("(if -1.0 (variable 2.0 a) (variable 3.0 a))", "(variable 3.0 a)");
|
---|
138 | // always true
|
---|
139 | AssertEqualAfterSimplification("(if 1.0 (variable 2.0 a) (variable 3.0 a))", "(variable 2.0 a)");
|
---|
140 | // always false (0.0)
|
---|
141 | AssertEqualAfterSimplification("(if 0.0 (variable 2.0 a) (variable 3.0 a))", "(variable 3.0 a)");
|
---|
142 | // complex constant condition (always false)
|
---|
143 | AssertEqualAfterSimplification("(if (* 1.0 -2.0) (variable 2.0 a) (variable 3.0 a))", "(variable 3.0 a)");
|
---|
144 | // complex constant condition (always false)
|
---|
145 | AssertEqualAfterSimplification("(if (/ (variable 1.0 a) (variable -2.0 a)) (variable 2.0 a) (variable 3.0 a))", "(variable 3.0 a)");
|
---|
146 | // insertion of relational operator
|
---|
147 | AssertEqualAfterSimplification("(if (variable 1.0 a) (variable 2.0 a) (variable 3.0 a))", "(if (> (variable 1.0 a) 0.0) (variable 2.0 a) (variable 3.0 a))");
|
---|
148 | #endregion
|
---|
149 |
|
---|
150 | #region factor variables
|
---|
151 | AssertEqualAfterSimplification("(factor a 1.0)", "(factor a 1.0)");
|
---|
152 | // factor folding
|
---|
153 | AssertEqualAfterSimplification("(+ (factor a 1.0 1.0) (factor a 2.0 3.0))", "(factor a 3.0 4.0)");
|
---|
154 | AssertEqualAfterSimplification("(- (factor a 1.0 1.0) (factor a 2.0 3.0))", "(factor a -1.0 -2.0)");
|
---|
155 | AssertEqualAfterSimplification("(* (factor a 2.0 2.0) (factor a 2.0 3.0))", "(factor a 4.0 6.0)");
|
---|
156 | AssertEqualAfterSimplification("(/ (factor a 2.0 5.0))", "(factor a 0.5 0.2)");
|
---|
157 | AssertEqualAfterSimplification("(/ (factor a 4.0 6.0) (factor a 2.0 3.0))", "(factor a 2.0 2.0)");
|
---|
158 | AssertEqualAfterSimplification("(+ 3.0 (factor a 4.0 6.0))", "(factor a 7.0 9.0)");
|
---|
159 | AssertEqualAfterSimplification("(+ (factor a 4.0 6.0) 3.0)", "(factor a 7.0 9.0)");
|
---|
160 | AssertEqualAfterSimplification("(- 3.0 (factor a 4.0 6.0))", "(factor a -1.0 -3.0)");
|
---|
161 | AssertEqualAfterSimplification("(- (factor a 4.0 6.0) 3.0)", "(factor a 1.0 3.0)");
|
---|
162 | AssertEqualAfterSimplification("(* 2.0 (factor a 4.0 6.0))", "(factor a 8.0 12.0)");
|
---|
163 | AssertEqualAfterSimplification("(* (factor a 4.0 6.0) 2.0)", "(factor a 8.0 12.0)");
|
---|
164 | AssertEqualAfterSimplification("(* (factor a 4.0 6.0) (variable 2.0 a))", "(* (factor a 8.0 12.0) (variable 1.0 a))"); // not possible (a is used as factor and double variable) interpreter will fail
|
---|
165 | AssertEqualAfterSimplification(
|
---|
166 | "(log (factor a 10.0 100.0))",
|
---|
167 | string.Format(CultureInfo.InvariantCulture, "(factor a {0} {1})", Math.Log(10.0), Math.Log(100.0)));
|
---|
168 | AssertEqualAfterSimplification(
|
---|
169 | "(exp (factor a 2.0 3.0))",
|
---|
170 | string.Format(CultureInfo.InvariantCulture, "(factor a {0} {1})", Math.Exp(2.0), Math.Exp(3.0)));
|
---|
171 | AssertEqualAfterSimplification("(sqrt (factor a 9.0 16.0))", "(factor a 3.0 4.0))");
|
---|
172 | AssertEqualAfterSimplification("(sqr (factor a 2.0 3.0))", "(factor a 4.0 9.0))");
|
---|
173 | AssertEqualAfterSimplification("(root (factor a 8.0 27.0) 3)", "(factor a 2.0 3.0))");
|
---|
174 | AssertEqualAfterSimplification("(pow (factor a 2.0 3.0) 3)", "(factor a 8.0 27.0))");
|
---|
175 |
|
---|
176 | AssertEqualAfterSimplification("(sin (factor a 1.0 2.0) )",
|
---|
177 | string.Format(CultureInfo.InvariantCulture, "(factor a {0} {1}))", Math.Sin(1.0), Math.Sin(2.0)));
|
---|
178 | AssertEqualAfterSimplification("(cos (factor a 1.0 2.0) )",
|
---|
179 | string.Format(CultureInfo.InvariantCulture, "(factor a {0} {1}))", Math.Cos(1.0), Math.Cos(2.0)));
|
---|
180 | AssertEqualAfterSimplification("(tan (factor a 1.0 2.0) )",
|
---|
181 | string.Format(CultureInfo.InvariantCulture, "(factor a {0} {1}))", Math.Tan(1.0), Math.Tan(2.0)));
|
---|
182 |
|
---|
183 |
|
---|
184 | AssertEqualAfterSimplification("(binfactor a val 1.0)", "(binfactor a val 1.0)");
|
---|
185 | // binfactor folding
|
---|
186 | AssertEqualAfterSimplification("(+ (binfactor a val 1.0) (binfactor a val 2.0))", "(binfactor a val 3.0)");
|
---|
187 | AssertEqualAfterSimplification("(+ (binfactor a val0 1.0) (binfactor a val1 2.0))", "(+ (binfactor a val0 1.0) (binfactor a val1 2.0))"); // cannot be simplified (different vals)
|
---|
188 | AssertEqualAfterSimplification("(+ (binfactor a val 1.0) (binfactor b val 2.0))", "(+ (binfactor a val 1.0) (binfactor b val 2.0))"); // cannot be simplified (different vars)
|
---|
189 | AssertEqualAfterSimplification("(- (binfactor a val 1.0) (binfactor a val 2.0))", "(binfactor a val -1.0)");
|
---|
190 | AssertEqualAfterSimplification("(* (binfactor a val 2.0) (binfactor a val 3.0))", "(binfactor a val 6.0)");
|
---|
191 | AssertEqualAfterSimplification("(/ (binfactor a val 6.0) (binfactor a val 3.0))", "(/ (binfactor a val 6.0) (binfactor a val 3.0))"); // not allowed! 0/0 for other values than 'val'
|
---|
192 | AssertEqualAfterSimplification("(/ (binfactor a val 4.0))", "(/ 1.0 (binfactor a val 4.0))"); // not allowed!
|
---|
193 |
|
---|
194 | AssertEqualAfterSimplification("(+ 3.0 (binfactor a val 4.0 ))", "(+ (binfactor a val 4.0 ) 3.0))"); // not allowed
|
---|
195 | AssertEqualAfterSimplification("(- 3.0 (binfactor a val 4.0 ))", "(+ (binfactor a val -4.0 ) 3.0)");
|
---|
196 | AssertEqualAfterSimplification("(+ (binfactor a val 4.0 ) 3.0)", "(+ (binfactor a val 4.0 ) 3.0)"); // not allowed
|
---|
197 | AssertEqualAfterSimplification("(- (binfactor a val 4.0 ) 3.0)", "(+ (binfactor a val 4.0 ) -3.0)");
|
---|
198 | AssertEqualAfterSimplification("(* 2.0 (binfactor a val 4.0))", "(binfactor a val 8.0 )");
|
---|
199 | AssertEqualAfterSimplification("(* (binfactor a val 4.0) 2.0)", "(binfactor a val 8.0 )");
|
---|
200 | AssertEqualAfterSimplification("(* (binfactor a val 4.0) (variable 2.0 a))", "(* (binfactor a val 1.0) (variable 1.0 a) 8.0)"); // not possible (a is used as factor and double variable) interpreter will fail
|
---|
201 | AssertEqualAfterSimplification("(log (binfactor a val 10.0))", "(log (binfactor a val 10.0))"); // not allowed (log(0))
|
---|
202 |
|
---|
203 | // exp( binfactor w val=a) = if(val=a) exp(w) else exp(0) = binfactor( (exp(w) - 1) val a) + 1
|
---|
204 | AssertEqualAfterSimplification("(exp (binfactor a val 3.0))",
|
---|
205 | string.Format(CultureInfo.InvariantCulture, "(+ (binfactor a val {0}) 1.0)", Math.Exp(3.0) - 1)
|
---|
206 | );
|
---|
207 | AssertEqualAfterSimplification("(sqrt (binfactor a val 16.0))", "(binfactor a val 4.0))"); // sqrt(0) = 0
|
---|
208 | AssertEqualAfterSimplification("(sqr (binfactor a val 3.0))", "(binfactor a val 9.0))"); // 0*0 = 0
|
---|
209 | AssertEqualAfterSimplification("(root (binfactor a val 27.0) 3)", "(binfactor a val 3.0))");
|
---|
210 | AssertEqualAfterSimplification("(pow (binfactor a val 3.0) 3)", "(binfactor a val 27.0))");
|
---|
211 |
|
---|
212 | AssertEqualAfterSimplification("(sin (binfactor a val 2.0) )",
|
---|
213 | string.Format(CultureInfo.InvariantCulture, "(binfactor a val {0}))", Math.Sin(2.0))); // sin(0) = 0
|
---|
214 | AssertEqualAfterSimplification("(cos (binfactor a val 2.0) )",
|
---|
215 | string.Format(CultureInfo.InvariantCulture, "(+ (binfactor a val {0}) 1.0)", Math.Cos(2.0) - 1)); // cos(0) = 1
|
---|
216 | AssertEqualAfterSimplification("(tan (binfactor a val 2.0) )",
|
---|
217 | string.Format(CultureInfo.InvariantCulture, "(binfactor a val {0}))", Math.Tan(2.0))); // tan(0) = 0
|
---|
218 |
|
---|
219 | // combination of factor and binfactor
|
---|
220 | AssertEqualAfterSimplification("(+ (binfactor a x0 2.0) (factor a 2.0 3.0))", "(factor a 4.0 3.0)");
|
---|
221 | AssertEqualAfterSimplification("(+ (factor a 2.0 3.0) (binfactor a x0 2.0))", "(factor a 4.0 3.0)");
|
---|
222 | AssertEqualAfterSimplification("(* (binfactor a x1 2.0) (factor a 2.0 3.0))", "(binfactor a x1 6.0)"); // all other values have weight zero in binfactor
|
---|
223 | AssertEqualAfterSimplification("(* (factor a 2.0 3.0) (binfactor a x1 2.0))", "(binfactor a x1 6.0)"); // all other values have weight zero in binfactor
|
---|
224 | AssertEqualAfterSimplification("(/ (binfactor a x0 2.0) (factor a 2.0 3.0))", "(binfactor a x0 1.0)");
|
---|
225 | AssertEqualAfterSimplification("(/ (factor a 2.0 3.0) (binfactor a x0 2.0))",
|
---|
226 | string.Format(CultureInfo.InvariantCulture, "(factor a 1.0 {0})", 3.0 / 0.0));
|
---|
227 | AssertEqualAfterSimplification("(- (binfactor a x0 2.0) (factor a 2.0 3.0))", "(factor a 0.0 -3.0)");
|
---|
228 | AssertEqualAfterSimplification("(- (factor a 2.0 3.0) (binfactor a x0 2.0))", "(factor a 0.0 3.0)");
|
---|
229 | #endregion
|
---|
230 |
|
---|
231 | #region abs
|
---|
232 | AssertEqualAfterSimplification("(abs 2.0)", "2.0");
|
---|
233 | AssertEqualAfterSimplification("(abs -2.0)", "2.0"); // constant folding
|
---|
234 | AssertEqualAfterSimplification("(abs (exp (variable 2.0 x)))", "(exp (variable 2.0 x)))"); // exp is always positive
|
---|
235 | AssertEqualAfterSimplification("(abs (exp (variable 2.0 x)))", "(exp (variable 2.0 x)))"); // exp is always positive
|
---|
236 | AssertEqualAfterSimplification("(abs (sqr (variable 2.0 a)))", "(sqr (variable 2.0 a))"); // sqr is always positive
|
---|
237 | AssertEqualAfterSimplification("(abs (sqrt (variable 2.0 a)))", "(sqrt (variable 2.0 a))"); // sqrt is always positive (for our cases)
|
---|
238 | AssertEqualAfterSimplification("(abs (cuberoot (variable 2.0 a)))", "(cuberoot (variable 2.0 a))"); // cuberoot is always positive (for our cases)
|
---|
239 |
|
---|
240 | AssertEqualAfterSimplification("(* (abs (variable 2.0 x)) 2.0)", "(abs (variable 4.0 x))"); // can multiply positive constants into abs
|
---|
241 | AssertEqualAfterSimplification("(* (abs (variable 2.0 x)) -2.0)", "(* (abs (variable 4.0 x)) -1.0)"); // for negative constants keep the sign
|
---|
242 |
|
---|
243 | AssertEqualAfterSimplification("(abs (* (variable 1.0 a) (variable 2.0 b)))", "(* (abs (variable 1.0 a)) (abs (variable 1.0 b)) 2.0))");
|
---|
244 | AssertEqualAfterSimplification("(abs (/ (variable 1.0 a) (variable 2.0 b)))", "(/ (abs (variable 1.0 a)) (abs (variable 2.0 b))))");
|
---|
245 | #endregion
|
---|
246 |
|
---|
247 | #region square and sqrt
|
---|
248 | AssertEqualAfterSimplification("(sqr (sqrt (variable 2.0 x)))", "(variable 2.0 x)");
|
---|
249 | AssertEqualAfterSimplification("(sqrt (sqr (variable 2.0 x)))", "(variable 2.0 x)");
|
---|
250 | AssertEqualAfterSimplification("(sqr (abs (variable 2.0 a)))", "(sqr (variable 2.0 a))");
|
---|
251 | AssertEqualAfterSimplification("(sqr (exp (variable 2.0 x)))", "(exp (variable 4.0 x))");
|
---|
252 |
|
---|
253 | AssertEqualAfterSimplification("(sqr (* (variable 2.0 a) (variable 3.0 b) (variable 4.0 c)))",
|
---|
254 | "(* (sqr (variable 1.0 a)) (sqr (variable 1.0 b)) (sqr (variable 1.0 c)) 576)"); // 2²*3²*4²
|
---|
255 | #endregion
|
---|
256 |
|
---|
257 | #region cube and cuberoot
|
---|
258 | AssertEqualAfterSimplification("(cube (cuberoot (variable 2.0 x)))", "(variable 2.0 x)");
|
---|
259 | AssertEqualAfterSimplification("(cuberoot (cube (variable 2.0 x)))", "(variable 2.0 x)");
|
---|
260 | AssertEqualAfterSimplification("(cube (exp (variable 2.0 x)))", "(exp (variable 6.0 x))");
|
---|
261 |
|
---|
262 | AssertEqualAfterSimplification("(sqr (cube (variable 2.0 x)))", "(pow (variable 2.0 x) 6)");
|
---|
263 | AssertEqualAfterSimplification("(cube (sqr (variable 2.0 x)))", "(pow (variable 2.0 x) 6)");
|
---|
264 | #endregion
|
---|
265 |
|
---|
266 | #region AQ
|
---|
267 | AssertEqualAfterSimplification("(* (aq (variable 1.0 x) (variable 1.0 y)) 2.0)", "(aq (variable 2.0 x) (variable 1.0 y))");
|
---|
268 | AssertEqualAfterSimplification("(/ (aq (variable 1.0 x) (variable 1.0 y)) 2.0)", "(aq (variable 0.5 x) (variable 1.0 y))");
|
---|
269 |
|
---|
270 | #endregion
|
---|
271 |
|
---|
272 | #region do not drop subtrees with small weights
|
---|
273 | AssertEqualAfterSimplification("(* 1e-14 (variable 1.0 a))", "(variable 1e-14 a)");
|
---|
274 | AssertEqualAfterSimplification("(+ (variable 1.0 a) 1e-14)",
|
---|
275 | "(+ (variable 1.0 a) 1e-14)");
|
---|
276 | // a scenario where a term with small weight can have large effect
|
---|
277 | AssertEqualAfterSimplification("(+ (* (pow (variable 1.0 a) 10) 1e-14) 1.0)",
|
---|
278 | "(+ (* (pow (variable 1.0 a) 10) 1e-14) 1.0)");
|
---|
279 | // a test case (from ticket #2985)
|
---|
280 | AssertEqualAfterSimplification("(+ (* (exp (variable 3.5861E+001 a)) 5.5606E-016) 5.9323E-002)",
|
---|
281 | "(+ (* (exp (variable 3.5861E+001 a)) 5.5606E-016) 5.9323E-002)");
|
---|
282 | #endregion
|
---|
283 |
|
---|
284 | #region vectors
|
---|
285 | AssertEqualAfterVectorSimplification("remove aggregation for scalars from Sum",
|
---|
286 | "SUM(a)",
|
---|
287 | "a");
|
---|
288 | AssertEqualAfterVectorSimplification("factor out multiplicative scalar from Sum",
|
---|
289 | "SUM(a{} * 2.0)",
|
---|
290 | "SUM(a{}) * 2.0");
|
---|
291 | AssertEqualAfterVectorSimplification("factor out additive scalar from Sum",
|
---|
292 | "SUM(a{} + 2.0)",
|
---|
293 | "LENGTH(a{}) * 2.0 + SUM(a{})");
|
---|
294 |
|
---|
295 | AssertEqualAfterVectorSimplification("remove aggregation for scalars from Mean",
|
---|
296 | "MEAN(a)",
|
---|
297 | "a");
|
---|
298 | AssertEqualAfterVectorSimplification("factor out multiplicative scalar from Mean",
|
---|
299 | "MEAN(2.0 * a{})",
|
---|
300 | "MEAN(a{}) * 2.0");
|
---|
301 | AssertEqualAfterVectorSimplification("factor out additive scalar from Mean",
|
---|
302 | "MEAN(a{} + 2.0)",
|
---|
303 | "MEAN(a{}) + 2.0");
|
---|
304 |
|
---|
305 | AssertEqualAfterVectorSimplification("remove aggregation for scalars from Length",
|
---|
306 | "LENGTH(a)",
|
---|
307 | "1.0");
|
---|
308 | AssertEqualAfterVectorSimplification("factor out multiplicative scalar from Length",
|
---|
309 | "LENGTH(a{} * 2.0)",
|
---|
310 | "LENGTH(a{})");
|
---|
311 | AssertEqualAfterVectorSimplification("factor out additive scalar from Length",
|
---|
312 | "LENGTH(a{} + 2.0)",
|
---|
313 | "LENGTH(a{})");
|
---|
314 |
|
---|
315 | AssertEqualAfterVectorSimplification("remove aggregation for scalars from StandardDeviation",
|
---|
316 | "STDEV(a)",
|
---|
317 | "0.0");
|
---|
318 | AssertEqualAfterVectorSimplification("factor out multiplicative scalar from StandardDeviation",
|
---|
319 | "STDEV(2.0 * a{})",
|
---|
320 | "STDEV(a{}) * 2.0");
|
---|
321 | AssertEqualAfterVectorSimplification("factor out additive scalar from StandardDeviation",
|
---|
322 | "STDEV(a{} + 2.0)",
|
---|
323 | "STDEV(a{})");
|
---|
324 |
|
---|
325 | AssertEqualAfterVectorSimplification("remove aggregation for scalars from Variance",
|
---|
326 | "VAR(a)",
|
---|
327 | "0.0");
|
---|
328 | AssertEqualAfterVectorSimplification("factor out multiplicative scalar from Variance",
|
---|
329 | "VAR(a{} * 2.0)",
|
---|
330 | "VAR(a{}) * 4.0");
|
---|
331 | AssertEqualAfterVectorSimplification("factor out additive scalar from Variance",
|
---|
332 | "VAR(a{} + 2.0)",
|
---|
333 | "VAR(a{})");
|
---|
334 | #endregion
|
---|
335 | }
|
---|
336 |
|
---|
337 |
|
---|
338 | private void AssertEqualAfterSimplification(string original, string expected) {
|
---|
339 | var formatter = new SymbolicExpressionTreeStringFormatter();
|
---|
340 | var importer = new SymbolicExpressionImporter();
|
---|
341 | var actualTree = TreeSimplifier.Simplify(importer.Import(original));
|
---|
342 | var expectedTree = importer.Import(expected);
|
---|
343 | Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
|
---|
344 | }
|
---|
345 |
|
---|
346 | private void AssertEqualAfterVectorSimplification(string msg, string original, string expected) {
|
---|
347 | var formatter = new InfixExpressionFormatter();
|
---|
348 | var parser = new InfixExpressionParser();
|
---|
349 | var simplifier = new VectorTreeSimplifier(new SymbolicDataAnalysisExpressionTreeVectorInterpreter());
|
---|
350 | var actualTree = simplifier.Simplify(parser.Parse(original));
|
---|
351 | var expectedTree = parser.Parse(expected);
|
---|
352 | Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree), msg);
|
---|
353 | }
|
---|
354 | }
|
---|
355 | }
|
---|
356 |
|
---|