SetGlobalVariable() public méthode

public SetGlobalVariable ( string name, double value ) : Variable
name string
value double
Résultat Variable
Exemple #1
0
 // Use this for initialization
 void Awake()
 {
     xmin     = -1f * minmaxVal;
     xmax     = minmaxVal;
     ymin     = -1f * minmaxVal;
     ymax     = minmaxVal;
     zmin     = -1f * minmaxVal;
     zmax     = minmaxVal;
     vectors  = new List <Transform>();
     startPts = new Vector3[2097152];
     offsets  = new Vector3[2097152];
     solver   = new AK.ExpressionSolver();
     expX     = new AK.Expression();
     expY     = new AK.Expression();
     expZ     = new AK.Expression();
     solver.SetGlobalVariable("x", 0);
     solver.SetGlobalVariable("y", 0);
     solver.SetGlobalVariable("z", 0);
     max_magnitude       = 0f;
     textureMap          = new Texture3D(128, 128, 128, TextureFormat.RGBA32, true);
     textureMap.wrapMode = TextureWrapMode.Clamp;
     numComplete         = 0;
     drawn     = true;
     drawQueue = 0;
     if (loadingMsg != null)
     {
         loadingMsgText = loadingMsg.GetComponent <TextMesh>();
     }
     sleepInterval = 126;
     //CalculateVectors();
     //DrawDensityPlot();
 }
Exemple #2
0
    public Vector3 findXYZ(Vector3 uvw)
    {
        Vector3       output = new Vector3();
        ExpressionSet es     = calcManager.expressionSet;
        float         scale  = calcManager.paramSurface.currentScale;

        foreach (string key in es.expressions.Keys)
        {
            AK.ExpressionSolver solver = es.solver;
            if (es.ranges.ContainsKey("u"))
            {
                solver.SetGlobalVariable("u", uvw.x);
            }
            if (es.ranges.ContainsKey("v"))
            {
                solver.SetGlobalVariable("v", uvw.y);
            }
            if (es.ranges.ContainsKey("w"))
            {
                solver.SetGlobalVariable("w", uvw.z);
            }
        }
        if (es.IsCompiled())
        {
            output.x = (float)es.expressions["X"].AKExpression.Evaluate();
            output.y = (float)es.expressions["Y"].AKExpression.Evaluate();
            output.z = (float)es.expressions["Z"].AKExpression.Evaluate();
        }
        return(output * scale);
    }
Exemple #3
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++;
    }
Exemple #4
0
    void MakeContiguousProceduralGrid()
    {
        vertices  = new Vector3[(sizeX + 1) * cellsPerUnit * (sizeY + 1) * cellsPerUnit];
        triangles = new int[sizeX * cellsPerUnit * sizeY * cellsPerUnit * 6];

        // tracker ints
        int v = 0, t = 0; // v == y of first nested for-loop, but this is clearer!

        // some calculations
        float   cellSize     = 1 / (float)cellsPerUnit;
        Vector3 vertexOffset = new Vector3((float)sizeX * 0.5f, 0, (float)sizeX * 0.5f); // turns out multiplication is cheaper than division
        int     numCellsX    = sizeX * cellsPerUnit;
        int     numCellsY    = sizeY * cellsPerUnit;

        // <= because we need the edge vertices at the end of the grid (so gridsize + 1)
        for (float x = 0; x <= sizeX; x += cellSize)
        {
            for (float y = 0; y <= sizeY; y += cellSize)
            {
                solver.SetGlobalVariable("x", x);
                solver.SetGlobalVariable("y", y);

                float z = (float)zFn.Evaluate();

                if (float.IsPositiveInfinity(z) || float.IsNegativeInfinity(z))
                {
                    vertices[v] = vertices[v - 1];
                }
                else
                {
                    vertices[v] = new Vector3(x, z, y) - vertexOffset; // yes, x,z,y
                }

                v++;
            }
        }

        v = 0; // #reset for more looping!

        for (int x = 0; x < numCellsX; x++)
        {
            for (int y = 0; y < numCellsY; y++)
            {
                triangles[t]     = v;
                triangles[t + 1] = v + 1;
                triangles[t + 2] = v + (numCellsX + 1);
                triangles[t + 3] = v + (numCellsX + 1);
                triangles[t + 4] = v + 1;
                triangles[t + 5] = (v + 1) + (numCellsX + 1);

                v++;
                t += 6;
            }

            v++; // idrg why yet, but something to do with the +1 to sizeX, sizeY
        }
    }
 // Use this for initialization
 void Start()
 {
     vectors  = new List <Transform>();
     startPts = new List <Vector3>();
     offsets  = new List <Vector3>();
     solver   = new AK.ExpressionSolver();
     expX     = new AK.Expression(); expY = new AK.Expression(); expZ = new AK.Expression();
     solver.SetGlobalVariable("x", 0); solver.SetGlobalVariable("y", 0); solver.SetGlobalVariable("z", 0);
     varX          = solver.GetGlobalVariable("x"); varY = solver.GetGlobalVariable("y"); varZ = solver.GetGlobalVariable("z");
     max_magnitude = 0f;
     //CalculateVectors();
     //DrawVectorField();
 }
        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());
        }
