Esempio n. 1
0
        private void RegisterDefaultFunctions()
        {
            FunctionRegistry.RegisterFunction("sin", (Func <double, double>)Math.Sin, true, false);
            FunctionRegistry.RegisterFunction("cos", (Func <double, double>)Math.Cos, true, false);
            FunctionRegistry.RegisterFunction("csc", (Func <double, double>)MathUtil.Csc, true, false);
            FunctionRegistry.RegisterFunction("sec", (Func <double, double>)MathUtil.Sec, true, false);
            FunctionRegistry.RegisterFunction("asin", (Func <double, double>)Math.Asin, true, false);
            FunctionRegistry.RegisterFunction("acos", (Func <double, double>)Math.Acos, true, false);
            FunctionRegistry.RegisterFunction("tan", (Func <double, double>)Math.Tan, true, false);
            FunctionRegistry.RegisterFunction("cot", (Func <double, double>)MathUtil.Cot, true, false);
            FunctionRegistry.RegisterFunction("atan", (Func <double, double>)Math.Atan, true, false);
            FunctionRegistry.RegisterFunction("acot", (Func <double, double>)MathUtil.Acot, true, false);
            FunctionRegistry.RegisterFunction("loge", (Func <double, double>)Math.Log, true, false);
            FunctionRegistry.RegisterFunction("log10", (Func <double, double>)Math.Log10, true, false);
            FunctionRegistry.RegisterFunction("logn", (Func <double, double, double>)((a, b) => Math.Log(a, b)), true, false);
            FunctionRegistry.RegisterFunction("sqrt", (Func <double, double>)Math.Sqrt, true, false);
            FunctionRegistry.RegisterFunction("abs", (Func <double, double>)Math.Abs, true, false);
            FunctionRegistry.RegisterFunction("if", (Func <double, double, double, double>)((a, b, c) => (a != 0.0 ? b : c)), true, false);
            FunctionRegistry.RegisterFunction("ifless", (Func <double, double, double, double, double>)((a, b, c, d) => (a < b ? c : d)), true, false);
            FunctionRegistry.RegisterFunction("ifmore", (Func <double, double, double, double, double>)((a, b, c, d) => (a > b ? c : d)), true, false);
            FunctionRegistry.RegisterFunction("ifequal", (Func <double, double, double, double, double>)((a, b, c, d) => (a == b ? c : d)), true, false);
            FunctionRegistry.RegisterFunction("ceiling", (Func <double, double>)Math.Ceiling, true, false);
            FunctionRegistry.RegisterFunction("floor", (Func <double, double>)Math.Floor, true, false);
            FunctionRegistry.RegisterFunction("truncate", (Func <double, double>)Math.Truncate, true, false);
            FunctionRegistry.RegisterFunction("round", (Func <double, double>)Math.Round, true, false);

            // Dynamic based arguments Functions
            FunctionRegistry.RegisterFunction("max", (DynamicFunc <double, double>)((a) => a.Max()), true, false);
            FunctionRegistry.RegisterFunction("min", (DynamicFunc <double, double>)((a) => a.Min()), true, false);
            FunctionRegistry.RegisterFunction("avg", (DynamicFunc <double, double>)((a) => a.Average()), true, false);
            FunctionRegistry.RegisterFunction("median", (DynamicFunc <double, double>)((a) => MathExtended.Median(a)), true, false);

            // Non Idempotent Functions
            FunctionRegistry.RegisterFunction("random", (Func <double>)random.NextDouble, false, false);
        }
