Beispiel #1
0
        public void CompileSampleFunc()
        {
            var exprString = "x ^ 3 + sin(3 * ln(x * 1)) + x ^ ln(2 * sin(3 * ln(x))) - 2 * x ^ 3";
            var dotnetFunc = new Func <double, double>(x =>
                                                       Math.Pow(x, 3) + Math.Sin(3 * Math.Log(x * 1)) + Math.Pow(x, Math.Log(2 * Math.Sin(3 * Math.Log(x))) - 2 * Math.Pow(x, 3)));

            using (var mathAssembly = new MathAssembly(exprString, "x"))
            {
                var rand = new Random();
                int i    = 0;
                while (i < 5)
                {
                    double x      = rand.NextDouble();
                    var    dotnet = dotnetFunc(x);
                    if (!double.IsNaN(dotnet))
                    {
                        var correct = WolframAlphaUtils.GetValue(exprString, new KeyValuePair <string, double>("x", x));
                        var actual  = mathAssembly.Func(x);
                        Assert.LessOrEqual(Math.Abs(correct - actual), Math.Abs(dotnet - actual));
                        i++;
                    }
                }
            }
        }
Beispiel #2
0
        public static void PrecompileTest2()
        {
            MathFunc func = new MathFunc("-(sin(x) ^ -2 * x) + -(sin(x) ^ -1 * cos(x)) + cos(x) ^ -2 * x + cos(x) ^ -1 * sin(x) ^ -1");

            Assert.IsTrue(WolframAlphaUtils.CheckEquality(func.ToString(), func.GetPrecompilied().ToString()));
        }
Beispiel #3
0
        public void CheckDerivativeWithWolframAlpha(string expression)
        {
            var derivativeExpression = new MathFunc(expression).GetDerivative().GetPrecompilied().ToString();

            Assert.IsTrue(WolframAlphaUtils.CheckDerivative(expression, derivativeExpression));
        }