[6732] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16565] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6732] | 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;
|
---|
[7615] | 23 | using System.Collections.Generic;
|
---|
[6741] | 24 | using System.Linq;
|
---|
[6732] | 25 | using System.Reflection;
|
---|
| 26 | using System.Reflection.Emit;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
[6740] | 29 | using HeuristicLab.Data;
|
---|
[6732] | 30 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[6740] | 31 | using HeuristicLab.Parameters;
|
---|
[16565] | 32 | using HEAL.Attic;
|
---|
[6732] | 33 |
|
---|
| 34 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
[16565] | 35 | [StorableType("426718E3-2A57-4CA4-98A1-65EDD0B0BDBF")]
|
---|
[6732] | 36 | [Item("SymbolicDataAnalysisExpressionTreeILEmittingInterpreter", "Interpreter for symbolic expression trees.")]
|
---|
[8436] | 37 | public sealed class SymbolicDataAnalysisExpressionTreeILEmittingInterpreter : ParameterizedNamedItem, ISymbolicDataAnalysisExpressionTreeInterpreter {
|
---|
| 38 | private static readonly Type thisType = typeof(SymbolicDataAnalysisExpressionTreeILEmittingInterpreter);
|
---|
| 39 | internal delegate double CompiledFunction(int sampleIndex, IList<double>[] columns);
|
---|
[7842] | 40 |
|
---|
[8436] | 41 | #region method infos
|
---|
| 42 | private static MethodInfo listGetValue = typeof(IList<double>).GetProperty("Item", new Type[] { typeof(int) }).GetGetMethod();
|
---|
| 43 |
|
---|
[6732] | 44 | private static MethodInfo cos = typeof(Math).GetMethod("Cos", new Type[] { typeof(double) });
|
---|
| 45 | private static MethodInfo sin = typeof(Math).GetMethod("Sin", new Type[] { typeof(double) });
|
---|
| 46 | private static MethodInfo tan = typeof(Math).GetMethod("Tan", new Type[] { typeof(double) });
|
---|
[16668] | 47 | private static MethodInfo tanh = typeof(Math).GetMethod("Tanh", new Type[] { typeof(double) });
|
---|
[6732] | 48 | private static MethodInfo exp = typeof(Math).GetMethod("Exp", new Type[] { typeof(double) });
|
---|
| 49 | private static MethodInfo log = typeof(Math).GetMethod("Log", new Type[] { typeof(double) });
|
---|
| 50 | private static MethodInfo power = typeof(Math).GetMethod("Pow", new Type[] { typeof(double), typeof(double) });
|
---|
[6755] | 51 | private static MethodInfo round = typeof(Math).GetMethod("Round", new Type[] { typeof(double) });
|
---|
[7842] | 52 | private static MethodInfo sqrt = typeof(Math).GetMethod("Sqrt", new Type[] { typeof(double) });
|
---|
[16356] | 53 | private static MethodInfo abs = typeof(Math).GetMethod("Abs", new Type[] { typeof(double) });
|
---|
[6732] | 54 |
|
---|
[7842] | 55 | private static MethodInfo airyA = thisType.GetMethod("AiryA", new Type[] { typeof(double) });
|
---|
| 56 | private static MethodInfo airyB = thisType.GetMethod("AiryB", new Type[] { typeof(double) });
|
---|
| 57 | private static MethodInfo gamma = thisType.GetMethod("Gamma", new Type[] { typeof(double) });
|
---|
| 58 | private static MethodInfo psi = thisType.GetMethod("Psi", new Type[] { typeof(double) });
|
---|
| 59 | private static MethodInfo dawson = thisType.GetMethod("Dawson", new Type[] { typeof(double) });
|
---|
| 60 | private static MethodInfo expIntegralEi = thisType.GetMethod("ExpIntegralEi", new Type[] { typeof(double) });
|
---|
| 61 | private static MethodInfo sinIntegral = thisType.GetMethod("SinIntegral", new Type[] { typeof(double) });
|
---|
| 62 | private static MethodInfo cosIntegral = thisType.GetMethod("CosIntegral", new Type[] { typeof(double) });
|
---|
| 63 | private static MethodInfo hypSinIntegral = thisType.GetMethod("HypSinIntegral", new Type[] { typeof(double) });
|
---|
| 64 | private static MethodInfo hypCosIntegral = thisType.GetMethod("HypCosIntegral", new Type[] { typeof(double) });
|
---|
| 65 | private static MethodInfo fresnelCosIntegral = thisType.GetMethod("FresnelCosIntegral", new Type[] { typeof(double) });
|
---|
| 66 | private static MethodInfo fresnelSinIntegral = thisType.GetMethod("FresnelSinIntegral", new Type[] { typeof(double) });
|
---|
| 67 | private static MethodInfo norm = thisType.GetMethod("Norm", new Type[] { typeof(double) });
|
---|
| 68 | private static MethodInfo erf = thisType.GetMethod("Erf", new Type[] { typeof(double) });
|
---|
| 69 | private static MethodInfo bessel = thisType.GetMethod("Bessel", new Type[] { typeof(double) });
|
---|
[14826] | 70 | private static MethodInfo string_eq = typeof(string).GetMethod("Equals", new Type[] { typeof(string) });
|
---|
[8436] | 71 | #endregion
|
---|
[7842] | 72 |
|
---|
[6732] | 73 | private const string CheckExpressionsWithIntervalArithmeticParameterName = "CheckExpressionsWithIntervalArithmetic";
|
---|
[13248] | 74 | private const string CheckExpressionsWithIntervalArithmeticParameterDescription = "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.";
|
---|
[7615] | 75 | private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
|
---|
[7842] | 76 |
|
---|
[6732] | 77 | public override bool CanChangeName {
|
---|
| 78 | get { return false; }
|
---|
| 79 | }
|
---|
[7842] | 80 |
|
---|
[6732] | 81 | public override bool CanChangeDescription {
|
---|
| 82 | get { return false; }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | #region parameter properties
|
---|
[13248] | 86 | public IFixedValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter {
|
---|
| 87 | get { return (IFixedValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }
|
---|
[6732] | 88 | }
|
---|
[7615] | 89 |
|
---|
[13248] | 90 | public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
| 91 | get { return (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
|
---|
[7615] | 92 | }
|
---|
[6732] | 93 | #endregion
|
---|
| 94 |
|
---|
| 95 | #region properties
|
---|
[13248] | 96 | public bool CheckExpressionsWithIntervalArithmetic {
|
---|
| 97 | get { return CheckExpressionsWithIntervalArithmeticParameter.Value.Value; }
|
---|
| 98 | set { CheckExpressionsWithIntervalArithmeticParameter.Value.Value = value; }
|
---|
[6732] | 99 | }
|
---|
[13248] | 100 | public int EvaluatedSolutions {
|
---|
| 101 | get { return EvaluatedSolutionsParameter.Value.Value; }
|
---|
| 102 | set { EvaluatedSolutionsParameter.Value.Value = value; }
|
---|
[7615] | 103 | }
|
---|
[6732] | 104 | #endregion
|
---|
| 105 |
|
---|
| 106 | [StorableConstructor]
|
---|
[16565] | 107 | private SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(StorableConstructorFlag _) : base(_) { }
|
---|
[7842] | 108 |
|
---|
[8436] | 109 | private SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(SymbolicDataAnalysisExpressionTreeILEmittingInterpreter original, Cloner cloner) : base(original, cloner) { }
|
---|
[6732] | 110 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 111 | return new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(this, cloner);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | public SymbolicDataAnalysisExpressionTreeILEmittingInterpreter()
|
---|
| 115 | : base("SymbolicDataAnalysisExpressionTreeILEmittingInterpreter", "Interpreter for symbolic expression trees.") {
|
---|
[13248] | 116 | Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName,
|
---|
| 117 | "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
|
---|
| 118 | Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
|
---|
[6732] | 119 | }
|
---|
| 120 |
|
---|
[13248] | 121 | public SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(string name, string description)
|
---|
| 122 | : base(name, description) {
|
---|
| 123 | Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName,
|
---|
| 124 | "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
|
---|
| 125 | Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[7615] | 128 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 129 | private void AfterDeserialization() {
|
---|
[13248] | 130 | var evaluatedSolutions = new IntValue(0);
|
---|
| 131 | var checkExpressionsWithIntervalArithmetic = new BoolValue(false);
|
---|
| 132 | if (Parameters.ContainsKey(EvaluatedSolutionsParameterName)) {
|
---|
| 133 | var evaluatedSolutionsParameter = (IValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName];
|
---|
| 134 | evaluatedSolutions = evaluatedSolutionsParameter.Value;
|
---|
| 135 | Parameters.Remove(EvaluatedSolutionsParameterName);
|
---|
| 136 | }
|
---|
| 137 | Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", evaluatedSolutions));
|
---|
| 138 | if (Parameters.ContainsKey(CheckExpressionsWithIntervalArithmeticParameterName)) {
|
---|
| 139 | var checkExpressionsWithIntervalArithmeticParameter = (IValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName];
|
---|
| 140 | Parameters.Remove(CheckExpressionsWithIntervalArithmeticParameterName);
|
---|
| 141 | checkExpressionsWithIntervalArithmetic = checkExpressionsWithIntervalArithmeticParameter.Value;
|
---|
| 142 | }
|
---|
| 143 | Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, CheckExpressionsWithIntervalArithmeticParameterDescription, checkExpressionsWithIntervalArithmetic));
|
---|
[7615] | 144 | }
|
---|
| 145 |
|
---|
| 146 | #region IStatefulItem
|
---|
| 147 | public void InitializeState() {
|
---|
[13248] | 148 | EvaluatedSolutions = 0;
|
---|
[7615] | 149 | }
|
---|
| 150 |
|
---|
| 151 | public void ClearState() {
|
---|
| 152 | }
|
---|
| 153 | #endregion
|
---|
| 154 |
|
---|
[13251] | 155 | private readonly object syncRoot = new object();
|
---|
[12509] | 156 | public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) {
|
---|
[13248] | 157 | if (CheckExpressionsWithIntervalArithmetic)
|
---|
[8436] | 158 | throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter.");
|
---|
| 159 |
|
---|
[13251] | 160 | lock (syncRoot) {
|
---|
| 161 | EvaluatedSolutions++; // increment the evaluated solutions counter
|
---|
| 162 | }
|
---|
[8436] | 163 | var state = PrepareInterpreterState(tree, dataset);
|
---|
[6741] | 164 |
|
---|
[8436] | 165 | Type[] methodArgs = { typeof(int), typeof(IList<double>[]) };
|
---|
| 166 | DynamicMethod testFun = new DynamicMethod("TestFun", typeof(double), methodArgs, typeof(SymbolicDataAnalysisExpressionTreeILEmittingInterpreter).Module);
|
---|
[6741] | 167 |
|
---|
[8436] | 168 | ILGenerator il = testFun.GetILGenerator();
|
---|
| 169 | CompileInstructions(il, state, dataset);
|
---|
| 170 | il.Emit(System.Reflection.Emit.OpCodes.Conv_R8);
|
---|
| 171 | il.Emit(System.Reflection.Emit.OpCodes.Ret);
|
---|
| 172 | var function = (CompiledFunction)testFun.CreateDelegate(typeof(CompiledFunction));
|
---|
| 173 |
|
---|
| 174 | IList<double>[] columns = dataset.DoubleVariables.Select(v => dataset.GetReadOnlyDoubleValues(v)).ToArray();
|
---|
| 175 |
|
---|
| 176 | foreach (var row in rows) {
|
---|
| 177 | yield return function(row, columns);
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[12509] | 181 | private InterpreterState PrepareInterpreterState(ISymbolicExpressionTree tree, IDataset dataset) {
|
---|
[8436] | 182 | Instruction[] code = SymbolicExpressionTreeCompiler.Compile(tree, OpCodes.MapSymbolToOpCode);
|
---|
| 183 | Dictionary<string, int> doubleVariableNames = dataset.DoubleVariables.Select((x, i) => new { x, i }).ToDictionary(e => e.x, e => e.i);
|
---|
| 184 | int necessaryArgStackSize = 0;
|
---|
| 185 | foreach (Instruction instr in code) {
|
---|
[6732] | 186 | if (instr.opCode == OpCodes.Variable) {
|
---|
[8436] | 187 | var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
|
---|
[9828] | 188 | instr.data = doubleVariableNames[variableTreeNode.VariableName];
|
---|
[6732] | 189 | } else if (instr.opCode == OpCodes.LagVariable) {
|
---|
[8436] | 190 | var laggedVariableTreeNode = (LaggedVariableTreeNode)instr.dynamicNode;
|
---|
[9828] | 191 | instr.data = doubleVariableNames[laggedVariableTreeNode.VariableName];
|
---|
[6732] | 192 | } else if (instr.opCode == OpCodes.VariableCondition) {
|
---|
[8436] | 193 | var variableConditionTreeNode = (VariableConditionTreeNode)instr.dynamicNode;
|
---|
[9828] | 194 | instr.data = doubleVariableNames[variableConditionTreeNode.VariableName];
|
---|
[6732] | 195 | } else if (instr.opCode == OpCodes.Call) {
|
---|
| 196 | necessaryArgStackSize += instr.nArguments + 1;
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
[8436] | 199 | return new InterpreterState(code, necessaryArgStackSize);
|
---|
[6732] | 200 | }
|
---|
| 201 |
|
---|
[12509] | 202 | private void CompileInstructions(ILGenerator il, InterpreterState state, IDataset ds) {
|
---|
[6732] | 203 | Instruction currentInstr = state.NextInstruction();
|
---|
| 204 | int nArgs = currentInstr.nArguments;
|
---|
| 205 |
|
---|
| 206 | switch (currentInstr.opCode) {
|
---|
| 207 | case OpCodes.Add: {
|
---|
| 208 | if (nArgs > 0) {
|
---|
[6809] | 209 | CompileInstructions(il, state, ds);
|
---|
[6732] | 210 | }
|
---|
| 211 | for (int i = 1; i < nArgs; i++) {
|
---|
[6809] | 212 | CompileInstructions(il, state, ds);
|
---|
[6732] | 213 | il.Emit(System.Reflection.Emit.OpCodes.Add);
|
---|
| 214 | }
|
---|
| 215 | return;
|
---|
| 216 | }
|
---|
| 217 | case OpCodes.Sub: {
|
---|
| 218 | if (nArgs == 1) {
|
---|
[6809] | 219 | CompileInstructions(il, state, ds);
|
---|
[6732] | 220 | il.Emit(System.Reflection.Emit.OpCodes.Neg);
|
---|
| 221 | return;
|
---|
| 222 | }
|
---|
| 223 | if (nArgs > 0) {
|
---|
[6809] | 224 | CompileInstructions(il, state, ds);
|
---|
[6732] | 225 | }
|
---|
| 226 | for (int i = 1; i < nArgs; i++) {
|
---|
[6809] | 227 | CompileInstructions(il, state, ds);
|
---|
[6732] | 228 | il.Emit(System.Reflection.Emit.OpCodes.Sub);
|
---|
| 229 | }
|
---|
| 230 | return;
|
---|
| 231 | }
|
---|
| 232 | case OpCodes.Mul: {
|
---|
| 233 | if (nArgs > 0) {
|
---|
[6809] | 234 | CompileInstructions(il, state, ds);
|
---|
[6732] | 235 | }
|
---|
| 236 | for (int i = 1; i < nArgs; i++) {
|
---|
[6809] | 237 | CompileInstructions(il, state, ds);
|
---|
[6732] | 238 | il.Emit(System.Reflection.Emit.OpCodes.Mul);
|
---|
| 239 | }
|
---|
| 240 | return;
|
---|
| 241 | }
|
---|
| 242 | case OpCodes.Div: {
|
---|
| 243 | if (nArgs == 1) {
|
---|
| 244 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0);
|
---|
[6809] | 245 | CompileInstructions(il, state, ds);
|
---|
[6732] | 246 | il.Emit(System.Reflection.Emit.OpCodes.Div);
|
---|
| 247 | return;
|
---|
| 248 | }
|
---|
| 249 | if (nArgs > 0) {
|
---|
[6809] | 250 | CompileInstructions(il, state, ds);
|
---|
[6732] | 251 | }
|
---|
| 252 | for (int i = 1; i < nArgs; i++) {
|
---|
[6809] | 253 | CompileInstructions(il, state, ds);
|
---|
[6732] | 254 | il.Emit(System.Reflection.Emit.OpCodes.Div);
|
---|
| 255 | }
|
---|
| 256 | return;
|
---|
| 257 | }
|
---|
| 258 | case OpCodes.Average: {
|
---|
[6809] | 259 | CompileInstructions(il, state, ds);
|
---|
[6732] | 260 | for (int i = 1; i < nArgs; i++) {
|
---|
[6809] | 261 | CompileInstructions(il, state, ds);
|
---|
[6732] | 262 | il.Emit(System.Reflection.Emit.OpCodes.Add);
|
---|
| 263 | }
|
---|
[14809] | 264 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, (double)nArgs);
|
---|
[6732] | 265 | il.Emit(System.Reflection.Emit.OpCodes.Div);
|
---|
| 266 | return;
|
---|
| 267 | }
|
---|
[16356] | 268 | case OpCodes.Absolute: {
|
---|
| 269 | CompileInstructions(il, state, ds);
|
---|
| 270 | il.Emit(System.Reflection.Emit.OpCodes.Call, abs);
|
---|
| 271 | return;
|
---|
| 272 | }
|
---|
[6732] | 273 | case OpCodes.Cos: {
|
---|
[6809] | 274 | CompileInstructions(il, state, ds);
|
---|
[6732] | 275 | il.Emit(System.Reflection.Emit.OpCodes.Call, cos);
|
---|
| 276 | return;
|
---|
| 277 | }
|
---|
| 278 | case OpCodes.Sin: {
|
---|
[6809] | 279 | CompileInstructions(il, state, ds);
|
---|
[6732] | 280 | il.Emit(System.Reflection.Emit.OpCodes.Call, sin);
|
---|
| 281 | return;
|
---|
| 282 | }
|
---|
| 283 | case OpCodes.Tan: {
|
---|
[6809] | 284 | CompileInstructions(il, state, ds);
|
---|
[6732] | 285 | il.Emit(System.Reflection.Emit.OpCodes.Call, tan);
|
---|
| 286 | return;
|
---|
| 287 | }
|
---|
[16670] | 288 | case OpCodes.Tanh: {
|
---|
[16668] | 289 | CompileInstructions(il, state, ds);
|
---|
| 290 | il.Emit(System.Reflection.Emit.OpCodes.Call, tanh);
|
---|
| 291 | return;
|
---|
| 292 | }
|
---|
[6732] | 293 | case OpCodes.Power: {
|
---|
[6809] | 294 | CompileInstructions(il, state, ds);
|
---|
| 295 | CompileInstructions(il, state, ds);
|
---|
[6755] | 296 | il.Emit(System.Reflection.Emit.OpCodes.Call, round);
|
---|
[6732] | 297 | il.Emit(System.Reflection.Emit.OpCodes.Call, power);
|
---|
| 298 | return;
|
---|
| 299 | }
|
---|
| 300 | case OpCodes.Root: {
|
---|
[6809] | 301 | CompileInstructions(il, state, ds);
|
---|
[6755] | 302 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // 1 / round(...)
|
---|
[6809] | 303 | CompileInstructions(il, state, ds);
|
---|
[6755] | 304 | il.Emit(System.Reflection.Emit.OpCodes.Call, round);
|
---|
| 305 | il.Emit(System.Reflection.Emit.OpCodes.Div);
|
---|
| 306 | il.Emit(System.Reflection.Emit.OpCodes.Call, power);
|
---|
| 307 | return;
|
---|
[6732] | 308 | }
|
---|
| 309 | case OpCodes.Exp: {
|
---|
[6809] | 310 | CompileInstructions(il, state, ds);
|
---|
[6732] | 311 | il.Emit(System.Reflection.Emit.OpCodes.Call, exp);
|
---|
| 312 | return;
|
---|
| 313 | }
|
---|
| 314 | case OpCodes.Log: {
|
---|
[6809] | 315 | CompileInstructions(il, state, ds);
|
---|
[6732] | 316 | il.Emit(System.Reflection.Emit.OpCodes.Call, log);
|
---|
| 317 | return;
|
---|
| 318 | }
|
---|
[7842] | 319 | case OpCodes.Square: {
|
---|
| 320 | CompileInstructions(il, state, ds);
|
---|
| 321 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0);
|
---|
| 322 | il.Emit(System.Reflection.Emit.OpCodes.Call, power);
|
---|
| 323 | return;
|
---|
| 324 | }
|
---|
[16356] | 325 | case OpCodes.Cube: {
|
---|
| 326 | CompileInstructions(il, state, ds);
|
---|
| 327 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 3.0);
|
---|
| 328 | il.Emit(System.Reflection.Emit.OpCodes.Call, power);
|
---|
| 329 | return;
|
---|
| 330 | }
|
---|
[7842] | 331 | case OpCodes.SquareRoot: {
|
---|
| 332 | CompileInstructions(il, state, ds);
|
---|
| 333 | il.Emit(System.Reflection.Emit.OpCodes.Call, sqrt);
|
---|
| 334 | return;
|
---|
| 335 | }
|
---|
[16356] | 336 | case OpCodes.CubeRoot: {
|
---|
| 337 | CompileInstructions(il, state, ds);
|
---|
| 338 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0 / 3.0);
|
---|
| 339 | il.Emit(System.Reflection.Emit.OpCodes.Call, power);
|
---|
| 340 | return;
|
---|
| 341 | }
|
---|
[7842] | 342 | case OpCodes.AiryA: {
|
---|
| 343 | CompileInstructions(il, state, ds);
|
---|
| 344 | il.Emit(System.Reflection.Emit.OpCodes.Call, airyA);
|
---|
| 345 | return;
|
---|
| 346 | }
|
---|
| 347 | case OpCodes.AiryB: {
|
---|
| 348 | CompileInstructions(il, state, ds);
|
---|
| 349 | il.Emit(System.Reflection.Emit.OpCodes.Call, airyB);
|
---|
| 350 | return;
|
---|
| 351 | }
|
---|
| 352 | case OpCodes.Bessel: {
|
---|
| 353 | CompileInstructions(il, state, ds);
|
---|
| 354 | il.Emit(System.Reflection.Emit.OpCodes.Call, bessel);
|
---|
| 355 | return;
|
---|
| 356 | }
|
---|
| 357 | case OpCodes.CosineIntegral: {
|
---|
| 358 | CompileInstructions(il, state, ds);
|
---|
| 359 | il.Emit(System.Reflection.Emit.OpCodes.Call, cosIntegral);
|
---|
| 360 | return;
|
---|
| 361 | }
|
---|
| 362 | case OpCodes.Dawson: {
|
---|
| 363 | CompileInstructions(il, state, ds);
|
---|
| 364 | il.Emit(System.Reflection.Emit.OpCodes.Call, dawson);
|
---|
| 365 | return;
|
---|
| 366 | }
|
---|
| 367 | case OpCodes.Erf: {
|
---|
| 368 | CompileInstructions(il, state, ds);
|
---|
| 369 | il.Emit(System.Reflection.Emit.OpCodes.Call, erf);
|
---|
| 370 | return;
|
---|
| 371 | }
|
---|
| 372 | case OpCodes.ExponentialIntegralEi: {
|
---|
| 373 | CompileInstructions(il, state, ds);
|
---|
| 374 | il.Emit(System.Reflection.Emit.OpCodes.Call, expIntegralEi);
|
---|
| 375 | return;
|
---|
| 376 | }
|
---|
| 377 | case OpCodes.FresnelCosineIntegral: {
|
---|
| 378 | CompileInstructions(il, state, ds);
|
---|
| 379 | il.Emit(System.Reflection.Emit.OpCodes.Call, fresnelCosIntegral);
|
---|
| 380 | return;
|
---|
| 381 | }
|
---|
| 382 | case OpCodes.FresnelSineIntegral: {
|
---|
| 383 | CompileInstructions(il, state, ds);
|
---|
| 384 | il.Emit(System.Reflection.Emit.OpCodes.Call, fresnelSinIntegral);
|
---|
| 385 | return;
|
---|
| 386 | }
|
---|
| 387 | case OpCodes.Gamma: {
|
---|
| 388 | CompileInstructions(il, state, ds);
|
---|
| 389 | il.Emit(System.Reflection.Emit.OpCodes.Call, gamma);
|
---|
| 390 | return;
|
---|
| 391 | }
|
---|
| 392 | case OpCodes.HyperbolicCosineIntegral: {
|
---|
| 393 | CompileInstructions(il, state, ds);
|
---|
| 394 | il.Emit(System.Reflection.Emit.OpCodes.Call, hypCosIntegral);
|
---|
| 395 | return;
|
---|
| 396 | }
|
---|
| 397 | case OpCodes.HyperbolicSineIntegral: {
|
---|
| 398 | CompileInstructions(il, state, ds);
|
---|
| 399 | il.Emit(System.Reflection.Emit.OpCodes.Call, hypSinIntegral);
|
---|
| 400 | return;
|
---|
| 401 | }
|
---|
| 402 | case OpCodes.Norm: {
|
---|
| 403 | CompileInstructions(il, state, ds);
|
---|
| 404 | il.Emit(System.Reflection.Emit.OpCodes.Call, norm);
|
---|
| 405 | return;
|
---|
| 406 | }
|
---|
| 407 | case OpCodes.Psi: {
|
---|
| 408 | CompileInstructions(il, state, ds);
|
---|
| 409 | il.Emit(System.Reflection.Emit.OpCodes.Call, psi);
|
---|
| 410 | return;
|
---|
| 411 | }
|
---|
| 412 | case OpCodes.SineIntegral: {
|
---|
| 413 | CompileInstructions(il, state, ds);
|
---|
| 414 | il.Emit(System.Reflection.Emit.OpCodes.Call, sinIntegral);
|
---|
| 415 | return;
|
---|
| 416 | }
|
---|
[16360] | 417 | case OpCodes.AnalyticQuotient: {
|
---|
[16356] | 418 | CompileInstructions(il, state, ds); // x1
|
---|
| 419 | CompileInstructions(il, state, ds); // x2
|
---|
| 420 |
|
---|
| 421 | il.Emit(System.Reflection.Emit.OpCodes.Dup);
|
---|
| 422 | il.Emit(System.Reflection.Emit.OpCodes.Mul); // x2*x2
|
---|
| 423 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0);
|
---|
| 424 | il.Emit(System.Reflection.Emit.OpCodes.Mul); // 1+x2*x2
|
---|
| 425 | il.Emit(System.Reflection.Emit.OpCodes.Call, sqrt);
|
---|
| 426 | il.Emit(System.Reflection.Emit.OpCodes.Div);
|
---|
| 427 | return;
|
---|
| 428 | }
|
---|
[6732] | 429 | case OpCodes.IfThenElse: {
|
---|
| 430 | Label end = il.DefineLabel();
|
---|
| 431 | Label c1 = il.DefineLabel();
|
---|
[6809] | 432 | CompileInstructions(il, state, ds);
|
---|
[16585] | 433 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 0.0); // > 0
|
---|
[6732] | 434 | il.Emit(System.Reflection.Emit.OpCodes.Cgt);
|
---|
| 435 | il.Emit(System.Reflection.Emit.OpCodes.Brfalse, c1);
|
---|
[6809] | 436 | CompileInstructions(il, state, ds);
|
---|
[6732] | 437 | il.Emit(System.Reflection.Emit.OpCodes.Br, end);
|
---|
| 438 | il.MarkLabel(c1);
|
---|
[6809] | 439 | CompileInstructions(il, state, ds);
|
---|
[6732] | 440 | il.MarkLabel(end);
|
---|
| 441 | return;
|
---|
| 442 | }
|
---|
| 443 | case OpCodes.AND: {
|
---|
| 444 | Label falseBranch = il.DefineLabel();
|
---|
| 445 | Label end = il.DefineLabel();
|
---|
[6809] | 446 | CompileInstructions(il, state, ds);
|
---|
[6732] | 447 | for (int i = 1; i < nArgs; i++) {
|
---|
[16585] | 448 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 0.0); // > 0
|
---|
[6732] | 449 | il.Emit(System.Reflection.Emit.OpCodes.Cgt);
|
---|
| 450 | il.Emit(System.Reflection.Emit.OpCodes.Brfalse, falseBranch);
|
---|
[6809] | 451 | CompileInstructions(il, state, ds);
|
---|
[6732] | 452 | }
|
---|
[16585] | 453 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 0.0); // > 0
|
---|
[6732] | 454 | il.Emit(System.Reflection.Emit.OpCodes.Cgt);
|
---|
| 455 | il.Emit(System.Reflection.Emit.OpCodes.Brfalse, falseBranch);
|
---|
| 456 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // 1
|
---|
| 457 | il.Emit(System.Reflection.Emit.OpCodes.Br, end);
|
---|
| 458 | il.MarkLabel(falseBranch);
|
---|
| 459 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // -1
|
---|
| 460 | il.Emit(System.Reflection.Emit.OpCodes.Neg);
|
---|
| 461 | il.MarkLabel(end);
|
---|
| 462 | return;
|
---|
| 463 | }
|
---|
| 464 | case OpCodes.OR: {
|
---|
| 465 | Label trueBranch = il.DefineLabel();
|
---|
| 466 | Label end = il.DefineLabel();
|
---|
| 467 | Label resultBranch = il.DefineLabel();
|
---|
[6809] | 468 | CompileInstructions(il, state, ds);
|
---|
[6732] | 469 | for (int i = 1; i < nArgs; i++) {
|
---|
| 470 | Label nextArgBranch = il.DefineLabel();
|
---|
| 471 | // complex definition because of special properties of NaN
|
---|
| 472 | il.Emit(System.Reflection.Emit.OpCodes.Dup);
|
---|
[16585] | 473 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 0.0); // <= 0
|
---|
[6732] | 474 | il.Emit(System.Reflection.Emit.OpCodes.Ble, nextArgBranch);
|
---|
| 475 | il.Emit(System.Reflection.Emit.OpCodes.Br, resultBranch);
|
---|
| 476 | il.MarkLabel(nextArgBranch);
|
---|
| 477 | il.Emit(System.Reflection.Emit.OpCodes.Pop);
|
---|
[6809] | 478 | CompileInstructions(il, state, ds);
|
---|
[6732] | 479 | }
|
---|
| 480 | il.MarkLabel(resultBranch);
|
---|
[16585] | 481 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 0.0); // > 0
|
---|
[6732] | 482 | il.Emit(System.Reflection.Emit.OpCodes.Cgt);
|
---|
| 483 | il.Emit(System.Reflection.Emit.OpCodes.Brtrue, trueBranch);
|
---|
| 484 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // -1
|
---|
| 485 | il.Emit(System.Reflection.Emit.OpCodes.Neg);
|
---|
| 486 | il.Emit(System.Reflection.Emit.OpCodes.Br, end);
|
---|
| 487 | il.MarkLabel(trueBranch);
|
---|
| 488 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // 1
|
---|
| 489 | il.MarkLabel(end);
|
---|
| 490 | return;
|
---|
| 491 | }
|
---|
| 492 | case OpCodes.NOT: {
|
---|
[6809] | 493 | CompileInstructions(il, state, ds);
|
---|
[16585] | 494 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 0.0); // > 0
|
---|
[6732] | 495 | il.Emit(System.Reflection.Emit.OpCodes.Cgt);
|
---|
[14809] | 496 | il.Emit(System.Reflection.Emit.OpCodes.Conv_R8); // convert to float64
|
---|
[6732] | 497 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); // * 2
|
---|
| 498 | il.Emit(System.Reflection.Emit.OpCodes.Mul);
|
---|
| 499 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // - 1
|
---|
| 500 | il.Emit(System.Reflection.Emit.OpCodes.Sub);
|
---|
| 501 | il.Emit(System.Reflection.Emit.OpCodes.Neg); // * -1
|
---|
| 502 | return;
|
---|
| 503 | }
|
---|
[10791] | 504 | case OpCodes.XOR: {
|
---|
| 505 | CompileInstructions(il, state, ds);
|
---|
[16585] | 506 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 0.0);
|
---|
[10791] | 507 | il.Emit(System.Reflection.Emit.OpCodes.Cgt);// > 0
|
---|
| 508 |
|
---|
| 509 | for (int i = 1; i < nArgs; i++) {
|
---|
| 510 | CompileInstructions(il, state, ds);
|
---|
[16585] | 511 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 0.0);
|
---|
[10791] | 512 | il.Emit(System.Reflection.Emit.OpCodes.Cgt);// > 0
|
---|
| 513 | il.Emit(System.Reflection.Emit.OpCodes.Xor);
|
---|
| 514 | }
|
---|
[14809] | 515 | il.Emit(System.Reflection.Emit.OpCodes.Conv_R8); // convert to float64
|
---|
| 516 |
|
---|
[10791] | 517 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); // * 2
|
---|
| 518 | il.Emit(System.Reflection.Emit.OpCodes.Mul);
|
---|
| 519 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // - 1
|
---|
| 520 | il.Emit(System.Reflection.Emit.OpCodes.Sub);
|
---|
| 521 | return;
|
---|
| 522 | }
|
---|
[6732] | 523 | case OpCodes.GT: {
|
---|
[6809] | 524 | CompileInstructions(il, state, ds);
|
---|
| 525 | CompileInstructions(il, state, ds);
|
---|
| 526 |
|
---|
[6732] | 527 | il.Emit(System.Reflection.Emit.OpCodes.Cgt); // 1 (>) / 0 (otherwise)
|
---|
[14809] | 528 | il.Emit(System.Reflection.Emit.OpCodes.Conv_R8); // convert to float64
|
---|
[6732] | 529 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); // * 2
|
---|
| 530 | il.Emit(System.Reflection.Emit.OpCodes.Mul);
|
---|
| 531 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // - 1
|
---|
| 532 | il.Emit(System.Reflection.Emit.OpCodes.Sub);
|
---|
| 533 | return;
|
---|
| 534 | }
|
---|
| 535 | case OpCodes.LT: {
|
---|
[6809] | 536 | CompileInstructions(il, state, ds);
|
---|
| 537 | CompileInstructions(il, state, ds);
|
---|
[6732] | 538 | il.Emit(System.Reflection.Emit.OpCodes.Clt);
|
---|
[14809] | 539 | il.Emit(System.Reflection.Emit.OpCodes.Conv_R8); // convert to float64
|
---|
[6732] | 540 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); // * 2
|
---|
| 541 | il.Emit(System.Reflection.Emit.OpCodes.Mul);
|
---|
| 542 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // - 1
|
---|
| 543 | il.Emit(System.Reflection.Emit.OpCodes.Sub);
|
---|
| 544 | return;
|
---|
| 545 | }
|
---|
| 546 | case OpCodes.TimeLag: {
|
---|
[6809] | 547 | LaggedTreeNode laggedTreeNode = (LaggedTreeNode)currentInstr.dynamicNode;
|
---|
| 548 | il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row -= lag
|
---|
| 549 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, laggedTreeNode.Lag);
|
---|
| 550 | il.Emit(System.Reflection.Emit.OpCodes.Add);
|
---|
| 551 | il.Emit(System.Reflection.Emit.OpCodes.Starg, 0);
|
---|
[6849] | 552 | var prevLaggedContext = state.InLaggedContext;
|
---|
| 553 | state.InLaggedContext = true;
|
---|
[6809] | 554 | CompileInstructions(il, state, ds);
|
---|
| 555 | il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row += lag
|
---|
| 556 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, laggedTreeNode.Lag);
|
---|
| 557 | il.Emit(System.Reflection.Emit.OpCodes.Sub);
|
---|
| 558 | il.Emit(System.Reflection.Emit.OpCodes.Starg, 0);
|
---|
[6849] | 559 | state.InLaggedContext = prevLaggedContext;
|
---|
[6809] | 560 | return;
|
---|
[6732] | 561 | }
|
---|
| 562 | case OpCodes.Integral: {
|
---|
[6809] | 563 | int savedPc = state.ProgramCounter;
|
---|
| 564 | LaggedTreeNode laggedTreeNode = (LaggedTreeNode)currentInstr.dynamicNode;
|
---|
| 565 | il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row -= lag
|
---|
| 566 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, laggedTreeNode.Lag);
|
---|
| 567 | il.Emit(System.Reflection.Emit.OpCodes.Add);
|
---|
| 568 | il.Emit(System.Reflection.Emit.OpCodes.Starg, 0);
|
---|
[6849] | 569 | var prevLaggedContext = state.InLaggedContext;
|
---|
| 570 | state.InLaggedContext = true;
|
---|
[6809] | 571 | CompileInstructions(il, state, ds);
|
---|
| 572 | for (int l = laggedTreeNode.Lag; l < 0; l++) {
|
---|
| 573 | il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row += lag
|
---|
| 574 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_1);
|
---|
| 575 | il.Emit(System.Reflection.Emit.OpCodes.Add);
|
---|
| 576 | il.Emit(System.Reflection.Emit.OpCodes.Starg, 0);
|
---|
| 577 | state.ProgramCounter = savedPc;
|
---|
| 578 | CompileInstructions(il, state, ds);
|
---|
| 579 | il.Emit(System.Reflection.Emit.OpCodes.Add);
|
---|
| 580 | }
|
---|
[6849] | 581 | state.InLaggedContext = prevLaggedContext;
|
---|
[6809] | 582 | return;
|
---|
[6732] | 583 | }
|
---|
| 584 |
|
---|
| 585 | //mkommend: derivate calculation taken from:
|
---|
| 586 | //http://www.holoborodko.com/pavel/numerical-methods/numerical-derivative/smooth-low-noise-differentiators/
|
---|
| 587 | //one sided smooth differentiatior, N = 4
|
---|
| 588 | // y' = 1/8h (f_i + 2f_i-1, -2 f_i-3 - f_i-4)
|
---|
| 589 | case OpCodes.Derivative: {
|
---|
[6809] | 590 | int savedPc = state.ProgramCounter;
|
---|
| 591 | CompileInstructions(il, state, ds);
|
---|
| 592 | il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row --
|
---|
| 593 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_M1);
|
---|
| 594 | il.Emit(System.Reflection.Emit.OpCodes.Add);
|
---|
| 595 | il.Emit(System.Reflection.Emit.OpCodes.Starg, 0);
|
---|
| 596 | state.ProgramCounter = savedPc;
|
---|
[6849] | 597 | var prevLaggedContext = state.InLaggedContext;
|
---|
| 598 | state.InLaggedContext = true;
|
---|
[6809] | 599 | CompileInstructions(il, state, ds);
|
---|
| 600 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); // f_0 + 2 * f_1
|
---|
| 601 | il.Emit(System.Reflection.Emit.OpCodes.Mul);
|
---|
| 602 | il.Emit(System.Reflection.Emit.OpCodes.Add);
|
---|
| 603 |
|
---|
| 604 | il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row -=2
|
---|
| 605 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_2);
|
---|
| 606 | il.Emit(System.Reflection.Emit.OpCodes.Sub);
|
---|
| 607 | il.Emit(System.Reflection.Emit.OpCodes.Starg, 0);
|
---|
| 608 | state.ProgramCounter = savedPc;
|
---|
| 609 | CompileInstructions(il, state, ds);
|
---|
| 610 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); // f_0 + 2 * f_1 - 2 * f_3
|
---|
| 611 | il.Emit(System.Reflection.Emit.OpCodes.Mul);
|
---|
| 612 | il.Emit(System.Reflection.Emit.OpCodes.Sub);
|
---|
| 613 |
|
---|
| 614 | il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row --
|
---|
| 615 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_M1);
|
---|
| 616 | il.Emit(System.Reflection.Emit.OpCodes.Add);
|
---|
| 617 | il.Emit(System.Reflection.Emit.OpCodes.Starg, 0);
|
---|
| 618 | state.ProgramCounter = savedPc;
|
---|
| 619 | CompileInstructions(il, state, ds);
|
---|
| 620 | il.Emit(System.Reflection.Emit.OpCodes.Sub); // f_0 + 2 * f_1 - 2 * f_3 - f_4
|
---|
| 621 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 8.0); // / 8
|
---|
| 622 | il.Emit(System.Reflection.Emit.OpCodes.Div);
|
---|
| 623 |
|
---|
| 624 | il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row +=4
|
---|
| 625 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_4);
|
---|
| 626 | il.Emit(System.Reflection.Emit.OpCodes.Add);
|
---|
| 627 | il.Emit(System.Reflection.Emit.OpCodes.Starg, 0);
|
---|
[6849] | 628 | state.InLaggedContext = prevLaggedContext;
|
---|
[6809] | 629 | return;
|
---|
[6732] | 630 | }
|
---|
| 631 | case OpCodes.Call: {
|
---|
[7842] | 632 | throw new NotSupportedException(
|
---|
| 633 | "Automatically defined functions are not supported by the SymbolicDataAnalysisTreeILEmittingInterpreter. Either turn of ADFs or change the interpeter.");
|
---|
[6732] | 634 | }
|
---|
| 635 | case OpCodes.Arg: {
|
---|
[7842] | 636 | throw new NotSupportedException(
|
---|
| 637 | "Automatically defined functions are not supported by the SymbolicDataAnalysisTreeILEmittingInterpreter. Either turn of ADFs or change the interpeter.");
|
---|
[6732] | 638 | }
|
---|
| 639 | case OpCodes.Variable: {
|
---|
[6741] | 640 | VariableTreeNode varNode = (VariableTreeNode)currentInstr.dynamicNode;
|
---|
[8798] | 641 | il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); // load columns array
|
---|
[9828] | 642 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, (int)currentInstr.data);
|
---|
[8798] | 643 | // load correct column of the current variable
|
---|
| 644 | il.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref);
|
---|
| 645 | il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex
|
---|
[6849] | 646 | if (!state.InLaggedContext) {
|
---|
| 647 | il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue);
|
---|
| 648 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, varNode.Weight); // load weight
|
---|
| 649 | il.Emit(System.Reflection.Emit.OpCodes.Mul);
|
---|
| 650 | } else {
|
---|
| 651 | var nanResult = il.DefineLabel();
|
---|
| 652 | var normalResult = il.DefineLabel();
|
---|
| 653 | il.Emit(System.Reflection.Emit.OpCodes.Dup);
|
---|
| 654 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0);
|
---|
| 655 | il.Emit(System.Reflection.Emit.OpCodes.Blt, nanResult);
|
---|
| 656 | il.Emit(System.Reflection.Emit.OpCodes.Dup);
|
---|
| 657 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, ds.Rows);
|
---|
| 658 | il.Emit(System.Reflection.Emit.OpCodes.Bge, nanResult);
|
---|
| 659 | il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue);
|
---|
| 660 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, varNode.Weight); // load weight
|
---|
| 661 | il.Emit(System.Reflection.Emit.OpCodes.Mul);
|
---|
| 662 | il.Emit(System.Reflection.Emit.OpCodes.Br, normalResult);
|
---|
| 663 | il.MarkLabel(nanResult);
|
---|
| 664 | il.Emit(System.Reflection.Emit.OpCodes.Pop); // rowIndex
|
---|
[8798] | 665 | il.Emit(System.Reflection.Emit.OpCodes.Pop); // column reference
|
---|
[6849] | 666 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, double.NaN);
|
---|
| 667 | il.MarkLabel(normalResult);
|
---|
| 668 | }
|
---|
[6732] | 669 | return;
|
---|
| 670 | }
|
---|
| 671 | case OpCodes.LagVariable: {
|
---|
[6809] | 672 | var nanResult = il.DefineLabel();
|
---|
| 673 | var normalResult = il.DefineLabel();
|
---|
[6770] | 674 | LaggedVariableTreeNode varNode = (LaggedVariableTreeNode)currentInstr.dynamicNode;
|
---|
[8798] | 675 | il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); // load columns array
|
---|
[9828] | 676 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, (int)currentInstr.data);
|
---|
[8798] | 677 | // load correct column of the current variable
|
---|
| 678 | il.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref);
|
---|
[6770] | 679 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, varNode.Lag); // lag
|
---|
[6849] | 680 | il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex
|
---|
| 681 | il.Emit(System.Reflection.Emit.OpCodes.Add); // actualRowIndex = rowIndex + sampleOffset
|
---|
[6809] | 682 | il.Emit(System.Reflection.Emit.OpCodes.Dup);
|
---|
| 683 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0);
|
---|
| 684 | il.Emit(System.Reflection.Emit.OpCodes.Blt, nanResult);
|
---|
| 685 | il.Emit(System.Reflection.Emit.OpCodes.Dup);
|
---|
| 686 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, ds.Rows);
|
---|
| 687 | il.Emit(System.Reflection.Emit.OpCodes.Bge, nanResult);
|
---|
[6770] | 688 | il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue);
|
---|
| 689 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, varNode.Weight); // load weight
|
---|
| 690 | il.Emit(System.Reflection.Emit.OpCodes.Mul);
|
---|
[6809] | 691 | il.Emit(System.Reflection.Emit.OpCodes.Br, normalResult);
|
---|
| 692 | il.MarkLabel(nanResult);
|
---|
[8798] | 693 | il.Emit(System.Reflection.Emit.OpCodes.Pop); // sample index
|
---|
| 694 | il.Emit(System.Reflection.Emit.OpCodes.Pop); // column reference
|
---|
[6809] | 695 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, double.NaN);
|
---|
| 696 | il.MarkLabel(normalResult);
|
---|
[6770] | 697 | return;
|
---|
[6732] | 698 | }
|
---|
| 699 | case OpCodes.Constant: {
|
---|
| 700 | ConstantTreeNode constNode = (ConstantTreeNode)currentInstr.dynamicNode;
|
---|
| 701 | il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, constNode.Value);
|
---|
| 702 | return;
|
---|
| 703 | }
|
---|
| 704 |
|
---|
| 705 | //mkommend: this symbol uses the logistic function f(x) = 1 / (1 + e^(-alpha * x) )
|
---|
| 706 | //to determine the relative amounts of the true and false branch see http://en.wikipedia.org/wiki/Logistic_function
|
---|
| 707 | case OpCodes.VariableCondition: {
|
---|
[7842] | 708 | throw new NotSupportedException("Interpretation of symbol " + currentInstr.dynamicNode.Symbol.Name +
|
---|
| 709 | " is not supported by the SymbolicDataAnalysisTreeILEmittingInterpreter");
|
---|
[6732] | 710 | }
|
---|
[7842] | 711 | default:
|
---|
| 712 | throw new NotSupportedException("Interpretation of symbol " + currentInstr.dynamicNode.Symbol.Name +
|
---|
| 713 | " is not supported by the SymbolicDataAnalysisTreeILEmittingInterpreter");
|
---|
[6732] | 714 | }
|
---|
| 715 | }
|
---|
| 716 |
|
---|
[7842] | 717 | public static double AiryA(double x) {
|
---|
| 718 | if (double.IsNaN(x)) return double.NaN;
|
---|
| 719 | double ai, aip, bi, bip;
|
---|
| 720 | alglib.airy(x, out ai, out aip, out bi, out bip);
|
---|
| 721 | return ai;
|
---|
| 722 | }
|
---|
| 723 |
|
---|
| 724 | public static double AiryB(double x) {
|
---|
| 725 | if (double.IsNaN(x)) return double.NaN;
|
---|
| 726 | double ai, aip, bi, bip;
|
---|
| 727 | alglib.airy(x, out ai, out aip, out bi, out bip);
|
---|
| 728 | return bi;
|
---|
| 729 | }
|
---|
| 730 | public static double Dawson(double x) {
|
---|
| 731 | if (double.IsNaN(x)) return double.NaN;
|
---|
| 732 | return alglib.dawsonintegral(x);
|
---|
| 733 | }
|
---|
| 734 |
|
---|
| 735 | public static double Gamma(double x) {
|
---|
| 736 | if (double.IsNaN(x)) return double.NaN;
|
---|
| 737 | return alglib.gammafunction(x);
|
---|
| 738 | }
|
---|
| 739 |
|
---|
| 740 | public static double Psi(double x) {
|
---|
| 741 | if (double.IsNaN(x)) return double.NaN;
|
---|
[8430] | 742 | else if (x <= 0 && (Math.Floor(x) - x).IsAlmost(0)) return double.NaN;
|
---|
[7842] | 743 | return alglib.psi(x);
|
---|
| 744 | }
|
---|
| 745 |
|
---|
| 746 | public static double ExpIntegralEi(double x) {
|
---|
| 747 | if (double.IsNaN(x)) return double.NaN;
|
---|
| 748 | return alglib.exponentialintegralei(x);
|
---|
| 749 | }
|
---|
| 750 |
|
---|
| 751 | public static double SinIntegral(double x) {
|
---|
| 752 | if (double.IsNaN(x)) return double.NaN;
|
---|
| 753 | double si, ci;
|
---|
| 754 | alglib.sinecosineintegrals(x, out si, out ci);
|
---|
| 755 | return si;
|
---|
| 756 | }
|
---|
| 757 |
|
---|
| 758 | public static double CosIntegral(double x) {
|
---|
| 759 | if (double.IsNaN(x)) return double.NaN;
|
---|
| 760 | double si, ci;
|
---|
| 761 | alglib.sinecosineintegrals(x, out si, out ci);
|
---|
| 762 | return ci;
|
---|
| 763 | }
|
---|
| 764 |
|
---|
| 765 | public static double HypSinIntegral(double x) {
|
---|
| 766 | if (double.IsNaN(x)) return double.NaN;
|
---|
| 767 | double shi, chi;
|
---|
| 768 | alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
|
---|
[8430] | 769 | return shi;
|
---|
[7842] | 770 | }
|
---|
| 771 |
|
---|
| 772 | public static double HypCosIntegral(double x) {
|
---|
| 773 | if (double.IsNaN(x)) return double.NaN;
|
---|
| 774 | double shi, chi;
|
---|
| 775 | alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
|
---|
| 776 | return chi;
|
---|
| 777 | }
|
---|
| 778 |
|
---|
| 779 | public static double FresnelCosIntegral(double x) {
|
---|
| 780 | if (double.IsNaN(x)) return double.NaN;
|
---|
| 781 | double c = 0, s = 0;
|
---|
| 782 | alglib.fresnelintegral(x, ref c, ref s);
|
---|
| 783 | return c;
|
---|
| 784 | }
|
---|
| 785 |
|
---|
| 786 | public static double FresnelSinIntegral(double x) {
|
---|
| 787 | if (double.IsNaN(x)) return double.NaN;
|
---|
| 788 | double c = 0, s = 0;
|
---|
| 789 | alglib.fresnelintegral(x, ref c, ref s);
|
---|
| 790 | return s;
|
---|
| 791 | }
|
---|
| 792 |
|
---|
| 793 | public static double Norm(double x) {
|
---|
| 794 | if (double.IsNaN(x)) return double.NaN;
|
---|
| 795 | return alglib.normaldistribution(x);
|
---|
| 796 | }
|
---|
| 797 |
|
---|
| 798 | public static double Erf(double x) {
|
---|
| 799 | if (double.IsNaN(x)) return double.NaN;
|
---|
| 800 | return alglib.errorfunction(x);
|
---|
| 801 | }
|
---|
| 802 |
|
---|
| 803 | public static double Bessel(double x) {
|
---|
| 804 | if (double.IsNaN(x)) return double.NaN;
|
---|
| 805 | return alglib.besseli0(x);
|
---|
| 806 | }
|
---|
[6732] | 807 | }
|
---|
| 808 | }
|
---|