private static Func <int[, ], Func <string, Func <CryptoMode, string> > > ProcessFunc() => key => message => mode => { var sbRet = new StringBuilder(); var matrix = new MatrixClass(key); var alphabet = AlphabetDictionaryGenerator.Generate(); if (mode == CryptoMode.Decrypt) { matrix = matrix.Inverse(); } var pos = 0; var matrixSize = key.GetLength(0); while (pos < message.Length) { for (var i = 0; i < matrixSize; i++) { var charPosition = 0; for (var j = 0; j < matrixSize; j++) { charPosition += (int)matrix[j, i].Numerator * alphabet[message.Substring(pos, matrixSize)[j]]; } sbRet.Append(alphabet.Keys.ElementAt(charPosition % 26)); } pos += matrixSize; } return(sbRet.ToString()); };
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); }
static void Main() { MatrixClass matrix1 = new MatrixClass(2, 2); matrix1[0, 0] = 1; matrix1[0, 1] = -2; matrix1[1, 0] = 3; matrix1[1, 1] = 3; MatrixClass matrix2 = new MatrixClass(2, 2); matrix2[0, 0] = 4; matrix2[0, 1] = 4; matrix2[1, 0] = 5; matrix2[1, 1] = 5; MatrixClass resultSum = matrix1 + matrix2; Console.WriteLine("Adding the two matrix:"); Console.WriteLine(resultSum.ToString()); MatrixClass resultExtraction = matrix1 - matrix2; Console.WriteLine("Extract Matrix2 from Matrix1:"); Console.WriteLine(resultExtraction.ToString()); MatrixClass resultMultiplication = matrix1 * matrix2; Console.WriteLine("Result of multiplication of Matrix1 and Matrix2:"); Console.WriteLine(resultMultiplication.ToString()); }
public override string Decrypt(string cipher) { MatrixClass matrix = new MatrixClass(key); matrix = matrix.Inverse(); int charPosition; StringBuilder result = new StringBuilder(); int matrixSize = key.GetLength(0); for (int r = 0; r < cipher.Length / matrixSize; ++r) { for (int i = 0; i < matrixSize; ++i) { charPosition = 0; for (int j = 0; j < matrixSize; ++j) { charPosition += (int)matrix[j, i].Numerator * alphabet[cipher[(r * matrixSize) + j]]; } if (template[i] == SymbolType.Character) { result.Append(alphabet.Keys.ElementAt(charPosition % alphabet.Count)); } } } return(result.ToString()); }
static void Main() { Console.WriteLine("Input n:"); int n = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Input m:"); int m = Convert.ToInt32(Console.ReadLine()); int[,] arr = new int[n, m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { Console.Write("Arr[" + (i + 1) + "," + (j + 1) + "] = "); arr[i, j] = Convert.ToInt32(Console.ReadLine()); } } MatrixClass arr_temp = new MatrixClass(n, m, arr); Console.WriteLine("\nMultiplyed elements value: "); Console.Write(arr_temp.multiply); Console.WriteLine(); Console.WriteLine("\nInput Index: "); int k = Convert.ToInt32(Console.ReadLine()); double sum = arr_temp[k - 1]; Console.Write("\nAvarage value: " + sum); Console.ReadKey(); }
//function to find number of groups public int numberOfGroups(MatrixClass matrix) { patientmatrix = matrix.matrix; int numberOfGroups = 0; bool[,] cellsVisited = new bool[patientmatrix.GetLength(0), patientmatrix.GetLength(1)]; for (int i = 0; i < patientmatrix.GetLength(0); i++) { for (int j = 0; j < patientmatrix.GetLength(1); j++) { //check if patient is 1 and the has not been visited earlier if (patientmatrix[i, j] == 1 && cellsVisited[i, j] != true) { numberOfGroups++; //Call function to perform Depth First Search on the element //Pass the matrix, row index, column index and cellsVisited matrix to traverse all the neighbours DepthFirstSearch(patientmatrix, i, j, cellsVisited); } else if (!(patientmatrix[i, j] == 1 || patientmatrix[i, j] == 0)) { return(-1); } } } return(numberOfGroups); }
public void SummDifferentRowsAndColumns() { MatrixClass <int> one = new MatrixClass <int>(6, 3); MatrixClass <int> two = new MatrixClass <int>(3, 6); MatrixClass <int> three; three = one + two; }
public override void Encrypt(StreamReader inputStream, StreamWriter outputStream) { MatrixClass matrix = new MatrixClass(key); int charPosition; int matrixSize = key.GetLength(0); Random random = new Random(); char b; int countBytes = 0; char[] temp = new char[3]; int k = 0; while (true) { if (k < matrixSize) { if (template[k] == SymbolType.Character) { b = (char)inputStream.Read(); ++countBytes; if (b != char.MaxValue) { temp[k] = b; } else if (b == char.MaxValue && countBytes % countOT != 0) { temp[k] = '-'; } else { break; } } else if (template[k] == SymbolType.Mask) { temp[k] = (char)random.Next(0, alphabet.Count); } ++k; continue; } for (int i = 0; i < matrixSize; ++i) { charPosition = 0; for (int j = 0; j < matrixSize; ++j) { charPosition += (int)matrix[j, i].Numerator * temp[j]; } outputStream.Write(alphabet.Keys.ElementAt(charPosition % alphabet.Count)); } k = 0; } outputStream.Flush(); }
public static MatrixClass operator +(MatrixClass matrix1Par, MatrixClass matrix2Par) { MatrixClass resultMatrix = new MatrixClass(matrix1Par.Rows,matrix1Par.Cols); for (int row = 0; row < matrix1Par.Rows; row++) { for (int col = 0; col < matrix1Par.Cols; col++) { resultMatrix[row,col] =matrix1Par[row, col] + matrix2Par[row, col]; } } return resultMatrix; }
public static MatrixClass operator *(MatrixClass matrix1Par, MatrixClass matrix2Par) { MatrixClass resultMatrix = new MatrixClass(matrix1Par.Rows, matrix1Par.Cols); for (int row = 0; row < matrix1Par.Rows; row++) { for (int col = 0; col < matrix1Par.Cols; col++) { resultMatrix[row, col] = matrix1Par[row, col] * matrix2Par[row, col]; } } return(resultMatrix); }
public static MatrixClass operator +(MatrixClass first, MatrixClass second) { MatrixClass result = new MatrixClass(first.Rows, first.Columns); for (int row = 0; row < first.Rows; row++) { for (int col = 0; col < first.Columns; col++) { result[row, col] = first[row, col] + second[row, col]; } } return result; }
public static MatrixClass operator -(MatrixClass first, MatrixClass second) { MatrixClass third = new MatrixClass(first.rows, second.columns); for (int rows = 0; rows < third.rows; rows++) { for (int columns = 0; columns < third.columns; columns++) { third[rows, columns] = first[rows, columns] - second[rows, columns]; } } return third; }
public ActionResult <int> FindNumberOfPatientGroups(MatrixClass matrix) { //Call function to find number of groups var numberOfGroups = _services.numberOfGroups(matrix); //if -1 is returned, it is an invalid group if (numberOfGroups < 0) { return(NotFound("Invalid patients group")); } Dictionary <string, int> response = new Dictionary <string, int>(); response.Add("numberOfGroups", numberOfGroups); return(Json(response)); }
public override string Encrypt(string plainText) { if (plainText.Length % countOT != 0) { plainText = string.Concat(plainText, "-"); } MatrixClass matrix = new MatrixClass(key); int charPosition; StringBuilder result = new StringBuilder(); int matrixSize = key.GetLength(0); Random random = new Random(); for (int r = 0; r < plainText.Length / countOT; ++r) { int[] temp = new int[3]; int posSubstring = 0; for (int k = 0; k < matrixSize; ++k) { if (template[k] == SymbolType.Character) { temp[k] = alphabet[plainText[(r * countOT) + posSubstring++]]; } else { temp[k] = alphabet[(char)random.Next(0, alphabet.Count)]; } } for (int i = 0; i < matrixSize; ++i) { charPosition = 0; for (int j = 0; j < matrixSize; ++j) { charPosition += (int)matrix[j, i].Numerator * temp[j]; } result.Append(alphabet.Keys.ElementAt(charPosition % alphabet.Count)); } } return(result.ToString()); }
static void Main() { MatrixClass<int> matrix1 = new MatrixClass<int>(2,2); matrix1[0, 0] = 1; matrix1[0, 1] = 2; matrix1[1, 0] = 3; matrix1[1, 1] = 0; MatrixClass<int> matrix2 = new MatrixClass<int>(2, 2); matrix2[0, 0] = 2; matrix2[0, 1] = 2; matrix2[1, 0] = 2; matrix2[1, 1] = 2; //Problem 10 - without predefening the operaotrs +,-, * the compiler throw an error. //You can check this - comment the +,-, * methods at the MatrixClass MatrixClass<int> matrixSum = matrix1 + matrix2; MatrixClass<int> matrixSub = matrix1 - matrix2; MatrixClass<int> matrixMulti = matrix1 * matrix2; }
static void Main() { MatrixClass <int> matrix1 = new MatrixClass <int>(2, 2); matrix1[0, 0] = 1; matrix1[0, 1] = 2; matrix1[1, 0] = 3; matrix1[1, 1] = 0; MatrixClass <int> matrix2 = new MatrixClass <int>(2, 2); matrix2[0, 0] = 2; matrix2[0, 1] = 2; matrix2[1, 0] = 2; matrix2[1, 1] = 2; //Problem 10 - without predefening the operaotrs +,-, * the compiler throw an error. //You can check this - comment the +,-, * methods at the MatrixClass MatrixClass <int> matrixSum = matrix1 + matrix2; MatrixClass <int> matrixSub = matrix1 - matrix2; MatrixClass <int> matrixMulti = matrix1 * matrix2; }
public override void Decrypt(StreamReader inputStream, StreamWriter outputStream) { MatrixClass matrix = new MatrixClass(key); matrix = matrix.Inverse(); int charPosition; int matrixSize = key.GetLength(0); int b; char[] temp = new char[3]; int k = 0; while ((b = inputStream.Read()) != -1) { if (k < matrixSize) { temp[k++] = (char)b; continue; } k = 0; for (int i = 0; i < matrixSize; ++i) { charPosition = 0; for (int j = 0; j < matrixSize; ++j) { charPosition += (int)matrix[j, i].Numerator * alphabet[temp[j]]; } if (template[i] == SymbolType.Character) { outputStream.Write(alphabet.Keys.ElementAt(charPosition % alphabet.Count)); } } } outputStream.Flush(); }
public void MultiplyTwoMatrisces() { int n = 4; MatrixClass <int> one = new MatrixClass <int>(n, n); MatrixClass <int> two = new MatrixClass <int>(n, n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { one[i, j] = 6; two[i, j] = 2; } } MatrixClass <int> result = one * two; int[,] matrixC = new int[, ] { { 48, 48, 48, 48 }, { 48, 48, 48, 48 }, { 48, 48, 48, 48 }, { 48, 48, 48, 48 } }; bool resultValue = true; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (matrixC[i, j] != result[i, j]) { resultValue = false; break; } } } Assert.AreEqual(resultValue, true); }
public static void Main() { MatrixClass matrix1 = new MatrixClass(2, 2); matrix1[0, 0] = 1; matrix1[0, 1] = -2; matrix1[1, 0] = 3; matrix1[1, 1] = 3; MatrixClass matrix2 = new MatrixClass(2, 2); matrix2[0, 0] = 4; matrix2[0, 1] = 4; matrix2[1, 0] = 5; matrix2[1, 1] = 5; MatrixClass resultSum = matrix1 + matrix2; Console.WriteLine("Adding the two matrix:"); Console.WriteLine(resultSum.ToString()); MatrixClass resultExtraction = matrix1 - matrix2; Console.WriteLine("Extract Matrix2 from Matrix1:"); Console.WriteLine(resultExtraction.ToString()); MatrixClass resultMultiplication = matrix1 * matrix2; Console.WriteLine("Result of multiplication of Matrix1 and Matrix2:"); Console.WriteLine(resultMultiplication.ToString()); }
public static MatrixClass operator *(MatrixClass first, MatrixClass second) { //Making sure first matrix columns and second matrix rows have the same length. Pop-up will notify in case of mismatch. Debug.Assert(first.columns == second.rows); { MatrixClass third = new MatrixClass(first.rows, second.columns); for (int rows = 0; rows < third.columns; rows++) { for (int columns = 0; columns < first.rows; columns++) { int value = 0; for (int modifier = 0; modifier < second.rows; modifier++) { value += first[columns, modifier] * second[modifier, rows]; } third[columns, rows] = value; } } return third; } }
public void CheckingTypeOfMatrices() { MatrixClass <char> genericMatrix = new MatrixClass <char>(5, 5); MatrixClass <bool> genericMatrix2 = new MatrixClass <bool>(5, 5); }
static void Main(string[] args) { int choise; while (true) { Console.WriteLine("1. Даны целые положительные числа M и N. Сформировать целочисленную матрицу"); Console.WriteLine("размера M × N, у которой все элементы I - й строки имеют значение 10·I(I = 1, …, M).\n"); Console.WriteLine("2. Дана целочисленная матрица размера M × N. Найти номер последнего из ее столбцов,"); Console.WriteLine("содержащих равное количество положительных и отрицательных элементов (нулевые элементы "); Console.WriteLine("матрицы не учитываются). Если таких столбцов нет, то вывести 0.\n"); Console.WriteLine("3. Дан массив A размера N и целое число K (0 < K <N). Преобразовать массив, увеличив"); Console.WriteLine("каждый его элемент на исходное значение элемента AK-1*2.\n"); Console.WriteLine("4. Дана матрица a размером n x n, заполненная неотрицательными целыми числами. "); Console.WriteLine("Расстояние между двумя элементами aij и apq определено как |i−p|+|j−q|. Требуется заменить каждый "); Console.WriteLine("нулевой элемент матрицы ближайшим ненулевым. Если есть две или больше ближайших ненулевых "); Console.WriteLine("ячейки, нуль должен быть оставлен.\n"); Console.WriteLine("5. Дан ступенчатый массив (двумерный массив). Каждая ячейка имеет вес указанный в "); Console.WriteLine("матрице, необходимо найти кратчайший маршрут из правого верхнего в левый нижний.\n"); Console.WriteLine("6. Выход"); choise = Convert.ToInt32(Console.ReadLine()); switch (choise) { case 1: int n, m; Console.WriteLine("Введите размерность (n-стрки, m-столбцы)"); n = Convert.ToInt32(Console.ReadLine()); m = Convert.ToInt32(Console.ReadLine()); MatrixClass MyMatrix1 = new MatrixClass(n, m); MyMatrix1.Task1(); break; case 2: Console.WriteLine("Введите размерность (n-стрки, m-столбцы)"); n = Convert.ToInt32(Console.ReadLine()); m = Convert.ToInt32(Console.ReadLine()); MatrixClass MyMatrix2 = new MatrixClass(n, m); int ans = MyMatrix2.Task2(); if (ans > 0) { Console.WriteLine($"Номер столбца: {ans+1}"); } else { Console.WriteLine("Такого столбца нет"); } break; case 3: int k; Console.WriteLine("Введите размерность (n-столбцы, 0<k<n)"); n = Convert.ToInt32(Console.ReadLine()); k = Convert.ToInt32(Console.ReadLine()); MatrixClass MyMatrix3 = new MatrixClass(n, 0, k); MyMatrix3.Task3(); break; case 4: Console.WriteLine("Введите размерность (n-стрки, m-столбцы)"); n = Convert.ToInt32(Console.ReadLine()); m = Convert.ToInt32(Console.ReadLine()); MatrixClass MyMatrix4 = new MatrixClass(n, m); MyMatrix4.Task4(); break; case 5: /*Console.WriteLine("Введите размерность (n-стрки, m-столбцы)"); * n = Convert.ToInt32(Console.ReadLine()); * m = Convert.ToInt32(Console.ReadLine());*/ MatrixClass MyMatrix5 = new MatrixClass(""); MyMatrix5.Task5(); break; case 6: Environment.Exit(0); break; } } }
public void NegativeN() { int row = -3, col = 2; MatrixClass <int> one = new MatrixClass <int>(row, col); MatrixClass <int> two = new MatrixClass <int>(row, col); }
static void Main() { MatrixClass first = new MatrixClass(5, 5); MatrixClass second = new MatrixClass(5, 5); Random random = new Random(); for (int rows = 0; rows < first.rows; rows++) { for (int columns = 0; columns < first.columns; columns++) { first[rows, columns] = random.Next(-150, 150); second[rows, columns] = random.Next(-150, 150); } } Console.WriteLine("Matrix 1:"); Console.WriteLine(first); Console.WriteLine("Matrix 2:"); Console.WriteLine(second); Console.WriteLine("Result after addition:"); Console.WriteLine(first + second); Console.WriteLine("Result upon substraction:"); Console.WriteLine(first - second); Console.WriteLine("Matrices multiplication:"); Console.WriteLine(first * second); }