You are on page 1of 21

Ain Shams University

Faculty of Engineering
Computers & Systems Engineering Department
CSE-323 Programming with Data Structures
Spring 2009-2010

Project 1

“Matrix Class”
Presented by:

1. Shehab Ahmed Sec: 2


2. Mona Mohsen Sec: 4
3. Marwa Shokry Sec: 4
PROJECT DESCRIPTION

Project Main Objectives:


 Increase The understanding of Object Oriented Programming concepts
 Use These concepts in Implementation small program which can do some Matrix Operations
 Increase The understanding of C++ as Object Oriented Programming Language and how to use its Data
Structures in Implementation this program

IMPLEMENTATION:

MAIN CLASSES
Here’s we have only 1 Class which name is “Matrix” and it contains all members (Variables & Functions) that
we need to do Matrix operations.

CLASSSES DESCRIPTION

MATRIX CLASS
Class Purpose:

 Declare all attributes (variables) like number of columns and rows for every instances of this class and
these attributes are needed to do matrix operations
 Declare all Methods (Functions) like Matrices Addition , Substraction & all others basic operations

Private members:

 Attributes:
o row (Integer) : Store the number of rows for each matrix’s object
o col (Integer) : Store the number of columns for each matrix’s object
o **P (float pointer refers to another pointer):
This pointer used to declare 2 dimension dynamic array which will be needed to store the
values of matrix.
 Methods:
o There’s no private methods
Public members:
 Attributes:
o There’s no public attributes

 Methods:
o matrix (int a,int b) (Constructor)
o ~matrix () (Destructor)
o matrix (const matrix& m) (Copy Constructor)
o trans()
o det()
o printing()
o writing()
o aexample()
o bexample()

PUBLIC MEMBERS DETAILS

1. Constructor:
Syntax: matrix (int a,int b)
Inputs: a,b (integers)
Outputs: No Output
Main Function: Inialize Matrix Object

Implementation details:

Description matrix (int a,int b)

 Declare i & j for loops


 Pass the value of a & b to row & col of the int i,j;
object row=a; col=b;
 Creating 2D Dynamic Array

p=new float *[row];


 Inialize matrix values with zero
for(i=0;i<row;i++)
p[i]=new float[col];

for (i=0;i<row;i++)
{
for(j=0;j<col;j++)
{ p[i][j]=0;
}
}
2. Destructor:
Syntax: ~matrix ()
Inputs: No inputs
Outputs: No outputs
Main Function: Free allocated memory reserved for
2D Dynamic array of matrix object

Implementation details:

Description ~matrix ()

 Declare i for loop


 Delete 2D Dynamic Array int i;
for (i=0;i<row;i++)
delete p[i];
delete p;

3. Copy Constructor:
Syntax: matrix (const matrix& m)
Inputs: m (matrix passed by Reference)
Outputs: No Outputs
Main Function: Used in (= Operator) & in case of
calling return variable of matrix type

Implementation details:

Description matrix (const matrix& m)

 Declare i & j for loops


 Pass the value of row & col of passed int i,j;
matrix to row & col for (this) matrix row=m.row;col=m.col;
 Creating 2D Dynamic Array

p= new float *[row];


 Copy passed matrix values to (this) matrix
values for (i=0;i<row;i++)
p[i]=new float[col];
for (i=0;i<row;i++)
{
for(j=0;j<col;j++)
{ p[i][j]=m.p[i][j];
}
}
4. Transpose:
Syntax: matrix trans()
Inputs: No Inputs
Outputs: Matrix object
Main Function: Convert each Row to Column

Function details:
Make new matrix have same number of rows & columns of (this) matrix then put first
row in (this) matrix in first column in new matrix and second row in (this) matrix in
second column in new matrix and so on then return back new matrix.

Implementation details:

Description matrix trans()

 Declare i & j for loops


 Declare New matrix object called temp int i,j;
 Creating 2D Dynamic Array for temp matrix temp(row,col);
Object
temp.p=new float*[row];
for( i=0;i<row;i++)
 Copy each row of (this) matrix to each
temp.p[i]=new float [col];
column of temp object

