コード例 #1
0
        public void CompileTest()
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");

            string testEquation = "3.2+2";
            EqCompiler compiler = new EqCompiler(testEquation, true);
            compiler.Compile();
            Assert.AreEqual(5.2, compiler.Calculate());
        }
コード例 #2
0
        public static void getEquationInterpolation(string equationString, double startTime, double endTime, int nSamples, int startIndex, double[]output, List<Variable> existingVariables, List<Waveform> existingCommonWaveforms, double wfDuration)
        {
            string status = getEquationStatusString(equationString, existingVariables, existingCommonWaveforms);

            if (status == "Valid equation.")
            {
                // HERE WE GO!

                dotMath.EqCompiler eq = new EqCompiler(equationString, true);
                // This should not fail, because if it did we would have caught it with the status check
                eq.Compile();

                string [] foundvars = eq.GetVariableList();
                Dictionary<string, Waveform> waveformDependentVariables = new Dictionary<string, Waveform>();

                bool usingTimeVariable = false;
                bool usingCommonWaveforms = false;

                if (foundvars != null)
                {
                    foreach (string varname in foundvars)
                    {
                        bool variableMapped = false;
                        if (varname != "t")
                        {
                            foreach (Variable var in existingVariables)
                            {
                                if (var.VariableName == varname)
                                {
                                    eq.SetVariable(varname, var.VariableValue);
                                    variableMapped = true;
                                }
                            }
                        }

                        if (varname == "t")
                        {
                            usingTimeVariable = true;
                            variableMapped = true;
                        }

                        // variable named varname has not yet been mapped. Thus it must be a common waveform
                        if (!variableMapped)
                        {
                            if (existingCommonWaveforms != null)
                            {
                                foreach (Waveform wf in existingCommonWaveforms)
                                {
                                    if (wf.WaveformName == varname)
                                    {
                                        if (!waveformDependentVariables.ContainsKey(varname))
                                        {
                                            waveformDependentVariables.Add(varname, wf);
                                            usingCommonWaveforms = true;
                                        }
                                    }
                                }
                            }
                        }

                    }
                }

                Dictionary<Waveform, double[]> commonWaveformInterpolations = new Dictionary<Waveform, double[]>();
                if (usingCommonWaveforms)
                {
                    foreach (Waveform wf in waveformDependentVariables.Values) {
                        WaveformEquationInterpolator.stackCount++;
                        if (stackCount > 40)
                        {
                            stackCount = 0;
                            throw new InvalidDataException("Stack count has reached 40 when attempting to interpolation an equation waveform. You have probably created a recursive reference loop using waveform equations. Please remove the offending circular references. Aborting interpolation.");
                        }
                            double[] interpol = wf.getInterpolation(nSamples, startTime, endTime, existingVariables, existingCommonWaveforms);
                            commonWaveformInterpolations.Add(wf, interpol);
                        stackCount--;
                    }
                }

                for (int i = 0; i < nSamples; i++)
                {
                    if (usingTimeVariable)
                    {
                        double time = i * (endTime - startTime) / (double)nSamples + startTime;

                        if (time > wfDuration)
                            time = wfDuration;

                        eq.SetVariable("t", time);
                    }

                    if (usingCommonWaveforms)
                    {
                        foreach (string vname in waveformDependentVariables.Keys)
                        {
                            eq.SetVariable(vname, commonWaveformInterpolations[waveformDependentVariables[vname]][i]);
                        }
                    }

                    try
                    {
                        double val = eq.Calculate();
                        if ((!double.IsInfinity(val)) && (!double.IsNaN(val)))
                        {
                            output[i + startIndex] = eq.Calculate();
                        }
                    }
                    catch (Exception)
                    {
                        return;
                    }
                }
            }
        }