//------------------------------------------------------------------------------------------ //Поэлемнетное деление public static RealMatrix GetElementwiseDivision( IntegerMatrix dividendMatrix, IntegerMatrix dividerMatrix ) { bool equalSize = IntegerMatrix.IsEqualMatricesSize(dividendMatrix, dividerMatrix); if (!equalSize) { throw new MatrixException(); } int rowCount = dividendMatrix.rowCount; int columnCount = dividerMatrix.columnCount; RealMatrix newMatrix = new RealMatrix(rowCount, columnCount); for (int row = 0; row < rowCount; row++) { for (int column = 0; column < columnCount; column++) { double dividend = dividendMatrix[row, column]; double divider = dividerMatrix[row, column]; double value = Convert.ToDouble(dividend) / Convert.ToDouble(divider); newMatrix[row, column] = value; } } return(newMatrix); }
//------------------------------------------------------------------------------------------ //Подматрица public IntegerMatrix GetSubMatrix( int rowTopLeft, int columnTopLeft, int rowBottomRight, int columnBottomRight ) { int rowCountSubMatrix = rowBottomRight - rowTopLeft + 1; int columnCountSubMatrix = columnBottomRight - columnTopLeft + 1; IntegerMatrix subMatrix = new IntegerMatrix(rowCountSubMatrix, columnCountSubMatrix); for ( int rowThisMatrix = rowTopLeft, rowSubMatrix = 0; rowThisMatrix <= rowBottomRight; rowThisMatrix++, rowSubMatrix++ ) { for ( int columnThisMatrix = columnTopLeft, columnSubMatrix = 0; columnThisMatrix <= columnBottomRight; columnThisMatrix++, columnSubMatrix++ ) { subMatrix[rowSubMatrix, columnSubMatrix] = this.dataArray[rowThisMatrix, columnThisMatrix]; } } return(subMatrix); }
//------------------------------------------------------------------------------------------ //Дополнительная матрица public IntegerMatrix GetAugmentedMatrix(int matrixRow, int matrixColumn) { int rowCountAugmentedMatrix = this.rowCount - 1; int columnCountAugmentedMatrix = this.columnCount - 1; IntegerMatrix augmentedMatrix = new IntegerMatrix(rowCountAugmentedMatrix, columnCountAugmentedMatrix); int rowAugmentMatrix = 0; int columnAugmentMatrix = 0; for (int rowThisMatrix = 0; rowThisMatrix < this.rowCount; rowThisMatrix++) { if (rowThisMatrix != matrixRow) { for ( int columnThisMatrix = 0; columnThisMatrix < this.columnCount; columnThisMatrix++ ) { if (columnThisMatrix != matrixColumn) { augmentedMatrix[rowAugmentMatrix, columnAugmentMatrix] = this.dataArray[rowThisMatrix, columnThisMatrix]; columnAugmentMatrix++; } } rowAugmentMatrix++; columnAugmentMatrix = 0; } } return(augmentedMatrix); }
//------------------------------------------------------------------------------------------ //Минор элемента (row, column) public int GetMinor(int row, int column) { IntegerMatrix augmentMatrix = this.GetAugmentedMatrix(row, column); int minor = augmentMatrix.GetDeterminant(); return(minor); }
//------------------------------------------------------------------------------------------ //Разреженная матрица public IntegerMatrix GetSparseMatrix(int rowStep, int columnStep) { int rowCount = this.RowCount / rowStep; if (this.RowCount % rowStep != 0) { rowCount++; } int columnCount = this.ColumnCount / columnStep; if (this.ColumnCount % columnStep != 0) { columnCount++; } IntegerMatrix newMatrix = new IntegerMatrix(rowCount, columnCount); for (int thisColumn = 0, column = 0; thisColumn < this.ColumnCount; thisColumn += columnStep, column++) { for (int thisRow = 0, row = 0; thisRow < this.RowCount; thisRow += rowStep, row++) { newMatrix[row, column] = this[thisRow, thisColumn]; } } return(newMatrix); }
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------ //Умножение матриц public static IntegerMatrix operator *(IntegerMatrix matrixOne, IntegerMatrix matrixTwo) { int rowCountMatrixOne = matrixOne.rowCount; int rowCountMatrixTwo = matrixTwo.rowCount; int columnCountMatrixOne = matrixOne.columnCount; int columnCountMatrixTwo = matrixTwo.columnCount; if (columnCountMatrixOne != rowCountMatrixTwo) { throw new MatrixException(); } IntegerMatrix resultMatrix = new IntegerMatrix(rowCountMatrixOne, columnCountMatrixTwo); for (int row = 0; row < rowCountMatrixOne; row++) { int[] dataRow = matrixOne.GetRow(row); for (int column = 0; column < columnCountMatrixTwo; column++) { int[] dataColumn = matrixTwo.GetColumn(column); int result = ArrayOperator.ScalarProduct(dataRow, dataColumn); resultMatrix[row, column] = result; } } return(resultMatrix); }
//------------------------------------------------------------------------------------------ //Разность матриц public static IntegerMatrix operator -(IntegerMatrix matrixOne, IntegerMatrix matrixTwo) { int rowCountMatrixOne = matrixOne.rowCount; int rowCountMatrixTwo = matrixTwo.rowCount; int columnCountMatrixOne = matrixOne.columnCount; int columnCountMatrixTwo = matrixTwo.columnCount; bool sizeEquality = IntegerMatrix.IsEqualMatricesSize(matrixOne, matrixTwo); if (!sizeEquality) { throw new MatrixException(); } IntegerMatrix resultMatrix = new IntegerMatrix(rowCountMatrixOne, columnCountMatrixOne); for (int row = 0; row < resultMatrix.rowCount; row++) { for (int column = 0; column < resultMatrix.columnCount; column++) { resultMatrix[row, column] = matrixOne[row, column] - matrixTwo[row, column]; } } return(resultMatrix); }
//---------------------------------------------------------------------------------------- public IntegerMatrix ExcludeEvenRowsAndColumns() { int rowsCount = this.RowCount % 2 == 0 ? this.RowCount / 2 : this.RowCount / 2 + 1; int columnsCount = this.ColumnCount % 2 == 0 ? this.ColumnCount / 2 : this.ColumnCount / 2 + 1; IntegerMatrix resMatrix = new IntegerMatrix(rowsCount, columnsCount); int row = 0; int column = 0; for (int thisRow = 0; thisRow < this.RowCount; thisRow++) { if (thisRow % 2 == 0) { continue; } for (int thisColumn = 0; thisColumn < this.ColumnCount; thisColumn++) { if (thisColumn % 2 == 0) { continue; } resMatrix[row, column] = this[thisRow, thisColumn]; column++; } row++; column = 0; } return(resMatrix); }
//------------------------------------------------------------------------------------------ //Матрица с расширенными границами public IntegerMatrix GetExtendedBoundsMatrix(int newRowCount, int newColumnCount) { IntegerMatrix newMatrix = new IntegerMatrix(newRowCount, newColumnCount); int rowTopLeft = (newRowCount - this.RowCount) / 2; int columnTopLeft = (newColumnCount - this.ColumnCount) / 2; newMatrix.SetSubMatrix(this, rowTopLeft, columnTopLeft); return(newMatrix); }
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------ //Десериализация public static IntegerMatrix Deserialize(string nameFile) { BinaryFormatter binaryFormatter = new BinaryFormatter(); Stream fileStream = new FileStream (nameFile, FileMode.Open, FileAccess.Read, FileShare.None); IntegerMatrix matrix = (IntegerMatrix)binaryFormatter.Deserialize(fileStream); fileStream.Close(); return(matrix); }
//------------------------------------------------------------------------------------------ //Единичная матрица public static IntegerMatrix IdentityMatrix(int size) { IntegerMatrix matrix = new IntegerMatrix(size, size); for (int index = 0; index < size; index++) { matrix[index, index] = 1; } return(matrix); }
//------------------------------------------------------------------------------------------ //Проверка размеров матриц public static bool IsEqualMatricesSize( IntegerMatrix matrixOne, IntegerMatrix matrixTwo ) { bool equalityRowsCount = (matrixOne.rowCount == matrixTwo.rowCount); bool equalityColumnsCount = (matrixOne.columnCount == matrixTwo.columnCount); bool result = equalityRowsCount && equalityColumnsCount; return(result); }
//------------------------------------------------------------------------------------------ //Подматрица из центра матрицы public IntegerMatrix GetCenteredSubMatrix(int rowCount, int columnCount) { int rowTopLeft = (this.RowCount - rowCount) / 2; int columnTopLeft = (this.ColumnCount - columnCount) / 2; int rowBottomRight = rowTopLeft + rowCount - 1; int columnBottomRight = columnTopLeft + columnCount - 1; IntegerMatrix resultMatrix = this.GetSubMatrix(rowTopLeft, columnTopLeft, rowBottomRight, columnBottomRight); return(resultMatrix); }
//----------------------------------------------------------------------------------------- public RealMatrix(IntegerMatrix matrix) { this.rowCount = matrix.RowCount; this.columnCount = matrix.ColumnCount; this.dataArray = new double[this.rowCount, this.columnCount]; for (int row = 0; row < this.rowCount; row++) { for (int column = 0; column < this.columnCount; column++) { this.dataArray[row, column] = matrix[row, column]; } } }
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------ //Матрица случайных целых чисел public static IntegerMatrix GetRandomIntegerMatrix(int rowCount, int columnCount, int maxValue) { RandomNumberGenerator randomNumberGenerator = new RandomNumberGenerator(); IntegerMatrix randomMatrix = new IntegerMatrix(rowCount, columnCount); for (int row = 0; row < randomMatrix.RowCount; row++) { for (int column = 0; column < randomMatrix.ColumnCount; column++) { int value = randomNumberGenerator.GetNextInteger(maxValue); randomMatrix[row, column] = value; } } return(randomMatrix); }
//------------------------------------------------------------------------------------------ //Умножение на число public static IntegerMatrix operator *(IntegerMatrix matrix, int value) { int rowCount = matrix.rowCount; int columnCount = matrix.columnCount; IntegerMatrix newMatrix = new IntegerMatrix(rowCount, columnCount); for (int row = 0; row < rowCount; row++) { for (int column = 0; column < columnCount; column++) { int newValue = matrix[row, column] * value; newMatrix[row, column] = newValue; } } return(newMatrix); }
//------------------------------------------------------------------------------------------ //Установка подматрицы public void SetSubMatrix(IntegerMatrix matrix, int rowTopLeft, int columnTopLeft) { if ( this.RowCount < rowTopLeft + matrix.RowCount || this.ColumnCount < columnTopLeft + matrix.ColumnCount ) { throw new MatrixException(); } for ( int row = rowTopLeft, subMatrixRow = 0; subMatrixRow < matrix.RowCount; row++, subMatrixRow++ ) { for ( int column = columnTopLeft, subMatrixColumn = 0; subMatrixColumn < matrix.ColumnCount; column++, subMatrixColumn++ ) { this[row, column] = matrix[subMatrixRow, subMatrixColumn]; } } }
//------------------------------------------------------------------------------------------ //Транспонирование матрицы public IntegerMatrix GetTransposedMatrix() { int columnCountNewMatrix = this.rowCount; int rowCountNewMatrix = this.columnCount; IntegerMatrix newMatrix = new IntegerMatrix(rowCountNewMatrix, columnCountNewMatrix); for (int rowThisMatrix = 0; rowThisMatrix < this.rowCount; rowThisMatrix++) { for ( int columnThisMatrix = 0; columnThisMatrix < this.columnCount; columnThisMatrix++ ) { int rowNewMatrix = columnThisMatrix; int columnNewMatrix = rowThisMatrix; newMatrix[rowNewMatrix, columnNewMatrix] = this.dataArray[rowThisMatrix, columnThisMatrix]; } } return(newMatrix); }
//----------------------------------------------------------------------------------------- public IntegerMatrix(IntegerMatrix matrix) : this(matrix.dataArray) { }