Esempio n. 2
0
        private void RegisterDefaultFunctions()
        {
            FunctionRegistry.RegisterFunction("sin", new Func <float, float>(Mathf.Sin));
            FunctionRegistry.RegisterFunction("cos", new Func <float, float>(Mathf.Cos));
            FunctionRegistry.RegisterFunction("csc", new Func <float, float>(Mathf.Csc));
            FunctionRegistry.RegisterFunction("sec", new Func <float, float>(Mathf.Sec));
            FunctionRegistry.RegisterFunction("asin", new Func <float, float>(Mathf.Asin));
            FunctionRegistry.RegisterFunction("acos", new Func <float, float>(Mathf.Acos));
            FunctionRegistry.RegisterFunction("tan", new Func <float, float>(Mathf.Tan));
            FunctionRegistry.RegisterFunction("cot", new Func <float, float>(Mathf.Cot));
            FunctionRegistry.RegisterFunction("atan", new Func <float, float>(Mathf.Atan));
            FunctionRegistry.RegisterFunction("acot", new Func <float, float>(Mathf.Acot));
            FunctionRegistry.RegisterFunction("loge", new Func <float, float>(Mathf.Log));
            FunctionRegistry.RegisterFunction("log10", new Func <float, float>(Mathf.Log10));
            FunctionRegistry.RegisterFunction("sqrt", new Func <float, float>(Mathf.Sqrt));
            FunctionRegistry.RegisterFunction("abs", new Func <float, float>(Math.Abs));
            FunctionRegistry.RegisterFunction("ceiling", new Func <float, float>(Mathf.Ceiling));
            FunctionRegistry.RegisterFunction("floor", new Func <float, float>(Mathf.Floor));
            FunctionRegistry.RegisterFunction("truncate", new Func <float, float>(Mathf.Truncate));
            FunctionRegistry.RegisterFunction("round", new Func <float, float>(Mathf.Round));

            FunctionRegistry.RegisterFunction("logn", new Func <float, float, float>(Mathf.Logn));

            FunctionRegistry.RegisterFunction("if", (Func <float, float, float, float>)((a, b, c) => (a != 0.0f ? b : c)));
            FunctionRegistry.RegisterFunction("ifless", (Func <float, float, float, float, float>)((a, b, c, d) => (a < b ? c : d)));
            FunctionRegistry.RegisterFunction("ifmore", (Func <float, float, float, float, float>)((a, b, c, d) => (a > b ? c : d)));
            FunctionRegistry.RegisterFunction("ifequal", (Func <float, float, float, float, float>)((a, b, c, d) => (a == b ? c : d)));
        }
        public void ThrowArgumentException_WhenRemoveAll()
        {
            var registry = new FunctionRegistry();

            registry.RegisterFunction("a", Invalid2.A, FunctionScope.All);
            registry.Remove("a", FunctionScope.All);
        }
Esempio n. 4
0
 private void RegisterDefaultFunctions()
 {
     FunctionRegistry.RegisterFunction("sin", (Func <double, double>)((a) => Math.Sin(a)), false);
     FunctionRegistry.RegisterFunction("cos", (Func <double, double>)((a) => Math.Cos(a)), false);
     FunctionRegistry.RegisterFunction("csc", (Func <double, double>)((a) => MathUtil.Csc(a)), false);
     FunctionRegistry.RegisterFunction("sec", (Func <double, double>)((a) => MathUtil.Sec(a)), false);
     FunctionRegistry.RegisterFunction("asin", (Func <double, double>)((a) => Math.Asin(a)), false);
     FunctionRegistry.RegisterFunction("acos", (Func <double, double>)((a) => Math.Acos(a)), false);
     FunctionRegistry.RegisterFunction("tan", (Func <double, double>)((a) => Math.Tan(a)), false);
     FunctionRegistry.RegisterFunction("cot", (Func <double, double>)((a) => MathUtil.Cot(a)), false);
     FunctionRegistry.RegisterFunction("atan", (Func <double, double>)((a) => Math.Atan(a)), false);
     FunctionRegistry.RegisterFunction("acot", (Func <double, double>)((a) => MathUtil.Acot(a)), false);
     FunctionRegistry.RegisterFunction("loge", (Func <double, double>)((a) => Math.Log(a)), false);
     FunctionRegistry.RegisterFunction("log10", (Func <double, double>)((a) => Math.Log10(a)), false);
     FunctionRegistry.RegisterFunction("logn", (Func <double, double, double>)((a, b) => Math.Log(a, b)), false);
     FunctionRegistry.RegisterFunction("sqrt", (Func <double, double>)((a) => Math.Sqrt(a)), false);
     FunctionRegistry.RegisterFunction("abs", (Func <double, double>)((a) => Math.Abs(a)), false);
     FunctionRegistry.RegisterFunction("max", (Func <double, double, double>)((a, b) => Math.Max(a, b)), false);
     FunctionRegistry.RegisterFunction("min", (Func <double, double, double>)((a, b) => Math.Min(a, b)), false);
     FunctionRegistry.RegisterFunction("if", (Func <double, double, double, double>)((a, b, c) => (a != 0.0 ? b : c)), false);
     FunctionRegistry.RegisterFunction("ifless", (Func <double, double, double, double, double>)((a, b, c, d) => (a < b ? c : d)), false);
     FunctionRegistry.RegisterFunction("ifmore", (Func <double, double, double, double, double>)((a, b, c, d) => (a > b ? c : d)), false);
     FunctionRegistry.RegisterFunction("ifequal", (Func <double, double, double, double, double>)((a, b, c, d) => (a == b ? c : d)), false);
     FunctionRegistry.RegisterFunction("ceiling", (Func <double, double>)((a) => Math.Ceiling(a)), false);
     FunctionRegistry.RegisterFunction("floor", (Func <double, double>)((a) => Math.Floor(a)), false);
     FunctionRegistry.RegisterFunction("truncate", (Func <double, double>)((a) => Math.Truncate(a)), false);
 }
