Exemplo n.º 1
0
 public static void Register(CalculationEngine ce)
 {
     ce.RegisterFunction("AND", 1, int.MaxValue, And);
     ce.RegisterFunction("OR", 1, int.MaxValue, Or);
     ce.RegisterFunction("NOT", 1, Not);
     ce.RegisterFunction("IF", 3, If);
     ce.RegisterFunction("TRUE", 0, True);
     ce.RegisterFunction("FALSE", 0, False);
 }
        public string ExampleCalculation()
        {
            double h = 0.002, sum = 0, I;

            CalculationEngine calculation = new CalculationEngine();

            if (!ExpressionChanges.Check_string(expression))
            {
                return("error");
            }
            if (ExpressionChanges.IsEmpty(botLim) || ExpressionChanges.IsEmpty(upperLim))
            {
                return("error_Lim");
            }

            if (Double.Parse(botLim) > Double.Parse(upperLim))
            {
                return("error_Lim");
            }

            try
            {
                for (double Xi = Convert.ToDouble(botLim) + h; Xi < Convert.ToDouble(upperLim); Xi = Xi + h)
                {
                    I = 0;

                    calculation.Variables["x"] = Xi;
                    I += (double)calculation.Evaluate(expression);
                    calculation.Variables["x"] = Xi - h;
                    I += (double)calculation.Evaluate(expression);
                    calculation.Variables["x"] = Xi - h / 2;
                    I   += 4 * (double)calculation.Evaluate(expression);
                    sum += I;
                }
            }
            catch (Exception e)
            {
                return("error");
            }

            double res = h / 6 * sum;

            if (Double.IsNaN(res))
            {
                return("error_ODZ");
            }

            return(res.ToString());
        }
Exemplo n.º 3
0
        private static object ContainsFn(CalculationEngine engine, List <Expression> parms)
        {
            var containsTrimStartChars = engine.Options.Functions.ContainsTrimStartChars.ToArray();
            var input    = parms[0].Evaluate();
            var includes = input as IList ?? new[] { input };
            var target   = parms[1].Evaluate() ?? "";
            var excludes = new List <string>();

            if (input is string s)
            {
                char separator = ';';
                if (parms.Count == 3)
                {
                    separator = (parms[2].Evaluate() as string ?? ";")[0];
                }

                includes = s.Split(separator);

                excludes = includes.Cast <object>().Where(o => ((string)o).Trim().StartsWith("!")).Select(o => o.ToString().Trim().TrimStart('!')).ToList();
                includes = includes.Cast <object>().Where(o => !((string)o).Trim().StartsWith("!")).Select(o => o.ToString().Trim()).ToList();
            }

            var targets = target as IEnumerable ?? new Object[] { target };

            if (target is string t)
            {
                targets = new[] { t };
            }

            var result = false;

            foreach (var o in targets)
            {
                if (includes.Cast <object>().Any(o1 => string.Equals(o1?.ToString().TrimStart(containsTrimStartChars), o?.ToString().TrimStart(containsTrimStartChars), StringComparison.InvariantCultureIgnoreCase)))
                {
                    result = true;
                }

                if (excludes.Any(o1 => string.Equals(o1?.ToString().TrimStart(containsTrimStartChars), o?.ToString(), StringComparison.InvariantCultureIgnoreCase)))
                {
                    result = false;
                    break;
                }
            }

            return(result);
        }
Exemplo n.º 4
0
        static object CountIf(CalculationEngine ce, List <Expression> p)
        {
            var cnt   = 0.0;
            var ienum = p[0] as IEnumerable;

            if (ienum != null)
            {
                var crit = (string)p[1].Evaluate();
                foreach (var value in ienum)
                {
                    if (!IsBlank(value))
                    {
                        var exp = string.Format("{0}{1}", value, crit);
                        if ((bool)ce.Evaluate(exp))
                        {
                            cnt++;
                        }
                    }
                }
            }

            return(cnt);
        }
Exemplo n.º 5
0
 public FunctionExpression(FunctionDefinition function, List <Expression> parms, CalculationEngine engine)
 {
     _fn     = function;
     Parms   = parms;
     _engine = engine;
 }
Exemplo n.º 6
0
 public ExpressionCache(CalculationEngine ce)
 {
     _ce  = ce;
     _dct = new Dictionary <string, WeakReference>();
 }
Exemplo n.º 7
0
 // ** ctor
 internal BindingExpression(CalculationEngine engine, List <BindingInfo> bindingPath, CultureInfo ci)
 {
     _ce          = engine;
     _bindingPath = bindingPath;
     _ci          = ci;
 }
