SymbolicateExpression() public method

public SymbolicateExpression ( string formula, string localVariable ) : Expression
formula string
localVariable string
return Expression
Exemplo n.º 1
0
        public static void TestUndefinedVariablePolicies()
        {
            ExpressionSolver solver = new ExpressionSolver();

            try
            {
                solver.SymbolicateExpression("test");
                throw new System.Exception("ExpressionSolverTest failed");
            }
            catch (AK.ESUnknownExpressionException)
            {
                // Expected behaviour
            }

            solver.undefinedVariablePolicy = ExpressionSolver.UndefinedVariablePolicy.DefineGlobalVariable;
            var exp2 = solver.SymbolicateExpression("test2");

            AssertSameValue(solver.GetGlobalVariable("test2").value, 0);
            AssertSameValue(exp2.Evaluate(), 0);

            solver.undefinedVariablePolicy = ExpressionSolver.UndefinedVariablePolicy.DefineExpressionLocalVariable;
            var exp3  = solver.SymbolicateExpression("sin(test3)");
            var test3 = exp3.GetVariable("test3");

            AssertSameValue(test3.value, 0);
            test3.value = System.Math.PI / 2;
            AssertSameValue(exp3.Evaluate(), 1);
        }
Exemplo n.º 2
0
 public void UpdateFunctions()
 {
     Clear();
     //if (es.CompileAll())
     //{
     es.CompileAll();
     expX = solver.SymbolicateExpression(es.expressions["X"].expression);
     expY = solver.SymbolicateExpression(es.expressions["Y"].expression);
     expZ = solver.SymbolicateExpression(es.expressions["Z"].expression);
     //}
     //else
     //{
     //    return;
     //}
     //try
     //{
     //    expX = solver.SymbolicateExpression(expressionX);
     //    expY = solver.SymbolicateExpression(expressionY);
     //    expZ = solver.SymbolicateExpression(expressionZ);
     //}
     //catch
     //{
     //    return;
     //}
     CalculateVectors();
     DrawVectorField();
 }
Exemplo n.º 3
0
    public void SetupSolver()
    {
        depth = parameter_names.Count;
        width = (int)Mathf.Pow(PARTICLE_COUNT, 1f / (float)depth);

        for (int i = 0; i < depth; i++)
        {
            string name = parameter_names[i];
            indexedParam[i]    = name;
            parameterMin[name] = (float)solver.EvaluateExpression(parameter_min[name]);
            parameterMax[name] = (float)solver.EvaluateExpression(parameter_max[name]);

            solver.SetGlobalVariable(name, 0);
        }

        expr1 = solver.SymbolicateExpression(expression1);
        expr2 = solver.SymbolicateExpression(expression2);
        expr3 = solver.SymbolicateExpression(expression3);

        for (int i = 0; i < depth; i++)
        {
            string      name = parameter_names[i];
            AK.Variable var  = solver.GetGlobalVariable(name);
            //vars.Add(var);
            vars[name] = var;
        }
    }
Exemplo n.º 4
0
        public static void TestGlobalConstants()
        {
            ExpressionSolver solver = new ExpressionSolver();

            solver.SetGlobalVariable("test", 1);
            var exp1 = solver.SymbolicateExpression("test+1");

            AssertSameValue(2.0, exp1.Evaluate());
            solver.SetGlobalVariable("test", 2);
            var exp2 = solver.SymbolicateExpression("test+1");

            AssertSameValue(3.0, exp2.Evaluate());
            AssertSameValue(exp1.Evaluate(), exp2.Evaluate());
        }
Exemplo n.º 5
0
        public static void TestExpLocalConstants()
        {
            ExpressionSolver solver = new ExpressionSolver();
            var exp1 = solver.SymbolicateExpression("test+1", new string[] { "test" });

            exp1.SetVariable("test", 1.0);
            var exp2 = solver.SymbolicateExpression("test+1", "test");            // If you define only one variable, you don't need to use the string[] format

            exp2.SetVariable("test", 2.0);
            solver.SetGlobalVariable("test", 1000);            // If there is name clash with a exp-local variable, we prefer the exp-local variable.
            AssertSameValue(exp1.Evaluate(), 2);
            AssertSameValue(exp2.Evaluate(), 3);
            var exp3 = solver.SymbolicateExpression("test^2");

            AssertSameValue(exp3.Evaluate(), 1000 * 1000);
        }
