C++ Matrix Class Implementation
Hey CPP Implementers,
Welcome to the my C++ blog which is made to help the community to gain expert knowledge in C++ programming language. Every beginner begins with HelloWorld program whichever language he starts to learn. This article is not for absolute beginners but those who have some little bit knowledge of C++ classes can learn what I am going to teach today here. C++ is an object oriented language which means there are objects that share some relationship among them. These objects have certain behavior which are indicated by their member functions. A C++ class is a blueprint of its object. It is not a concrete things. An object of a C++ class is an entity.
Let us simply understand a Matrix Class and see how this is implemented in C++.
If you ever used matrix in your early math, certain things may come up into your mind. You may be thinking of a rectangular shape containing numbers. You may be thinking of certain operations which you used to operate on matrices. These operations are called behavior of Matrix objects.
Here is C++ header file. This file contains declarations of different private and public data members and functions of Matrix class. To use this file you should name it as Matrix.h file. This matrix class use two dimensional arrays to store the contents of matrix. In order to handle large matrices, I have made dynamic two dimensional array so that large Matrix of any size can be handled easily.
I have included fstream since I am reading the contents of matrix from a text file.
#ifndef MYFILE_H
#define MYFILE_H
#include<fstream>
//class name
class Matrix
{
//overloading >> operator so that it can read its content from text file as a matrix
friend std::ifstream & operator>>(std::ifstream & , Matrix &);
//overloading << operator so that it can write its content into text file as a matrix
friend std::ofstream & operator<<(std::ofstream & , const Matrix &);
private:
//2 dimensional dynamic array to store Matrix contents
int ** matrix;
// To store how many rows this matrix has
int rows;
// To store how many columns this matrix has
int columns;
public:
//default constructor
Matrix();
//destructor
~Matrix();
//parameter constructor. This will generate matrix of r rows and c columns
Matrix (int r,int c);
//Copy Constructor declaration
Matrix (const Matrix &);
//function to check whether matrix is square or not
bool isSquareMatrix();
//function to check whether matrix is same by reversing its diagonals or not
bool reverseDiagonals();
//function to take transpose of a matrix
void transpose();
//This function returns the total rows of matrix
int getRows();
//This function returns the total number of columns of matrix
int getColumns();
//You can set the rows of matrix by calling this function
void setRows();
//You can set the columns of matrix by calling this function
void setColumns();
//Function to check whether Matrix is symmetric or not
bool checkSymmetric();
int isSymmetric();
//This function returns the contents of upper triangle of a matrix
void upperTriangle();
//This function returns the contents of lower triangle of a matrix
void lowerTriangle();
//This function assigns a matrix into another matrix
const Matrix & operator=(const Matrix &);
// This returns if this matrix is equal to argument matrix or not
bool operator==(const Matrix & )const;
// This returns if this matrix is not equal to argument matrix or not
bool operator!=(const Matrix & )const;
// This function adds the two matrices and returns the resultant matrix
friend Matrix operator+(const Matrix &,const Matrix & );
// This function multiplies the two matrices and returns the resultant matrix
friend Matrix operator*(const Matrix &,const Matrix & );
};
#endif
#include"myfile.h"
using namespace std;
//default constructor sets a Matrix to zero rows and zeros columns
Matrix::Matrix()
{
rows=0;
columns=0;
}
//parameteric constructor sets a Matrix given rows and columns
//declaring 2D dynamic array
Matrix::Matrix(int r,int c)
{
rows=r;
columns=c;
matrix= new int * [rows];
for(int i=0; i<rows; i++)
{
matrix[i]=new int [columns];
}
}
Matrix::Matrix(const Matrix & other)
{
rows=other.rows;
columns=other.columns;
matrix=new int * [rows];
for(int i=0; i<rows; i++)
{
matrix[i]=new int [columns];
}
for(int i=0; i<rows; i++)
{
for(int j=0; j<columns; j++)
{
matrix[i][j]=other.matrix[i][j];
}
}
}
ofstream & operator <<(ofstream & out,const Matrix & otherMatrix)
{
for(int i=0; i<otherMatrix.rows; i++)
{
for(int j=0; j<otherMatrix.columns; j++)
{
out<<otherMatrix.matrix[i][j]<<"\t";
}
out<<endl;
}
return out;
}
ifstream & operator>>(ifstream & in,Matrix & otherMatrix)
{
for(int i=0; i<otherMatrix.rows; i++)
{
for(int j=0; j<otherMatrix.columns; j++)
{
in>>otherMatrix.matrix[i][j];
}
}
return in;
}
Matrix operator+(const Matrix & otherMatrix,const Matrix & othermatrix1)
{
if(otherMatrix.rows==othermatrix1.rows && otherMatrix.columns==othermatrix1.columns)
{
int rows1=otherMatrix.rows;
int columns1=othermatrix1.columns;
Matrix tempmatrix(rows1,columns1);
for(int i=0; i<othermatrix1.rows; i++)
{
for(int j=0; j<othermatrix1.columns; j++)
{
tempmatrix.matrix[i][j]=otherMatrix.matrix[i][j] + othermatrix1.matrix[i][j];
}
}
return tempmatrix;
}
else
cout<<"Sorry !!!! Addition of these matrices is not possible "<<endl;
}
Matrix operator*(const Matrix & other,const Matrix & other1 )
{
if(other.columns==other1.rows )
{
int rows=other.rows;
int columns=other1.columns;
Matrix tempmatrix(rows,columns);
for(int i=0; i<rows; i++)
{
for(int j=0; j<columns; j++)
{
for(int k=0; k<other.columns; k++)
{
tempmatrix.matrix[i][j]=0;
tempmatrix.matrix[i][j]=tempmatrix.matrix[i][j]+other.matrix[i][k]*other1.matrix[k][j];
}
}
}
return tempmatrix;
}
else cout<<"Sorry !!!! Multiplication of tese matrices is not possible"<<endl;
}
bool Matrix::operator==(const Matrix & some)const
{
for(int i=0; i<rows; i++)
{
for(int j=0; j<columns; j++)
{
return (matrix[i][j]==some.matrix[i][j]);
}
}
}
bool Matrix::operator!=(const Matrix & some1 )const
{
for(int i=0; i<rows; i++)
{
for (int j=0; j<columns; j++)
{
return (matrix[i][j]!=some1.matrix[i][j]);
}
}
}
const Matrix & Matrix::operator=(const Matrix & someother )
{
for(int i=0; i<rows; i++)
{
for(int j=0; j<columns; j++)
{
matrix[i][j]=someother.matrix[i][j];
}
}
return *this;
}
bool Matrix::isSquareMatrix()
{
if(rows==columns)
{
return true;
}
else return -1;
}
bool Matrix::reverseDiagonals()
{
if(rows==columns)
{
for(int i=0; i<rows/2; i++)
{
int temp=0;
temp=matrix[i][i];
matrix[i][i]=matrix[rows-1-i][rows-1-i];
matrix[rows-1-i][rows-1-i]=temp;
}
for(int i=0; i<rows/2; i++)
{
int temp=0;
temp=matrix[i][rows-1-i];
matrix[i][rows-1-i]=matrix[rows-1-i][i];
matrix[rows-1-i][i]=temp;
}
}
else return -1;
}
void Matrix::transpose()
{
for(int i = 0; i < rows; i++)
{
for(int j = i; j < columns; j++)
{
int temp=0;
temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}
int Matrix::getRows()
{
return rows;
}
int Matrix::getColumns()
{
return columns;
}
void Matrix::setRows()
{
if(rows>0)
rows=rows;
else rows=1;
}
void Matrix::setColumns()
{
if(columns>0)
columns=columns;
else columns=1;
}
bool Matrix::checkSymmetric()
{
for(int i=0; i<rows/2; i++)
{
for(int j=i+1; j<columns; j++)
{
if(matrix[i][j]==matrix[j][i])
return true;
else return false;
}
}
}
void Matrix::upperTriangle()
{
if(rows==columns)
{
for(int i=0; i<rows; i++)
{
for(int j=i+1; j<columns; j++)
{
matrix[j][i]=0;
}
}
}
else cout<<"Sorry !!!! The Matrix is not square so its uppertriangle cannot be found"<<endl;
}
void Matrix::lowerTriangle()
{
if(rows==columns)
{
for(int i=0; i<rows; i++)
{
for(int j=i+1; j<columns; j++)
{
matrix[i][j]=0;
}
}
}
else cout<<"Sorry !!!! The Matrix is not square so its lower triangle cannot be found"<<endl;
}
int Matrix::isSymmetric()
{
checkSymmetric();
if(checkSymmetric()==true)
return 1;
else return 0;
}
Matrix::~Matrix()
{
getRows();
int r=0;
r=getRows();
for(int i=0; i<r; i++)
{
delete []matrix[i];
}
delete [] matrix;
}
int main()
{
ifstream infile;
infile.open("z:\\matrix.txt");
if(!infile)
{
cerr<<"File opening error"<<endl;
}
ofstream outfile;
outfile.open("z:\\out.txt");
int rows1=0;
infile>>rows1;
int columns1=0;
infile>>columns1;
Matrix M1(rows1,columns1);
infile>>M1;
int rows2=0;
int columns2=0;
infile>>rows2;
infile>>columns2;
Matrix M2(rows2,columns2);
infile>>M2;
Matrix M3(rows1,columns1);
M3= M1+ M2;
outfile<<"The sum of first two Matrices is"<<endl;
outfile<<M3<<endl;
int rows3=0;
infile>>rows3;
int columns3=0;
infile>>columns3;
Matrix M4(rows3,columns3);
infile>>M4;
int rows5;
int columns5;
infile>>rows5;
infile>>columns5;
Matrix M5(rows5,columns5);
infile>>M5;
Matrix M6(rows3,columns5);
M6=M4*M5;
outfile<<"The product of two Matrices is"<<endl;
outfile<<M6<<endl<<endl;
Matrix M7(M6);
outfile<<"copy constructor is called which saves previous Matrix"<<endl<<endl<<endl;
outfile<<M7<<endl<<endl;// Copy Constructor is called
outfile<<"Checking the equality of the first two matrix"<<endl<<endl;
if(M1== M2)
outfile<< "Matrices are equal"<<endl<<endl;
else
outfile<< "matrices are not equal"<<endl<<endl<<endl;
outfile<<"CHECKING THE SYMMETRY OF NEXT MATRIX"<<endl;
int rows6;
int columns6;
infile>>rows6;
infile>>columns6;
Matrix M8(rows6,columns6);
infile>>M8;
if(rows6==columns6)
{
M8.isSymmetric();
if(M8.isSymmetric()==1)
{
outfile<<"Matrix is Symmetric"<<endl;
}
else
{
outfile<<"Matrix is not symmetiric"<<endl;
}
}
else
{
outfile<<"Matrix is not square so its symmetry cannot be checked"<<endl<<endl;
}
int rows7=0;
int columns7=0;
infile>>rows7;
infile>>columns7;
Matrix M9(rows7,columns7);
infile>>M9;
M9.transpose();
outfile<<M9<<endl<<endl;
outfile<<"Now REVERSING THE DIOGNALS OF THE PREVIOUS MATRIX"<<endl<<endl;
M9.reverseDiagonals();
outfile<<M9<<endl<<endl;
outfile<<"NOw FINDING THE UPPER AND LOWER TRIANGLE OF THE PREVIOUS MATRIX"<<endl<<endl;
M9.lowerTriangle();
outfile<<"Lower Triangle"<<endl<<endl;
outfile<<M9<<endl<<endl;
outfile<<"UPPER Triangle and LOwer triangle"<<endl<<endl;
M9.upperTriangle();
outfile<<M9<<endl<<endl;
return 0;
}
So, this was the complete tutorial presented before you so that you can understand how you can make use of C++ to implement Matrix class. If you have any questions related to above matrix implementation then do let me know. I will try my best to resolve your queries. Cheers!
Welcome to the my C++ blog which is made to help the community to gain expert knowledge in C++ programming language. Every beginner begins with HelloWorld program whichever language he starts to learn. This article is not for absolute beginners but those who have some little bit knowledge of C++ classes can learn what I am going to teach today here. C++ is an object oriented language which means there are objects that share some relationship among them. These objects have certain behavior which are indicated by their member functions. A C++ class is a blueprint of its object. It is not a concrete things. An object of a C++ class is an entity.
Let us simply understand a Matrix Class and see how this is implemented in C++.
If you ever used matrix in your early math, certain things may come up into your mind. You may be thinking of a rectangular shape containing numbers. You may be thinking of certain operations which you used to operate on matrices. These operations are called behavior of Matrix objects.
Here is C++ header file. This file contains declarations of different private and public data members and functions of Matrix class. To use this file you should name it as Matrix.h file. This matrix class use two dimensional arrays to store the contents of matrix. In order to handle large matrices, I have made dynamic two dimensional array so that large Matrix of any size can be handled easily.
I have included fstream since I am reading the contents of matrix from a text file.
#ifndef MYFILE_H
#define MYFILE_H
#include<fstream>
//class name
class Matrix
{
//overloading >> operator so that it can read its content from text file as a matrix
friend std::ifstream & operator>>(std::ifstream & , Matrix &);
//overloading << operator so that it can write its content into text file as a matrix
friend std::ofstream & operator<<(std::ofstream & , const Matrix &);
private:
//2 dimensional dynamic array to store Matrix contents
int ** matrix;
// To store how many rows this matrix has
int rows;
// To store how many columns this matrix has
int columns;
public:
//default constructor
Matrix();
//destructor
~Matrix();
//parameter constructor. This will generate matrix of r rows and c columns
Matrix (int r,int c);
//Copy Constructor declaration
Matrix (const Matrix &);
//function to check whether matrix is square or not
bool isSquareMatrix();
//function to check whether matrix is same by reversing its diagonals or not
bool reverseDiagonals();
//function to take transpose of a matrix
void transpose();
//This function returns the total rows of matrix
int getRows();
//This function returns the total number of columns of matrix
int getColumns();
//You can set the rows of matrix by calling this function
void setRows();
//You can set the columns of matrix by calling this function
void setColumns();
//Function to check whether Matrix is symmetric or not
bool checkSymmetric();
int isSymmetric();
//This function returns the contents of upper triangle of a matrix
void upperTriangle();
//This function returns the contents of lower triangle of a matrix
void lowerTriangle();
//This function assigns a matrix into another matrix
const Matrix & operator=(const Matrix &);
// This returns if this matrix is equal to argument matrix or not
bool operator==(const Matrix & )const;
// This returns if this matrix is not equal to argument matrix or not
bool operator!=(const Matrix & )const;
// This function adds the two matrices and returns the resultant matrix
friend Matrix operator+(const Matrix &,const Matrix & );
// This function multiplies the two matrices and returns the resultant matrix
friend Matrix operator*(const Matrix &,const Matrix & );
};
#endif
HERE IS THE IMPLEMENTATION FILE
#include<iostream>
#include<fstream>#include"myfile.h"
using namespace std;
//default constructor sets a Matrix to zero rows and zeros columns
Matrix::Matrix()
{
rows=0;
columns=0;
}
//parameteric constructor sets a Matrix given rows and columns
//declaring 2D dynamic array
Matrix::Matrix(int r,int c)
{
rows=r;
columns=c;
matrix= new int * [rows];
for(int i=0; i<rows; i++)
{
matrix[i]=new int [columns];
}
}
Matrix::Matrix(const Matrix & other)
{
rows=other.rows;
columns=other.columns;
matrix=new int * [rows];
for(int i=0; i<rows; i++)
{
matrix[i]=new int [columns];
}
for(int i=0; i<rows; i++)
{
for(int j=0; j<columns; j++)
{
matrix[i][j]=other.matrix[i][j];
}
}
}
ofstream & operator <<(ofstream & out,const Matrix & otherMatrix)
{
for(int i=0; i<otherMatrix.rows; i++)
{
for(int j=0; j<otherMatrix.columns; j++)
{
out<<otherMatrix.matrix[i][j]<<"\t";
}
out<<endl;
}
return out;
}
ifstream & operator>>(ifstream & in,Matrix & otherMatrix)
{
for(int i=0; i<otherMatrix.rows; i++)
{
for(int j=0; j<otherMatrix.columns; j++)
{
in>>otherMatrix.matrix[i][j];
}
}
return in;
}
Matrix operator+(const Matrix & otherMatrix,const Matrix & othermatrix1)
{
if(otherMatrix.rows==othermatrix1.rows && otherMatrix.columns==othermatrix1.columns)
{
int rows1=otherMatrix.rows;
int columns1=othermatrix1.columns;
Matrix tempmatrix(rows1,columns1);
for(int i=0; i<othermatrix1.rows; i++)
{
for(int j=0; j<othermatrix1.columns; j++)
{
tempmatrix.matrix[i][j]=otherMatrix.matrix[i][j] + othermatrix1.matrix[i][j];
}
}
return tempmatrix;
}
else
cout<<"Sorry !!!! Addition of these matrices is not possible "<<endl;
}
Matrix operator*(const Matrix & other,const Matrix & other1 )
{
if(other.columns==other1.rows )
{
int rows=other.rows;
int columns=other1.columns;
Matrix tempmatrix(rows,columns);
for(int i=0; i<rows; i++)
{
for(int j=0; j<columns; j++)
{
for(int k=0; k<other.columns; k++)
{
tempmatrix.matrix[i][j]=0;
tempmatrix.matrix[i][j]=tempmatrix.matrix[i][j]+other.matrix[i][k]*other1.matrix[k][j];
}
}
}
return tempmatrix;
}
else cout<<"Sorry !!!! Multiplication of tese matrices is not possible"<<endl;
}
bool Matrix::operator==(const Matrix & some)const
{
for(int i=0; i<rows; i++)
{
for(int j=0; j<columns; j++)
{
return (matrix[i][j]==some.matrix[i][j]);
}
}
}
bool Matrix::operator!=(const Matrix & some1 )const
{
for(int i=0; i<rows; i++)
{
for (int j=0; j<columns; j++)
{
return (matrix[i][j]!=some1.matrix[i][j]);
}
}
}
const Matrix & Matrix::operator=(const Matrix & someother )
{
for(int i=0; i<rows; i++)
{
for(int j=0; j<columns; j++)
{
matrix[i][j]=someother.matrix[i][j];
}
}
return *this;
}
bool Matrix::isSquareMatrix()
{
if(rows==columns)
{
return true;
}
else return -1;
}
bool Matrix::reverseDiagonals()
{
if(rows==columns)
{
for(int i=0; i<rows/2; i++)
{
int temp=0;
temp=matrix[i][i];
matrix[i][i]=matrix[rows-1-i][rows-1-i];
matrix[rows-1-i][rows-1-i]=temp;
}
for(int i=0; i<rows/2; i++)
{
int temp=0;
temp=matrix[i][rows-1-i];
matrix[i][rows-1-i]=matrix[rows-1-i][i];
matrix[rows-1-i][i]=temp;
}
}
else return -1;
}
void Matrix::transpose()
{
for(int i = 0; i < rows; i++)
{
for(int j = i; j < columns; j++)
{
int temp=0;
temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}
int Matrix::getRows()
{
return rows;
}
int Matrix::getColumns()
{
return columns;
}
void Matrix::setRows()
{
if(rows>0)
rows=rows;
else rows=1;
}
void Matrix::setColumns()
{
if(columns>0)
columns=columns;
else columns=1;
}
bool Matrix::checkSymmetric()
{
for(int i=0; i<rows/2; i++)
{
for(int j=i+1; j<columns; j++)
{
if(matrix[i][j]==matrix[j][i])
return true;
else return false;
}
}
}
void Matrix::upperTriangle()
{
if(rows==columns)
{
for(int i=0; i<rows; i++)
{
for(int j=i+1; j<columns; j++)
{
matrix[j][i]=0;
}
}
}
else cout<<"Sorry !!!! The Matrix is not square so its uppertriangle cannot be found"<<endl;
}
void Matrix::lowerTriangle()
{
if(rows==columns)
{
for(int i=0; i<rows; i++)
{
for(int j=i+1; j<columns; j++)
{
matrix[i][j]=0;
}
}
}
else cout<<"Sorry !!!! The Matrix is not square so its lower triangle cannot be found"<<endl;
}
int Matrix::isSymmetric()
{
checkSymmetric();
if(checkSymmetric()==true)
return 1;
else return 0;
}
Matrix::~Matrix()
{
getRows();
int r=0;
r=getRows();
for(int i=0; i<r; i++)
{
delete []matrix[i];
}
delete [] matrix;
}
int main()
{
ifstream infile;
infile.open("z:\\matrix.txt");
if(!infile)
{
cerr<<"File opening error"<<endl;
}
ofstream outfile;
outfile.open("z:\\out.txt");
int rows1=0;
infile>>rows1;
int columns1=0;
infile>>columns1;
Matrix M1(rows1,columns1);
infile>>M1;
int rows2=0;
int columns2=0;
infile>>rows2;
infile>>columns2;
Matrix M2(rows2,columns2);
infile>>M2;
Matrix M3(rows1,columns1);
M3= M1+ M2;
outfile<<"The sum of first two Matrices is"<<endl;
outfile<<M3<<endl;
int rows3=0;
infile>>rows3;
int columns3=0;
infile>>columns3;
Matrix M4(rows3,columns3);
infile>>M4;
int rows5;
int columns5;
infile>>rows5;
infile>>columns5;
Matrix M5(rows5,columns5);
infile>>M5;
Matrix M6(rows3,columns5);
M6=M4*M5;
outfile<<"The product of two Matrices is"<<endl;
outfile<<M6<<endl<<endl;
Matrix M7(M6);
outfile<<"copy constructor is called which saves previous Matrix"<<endl<<endl<<endl;
outfile<<M7<<endl<<endl;// Copy Constructor is called
outfile<<"Checking the equality of the first two matrix"<<endl<<endl;
if(M1== M2)
outfile<< "Matrices are equal"<<endl<<endl;
else
outfile<< "matrices are not equal"<<endl<<endl<<endl;
outfile<<"CHECKING THE SYMMETRY OF NEXT MATRIX"<<endl;
int rows6;
int columns6;
infile>>rows6;
infile>>columns6;
Matrix M8(rows6,columns6);
infile>>M8;
if(rows6==columns6)
{
M8.isSymmetric();
if(M8.isSymmetric()==1)
{
outfile<<"Matrix is Symmetric"<<endl;
}
else
{
outfile<<"Matrix is not symmetiric"<<endl;
}
}
else
{
outfile<<"Matrix is not square so its symmetry cannot be checked"<<endl<<endl;
}
int rows7=0;
int columns7=0;
infile>>rows7;
infile>>columns7;
Matrix M9(rows7,columns7);
infile>>M9;
M9.transpose();
outfile<<M9<<endl<<endl;
outfile<<"Now REVERSING THE DIOGNALS OF THE PREVIOUS MATRIX"<<endl<<endl;
M9.reverseDiagonals();
outfile<<M9<<endl<<endl;
outfile<<"NOw FINDING THE UPPER AND LOWER TRIANGLE OF THE PREVIOUS MATRIX"<<endl<<endl;
M9.lowerTriangle();
outfile<<"Lower Triangle"<<endl<<endl;
outfile<<M9<<endl<<endl;
outfile<<"UPPER Triangle and LOwer triangle"<<endl<<endl;
M9.upperTriangle();
outfile<<M9<<endl<<endl;
return 0;
}
So, this was the complete tutorial presented before you so that you can understand how you can make use of C++ to implement Matrix class. If you have any questions related to above matrix implementation then do let me know. I will try my best to resolve your queries. Cheers!
Post a Comment