for( i=0;i<row;i++)
{ for( j=0;j<col;j++)
{ temp.p[j][i]=p[i][j];
}
}
return temp;

5. Printing:
Syntax: void printing()
Inputs: No Inputs
Outputs: No Output
Main Function: Print the Contents of (this) Matrix
Implementation details:

Description void printing()


 Print “ Matrix = ” in screen
 Declare i & j for loops cout<<"\nMatrix = ";
int i,j;
 Move to new line to print new row
 Print the contents of each row for(i=0;i<row;i++)
{ cout<<endl;
 Move to new line after finishing
for (j=0;j<col;j++)
cout<<"\t"<<p[i][j];
}
cout<<"\n";

6. Writing :
Syntax: void writing()
Inputs: No Inputs
Outputs: No Output
Main Function: Let user insert the contents of matrix

Function details:
Ask user to insert the values of already exists matrix object whose number of Rows &
Column are declared and known.

Implementation details:

Description void writing()

 Declare i & j for loops


int i,j;
 Notify user if the number of columns not equal
number of rows if (row != col)

cout<<"\nNote: that The matrix is not


 Request the values of matrix contents from user SQUARE but you still can continue\n";
 Use nested loops to insert value for each element
of matrix
cout<<"\nPlease insert the matrix elements
Row by Row\n";

for(i=0;i<row;i++)

{cout<<"\nRow "<<i+1<<" (Only


"<<col<<" Columns): ";

for (j=0;j<col;j++)

{cin>>p[i][j];} }
7. Overloading + Operator :
Syntax: matrix operator +(const matrix& a)
Inputs: m (matrix passed by Reference)
Outputs: Matrix object
Main Function: Addition operation between two objects
of class matrix

Function details:
This function is used to do addition operation between 2 objects from matrix class and it
can be done by add each element of first object to corresponding element in second
object where the first object is (this) matrix and second object is (a) noting that if the 2
objects haven’t same dimensions the function will return matrix with zero elements and
has same dimension of Matrix a.

Implementation details:

Description matrix operator+(const matrix& a)

 Declare new matrix called temp


matrix temp(a.row,a.col);
 Notify user if there dimensions difference between
(this) matrix and (a) matrix if ((a.col!=col)||(a.row!=row))
 Return matrix temp which is initialized with zero { cout<<"\nERROR!!\nDifferent
values in case of dimensions difference Dimension\a\a\nNo Operation will Happened";

cout<<"\nthe the result of this


 In case of same dimensions do addition operation operation will be zero matrix"<<endl;
 Use nested loops to add each element in (this) to
corresponding element in (a) and put the result in return temp;}
(temp) else
 Return temp matrix
{ for(int i=0;i<temp.row;i++)

for (int j=0;j<temp.col;j++)

{temp.p[i][j]=p[i][j]+a.p[i][j];}

return temp;}
8. Overloading - Operator :
Syntax: matrix operator -(const matrix& a)
Inputs: m (matrix passed by Reference)
Outputs: Matrix object
Main Function: Substraction operation between two
objects of class matrix

Function details:
This function is used to do subtraction operation between 2 objects from matrix class and
this can be done by doing this operation between each element of first object and
corresponding element in second object where the first object is (this) matrix and second
object is (a) noting that if the 2 objects haven’t same dimensions the function will return
matrix with zero elements and has same dimension of Matrix a.

Implementation details:

Description matrix operator-(const matrix& a)

 Declare new matrix called temp


matrix temp(a.row,a.col);
 Notify user if there dimensions difference between
(this) matrix and (a) matrix if ((a.col!=col)||(a.row!=row))
 Return matrix temp which is initialized with zero { cout<<"\nERROR!!\nDifferent
values in case of dimensions difference Dimension\a\a\nNo Operation will Happened";

cout<<"\nthe the result of this


 In case of same dimensions do subtraction operation will be zero matrix"<<endl;
 Use nested loops to do subtraction between each
element in (this) and corresponding element in (a) return temp;}
and put the result in (temp) else
 Return temp matrix
{ for(int i=0;i<temp.row;i++)

for (int j=0;j<temp.col;j++)

{temp.p[i][j]=p[i][j]-a.p[i][j];}

return temp;}
9. Overloading = Operator :
Syntax: matrix operator =(const matrix& a)
Inputs: m (matrix passed by Reference)
Outputs: Matrix object
Main Function: Assign object from class matrix to
another one