Exemplo n.º 6
0
    // Evaluate all equations for a given x (called by a thread), and popluate arrays with the results
    void ThreadedEvaluate(float x_temp, int inOrderCount)
    {
        AK.ExpressionSolver subSolver = new AK.ExpressionSolver();
        AK.Expression       subExp    = new AK.Expression();
        subSolver.SetGlobalVariable("x", 0);
        subSolver.SetGlobalVariable("y", 0);
        subSolver.SetGlobalVariable("z", 0);
        AK.Variable subX = subSolver.GetGlobalVariable("x");
        AK.Variable subY = subSolver.GetGlobalVariable("y");
        AK.Variable subZ = subSolver.GetGlobalVariable("z");
        subExp = subSolver.SymbolicateExpression(es.expressions["X"].expression);

        for (float y_temp = ymin; y_temp < ymax; y_temp += delta)
        {
            for (float z_temp = zmin; z_temp < zmax; z_temp += delta)
            {
                if ((int)((z_temp - zmin) / delta) % sleepInterval == 0)
                {
                    Thread.Sleep(1);
                }

                subX.value = x_temp;
                subY.value = z_temp;
                subZ.value = y_temp;

                float x = (float)subExp.Evaluate();

                //Mathf.Clamp(x, -Mathf.Exp(20), Mathf.Exp(20));

                //float y = (float)expY.Evaluate();
                //Mathf.Clamp(y, -Mathf.Exp(20), Mathf.Exp(20));

                //float z = (float)expZ.Evaluate();
                //Mathf.Clamp(z, -Mathf.Exp(20), Mathf.Exp(20));

                Vector3 target = new Vector3(x_temp, y_temp, z_temp);

                Vector3 result = new Vector3(x, 0, 0);
                if (float.IsNaN(x)
                    //   || float.IsNaN(y)
                    //   || float.IsNaN(z)
                    ||
                    Mathf.Abs(x) == 0)
                {
                    result = new Vector3(0, 0, 0);
                }

                //Vector3 direction = result.normalized;
                lock (lck)
                {
                    max_magnitude = (Mathf.Abs(x) > max_magnitude) ? Mathf.Abs(x) : max_magnitude;
                }
                startPts[inOrderCount] = target;
                offsets[inOrderCount]  = result;
                inOrderCount++;
            }
        }
        // numComplete++;
    }
Exemplo n.º 7
0
 public void Go(string sfx, string sfy, string sfu, string sfv)
 {
     this.sfx = sfx;
     this.sfy = sfy;
     this.sfu = sfu;
     this.sfv = sfv;
     fxexp    = solver.SymbolicateExpression(this.sfx, new string[] { "s", "t", "a" });
     fyexp    = solver.SymbolicateExpression(this.sfy, new string[] { "s", "t", "a" });
     fuexp    = solver.SymbolicateExpression(this.sfu, new string[] { "s", "t", "a" });
     fvexp    = solver.SymbolicateExpression(this.sfv, new string[] { "s", "t", "a" });
     UpdatePoints();
 }
Exemplo n.º 8
0
 public virtual bool GenerateAKSolver(AK.ExpressionSolver solver)
 {
     try
     {
         AKExpression = solver.SymbolicateExpression(rawText);
     }
     catch (System.Exception exception)
     {
         return(false);
     }
     return(true);
 }
