[12807] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[12807] | 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.Linq;
|
---|
| 25 | using System.Linq.Expressions;
|
---|
| 26 | using System.Reflection;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Data;
|
---|
| 30 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 31 | using HeuristicLab.Parameters;
|
---|
| 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 33 |
|
---|
| 34 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
| 35 | [StorableClass]
|
---|
| 36 | [Item("SymbolicDataAnalysisExpressionCompiledTreeInterpreter", "Interpreter that converts the tree into a Linq.Expression then compiles it.")]
|
---|
| 37 | public sealed class SymbolicDataAnalysisExpressionCompiledTreeInterpreter : ParameterizedNamedItem, ISymbolicDataAnalysisExpressionTreeInterpreter {
|
---|
| 38 | private const string CheckExpressionsWithIntervalArithmeticParameterName = "CheckExpressionsWithIntervalArithmetic";
|
---|
[13222] | 39 | private const string CheckExpressionsWithIntervalArithmeticParameterDescription = "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.";
|
---|
[12807] | 40 | private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
|
---|
| 41 |
|
---|
| 42 | #region method info for the commonly called functions
|
---|
| 43 | private static readonly MethodInfo Sin = typeof(Math).GetMethod("Sin", new[] { typeof(double) });
|
---|
| 44 | private static readonly MethodInfo Cos = typeof(Math).GetMethod("Cos", new[] { typeof(double) });
|
---|
| 45 | private static readonly MethodInfo Tan = typeof(Math).GetMethod("Tan", new[] { typeof(double) });
|
---|
| 46 | private static readonly MethodInfo Sqrt = typeof(Math).GetMethod("Sqrt", new[] { typeof(double) });
|
---|
| 47 | private static readonly MethodInfo Floor = typeof(Math).GetMethod("Floor", new[] { typeof(double) });
|
---|
[13288] | 48 | private static readonly MethodInfo Round = typeof(Math).GetMethod("Round", new[] { typeof(double) });
|
---|
[12807] | 49 | private static readonly MethodInfo Exp = typeof(Math).GetMethod("Exp", new[] { typeof(double) });
|
---|
| 50 | private static readonly MethodInfo Log = typeof(Math).GetMethod("Log", new[] { typeof(double) });
|
---|
| 51 | private static readonly MethodInfo IsNaN = typeof(double).GetMethod("IsNaN");
|
---|
[13288] | 52 | private static readonly MethodInfo IsAlmost = typeof(DoubleExtensions).GetMethod("IsAlmost");
|
---|
[12807] | 53 | private static readonly MethodInfo Gamma = typeof(alglib).GetMethod("gammafunction", new[] { typeof(double) });
|
---|
| 54 | private static readonly MethodInfo Psi = typeof(alglib).GetMethod("psi", new[] { typeof(double) });
|
---|
| 55 | private static readonly MethodInfo DawsonIntegral = typeof(alglib).GetMethod("dawsonintegral", new[] { typeof(double) });
|
---|
| 56 | private static readonly MethodInfo ExponentialIntegralEi = typeof(alglib).GetMethod("exponentialintegralei", new[] { typeof(double) });
|
---|
| 57 | private static readonly MethodInfo SineCosineIntegrals = typeof(alglib).GetMethod("sinecosineintegrals", new[] { typeof(double), typeof(double).MakeByRefType(), typeof(double).MakeByRefType() });
|
---|
| 58 | private static readonly MethodInfo HyperbolicSineCosineIntegrals = typeof(alglib).GetMethod("hyperbolicsinecosineintegrals", new[] { typeof(double), typeof(double).MakeByRefType(), typeof(double).MakeByRefType() });
|
---|
| 59 | private static readonly MethodInfo FresnelIntegral = typeof(alglib).GetMethod("fresnelintegral", new[] { typeof(double), typeof(double).MakeByRefType(), typeof(double).MakeByRefType() });
|
---|
| 60 | private static readonly MethodInfo Airy = typeof(alglib).GetMethod("airy", new[] { typeof(double), typeof(double).MakeByRefType(), typeof(double).MakeByRefType(), typeof(double).MakeByRefType(), typeof(double).MakeByRefType() });
|
---|
| 61 | private static readonly MethodInfo NormalDistribution = typeof(alglib).GetMethod("normaldistribution", new[] { typeof(double) });
|
---|
| 62 | private static readonly MethodInfo ErrorFunction = typeof(alglib).GetMethod("errorfunction", new[] { typeof(double) });
|
---|
| 63 | private static readonly MethodInfo Bessel = typeof(alglib).GetMethod("besseli0", new[] { typeof(double) });
|
---|
| 64 | #endregion
|
---|
| 65 |
|
---|
| 66 | public override bool CanChangeName { get { return false; } }
|
---|
| 67 | public override bool CanChangeDescription { get { return false; } }
|
---|
| 68 |
|
---|
| 69 | #region parameter properties
|
---|
[13141] | 70 | public IFixedValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter {
|
---|
| 71 | get { return (IFixedValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }
|
---|
[12807] | 72 | }
|
---|
| 73 |
|
---|
[13141] | 74 | public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
| 75 | get { return (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
|
---|
[12807] | 76 | }
|
---|
| 77 | #endregion
|
---|
| 78 |
|
---|
| 79 | #region properties
|
---|
[13141] | 80 | public bool CheckExpressionsWithIntervalArithmetic {
|
---|
| 81 | get { return CheckExpressionsWithIntervalArithmeticParameter.Value.Value; }
|
---|
| 82 | set { CheckExpressionsWithIntervalArithmeticParameter.Value.Value = value; }
|
---|
[12807] | 83 | }
|
---|
[13141] | 84 | public int EvaluatedSolutions {
|
---|
| 85 | get { return EvaluatedSolutionsParameter.Value.Value; }
|
---|
| 86 | set { EvaluatedSolutionsParameter.Value.Value = value; }
|
---|
[12807] | 87 | }
|
---|
| 88 | #endregion
|
---|
| 89 |
|
---|
| 90 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 91 | return new SymbolicDataAnalysisExpressionCompiledTreeInterpreter(this, cloner);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | private SymbolicDataAnalysisExpressionCompiledTreeInterpreter(SymbolicDataAnalysisExpressionCompiledTreeInterpreter original, Cloner cloner)
|
---|
| 95 | : base(original, cloner) {
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | [StorableConstructor]
|
---|
| 99 | private SymbolicDataAnalysisExpressionCompiledTreeInterpreter(bool deserializing)
|
---|
| 100 | : base(deserializing) {
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[13141] | 103 | public SymbolicDataAnalysisExpressionCompiledTreeInterpreter() :
|
---|
| 104 | base("SymbolicDataAnalysisExpressionCompiledTreeInterpreter", "Interpreter which compiles the tree into a lambda") {
|
---|
[13222] | 105 | Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, CheckExpressionsWithIntervalArithmeticParameterDescription, new BoolValue(false)));
|
---|
[13141] | 106 | Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
|
---|
[12807] | 107 | }
|
---|
| 108 |
|
---|
[13141] | 109 | public SymbolicDataAnalysisExpressionCompiledTreeInterpreter(string name, string description) :
|
---|
| 110 | base(name, description) {
|
---|
[13222] | 111 | Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, CheckExpressionsWithIntervalArithmeticParameterDescription, new BoolValue(false)));
|
---|
[13141] | 112 | Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[12807] | 115 | public void InitializeState() {
|
---|
[13141] | 116 | EvaluatedSolutions = 0;
|
---|
[12807] | 117 | }
|
---|
| 118 |
|
---|
[13222] | 119 | public void ClearState() { }
|
---|
[12807] | 120 |
|
---|
[13251] | 121 | private readonly object syncRoot = new object();
|
---|
[12807] | 122 | public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) {
|
---|
[13141] | 123 | if (CheckExpressionsWithIntervalArithmetic)
|
---|
[12807] | 124 | throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter.");
|
---|
| 125 |
|
---|
[13251] | 126 | lock (syncRoot) {
|
---|
[13141] | 127 | EvaluatedSolutions++; // increment the evaluated solutions counter
|
---|
[12807] | 128 | }
|
---|
[13222] | 129 | var columns = dataset.DoubleVariables.Select(x => (IList<double>)dataset.GetReadOnlyDoubleValues(x)).ToArray();
|
---|
[12807] | 130 | var compiled = CompileTree(tree, dataset);
|
---|
[13140] | 131 | return rows.Select(x => compiled(x, columns));
|
---|
[12807] | 132 | }
|
---|
| 133 |
|
---|
[13222] | 134 | public static Func<int, IList<double>[], double> CompileTree(ISymbolicExpressionTree tree, IDataset dataset) {
|
---|
[13313] | 135 | var lambda = CreateDelegate(tree, dataset);
|
---|
| 136 | return lambda.Compile();
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | public static Expression<Func<int, IList<double>[], double>> CreateDelegate(ISymbolicExpressionTree tree, IDataset dataset) {
|
---|
[12807] | 140 | var row = Expression.Parameter(typeof(int));
|
---|
[13222] | 141 | var columns = Expression.Parameter(typeof(IList<double>[]));
|
---|
[13140] | 142 | var variableIndices = dataset.DoubleVariables.Select((x, i) => new { x, i }).ToDictionary(e => e.x, e => e.i);
|
---|
| 143 | var expr = MakeExpr(tree, variableIndices, row, columns);
|
---|
[13222] | 144 | var lambda = Expression.Lambda<Func<int, IList<double>[], double>>(expr, row, columns);
|
---|
[13313] | 145 | return lambda;
|
---|
[12807] | 146 | }
|
---|
| 147 |
|
---|
[13141] | 148 | private static Expression MakeExpr(ISymbolicExpressionTree tree, Dictionary<string, int> variableIndices, Expression row, Expression columns) {
|
---|
[12807] | 149 | var actualRoot = tree.Root.GetSubtree(0).GetSubtree(0);
|
---|
[13140] | 150 | return MakeExpr(actualRoot, variableIndices, row, columns);
|
---|
[12807] | 151 | }
|
---|
| 152 |
|
---|
[13222] | 153 | private static readonly PropertyInfo Indexer = typeof(IList<double>).GetProperty("Item");
|
---|
[13141] | 154 | private static Expression MakeExpr(ISymbolicExpressionTreeNode node, Dictionary<string, int> variableIndices, Expression row, Expression columns) {
|
---|
[12807] | 155 | var opcode = OpCodes.MapSymbolToOpCode(node);
|
---|
[13141] | 156 | #region switch opcode
|
---|
[12807] | 157 | switch (opcode) {
|
---|
[13222] | 158 | case OpCodes.Constant: {
|
---|
[12807] | 159 | var constantTreeNode = (ConstantTreeNode)node;
|
---|
| 160 | return Expression.Constant(constantTreeNode.Value);
|
---|
| 161 | }
|
---|
[13222] | 162 | case OpCodes.Variable: {
|
---|
[12807] | 163 | var variableTreeNode = (VariableTreeNode)node;
|
---|
| 164 | var variableWeight = Expression.Constant(variableTreeNode.Weight);
|
---|
[13140] | 165 | var variableName = variableTreeNode.VariableName;
|
---|
| 166 | var indexExpr = Expression.Constant(variableIndices[variableName]);
|
---|
| 167 | var valuesExpr = Expression.ArrayIndex(columns, indexExpr);
|
---|
[13288] | 168 | return Expression.Multiply(variableWeight, Expression.Property(valuesExpr, Indexer, row));
|
---|
[12807] | 169 | }
|
---|
[13222] | 170 | case OpCodes.Add: {
|
---|
[13140] | 171 | Expression result = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 172 | for (int i = 1; i < node.SubtreeCount; ++i) {
|
---|
[13140] | 173 | result = Expression.Add(result, MakeExpr(node.GetSubtree(i), variableIndices, row, columns));
|
---|
[12807] | 174 | }
|
---|
| 175 | return result;
|
---|
| 176 | }
|
---|
[13222] | 177 | case OpCodes.Sub: {
|
---|
[13140] | 178 | Expression result = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 179 | if (node.SubtreeCount == 1)
|
---|
| 180 | return Expression.Negate(result);
|
---|
| 181 | for (int i = 1; i < node.SubtreeCount; ++i) {
|
---|
[13140] | 182 | result = Expression.Subtract(result, MakeExpr(node.GetSubtree(i), variableIndices, row, columns));
|
---|
[12807] | 183 | }
|
---|
| 184 | return result;
|
---|
| 185 | }
|
---|
[13222] | 186 | case OpCodes.Mul: {
|
---|
[13140] | 187 | Expression result = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 188 | for (int i = 1; i < node.SubtreeCount; ++i) {
|
---|
[13140] | 189 | result = Expression.Multiply(result, MakeExpr(node.GetSubtree(i), variableIndices, row, columns));
|
---|
[12807] | 190 | }
|
---|
| 191 | return result;
|
---|
| 192 | }
|
---|
[13222] | 193 | case OpCodes.Div: {
|
---|
[13140] | 194 | Expression result = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 195 | if (node.SubtreeCount == 1)
|
---|
| 196 | return Expression.Divide(Expression.Constant(1.0), result);
|
---|
| 197 | for (int i = 1; i < node.SubtreeCount; ++i) {
|
---|
[13140] | 198 | result = Expression.Divide(result, MakeExpr(node.GetSubtree(i), variableIndices, row, columns));
|
---|
[12807] | 199 | }
|
---|
| 200 | return result;
|
---|
| 201 | }
|
---|
[13222] | 202 | case OpCodes.Average: {
|
---|
[13140] | 203 | Expression result = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 204 | for (int i = 1; i < node.SubtreeCount; ++i) {
|
---|
[13140] | 205 | result = Expression.Add(result, MakeExpr(node.GetSubtree(i), variableIndices, row, columns));
|
---|
[12807] | 206 | }
|
---|
| 207 | return Expression.Divide(result, Expression.Constant((double)node.SubtreeCount));
|
---|
| 208 | }
|
---|
[13222] | 209 | case OpCodes.Cos: {
|
---|
[13140] | 210 | var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 211 | return Expression.Call(Cos, arg);
|
---|
| 212 | }
|
---|
[13222] | 213 | case OpCodes.Sin: {
|
---|
[13140] | 214 | var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 215 | return Expression.Call(Sin, arg);
|
---|
| 216 | }
|
---|
[13222] | 217 | case OpCodes.Tan: {
|
---|
[13140] | 218 | var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 219 | return Expression.Call(Tan, arg);
|
---|
| 220 | }
|
---|
[13222] | 221 | case OpCodes.Square: {
|
---|
[13140] | 222 | var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[13288] | 223 | return Expression.Power(arg, Expression.Constant(2.0));
|
---|
[12807] | 224 | }
|
---|
[13222] | 225 | case OpCodes.Power: {
|
---|
[13140] | 226 | var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
| 227 | var power = MakeExpr(node.GetSubtree(1), variableIndices, row, columns);
|
---|
[13288] | 228 | return Expression.Power(arg, Expression.Call(Round, power));
|
---|
[12807] | 229 | }
|
---|
[13222] | 230 | case OpCodes.SquareRoot: {
|
---|
[13140] | 231 | var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 232 | return Expression.Call(Sqrt, arg);
|
---|
| 233 | }
|
---|
[13222] | 234 | case OpCodes.Root: {
|
---|
[13140] | 235 | var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
| 236 | var power = MakeExpr(node.GetSubtree(1), variableIndices, row, columns);
|
---|
[13288] | 237 | return Expression.Power(arg, Expression.Divide(Expression.Constant(1.0), Expression.Call(Round, power)));
|
---|
[12807] | 238 | }
|
---|
[13222] | 239 | case OpCodes.Exp: {
|
---|
[13140] | 240 | var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 241 | return Expression.Call(Exp, arg);
|
---|
| 242 | }
|
---|
[13222] | 243 | case OpCodes.Log: {
|
---|
[13140] | 244 | var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 245 | return Expression.Call(Log, arg);
|
---|
| 246 | }
|
---|
[13222] | 247 | case OpCodes.Gamma: {
|
---|
[13140] | 248 | var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 249 | var isNaN = Expression.Call(IsNaN, arg);
|
---|
| 250 |
|
---|
| 251 | var result = Expression.Variable(typeof(double));
|
---|
| 252 | var expr = Expression.Block(
|
---|
| 253 | new[] { result },
|
---|
| 254 | Expression.IfThenElse(
|
---|
| 255 | isNaN,
|
---|
| 256 | Expression.Assign(result, Expression.Constant(double.NaN)),
|
---|
[13313] | 257 | Expression.Assign(result, Expression.Call(Gamma, arg))
|
---|
[12807] | 258 | ),
|
---|
| 259 | result
|
---|
| 260 | );
|
---|
| 261 | return expr;
|
---|
| 262 | }
|
---|
[13222] | 263 | case OpCodes.Psi: {
|
---|
[13140] | 264 | var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 265 | var isNaN = Expression.Call(IsNaN, arg);
|
---|
| 266 |
|
---|
| 267 | var result = Expression.Variable(typeof(double));
|
---|
| 268 | var floor = Expression.Call(Floor, arg);
|
---|
| 269 | var expr = Expression.Block(
|
---|
| 270 | new[] { result },
|
---|
| 271 | Expression.IfThenElse(
|
---|
| 272 | isNaN,
|
---|
| 273 | Expression.Assign(result, Expression.Constant(double.NaN)),
|
---|
| 274 | Expression.IfThenElse(
|
---|
[13288] | 275 | Expression.AndAlso(
|
---|
| 276 | Expression.LessThanOrEqual(arg, Expression.Constant(0.0)),
|
---|
| 277 | Expression.Call(IsAlmost, Expression.Subtract(floor, arg), Expression.Constant(0.0))),
|
---|
[12807] | 278 | Expression.Assign(result, Expression.Constant(double.NaN)),
|
---|
[13313] | 279 | Expression.Assign(result, Expression.Call(Psi, arg)))
|
---|
[12807] | 280 | ),
|
---|
| 281 | result);
|
---|
| 282 |
|
---|
| 283 | return expr;
|
---|
| 284 | }
|
---|
[13222] | 285 | case OpCodes.Dawson: {
|
---|
[13140] | 286 | var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 287 | var isNaN = Expression.Call(IsNaN, arg);
|
---|
| 288 | var result = Expression.Variable(typeof(double));
|
---|
| 289 | var expr = Expression.Block(
|
---|
| 290 | new[] { result },
|
---|
| 291 | Expression.IfThenElse(isNaN,
|
---|
| 292 | Expression.Assign(result, Expression.Constant(double.NaN)),
|
---|
[13313] | 293 | Expression.Assign(result, Expression.Call(DawsonIntegral, arg))),
|
---|
[12807] | 294 | result
|
---|
| 295 | );
|
---|
| 296 |
|
---|
| 297 | return expr;
|
---|
| 298 | }
|
---|
[13222] | 299 | case OpCodes.ExponentialIntegralEi: {
|
---|
[13140] | 300 | var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 301 | var isNaN = Expression.Call(IsNaN, arg);
|
---|
| 302 | var result = Expression.Variable(typeof(double));
|
---|
| 303 | var expr = Expression.Block(
|
---|
| 304 | new[] { result },
|
---|
| 305 | Expression.IfThenElse(isNaN,
|
---|
| 306 | Expression.Assign(result, Expression.Constant(double.NaN)),
|
---|
[13313] | 307 | Expression.Assign(result, Expression.Call(ExponentialIntegralEi, arg))),
|
---|
[12807] | 308 | result
|
---|
| 309 | );
|
---|
| 310 |
|
---|
| 311 | return expr;
|
---|
| 312 | }
|
---|
[13222] | 313 | case OpCodes.SineIntegral: {
|
---|
[13140] | 314 | var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 315 | var isNaN = Expression.Call(IsNaN, arg);
|
---|
| 316 | var si = Expression.Variable(typeof(double));
|
---|
| 317 | var ci = Expression.Variable(typeof(double));
|
---|
| 318 | var block = Expression.Block(
|
---|
| 319 | new[] { si, ci },
|
---|
[13313] | 320 | Expression.Call(SineCosineIntegrals, arg, si, ci),
|
---|
[12807] | 321 | si
|
---|
| 322 | );
|
---|
| 323 | var result = Expression.Variable(typeof(double));
|
---|
| 324 | var expr = Expression.Block(new[] { result },
|
---|
| 325 | Expression.IfThenElse(isNaN,
|
---|
| 326 | Expression.Assign(result, Expression.Constant(double.NaN)),
|
---|
| 327 | Expression.Assign(result, block)),
|
---|
| 328 | result
|
---|
| 329 | );
|
---|
| 330 |
|
---|
| 331 | return expr;
|
---|
| 332 | }
|
---|
[13222] | 333 | case OpCodes.CosineIntegral: {
|
---|
[13140] | 334 | var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 335 | var isNaN = Expression.Call(IsNaN, arg);
|
---|
| 336 | var si = Expression.Variable(typeof(double));
|
---|
| 337 | var ci = Expression.Variable(typeof(double));
|
---|
| 338 | var block = Expression.Block(
|
---|
| 339 | new[] { si, ci },
|
---|
[13313] | 340 | Expression.Call(SineCosineIntegrals, arg, si, ci),
|
---|
[12807] | 341 | ci
|
---|
| 342 | );
|
---|
| 343 | var result = Expression.Variable(typeof(double));
|
---|
| 344 | var expr = Expression.Block(new[] { result },
|
---|
| 345 | Expression.IfThenElse(isNaN,
|
---|
| 346 | Expression.Assign(result, Expression.Constant(double.NaN)),
|
---|
| 347 | Expression.Assign(result, block)),
|
---|
| 348 | result
|
---|
| 349 | );
|
---|
| 350 |
|
---|
| 351 | return expr;
|
---|
| 352 | }
|
---|
[13222] | 353 | case OpCodes.HyperbolicSineIntegral: {
|
---|
[13140] | 354 | var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 355 | var isNaN = Expression.Call(IsNaN, arg);
|
---|
| 356 | var shi = Expression.Variable(typeof(double));
|
---|
| 357 | var chi = Expression.Variable(typeof(double));
|
---|
| 358 | var block = Expression.Block(
|
---|
| 359 | new[] { shi, chi },
|
---|
[13288] | 360 | Expression.Call(HyperbolicSineCosineIntegrals, arg, shi, chi),
|
---|
[12807] | 361 | shi
|
---|
| 362 | );
|
---|
| 363 | var result = Expression.Variable(typeof(double));
|
---|
| 364 | var expr = Expression.Block(new[] { result },
|
---|
| 365 | Expression.IfThenElse(isNaN,
|
---|
| 366 | Expression.Assign(result, Expression.Constant(double.NaN)),
|
---|
| 367 | Expression.Assign(result, block)),
|
---|
| 368 | result
|
---|
| 369 | );
|
---|
| 370 |
|
---|
| 371 | return expr;
|
---|
| 372 | }
|
---|
[13222] | 373 | case OpCodes.HyperbolicCosineIntegral: {
|
---|
[13140] | 374 | var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 375 | var isNaN = Expression.Call(IsNaN, arg);
|
---|
| 376 | var shi = Expression.Variable(typeof(double));
|
---|
| 377 | var chi = Expression.Variable(typeof(double));
|
---|
| 378 | var block = Expression.Block(
|
---|
| 379 | new[] { shi, chi },
|
---|
[13288] | 380 | Expression.Call(HyperbolicSineCosineIntegrals, arg, shi, chi),
|
---|
[12807] | 381 | chi
|
---|
| 382 | );
|
---|
| 383 | var result = Expression.Variable(typeof(double));
|
---|
| 384 | var expr = Expression.Block(new[] { result },
|
---|
| 385 | Expression.IfThenElse(isNaN,
|
---|
| 386 | Expression.Assign(result, Expression.Constant(double.NaN)),
|
---|
| 387 | Expression.Assign(result, block)),
|
---|
| 388 | result
|
---|
| 389 | );
|
---|
| 390 |
|
---|
| 391 | return expr;
|
---|
| 392 | }
|
---|
[13222] | 393 | case OpCodes.FresnelSineIntegral: {
|
---|
[13140] | 394 | var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 395 | var isNaN = Expression.Call(IsNaN, arg);
|
---|
| 396 | var s = Expression.Variable(typeof(double));
|
---|
| 397 | var c = Expression.Variable(typeof(double));
|
---|
[13313] | 398 | var block = Expression.Block(new[] { s, c }, Expression.Call(FresnelIntegral, arg, c, s), s);
|
---|
[12807] | 399 | var result = Expression.Variable(typeof(double));
|
---|
| 400 | var expr = Expression.Block(new[] { result },
|
---|
| 401 | Expression.IfThenElse(isNaN,
|
---|
| 402 | Expression.Assign(result, Expression.Constant(double.NaN)),
|
---|
| 403 | Expression.Assign(result, block)),
|
---|
| 404 | result
|
---|
| 405 | );
|
---|
| 406 |
|
---|
| 407 | return expr;
|
---|
| 408 | }
|
---|
[13222] | 409 | case OpCodes.FresnelCosineIntegral: {
|
---|
[13140] | 410 | var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 411 | var isNaN = Expression.Call(IsNaN, arg);
|
---|
| 412 | var s = Expression.Variable(typeof(double));
|
---|
| 413 | var c = Expression.Variable(typeof(double));
|
---|
[13313] | 414 | var block = Expression.Block(new[] { s, c }, Expression.Call(FresnelIntegral, arg, c, s), c);
|
---|
[12807] | 415 | var result = Expression.Variable(typeof(double));
|
---|
| 416 | var expr = Expression.Block(new[] { result },
|
---|
| 417 | Expression.IfThenElse(isNaN,
|
---|
| 418 | Expression.Assign(result, Expression.Constant(double.NaN)),
|
---|
| 419 | Expression.Assign(result, block)),
|
---|
| 420 | result
|
---|
| 421 | );
|
---|
| 422 |
|
---|
| 423 | return expr;
|
---|
| 424 | }
|
---|
[13222] | 425 | case OpCodes.AiryA: {
|
---|
[13140] | 426 | var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 427 | var isNaN = Expression.Call(IsNaN, arg);
|
---|
| 428 | var ai = Expression.Variable(typeof(double));
|
---|
| 429 | var aip = Expression.Variable(typeof(double));
|
---|
| 430 | var bi = Expression.Variable(typeof(double));
|
---|
| 431 | var bip = Expression.Variable(typeof(double));
|
---|
[13313] | 432 | var block = Expression.Block(new[] { ai, aip, bi, bip }, Expression.Call(Airy, arg, ai, aip, bi, bip), ai);
|
---|
[12807] | 433 | var result = Expression.Variable(typeof(double));
|
---|
| 434 | var expr = Expression.Block(new[] { result },
|
---|
| 435 | Expression.IfThenElse(isNaN,
|
---|
| 436 | Expression.Assign(result, Expression.Constant(double.NaN)),
|
---|
| 437 | Expression.Assign(result, block)),
|
---|
| 438 | result
|
---|
| 439 | );
|
---|
| 440 |
|
---|
| 441 | return expr;
|
---|
| 442 | }
|
---|
[13222] | 443 | case OpCodes.AiryB: {
|
---|
[13140] | 444 | var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 445 | var isNaN = Expression.Call(IsNaN, arg);
|
---|
| 446 | var ai = Expression.Variable(typeof(double));
|
---|
| 447 | var aip = Expression.Variable(typeof(double));
|
---|
| 448 | var bi = Expression.Variable(typeof(double));
|
---|
| 449 | var bip = Expression.Variable(typeof(double));
|
---|
[13313] | 450 | var block = Expression.Block(new[] { ai, aip, bi, bip }, Expression.Call(Airy, arg, ai, aip, bi, bip), bi);
|
---|
[12807] | 451 | var result = Expression.Variable(typeof(double));
|
---|
| 452 | var expr = Expression.Block(new[] { result },
|
---|
| 453 | Expression.IfThenElse(isNaN,
|
---|
| 454 | Expression.Assign(result, Expression.Constant(double.NaN)),
|
---|
| 455 | Expression.Assign(result, block)),
|
---|
| 456 | result
|
---|
| 457 | );
|
---|
| 458 |
|
---|
| 459 | return expr;
|
---|
| 460 | }
|
---|
[13222] | 461 | case OpCodes.Norm: {
|
---|
[13140] | 462 | var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 463 | var result = Expression.Variable(typeof(double));
|
---|
[13288] | 464 | return Expression.Block(
|
---|
| 465 | new[] { result },
|
---|
| 466 | Expression.IfThenElse(
|
---|
[13313] | 467 | Expression.Call(IsNaN, arg),
|
---|
| 468 | Expression.Assign(result, arg),
|
---|
[13288] | 469 | Expression.Assign(result, Expression.Call(NormalDistribution, arg))),
|
---|
| 470 | result);
|
---|
[12807] | 471 | }
|
---|
[13222] | 472 | case OpCodes.Erf: {
|
---|
[13140] | 473 | var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 474 | var isNaN = Expression.Call(IsNaN, arg);
|
---|
| 475 | var result = Expression.Variable(typeof(double));
|
---|
[13288] | 476 | return Expression.Block(
|
---|
| 477 | new[] { result },
|
---|
| 478 | Expression.IfThenElse(
|
---|
| 479 | isNaN,
|
---|
| 480 | Expression.Assign(result, Expression.Constant(double.NaN)),
|
---|
| 481 | Expression.Assign(result, Expression.Call(ErrorFunction, arg))),
|
---|
| 482 | result);
|
---|
[12807] | 483 | }
|
---|
[13222] | 484 | case OpCodes.Bessel: {
|
---|
[13140] | 485 | var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 486 | var isNaN = Expression.Call(IsNaN, arg);
|
---|
| 487 | var result = Expression.Variable(typeof(double));
|
---|
[13288] | 488 | return Expression.Block(
|
---|
[12807] | 489 | new[] { result },
|
---|
| 490 | Expression.IfThenElse(
|
---|
| 491 | isNaN,
|
---|
| 492 | Expression.Assign(result, Expression.Constant(double.NaN)),
|
---|
[13288] | 493 | Expression.Assign(result, Expression.Call(Bessel, arg))),
|
---|
[12807] | 494 | result);
|
---|
| 495 | }
|
---|
[13222] | 496 | case OpCodes.IfThenElse: {
|
---|
[13140] | 497 | var test = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 498 | var result = Expression.Variable(typeof(double));
|
---|
| 499 | var condition = Expression.IfThenElse(Expression.GreaterThan(test, Expression.Constant(0.0)),
|
---|
[13140] | 500 | Expression.Assign(result, MakeExpr(node.GetSubtree(1), variableIndices, row, columns)),
|
---|
| 501 | Expression.Assign(result, MakeExpr(node.GetSubtree(2), variableIndices, row, columns)));
|
---|
[12807] | 502 | return Expression.Block(new[] { result }, condition, result);
|
---|
| 503 | }
|
---|
[13222] | 504 | case OpCodes.AND: {
|
---|
[12807] | 505 | var result = Expression.Variable(typeof(double));
|
---|
[13140] | 506 | var expr = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 507 |
|
---|
| 508 | for (int i = 1; i < node.SubtreeCount; ++i) {
|
---|
| 509 | expr = Expression.Block(new[] { result },
|
---|
| 510 | Expression.IfThenElse(
|
---|
| 511 | Expression.GreaterThan(expr, Expression.Constant(0.0)),
|
---|
[13140] | 512 | Expression.Assign(result, MakeExpr(node.GetSubtree(i), variableIndices, row, columns)),
|
---|
[12807] | 513 | Expression.Assign(result, expr)),
|
---|
| 514 | result
|
---|
| 515 | );
|
---|
| 516 | }
|
---|
| 517 |
|
---|
| 518 | return Expression.Block(
|
---|
| 519 | new[] { result },
|
---|
| 520 | Expression.Assign(result, expr),
|
---|
| 521 | Expression.IfThenElse(
|
---|
| 522 | Expression.GreaterThan(result, Expression.Constant(0.0)),
|
---|
| 523 | Expression.Assign(result, Expression.Constant(1.0)),
|
---|
| 524 | Expression.Assign(result, Expression.Constant(-1.0))
|
---|
| 525 | ),
|
---|
| 526 | result
|
---|
| 527 | );
|
---|
| 528 | }
|
---|
[13222] | 529 | case OpCodes.OR: {
|
---|
[12807] | 530 | var result = Expression.Variable(typeof(double));
|
---|
[13140] | 531 | var expr = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 532 |
|
---|
| 533 | for (int i = 1; i < node.SubtreeCount; ++i) {
|
---|
| 534 | expr = Expression.Block(new[] { result },
|
---|
| 535 | Expression.IfThenElse(
|
---|
| 536 | Expression.LessThanOrEqual(expr, Expression.Constant(0.0)),
|
---|
[13140] | 537 | Expression.Assign(result, MakeExpr(node.GetSubtree(i), variableIndices, row, columns)),
|
---|
[12807] | 538 | Expression.Assign(result, expr)),
|
---|
| 539 | result
|
---|
| 540 | );
|
---|
| 541 | }
|
---|
| 542 |
|
---|
| 543 | return Expression.Block(
|
---|
| 544 | new[] { result },
|
---|
| 545 | Expression.Assign(result, expr),
|
---|
| 546 | Expression.IfThenElse(
|
---|
| 547 | Expression.GreaterThan(result, Expression.Constant(0.0)),
|
---|
| 548 | Expression.Assign(result, Expression.Constant(1.0)),
|
---|
| 549 | Expression.Assign(result, Expression.Constant(-1.0))
|
---|
| 550 | ),
|
---|
| 551 | result
|
---|
| 552 | );
|
---|
| 553 | }
|
---|
[13222] | 554 | case OpCodes.NOT: {
|
---|
[13140] | 555 | var value = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
[12807] | 556 | var result = Expression.Variable(typeof(double));
|
---|
| 557 | var condition = Expression.IfThenElse(Expression.GreaterThan(value, Expression.Constant(0.0)),
|
---|
| 558 | Expression.Assign(result, Expression.Constant(-1.0)),
|
---|
| 559 | Expression.Assign(result, Expression.Constant(1.0)));
|
---|
| 560 | return Expression.Block(new[] { result }, condition, result);
|
---|
| 561 | }
|
---|
[13222] | 562 | case OpCodes.XOR: {
|
---|
[12807] | 563 | var ps = Expression.Variable(typeof(int));
|
---|
| 564 | var block = Expression.Block(
|
---|
| 565 | new[] { ps },
|
---|
| 566 | Expression.Assign(ps, Expression.Constant(0)),
|
---|
| 567 | ps
|
---|
| 568 | );
|
---|
| 569 |
|
---|
| 570 | foreach (var subtree in node.Subtrees) {
|
---|
[13140] | 571 | var expr = MakeExpr(subtree, variableIndices, row, columns);
|
---|
[12807] | 572 | block = Expression.Block(
|
---|
| 573 | new[] { ps },
|
---|
| 574 | Expression.Assign(ps, block),
|
---|
| 575 | Expression.IfThen(Expression.GreaterThan(expr, Expression.Constant(0.0)),
|
---|
| 576 | Expression.PostIncrementAssign(ps)),
|
---|
| 577 | ps
|
---|
| 578 | );
|
---|
| 579 | }
|
---|
| 580 |
|
---|
| 581 | var result = Expression.Variable(typeof(double));
|
---|
| 582 | var xorExpr = Expression.Block(
|
---|
| 583 | new[] { result },
|
---|
| 584 | Expression.IfThenElse(
|
---|
| 585 | Expression.Equal(Expression.Modulo(block, Expression.Constant(2)), Expression.Constant(0)),
|
---|
| 586 | Expression.Assign(result, Expression.Constant(-1.0)),
|
---|
| 587 | Expression.Assign(result, Expression.Constant(1.0))
|
---|
| 588 | ),
|
---|
| 589 | result
|
---|
| 590 | );
|
---|
| 591 | return xorExpr;
|
---|
| 592 | }
|
---|
[13222] | 593 | case OpCodes.GT: {
|
---|
[13140] | 594 | var left = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
| 595 | var right = MakeExpr(node.GetSubtree(1), variableIndices, row, columns);
|
---|
[12807] | 596 | var result = Expression.Variable(typeof(double));
|
---|
| 597 |
|
---|
| 598 | var condition = Expression.IfThenElse(Expression.GreaterThan(left, right),
|
---|
| 599 | Expression.Assign(result, Expression.Constant(1.0)), Expression.Assign(result, Expression.Constant(-1.0)));
|
---|
| 600 | return Expression.Block(
|
---|
| 601 | new[] { result },
|
---|
| 602 | condition,
|
---|
| 603 | result);
|
---|
| 604 | }
|
---|
[13222] | 605 | case OpCodes.LT: {
|
---|
[13140] | 606 | var left = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
| 607 | var right = MakeExpr(node.GetSubtree(1), variableIndices, row, columns);
|
---|
[12807] | 608 | var result = Expression.Variable(typeof(double));
|
---|
| 609 |
|
---|
| 610 | var condition = Expression.IfThenElse(Expression.LessThan(left, right),
|
---|
| 611 | Expression.Assign(result, Expression.Constant(1.0)), Expression.Assign(result, Expression.Constant(-1.0)));
|
---|
| 612 | return Expression.Block(new[] { result }, condition, result);
|
---|
| 613 | }
|
---|
[13222] | 614 | case OpCodes.VariableCondition: {
|
---|
[12807] | 615 | var variableConditionTreeNode = (VariableConditionTreeNode)node;
|
---|
[14345] | 616 | if (variableConditionTreeNode.Symbol.IgnoreSlope) throw new NotSupportedException("Strict variable conditionals are not supported");
|
---|
[13140] | 617 | var variableName = variableConditionTreeNode.VariableName;
|
---|
| 618 | var indexExpr = Expression.Constant(variableIndices[variableName]);
|
---|
| 619 | var valuesExpr = Expression.ArrayIndex(columns, indexExpr);
|
---|
[13288] | 620 | var variableValue = Expression.Property(valuesExpr, Indexer, row);
|
---|
[12807] | 621 | var variableThreshold = Expression.Constant(variableConditionTreeNode.Threshold);
|
---|
| 622 | var variableSlope = Expression.Constant(variableConditionTreeNode.Slope);
|
---|
| 623 |
|
---|
| 624 | var x = Expression.Subtract(variableValue, variableThreshold);
|
---|
| 625 | var xSlope = Expression.Multiply(Expression.Negate(variableSlope), x);
|
---|
| 626 | var xSlopeExp = Expression.Call(Exp, xSlope);
|
---|
| 627 | var p = Expression.Divide(Expression.Constant(1), Expression.Add(Expression.Constant(1), xSlopeExp));
|
---|
[13140] | 628 | var trueBranch = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
|
---|
| 629 | var falseBranch = MakeExpr(node.GetSubtree(1), variableIndices, row, columns);
|
---|
[12807] | 630 | return Expression.Add(
|
---|
| 631 | Expression.Multiply(trueBranch, p),
|
---|
| 632 | Expression.Multiply(falseBranch, Expression.Subtract(Expression.Constant(1), p))
|
---|
| 633 | );
|
---|
| 634 | }
|
---|
| 635 | default:
|
---|
| 636 | throw new NotSupportedException("Unsupported symbol: " + node.Symbol);
|
---|
| 637 | }
|
---|
[13141] | 638 | #endregion
|
---|
[12807] | 639 | }
|
---|
| 640 | }
|
---|
| 641 | }
|
---|