Your goal for this breakout is to create an easytouse 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):
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 scaler 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 c = a.multiply(b); cout<<\"[\"<<c.at(0,0)<<\"]\"<<endl //[7]
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<std::initializer list<double>> or simply i list if you use the provided typedef
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<std::initializer list<double>> or simply i list if you use the provided typedef. It is preferred that you specify the parameter as a const i list & in order to avoid any unnecessary copying.
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 Scalers: 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:
You should organize your project into the following files:
Matrix.h: This file should include the class prototype presented above as well as the prototypes for operator overloads that you implement. You MAY NOT modify the function prototypes that are included in the Matrix class prototype. However, you may add additional function prototypes and variables to the class prototype as needed. Make sure that this header file also includes a header guard (i.e., the #ifndef macro, etc.).
Matrix.cpp: This file should contain the implementation of your class’s functions as well as the implementation of any operator overloads that you implement.
p1.cpp: This file should contain a small/moderately sized driver that demonstrates the full range of functionality of your Matrix class.
Additionally, make sure that you adhere to the following:
• All functions must be documented using Javadoc-style comments. Use inline documentation, as needed, to explain ambiguous or tricky parts of your code.
Since this project makes use of dynamic memory allocation, you are expected to ensure that your Matrix implementation doesn’t result in any memory leaks. We will test for memory leaks using the valgrind utility. For this project, having no memory leaks will be one or more of the test cases.
Solution
Matrix.h
class Matrix{
int m,n;
int **mat;
public:
Matrix(int rows,int cols);
Matrix(const Matrix &m); //Copy constructor
~Matrix();
Matrix add(int& s) ;
Matrix add(Matrix & m);
Matrix subtract(int& s) ;
Matrix subtract( Matrix & m);
Matrix multiply(int& s) ;
Matrix multiply( Matrix & m);
Matrix divide(int& s) ;
Matrix t() ; //transpose
const int getRows() const{return m;}
const int getCols() const{return n;}
int & at(int r,int c) ;
const int &at (int r ,int col) const;
//Operators overloaded
friend ostream& operator <<(ostream& out,const Matrix &m);
friend istream& operator >>(istream& in, Matrix &m);
Matrix operator+(int m);
Matrix operator+(Matrix &m);
Matrix operator-(int m);
Matrix operator-(Matrix &m);
Matrix operator* (int m);
Matrix operator* (Matrix &m);
Matrix operator/ (int m);
Matrix& operator =(const Matrix &m);
};
Matrix.cpp
#include<iostream>
using namespace std;
#include \"Matrix.h\"
//This is also a initializer list
Matrix::Matrix(int r,int c):m(r),n(c){
mat=new int*[m];
for(int i=0;i<m;i++)
mat[i]=new int[n];
}
Matrix::Matrix(const Matrix& ma){
m=ma.getRows();
n=ma.getCols();
mat=new int*[m];
for(int i=0;i<m;i++)
mat[i]=new int[n];
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
mat[i][j]=ma.at(i,j);
}
Matrix::~Matrix()
{
delete [] mat;
}
Matrix Matrix::add(int& s)
{
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
mat[i][j]+=s;
return *this;
}
Matrix Matrix::add(Matrix &ma){
int r=ma.getRows();
int c=ma.getCols();
if(r!=m || c!=n){
cout<<\"Unable to add matrices of different size\"<<endl;
return *this;
}
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
mat[i][j]+=ma.at(i,j);
return *this;
}
Matrix Matrix::subtract(int& s)
{
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
mat[i][j]-=s;
return *this;
}
Matrix Matrix::subtract(Matrix &ma){
int r=ma.getRows();
int c=ma.getCols();
if(r!=m || c!=n){
cout<<\"Unable to subtract matrices of different size\"<<endl;
return *this;
}
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
mat[i][j]-=ma.at(i,j);
return *this;
}
Matrix Matrix::multiply(int& s)
{
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
mat[i][j]*=s;
return *this;
}
Matrix Matrix::multiply(Matrix &ma){
int r=ma.getRows();
int c=ma.getCols();
if(n!=r){
cout<<\"Unable to multiply matrices of incompatible size\"<<endl;
return *this;
}
Matrix tmp(m,c);
for(int i=0;i<m;i++)
for(int j=0;j<c;j++)
for(int k=0;k<n;k++)
tmp.at(i,j)+= (mat[i][k]*ma.at(k,j));
delete[] mat;
*this=tmp;
this->n=c;
return *this;
}
Matrix Matrix::divide(int& s)
{
if(s==0){
cout<<\"Can\'t divide bye 0\"<<endl;
return *this;
}
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
mat[i][j]/=s;
return *this;
}
Matrix Matrix::t() //transpose
{
Matrix tmp(n,m);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
tmp.at(i,j)=mat[j][i];
*this=tmp;
return *this;
}
Matrix Matrix::operator +(int s){
return add(s);
}
Matrix Matrix::operator +(Matrix& s){
return add(s);
}
Matrix Matrix::operator -(int s){
return subtract(s);
}
Matrix Matrix::operator -(Matrix& s){
return subtract(s);
}
Matrix Matrix::operator *(int s){
return multiply(s);
}
Matrix Matrix::operator *(Matrix& s){
return multiply(s);
}
Matrix Matrix::operator /(int s){
return divide(s);
}
ostream & operator <<(ostream &out, const Matrix & ma){
int m=ma.m;
int n=ma.n;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++){
cout<<ma.mat[i][j]<<\" \";
}
cout<<endl;
}
}
istream & operator>>(istream & in,Matrix &ma)
{
cout<<\"enter \"<<(ma.m * ma.n)<<\" values separated by space\"<<endl;
for(int i=0;i<ma.m;i++)
for(int j=0;j<ma.n;j++)
in>>ma.mat[i][j];
}
Matrix& Matrix::operator=(const Matrix & ma){
int r=ma.getRows();
int c=ma.getCols();
this->m=r;
this->n=c;
mat=new int*[m];
for(int i=0;i<m;i++)
mat[i]=new int[n];
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
mat[i][j]=ma.at(i,j);
return *this;
}
int & Matrix::at(int r,int c)
{
return mat[r][c];
}
const int &Matrix::at(int r,int c) const{
return mat[r][c];
}
main.cpp
#include<iostream>
using namespace std;
#include \"Matrix.h\"
int main(void)
{
Matrix m1(2,2),m2(2,2);
cin>>m1;
cin>>m2;
cout<<m1;
cout<<m2;
m1=m1+m2;
m1=m1*2;
cout<<m1;
m1=m1*m2;
cout<<\"After matrix multiplaction:\"<<endl;
cout<<\"Matrix 1:\"<<endl;
cout<<m1;
cout<<\"Matrix 2:\"<<endl;
cout<<m2;
cout<<\"Resultant Matrix after multiplication:\"<<endl;
cout<<m1;
m1.t();
cout<<\"Transposed matrix:\"<<endl;
cout<<m1;
return 0;
}
Output:
enter 4 values separated by space
1 2 3 4
enter 4 values separated by space
1 2 3 4
1 2
3 4
1 2
3 4
4 8
12 16
After matrix multiplaction:
Matrix 1:
28 40
60 88
Matrix 2:
1 2
3 4
Resultant Matrix after multiplication:
28 40
60 88
Transposed matrix:
28 60
40 88