Exemplo n.º 9
0
    void ThreadedEvaluate(int TID, int chunkSize, int extra)
    {
        AK.ExpressionSolver solver_ = new AK.ExpressionSolver();
        AK.Variable         u_      = solver_.SetGlobalVariable("theta", 0);
        AK.Variable         v_      = solver_.SetGlobalVariable("phi", 0);
        AK.Expression       exp_    = solver_.SymbolicateExpression(expr);
        float umin = (float)solver_.EvaluateExpression(uminExpr);
        float umax = (float)solver_.EvaluateExpression(umaxExpr);
        float vmin = (float)solver_.EvaluateExpression(vminExpr);
        float vmax = (float)solver_.EvaluateExpression(vmaxExpr);

        System.Random rand = new System.Random();
        for (int i = 0; i < chunkSize + extra; i++)
        {
            float u = (float)(chunkSize * TID + i) / (resolution - 1);
            u_.value = umin + (umax - umin) * u;
            for (int j = 0; j < resolution; j++)
            {
                float v = (float)j / (resolution - 1);
                v_.value = vmin + (vmax - vmin) * v;
                float   result    = (float)exp_.Evaluate();
                Vector3 spherical = new Vector3((float)u_.value, (float)v_.value, result);
                Vector3 cartesian = SphericalToCartesian(spherical);
                Vector3 vel       = new Vector3(
                    (float)rand.NextDouble(),
                    (float)rand.NextDouble(),
                    (float)rand.NextDouble());
                vel = vel.normalized * 0.1f;
                float sqrt2 = Mathf.Sqrt(2.0f);
                Color c     = Color.cyan * Mathf.Sqrt(u * u + v * v) / sqrt2
                              + Color.yellow * (1.0f - Mathf.Sqrt(u * u + v * v) / sqrt2)
                              + Color.red * Mathf.Sqrt(u * u + (1 - v) * (1 - v)) / sqrt2
                              + Color.green * (1.0f - Mathf.Sqrt(u * u + (1 - v) * (1 - v)) / sqrt2);

                Vector3  pos = new Vector3(cartesian.x, cartesian.z, cartesian.y);
                Particle p   = new Particle()
                {
                    position = pos,
                    velocity = vel,
                    color    = c
                };
                lock (insert_lck)
                {
                    results.Add(p);
                }
            }
        }
    }
Exemplo n.º 10
0
    // Use this for initialization
    void Awake()
    {
        mesh = GetComponent <MeshFilter>().mesh; // aww yisss Awake > Start for variable value initialization

        solver = GlobalDataStore.getSolver();
        zFn    = GlobalDataStore.getZFn();

        // incase we start from "main" scene directly.
        if (solver == null)
        {
            solver = new AK.ExpressionSolver();
            solver.SetGlobalVariable("x", 0);
            solver.SetGlobalVariable("y", 0);
            zFn = solver.SymbolicateExpression("(x^2 + y^2) / (x*y+1)");
        }
    }
Exemplo n.º 11
0
 // Reset the threads and prepare to run new calculations so long as the plotter is not graphing
 public void UpdateFunctions()
 {
     if (drawn)
     {
         drawn = false;
         Clear();
         //if (es.CompileAll())
         //{
         es.CompileAll();
         expX = solver.SymbolicateExpression(es.expressions["X"].expression);
         CalculateVectors();
     }
     else
     {
         drawQueue++;
     }
     //DrawDensityPlot();
 }
