void NewtonsMethod()
        {
            Function       = textbox_InpFunct_Newt.Text;
            DeriveFunction = textbox_InpDerFunct_Newt.Text;

            double Eps = 0.0001;

            MathParser.Parser p = new MathParser.Parser();
            CurrentX = InitialApprox;

            string derfunction;
            string function;

            Counter[1] = 0;

            while (Math.Abs(PreviousX - CurrentX) >= Eps)
            {
                PreviousX   = CurrentX;
                derfunction = ReplaceVariable(DeriveFunction, "x", PreviousX);
                function    = ReplaceVariable(Function, "x", PreviousX);
                CurrentX    = PreviousX - p.ResultReturn(function) / p.ResultReturn(derfunction);
                Counter[1]++;
                if (Counter[1] > 10000)
                {
                    MessageBox.Show("Итераций больше 10000. Остановка программы. Текущий результат = " + CurrentX.ToString());
                    Thread.Sleep(2000);
                    Close();
                    return;
                }
            }
            Answer[1] = CurrentX;
        }
        void MSI()
        {
            Function       = textbox_InpFunct_MSI.Text;
            DeriveFunction = textbox_InpDerFunct_MSI.Text;

            MathParser.Parser p = new MathParser.Parser();
            PreviousX = InitialApprox;

            string DerfunctionLeft  = ReplaceVariable(DeriveFunction, "x", leftBoard);
            string DerfunctionRight = ReplaceVariable(DeriveFunction, "x", rightBoard);
            double Max = Math.Abs(Math.Max(p.ResultReturn(DerfunctionLeft), p.ResultReturn(DerfunctionRight)));

            if (Max >= 1)
            {
                return;
            }

            double q = Max;
            string function;
            double Eps   = 0.0001;
            double Apost = (1 - q) * Eps / q;

            Counter[0] = 0;

            while (Math.Abs(PreviousX - CurrentX) > Eps)
            {
                PreviousX = CurrentX;
                function  = ReplaceVariable(Function, "x", PreviousX);
                CurrentX  = p.ResultReturn(function);
                Counter[0]++;
            }
            Answer[0] = CurrentX;
        }
Esempio n. 3
0
        public static double Calc(string f, double x0)
        {
            string x      = Convert.ToString(x0);
            string newstr = f.Replace("x", "(" + x + ")");

            MathParser.Parser p = new MathParser.Parser();
            p.Evaluate(newstr);
            return(p.Result);
        }
        //calculate function a
        public double Counter(String a, double[] X, int m)
        {
            double c = 0;

            a = ReplaceMathFunctions(a);
            a = GetFinalFunction(a, X, m);

            MathParser.Parser p = new MathParser.Parser();
            if (p.Evaluate(a))
            {
                c = p.Result;
            }
            return(c);
        }
Esempio n. 5
0
        static void RunBenchmark(BenchmarkKind which)
        {
            var yamp       = YAMP.Parser.PrimaryContext;
            var mpparser   = new MathParser.Parser();
            var mptk       = new MathParserTK_NET.MathParserTK();
            var mpnet      = new MathParserNet.Parser();
            var mfmp       = new MathFunctions.MathParser();
            var llmp       = new MathParserDataStructures.MathObj();
            var calcEngine = new CalcEngine.CalcEngine();

            calcEngine.CacheExpressions = false;

            var lines = new string[0];

            switch (which)
            {
            case BenchmarkKind.Standard:
                //         UB
                //YAMP  : 154 ms
                //LLMP  : 108 ms
                //MP    : 4134 ms
                //MPTK  : 375 ms
                //MPNET : 3054 ms
                //MFP   : 88 ms
                //CALEN : 33 ms
                //NCALC : 420 ms
                lines = MakeTenK("2-3*5+7/2-8*2");
                break;

            case BenchmarkKind.File:
                //         UB
                //YAMP  : 2084 ms
                //LLMP  : 1072 ms
                //MP    : 372847 ms
                //MPTK  : ---
                //MPNET : ---
                //MFP   : ---
                //CALEN : 271 ms
                //NCALC : ---
                if (!File.Exists(BMK_FILE))
                {
                    GenerateBenchmarks();
                }

                lines = File.ReadAllLines(BMK_FILE);
                break;

            case BenchmarkKind.Little:
                //         UB
                //YAMP  : 71 ms
                //LLMP  : 59 ms
                //MP    : 1840 ms
                //MPTK  : 87 ms
                //MPNET : 3232 ms
                //MFP   : 37 ms
                //CALEN : 23 ms
                //NCALC : 247 ms
                lines = MakeTenK("2+3");
                break;

            case BenchmarkKind.Thomson:
                //         UB
                //YAMP  : 193 ms
                //LLMP  : 138 ms
                //MP    : 11508 ms
                //MPTK  : 647 ms
                //MPNET : 3827 ms
                //MFP   : ---
                //CALEN : 41 ms
                //NCALC : ---
                lines = MakeTenK("2-(3*5)^2+7/(2-8)*2");
                break;
            }

            Console.WriteLine("Starting benchmarks ...");
            Console.WriteLine("----------");

            // The implementation here... YAMP
            Benchmark("YAMP", lines, query => yamp.Run(query));

            //http://www.codeproject.com/Articles/53001/LL-Mathematical-Parser
            Benchmark("LLMathParser", lines, query => llmp.Evaluate(query, new char[0], new double[0]));

            //http://www.codeproject.com/Articles/11164/Math-Parser
            Benchmark("MathParser", lines, query => mpparser.Evaluate(query));

            //http://www.codeproject.com/Tips/381509/Math-Parser-NET-Csharp
            Benchmark("MathParserTK", lines, query => mptk.Parse(query, false));

            //http://www.codeproject.com/Articles/274093/Math-Parser-NET
            Benchmark("MathParserNet", lines, query => mpnet.Simplify(query));

            //http://www.codeproject.com/Articles/23061/MathParser-Math-Formula-Parser
            Benchmark("MathFormulaParser", lines, query => mfmp.Calculate(query));

            //http://www.codeproject.com/Articles/246374/A-Calculation-Engine-for-NET
            Benchmark("CalcEngine", lines, query => calcEngine.Evaluate(query));

            //http://ncalc.codeplex.com/
            //Benchmark("NCalc", lines, query => new NCalc.Expression(query, NCalc.EvaluateOptions.NoCache).Evaluate());
        }