Esempio n. 5
0
        public void TestNotOverwritable()
        {
            FunctionRegistry registry = new FunctionRegistry(false);

            Func <double, double, double> testFunction1 = (a, b) => a * b;
            Func <double, double, double> testFunction2 = (a, b) => a * b;

            registry.RegisterFunction("test", testFunction1, false);
#if !NETCORE
            AssertExtensions.ThrowsException <Exception>(() =>
            {
                registry.RegisterFunction("test", testFunction2, false);
            });
#else
            Assert.Throws <Exception>(() =>
            {
                registry.RegisterFunction("test", testFunction2, false);
            });
#endif
        }
        public void Successfully_RegisterFunction()
        {
            var registry = new FunctionRegistry();

            registry.RegisterFunction("c", FunctionContainer.A, FunctionScope.Basic);
            var conf = new RollerConfig()
            {
                FunctionRegistry = registry, GetRandomBytes = GetRNG(Roll9())
            };

            EvaluateRoll("1d20.c()", conf, 1, "1d20.c() => 9 => 10");
        }
        public void TestAddFunc2()
        {
            FunctionRegistry registry = new FunctionRegistry();

            Func <float, float, float> testFunction = (a, b) => a * b;

            registry.RegisterFunction("test", testFunction);

            FunctionInfo functionInfo = registry.GetFunctionInfo("test");

            Assert.IsNotNull(functionInfo);
            Assert.AreEqual("test", functionInfo.FunctionName);
            Assert.AreEqual(2, functionInfo.NumberOfParameters);
            Assert.AreEqual(testFunction, functionInfo.Function);
        }
        public void Successfully_RemoveFunction()
        {
            var registry = new FunctionRegistry();

            registry.RegisterFunction("a", Invalid2.A, FunctionScope.All);

            Assert.IsTrue(registry.Contains("a", FunctionScope.Basic));
            Assert.IsTrue(registry.Contains("a", FunctionScope.Group));
            Assert.IsTrue(registry.Contains("a", FunctionScope.Global));

            registry.Remove("a", FunctionScope.Basic);

            Assert.IsFalse(registry.Contains("a", FunctionScope.Basic));
            Assert.IsTrue(registry.Contains("a", FunctionScope.Group));
            Assert.IsTrue(registry.Contains("a", FunctionScope.Global));
        }
