public void ThirdEquationTest()
        {
            Matrix variableCoefficients = new Matrix(new float[, ]
            {
                { 8, 5, 3 },
                { -2, 8, 1 },
                { 1, 3, -10 }
            });

            Matrix freeCoefficients = new Matrix(new float[, ]
            {
                { 30 },
                { 15 },
                { 42 }
            });

            Matrix expectedResult = new Matrix(new float[, ]
            {
                { 3f },
                { 3f },
                { -3f }
            });

            float epsilon = 0.0001f;

            Matrix result = GaussZeidelMethod.Solve(variableCoefficients, freeCoefficients, epsilon);

            Assert.True(expectedResult.NearEquals(result), "Matrices are not equal:\nExpected:{0}\nResult:{1}",
                        expectedResult.ToString(), result.ToString());
        }
        public void SecondEquationTest()
        {
            Matrix variableCoefficients = new Matrix(new float[, ]
            {
                { 2, -1, -1 },
                { 1, 3, -2 },
                { 1, 2, 3 }
            });

            Matrix freeCoefficients = new Matrix(new float[, ]
            {
                { 5 },
                { 7 },
                { 10 }
            });

            Matrix expectedResult = new Matrix(new float[, ]
            {
                { 61f / 16f },
                { 27f / 16f },
                { 15f / 16f }
            });

            float epsilon = 0.0001f;

            Matrix result = GaussZeidelMethod.Solve(variableCoefficients, freeCoefficients, epsilon);

            Assert.True(expectedResult.NearEquals(result), "Matrices are not equal:\nExpected:{0}\nResult:{1}",
                        expectedResult.ToString(), result.ToString());
        }
示例#3
0
        private void btnCalc_Click(object sender, EventArgs e)
        {
            Point2D x0, h;
            double  eps = 0, x0x = 0, x0y = 0, hx = 0, hy = 0;
            Point2D res;

            //Парсинг параметров
            try
            {
                x0x = double.Parse(txtX0X.Text);
                x0y = double.Parse(txtX0Y.Text);
                hx  = double.Parse(txtHX.Text);
                hy  = double.Parse(txtHY.Text);
                eps = double.Parse(txtEps.Text);
            }
            catch (FormatException exp)
            {
                MessageBox.Show("Неверный ввод - не число");
                return;
            }

            x0 = new Point2D(x0x, x0y);
            h  = new Point2D(hx, hy);

            //Определяем функцию
            Func <double, double, double> f;

            if (rbTest.Checked)
            {
                f = test;
            }
            else
            {
                f = task211;
            }

            //Вычисление
            if (rbGZ.Checked)
            {
                res = GaussZeidelMethod.FindMinimum(f, x0, h, eps);
            }
            else
            {
                res = HookJeevesMethod.FindMinimum(f, x0, h, eps);
            }

            txtOut.Text = res.ToString(OptimizationUtils.GetFormat(eps));
        }
示例#4
0
        static void Test(int iterations, Matrix variableCoefficients, Matrix freeCoefficients)
        {
            float epsilon = 0.0001f;

            float relaxationWeight = 0.8f;
            var   stopwatch        = new Stopwatch();

            Console.Write("######################################################\nSelect the algorithm:\n1.Gauss\n2.Jacobi\n3.Gauss-Seidel\n4.Relaxation\nEnter the choice:");
            int algorithm = Convert.ToInt32(Console.ReadLine());

            switch (algorithm)
            {
            case 1:
            {
                stopwatch.Start();
                for (int i = 0; i < iterations; i++)
                {
                    var result = GaussMethod.Solve(Matrix.Clone(variableCoefficients), Matrix.Clone(freeCoefficients));
                }
                stopwatch.Stop();
                Console.WriteLine("Total elapsed time: " + stopwatch.Elapsed + "\n######################################################");
                break;
            }

            case 2:
            {
                stopwatch.Start();
                for (int i = 0; i < iterations; i++)
                {
                    var result = JacobiMethod.Solve(Matrix.Clone(variableCoefficients), Matrix.Clone(freeCoefficients), epsilon);
                }
                stopwatch.Stop();
                Console.WriteLine("Total elapsed time: " + stopwatch.Elapsed);
                break;
            }

            case 3:
            {
                stopwatch.Start();
                for (int i = 0; i < iterations; i++)
                {
                    var result = GaussZeidelMethod.Solve(Matrix.Clone(variableCoefficients), Matrix.Clone(freeCoefficients), epsilon);
                }
                stopwatch.Stop();
                Console.WriteLine("Total elapsed time: " + stopwatch.Elapsed);
                break;
            }

            case 4:
            {
                stopwatch.Start();
                for (int i = 0; i < iterations; i++)
                {
                    var result = RelaxationMethod.Solve(Matrix.Clone(variableCoefficients), Matrix.Clone(freeCoefficients), epsilon, relaxationWeight);
                }
                stopwatch.Stop();
                Console.WriteLine("Total elapsed time: " + stopwatch.Elapsed);
                break;
            }
            }
        }