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);
        }
        public void TestCos(double input, double expected)
        {
            NumberMath.AngleMode = AngleMode.Deg;
            Number result = NumberMath.Cos(input);

            AreEqual(expected, result, 1E-12);
        }
Beispiel #3
0
 public static PolynomialTerm Cos(PolynomialTerm name)
 {
     if (name.IsConstant)
     {
         return(NumberMath.RoundedCos((double)name));
     }
     else
     {
         return(IndeterminateExpression.Cos((char)name));
     }
 }
Beispiel #4
0
 private void AreEqual(Number expected, Number result, Number diff)
 {
     if (result.State == NumberState.Value &&
         expected.State == NumberState.Value)
     {
         bool assert = NumberMath.Abs(expected - result) < diff;
         Assert.IsTrue(assert);
     }
     else
     {
         Assert.IsTrue(expected.State == result.State);
     }
 }
Beispiel #5
0
        public static void AreEqual(Number expected, Number result, double delta = 0.0)
        {
            if (NumberAlgorithms.IsSpecialState(expected) &&
                NumberAlgorithms.IsSpecialState(result) &&
                expected.State != result.State)
            {
                Assert.Fail("Expected: {0}, result: {1}", expected, result);
            }
            else
            {
                Assert.Pass();
            }


            var diff = NumberMath.Abs(expected - result) < delta;

            if (!diff)
            {
                Assert.Fail("Expected: {0}, result: {1}", expected, result);
            }

            Assert.Pass();
        }
Beispiel #6
0
 protected override Number Evaluate(Number number1, Number number2)
 {
     return(NumberMath.Pow(number1, number2));
 }
Beispiel #7
0
        public override IExpression?Differentiate(string byVariable)
        {
            if (Right is ConstantExpression)
            {
                // f(x) = g(x)^n
                // f'(x) = n * g'(x) * g(x)^(n-1)
                return
                    (new MultiplyExpression(new MultiplyExpression(Right, Left?.Differentiate(byVariable)),
                                            new ExponentExpression(Left, new SubtractExpression(Right, new ConstantExpression(1)))));
            }
            var simple = Left?.Simplify();

            if (simple is ConstantExpression constant)
            {
                // f(x) = a^g(x)
                // f'(x) = (ln a) * g'(x) * a^g(x)
                var a = constant.Value;
                return(new MultiplyExpression(new MultiplyExpression(new ConstantExpression(NumberMath.Ln(a)), Right?.Differentiate(byVariable)), new ExponentExpression(simple, Right)));
            }
            throw new CannotDifferentiateException(Resources.CanotDifferentiate);
        }
Beispiel #8
0
 protected override Number Evaluate(Number number)
 {
     return(NumberMath.Round(NumberMath.Cos(number), 21));
 }
Beispiel #9
0
        public void TestFactorial(int n, double expected)
        {
            Number result = NumberMath.Factorial(n);

            AreEqual(expected, result, 1E-9);
        }
Beispiel #10
0
        public void TestRadToGrad(double rad, double expectedGrad)
        {
            Number result = NumberMath.RadToGrad(rad);

            AreEqual(expectedGrad, result, 1E-9);
        }
 private static Number FactorialWrapper(Number arg)
 {
     return(NumberMath.Factorial((int)arg.ToDouble()));
 }
Beispiel #12
0
        public void TestGradToDeg(double grad, double expectedDeg)
        {
            Number result = NumberMath.GradToDeg(grad);

            AreEqual(expectedDeg, result, 1E-9);
        }
Beispiel #13
0
        public void TestDegToGrad(double deg, double expectedGrad)
        {
            Number result = NumberMath.DegToGrad(deg);

            AreEqual(expectedGrad, result, 1E-9);
        }
 public void RoundedCosTests(double value, double expected)
 {
     NumberMath.RoundedCos(value).AssertIsEqualTo(expected);
 }
Beispiel #15
0
 protected override Number Evaluate(Number number)
 {
     return(NumberMath.Ln(number));
 }
Beispiel #16
0
        public void TestGradToRad(double grad, double expectedRad)
        {
            Number result = NumberMath.GradToRad(grad);

            AreEqual(expectedRad, result, 1E-9);
        }
        /// <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));
        }