Exemplo n.º 8
0
 public static void Register(CalculationEngine ce)
 {
     ce.RegisterFunction("ABS", 1, Abs);
     ce.RegisterFunction("ACOS", 1, Acos);
     //ce.RegisterFunction("ACOSH", Acosh, 1);
     ce.RegisterFunction("ASIN", 1, Asin);
     //ce.RegisterFunction("ASINH", Asinh, 1);
     ce.RegisterFunction("ATAN", 1, Atan);
     ce.RegisterFunction("ATAN2", 2, Atan2);
     //ce.RegisterFunction("ATANH", Atanh, 1);
     ce.RegisterFunction("CEILING", 1, Ceiling);
     //ce.RegisterFunction("COMBIN", Combin, 1);
     ce.RegisterFunction("COS", 1, Cos);
     ce.RegisterFunction("COSH", 1, Cosh);
     //ce.RegisterFunction("DEGREES", Degrees, 1);
     //ce.RegisterFunction("EVEN", Even, 1);
     ce.RegisterFunction("EXP", 1, Exp);
     //ce.RegisterFunction("FACT", Fact, 1);
     //ce.RegisterFunction("FACTDOUBLE", FactDouble, 1);
     ce.RegisterFunction("FLOOR", 1, Floor);
     //ce.RegisterFunction("GCD", Gcd, 1);
     ce.RegisterFunction("INT", 1, Int);
     //ce.RegisterFunction("LCM", Lcm, 1);
     ce.RegisterFunction("LN", 1, Ln);
     ce.RegisterFunction("LOG", 1, 2, Log);
     ce.RegisterFunction("LOG10", 1, Log10);
     //ce.RegisterFunction("MDETERM", MDeterm, 1);
     //ce.RegisterFunction("MINVERSE", MInverse, 1);
     //ce.RegisterFunction("MMULT", MMult, 1);
     //ce.RegisterFunction("MOD", Mod, 2);
     //ce.RegisterFunction("MROUND", MRound, 1);
     //ce.RegisterFunction("MULTINOMIAL", Multinomial, 1);
     //ce.RegisterFunction("ODD", Odd, 1);
     ce.RegisterFunction("PI", 0, Pi);
     ce.RegisterFunction("POWER", 2, Power);
     //ce.RegisterFunction("PRODUCT", Product, 1);
     //ce.RegisterFunction("QUOTIENT", Quotient, 1);
     //ce.RegisterFunction("RADIANS", Radians, 1);
     ce.RegisterFunction("RAND", 0, Rand);
     ce.RegisterFunction("RANDBETWEEN", 2, RandBetween);
     //ce.RegisterFunction("ROMAN", Roman, 1);
     //ce.RegisterFunction("ROUND", Round, 1);
     //ce.RegisterFunction("ROUNDDOWN", RoundDown, 1);
     //ce.RegisterFunction("ROUNDUP", RoundUp, 1);
     //ce.RegisterFunction("SERIESSUM", SeriesSum, 1);
     ce.RegisterFunction("SIGN", 1, Sign);
     ce.RegisterFunction("SIN", 1, Sin);
     ce.RegisterFunction("SINH", 1, Sinh);
     ce.RegisterFunction("SQRT", 1, Sqrt);
     //ce.RegisterFunction("SQRTPI", SqrtPi, 1);
     //ce.RegisterFunction("SUBTOTAL", Subtotal, 1);
     ce.RegisterFunction("SUM", 1, int.MaxValue, Sum);
     //ce.RegisterFunction("SUMIF", SumIf, 1);
     //ce.RegisterFunction("SUMPRODUCT", SumProduct, 1);
     //ce.RegisterFunction("SUMSQ", SumSq, 1);
     //ce.RegisterFunction("SUMX2MY2", SumX2MY2, 1);
     //ce.RegisterFunction("SUMX2PY2", SumX2PY2, 1);
     //ce.RegisterFunction("SUMXMY2", SumXMY2, 1);
     ce.RegisterFunction("TAN", 1, Tan);
     ce.RegisterFunction("TANH", 1, Tanh);
     ce.RegisterFunction("TRUNC", 1, Trunc);
 }