Exemplo n.º 12
0
 public static void TestComplexFormulas()
 {
     ExpressionSolver solver = new ExpressionSolver();
     {
         var exp = solver.SymbolicateExpression("sin(1/(0.5*x))", "x");
         var x   = 21.0;
         exp.SetVariable("x", x);
         AssertSameValue(exp.Evaluate(), System.Math.Sin(1 / (0.5 * x)));
     }
     {
         var exp = solver.SymbolicateExpression("(1+1)+.5");
         AssertSameValue(exp.Evaluate(), 2.5f);
     }
     {
         var exp = solver.SymbolicateExpression("sin(2/(1-0.5*x))", "x");
         var x   = 21.0;
         exp.SetVariable("x", x);
         AssertSameValue(exp.Evaluate(), System.Math.Sin(2 / (1 - 0.5 * x)));
     }
     {
         var exp = solver.SymbolicateExpression("sin((2/(1-0.5*x))/2)", "x");
         var x   = 21.0;
         exp.SetVariable("x", x);
         AssertSameValue(exp.Evaluate(), System.Math.Sin((2 / (1 - 0.5 * x)) / 2));
     }
     {
         var exp = solver.SymbolicateExpression("1/(1+sin((2/(1-0.5*x))/2))", "x");
         var x   = 21.0;
         exp.SetVariable("x", x);
         AssertSameValue(exp.Evaluate(), 1 / (1 + System.Math.Sin((2 / (1 - 0.5 * x)) / 2)));
     }
     {
         var exp = solver.SymbolicateExpression(" (1-(3*(x/( (x+22)/x)-1*2*x))^0.5)/(x+1) ", "x");
         var x   = -21.0;
         exp.SetVariable("x", x);
         AssertSameValue(exp.Evaluate(), (1 - System.Math.Pow(3 * (x / ((x + 22) / x) - 1 * 2 * x), 0.5)) / (x + 1));
     }
     {
         var exp = solver.SymbolicateExpression("(1+1/x)^x", "x");
         var x   = 30000000.0;
         exp.SetVariable("x", x);
         AssertSameValue(exp.Evaluate(), System.Math.E);
     }
 }
Exemplo n.º 13
0
    public void setZFn()
    {
        string userInputFunction = InputField.GetComponent <InputField>().text;

        if (userInputFunction.Length == 0)
        {
            userInputFunction = "(x^2+y^2)/(xy)";
        }
        userInputFunction = EnsafenFnString(userInputFunction); // make 5x -> 5*x, etc

        Expression zFn = solver.SymbolicateExpression(EnsafenFnString(userInputFunction));

        setArgs(5, 10);

        Debug.Log("Result: " + zFn.Evaluate());

        GlobalDataStore.setSolver(solver);
        GlobalDataStore.setZFn(zFn);

        SceneManager.LoadScene("main");
    }
