Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicDataAnalysisExpressionTreeInterpreterTest.cs @ 9844

Last change on this file since 9844 was 9828, checked in by mkommend, 11 years ago

#2021: Integrated the linear interpreter in the trunk and restructed interpreter unit tests.

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