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