- Timestamp:
- 11/19/15 12:02:20 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionCompiledTreeInterpreter.cs
r13251 r13288 46 46 private static readonly MethodInfo Sqrt = typeof(Math).GetMethod("Sqrt", new[] { typeof(double) }); 47 47 private static readonly MethodInfo Floor = typeof(Math).GetMethod("Floor", new[] { typeof(double) }); 48 private static readonly MethodInfo Round = typeof(Math).GetMethod("Round", new[] { typeof(double) }); 48 49 private static readonly MethodInfo Exp = typeof(Math).GetMethod("Exp", new[] { typeof(double) }); 49 50 private static readonly MethodInfo Log = typeof(Math).GetMethod("Log", new[] { typeof(double) }); 50 51 private static readonly MethodInfo IsNaN = typeof(double).GetMethod("IsNaN"); 52 private static readonly MethodInfo IsAlmost = typeof(DoubleExtensions).GetMethod("IsAlmost"); 51 53 private static readonly MethodInfo Gamma = typeof(alglib).GetMethod("gammafunction", new[] { typeof(double) }); 52 54 private static readonly MethodInfo Psi = typeof(alglib).GetMethod("psi", new[] { typeof(double) }); … … 159 161 var indexExpr = Expression.Constant(variableIndices[variableName]); 160 162 var valuesExpr = Expression.ArrayIndex(columns, indexExpr); 161 var variableValue = Expression.Property(valuesExpr, Indexer, row); 162 return Expression.Multiply(variableWeight, variableValue); 163 return Expression.Multiply(variableWeight, Expression.Property(valuesExpr, Indexer, row)); 163 164 } 164 165 case OpCodes.Add: { … … 215 216 case OpCodes.Square: { 216 217 var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns); 217 return Expression.Power(arg, Expression.Constant(2 ));218 return Expression.Power(arg, Expression.Constant(2.0)); 218 219 } 219 220 case OpCodes.Power: { 220 221 var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns); 221 222 var power = MakeExpr(node.GetSubtree(1), variableIndices, row, columns); 222 return Expression.Power(arg, Expression.Call( Floor, power));223 return Expression.Power(arg, Expression.Call(Round, power)); 223 224 } 224 225 case OpCodes.SquareRoot: { … … 229 230 var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns); 230 231 var power = MakeExpr(node.GetSubtree(1), variableIndices, row, columns); 231 return Expression.Power(arg, Expression.Divide(Expression.Constant(1.0), power));232 return Expression.Power(arg, Expression.Divide(Expression.Constant(1.0), Expression.Call(Round, power))); 232 233 } 233 234 case OpCodes.Exp: { … … 269 270 Expression.Assign(result, Expression.Constant(double.NaN)), 270 271 Expression.IfThenElse( 271 Expression.AndAlso(Expression.LessThanOrEqual(arg, Expression.Constant(0.0)), 272 Expression.Equal(Expression.Subtract(floor, arg), Expression.Constant(0.0))), 272 Expression.AndAlso( 273 Expression.LessThanOrEqual(arg, Expression.Constant(0.0)), 274 Expression.Call(IsAlmost, Expression.Subtract(floor, arg), Expression.Constant(0.0))), 273 275 Expression.Assign(result, Expression.Constant(double.NaN)), 274 276 Expression.Assign(result, psi)) … … 297 299 var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns); 298 300 var isNaN = Expression.Call(IsNaN, arg); 299 var expIntegrapEi = 300 Expression.Call(ExponentialIntegralEi, arg); 301 var expIntegrapEi = Expression.Call(ExponentialIntegralEi, arg); 301 302 var result = Expression.Variable(typeof(double)); 302 303 var expr = Expression.Block( … … 357 358 var shi = Expression.Variable(typeof(double)); 358 359 var chi = Expression.Variable(typeof(double)); 359 var hypSinCosIntegrals = Expression.Call(HyperbolicSineCosineIntegrals, arg, shi, chi);360 360 var block = Expression.Block( 361 361 new[] { shi, chi }, 362 hypSinCosIntegrals,362 Expression.Call(HyperbolicSineCosineIntegrals, arg, shi, chi), 363 363 shi 364 364 ); … … 378 378 var shi = Expression.Variable(typeof(double)); 379 379 var chi = Expression.Variable(typeof(double)); 380 var hypSinCosIntegrals = Expression.Call(HyperbolicSineCosineIntegrals, arg, shi, chi);381 380 var block = Expression.Block( 382 381 new[] { shi, chi }, 383 hypSinCosIntegrals,382 Expression.Call(HyperbolicSineCosineIntegrals, arg, shi, chi), 384 383 chi 385 384 ); … … 470 469 var isNaN = Expression.Call(IsNaN, arg); 471 470 var result = Expression.Variable(typeof(double)); 472 var norm = Expression.Call(NormalDistribution, arg);473 474 var expr = Expression.Block(new[] { result },475 Expression.IfThenElse(isNaN, Expression.Assign(result, Expression.Constant(double.NaN)),476 Expression.Assign(result, norm)), result);477 478 return expr;471 return Expression.Block( 472 new[] { result }, 473 Expression.IfThenElse( 474 isNaN, 475 Expression.Assign(result, Expression.Constant(double.NaN)), 476 Expression.Assign(result, Expression.Call(NormalDistribution, arg))), 477 result); 479 478 } 480 479 case OpCodes.Erf: { … … 482 481 var isNaN = Expression.Call(IsNaN, arg); 483 482 var result = Expression.Variable(typeof(double)); 484 var erf = Expression.Call(ErrorFunction, arg);485 486 var expr = Expression.Block(new[] { result },487 Expression.IfThenElse(isNaN, Expression.Assign(result, Expression.Constant(double.NaN)),488 Expression.Assign(result, erf)), result);489 490 return expr;483 return Expression.Block( 484 new[] { result }, 485 Expression.IfThenElse( 486 isNaN, 487 Expression.Assign(result, Expression.Constant(double.NaN)), 488 Expression.Assign(result, Expression.Call(ErrorFunction, arg))), 489 result); 491 490 } 492 491 case OpCodes.Bessel: { … … 494 493 var isNaN = Expression.Call(IsNaN, arg); 495 494 var result = Expression.Variable(typeof(double)); 496 var bessel = Expression.Call(Bessel, arg); 497 var expr = Expression.Block( 495 return Expression.Block( 498 496 new[] { result }, 499 497 Expression.IfThenElse( 500 498 isNaN, 501 499 Expression.Assign(result, Expression.Constant(double.NaN)), 502 Expression.Assign(result, bessel)),500 Expression.Assign(result, Expression.Call(Bessel, arg))), 503 501 result); 504 505 return expr;506 502 } 507 503 case OpCodes.IfThenElse: { … … 628 624 var indexExpr = Expression.Constant(variableIndices[variableName]); 629 625 var valuesExpr = Expression.ArrayIndex(columns, indexExpr); 630 var variableValue = Expression. ArrayIndex(valuesExpr, row);626 var variableValue = Expression.Property(valuesExpr, Indexer, row); 631 627 var variableThreshold = Expression.Constant(variableConditionTreeNode.Threshold); 632 628 var variableSlope = Expression.Constant(variableConditionTreeNode.Slope); … … 643 639 ); 644 640 } 645 case OpCodes.LagVariable: {646 var laggedVariableTreeNode = (LaggedVariableTreeNode)node;647 var lag = Expression.Constant(laggedVariableTreeNode.Lag);648 var variableWeight = Expression.Constant(laggedVariableTreeNode.Weight);649 var variableName = laggedVariableTreeNode.VariableName;650 var indexExpr = Expression.Constant(variableIndices[variableName]);651 var valuesExpr = Expression.ArrayIndex(columns, indexExpr);652 var variableValue = Expression.Property(valuesExpr, Indexer, Expression.Add(row, lag));653 return Expression.Multiply(variableWeight, variableValue);654 }655 case OpCodes.TimeLag: {656 var timeLagTreeNode = (LaggedTreeNode)node;657 var lag = Expression.Constant(timeLagTreeNode.Lag);658 return MakeExpr(timeLagTreeNode.GetSubtree(0), variableIndices, Expression.Add(row, lag), columns);659 }660 case OpCodes.Integral: {661 var timeLagTreeNode = (LaggedTreeNode)node;662 var subtree = node.GetSubtree(0);663 var sum = MakeExpr(subtree, variableIndices, row, columns);664 var sign = Expression.Constant(Math.Sign(timeLagTreeNode.Lag));665 var lag = Expression.Add(row, sign);666 for (int i = 0; i < Math.Abs(timeLagTreeNode.Lag); ++i) {667 sum = Expression.Add(sum, MakeExpr(subtree, variableIndices, lag, columns));668 lag = Expression.Add(lag, sign);669 }670 return sum;671 }672 case OpCodes.Derivative: {673 var subtree = node.GetSubtree(0);674 var f0 = MakeExpr(subtree, variableIndices, row, columns);675 var f1 = MakeExpr(subtree, variableIndices, Expression.Subtract(row, Expression.Constant(1)), columns);676 var f3 = MakeExpr(subtree, variableIndices, Expression.Subtract(row, Expression.Constant(3)), columns);677 var f4 = MakeExpr(subtree, variableIndices, Expression.Subtract(row, Expression.Constant(4)), columns);678 679 var result = f0;680 result = Expression.Add(result, Expression.Multiply(Expression.Constant(2.0), f1));681 result = Expression.Subtract(result, Expression.Multiply(Expression.Constant(2.0), f3));682 result = Expression.Subtract(result, f4);683 return Expression.Divide(result, Expression.Constant(8.0));684 }685 641 default: 686 642 throw new NotSupportedException("Unsupported symbol: " + node.Symbol); … … 688 644 #endregion 689 645 } 690 // util stuff691 private static Func<T, R> GetField<T, R>(string fieldName) {692 ParameterExpression param = Expression.Parameter(typeof(T), "arg");693 MemberExpression member = Expression.Field(param, fieldName);694 LambdaExpression lambda = Expression.Lambda(typeof(Func<T, R>), member, param);695 Func<T, R> compiled = (Func<T, R>)lambda.Compile();696 return compiled;697 }698 646 } 699 647 }
Note: See TracChangeset
for help on using the changeset viewer.