i need your help
-
I have a project and this is what the project is about :
Implement a template library handling matrices.
the library must offer operation like:
• submatrix: return a matrix that contains a contiguous subrange of rows and columns of the original matrix
• transpose: return a matrix with rows and columns inverted
The matrices returned by such operators must share the data with the original matrix, but direct copy of the matrices and vectors must result in a deep copy (sharing a matrix can be obtained by taking a submatrix extending the whole row and column ranges).
Create iterators for the matrix traversing the whole data in any order.
Provide matrix addition and (row-by-column) multiplication as overloaded operatorsso i did everything and i am stuck in how to implment operator overloaded for multiplication and addition, i mean should i creat a class for this operation or should i implment them in the main and how to do this.
this is the matrix interface:
#include #include #include using namespace std;
// creating matrux interface
template < typename T>
class matrix_interface {public:
// virtual keyword allows derived classes to replace the implementation provided by the base class
virtual int getCol() const = 0;
virtual int getRow() const = 0;
virtual T get(int row, int col) const = 0;
virtual T& get_ref(int row, int col) = 0;
virtual ~matrix_interface() {};};
//Base
template < typename T>
class standard_matrix : public matrix_interface {private:
int row,col; vector data;
public:
int getRow() const override { return row; } int getCol() const override { return col; } standard\_matrix(const vector &data, int row, int col) : row(row), col(col), data(data) { if (data.size() < row\*col) throw ("the vector is invalid"); if (row < 1 || col < 1 ){ throw ("the dimensions are invalid"); } } T &getRef(int r, int c) override { if (r < 1 || r > row) { throw "Row is off limit"; } else if (c < 1 || c > col) { throw "Column is off limits"; } else{ T& t= data.at(col \* (r - 1) + c - 1); return t; } } T get(int r, int c) const override { if (r < 1 || r > row) { throw "Row is off limit"; } else if (c < 1 || c > col) {
-
I have a project and this is what the project is about :
Implement a template library handling matrices.
the library must offer operation like:
• submatrix: return a matrix that contains a contiguous subrange of rows and columns of the original matrix
• transpose: return a matrix with rows and columns inverted
The matrices returned by such operators must share the data with the original matrix, but direct copy of the matrices and vectors must result in a deep copy (sharing a matrix can be obtained by taking a submatrix extending the whole row and column ranges).
Create iterators for the matrix traversing the whole data in any order.
Provide matrix addition and (row-by-column) multiplication as overloaded operatorsso i did everything and i am stuck in how to implment operator overloaded for multiplication and addition, i mean should i creat a class for this operation or should i implment them in the main and how to do this.
this is the matrix interface:
#include #include #include using namespace std;
// creating matrux interface
template < typename T>
class matrix_interface {public:
// virtual keyword allows derived classes to replace the implementation provided by the base class
virtual int getCol() const = 0;
virtual int getRow() const = 0;
virtual T get(int row, int col) const = 0;
virtual T& get_ref(int row, int col) = 0;
virtual ~matrix_interface() {};};
//Base
template < typename T>
class standard_matrix : public matrix_interface {private:
int row,col; vector data;
public:
int getRow() const override { return row; } int getCol() const override { return col; } standard\_matrix(const vector &data, int row, int col) : row(row), col(col), data(data) { if (data.size() < row\*col) throw ("the vector is invalid"); if (row < 1 || col < 1 ){ throw ("the dimensions are invalid"); } } T &getRef(int r, int c) override { if (r < 1 || r > row) { throw "Row is off limit"; } else if (c < 1 || c > col) { throw "Column is off limits"; } else{ T& t= data.at(col \* (r - 1) + c - 1); return t; } } T get(int r, int c) const override { if (r < 1 || r > row) { throw "Row is off limit"; } else if (c < 1 || c > col) {
Member 14982052 wrote:
i am stuck in how to implment operator overloaded for multiplication and addition, i mean should i creat a class for this operation or should i implment them in the main and how to do this.
You should (have to) implement them in your matrix class. See the example: [beginner - C++ operator overloading for matrix operations - follow-up - Code Review Stack Exchange](https://codereview.stackexchange.com/questions/149669/c-operator-overloading-for-matrix-operations-follow-up)