Function details:
This function used to assign all attributes of one object to another one where the object
which is at right hand side of equal operator is assigned to the one which is at left hand
side noting that if the 2 objects haven’t same dimensions the object will not be assigned

Implementation details:

Description matrix operator=(const matrix& a)

 Check dimensions difference


if ((a.col!=col)||(a.row!=row))
 Notify user if there dimensions difference between
(this) matrix and (a) matrix { cout<<"\nERROR!!\nDifferent
Dimension\a\a\nNo Operation will Happened";
 Return (this) matrix with no change in its value cout<<"\nthe matrix will not be
 In case of same dimensions assign passed object to assigned"<<endl;
(this) object return *this;}
 Use nested loops to do assignment operation for
each element in (this) and corresponding element else
in (a) {
 Return (this) matrix
for(int i=0;i<row;i++)

{for (int j=0;j<col;j++)

{p[i][j]=a.p[i][j];}

return *this;
10. Determinant of matrix :
Syntax: double det()
Inputs: No Inputs
Outputs: Determinant Value (Double)
Main Function: Calculate determinant of matrix

Function details:
Calculate the determinant of matrix using Gauss Elimination Method

Implementation details:

Description double det()

 Check if the matrix is square matrix or not


matrix tmp(row,col);

tmp=*this;
 Check if the determinant equal zero double det_value=1.0;

float*temp,A;

int r=1;

temp=new float [col];


 Calculate the determinant using gauss elimination if(row!=col)

{ cout<<"\nERROR!!\nDifferent
Dimension\a\a\n"<<endl;

else

{ while(p[0][0]==0&&r<row)

{ for(int i=0;i<col;i++)

{ temp[i]=p[0][0];

p[0][0]=p[r][i];

p[r][i]=temp[i]; }

r++;

 Return det_value
for(int k=1;k<row;k++)

{ for(int r=k;r<row;r++)

{ if(p[k-1][k-1]==0)

{det_value=0;}

else
{

A=p[r][k-1]/p[k-1][k-1];

for(int i=0;i<col;i++)

temp[i]=p[k-1][i]*A;

p[r][i]=p[r][i]-temp[i];

for(int r=0;r<row;r++)

{det_value*=p[r][r];}

*this=tmp;

delete temp;

return det_value;
11. Inverse of matrix :
Syntax: matrix inv()
Inputs: No Inputs
Outputs: matrix object (Double)
Main Function: Calculate inverse of matrix

Function details:
Calculate the inverse of matrix using Gauss Elimination Method

Implementation details:

Description matrix inv()


float m;int d=0;int l=0;
 Check if the matrix is square matrix or not
float x=0; float y=0;

if(row != col)

{ cout<<"not valid";
 Check if the determinant equal zero
return *this;
 Calculate the inverse using gauss elimination }

else if (det()== 0)

cout << "not valid matrix";

return *this;

matrix b(row ,col);

for (int i=0;i<row;i++)

{ for (int j=0;j<row ;j++)

{ if (i==j)

b.p[i][j]=1;

else

b.p[i][j]=0;}

while (p[0][0]==0)

{float *m=new float[row];

for(int i=0;i<row;i++)

{ m [i]=p[0][i];
 Return (b)matrix
p[0][i]=p[d][i];
p[d][i]=m [i];

m[i]= b.p[0][i];

b.p[0][i]=b.p[d][i];

b.p[d][i]= m [i]; }

d++; }

for (int k=0; k<row ; k++)

{ x=p[l][l];

for (int i=0;i< row ;i++)

{ p[l][i]= p[l][i]/x;

b.p[l][i]=b.p[l][i]/x;}

for (int j=0;j<row;j++)

{ if (j !=l)

{ y=p[j][l];

for (int i=0;i<row;i++)

p[j][i]-=m*p[l][i];

b.p[j][i]-= m*b.p[l][i];}

l=l+1;}

}return b;
12. Initializing Matrix with specific values (1) :
Syntax: void aexample()
Inputs: No Inputs
Outputs: No Outputs
Main Function: initialize object with specfic values
for testing purpose

Function details:
2.4 5 30.2
(
Initializing object with 3x3 Matrix which have the following values 20.5 44
45
9
5.5 12 )
Implementation details:

Description void aexample()

 Print “Inialize Matrix…” in screen


cout<<"Inialize Matrix…"<<endl;
 Assigning values to (this) matrix object
p[0][0]=2.4;p[0][1]=5.0;p[0][2]=30.2;
p[1][0]=20.5;p[1][1]=44.0;p[1][2]=9.0;
p[2][0]=45;p[2][1]=5.5;p[2][2]=12.0;

13. Initializing Matrix with specific values (2) :


Syntax: void bexample()
Inputs: No Inputs
Outputs: No Outputs
Main Function: initialize object with specfic values
for testing purpose

Function details:
10 43 8
(
Initializing object with 3x3 Matrix which have the following values 15.6 30 9
32 43 2 )
Implementation details:

Description void bexample()

 Print “Initialize Matrix…” in


cout<<" Initialize Matrix… "<<endl;
screen
 Assigning values to (this) matrix object p[0][0]=10.0;p[0][1]=43.0;p[0][2]=8.0;
p[1][0]=15.6;p[1][1]=30.0;p[1][2]=9.0;
p[2][0]=32.0;p[2][1]=43.0;p[2][2]=2.0;
PERFORMANCE & FUNCTIONALITY TEST

Functionality Test:
 The functionality test done by using two member functions aexample()and
bexample()which are used to initialize 3x3 matrix object with specific values
 At the starting of our test we will declare 3 objects of matrix class and
initialize 2 of them with values in aexample()and bexample()then put the
result of any operation in the third one.
 After that we will test each operation that class can do and then compare the
outputs of matrix class with outputs of MATLAB 2009 Program

Performance Test:
 To measure the performance of Matrix class operations we will use this code:
clock_t start =clock();
<Operation>
clock_t end =clock(); cout<<"\nTime Consumed =
"<<((double)end-start)/CLOCKS_PER_SEC<<" seconds"<<endl;
where <Operation> will be replaced by one of matrix member functions
and execution time of operation will appear will running the program

 To measure the performance of same operations in MATLAB we will use this


code:

tic;
<Operation>
Toc;
where <Operation> will be replaced by one of MATLAB functions which do
the same operation that matrix class do , noting that the syntax of same
operation is different between Matrix Class & MATLAB Program
 Here is also execution time of operation after the above statements executed
Operation Table:
 The Table below will show all operation that Matrix class can do and the
corresponding one in MATLAB
 Note that A ,B and C each one of them is matrix and D is Double

Operation Table
<Operation> Matrix Class Syntax MATLAB Syntax

matrix A(3,3);
Declaring 3x3 Matrix A=[row1;row2;row3];
A.writing();
C=A+B;
Addition C=A+B
C.printing();
C=A-B;
Subtraction C=A-B
C.printing();
C=A*B; C=A*B
Multiplication
C.printing();
Transpose C=A.trans(); C=A’
double D;
Determinant D=A.det(); det(A)
cout<<D;
double D;
Inverse D=A.inv(); inv(A)
cout<<D;
Examples:
 Here is a complete example code to do addition operation
 This example will be used also to measure performance of each operation
Matrix class:
int main(){
matrix A(3,3),B(3,3),C(3,3);
A.aexample();
B.bexample();
clock_t start =clock();

C=A+B; //These statements can be replaced


C.printing(); //by any operation from table

cout<<"\nTime Consumed = "<<((double)end-start)


/CLOCKS_PER_SEC<<"seconds"<<endl;
retrun 0;
}

MATLAB:
>> A=[row1;row2;row3]
>> B=[row1;row2;row3]
>> tic; C= A+B; toc; %Red statement can be replaced from table

 Next paper includes testing table which shows the output of each operation and the
execution time of each one where execution time is measure of performance

You might also like