Exemplo n.º 9
0
 public static void Register(CalculationEngine ce)
 {
     //ce.RegisterFunction("AVEDEV", AveDev, 1, int.MaxValue);
     ce.RegisterFunction("AVERAGE", 1, int.MaxValue, Average);
     ce.RegisterFunction("AVERAGEA", 1, int.MaxValue, AverageA);
     //BETADIST	Returns the beta cumulative distribution function
     //BETAINV	Returns the inverse of the cumulative distribution function for a specified beta distribution
     //BINOMDIST	Returns the individual term binomial distribution probability
     //CHIDIST	Returns the one-tailed probability of the chi-squared distribution
     //CHIINV	Returns the inverse of the one-tailed probability of the chi-squared distribution
     //CHITEST	Returns the test for independence
     //CONFIDENCE	Returns the confidence interval for a population mean
     //CORREL	Returns the correlation coefficient between two data sets
     ce.RegisterFunction("COUNT", 1, int.MaxValue, Count);
     ce.RegisterFunction("COUNTA", 1, int.MaxValue, CountA);
     ce.RegisterFunction("COUNTBLANK", 1, int.MaxValue, CountBlank);
     ce.RegisterFunction("COUNTIF", 2, CountIf);
     //COVAR	Returns covariance, the average of the products of paired deviations
     //CRITBINOM	Returns the smallest value for which the cumulative binomial distribution is less than or equal to a criterion value
     //DEVSQ	Returns the sum of squares of deviations
     //EXPONDIST	Returns the exponential distribution
     //FDIST	Returns the F probability distribution
     //FINV	Returns the inverse of the F probability distribution
     //FISHER	Returns the Fisher transformation
     //FISHERINV	Returns the inverse of the Fisher transformation
     //FORECAST	Returns a value along a linear trend
     //FREQUENCY	Returns a frequency distribution as a vertical array
     //FTEST	Returns the result of an F-test
     //GAMMADIST	Returns the gamma distribution
     //GAMMAINV	Returns the inverse of the gamma cumulative distribution
     //GAMMALN	Returns the natural logarithm of the gamma function, Γ(x)
     //GEOMEAN	Returns the geometric mean
     //GROWTH	Returns values along an exponential trend
     //HARMEAN	Returns the harmonic mean
     //HYPGEOMDIST	Returns the hypergeometric distribution
     //INTERCEPT	Returns the intercept of the linear regression line
     //KURT	Returns the kurtosis of a data set
     //LARGE	Returns the k-th largest value in a data set
     //LINEST	Returns the parameters of a linear trend
     //LOGEST	Returns the parameters of an exponential trend
     //LOGINV	Returns the inverse of the lognormal distribution
     //LOGNORMDIST	Returns the cumulative lognormal distribution
     ce.RegisterFunction("MAX", 1, int.MaxValue, Max);
     ce.RegisterFunction("MAXA", 1, int.MaxValue, MaxA);
     //MEDIAN	Returns the median of the given numbers
     ce.RegisterFunction("MIN", 1, int.MaxValue, Min);
     ce.RegisterFunction("MINA", 1, int.MaxValue, MinA);
     //MODE	Returns the most common value in a data set
     //NEGBINOMDIST	Returns the negative binomial distribution
     //NORMDIST	Returns the normal cumulative distribution
     //NORMINV	Returns the inverse of the normal cumulative distribution
     //NORMSDIST	Returns the standard normal cumulative distribution
     //NORMSINV	Returns the inverse of the standard normal cumulative distribution
     //PEARSON	Returns the Pearson product moment correlation coefficient
     //PERCENTILE	Returns the k-th percentile of values in a range
     //PERCENTRANK	Returns the percentage rank of a value in a data set
     //PERMUT	Returns the number of permutations for a given number of objects
     //POISSON	Returns the Poisson distribution
     //PROB	Returns the probability that values in a range are between two limits
     //QUARTILE	Returns the quartile of a data set
     //RANK	Returns the rank of a number in a list of numbers
     //RSQ	Returns the square of the Pearson product moment correlation coefficient
     //SKEW	Returns the skewness of a distribution
     //SLOPE	Returns the slope of the linear regression line
     //SMALL	Returns the k-th smallest value in a data set
     //STANDARDIZE	Returns a normalized value
     ce.RegisterFunction("STDEV", 1, int.MaxValue, StDev);
     ce.RegisterFunction("STDEVA", 1, int.MaxValue, StDevA);
     ce.RegisterFunction("STDEVP", 1, int.MaxValue, StDevP);
     ce.RegisterFunction("STDEVPA", 1, int.MaxValue, StDevPA);
     //STEYX	Returns the standard error of the predicted y-value for each x in the regression
     //TDIST	Returns the Student's t-distribution
     //TINV	Returns the inverse of the Student's t-distribution
     //TREND	Returns values along a linear trend
     //TRIMMEAN	Returns the mean of the interior of a data set
     //TTEST	Returns the probability associated with a Student's t-test
     ce.RegisterFunction("VAR", 1, int.MaxValue, Var);
     ce.RegisterFunction("VARA", 1, int.MaxValue, VarA);
     ce.RegisterFunction("VARP", 1, int.MaxValue, VarP);
     ce.RegisterFunction("VARPA", 1, int.MaxValue, VarPA);
     //WEIBULL	Returns the Weibull distribution
     //ZTEST	Returns the one-tailed probability-value of a z-test
 }