static public Vector Modified_withReverse(ScalarFunk1_N[][] matrixFunks, ScalarFunk1_N[] vectorFunks, double Eps = 10e-5) { Console.Write("Begin vector: "); var vectorX_next = new Vector(vectorFunks.Length); vectorX_next.SetValues(new double[] { 0.5, 0.5, 1.5, -1.0, -0.5, 1.5, 0.5, -0.5, 1.5, -1.5 }); vectorX_next.Show(); Console.WriteLine("Fill matrix: "); var matrixJ = new Matrix(matrixFunks.Length); matrixJ.SetValueByFunks(matrixFunks, vectorX_next.data); matrixJ.Show(); Console.WriteLine("Reverse matrix with LU: "); var matrixJ_L = new Matrix(matrixJ.N); var matrixJ_U = new Matrix(matrixJ.N); var matrixJ_P = new Matrix(matrixJ.N); LUP_decomposition.LUP(matrixJ, matrixJ_L, matrixJ_U, matrixJ_P); var matrixJ_reverse = LUP_decomposition.Reverse(matrixJ_L, matrixJ_U, matrixJ_P); matrixJ_reverse.Show(); var vectorF = new Vector(vectorFunks.Length); var vectorX_current = new Vector(vectorFunks.Length); var countIter = 0; do { countIter++; vectorX_current.Copy(vectorX_next); vectorF.SetValueByFunks(vectorFunks, vectorX_current.data); vectorX_next = vectorX_current - matrixJ_reverse * vectorF; Console.Write("Next vector: "); vectorX_next.Show(); } while ((vectorX_next - vectorX_current).Norm() >= Eps); Console.Write("Count iter: " + countIter.ToString() + '\n'); return(vectorX_next); }
static void Task_1() { using (StreamReader reader = new StreamReader("InputMatrixs.txt")) { var strLine = ""; while (!reader.EndOfStream) { Console.Write("______________________________________\n"); #region Считывание матрицы strLine = reader.ReadLine(); if (strLine == "") { break; } var sizeMatrix = Int32.Parse(strLine); var matrixA = new Matrix(sizeMatrix); for (int i = 0; i < sizeMatrix; i++) { strLine = reader.ReadLine(); var values = strLine.Split(' '); for (int j = 0; j < sizeMatrix; j++) { matrixA.data[i][j] = Int32.Parse(values[j]); } } #endregion #region LU разложение var matrixL = new Matrix(sizeMatrix); var matrixU = new Matrix(sizeMatrix); var matrixP = new Matrix(sizeMatrix); LUP_decomposition.LUP(matrixA, matrixL, matrixU, matrixP); Console.Write('\n'); Console.Write("Matrix L\n"); matrixL.Show(); Console.Write('\n'); Console.Write("Matrix U\n"); matrixU.Show(); Console.Write('\n'); Console.Write("Matrix P\n"); matrixP.Show(); Console.Write('\n'); var checkMatrixLeft = matrixL * matrixU; Console.Write("Matrix L * U\n"); checkMatrixLeft.Show(); Console.Write('\n'); var checkMatrixRigth = matrixP * matrixA; Console.Write("Matrix P * A\n"); checkMatrixRigth.Show(); #endregion #region Определитель Console.Write('\n'); var detA = LUP_decomposition.Det(matrixL, matrixU, matrixP); Console.Write("Det A with LU: " + detA.ToString() + '\n'); #endregion #region Обратная матрица Console.Write('\n'); var A_reverse = LUP_decomposition.Reverse(matrixL, matrixU, matrixP); Console.Write("A_reverse with LU:\n"); A_reverse.Show(); Console.Write('\n'); var CheckReverse_1 = matrixA * A_reverse; Console.Write("Check Reverse (A * A_reverse):\n"); CheckReverse_1.Show(); Console.Write('\n'); var CheckReverse_2 = A_reverse * matrixA; Console.Write("Check Reverse (A_reverse * A):\n"); CheckReverse_2.Show(); #endregion #region СЛАУ Console.Write('\n'); Console.Write("SLAU with LU:\n"); Random rnd = new Random(); Vector b = new Vector(matrixA.N); Console.Write("Generate vector b: "); for (int i = 0; i < b.data.Length; i++) { b.data[i] = rnd.Next() % 100; Console.Write(b.data[i].ToString("0.00") + ' '); } Console.Write("\nResult x: "); var x = LUP_decomposition.SLAU(matrixL, matrixU, matrixP, b); for (int i = 0; i < x.data.Length; i++) { Console.Write(x.data[i].ToString("0.00") + ' '); } Console.Write('\n'); #endregion #region Число обусловленности Console.Write('\n'); Console.Write("Condition number: "); var condNumber = matrixA.Norm() * LUP_decomposition.Reverse(matrixL, matrixU, matrixP).Norm(); Console.Write(condNumber.ToString("0.00") + '\n'); #endregion } } }