Esempio n. 9
0
        public void TestOptimizerIdempotentFunction()
        {
            Optimizer optimizer = new Optimizer(new Interpreter());

            TokenReader   tokenReader = new TokenReader(CultureInfo.InvariantCulture);
            IList <Token> tokens      = tokenReader.Read("test(var1, (2+3) * 500)");

            IFunctionRegistry functionRegistry = new FunctionRegistry(true);

            functionRegistry.RegisterFunction("test", (Func <double, double, double>)((a, b) => a + b));

            AstBuilder astBuilder = new AstBuilder(functionRegistry, true);
            Operation  operation  = astBuilder.Build(tokens);

            Function optimizedFuction = (Function)optimizer.Optimize(operation, functionRegistry, null);

            Assert.AreEqual(typeof(FloatingPointConstant), optimizedFuction.Arguments[1].GetType());
        }
Esempio n. 10
0
        public void TestOptimizerNonIdempotentFunction()
        {
            Optimizer optimizer = new Optimizer(new Interpreter());

            TokenReader   tokenReader = new TokenReader(CultureInfo.InvariantCulture);
            IList <Token> tokens      = tokenReader.Read("test(500)");

            IFunctionRegistry functionRegistry = new FunctionRegistry(true);

            functionRegistry.RegisterFunction("test", (Func <double, double>)(a => a), false, true);

            AstBuilder astBuilder = new AstBuilder(functionRegistry, true);
            Operation  operation  = astBuilder.Build(tokens);

            Operation optimizedFuction = optimizer.Optimize(operation, functionRegistry, null);

            Assert.AreEqual(typeof(Function), optimizedFuction.GetType());
            Assert.AreEqual(typeof(IntegerConstant), ((Function)optimizedFuction).Arguments[0].GetType());
        }
Esempio n. 11
0
        public void TestAddFunc2()
        {
            FunctionRegistry registry = new FunctionRegistry(false);

            Func <double, double, double> testFunction = (a, b) => a * b;

            registry.RegisterFunction("test", testFunction);

            FunctionInfo functionInfo = registry.GetFunctionInfo("test");

#if !NETCORE
            Assert.IsNotNull(functionInfo);
            Assert.AreEqual("test", functionInfo.FunctionName);
            Assert.AreEqual(2, functionInfo.NumberOfParameters);
            Assert.AreEqual(testFunction, functionInfo.Function);
#else
            Assert.NotNull(functionInfo);
            Assert.Equal("test", functionInfo.FunctionName);
            Assert.Equal(2, functionInfo.NumberOfParameters);
            Assert.Equal(testFunction, functionInfo.Function);
#endif
        }
Esempio n. 12
0
 public void AddFunction(string functionName, Func <float, float, float, float, float, float, float, float, float, float, float, float, float, float, float, float, float> function) => FunctionRegistry.RegisterFunction(functionName, function);
Esempio n. 13
0
 public void AddFunction(string functionName, DynamicFunc <double, double> functionDelegate)
 {
     FunctionRegistry.RegisterFunction(functionName, functionDelegate);
 }
Esempio n. 14
0
 public void AddFunction(string functionName, Func <double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double> function)
 {
     FunctionRegistry.RegisterFunction(functionName, function);
 }
Esempio n. 15
0
 public void AddFunction(string functionName, DynamicFunc <double, double> functionDelegate, bool isIdempotent = true)
 {
     FunctionRegistry.RegisterFunction(functionName, functionDelegate, isIdempotent, true);
 }
Esempio n. 16
0
 public void AddFunction(string functionName, Func <double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double> function, bool isIdempotent = true)
 {
     FunctionRegistry.RegisterFunction(functionName, function, isIdempotent, true);
 }
Esempio n. 17
0
 public void AddFunction(string functionName, Func <double, double> function, bool overwritable)
 {
     FunctionRegistry.RegisterFunction(functionName, function, overwritable);
 }
        public void ThrowArgumentException_WhenRegisteringEmptyFunction()
        {
            var registry = new FunctionRegistry();

            registry.RegisterFunction("", Invalid2.A);
        }
        public void ThrowArgumentNullException_WhenRegisteringNullFunction()
        {
            var registry = new FunctionRegistry();

            registry.RegisterFunction(null, Invalid2.A);
        }