Exemplo n.º 14
0
        public static void TestSum()
        {
            const int        N      = 10000;
            ExpressionSolver solver = new ExpressionSolver();
            var    exp = solver.SymbolicateExpression("1/2^i", "i");
            double sum = 0;

            for (int i = 0; i < N; i++)
            {
                exp.SetVariable("i", i);
                sum += exp.Evaluate();
            }
            AssertSameValue(sum, 2);
            sum = 0;
            var variable = exp.GetVariable("i");

            for (int i = 0; i < N; i++)
            {
                variable.value = i;
                sum           += exp.Evaluate();
            }
            AssertSameValue(sum, 2);
        }
 public static void TestComplexFormulas()
 {
     ExpressionSolver solver = new ExpressionSolver();
     {
         var exp = solver.SymbolicateExpression("sin(1/(0.5*x))","x");
         var x = 21.0;
         exp.SetVariable("x",x);
         AssertSameValue(exp.Evaluate(),System.Math.Sin(1/(0.5*x)) );
     }
     {
         var exp = solver.SymbolicateExpression("(1+1)+.5");
         AssertSameValue(exp.Evaluate(),2.5f);
     }
     {
         var exp = solver.SymbolicateExpression("sin(2/(1-0.5*x))","x");
         var x = 21.0;
         exp.SetVariable("x",x);
         AssertSameValue(exp.Evaluate(),System.Math.Sin(2/(1-0.5*x)) );
     }
     {
         var exp = solver.SymbolicateExpression("sin((2/(1-0.5*x))/2)","x");
         var x = 21.0;
         exp.SetVariable("x",x);
         AssertSameValue(exp.Evaluate(),System.Math.Sin((2/(1-0.5*x))/2));
     }
     {
         var exp = solver.SymbolicateExpression("1/(1+sin((2/(1-0.5*x))/2))","x");
         var x = 21.0;
         exp.SetVariable("x",x);
         AssertSameValue(exp.Evaluate(),1/(1+System.Math.Sin((2/(1-0.5*x))/2)));
     }
     {
         var exp = solver.SymbolicateExpression(" (1-(3*(x/( (x+22)/x)-1*2*x))^0.5)/(x+1) ","x");
         var x = -21.0;
         exp.SetVariable("x",x);
         AssertSameValue(exp.Evaluate(), (1-System.Math.Pow(3*(x/( (x+22)/x)-1*2*x),0.5))/(x+1) );
     }
     {
         var exp = solver.SymbolicateExpression("(1+1/x)^x","x");
         var x = 30000000.0;
         exp.SetVariable("x",x);
         AssertSameValue(exp.Evaluate(),System.Math.E);
     }
 }
 public static void TestExpLocalConstants()
 {
     ExpressionSolver solver = new ExpressionSolver();
     var exp1 = solver.SymbolicateExpression("test+1",new string[]{"test"});
     exp1.SetVariable("test",1.0);
     var exp2 = solver.SymbolicateExpression("test+1","test"); // If you define only one variable, you don't need to use the string[] format
     exp2.SetVariable("test",2.0);
     solver.SetGlobalVariable("test",1000); // If there is name clash with a exp-local variable, we prefer the exp-local variable.
     AssertSameValue(exp1.Evaluate(),2);
     AssertSameValue(exp2.Evaluate(),3);
     var exp3 = solver.SymbolicateExpression("test^2");
     AssertSameValue(exp3.Evaluate(),1000*1000);
 }
        public static void TestUndefinedVariablePolicies()
        {
            ExpressionSolver solver = new ExpressionSolver();
            try
            {
                solver.SymbolicateExpression("test");
                throw new System.Exception("ExpressionSolverTest failed");
            }
            catch (AK.ESUnknownExpressionException)
            {
                // Expected behaviour
            }

            solver.undefinedVariablePolicy = ExpressionSolver.UndefinedVariablePolicy.DefineGlobalVariable;
            var exp2 = solver.SymbolicateExpression("test2");
            AssertSameValue(solver.GetGlobalVariable("test2").value,0);
            AssertSameValue(exp2.Evaluate(),0);

            solver.undefinedVariablePolicy = ExpressionSolver.UndefinedVariablePolicy.DefineExpressionLocalVariable;
            var exp3 = solver.SymbolicateExpression("sin(test3)");
            var test3 = exp3.GetVariable("test3");
            AssertSameValue(test3.value,0);
            test3.value = System.Math.PI/2;
            AssertSameValue(exp3.Evaluate(),1);
        }
 public static void TestSum()
 {
     const int N = 10000;
     ExpressionSolver solver = new ExpressionSolver();
     var exp = solver.SymbolicateExpression("1/2^i","i");
     double sum = 0;
     for (int i=0;i<N;i++)
     {
         exp.SetVariable("i",i);
         sum += exp.Evaluate();
     }
     AssertSameValue(sum,2);
     sum = 0;
     var variable = exp.GetVariable("i");
     for (int i=0;i<N;i++)
     {
         variable.value = i;
         sum += exp.Evaluate();
     }
     AssertSameValue(sum,2);
 }
        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)
            {
            }
        }
 public static void TestGlobalConstants()
 {
     ExpressionSolver solver = new ExpressionSolver();
     solver.SetGlobalVariable("test",1);
     var exp1 = solver.SymbolicateExpression("test+1");
     AssertSameValue(2.0,exp1.Evaluate());
     solver.SetGlobalVariable("test",2);
     var exp2 = solver.SymbolicateExpression("test+1");
     AssertSameValue(3.0,exp2.Evaluate());
     AssertSameValue(exp1.Evaluate(),exp2.Evaluate());
 }
 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.º 22
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)
            {
            }
        }
Exemplo n.º 23
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)));
        }
