AddCustomFunction() public method

public AddCustomFunction ( string name, double>.System func, bool enableSymbolicationTimeEvaluation = false ) : void
name string
func double>.System
enableSymbolicationTimeEvaluation bool
return void
        public static void TestStringFuncs()
        {
            ExpressionSolver solver = new ExpressionSolver();
            solver.AddCustomFunction("strlen",1, delegate(object[] p) {
                return ((string)p[0]).Length;
            },true);
            var exp = solver.SymbolicateExpression("strlen('123')");
            AssertSameValue(exp.Evaluate(),3.0);
            exp = solver.SymbolicateExpression("strlen('12\\'3')");
            AssertSameValue(exp.Evaluate(),4.0);
            exp = solver.SymbolicateExpression("strlen('12\\'3 4')");
            AssertSameValue(exp.Evaluate(),6.0);
            solver.AddCustomFunction("strlen2",2, delegate(object[] p) {
                return ((string)p[0]).Length*(double)p[1];
            });
            exp = solver.SymbolicateExpression("strlen2('12\\'3 4',2.5)");
            AssertSameValue(exp.Evaluate(),6.0*2.5);

            string[] erroneousStrings = new string[]{"strlen(''')","strlen('''')","''"};
            foreach (var errorString in erroneousStrings)
            {
                try
                {
                    exp = solver.SymbolicateExpression(errorString);
                    throw new System.Exception("ExpressionSolverTest failed");
                }
                catch (ESSyntaxErrorException)
                {
                    // Parameters were not given correctly - syntax error expected
                }
                catch (System.Exception)
                {
                    throw new System.Exception("ExpressionSolverTest failed");
                }
            }

            // Because strlen should be evaluated at symbolication time, the following should reduce to one real value symbol:
            exp = solver.SymbolicateExpression("(1+strlen('123')+1)/strlen('12345')");
            Assert(exp.root.type == SymbolType.RealValue);
            AssertSameValue(exp.root.value,1);
            // But if one of the parameters is not constant, then we cant do it:
            exp = solver.SymbolicateExpression("strlen('123')+x", new string[]{"x"});
            Assert(exp.root.type == SymbolType.SubExpression);

            // Test string variables. Both exp-local and global
            exp = solver.SymbolicateExpression("strlen(stringVariableTest)","$stringVariableTest");
            exp.SetVariable("stringVariableTest","test");
            AssertSameValue(exp.Evaluate(),4);
            try
            {
                exp.SetVariable("stringVariableTest",121);
                Assert(false);
            }
            catch (ESParameterTypeChangedException)
            {
            }
            solver.SetGlobalVariable("striva","123");
            exp = solver.SymbolicateExpression("strlen( (  striva  )  )/3");
            AssertSameValue(exp.Evaluate(),1);
            try
            {
                solver.SetGlobalVariable("striva",42141.0);
                Assert(false);
            }
            catch (ESParameterTypeChangedException)
            {
            }
        }
