/// <summary>Create an instance of a <see cref="Calculator"/> which has it's own operators and variables.</summary> /// <param name="context">The math context.</param> /// <param name="variables">A collection of variables.</param> public Calculator(IMathContext context, IVariablesCollection?variables = default) { context.EnsureNotNull(nameof(context)); _mathContext = context; _evaluator = new ExpressionEvaluator(_mathContext, this); this["PI"] = Math.PI; this["E"] = Math.E; variables?.CopyTo(this); }
/// <summary>Initializez a new instance of an expression optimizer.</summary> /// <param name="mathContext">The math context used to evaluate expressions.</param> public ExpressionOptimizer(IMathContext mathContext) { mathContext.EnsureNotNull(nameof(mathContext)); _context = mathContext; _expressionOptimizers = new Dictionary <ExpressionType, Func <IExpression, IExpression> > { [ExpressionType.BinaryExpression] = OptimizeBinaryExpression, [ExpressionType.UnaryExpression] = EvaluateUnaryExpression, [ExpressionType.ConstantExpression] = OptimizeConstantExpression, [ExpressionType.GroupingExpression] = OptimizeGroupingExpression, [ExpressionType.VariableExpression] = SkipExpressionOptimization, [ExpressionType.ValueExpression] = SkipExpressionOptimization }; }
/// <summary>Initializez a new instance of an expression evaluator.</summary> /// <param name="context">The math context.</param> /// <param name="variables">The variables collection.</param> public ExpressionEvaluator(IMathContext context, IVariablesCollection variables) { context.EnsureNotNull(nameof(context)); variables.EnsureNotNull(nameof(variables)); _variables = variables; _context = context; _expressionEvaluators = new Dictionary <ExpressionType, Func <IExpression, ValueExpression> > { [ExpressionType.BinaryExpression] = EvaluateBinaryExpression, [ExpressionType.UnaryExpression] = EvaluateUnaryExpression, [ExpressionType.ConstantExpression] = EvaluateConstantExpression, [ExpressionType.GroupingExpression] = EvaluateGroupingExpression, [ExpressionType.VariableExpression] = EvaluateVariableExpression }; }