1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Globalization;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
27 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
28 | using HeuristicLab.Random;
|
---|
29 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
30 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic_34.Tests {
|
---|
31 |
|
---|
32 |
|
---|
33 | [TestClass()]
|
---|
34 | public class SymbolicDataAnalysisExpressionTreeInterpreterTest {
|
---|
35 | private const int N = 1000;
|
---|
36 | private const int Rows = 1000;
|
---|
37 | private const int Columns = 50;
|
---|
38 | private TestContext testContextInstance;
|
---|
39 |
|
---|
40 | /// <summary>
|
---|
41 | ///Gets or sets the test context which provides
|
---|
42 | ///information about and functionality for the current test run.
|
---|
43 | ///</summary>
|
---|
44 | public TestContext TestContext {
|
---|
45 | get {
|
---|
46 | return testContextInstance;
|
---|
47 | }
|
---|
48 | set {
|
---|
49 | testContextInstance = value;
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | [TestMethod]
|
---|
54 | public void SymbolicDataAnalysisExpressionTreeInterpreterTypeCoherentGrammarPerformanceTest() {
|
---|
55 | TypeCoherentGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeInterpreter(), 12.5e6);
|
---|
56 | }
|
---|
57 | [TestMethod]
|
---|
58 | public void SymbolicDataAnalysisExpressionTreeInterpreterFullGrammarPerformanceTest() {
|
---|
59 | FullGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeInterpreter(), 12.5e6);
|
---|
60 | }
|
---|
61 | [TestMethod]
|
---|
62 | public void SymbolicDataAnalysisExpressionTreeInterpreterArithmeticGrammarPerformanceTest() {
|
---|
63 | ArithmeticGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeInterpreter(), 12.5e6);
|
---|
64 | }
|
---|
65 |
|
---|
66 | [TestMethod]
|
---|
67 | public void SymbolicDataAnalysisExpressionTreeILEmittingInterpreterTypeCoherentGrammarPerformanceTest() {
|
---|
68 | TypeCoherentGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(), 7.5e6);
|
---|
69 | }
|
---|
70 | [TestMethod]
|
---|
71 | public void SymbolicDataAnalysisExpressionTreeILEmittingInterpreterFullGrammarPerformanceTest() {
|
---|
72 | FullGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(), 7.5e6);
|
---|
73 | }
|
---|
74 | [TestMethod]
|
---|
75 | public void SymbolicDataAnalysisExpressionTreeILEmittingInterpreterArithmeticGrammarPerformanceTest() {
|
---|
76 | ArithmeticGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(), 7.5e6);
|
---|
77 | }
|
---|
78 |
|
---|
79 | private void TypeCoherentGrammarPerformanceTest(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, double nodesPerSecThreshold) {
|
---|
80 | var twister = new MersenneTwister(31415);
|
---|
81 | var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
|
---|
82 | var grammar = new TypeCoherentExpressionGrammar();
|
---|
83 | grammar.ConfigureAsDefaultRegressionGrammar();
|
---|
84 | grammar.MaximumFunctionArguments = 0;
|
---|
85 | grammar.MaximumFunctionDefinitions = 0;
|
---|
86 | grammar.MinimumFunctionArguments = 0;
|
---|
87 | grammar.MinimumFunctionDefinitions = 0;
|
---|
88 | var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
|
---|
89 | foreach (ISymbolicExpressionTree tree in randomTrees) {
|
---|
90 | Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
|
---|
91 | }
|
---|
92 | double nodesPerSec = Util.CalculateEvaluatedNodesPerSec(randomTrees, interpreter, dataset, 3);
|
---|
93 | //mkommend: commented due to performance issues on the builder
|
---|
94 | // Assert.IsTrue(nodesPerSec > nodesPerSecThreshold); // evaluated nodes per seconds must be larger than 15mNodes/sec
|
---|
95 | }
|
---|
96 |
|
---|
97 | private void FullGrammarPerformanceTest(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, double nodesPerSecThreshold) {
|
---|
98 | var twister = new MersenneTwister(31415);
|
---|
99 | var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
|
---|
100 | var grammar = new FullFunctionalExpressionGrammar();
|
---|
101 | grammar.MaximumFunctionArguments = 0;
|
---|
102 | grammar.MaximumFunctionDefinitions = 0;
|
---|
103 | grammar.MinimumFunctionArguments = 0;
|
---|
104 | grammar.MinimumFunctionDefinitions = 0;
|
---|
105 | var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
|
---|
106 | foreach (ISymbolicExpressionTree tree in randomTrees) {
|
---|
107 | Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
|
---|
108 | }
|
---|
109 | double nodesPerSec = Util.CalculateEvaluatedNodesPerSec(randomTrees, interpreter, dataset, 3);
|
---|
110 | //mkommend: commented due to performance issues on the builder
|
---|
111 | //Assert.IsTrue(nodesPerSec > nodesPerSecThreshold); // evaluated nodes per seconds must be larger than 15mNodes/sec
|
---|
112 | }
|
---|
113 |
|
---|
114 | private void ArithmeticGrammarPerformanceTest(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, double nodesPerSecThreshold) {
|
---|
115 | var twister = new MersenneTwister(31415);
|
---|
116 | var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
|
---|
117 | var grammar = new ArithmeticExpressionGrammar();
|
---|
118 | grammar.MaximumFunctionArguments = 0;
|
---|
119 | grammar.MaximumFunctionDefinitions = 0;
|
---|
120 | grammar.MinimumFunctionArguments = 0;
|
---|
121 | grammar.MinimumFunctionDefinitions = 0;
|
---|
122 | var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
|
---|
123 | foreach (SymbolicExpressionTree tree in randomTrees) {
|
---|
124 | Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
|
---|
125 | }
|
---|
126 |
|
---|
127 | double nodesPerSec = Util.CalculateEvaluatedNodesPerSec(randomTrees, interpreter, dataset, 3);
|
---|
128 | //mkommend: commented due to performance issues on the builder
|
---|
129 | //Assert.IsTrue(nodesPerSec > nodesPerSecThreshold); // evaluated nodes per seconds must be larger than 15mNodes/sec
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | /// <summary>
|
---|
134 | ///A test for Evaluate
|
---|
135 | ///</summary>
|
---|
136 | [TestMethod]
|
---|
137 | public void SymbolicDataAnalysisExpressionTreeInterpreterEvaluateTest() {
|
---|
138 | Dataset ds = new Dataset(new string[] { "Y", "A", "B" }, new double[,] {
|
---|
139 | { 1.0, 1.0, 1.0 },
|
---|
140 | { 2.0, 2.0, 2.0 },
|
---|
141 | { 3.0, 1.0, 2.0 },
|
---|
142 | { 4.0, 1.0, 1.0 },
|
---|
143 | { 5.0, 2.0, 2.0 },
|
---|
144 | { 6.0, 1.0, 2.0 },
|
---|
145 | { 7.0, 1.0, 1.0 },
|
---|
146 | { 8.0, 2.0, 2.0 },
|
---|
147 | { 9.0, 1.0, 2.0 },
|
---|
148 | { 10.0, 1.0, 1.0 },
|
---|
149 | { 11.0, 2.0, 2.0 },
|
---|
150 | { 12.0, 1.0, 2.0 }
|
---|
151 | });
|
---|
152 |
|
---|
153 | var interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
|
---|
154 | EvaluateTerminals(interpreter, ds);
|
---|
155 | EvaluateOperations(interpreter, ds);
|
---|
156 | EvaluateAdf(interpreter, ds);
|
---|
157 | }
|
---|
158 |
|
---|
159 | [TestMethod]
|
---|
160 | public void SymbolicDataAnalysisExpressionILEmittingTreeInterpreterEvaluateTest() {
|
---|
161 | Dataset ds = new Dataset(new string[] { "Y", "A", "B" }, new double[,] {
|
---|
162 | { 1.0, 1.0, 1.0 },
|
---|
163 | { 2.0, 2.0, 2.0 },
|
---|
164 | { 3.0, 1.0, 2.0 },
|
---|
165 | { 4.0, 1.0, 1.0 },
|
---|
166 | { 5.0, 2.0, 2.0 },
|
---|
167 | { 6.0, 1.0, 2.0 },
|
---|
168 | { 7.0, 1.0, 1.0 },
|
---|
169 | { 8.0, 2.0, 2.0 },
|
---|
170 | { 9.0, 1.0, 2.0 },
|
---|
171 | { 10.0, 1.0, 1.0 },
|
---|
172 | { 11.0, 2.0, 2.0 },
|
---|
173 | { 12.0, 1.0, 2.0 }
|
---|
174 | });
|
---|
175 |
|
---|
176 | var interpreter = new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter();
|
---|
177 | EvaluateTerminals(interpreter, ds);
|
---|
178 | EvaluateOperations(interpreter, ds);
|
---|
179 | }
|
---|
180 |
|
---|
181 | private void EvaluateTerminals(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) {
|
---|
182 | // constants
|
---|
183 | Evaluate(interpreter, ds, "(+ 1.5 3.5)", 0, 5.0);
|
---|
184 |
|
---|
185 | // variables
|
---|
186 | Evaluate(interpreter, ds, "(variable 2.0 a)", 0, 2.0);
|
---|
187 | Evaluate(interpreter, ds, "(variable 2.0 a)", 1, 4.0);
|
---|
188 | }
|
---|
189 |
|
---|
190 | private void EvaluateAdf(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) {
|
---|
191 |
|
---|
192 | // ADF
|
---|
193 | Evaluate(interpreter, ds, @"(PROG
|
---|
194 | (MAIN
|
---|
195 | (CALL ADF0))
|
---|
196 | (defun ADF0 1.0))", 1, 1.0);
|
---|
197 | Evaluate(interpreter, ds, @"(PROG
|
---|
198 | (MAIN
|
---|
199 | (* (CALL ADF0) (CALL ADF0)))
|
---|
200 | (defun ADF0 2.0))", 1, 4.0);
|
---|
201 | Evaluate(interpreter, ds, @"(PROG
|
---|
202 | (MAIN
|
---|
203 | (CALL ADF0 2.0 3.0))
|
---|
204 | (defun ADF0
|
---|
205 | (+ (ARG 0) (ARG 1))))", 1, 5.0);
|
---|
206 | Evaluate(interpreter, ds, @"(PROG
|
---|
207 | (MAIN (CALL ADF1 2.0 3.0))
|
---|
208 | (defun ADF0
|
---|
209 | (- (ARG 1) (ARG 0)))
|
---|
210 | (defun ADF1
|
---|
211 | (+ (CALL ADF0 (ARG 1) (ARG 0))
|
---|
212 | (CALL ADF0 (ARG 0) (ARG 1)))))", 1, 0.0);
|
---|
213 | Evaluate(interpreter, ds, @"(PROG
|
---|
214 | (MAIN (CALL ADF1 (variable 2.0 a) 3.0))
|
---|
215 | (defun ADF0
|
---|
216 | (- (ARG 1) (ARG 0)))
|
---|
217 | (defun ADF1
|
---|
218 | (CALL ADF0 (ARG 1) (ARG 0))))", 1, 1.0);
|
---|
219 | Evaluate(interpreter, ds,
|
---|
220 | @"(PROG
|
---|
221 | (MAIN (CALL ADF1 (variable 2.0 a) 3.0))
|
---|
222 | (defun ADF0
|
---|
223 | (- (ARG 1) (ARG 0)))
|
---|
224 | (defun ADF1
|
---|
225 | (+ (CALL ADF0 (ARG 1) (ARG 0))
|
---|
226 | (CALL ADF0 (ARG 0) (ARG 1)))))", 1, 0.0);
|
---|
227 | }
|
---|
228 |
|
---|
229 | private void EvaluateOperations(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) {
|
---|
230 | // addition
|
---|
231 | Evaluate(interpreter, ds, "(+ (variable 2.0 a ))", 1, 4.0);
|
---|
232 | Evaluate(interpreter, ds, "(+ (variable 2.0 a ) (variable 3.0 b ))", 0, 5.0);
|
---|
233 | Evaluate(interpreter, ds, "(+ (variable 2.0 a ) (variable 3.0 b ))", 1, 10.0);
|
---|
234 | Evaluate(interpreter, ds, "(+ (variable 2.0 a) (variable 3.0 b ))", 2, 8.0);
|
---|
235 | Evaluate(interpreter, ds, "(+ 8.0 2.0 2.0)", 0, 12.0);
|
---|
236 |
|
---|
237 | // subtraction
|
---|
238 | Evaluate(interpreter, ds, "(- (variable 2.0 a ))", 1, -4.0);
|
---|
239 | Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b))", 0, -1.0);
|
---|
240 | Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b ))", 1, -2.0);
|
---|
241 | Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b ))", 2, -4.0);
|
---|
242 | Evaluate(interpreter, ds, "(- 8.0 2.0 2.0)", 0, 4.0);
|
---|
243 |
|
---|
244 | // multiplication
|
---|
245 | Evaluate(interpreter, ds, "(* (variable 2.0 a ))", 0, 2.0);
|
---|
246 | Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 0, 6.0);
|
---|
247 | Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 1, 24.0);
|
---|
248 | Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 2, 12.0);
|
---|
249 | Evaluate(interpreter, ds, "(* 8.0 2.0 2.0)", 0, 32.0);
|
---|
250 |
|
---|
251 | // division
|
---|
252 | Evaluate(interpreter, ds, "(/ (variable 2.0 a ))", 1, 1.0 / 4.0);
|
---|
253 | Evaluate(interpreter, ds, "(/ (variable 2.0 a ) 2.0)", 0, 1.0);
|
---|
254 | Evaluate(interpreter, ds, "(/ (variable 2.0 a ) 2.0)", 1, 2.0);
|
---|
255 | Evaluate(interpreter, ds, "(/ (variable 3.0 b ) 2.0)", 2, 3.0);
|
---|
256 | Evaluate(interpreter, ds, "(/ 8.0 2.0 2.0)", 0, 2.0);
|
---|
257 |
|
---|
258 | // gt
|
---|
259 | Evaluate(interpreter, ds, "(> (variable 2.0 a) 2.0)", 0, -1.0);
|
---|
260 | Evaluate(interpreter, ds, "(> 2.0 (variable 2.0 a))", 0, -1.0);
|
---|
261 | Evaluate(interpreter, ds, "(> (variable 2.0 a) 1.9)", 0, 1.0);
|
---|
262 | Evaluate(interpreter, ds, "(> 1.9 (variable 2.0 a))", 0, -1.0);
|
---|
263 | Evaluate(interpreter, ds, "(> (log -1.0) (log -1.0))", 0, -1.0); // (> nan nan) should be false
|
---|
264 |
|
---|
265 | // lt
|
---|
266 | Evaluate(interpreter, ds, "(< (variable 2.0 a) 2.0)", 0, -1.0);
|
---|
267 | Evaluate(interpreter, ds, "(< 2.0 (variable 2.0 a))", 0, -1.0);
|
---|
268 | Evaluate(interpreter, ds, "(< (variable 2.0 a) 1.9)", 0, -1.0);
|
---|
269 | Evaluate(interpreter, ds, "(< 1.9 (variable 2.0 a))", 0, 1.0);
|
---|
270 | Evaluate(interpreter, ds, "(< (log -1.0) (log -1.0))", 0, -1.0); // (< nan nan) should be false
|
---|
271 |
|
---|
272 | // If
|
---|
273 | Evaluate(interpreter, ds, "(if -10.0 2.0 3.0)", 0, 3.0);
|
---|
274 | Evaluate(interpreter, ds, "(if -1.0 2.0 3.0)", 0, 3.0);
|
---|
275 | Evaluate(interpreter, ds, "(if 0.0 2.0 3.0)", 0, 3.0);
|
---|
276 | Evaluate(interpreter, ds, "(if 1.0 2.0 3.0)", 0, 2.0);
|
---|
277 | Evaluate(interpreter, ds, "(if 10.0 2.0 3.0)", 0, 2.0);
|
---|
278 | Evaluate(interpreter, ds, "(if (log -1.0) 2.0 3.0)", 0, 3.0); // if(nan) should return the else branch
|
---|
279 |
|
---|
280 | // NOT
|
---|
281 | Evaluate(interpreter, ds, "(not -1.0)", 0, 1.0);
|
---|
282 | Evaluate(interpreter, ds, "(not -2.0)", 0, 1.0);
|
---|
283 | Evaluate(interpreter, ds, "(not 1.0)", 0, -1.0);
|
---|
284 | Evaluate(interpreter, ds, "(not 2.0)", 0, -1.0);
|
---|
285 | Evaluate(interpreter, ds, "(not 0.0)", 0, 1.0);
|
---|
286 | Evaluate(interpreter, ds, "(not (log -1.0))", 0, 1.0);
|
---|
287 |
|
---|
288 | // AND
|
---|
289 | Evaluate(interpreter, ds, "(and -1.0 -2.0)", 0, -1.0);
|
---|
290 | Evaluate(interpreter, ds, "(and -1.0 2.0)", 0, -1.0);
|
---|
291 | Evaluate(interpreter, ds, "(and 1.0 -2.0)", 0, -1.0);
|
---|
292 | Evaluate(interpreter, ds, "(and 1.0 0.0)", 0, -1.0);
|
---|
293 | Evaluate(interpreter, ds, "(and 0.0 0.0)", 0, -1.0);
|
---|
294 | Evaluate(interpreter, ds, "(and 1.0 2.0)", 0, 1.0);
|
---|
295 | Evaluate(interpreter, ds, "(and 1.0 2.0 3.0)", 0, 1.0);
|
---|
296 | Evaluate(interpreter, ds, "(and 1.0 -2.0 3.0)", 0, -1.0);
|
---|
297 | Evaluate(interpreter, ds, "(and (log -1.0))", 0, -1.0); // (and NaN)
|
---|
298 | Evaluate(interpreter, ds, "(and (log -1.0) 1.0)", 0, -1.0); // (and NaN 1.0)
|
---|
299 |
|
---|
300 |
|
---|
301 | // OR
|
---|
302 | Evaluate(interpreter, ds, "(or -1.0 -2.0)", 0, -1.0);
|
---|
303 | Evaluate(interpreter, ds, "(or -1.0 2.0)", 0, 1.0);
|
---|
304 | Evaluate(interpreter, ds, "(or 1.0 -2.0)", 0, 1.0);
|
---|
305 | Evaluate(interpreter, ds, "(or 1.0 2.0)", 0, 1.0);
|
---|
306 | Evaluate(interpreter, ds, "(or 0.0 0.0)", 0, -1.0);
|
---|
307 | Evaluate(interpreter, ds, "(or -1.0 -2.0 -3.0)", 0, -1.0);
|
---|
308 | Evaluate(interpreter, ds, "(or -1.0 -2.0 3.0)", 0, 1.0);
|
---|
309 | Evaluate(interpreter, ds, "(or (log -1.0))", 0, -1.0); // (or NaN)
|
---|
310 | Evaluate(interpreter, ds, "(or (log -1.0) 1.0)", 0, -1.0); // (or NaN 1.0)
|
---|
311 |
|
---|
312 | // sin, cos, tan
|
---|
313 | Evaluate(interpreter, ds, "(sin " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, 0.0);
|
---|
314 | Evaluate(interpreter, ds, "(sin 0.0)", 0, 0.0);
|
---|
315 | Evaluate(interpreter, ds, "(cos " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, -1.0);
|
---|
316 | Evaluate(interpreter, ds, "(cos 0.0)", 0, 1.0);
|
---|
317 | Evaluate(interpreter, ds, "(tan " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, Math.Tan(Math.PI));
|
---|
318 | Evaluate(interpreter, ds, "(tan 0.0)", 0, Math.Tan(Math.PI));
|
---|
319 |
|
---|
320 | // exp, log
|
---|
321 | Evaluate(interpreter, ds, "(log (exp 7.0))", 0, Math.Log(Math.Exp(7)));
|
---|
322 | Evaluate(interpreter, ds, "(exp (log 7.0))", 0, Math.Exp(Math.Log(7)));
|
---|
323 | Evaluate(interpreter, ds, "(log -3.0)", 0, Math.Log(-3));
|
---|
324 |
|
---|
325 | // power
|
---|
326 | Evaluate(interpreter, ds, "(pow 2.0 3.0)", 0, 8.0);
|
---|
327 | Evaluate(interpreter, ds, "(pow 4.0 0.5)", 0, 1.0); // interpreter should round to the nearest integer value value (.5 is rounded to the even number)
|
---|
328 | Evaluate(interpreter, ds, "(pow 4.0 2.5)", 0, 16.0); // interpreter should round to the nearest integer value value (.5 is rounded to the even number)
|
---|
329 | Evaluate(interpreter, ds, "(pow -2.0 3.0)", 0, -8.0);
|
---|
330 | Evaluate(interpreter, ds, "(pow 2.0 -3.0)", 0, 1.0 / 8.0);
|
---|
331 | Evaluate(interpreter, ds, "(pow -2.0 -3.0)", 0, -1.0 / 8.0);
|
---|
332 |
|
---|
333 | // root
|
---|
334 | Evaluate(interpreter, ds, "(root 9.0 2.0)", 0, 3.0);
|
---|
335 | Evaluate(interpreter, ds, "(root 27.0 3.0)", 0, 3.0);
|
---|
336 | Evaluate(interpreter, ds, "(root 2.0 -3.0)", 0, Math.Pow(2.0, -1.0 / 3.0));
|
---|
337 |
|
---|
338 | // mean
|
---|
339 | Evaluate(interpreter, ds, "(mean -1.0 1.0 -1.0)", 0, -1.0 / 3.0);
|
---|
340 |
|
---|
341 | // lag
|
---|
342 | Evaluate(interpreter, ds, "(lagVariable 1.0 a -1) ", 1, ds.GetDoubleValue("A", 0));
|
---|
343 | Evaluate(interpreter, ds, "(lagVariable 1.0 a -1) ", 2, ds.GetDoubleValue("A", 1));
|
---|
344 | Evaluate(interpreter, ds, "(lagVariable 1.0 a 0) ", 2, ds.GetDoubleValue("A", 2));
|
---|
345 | Evaluate(interpreter, ds, "(lagVariable 1.0 a 1) ", 0, ds.GetDoubleValue("A", 1));
|
---|
346 |
|
---|
347 | // integral
|
---|
348 | Evaluate(interpreter, ds, "(integral -1.0 (variable 1.0 a)) ", 1, ds.GetDoubleValue("A", 0) + ds.GetDoubleValue("A", 1));
|
---|
349 | Evaluate(interpreter, ds, "(integral -1.0 (lagVariable 1.0 a 1)) ", 1, ds.GetDoubleValue("A", 1) + ds.GetDoubleValue("A", 2));
|
---|
350 | Evaluate(interpreter, ds, "(integral -2.0 (variable 1.0 a)) ", 2, ds.GetDoubleValue("A", 0) + ds.GetDoubleValue("A", 1) + ds.GetDoubleValue("A", 2));
|
---|
351 | Evaluate(interpreter, ds, "(integral -1.0 (* (variable 1.0 a) (variable 1.0 b)))", 1, ds.GetDoubleValue("A", 0) * ds.GetDoubleValue("B", 0) + ds.GetDoubleValue("A", 1) * ds.GetDoubleValue("B", 1));
|
---|
352 | Evaluate(interpreter, ds, "(integral -2.0 3.0)", 1, 9.0);
|
---|
353 |
|
---|
354 | // derivative
|
---|
355 | // (f_0 + 2 * f_1 - 2 * f_3 - f_4) / 8; // h = 1
|
---|
356 | Evaluate(interpreter, ds, "(diff (variable 1.0 a)) ", 5, (ds.GetDoubleValue("A", 5) + 2 * ds.GetDoubleValue("A", 4) - 2 * ds.GetDoubleValue("A", 2) - ds.GetDoubleValue("A", 1)) / 8.0);
|
---|
357 | Evaluate(interpreter, ds, "(diff (variable 1.0 b)) ", 5, (ds.GetDoubleValue("B", 5) + 2 * ds.GetDoubleValue("B", 4) - 2 * ds.GetDoubleValue("B", 2) - ds.GetDoubleValue("B", 1)) / 8.0);
|
---|
358 | Evaluate(interpreter, ds, "(diff (* (variable 1.0 a) (variable 1.0 b)))", 5, +
|
---|
359 | (ds.GetDoubleValue("A", 5) * ds.GetDoubleValue("B", 5) +
|
---|
360 | 2 * ds.GetDoubleValue("A", 4) * ds.GetDoubleValue("B", 4) -
|
---|
361 | 2 * ds.GetDoubleValue("A", 2) * ds.GetDoubleValue("B", 2) -
|
---|
362 | ds.GetDoubleValue("A", 1) * ds.GetDoubleValue("B", 1)) / 8.0);
|
---|
363 | Evaluate(interpreter, ds, "(diff -2.0 3.0)", 5, 0.0);
|
---|
364 |
|
---|
365 | // timelag
|
---|
366 | Evaluate(interpreter, ds, "(lag -1.0 (lagVariable 1.0 a 2)) ", 1, ds.GetDoubleValue("A", 2));
|
---|
367 | Evaluate(interpreter, ds, "(lag -2.0 (lagVariable 1.0 a 2)) ", 2, ds.GetDoubleValue("A", 2));
|
---|
368 | Evaluate(interpreter, ds, "(lag -1.0 (* (lagVariable 1.0 a 1) (lagVariable 1.0 b 2)))", 1, ds.GetDoubleValue("A", 1) * ds.GetDoubleValue("B", 2));
|
---|
369 | Evaluate(interpreter, ds, "(lag -2.0 3.0)", 1, 3.0);
|
---|
370 |
|
---|
371 | {
|
---|
372 | // special functions
|
---|
373 | Action<double> checkAiry = (x) => {
|
---|
374 | double ai, aip, bi, bip;
|
---|
375 | alglib.airy(x, out ai, out aip, out bi, out bip);
|
---|
376 | Evaluate(interpreter, ds, "(airya " + x + ")", 0, ai);
|
---|
377 | Evaluate(interpreter, ds, "(airyb " + x + ")", 0, bi);
|
---|
378 | };
|
---|
379 |
|
---|
380 | Action<double> checkBessel = (x) => {
|
---|
381 | Evaluate(interpreter, ds, "(bessel " + x + ")", 0, alglib.besseli0(x));
|
---|
382 | };
|
---|
383 |
|
---|
384 | Action<double> checkSinCosIntegrals = (x) => {
|
---|
385 | double si, ci;
|
---|
386 | alglib.sinecosineintegrals(x, out si, out ci);
|
---|
387 | Evaluate(interpreter, ds, "(cosint " + x + ")", 0, ci);
|
---|
388 | Evaluate(interpreter, ds, "(sinint " + x + ")", 0, si);
|
---|
389 | };
|
---|
390 | Action<double> checkHypSinCosIntegrals = (x) => {
|
---|
391 | double shi, chi;
|
---|
392 | alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
|
---|
393 | Evaluate(interpreter, ds, "(hypcosint " + x + ")", 0, chi);
|
---|
394 | Evaluate(interpreter, ds, "(hypsinint " + x + ")", 0, shi);
|
---|
395 | };
|
---|
396 | Action<double> checkFresnelSinCosIntegrals = (x) => {
|
---|
397 | double c = 0, s = 0;
|
---|
398 | alglib.fresnelintegral(x, ref c, ref s);
|
---|
399 | Evaluate(interpreter, ds, "(fresnelcosint " + x + ")", 0, c);
|
---|
400 | Evaluate(interpreter, ds, "(fresnelsinint " + x + ")", 0, s);
|
---|
401 | };
|
---|
402 | Action<double> checkNormErf = (x) => {
|
---|
403 | Evaluate(interpreter, ds, "(norm " + x + ")", 0, alglib.normaldistribution(x));
|
---|
404 | Evaluate(interpreter, ds, "(erf " + x + ")", 0, alglib.errorfunction(x));
|
---|
405 | };
|
---|
406 |
|
---|
407 | Action<double> checkGamma = (x) => {
|
---|
408 | Evaluate(interpreter, ds, "(gamma " + x + ")", 0, alglib.gammafunction(x));
|
---|
409 | };
|
---|
410 | Action<double> checkPsi = (x) => {
|
---|
411 | try {
|
---|
412 | Evaluate(interpreter, ds, "(psi " + x + ")", 0, alglib.psi(x));
|
---|
413 | }
|
---|
414 | catch (alglib.alglibexception) { // ignore cases where alglib throws an exception
|
---|
415 | }
|
---|
416 | };
|
---|
417 | Action<double> checkDawson = (x) => {
|
---|
418 | Evaluate(interpreter, ds, "(dawson " + x + ")", 0, alglib.dawsonintegral(x));
|
---|
419 | };
|
---|
420 | Action<double> checkExpInt = (x) => {
|
---|
421 | Evaluate(interpreter, ds, "(expint " + x + ")", 0, alglib.exponentialintegralei(x));
|
---|
422 | };
|
---|
423 |
|
---|
424 |
|
---|
425 |
|
---|
426 | foreach (var e in new[] { -2.0, -1.0, 0.0, 1.0, 2.0 }) {
|
---|
427 | checkAiry(e);
|
---|
428 | checkBessel(e);
|
---|
429 | checkSinCosIntegrals(e);
|
---|
430 | checkGamma(e);
|
---|
431 | checkExpInt(e);
|
---|
432 | checkDawson(e);
|
---|
433 | checkPsi(e);
|
---|
434 | checkNormErf(e);
|
---|
435 | checkFresnelSinCosIntegrals(e);
|
---|
436 | checkHypSinCosIntegrals(e);
|
---|
437 | }
|
---|
438 | }
|
---|
439 | }
|
---|
440 |
|
---|
441 | private void Evaluate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds, string expr, int index, double expected) {
|
---|
442 | var importer = new SymbolicExpressionImporter();
|
---|
443 | ISymbolicExpressionTree tree = importer.Import(expr);
|
---|
444 |
|
---|
445 | double actual = interpreter.GetSymbolicExpressionTreeValues(tree, ds, Enumerable.Range(index, 1)).First();
|
---|
446 |
|
---|
447 | Assert.IsFalse(double.IsNaN(actual) && !double.IsNaN(expected));
|
---|
448 | Assert.IsFalse(!double.IsNaN(actual) && double.IsNaN(expected));
|
---|
449 | Assert.AreEqual(expected, actual, 1.0E-12, expr);
|
---|
450 | }
|
---|
451 | }
|
---|
452 | }
|
---|