Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HLScript/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicDataAnalysisExpressionTreeInterpreterTest.cs @ 12912

Last change on this file since 12912 was 9956, checked in by mkommend, 11 years ago

#2108: Removed accidently commited code (r9955)in symbolic expression tree interpreter test.

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