Esempio n. 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);
            }
        }
Esempio n. 2
0
        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));
            }
        }