private int[,] RemoveLinearDependence(int[,] matrix, ref int gap) { int[,] tempMatrix = (int[, ])matrix.Clone(); List <int> linearDependentRows = new List <int>(); for (int i = 0; i < tempMatrix.GetLength(0); i++) { int count = 0; for (int j = 0; j < tempMatrix.GetLength(1); j++) { count += tempMatrix[i, j]; } if (count == 0 || count == tempMatrix.GetLength(1)) { linearDependentRows.Add(i); } } linearDependentRows.Reverse(); foreach (var row in linearDependentRows) { if (row == tempMatrix.GetLength(0)) { continue; } MatrixOperation.PermuteRow(tempMatrix, row, tempMatrix.GetLength(0) - 1); } int[,] result = new int[tempMatrix.GetLength(0) - linearDependentRows.Count, tempMatrix.GetLength(1)]; for (int i = 0; i < result.GetLength(0); i++) { for (int j = 0; j < tempMatrix.GetLength(1); j++) { result[i, j] = tempMatrix[i, j]; } } gap -= linearDependentRows.Count; return(result); }
private static int[,] GetEncodeMatrix(int[,] parityMatrix, out int gap) { int[,] parityMatrixCopy = (int[, ])parityMatrix.Clone(); int permutations = 0; for (int i = 0; i < parityMatrixCopy.GetLength(0) - permutations; i++) { if (parityMatrixCopy[i, parityMatrixCopy.GetLength(1) - 1] == 1) { MatrixOperation.PermuteRow(parityMatrixCopy, i, parityMatrixCopy.GetLength(0) - 1); permutations++; i = 0; } } gap = permutations - 1; // номер столбца, с которым мы работаем int p = parityMatrixCopy.GetLength(1) - 2; int j = 2; for (; ;) { int diagonalRow = parityMatrixCopy.GetLength(0) - (gap + j); if (diagonalRow < 0) { break; } int columnWithMinOnes = 0; int minOnesInColumn = Int32.MaxValue; for (int column = 0; column <= p; column++) { int onesInColumn = 0; for (int row = 0; row <= diagonalRow; row++) { if (parityMatrixCopy[row, column] == 1) { onesInColumn++; } } if (onesInColumn < minOnesInColumn && onesInColumn > 0) { minOnesInColumn = onesInColumn; columnWithMinOnes = column; } } MatrixOperation.PermuteColumn(parityMatrixCopy, columnWithMinOnes, p); int rowWithOne = 0; for (int row = 0; row <= diagonalRow; row++) { if (parityMatrixCopy[row, p] == 1) { rowWithOne = row; break; } } MatrixOperation.PermuteRow(parityMatrixCopy, rowWithOne, diagonalRow); if (minOnesInColumn > 1) { for (int i = 0; i < minOnesInColumn - 1; i++) { rowWithOne = 0; for (int row = 0; row < diagonalRow; row++) { if (parityMatrixCopy[row, p] == 1) { rowWithOne = row; break; } } MatrixOperation.PermuteRow(parityMatrixCopy, rowWithOne, parityMatrixCopy.GetLength(0) - 1); gap++; diagonalRow--; } } j++; p--; } int[,] gaussMatrix = GetGaussMatrix(parityMatrixCopy, gap); parityMatrixCopy = MatrixOperation.LogicMultiplicateMatrixes(gaussMatrix, parityMatrixCopy); Console.WriteLine("Проверочная"); MatrixOperation.ShowMatrix(parityMatrixCopy); return(parityMatrixCopy); }