Esempio n. 6
0
File: Form5.cs Progetto: Gollos/MAN
 private void button4_Click(object sender, EventArgs e)
 {
     MathParser.Parser p = new MathParser.Parser();
     if (p.Evaluate(textBox8.Text))
         richTextBox1.Text = p.Result.ToString();
     else richTextBox1.Text = "Ошибочное выражение";
 }
Esempio n. 7
0
        static void RunBenchmark(BenchmarkKind which)
        {
            var yamp = new YAMP.Parser();
            var mpparser = new MathParser.Parser();
            var mptk = new MathParserTK_NET.MathParserTK();
            var mpnet = new MathParserNet.Parser();
            var mfmp = new MathFunctions.MathParser();
            var llmp = new MathParserDataStructures.MathObj();
            var calcEngine = new CalcEngine.CalcEngine();
            calcEngine.CacheExpressions = false;

            var lines = new string[0];

            switch (which)
            {
                case BenchmarkKind.Standard:
                    //         UB
                    //YAMP  : 154 ms
                    //LLMP  : 108 ms
                    //MP    : 4134 ms
                    //MPTK  : 375 ms
                    //MPNET : 3054 ms
                    //MFP   : 88 ms
                    //CALEN : 33 ms
                    //NCALC : 420 ms
                    lines = MakeTenK("2-3*5+7/2-8*2");
                    break;

                case BenchmarkKind.File:
                    //         UB
                    //YAMP  : 2084 ms
                    //LLMP  : 1072 ms
                    //MP    : 372847 ms
                    //MPTK  : ---
                    //MPNET : ---
                    //MFP   : ---
                    //CALEN : 271 ms
                    //NCALC : ---
                    if (!File.Exists(BMK_FILE))
                        GenerateBenchmarks();

                    lines = File.ReadAllLines(BMK_FILE);
                    break;

                case BenchmarkKind.Little:
                    //         UB
                    //YAMP  : 71 ms
                    //LLMP  : 59 ms
                    //MP    : 1840 ms
                    //MPTK  : 87 ms
                    //MPNET : 3232 ms
                    //MFP   : 37 ms
                    //CALEN : 23 ms
                    //NCALC : 247 ms
                    lines = MakeTenK("2+3");
                    break;

                case BenchmarkKind.Thomson:
                    //         UB
                    //YAMP  : 193 ms
                    //LLMP  : 138 ms
                    //MP    : 11508 ms
                    //MPTK  : 647 ms
                    //MPNET : 3827 ms
                    //MFP   : ---
                    //CALEN : 41 ms
                    //NCALC : ---
                    lines = MakeTenK("2-(3*5)^2+7/(2-8)*2");
                    break;
            }

            Console.WriteLine("Starting benchmarks ...");
            Console.WriteLine("----------");

            // The implementation here... YAMP
            Benchmark("YAMP", lines, query => yamp.Evaluate(query));

            //http://www.codeproject.com/Articles/53001/LL-Mathematical-Parser
            Benchmark("LLMathParser", lines, query => llmp.Evaluate(query, new char[0], new double[0]));

            //http://www.codeproject.com/Articles/11164/Math-Parser
            Benchmark("MathParser", lines, query => mpparser.Evaluate(query));

            //http://www.codeproject.com/Tips/381509/Math-Parser-NET-Csharp
            Benchmark("MathParserTK", lines, query => mptk.Parse(query, false));

            //http://www.codeproject.com/Articles/274093/Math-Parser-NET
            Benchmark("MathParserNet", lines, query => mpnet.Simplify(query));

            //http://www.codeproject.com/Articles/23061/MathParser-Math-Formula-Parser
            Benchmark("MathFormulaParser", lines, query => mfmp.Calculate(query));

            //http://www.codeproject.com/Articles/246374/A-Calculation-Engine-for-NET
            Benchmark("CalcEngine", lines, query => calcEngine.Evaluate(query));

            //http://ncalc.codeplex.com/
            //Benchmark("NCalc", lines, query => new NCalc.Expression(query, NCalc.EvaluateOptions.NoCache).Evaluate());
        }