Question
Your goal for this breakout is to create an easy-to-use Matrix class in C++ that makes use of dynamic memory allocation and includes basic matrix
Your goal for this breakout is to create an easy-to-use Matrix class in C++ that makes use of dynamic memory allocation and includes basic matrix operations both in the form of regular functions and via operator overloading.
Here is the basic prototype for the Matrix class (you may need to add more to it to support some of the additional features listed further down in this document):
typedef unsigned int uint; typedef initializer_list> i_list;
class Matrix { public:
Matrix(uint rows, uint cols); Matrix(const i_list & list); Matrix(const Matrix & m); ~Matrix();
Matrix add(double s) const; Matrix add(const Matrix & m) const;
Matrix subtract(double s) const; Matrix subtract(const Matrix & m) const;
Matrix multiply(double s) const; Matrix multiply(const Matrix & m) const;
Matrix divide(double s) const; Matrix t() const;
const uint numRows() const; const uint numCols() const;
double & at(uint row, uint col); const double & at (uint row, uint col) const; // get element at row,col (when using a const object)
}; // Matrix
// constructor (all elements initialized to 0) // constructor (using initializer list) // copy constructor // destructor
// add scalar to this matrix // add this matrix and another matrix
// subtract scalar from this matrix // subtract another matrix from this matrix
// multiply this matrix by a scaler // multiply this matrix by another matrix
// divide this matrix by a scaler // transpose of this matrix
// returns the number of rows // returns the number of cols
// get/set element at row,col
Important Class Details
Your Matrix implementation will contain elements of type double. In the prototype presented above, the term scaler refers to a regular number. For example, if you add a scalar to a matrix, then each element in the matrix gets that number added to it. Contrast this with the member functions that take a Matrix as their parameter. These functions represent regular matrix operations. For some of these operations (e.g., multiplication, transpose, etc.), you may need to consult some sort of reference in order to recall the exact procedure/meaning behind the operation.
NOTE: You MAY assume valid input for all operations. NOTE: You MAY NOT use library classes such as std::array, std::vector, std::list, etc. for this project. You must
implement your Matrix class internally using a dynamically allocated array. Example Usage 1
Matrix a(2, 2); a.at(0, 0) = 1; // [ 1, 2 ] a.at(0, 1) = 2; // [ 1, 3 ] a.at(1, 0) = 1; a.at(1, 1) = 3;
Matrix b(2, 1); b.at(0, 0) = 3; // [ 3 ] b.at(1, 0) = 2; // [ 2 ]
Matrix c = a.multiply(b); cout<<"["< The usage of the member function at(uint, uint) is what facilities our ability to perform operations such as a.at(0, 0) = 1. If you implement this function carefully, then this behavior should work because the function returns a reference to an element. In order to support the constructor overload (i.e., matrix construction using an initializer list), you will need to use a standard template library (STL) class called std::initializer list1. The type signature for the << parameter representing the list should be std::initializer list Operator Overloading You will also need to overload operators in order to support the following functionality. It is up to you whether or not these should be member or non-member overloads. You should also support stream insertion (similar to overriding the toString method in Java) so that your matrices can easily be printed. You should also support matrix assignment using an initializer list (to easily overwrite existing elements) that looks like the following: { 3, 4 }}; In order to support this last operator overload (i.e., matrix assignment using an initializer list), you will need to use a standard template library (STL) class called std::initializer list. The type signature for the << parameter representing the list should be std::initializer list Additional Features In addition to the requirements for listed above, you need to make sure your Matrix class supports the following features: Overloaded Function Call Operator (operator()(uint row, uint col)): After creating a (non-dynamically allocated) Matrix object, the user should be able to access the elements using the function call operator (as an alternative to using the at function): Overloaded Copy Assignment Operator (operator=(const Matrix &)): You should have already overloaded the assignment operator to take in a special kind of initializer list. Now you need to provide an additional overload that supports copy assignment. This will make your Matrix class more consistent since copy assignment parallels copy construction. Here is an example: Overloaded Non-Member Arithmetic Operators for Scalars: You should have already created overloads to support the basic arithmetic operations where the right-hand-side of an operation is a scaler value. Now you need to implement operator overloads so that scalers can be used on the left-hand-side of an operation. Here is an example showing the operators that you need to support: Overloaded Unary Minus Operator (operator-()): You need to support negating your Matrix objects: << "[ " << c.at(1, 0) << " ]" << endl; // [ 9 ]
Matrix d = {{1, 2}, // this will implicitly call the overloaded constructor {3, 4}}; // that takes an initializer list
// assume we have two matrices of appropriate size already set up Matrix a; Matrix b;
// after providing the overloads, you should be able to do any of the following operations // using regular operators instead of the member functions Matrix c0 = a + 5.2; Matrix c1 = a + a; // NOTE: these examples actually end up calling the copy constructor Matrix c2 = a - 3.5; // e.g., this line is the same as Matrix c2(a - 3.5);
Matrix c3 = b - b; Matrix c4 = a * 2.1; Matrix c5 = a * b; Matrix c6 = a / 2.0;
cout << a << endl; // example output: [ 1, 2 ] // [ 1, 3 ]
Matrix d(2, 2); d = {{ 1, 2 },
Matrix a(1, 1); a(0, 0) = 5; cout << a(0,0) << endl;
Matrix a(1, 1); a(0, 0) = 5;
Matrix b(1, 1); b = a; // copy assignment
Matrix a = {{1, 2}, {3, 4}};
Matrix b = 4.0 + a; // [ 5, 6 ] // [ 7, 8 ]
Matrix c = 4.0 - a; // [ 3, 2 ] // [ 1, 0 ]
Matrix d = 2.0 * a; // [ 2, 4 ] // [ 6, 8 ]
Matrix e = 12.0 / a; // [ 12, 6 ] [ 4, 3 ]
Matrix a = {{1, 2}}; cout << -a << endl; // [ -1, -2 ]
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started