[5574] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5574] | 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;
|
---|
[7926] | 27 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
[5574] | 28 | using HeuristicLab.Random;
|
---|
| 29 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
[7915] | 30 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic_34.Tests {
|
---|
[5574] | 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]
|
---|
[7926] | 54 | public void SymbolicDataAnalysisExpressionTreeInterpreterTypeCoherentGrammarPerformanceTest() {
|
---|
| 55 | TypeCoherentGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeInterpreter(), 12.5e6);
|
---|
| 56 | }
|
---|
| 57 | [TestMethod]
|
---|
[6732] | 58 | public void SymbolicDataAnalysisExpressionTreeInterpreterFullGrammarPerformanceTest() {
|
---|
| 59 | FullGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeInterpreter(), 12.5e6);
|
---|
| 60 | }
|
---|
| 61 | [TestMethod]
|
---|
| 62 | public void SymbolicDataAnalysisExpressionTreeInterpreterArithmeticGrammarPerformanceTest() {
|
---|
[7707] | 63 | ArithmeticGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeInterpreter(), 12.5e6);
|
---|
[6732] | 64 | }
|
---|
| 65 |
|
---|
| 66 | [TestMethod]
|
---|
[7926] | 67 | public void SymbolicDataAnalysisExpressionTreeILEmittingInterpreterTypeCoherentGrammarPerformanceTest() {
|
---|
| 68 | TypeCoherentGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(), 7.5e6);
|
---|
| 69 | }
|
---|
| 70 | [TestMethod]
|
---|
[6732] | 71 | public void SymbolicDataAnalysisExpressionTreeILEmittingInterpreterFullGrammarPerformanceTest() {
|
---|
[6865] | 72 | FullGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(), 7.5e6);
|
---|
[6732] | 73 | }
|
---|
| 74 | [TestMethod]
|
---|
| 75 | public void SymbolicDataAnalysisExpressionTreeILEmittingInterpreterArithmeticGrammarPerformanceTest() {
|
---|
[7707] | 76 | ArithmeticGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(), 7.5e6);
|
---|
[6732] | 77 | }
|
---|
| 78 |
|
---|
[7926] | 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 |
|
---|
[6732] | 96 | private void FullGrammarPerformanceTest(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, double nodesPerSecThreshold) {
|
---|
[5574] | 97 | var twister = new MersenneTwister(31415);
|
---|
| 98 | var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
|
---|
[5686] | 99 | var grammar = new FullFunctionalExpressionGrammar();
|
---|
| 100 | grammar.MaximumFunctionArguments = 0;
|
---|
| 101 | grammar.MaximumFunctionDefinitions = 0;
|
---|
| 102 | grammar.MinimumFunctionArguments = 0;
|
---|
| 103 | grammar.MinimumFunctionDefinitions = 0;
|
---|
[5574] | 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);
|
---|
[6732] | 109 | Assert.IsTrue(nodesPerSec > nodesPerSecThreshold); // evaluated nodes per seconds must be larger than 15mNodes/sec
|
---|
[5574] | 110 | }
|
---|
| 111 |
|
---|
[6732] | 112 | private void ArithmeticGrammarPerformanceTest(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, double nodesPerSecThreshold) {
|
---|
[5574] | 113 | var twister = new MersenneTwister(31415);
|
---|
| 114 | var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
|
---|
[5686] | 115 | var grammar = new ArithmeticExpressionGrammar();
|
---|
| 116 | grammar.MaximumFunctionArguments = 0;
|
---|
| 117 | grammar.MaximumFunctionDefinitions = 0;
|
---|
| 118 | grammar.MinimumFunctionArguments = 0;
|
---|
| 119 | grammar.MinimumFunctionDefinitions = 0;
|
---|
[5574] | 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 | }
|
---|
[6732] | 124 |
|
---|
[5574] | 125 | double nodesPerSec = Util.CalculateEvaluatedNodesPerSec(randomTrees, interpreter, dataset, 3);
|
---|
[6732] | 126 | Assert.IsTrue(nodesPerSec > nodesPerSecThreshold); // evaluated nodes per seconds must be larger than 15mNodes/sec
|
---|
[5574] | 127 | }
|
---|
| 128 |
|
---|
| 129 |
|
---|
| 130 | /// <summary>
|
---|
| 131 | ///A test for Evaluate
|
---|
| 132 | ///</summary>
|
---|
| 133 | [TestMethod]
|
---|
[6732] | 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 },
|
---|
[6809] | 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 }
|
---|
[6732] | 148 | });
|
---|
[5574] | 149 |
|
---|
[6732] | 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() {
|
---|
[5574] | 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 },
|
---|
[6809] | 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 }
|
---|
[5574] | 171 | });
|
---|
| 172 |
|
---|
[6732] | 173 | var interpreter = new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter();
|
---|
| 174 | EvaluateTerminals(interpreter, ds);
|
---|
| 175 | EvaluateOperations(interpreter, ds);
|
---|
| 176 | }
|
---|
[5574] | 177 |
|
---|
[6732] | 178 | private void EvaluateTerminals(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) {
|
---|
[5574] | 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);
|
---|
[6732] | 185 | }
|
---|
[5574] | 186 |
|
---|
[6732] | 187 | private void EvaluateAdf(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) {
|
---|
[5574] | 188 |
|
---|
[6732] | 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) {
|
---|
[5574] | 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);
|
---|
[6732] | 260 | Evaluate(interpreter, ds, "(> (log -1.0) (log -1.0))", 0, -1.0); // (> nan nan) should be false
|
---|
[5574] | 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);
|
---|
[6732] | 267 | Evaluate(interpreter, ds, "(< (log -1.0) (log -1.0))", 0, -1.0); // (< nan nan) should be false
|
---|
[5574] | 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);
|
---|
[6732] | 275 | Evaluate(interpreter, ds, "(if (log -1.0) 2.0 3.0)", 0, 3.0); // if(nan) should return the else branch
|
---|
[5574] | 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);
|
---|
[6732] | 283 | Evaluate(interpreter, ds, "(not (log -1.0))", 0, 1.0);
|
---|
[5574] | 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);
|
---|
[6732] | 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)
|
---|
[5574] | 296 |
|
---|
[6732] | 297 |
|
---|
[5574] | 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);
|
---|
[6732] | 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)
|
---|
[5574] | 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 |
|
---|
[6755] | 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 |
|
---|
[5574] | 335 | // mean
|
---|
| 336 | Evaluate(interpreter, ds, "(mean -1.0 1.0 -1.0)", 0, -1.0 / 3.0);
|
---|
| 337 |
|
---|
[6769] | 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));
|
---|
[6809] | 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);
|
---|
[8123] | 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 | }
|
---|
[5574] | 436 | }
|
---|
| 437 |
|
---|
[6732] | 438 | private void Evaluate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds, string expr, int index, double expected) {
|
---|
[5574] | 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 |
|
---|
[6809] | 444 | Assert.IsFalse(double.IsNaN(actual) && !double.IsNaN(expected));
|
---|
| 445 | Assert.IsFalse(!double.IsNaN(actual) && double.IsNaN(expected));
|
---|
[5574] | 446 | Assert.AreEqual(expected, actual, 1.0E-12, expr);
|
---|
| 447 | }
|
---|
| 448 | }
|
---|
| 449 | }
|
---|