Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- class Matrix {
- public:
- Matrix(size_t RowCount, size_t ColumnCount);
- Matrix(Matrix && rhs);
- ~Matrix();
- Matrix& operator=(Matrix && rhs);
- friend Matrix operator*(const Matrix &lhs, const Matrix &rhs);
- // Matrix& operator*=(const Matrix &rhs); // nicht möglich?
- private:
- void alloc_space();
- const size_t rowCount;
- const size_t columnCount;
- double *elements;
- };
- Matrix::Matrix(size_t RowCount, size_t ColumnCount)
- : rowCount(RowCount), columnCount(ColumnCount)
- {
- alloc_space();
- }
- Matrix::~Matrix()
- {
- delete[] elements;
- }
- void Matrix::alloc_space()
- {
- elements = new double[rowCount * columnCount];
- }
- Matrix& Matrix::operator=(Matrix && rhs)
- {
- delete[] elements;
- elements = rhs.elements;
- rowCount = rhs.rowCount;
- columnCount = rhs.columnCount;
- delete[] rhs.elements;
- rhs.elements = nullptr;
- rhs.rowCount = 0;
- rhs.columnCount = 0;
- return *this;
- }
- Matrix operator*(const Matrix &lhs, const Matrix &rhs)
- {
- Matrix prod{lhs.rowCount, rhs.columnCount};
- if (lhs.columnCount != rhs.rowCount) {
- // throw math error
- }
- for (size_t m = 0; m < prod.rowCount; m++) {
- for (size_t n = 0; n < prod.columnCount; n++) {
- prod.elements[m * prod.columnCount + n] = 0;
- for (size_t k = 0; k < lhs.columnCount; k++) {
- prod.elements[m * prod.columnCount + n] +=
- lhs.elements[m * lhs.columnCount + k] * rhs.elements[k * rhs.rowCount + n];
- }
- }
- }
- return std::move(prod);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement