Beispiel #1
0
        public override double EvaluateVarFunction(VarFuncToken token)
        {
            Func <double, double> func;
            int start;

            switch (token.Symbol)
            {
            case ParserSymbols.Sum:
                func = ExpressionTreeEvaluator.CreateDelegate(token.SubTokens[3],
                                                              token.VariableSymbol, parser, Context.Options);
                start = (int)eval(token.SubTokens[1]);
                return(Enumerable.Range(start, (int)eval(token.SubTokens[2]) - start + 1)
                       .Select(v => func(v)).Sum());

            case ParserSymbols.Prod:
                func = ExpressionTreeEvaluator.CreateDelegate(token.SubTokens[3],
                                                              token.VariableSymbol, parser, Context.Options);
                start = (int)eval(token.SubTokens[1]);
                return(Enumerable.Range(start, (int)eval(token.SubTokens[2]) - start + 1)
                       .Select(v => func(v))
                       .Aggregate((v1, v2) => v1 * v2));

            default:
                if (Context.Options.StrictMode)
                {
                    throw new UnsupportedOperationException(token.Symbol);
                }
                return(double.NaN);
            }
        }
        public static Func <double, double> CreateDelegate(TreeToken token, string variable, Parser parser, EvaluationOptions options)
        {
            var paramColl  = new ParameterCollection <double>(variable);
            var context    = new EvaluationContext(options, paramColl);
            var eval       = new ExpressionTreeEvaluator(parser, context);
            var expr       = token.Evaluate(eval);
            var parameters = paramColl.GetParameters();

            return(Expression.Lambda <Func <double, double> >(expr, parameters).Compile());
        }
        public override Expression EvaluateVarFunction(VarFuncToken token)
        {
            Func <double, double> func;

            switch (token.Symbol)
            {
            case ParserSymbols.Sum:
                func = ExpressionTreeEvaluator.CreateDelegate(token.SubTokens[3],
                                                              token.VariableSymbol, parser, Context.Options);
                return(iterateExpr(0, token.SubTokens[1], token.SubTokens[2], (v, i) => v + func(i)));

            case ParserSymbols.Prod:
                func = ExpressionTreeEvaluator.CreateDelegate(token.SubTokens[3],
                                                              token.VariableSymbol, parser, Context.Options);
                return(iterateExpr(1, token.SubTokens[1], token.SubTokens[2], (v, i) => v * func(i)));

            default:
                if (Context.Options.StrictMode)
                {
                    throw new UnsupportedOperationException(token.Symbol);
                }
                return(Expression.Constant(double.NaN));
            }
        }