#ifndef DESCSS_Matrix_h #define DESCSS_Matrix_h template class matrix { public: matrix(int n, int m); matrix(const matrix&); ~matrix(); void Input(T* p); void Output(); int rows() const { return n; }; int cols() const { return m; }; T& operator()(int i, int j) const; matrix& operator=(const matrix&); matrix operator+() const; matrix operator+(const matrix&) const; matrix operator*(const matrix&) const; matrix operator+(const T&) const; matrix operator*(const T&) const; matrix operator/(const T&) const; protected: int n, m; T** element; }; template matrix::matrix(int n, int m) { if (m < 0 || n < 0) std::cout << "Rows and Cols must be >= 0 " << std::endl; if ((m == 0 || n == 0) && (m != 0 || n != 0)) std::cout << "Either both or neither rows and columns should be zero " << std::endl; this->m = m, this->n = n; element = new T*[n]; for (int i = 0; i < n; i++) element[i] = new T[m]; } template matrix::matrix(const matrix& A) { size_t number = A.n * A.m; element = new T*[n]; for (int i = 0; i < n; i++) element[i] = new T[m]; std::copy(A.element, A.element + number, element); } template matrix::~matrix() {} template matrix& matrix::operator=(const matrix& A) { if (this != &A) { delete[] element; size_t number = A.n * A.m; element = new T*[n]; for (int i = 0; i < n; i++) element[i] = new T[m]; std::copy(A.element, A.element + number, element); } return *this; } template void matrix::Input(T* p) { for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) this->element[i][j] = *(p++); } template void matrix::Output() { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) std::cout << element[i][j] << " "; std::cout << std::endl; } } template T& matrix::operator()(int i, int j) const { if (i < 1 || i > n || j < 1 || j > m) std::cout << "Matrix Index Out Of Bounds " << std::endl; return element[i - 1][j - 1]; } template matrix matrix::operator+(const matrix& A) const { if (n != A.n || m != A.m) std::cout << "Matrix Size is Out of batch " << std::endl; matrix w(n, m); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) w.element[i][j] = element[i][j] + A.element[i][j]; return w; } template matrix matrix::operator+(const T& C) const { matrix w(n, m); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) w.element[i][j] = element[i][j] + C; return w; } template matrix matrix::operator*(const matrix& A) const { if (m != A.n) std::cout << "Matrix Style is Out of batch " << std::endl; matrix w(n, m); for (int i = 0; i < n; i++) for (int j = 0; j < A.m; j++) { w.element[i][j] = 0; for (int k = 0; k < m; k++) w.element[i][j] += element[i][k] * A.element[k][j]; } return w; } template matrix matrix::operator*(const T& C) const { matrix w(n, m); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) w.element[i][j] = element[i][j] * C; return w; } template matrix matrix::operator/(const T& C) const { matrix w(n, m); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) w.element[i][j] = element[i][j] / C; return w; } #endif