static void Main() { // You are free to change the sizes of the matrices. That is just example to preview how it works :) MatrixT <int> matrix1 = new MatrixT <int>(3, 3); FillTheMatrix(matrix1); Console.WriteLine(Environment.NewLine); MatrixT <int> matrix2 = new MatrixT <int>(3, 3); FillTheMatrix(matrix2); Console.WriteLine(Environment.NewLine); // You can change the names,sizes and actions of the matrices.This is just example to preview how it works MatrixT <int> sumOfMatrices = matrix1 + matrix2; MatrixT <int> substractMatrices = matrix1 - matrix2; MatrixT <int> multipliedMatrices = matrix1 * matrix2; //Console.Clear(); Console.WriteLine("\nThe new summed matrix is:\n"); Console.WriteLine(sumOfMatrices.ToString()); Console.WriteLine("\nThe new substracted matrix is:\n"); Console.WriteLine(substractMatrices.ToString()); Console.WriteLine("\nThe new multiplied matrix is:\n"); Console.WriteLine(multipliedMatrices.ToString()); }
public static MatrixT <int> FillTheMatrix(MatrixT <int> matrix) { for (int row = 0; row < matrix.Rows; row++) { for (int col = 0; col < matrix.Cols; col++) { Console.Write("matrix1 index[{0},{1}] = ", row, col); matrix[row, col] = int.Parse(Console.ReadLine()); } } return(matrix); }
// Create operator ' - ' public static MatrixT <T> operator -(MatrixT <T> matrixOne, MatrixT <T> matrixTwo) { MatrixT <T> resultMatrix = new MatrixT <T>(matrixOne.Rows, matrixOne.Cols); if (matrixOne.Rows == matrixTwo.Rows && matrixOne.Cols == matrixTwo.Cols) { for (int rows = 0; rows < matrixOne.Rows; rows++) { for (int cols = 0; cols < matrixOne.Cols; cols++) { resultMatrix[rows, cols] = (dynamic)matrixOne[rows, cols] - matrixTwo[rows, cols]; } } } else { throw new ArgumentException("\nThe matrices have different sizes!\nThey must be equal."); } return(resultMatrix); }
//Create operator for multiplying public static MatrixT <T> operator *(MatrixT <T> matrixOne, MatrixT <T> matrixTwo) { MatrixT <T> resultMatrix = new MatrixT <T>(matrixOne.Rows, matrixTwo.Cols); if (matrixOne.Cols != matrixTwo.Rows) { throw new ArgumentException("Imposibble operation:\nThe colums of first matrice are not equal to rows of second matrice!"); } else if (matrixOne.Cols == matrixTwo.Rows) { for (int rows = 0; rows < resultMatrix.Rows; rows++) { for (int cols = 0; cols < resultMatrix.Cols; cols++) { for (int i = 0; i < matrixOne.Cols; i++) { resultMatrix[rows, cols] += (dynamic)matrixOne[rows, i] * matrixTwo[i, cols]; } } } } return(resultMatrix); }