namespace Pavlenko_3PKS_318
static void Swap<T>(ref T x, ref T y)
static bool checkUsl(ref double[] xk, ref double[] xkp, int n, double eps)
for (int i = 0; i < n; i++)
norm += (xk[i] - xkp[i]) * (xk[i] - xkp[i]);
return (Math.Sqrt(norm) < eps);
static void matricaOutput(ref double[,] X, ref double[] B, int n)
Console.WriteLine("Вывод матрицы со стобцом свободных членов(округление значений до 4-x знаков)");
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
Console.Write("{0}\t", Math.Round(X[i, j], 4));
Console.Write("{0}\n", Math.Round(B[i], 4));
static void change(ref double[,] matrix, ref double[] b, int n, int num)
double[] stroka = new double[n];
for (int i = num + 1; i < n; i++)
for (int j = 0; j < n; j++)
stroka[j] = matrix[i, j];
if (checkDiag(ref stroka, n, num))
for (int j = 0; j < n; j++)
Swap(ref matrix[numOfStr, j], ref matrix[num, j]);
Swap(ref b[num], ref b[numOfStr]);
static bool checkDiag(ref double[] stroka, int n, int i)
double tmp = Math.Abs(stroka[i]);
for (int j = 0; j < n; j++)
summ += Math.Abs(stroka[j]);
static void Main(string[] args)
Console.Write("Введите размерность матрицы n: ");
int n = Convert.ToInt32(Console.ReadLine()); ;
double[,] matrix = new double[n, n];
double[] b = new double[n];
Console.WriteLine("Ввод матрицы: ");
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
Console.Write("\nВведите элемент с индексом i: {0} j: {0} =", i + 1, j + 1);
matrix[i, j] = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("\nВвод столбца свободных членов: ");
for (int i = 0; i < n; i++)
Console.WriteLine("\n{0}-й элемент: ", i + 1);
b[i] = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Введённая матрица:");
matricaOutput(ref matrix, ref b, n);
for (int i = 0; i < n; i++)
double tmp = Math.Abs(matrix[i, i]);
for (int j = 0; j < n; j++)
summ += Math.Abs(matrix[i, j]);
change(ref matrix, ref b, n, i);
Console.WriteLine("Матрица после преобразований: ");
matricaOutput(ref matrix, ref b, n);
double[] Xt = new double[n];
double[] Xpred = new double[n];
for (int i = 0; i < n; i++)
double tmp = Math.Abs(matrix[i, i]);
for (int j = 0; j < n; j++)
summ += Math.Abs(matrix[i, j]);
for (int i = 0; i < n; i++)
double diagElem = matrix[i, i];
for (int j = 0; j < n; j++)
matrix[i, j] /= diagElem;
for (int i = 0; i < n; i++)
matricaOutput(ref matrix, ref b, n);
for (int i = 0; i < n; i++)
for (int i = 0; i < n; i++)
for (int j = 0; j < i; j++)
var += (matrix[i, j] * Xt[j]);
for (int j = i + 1; j < n; j++)
var += (matrix[i, j] * Xpred[j]);
Xt[i] = (b[i] - var) / matrix[i, i];
} while (!checkUsl(ref Xt, ref Xpred, n, eps));
for (int i = 0; i < n; i++)
Console.WriteLine("X{0} = {1}", i + 1, Math.Round(Xt[i], 4));