public static Matrix map(Matrix m, LambdaExpression mapper, VariableContext vc) { ExpressionEvaluator evaluator = new MathyLanguageService().CreateEvaluator(); List <List <double> > result = new List <List <double> >(); double[] row = new double[m.ColumnCount]; for (int i = 0; i <= m.RowCount - 1; i++) { for (int j = 0; j <= m.ColumnCount - 1; j++) { row[j] = m[i, j]; } vc.Set(mapper.VariableNames[0], row.Where(c => !double.IsNaN(c)).ToArray()); if (mapper.VariableNames.Length > 1) { vc.Set(mapper.VariableNames[1], i); } double[] mappedRow; var obj = evaluator.Evaluate(mapper.Body, vc); try { mappedRow = (double[])obj; } catch (Exception ex) { var a = (object[])obj; mappedRow = (double[])a[0]; } result.Add(new List <double>()); for (int j = 0; j <= mappedRow.Length - 1; j++) { result[i].Add(mappedRow[j]); } } vc.Remove(mapper.VariableNames[0]); List <double> cells = new List <double>(); foreach (List <double> resultRow in result) { foreach (double cell in resultRow) { cells.Add(cell); } } return(new Matrix(m.RowCount, result[0].Count, cells.ToArray())); }
public static VariableContextExpression diff(VariableContextExpression expression, string variableName) { VariableContext c = expression.VariableContext; Expression diff = new Differ(expression.Expression, variableName).Calculate(c); TypeCheckingContext context = new MathyLanguageService().CreateTypeCheckingContext(c); foreach (VariableInfo variable in expression.Variables) { c.Set(variable.Name, context.CreateAutoCreatedVariableValue(variable.Type)); } VariableContextExpression result = new VariableContextExpression(diff, expression.Variables, 0, 0) { VariableContext = expression.VariableContext }; new MathyLanguageService().CreateTypeChecker().PerformTypeChecking(result, context); result = new VariableContextExpression(new Cherimoya.Reduction.ExpressionReductor(new MathyLanguageService()).Reduce(diff), expression.Variables, 0, 0) { VariableContext = expression.VariableContext }; new MathyLanguageService().CreateTypeChecker().PerformTypeChecking(result, context); foreach (VariableInfo variable in expression.Variables) { context.VariableContext.Remove(variable.Name); } return(result); }
private VariableContext CreateVariableContext() { VariableContext vc = MathyLanguageService.CreateVariableContext(); foreach (Step step in Steps) { for (int i = 0; i <= step.InSourceVariables.Length - 1; i++) { object value = step.InValues[i]; if (value != null) { if (value is double) { value = Math.Round((double)value, Settings.DecimalDigitCount); } else if (value is double[]) { value = (value as double[]).Where(j => !double.IsNaN(j)).Select(j => Math.Round(j, Settings.DecimalDigitCount)).ToArray(); } else if (value is int[]) { value = (value as int[]).Select(j => Math.Round((double)j, Settings.DecimalDigitCount)).ToArray(); } else if (value is Vector) { Vector v = (value as Vector).Clone(); v.SetDecimalDigitCount(Settings.DecimalDigitCount); value = v; } else if (value is Matrix) { Matrix m = (value as Matrix).Clone(); m.SetDecimalDigitCount(Settings.DecimalDigitCount); value = m; } vc.Set(step.InSourceVariables[i].Name, value); } } } vc.Set("_EvaluationContext", this); return(vc); }
private VariableContext CreateVariebleContext() { VariableContext vc = MathyLanguageService.CreateVariableContext(); foreach (SourceVariable variable in plan.Variables) { vc.Set(variable.Name, DataFuncs.CreateDefaultValue(variable.Type)); } return(vc); }
public static VariableContext CreateVariableContext(Dictionary <string, object> variables) { VariableContext c = MathyLanguageService.CreateVariableContext(); foreach (string variableName in variables.Keys) { c.Set(variableName, variables[variableName]); } return(c); }
private static object[] map(object[] items, LambdaExpression mapper, VariableContext vc) { ExpressionEvaluator evaluator = new MathyLanguageService().CreateEvaluator(); object[] result = new object[items.Length]; for (int i = 0; i <= items.Length - 1; i++) { vc.Set(mapper.VariableNames[0], items[i]); result[i] = evaluator.Evaluate(mapper.Body, vc); } vc.Remove(mapper.VariableNames[0]); return(result); }
public static object eval(Expression expression, Dictionary <string, object> variables, VariableContext vc) { foreach (string variableName in variables.Keys) { vc.Set(variableName, variables[variableName]); } object result = new MathyLanguageService().CreateEvaluator().Evaluate(expression is VariableContextExpression ? (expression as VariableContextExpression).Expression : expression, vc); foreach (string variableName in variables.Keys) { vc.Remove(variableName); } return(result); }
public static Expression Diff(Expression e, string x, Dictionary <string, object> variables) { VariableContext c = Funcs.CreateVariableContext(variables); Expression diff = new Differ(e, x).Calculate(c); foreach (string variableName in variables.Keys) { c.Set(variableName, variables[variableName]); } new MathyLanguageService().CreateTypeChecker().PerformTypeChecking(diff, new MathyLanguageService().CreateTypeCheckingContext(c)); diff = new Cherimoya.Reduction.ExpressionReductor(new MathyLanguageService()).Reduce(diff); foreach (string variableName in variables.Keys) { c.Remove(variableName); } return(diff); }
private object EvaluteBinaryExpression(BinaryExpression expression, VariableContext context) { BinaryOperator op = expression.Operator; if (op == BinaryOperator.Assign) { string variableName = ((VariableExpression)expression.Left).VariableName; object right1 = Evaluate(expression.Right, context); context.Set(variableName, right1); return(right1); } object left = Evaluate(expression.Left, context); if (op == BinaryOperator.And) { return(!(bool)left ? false : (bool)Evaluate(expression.Right, context)); } else if (op == BinaryOperator.Or) { return((bool)left ? true : (bool)Evaluate(expression.Right, context)); } object right = Evaluate(expression.Right, context); if (op == BinaryOperator.Add) { return(Add(left, right)); } else if (op == BinaryOperator.Subtract) { return(Subtract(left, right)); } else if (op == BinaryOperator.Multiply) { return(Multiply(left, right)); } else if (op == BinaryOperator.Divide) { return(Divide(left, right)); } else if (op == BinaryOperator.LessThan) { return(LessThan(left, right)); } else if (op == BinaryOperator.LessEqualThan) { return(LessEqualThan(left, right)); } else if (op == BinaryOperator.GreaterThan) { return(GreaterThan(left, right)); } else if (op == BinaryOperator.GreaterEqualThan) { return(GreaterEqualThan(left, right)); } else if (op == BinaryOperator.Equal) { return(Equal(left, right)); } else if (op == BinaryOperator.NotEqual) { return(NotEqual(left, right)); } return(null); }
private object EvaluteBinaryExpression(BinaryExpression e, VariableContext context, out bool isEvaluated) { isEvaluated = false; bool isTupleValue = e.Right.Type.Name.Contains("Tuple"); if (e.Operator == BinaryOperator.Assign && (e.Left is MultipleVariableExpression || isTupleValue)) { isEvaluated = true; object rightValue = Evaluate(e.Right, context); if (e.Left is MultipleVariableExpression) { MultipleVariableExpression mv = e.Left as MultipleVariableExpression; if (isTupleValue) { int count = mv.Type.GenericTypeArguments.Length; for (int i = 0; i <= count - 1; i++) { context.Set(mv.Variables[i], rightValue.GetType().GetProperty("Item" + (i + 1)).GetValue(rightValue)); } } else { context.Set(mv.Variables[0], rightValue); } } else { context.Set((e.Left as VariableExpression).VariableName, rightValue.GetType().GetProperty("Item1").GetValue(rightValue)); } return(rightValue); } else if (e.Operator == BinaryOperator.Add) { if (e.Left.Type == typeof(Matrix) && e.Right.Type == typeof(Matrix)) { isEvaluated = true; return((Evaluate(e.Left, context) as Matrix).Add(Evaluate(e.Right, context) as Matrix)); } else if (e.Left.Type == typeof(Matrix) && Types.IsNumberType(e.Right.Type)) { isEvaluated = true; return((Evaluate(e.Left, context) as Matrix).Add(Types.ConvertValue <double>(Evaluate(e.Right, context)))); } else if (e.Right.Type == typeof(Matrix) && Types.IsNumberType(e.Left.Type)) { isEvaluated = true; return((Evaluate(e.Right, context) as Matrix).Add(Types.ConvertValue <double>(Evaluate(e.Left, context)))); } else if (e.Left.Type == typeof(Vector) && Types.IsNumberType(e.Right.Type)) { isEvaluated = true; return((Evaluate(e.Left, context) as Vector).Add(Types.ConvertValue <double>(Evaluate(e.Right, context)))); } else if (e.Right.Type == typeof(Vector) && Types.IsNumberType(e.Left.Type)) { isEvaluated = true; return((Evaluate(e.Right, context) as Vector).Add(Types.ConvertValue <double>(Evaluate(e.Left, context)))); } } else if (e.Operator == BinaryOperator.Subtract) { if (e.Left.Type == typeof(Matrix) && e.Right.Type == typeof(Matrix)) { isEvaluated = true; return((Evaluate(e.Left, context) as Matrix).Add((Evaluate(e.Right, context) as Matrix).ToNegative())); } else if (e.Left.Type == typeof(Matrix) && Types.IsNumberType(e.Right.Type)) { isEvaluated = true; return((Evaluate(e.Left, context) as Matrix).Subtract(Types.ConvertValue <double>(Evaluate(e.Right, context)))); } else if (e.Right.Type == typeof(Matrix) && Types.IsNumberType(e.Left.Type)) { isEvaluated = true; return((Evaluate(e.Right, context) as Matrix).Subtract(Types.ConvertValue <double>(Evaluate(e.Left, context))).GetNegative()); } else if (e.Left.Type == typeof(Vector) && Types.IsNumberType(e.Right.Type)) { isEvaluated = true; return((Evaluate(e.Left, context) as Vector).Subtract(Types.ConvertValue <double>(Evaluate(e.Right, context)))); } else if (e.Right.Type == typeof(Vector) && Types.IsNumberType(e.Left.Type)) { isEvaluated = true; return((Evaluate(e.Right, context) as Vector).Subtract(Types.ConvertValue <double>(Evaluate(e.Left, context))).GetNegative()); } } else if (e.Operator == BinaryOperator.Multiply) { if (e.Left.Type == typeof(Matrix) && e.Right.Type == typeof(Matrix)) { isEvaluated = true; return((Evaluate(e.Left, context) as Matrix).Multiply(Evaluate(e.Right, context) as Matrix)); } else if (e.Left.Type == typeof(Matrix)) { isEvaluated = true; return((Evaluate(e.Left, context) as Matrix).Multiply(Convert.ToDouble(Evaluate(e.Right, context)))); } else if (e.Right.Type == typeof(Matrix)) { isEvaluated = true; return((Evaluate(e.Right, context) as Matrix).Multiply(Convert.ToDouble(Evaluate(e.Left, context)))); } else if (e.Left.Type == typeof(Vector) && Types.IsNumberType(e.Right.Type)) { isEvaluated = true; return((Evaluate(e.Left, context) as Vector).Multiply(Types.ConvertValue <double>(Evaluate(e.Right, context)))); } else if (e.Right.Type == typeof(Vector) && Types.IsNumberType(e.Left.Type)) { isEvaluated = true; return((Evaluate(e.Right, context) as Vector).Multiply(Types.ConvertValue <double>(Evaluate(e.Left, context))).GetNegative()); } } else if (e.Operator == BinaryOperator.Divide) { if (e.Left.Type == typeof(Matrix) && e.Right.Type == typeof(Matrix)) { isEvaluated = true; return((Evaluate(e.Left, context) as Matrix).Divide((Evaluate(e.Right, context) as Matrix))); } else if (e.Left.Type == typeof(Matrix)) { isEvaluated = true; return((Evaluate(e.Left, context) as Matrix).Multiply(1 / Convert.ToDouble(Evaluate(e.Right, context)))); } else if (e.Right.Type == typeof(Matrix)) { isEvaluated = true; return((Evaluate(e.Right, context) as Matrix).ElementInvert().Multiply(Convert.ToDouble(Evaluate(e.Left, context)))); } if (e.Left.Type == typeof(Vector)) { isEvaluated = true; return((Evaluate(e.Left, context) as Vector).Multiply(1 / Convert.ToDouble(Evaluate(e.Right, context)))); } else if (e.Right.Type == typeof(Vector)) { isEvaluated = true; return((Evaluate(e.Right, context) as Vector).ElementInvert().Multiply(Convert.ToDouble(Evaluate(e.Left, context)))); } } return(null); }
private object EvaluateIterationSumExpression(IterationSumExpression e, VariableContext context) { int[] values = new int[e.Variables.Length]; int[] fromValues = new int[e.Variables.Length]; int[] toValues = new int[e.Variables.Length]; int index = 0; foreach (IterationSumVariable variable in e.Variables) { if (context.HasVariable(variable.Name)) { throw new Exception(string.Format("Iteration variable {0} is already in use.", variable.Name)); } fromValues[index] = Types.ConvertValue <int>(Evaluate(variable.From, context)); toValues[index] = Types.ConvertValue <int>(Evaluate(variable.To, context)); context.Set(variable.Name, fromValues[index]); values[index] = fromValues[index]; index++; } double sum = 0; while (true) { double value = Types.ConvertValue <double>(Evaluate(e.Body, context));; if (!double.IsNaN(value)) { sum += value; } if (!IncreaseIterationValues(values, fromValues, toValues)) { break; } else { for (int i = 0; i <= e.Variables.Length - 1; i++) { context.Set(e.Variables[i].Name, values[i]); } } } foreach (IterationSumVariable variable in e.Variables) { context.Remove(variable.Name); } return(sum); }
public static VariableContextExpression ucomb(VariableContextExpression func, Matrix r, Vector u) { int count = func.Variables.Length; VariableContextExpression[] diffs = func.Variables.Select(i => ExpressionFuncs.diff(func, i.Name)).ToArray(); Expression e1 = null; for (int i = 0; i <= count - 1; i++) { Expression right = BinaryExpression.Create(BinaryOperator.Multiply, ConstantExpression.create(u[i] * u[i], 0, 0), new FunctionCallExpression("pow", new Expression[] { diffs[i].Expression, ConstantExpression.create(2, 0, 0) }, 0, 0)); e1 = e1 == null ? right : BinaryExpression.Create(BinaryOperator.Add, e1, right); } Expression e2 = null; for (int i = 1; i <= count - 1; i++) { for (int j = i + 1; j <= count; j++) { Expression right = BinaryExpression.Create(BinaryOperator.Multiply, ConstantExpression.create(u[i - 1] * u[j - 1] * r[i - 1, j - 1], 0, 0), BinaryExpression.Create(BinaryOperator.Multiply, diffs[i - 1].Expression, diffs[j - 1].Expression)); e2 = e2 == null ? right : BinaryExpression.Create(BinaryOperator.Add, e2, right); } } e2 = BinaryExpression.Create(BinaryOperator.Multiply, ConstantExpression.create(2, 0, 0), e2); Expression e = BinaryExpression.Create(BinaryOperator.Add, e1, e2); VariableContext c = func.VariableContext; TypeCheckingContext context = new MathyLanguageService().CreateTypeCheckingContext(c); foreach (VariableInfo variable in func.Variables) { c.Set(variable.Name, context.CreateAutoCreatedVariableValue(variable.Type)); } VariableContextExpression result = new VariableContextExpression(e, func.Variables, 0, 0) { VariableContext = func.VariableContext }; new MathyLanguageService().CreateTypeChecker().PerformTypeChecking(result, context); result = new VariableContextExpression(new Cherimoya.Reduction.ExpressionReductor(new MathyLanguageService()).Reduce(e), func.Variables, 0, 0) { VariableContext = func.VariableContext }; new MathyLanguageService().CreateTypeChecker().PerformTypeChecking(result, context); foreach (VariableInfo variable in func.Variables) { context.VariableContext.Remove(variable.Name); } return(result); }