using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; class Matrix { private T[,] matrix; private int rows; private int cols; private const int defaultCap = 4; public Matrix() : this(defaultCap, defaultCap) { } public Matrix(int rows, int cols) { if ((typeof(T) == typeof(sbyte)) || (typeof(T) == typeof(byte)) || (typeof(T) == typeof(short)) || (typeof(T) == typeof(ushort)) || (typeof(T) == typeof(int)) || (typeof(T) == typeof(uint)) || (typeof(T) == typeof(long)) || (typeof(T) == typeof(float)) || (typeof(T) == typeof(double)) || (typeof(T) == typeof(decimal))) { this.rows = rows; this.cols = cols; matrix = new T[rows, cols]; } else { throw new InvalidCastException(); } } public T this[int row, int col] { get { if ((row <= 0 || row >= this.rows) || (col <= 0 || col >= this.cols)) { throw new IndexOutOfRangeException(); } return matrix[row, col]; } set { if ((row <= 0 || row >= this.rows) || (col <= 0 || col >= this.cols)) { throw new IndexOutOfRangeException(); } matrix[row, col] = value; } } //addition public static Matrix operator +(Matrix A, Matrix B) { if (A.rows != B.rows || A.cols != B.cols ) { throw new InvalidOperationException(); } Matrix C = new Matrix(A.rows, A.cols); for (int r = 0; r < A.rows; r++) { for (int c = 0; c < A.cols; c++) { dynamic dyn1 = A.cols; dynamic dyn2 = B.cols; C[r, c] = (T) (dyn1 + dyn2); } } return C; } // subtr public static Matrix operator -(Matrix A, Matrix B) { if (A.rows != B.rows || A.cols != B.cols ) { throw new InvalidOperationException(); } Matrix C = new Matrix(A.rows, A.cols); for (int r = 0; r < A.rows; r++) { for (int c = 0; c < A.cols; c++) { dynamic dyn1 = A.cols; dynamic dyn2 = B.cols; C[r, c] = (T) (dyn1 - dyn2); } } return C; } // multiply public static Matrix operator *(Matrix A, Matrix B) { if (A.cols != B.rows) { throw new InvalidOperationException(); } Matrix C = new Matrix(A.rows, B.cols); for (int row = 0; row < A.rows; row++) { for (int col = 0; col < B.cols; col++) { for (int i = 0; i < A.cols; i++) { dynamic dyn1 = A[row, i]; dynamic dyn2 = B[i, col]; C[row,col] = (T) (dyn1 * dyn2); } } } return C; } public static bool operator true(Matrix M){ for (int i = 0; i < M.rows; i++) { for (int j = 0; j < M.cols; j++) { dynamic num = M[i, j]; if (num != 0) { return true; } } } return false; } public static bool operator false(Matrix M) { for (int i = 0; i < M.rows; i++) { for (int j = 0; j < M.cols; j++) { dynamic num = M[i, j]; if (num != 0) { return false; } } } return true; } }