Exemple #7
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);
                }
            }
        }
    }
Exemple #8
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;
        }
    }
    public bool CompileAll()
    {
        bool isValid = true;

        foreach (string RO in ranges.Keys)
        {
            solver.SetGlobalVariable(RO, -666);
            ranges[RO].Min.compileTokens();
            expValidity[RO] = ranges[RO].Min.GenerateAKSolver(solver);
            ranges[RO].Max.compileTokens();
            expValidity[RO] &= ranges[RO].Max.GenerateAKSolver(solver);
            isValid         &= expValidity[RO];
        }
        foreach (string EX in expressions.Keys)
        {
            expressions[EX].compileTokens();
            expValidity[EX.ToString()] = expressions[EX].GenerateAKSolver(solver);
            isValid &= expValidity[EX.ToString()];
        }
        StatisticsTracking.InstantEvent("Expression Value", "Value Updated", new Dictionary <string, object>()
        {
            { "valid", isValid }
        });
        return(isValid);
    }
Exemple #10
0
 // Use this for initialization
 void setArgs(params double[] values)
 {
     for (int i = 0; i < depVars.Length; i++)
     {
         solver.SetGlobalVariable(depVars[i], values[i]);
     }
 }
Exemple #11
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)");
        }
    }
Exemple #12
0
 public bool CompileAll()
 {
     coefValidity = true;
     foreach (string PO in eqnCoefs.Keys)
     {
         solver.SetGlobalVariable(PO, -666);
         eqnCoefs[PO].compileTokens();
         coefValidity &= eqnCoefs[PO].GenerateAKSolver(solver);
     }
     return(coefValidity);
 }
        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);
        }
Exemple #14
0
    public bool CompileAll()
    {
        bool isValid = true;

        foreach (string PO in ptCoords.Keys)
        {
            solver.SetGlobalVariable(PO, -666);
            ptCoords[PO].X.compileTokens();
            expValidity[PO] = ptCoords[PO].X.GenerateAKSolver(solver);
            ptCoords[PO].Y.compileTokens();
            expValidity[PO] &= ptCoords[PO].Y.GenerateAKSolver(solver);
            ptCoords[PO].Z.compileTokens();
            expValidity[PO] &= ptCoords[PO].Z.GenerateAKSolver(solver);
            isValid         &= expValidity[PO];
        }
        return(isValid);
    }
Exemple #15
0
    void Start()
    {
        // config
        depVars = new string[2] {
            "x", "y"
        };

        // object retrieval
        InputField = GameObject.Find("UIInputFunction");

        // init
        solver = new AK.ExpressionSolver();

        // logic
        foreach (string depVar in depVars)
        {
            solver.SetGlobalVariable(depVar, 0);
        }
    }
Exemple #16
0
    public bool CompileAll()
    {
        bool isValid = true;

        foreach (string RO in ranges.Keys)
        {
            solver.SetGlobalVariable(RO, -666);
            ranges[RO].Min.compileTokens();
            expValidity[RO] = ranges[RO].Min.GenerateAKSolver(solver);
            ranges[RO].Max.compileTokens();
            expValidity[RO] &= ranges[RO].Max.GenerateAKSolver(solver);
            isValid         &= expValidity[RO];
        }
        foreach (ExpOptions EX in expressions.Keys)
        {
            expressions[EX].compileTokens();
            expValidity[EX.ToString()] = expressions[EX].GenerateAKSolver(solver);
            isValid &= expValidity[EX.ToString()];
        }
        return(isValid);
    }
Exemple #17
0
    public Vector3 findXYZ(Vector3 uvw)
    {
        Vector3       output = new Vector3();
        ExpressionSet es     = calcManager.expressionSet;
        float         scale  = calcManager.paramSurface.currentScale;

        foreach (ExpressionSet.ExpOptions key in es.expressions.Keys)
        {
            AK.ExpressionSolver solver = es.solver;
            if (es.ranges.ContainsKey("t"))
            {
                solver.SetGlobalVariable("t", uvw.x);
            }
        }
        if (es.expressions[X].AKExpression != null &&
            es.expressions[Y].AKExpression != null &&
            es.expressions[Z].AKExpression != null)
        {
            output.x = (float)es.expressions[X].AKExpression.Evaluate();
            output.y = (float)es.expressions[Y].AKExpression.Evaluate();
            output.z = (float)es.expressions[Z].AKExpression.Evaluate();
        }
        return(output * scale);
    }
 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 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)  ));
 }
        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)
            {
            }
        }
Exemple #23
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;
    }
        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)));
        }