Beispiel #1
0
        public void When_Conditional_Expect_Reference()
        {
            var parser = new SimpleDerivativeParser();

            Check(1, parser.Parse("1 >= 0 ? 1 : 2"));
            Check(2, parser.Parse("1 >= 3 ? 1 : 2"));
        }
Beispiel #2
0
        public void When_Brackets_Expect_Reference()
        {
            var parser = new SimpleDerivativeParser();

            Check(1 - (5.8 - 12) - 3, parser.Parse("1 - (5.8 - 12) - 3"));
            Check(2 * (2 + 3) * 4, parser.Parse("2 * ((2 + 3)) * 4"));
        }
Beispiel #3
0
        protected void Check(Func <double, double> reference, SimpleDerivativeParser parser, string expression)
        {
            double x = 0;

            void VariableFound(object sender, VariableFoundEventArgs <Derivatives <Func <double> > > e)
            {
                if (e.Name == "x")
                {
                    e.Result    = new DoubleDerivatives();
                    e.Result[0] = () => x;
                }
            };
            parser.VariableFound += VariableFound;


            for (x = -2; x <= 2; x += 0.5)
            {
                var expected = reference(x);
                var parsed   = parser.Parse(expression);

                var actual = parsed[0].Invoke();
                var tol    = Math.Max(Math.Abs(expected), Math.Abs(actual)) * RelativeTolerance + AbsoluteTolerance;
                Assert.AreEqual(expected, actual, tol);
            }
            parser.VariableFound -= VariableFound;
        }
Beispiel #4
0
        protected void Check(Func <double[], double>[] reference, SimpleDerivativeParser parser, string expression)
        {
            double[] x = new double[reference.Length];
            void VariableFound(object sender, VariableFoundEventArgs <Derivatives <Func <double> > > e)
            {
                if (e.Name.Length == 1)
                {
                    var c = e.Name[0] - 'a';
                    if (c >= 0 && c < reference.Length - 1)
                    {
                        e.Result        = new DoubleDerivatives();
                        e.Result[0]     = () => x[c];
                        e.Result[c + 1] = () => 1.0;
                    }
                }
            }

            parser.VariableFound += VariableFound;

            // Iterate through a number of values
            for (var i = 0; i < x.Length; i++)
            {
                x[i] = -2.0;
            }
            while (true)
            {
                var parsed = parser.Parse(expression);
                Derivatives <Func <double> > .FudgeFactor = 0; // Don't fudge the numbers, we're testing here!
                for (var i = 0; i < reference.Length; i++)
                {
                    var expected = reference[i](x);
                    var actual   = parsed[i].Invoke();
                    var tol      = Math.Max(Math.Abs(expected), Math.Abs(actual)) * RelativeTolerance + AbsoluteTolerance;
                    Assert.AreEqual(expected, actual, tol);
                }

                // Find the last index that reached the maximum
                var index = x.Length - 1;
                while (index >= 0 && x[index] >= 2.0)
                {
                    index--;
                }
                if (index < 0)
                {
                    break;
                }
                x[index] += 0.5;
                for (var i = index + 1; i < x.Length; i++)
                {
                    x[i] = 0.0;
                }
            }
            parser.VariableFound -= VariableFound;
        }
Beispiel #5
0
        public void When_Power_Expect_Reference()
        {
            var parser = new SimpleDerivativeParser();

            Check(Math.Pow(2, Math.Pow(0.5, 3)), parser.Parse("2^0.5^3"));
        }
Beispiel #6
0
        public void When_Division_Expect_Reference()
        {
            var parser = new SimpleDerivativeParser();

            Check(4 / 0.4 / 2.8, parser.Parse("4 / 0.4 / 2.8"));
        }
Beispiel #7
0
        public void When_Multiplication_Expect_Reference()
        {
            var parser = new SimpleDerivativeParser();

            Check(3 * 1.8 * 0.9, parser.Parse("3 * 1.8 * 0.9"));
        }
Beispiel #8
0
        public void When_Subtraction_Expect_Reference()
        {
            var parser = new SimpleDerivativeParser();

            Check(2 - 5.8 - 12, parser.Parse("2 - 5.8 - 12"));
        }
Beispiel #9
0
        public void When_Addition_Expect_Reference()
        {
            var parser = new SimpleDerivativeParser();

            Check(1 + 2.5 + 10.8, parser.Parse("1 + 2.5 + 10.8"));
        }