Exemplo n.º 2
0
        public static void TestFuncs()
        {
            ExpressionSolver solver = new ExpressionSolver();

            solver.SetGlobalVariable("zero", 0);
            var exp1 = solver.SymbolicateExpression("sin(pi/2)-cos(zero)");

            AssertSameValue(exp1.Evaluate(), 0);
            var exp2 = solver.SymbolicateExpression("2*e^zero - exp(zero)");

            AssertSameValue(exp2.Evaluate(), 1);
            var exp3 = solver.SymbolicateExpression("log(e^6)");

            AssertSameValue(exp3.Evaluate(), 6);
            var exp4 = solver.SymbolicateExpression("sqrt(2)-2^0.5");

            AssertSameValue(exp4.Evaluate(), 0);
            var exp5 = solver.SymbolicateExpression("exp(log(6))");

            AssertSameValue(exp5.Evaluate(), 6);
            var rnd = new System.Random();

            solver.AddCustomFunction("Rnd1", 2, delegate(double[] p) {
                return(p[0] + (p[1] - p[0]) * (rnd.NextDouble()));
            }, false);
            var exp6     = solver.SymbolicateExpression("Rnd1(0,1)");
            var firstRnd = exp6.Evaluate();
            int iter     = 0;

            while (true)
            {
                var secondRnd = exp6.Evaluate();
                if (firstRnd != secondRnd)
                {
                    break;
                }
                iter++;
                if (iter == 10000)
                {
                    // Probability of this happening is miniscule if everything works as it should
                    throw new System.Exception("ExpressionSolverTest failed");
                }
            }
            solver.AddCustomFunction("Rnd2", 2, delegate(double[] p) {
                return(p[0] + (p[1] - p[0]) * (rnd.NextDouble()));
            }, true);
            var exp7 = solver.SymbolicateExpression("Rnd2(0,1)");

            AssertSameValue(exp7.Evaluate(), exp7.Evaluate());
            var exp8 = solver.SymbolicateExpression("cos(0)+1*2");

            AssertSameValue(exp8.Evaluate(), 3);
            solver.AddCustomFunction("dist", 5, delegate(double[] p) {
                return(System.Math.Pow((p[2] - p[0]) * (p[2] - p[0]) + (p[3] - p[1]) * (p[3] - p[1]), p[4]));
            }, true);
            var    exp9 = solver.SymbolicateExpression("dist(3*x,(4*x),+6*x,-1*x,sin(x))", "x");
            double x    = 21;

            exp9.SetVariable("x", x);
            AssertSameValue(exp9.Evaluate(), System.Math.Pow((3 * x - 6 * x) * (3 * x - 6 * x) + (4 * x + x) * (4 * x + x), System.Math.Sin(x)));
        }
 public static void TestFuncs()
 {
     ExpressionSolver solver = new ExpressionSolver();
     solver.SetGlobalVariable("zero",0);
     var exp1 = solver.SymbolicateExpression("sin(pi/2)-cos(zero)");
     AssertSameValue(exp1.Evaluate(),0);
     var exp2 = solver.SymbolicateExpression("2*e^zero - exp(zero)");
     AssertSameValue(exp2.Evaluate(),1);
     var exp3 = solver.SymbolicateExpression("log(e^6)");
     AssertSameValue(exp3.Evaluate(),6);
     var exp4 = solver.SymbolicateExpression("sqrt(2)-2^0.5");
     AssertSameValue(exp4.Evaluate(),0);
     var exp5 = solver.SymbolicateExpression("exp(log(6))");
     AssertSameValue(exp5.Evaluate(),6);
     var rnd = new System.Random();
     solver.AddCustomFunction("Rnd1",2, delegate(double[] p) {
         return p[0] + (p[1]-p[0])*(rnd.NextDouble());
     },false);
     var exp6 = solver.SymbolicateExpression("Rnd1(0,1)");
     var firstRnd = exp6.Evaluate();
     int iter = 0;
     while (true)
     {
         var secondRnd = exp6.Evaluate();
         if (firstRnd != secondRnd)
         {
             break;
         }
         iter++;
         if (iter==10000)
         {
             // Probability of this happening is miniscule if everything works as it should
             throw new System.Exception("ExpressionSolverTest failed");
         }
     }
     solver.AddCustomFunction("Rnd2",2, delegate(double[] p) {
         return p[0] + (p[1]-p[0])*(rnd.NextDouble());
     },true);
     var exp7 = solver.SymbolicateExpression("Rnd2(0,1)");
     AssertSameValue(exp7.Evaluate(),exp7.Evaluate());
     var exp8 = solver.SymbolicateExpression("cos(0)+1*2");
     AssertSameValue(exp8.Evaluate(),3);
     solver.AddCustomFunction("dist",5, delegate(double[] p) {
         return System.Math.Pow( (p[2]-p[0])*(p[2]-p[0]) + (p[3]-p[1])*(p[3]-p[1])    ,p[4]);
     },true);
     var exp9 = solver.SymbolicateExpression("dist(3*x,(4*x),+6*x,-1*x,sin(x))","x");
     double x = 21;
     exp9.SetVariable("x",x);
     AssertSameValue(exp9.Evaluate(),System.Math.Pow( (3*x-6*x)*(3*x-6*x)+(4*x+x)*(4*x+x),System.Math.Sin(x)  ));
 }
Exemplo n.º 4
0
        public static void TestStringFuncs()
        {
            ExpressionSolver solver = new ExpressionSolver();

            solver.AddCustomFunction("strlen", 1, delegate(object[] p) {
                return(((string)p[0]).Length);
            }, true);
            var exp = solver.SymbolicateExpression("strlen('123')");

            AssertSameValue(exp.Evaluate(), 3.0);
            exp = solver.SymbolicateExpression("strlen('12\\'3')");
            AssertSameValue(exp.Evaluate(), 4.0);
            exp = solver.SymbolicateExpression("strlen('12\\'3 4')");
            AssertSameValue(exp.Evaluate(), 6.0);
            solver.AddCustomFunction("strlen2", 2, delegate(object[] p) {
                return(((string)p[0]).Length * (double)p[1]);
            });
            exp = solver.SymbolicateExpression("strlen2('12\\'3 4',2.5)");
            AssertSameValue(exp.Evaluate(), 6.0 * 2.5);

            string[] erroneousStrings = new string[] { "strlen(''')", "strlen('''')", "''" };
            foreach (var errorString in erroneousStrings)
            {
                try
                {
                    exp = solver.SymbolicateExpression(errorString);
                    throw new System.Exception("ExpressionSolverTest failed");
                }
                catch (ESSyntaxErrorException)
                {
                    // Parameters were not given correctly - syntax error expected
                }
                catch (System.Exception)
                {
                    throw new System.Exception("ExpressionSolverTest failed");
                }
            }

            // Because strlen should be evaluated at symbolication time, the following should reduce to one real value symbol:
            exp = solver.SymbolicateExpression("(1+strlen('123')+1)/strlen('12345')");
            Assert(exp.root.type == SymbolType.RealValue);
            AssertSameValue(exp.root.value, 1);
            // But if one of the parameters is not constant, then we cant do it:
            exp = solver.SymbolicateExpression("strlen('123')+x", new string[] { "x" });
            Assert(exp.root.type == SymbolType.SubExpression);

            // Test string variables. Both exp-local and global
            exp = solver.SymbolicateExpression("strlen(stringVariableTest)", "$stringVariableTest");
            exp.SetVariable("stringVariableTest", "test");
            AssertSameValue(exp.Evaluate(), 4);
            try
            {
                exp.SetVariable("stringVariableTest", 121);
                Assert(false);
            }
            catch (ESParameterTypeChangedException)
            {
            }
            solver.SetGlobalVariable("striva", "123");
            exp = solver.SymbolicateExpression("strlen( (  striva  )  )/3");
            AssertSameValue(exp.Evaluate(), 1);
            try
            {
                solver.SetGlobalVariable("striva", 42141.0);
                Assert(false);
            }
            catch (ESParameterTypeChangedException)
            {
            }
        }