Beispiel #1
0
        public void TestRound(string input, int decimals, string expected)
        {
            var number = Number.Parse(input, CultureInfo.InvariantCulture);
            var op     = NumberMath.Round(number, decimals).ToString();

            Assert.AreEqual(expected, op);
        }
Beispiel #2
0
 protected override Number Evaluate(Number number)
 {
     return(NumberMath.Round(NumberMath.Cos(number), 21));
 }
        /// <summary>
        /// Integrates the expression with Simpson algorithm
        /// </summary>
        /// <param name="expression">Expression to integrate</param>
        /// <param name="var">Integrate by which expression</param>
        /// <param name="from">Start value</param>
        /// <param name="to">End value</param>
        /// <param name="steps">Number of steps</param>
        /// <returns>Integration result</returns>
        public static INumber Integrate(this IExpression expression, string var, INumber from, INumber to, int steps = 2048)
        {
            Number?_from = from as Number;
            Number?_to   = to as Number;

            if (_from == null || _to == null)
            {
                throw new ExpressionEngineException();
            }

            if (_to < _from)
            {
                throw new ArgumentException(Resources.IntegrateErrorRange);
            }

            if (steps < 2 || (steps % 2 == 1))
            {
                throw new ArgumentException(Resources.InvalidSteps);
            }

            var flatExpression = expression.Flatten();

            var vars = flatExpression.FirstOrDefault(e => e.Variables != null)?.Variables;

            bool trigonometric = flatExpression.Any(e => IsTrigonometricNode(e));

            if (trigonometric)
            {
                switch (NumberMath.AngleMode)
                {
                case AngleMode.Deg:
                    _from = NumberMath.DegToRad(_from);
                    _to   = NumberMath.DegToRad(_to);
                    break;

                case AngleMode.Grad:
                    _from = NumberMath.DegToGrad(_from);
                    _to   = NumberMath.DegToGrad(_to);
                    break;
                }
            }

            if (vars == null)
            {
                throw new InvalidOperationException(Resources.NoVariables);
            }

            if (string.IsNullOrEmpty(var))
            {
                throw new ArgumentNullException(nameof(var));
            }

            AngleMode old = NumberMath.AngleMode;

            NumberMath.AngleMode = AngleMode.Rad;

            Number h = (_to - _from) / steps;
            Number x = _from + h;
            Number s = 0.0;

            for (int i = 1; i < steps / 2; i++)
            {
                vars[var] = x;
                Number Fx = (expression.Evaluate() as Number) !;
                vars[var] = x + h;
                Number Fx2 = (expression.Evaluate() as Number) !;

                s  = s + (2 * Fx) + Fx2;
                x += 2 * h;
            }

            vars[var] = from;
            Number Fxa = (expression.Evaluate() as Number) !;

            vars[var] = to;
            Number Fxb = (expression.Evaluate() as Number) !;

            vars[var] = _to - h;
            Number Fxhb = (expression.Evaluate() as Number) !;

            Number result = (h / 3) * ((2 * s) + Fxa + Fxb + (4 * Fxhb));

            NumberMath.AngleMode = old;

            return(NumberMath.Round(result, 15));
        }