コード例 #1
0
ファイル: EvalApp.cs プロジェクト: zigaosolin/SharpMedia
        public override int Start(string verb, string[] arguments)
        {
            if (arguments.Length != 1)
            {
                throw new ArgumentException("Invalid arguments, expecting only one argument.");
            }


            Expression expression = Expression.Parse(arguments[0]);

            // We set parameters.
            Expression.FunctionParams p = expression.GetParams(false);

            // We apply some defined.
            p["PI"] = MathHelper.PI;
            p["pi"] = MathHelper.PI;
            p["E"]  = MathHelper.E;
            p["e"]  = MathHelper.E;

            foreach (KeyValuePair <string, double> element in parameters)
            {
                p[element.Key] = element.Value;
            }

            // We evaluate function.
            console.Out.WriteLine(expression.Evald(p, functionsSet,
                                                   typeof(EvalApp).Module).ToString(CultureInfo.InvariantCulture.NumberFormat));

            return(0);
        }
コード例 #2
0
        public void QuadraticTest()
        {
            Expression exp = Expression.Parse("quadratic", "(3+2)^2*x^2+(7/4)*x+(x/4)");

            Expression.FunctionParams p = exp.Params;
            Functionf f = exp.GetFunctionf(p, FunctionSet.Default);

            Assert.AreEqual(f(1.0f), (25.0f + 7.0f / 4.0f + 0.25f));
        }
コード例 #3
0
        internal void MultiDimToSingle()
        {
            Expression exp = Expression.Parse("x^x,x^(x^x+1),x*x");

            Expression.FunctionParams p = exp.GetParams(false);
            p.SetBinding("x", exp.Variable(0));
            Functiond f = exp.GetFunctiond(p, FunctionSet.Default);

            Assert.AreEqual(f(2.0f), 4.0f);
        }
コード例 #4
0
        public void LinearTest()
        {
            // Prepare; x must be auto bound.
            Expression exp = Expression.Parse("linear", "k*x+n");

            Expression.FunctionParams p = exp.GetParams(true);
            p["k"] = 2.0;
            p["n"] = -2.0;
            Functionf f = exp.GetFunctionf(p, FunctionSet.Default);

            Assert.AreEqual(f(1.0f), 0.0f);
            Assert.AreEqual(f(2.0f), 2.0f);
        }
コード例 #5
0
        public void IdentityTest()
        {
            // Construct expression.
            Expression exp = Expression.Parse("x");

            Expression.FunctionParams p = exp.Params;
            p.SetBinding("x", exp.Variable(0));
            Functionf f = exp.GetFunctionf(p, null);

            // Test it.
            for (float x = 0.0f; x < 100.0f; x += 1.0f)
            {
                Assert.AreEqual(f(x), x);
            }
        }