private string Process(string message, Mode mode) { MatrixClass matrix = new MatrixClass(key); if (mode == Mode.Decrypt) { matrix = matrix.Inverse(); } int pos = 0, charPosition; string substring, result = ""; int matrixSize = key.GetLength(0); while (pos < message.Length) { substring = message.Substring(pos, matrixSize); pos += matrixSize; for (int i = 0; i < matrixSize; i++) { charPosition = 0; for (int j = 0; j < matrixSize; j++) { charPosition += (int)matrix[j, i].Numerator * alphabet[substring[j]]; } result += alphabet.Keys.ElementAt(charPosition % 26); } } return result; }
/// <summary> /// The function returns the adjoint of the current MatrixClass /// </summary> public MatrixClass Adjoint(int b) { if (this.Rows != this.Cols) throw new MatrixClassException("Adjoint of a non-square MatrixClass does not exists"); MatrixClass AdjointMatrixClass = new MatrixClass(this.Rows, this.Cols); for (int i = 0; i < this.Rows; i++) for (int j = 0; j < this.Cols; j++) AdjointMatrixClass[i, j] = b * Math.Pow(-1, i + j) * (Minor(this, j, i).Determinent()); return AdjointMatrixClass; }
/// <summary> /// The function returns a Scalar MatrixClass of dimension ( Row x Col ) and scalar K /// </summary> public static MatrixClass ScalarMatrixClass(int iRows, int iCols, int K) { Fraction zero = new Fraction(0); Fraction scalar = new Fraction(K); MatrixClass MatrixClass = new MatrixClass(iRows, iCols); for (int i = 0; i < iRows; i++) for (int j = 0; j < iCols; j++) { if (i == j) MatrixClass[i, j] = scalar; else MatrixClass[i, j] = zero; } return MatrixClass; }
/// <summary> /// The function returns a Unit MatrixClass of dimension ( Row x Col ) /// </summary> public static MatrixClass UnitMatrixClass(int iRows, int iCols) { Fraction temp = new Fraction(1); MatrixClass MatrixClass = new MatrixClass(iRows, iCols); for (int i = 0; i < iRows; i++) for (int j = 0; j < iCols; j++) MatrixClass[i, j] = temp; return MatrixClass; }
/// <summary> /// The function concatenates the two given matrices column-wise /// it can be helpful in a equation solver class where the augmented MatrixClass is obtained by concatenation /// </summary> public static MatrixClass Concatenate(MatrixClass MatrixClass1, MatrixClass MatrixClass2) { if (MatrixClass1.Rows != MatrixClass2.Rows) throw new MatrixClassException("Concatenation not possible"); MatrixClass MatrixClass = new MatrixClass(MatrixClass1.Rows, MatrixClass1.Cols + MatrixClass2.Cols); for (int i = 0; i < MatrixClass.Rows; i++) for (int j = 0; j < MatrixClass.Cols; j++) { if (j < MatrixClass1.Cols) MatrixClass[i, j] = MatrixClass1[i, j]; else MatrixClass[i, j] = MatrixClass2[i, j - MatrixClass1.Cols]; } return MatrixClass; }
/// <summary> /// The function return the Minor of element[Row,Col] of a MatrixClass object /// </summary> public static MatrixClass Minor(MatrixClass MatrixClass, int iRow, int iCol) { MatrixClass minor = new MatrixClass(MatrixClass.Rows - 1, MatrixClass.Cols - 1); int m = 0, n = 0; for (int i = 0; i < MatrixClass.Rows; i++) { if (i == iRow) continue; n = 0; for (int j = 0; j < MatrixClass.Cols; j++) { if (j == iCol) continue; minor[m, n] = MatrixClass[i, j]; n++; } m++; } return minor; }
/// <summary> /// Internal Fucntions for the above operators /// </summary> private static MatrixClass Negate(MatrixClass MatrixClass) { return MatrixClass.Multiply(MatrixClass, -1); }
/// <summary> /// The helper function for the above Determinent() method /// it calls itself recursively and computes determinent using minors /// </summary> private Fraction Determinent(MatrixClass MatrixClass) { Fraction det = new Fraction(0); if (MatrixClass.Rows != MatrixClass.Cols) throw new MatrixClassException("Determinent of a non-square MatrixClass doesn't exist"); if (MatrixClass.Rows == 1) return MatrixClass[0, 0]; for (int j = 0; j < MatrixClass.Cols; j++) det += (MatrixClass[0, j] * Determinent(MatrixClass.Minor(MatrixClass, 0, j)) * (int)System.Math.Pow(-1, 0 + j)); return det; }
private static MatrixClass Multiply(MatrixClass MatrixClass, Fraction frac) { MatrixClass result = new MatrixClass(MatrixClass.Rows, MatrixClass.Cols); for (int i = 0; i < MatrixClass.Rows; i++) for (int j = 0; j < MatrixClass.Cols; j++) result[i, j] = MatrixClass[i, j] * frac; return result; }
private static MatrixClass Multiply(MatrixClass MatrixClass1, MatrixClass MatrixClass2) { if (MatrixClass1.Cols != MatrixClass2.Rows) throw new MatrixClassException("Operation not possible"); MatrixClass result = MatrixClass.NullMatrixClass(MatrixClass1.Rows, MatrixClass2.Cols); for (int i = 0; i < result.Rows; i++) for (int j = 0; j < result.Cols; j++) for (int k = 0; k < MatrixClass1.Cols; k++) result[i, j] += MatrixClass1[i, k] * MatrixClass2[k, j]; return result; }
private static MatrixClass Add(MatrixClass MatrixClass1, MatrixClass MatrixClass2) { if (MatrixClass1.Rows != MatrixClass2.Rows || MatrixClass1.Cols != MatrixClass2.Cols) throw new MatrixClassException("Operation not possible"); MatrixClass result = new MatrixClass(MatrixClass1.Rows, MatrixClass1.Cols); for (int i = 0; i < result.Rows; i++) for (int j = 0; j < result.Cols; j++) result[i, j] = MatrixClass1[i, j] + MatrixClass2[i, j]; return result; }
/// <summary> /// The function returns the transpose of the current MatrixClass /// </summary> public MatrixClass Transpose() { MatrixClass TransposeMatrixClass = new MatrixClass(this.Cols, this.Rows); for (int i = 0; i < TransposeMatrixClass.Rows; i++) for (int j = 0; j < TransposeMatrixClass.Cols; j++) TransposeMatrixClass[i, j] = this[j, i]; return TransposeMatrixClass; }
/// <summary> /// The function duplicates the current MatrixClass object /// </summary> public MatrixClass Duplicate() { MatrixClass MatrixClass = new MatrixClass(Rows, Cols); for (int i = 0; i < Rows; i++) for (int j = 0; j < Cols; j++) MatrixClass[i, j] = this[i, j]; return MatrixClass; }