#include #include #include class Matrix { private: int rowSize, columnSize; int *content; friend std::ostream &operator<<(std::ostream &output, Matrix &mat); friend std::istream &operator>>(std::istream &input, Matrix &mat); friend Matrix& operator*(int lambda, Matrix& mat); public: Matrix(); Matrix(int newRowSize, int newColumSize); Matrix(Matrix &otherMat); ~Matrix(); Matrix &operator*(Matrix &otherMatrix); Matrix &operator*(int lambda); int &operator()(int row, int column); Matrix &operator=(Matrix &otherMatrix); Matrix &operator+(Matrix &otherMatrix); bool operator==(Matrix &otherMatrix); Matrix &operator-(Matrix &otherMatrix); int getRowSize(); int getColumnSize(); Matrix &transpose(); }; Matrix::Matrix() { this->rowSize = 0; this->columnSize = 0; this->content = NULL; } Matrix::Matrix(int newRowSize, int newColumnSize) { this->rowSize = newRowSize; this->columnSize = newColumnSize; this->content = new int[newRowSize * newColumnSize]; for (int i = 0; i < newRowSize * newColumnSize; i++) { this->content[i] = 0; } } Matrix::Matrix(Matrix &otherMat) { this->rowSize = otherMat.getRowSize(); this->columnSize = otherMat.getColumnSize(); this->content = new int[this->rowSize * this->columnSize]; for (int r = 0; r < this->rowSize; r++) { for (int c = 0; c < this->columnSize; c++) { (*this)(r, c) = otherMat(r, c); } } } Matrix::~Matrix() { delete[] this->content; } int &Matrix::operator()(int row, int column) { assert(row >= 0 && row < this->rowSize && column >= 0 && column < this->columnSize); return this->content[row * this->columnSize + column]; } Matrix &Matrix::operator*(Matrix &otherMatrix) { if (this->columnSize != otherMatrix.getRowSize()) { std::cout << "The size of these two matrix does not allow " "multiplying. The matrix on the left has " << this->columnSize << " columns, whereas the matrix on the right has " << otherMatrix.getRowSize() << " rows. " << std::endl; return *(new Matrix()); } int resultRowSize = this->rowSize, resultColumnSize = otherMatrix.getColumnSize(); Matrix *resultMat = new Matrix(resultRowSize, resultColumnSize); int tmpSum; for (int resultRow = 0; resultRow < resultRowSize; resultRow++) { for (int resultColumn = 0; resultColumn < resultColumnSize; resultColumn++) { tmpSum = 0; for (int p = 0; p < this->columnSize; p++) { tmpSum += (*this)(resultRow, p) * otherMatrix(p, resultColumn); } (*resultMat)(resultRow, resultColumn) = tmpSum; } } return *resultMat; } Matrix &Matrix::operator*(int lambda) { return lambda * (*this); } Matrix &Matrix::operator=(Matrix &otherMat) { if (NULL != this->content) { delete[] this->content; } this->rowSize = otherMat.getRowSize(); this->columnSize = otherMat.getColumnSize(); this->content = new int[this->rowSize * this->columnSize]; for (int r = 0; r < this->rowSize; r++) { for (int c = 0; c < this->columnSize; c++) { (*this)(r, c) = otherMat(r, c); } } return (*this); } Matrix &Matrix::operator+(Matrix &otherMat) { if (this->rowSize != otherMat.getRowSize() || this->columnSize != otherMat.getColumnSize()) { std::cout << "The size of these two does not allow adding. The matrix " "on the left is " << this->rowSize << "x" << this->columnSize << " whereas the matrix on the right is " << otherMat.getRowSize() << "x" << otherMat.getColumnSize() << "." << std::endl; return *(new Matrix()); } Matrix* result = new Matrix(*this); for (int r = 0; r < this->rowSize; r++) { for (int c = 0; c < this->columnSize; c++) { (*result)(r, c) += otherMat(r, c); } } return (*result); } bool Matrix::operator==(Matrix &otherMat) { if (this->rowSize != otherMat.getRowSize() || this->columnSize != otherMat.getColumnSize()) { return false; } for (int r = 0; r < this->rowSize; r++) { for (int c = 0; c < this->columnSize; c++) { if ((*this)(r, c) != otherMat(r, c)) { return false; } } } return true; } int Matrix::getRowSize() { return this->rowSize; } int Matrix::getColumnSize() { return this->columnSize; } Matrix &Matrix::transpose() { Matrix *result = new Matrix(this->columnSize, this->rowSize); for (int r = 0; r < this->rowSize; r++) { for (int c = 0; c < this->columnSize; c++) { (*result)(c, r) = (*this)(r, c); } } return (*result); } std::ostream &operator<<(std::ostream &output, Matrix &mat) { output << "┌"; for (int i = 0; i < mat.columnSize * 5 + 1; i++) { output << " "; } output << "┐" << std::endl; for (int r = 0; r < mat.rowSize; r++) { output << "│ "; for (int c = 0; c < mat.columnSize; c++) { output << std::setw(4) << std::setfill(' ') << mat(r, c) << " "; } output << "│" << std::endl; } output << "└"; for (int i = 0; i < mat.columnSize * 5 + 1; i++) { output << " "; } output << "┘" << std::endl; return output; } std::istream &operator>>(std::istream &input, Matrix &mat) { std::cout << "Start input a " << mat.rowSize << "x" << mat.columnSize << " matrix:" << std::endl; for (int r = 0; r < mat.rowSize; r++) { for (int c = 0; c < mat.columnSize; c++) { std::cout << r << ", " << c << " (original: " << mat(r, c) << "): "; input >> mat(r, c); } } return input; } Matrix& operator*(int lambda, Matrix& mat) { Matrix* result = new Matrix(mat); for (int r = 0; r < mat.getRowSize(); r++) { for (int c = 0; c < mat.getColumnSize(); c++) { (*result)(r, c) *= lambda; } } return (*result); } Matrix &Matrix::operator-(Matrix &otherMatrix) { return (*this) + (-1)*otherMatrix; } int main() { int rowSize, columnSize; std::cout << "Input the size of the matrix: m x n" << std::endl; std::cin >> rowSize >> columnSize; Matrix a(rowSize, columnSize); Matrix b; std::cout << a << b; std::cin >> a; b = a.transpose(); Matrix c = a * b; // clang-format off std::cout << "a =\n" << a << "b =\n" << b << "a - a*3\n" << a - a*3 << "a * 3 =\n" << a * 3 << "a * b =\n"<< a * b << "a * aT =\n" << a * a.transpose() << "a * a =\n" << a * a << "a * b == c: " << (a * b == c) << std::endl << "a == c: " << (a == c) << std::endl; // clang-format on return 0; }