/// <summary> /// Calculates the value for a given calculation expression based on the supplied datafields (if datafields are part of the calculation expression) /// </summary> /// <param name="expression"></param> /// <param name="precision"></param> /// <param name="datafields"></param> /// <returns></returns> public double Calculate(string expression, int precision, List <DataFieldResult> datafields) { var filledExpression = expression; if (string.IsNullOrEmpty(filledExpression) == false) { foreach (var item in datafields) { if (item.FieldType == DataFieldType.Currency) { filledExpression = filledExpression.Replace("[" + item.Name + "]", MakeCurrencyTextParseableAsFloat(item.Value)); } else { filledExpression = filledExpression.Replace("[" + item.Name + "]", item.Value.Replace(',', '.').Replace(" ", string.Empty).Replace("'", string.Empty)); } } var calculator = new XtensibleCalculator(); var linqExpression = calculator.ParseExpression(filledExpression); var linqFunction = linqExpression.Compile(); var retVal = linqFunction(); if (precision >= 0) { retVal = Math.Round(retVal, precision); } return(retVal); } else { return(0); } }
public Evaluator() { calc = new XtensibleCalculator(); calc.RegisterFunction("sqrt", Math.Sqrt); calc.RegisterFunction("min", Math.Min); calc.RegisterFunction("max", Math.Max); }
static void Main(string[] args) { var calc = new XtensibleCalculator() .RegisterFunction("Multiply", (a, b, c) => a * b * c); var func = calc.ParseExpression("Multiply(x, y, PI)", x => 2, y => 2 + 3).Compile(); Console.WriteLine("Result: {0}", func()); Console.ReadKey(); }
static void Main(string[] args) { var calc = new XtensibleCalculator() .RegisterFunction("Multiply", (a, b, c) => a * b * c) .RegisterFunction("MultiplySquare", "Multiply(a, b, c) * Multiply(a, b, c)", "a", "b", "c"); var func = calc.ParseExpression("Multiply(x, y, PI)", x => 2, y => 2 + 3).Compile(); var product = calc.ParseExpression("MultiplySquare(a, b, c)", new Dictionary <string, double> { { "a", 1 }, { "b", 2 }, { "c", 3 } }).Compile(); Console.WriteLine($"Multiply: {func()}"); Console.WriteLine($"Product: {product()}"); Console.ReadKey(); }
public void DrawRealPlot(Graphics g, string xt, string yt) { try { g.Clear(BC); DrawAxesNet(g); int width = (int)g.ClipBounds.Width, height = (int)g.ClipBounds.Height; XtensibleCalculator c = new XtensibleCalculator(); Func <Dictionary <string, double>, double> fx, fy; var exp = c.ParseFunction(xt); fx = exp.Compile(); exp = c.ParseFunction(yt); fy = exp.Compile(); Dictionary <string, double> Dict = new Dictionary <string, double>(); Dict.Add("t", StartT); Point p = new Point(conv.II(fx(Dict)), conv.JJ(-fy(Dict))); for (double i = StartT + Step; i < EndT; i += Step) { Dict["t"] = i; Point pt = new Point(conv.II(fx(Dict)), conv.JJ(-fy(Dict))); try { g.DrawLine(new Pen(LC), p, pt); } catch (OverflowException) { break; } p = pt; } } catch (Sprache.ParseException) { g.Clear(BC); DrawAxesNet(g); } }
public double SearchForT(int x, int y, string xt, string yt) { XtensibleCalculator c = new XtensibleCalculator(); Func <Dictionary <string, double>, double> fx, fy; var exp = c.ParseFunction(xt); fx = exp.Compile(); exp = c.ParseFunction(yt); fy = exp.Compile(); Dictionary <string, double> Dict = new Dictionary <string, double>(); Dict.Add("t", 0); for (double i = StartT; i < EndT; i += Step / 10) { Dict["t"] = i; Point pt = new Point(conv.II(fx(Dict)), conv.JJ(-fy(Dict))); if (Math.Abs(pt.X - x) < 5 && Math.Abs(pt.Y - y) < 5) { return(Dict["t"]); } } return(5000); }