Exemplo n.º 24
0
    void SamplePoints()
    {
        Vector3 start = referencePoint.lastLocalPos;

        //lck = new object();
        //thread_num = SystemInfo.processorCount;

        //thread_num = 1;
        //thread_num = (thread_num > 2) ? 2 : thread_num;

        tmin = (float)solver.EvaluateExpression(t_min);
        tmin = (tmin > 0) ? 0 : tmin;
        tmax = (float)solver.EvaluateExpression(t_max);
        tmax = (tmax < 0) ? 0 : tmax;

        currLocalPos = referencePoint.lastLocalPos;
        currExpX     = vectorField.expressionX;
        currExpY     = vectorField.expressionY;
        currExpZ     = vectorField.expressionZ;

        int lastCount = positions.Count;

        positions.Clear();

        solver = new AK.ExpressionSolver();

        solver.SetGlobalVariable("x", referencePoint.lastLocalPos.x);
        solver.SetGlobalVariable("y", referencePoint.lastLocalPos.y);
        solver.SetGlobalVariable("z", referencePoint.lastLocalPos.z);
        expX = solver.SymbolicateExpression(vectorField.es.expressions[ExpressionSet.ExpOptions.X].expression);
        expY = solver.SymbolicateExpression(vectorField.es.expressions[ExpressionSet.ExpOptions.Y].expression);
        expZ = solver.SymbolicateExpression(vectorField.es.expressions[ExpressionSet.ExpOptions.Z].expression);
        varX = solver.GetGlobalVariable("x");
        varY = solver.GetGlobalVariable("y");
        varZ = solver.GetGlobalVariable("z");

        //solvers = new AK.ExpressionSolver[thread_num];
        //expXs = new AK.Expression[thread_num];
        //expYs = new AK.Expression[thread_num];
        //expZs = new AK.Expression[thread_num];
        //for(int i = 0; i < thread_num; i++)
        //{
        //    solvers[i] = new AK.ExpressionSolver();
        //    solvers[i].SetGlobalVariable("x", 0);
        //    solvers[i].SetGlobalVariable("y", 0);
        //    solvers[i].SetGlobalVariable("z", 0);
        //    expXs[i] = solvers[i].SymbolicateExpression(vectorField.es.expressions[ExpressionSet.ExpOptions.X].expression);
        //    expYs[i] = solvers[i].SymbolicateExpression(vectorField.es.expressions[ExpressionSet.ExpOptions.Y].expression);
        //    expZs[i] = solvers[i].SymbolicateExpression(vectorField.es.expressions[ExpressionSet.ExpOptions.Z].expression);
        //}
        //Thread[] threads = new Thread[thread_num];
        float positiveCount = tmax / time_step;

        positiveCount = (positiveCount > 50000) ? 50000 : positiveCount;
        float negativeCount = -tmin / time_step;

        negativeCount = (negativeCount > 50000) ? 50000 : negativeCount;
        //Vector3[] startPts = new Vector3[thread_num];
        //startPts[0] = referencePoint.lastLocalPos;
        //for(int i = 1; i < thread_num; i++)
        //{
        //    startPts[i] = RK4(startPts[i - 1], time_step);
        //}
        //for(int i = 0; i < thread_num; i++)
        //{
        //    int index = i;
        //    threads[i] = new Thread(() => ThreadedSampling(index, startPts[index], time_step * thread_num, positiveCount / thread_num,
        //        negativeCount / thread_num));
        //    threads[i].Start();
        //}
        ////for (int i = 0; i < 5; i++)
        ////{
        ////    yield return null;
        ////}
        //for (int i = 0; i < thread_num; i++)
        //{
        //    threads[i].Join();
        //}

        Vector3 curr = start;

        for (int i = 0; i < positiveCount; i++)
        {
            curr = RK4(curr, time_step);
            positions.Add(i + 1, curr);
        }
        curr = start;
        for (int i = 0; i < negativeCount; i++)
        {
            curr = RK4(curr, -time_step);
            positions.Add(-i, curr);
        }

        if (positions.Count != lastCount)
        {
            InitializeParticleSystem();
        }

        //RenderParticles();

        currHighlight   = 0;
        thread_finished = true;
    }