Beispiel #1
0
        private void button2_Click(object sender, EventArgs e)
        {
            int Length;

            try
            {
                Length = Convert.ToInt32(textBox1.Text);
            }
            catch
            {
                return;
            }
            if ((Length > 30 || Length < 1))
            {
                LogOutput("Wrong size");
                return;
            }
            if (!MatrixExist)
            {
                LogOutput("You need to precompute inverte matrix");
                return;
            }
            double[,] x = new double[Length, 1];
            double[,] u = new double[Length, 1];
            double[,] v = new double[Length, 1];
            double[,] z = new double[Length, 1];
            double[,] y = new double[Length, 1];
            double[,] b = new double[Length, 1];
            double[,] A = new double[Length, Length];
            try
            {
                for (int i = 0; i < Length; i++)
                {
                    x[i, 0] = (Convert.ToDouble(dataGridView1.Rows[i].Cells[Length].Value));
                }
            }
            catch
            {
                LogOutput("Initial x vector is wrong;");
                return;
            }
            try
            {
                for (int i = 0; i < Length; i++)
                {
                    b[i, 0] = (Convert.ToDouble(dataGridView1.Rows[i].Cells[Length + 1].Value));
                }
            }
            catch
            {
                LogOutput("B vector is wrong");
                return;
            }
            try
            {
                for (int i = 0; i < Length; i++)
                {
                    for (int j = 0; j < Length; j++)
                    {
                        A[i, j] = (Convert.ToDouble(dataGridView1.Rows[i].Cells[j].Value));
                    }
                }
            }
            catch
            {
                LogOutput("A matrix is wrong");
                return;
            }
            try
            {
                for (int i = 0; i < Length; i++)
                {
                    u[i, 0] = (Convert.ToDouble(dataGridView1.Rows[i].Cells[Length + 2].Value));
                }
            }
            catch
            {
                LogOutput("U vector is wrong");
                return;
            }
            try
            {
                for (int i = 0; i < Length; i++)
                {
                    v[i, 0] = (Convert.ToDouble(dataGridView1.Rows[i].Cells[Length + 3].Value));
                }
            }
            catch
            {
                LogOutput("V vector is wrong");
                return;
            }
            DateTime Now = DateTime.Now;
            var      UV  = Matrixes.Multiply(u, Matrixes.GetTransp(v, Length, 1));

            for (int i = 0; i < Length; i++)
            {
                for (int j = 0; j < Length; j++)
                {
                    NewMatrix[i, j] = A[i, j] - UV[i, j];
                }
            }
            z = Matrixes.GetTransp(Matrixes.Multiply(Matrixes.GetTransp(v, Length, 1), InverseMatrix), 1, Length);
            y = Matrixes.Multiply(InverseMatrix, u);
            double alfa = 1d / (1 - Matrixes.Multiply(Matrixes.GetTransp(v, Length, 1), y)[0, 0]);
            double beta = Matrixes.Multiply(Matrixes.GetTransp(z, Length, 1), b)[0, 0];

            for (int i = 0; i < Length; i++)
            {
                x[i, 0] = x[i, 0] + alfa * beta * y[i, 0];
            }
            for (int i = 0; i < Length; i++)
            {
                dataGridView2.Rows[i].Cells[Length].Value = x[i, 0];
            }
            LogOutput("Time of work =" + (DateTime.Now - Now).TotalMilliseconds / 1000 + "s");
        }
Beispiel #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            int Length;

            try
            {
                Length = Convert.ToInt32(textBox1.Text);
            }
            catch
            {
                return;
            }
            if ((Length > 30 || Length < 1))
            {
                LogOutput("Wrong size");
                return;
            }
            double[,] x = new double[Length, 1];
            double[,] u = new double[Length, 1];
            double[,] v = new double[Length, 1];
            double[,] b = new double[Length, 1];
            double[,] A = new double[Length, Length];
            try
            {
                for (int i = 0; i < Length; i++)
                {
                    x[i, 0] = (Convert.ToDouble(dataGridView1.Rows[i].Cells[Length].Value));
                }
            }
            catch
            {
                LogOutput("Initial x vector is wrong;");
                return;
            }
            try
            {
                for (int i = 0; i < Length; i++)
                {
                    b[i, 0] = (Convert.ToDouble(dataGridView1.Rows[i].Cells[Length + 1].Value));
                }
            }
            catch
            {
                LogOutput("B vector is wrong");
                return;
            }
            try
            {
                for (int i = 0; i < Length; i++)
                {
                    for (int j = 0; j < Length; j++)
                    {
                        A[i, j] = (Convert.ToDouble(dataGridView1.Rows[i].Cells[j].Value));
                    }
                }
            }
            catch
            {
                LogOutput("A matrix is wrong");
                return;
            }
            try
            {
                for (int i = 0; i < Length; i++)
                {
                    u[i, 0] = (Convert.ToDouble(dataGridView1.Rows[i].Cells[Length + 2].Value));
                }
            }
            catch
            {
                LogOutput("U vector is wrong");
                return;
            }
            try
            {
                for (int i = 0; i < Length; i++)
                {
                    v[i, 0] = (Convert.ToDouble(dataGridView1.Rows[i].Cells[Length + 3].Value));
                }
            }
            catch
            {
                LogOutput("V vector is wrong");
                return;
            }
            DateTime Now = DateTime.Now;

            InverseMatrix = Matrixes.GetReverse(A, Length);
            var res = Matrixes.Multiply(InverseMatrix, b);

            for (int i = 0; i < Length; i++)
            {
                dataGridView1.Rows[i].Cells[Length].Value = res[i, 0];
            }
            var UV = Matrixes.Multiply(u, Matrixes.GetTransp(v, Length, 1));

            MatrixExist = true;
            dataGridView2.Columns.Clear();
            dataGridView2.Rows.Clear();
            for (int i = 0; i < Length; i++)
            {
                dataGridView2.Columns.Add(new DataGridViewTextBoxColumn());
            }
            dataGridView2.Columns.Add(new DataGridViewTextBoxColumn());
            dataGridView2.Columns.Add(new DataGridViewTextBoxColumn());
            for (int i = 0; i < Length; i++)
            {
                dataGridView2.Rows.Add();
            }
            NewMatrix = new double[3, 3];
            for (int i = 0; i < Length; i++)
            {
                for (int j = 0; j < Length; j++)
                {
                    dataGridView2.Rows[i].Cells[j].Value = (NewMatrix[i, j] = A[i, j] - UV[i, j]);
                }
                dataGridView2.Rows[i].Cells[Length + 1].Value = b[i, 0];
            }
            LogOutput("Time of work =" + (DateTime.Now - Now).TotalMilliseconds / 1000 + "s");
            MatrixExist = true;
        }