Professional Documents
Culture Documents
Faculty of Engineering
Computers & Systems Engineering Department
CSE-323 Programming with Data Structures
Spring 2009-2010
Project 1
“Matrix Class”
Presented by:
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()
1. Constructor:
Syntax: matrix (int a,int b)
Inputs: a,b (integers)
Outputs: No Output
Main Function: Inialize Matrix Object
Implementation details:
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 ()
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:
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:
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:
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:
for(i=0;i<row;i++)
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:
{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:
{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:
{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:
tmp=*this;
Check if the determinant equal zero double det_value=1.0;
float*temp,A;
int r=1;
{ 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:
if(row != col)
{ cout<<"not valid";
Check if the determinant equal zero
return *this;
Calculate the inverse using gauss elimination }
else if (det()== 0)
return *this;
{ if (i==j)
b.p[i][j]=1;
else
b.p[i][j]=0;}
while (p[0][0]==0)
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++; }
{ x=p[l][l];
{ p[l][i]= p[l][i]/x;
b.p[l][i]=b.p[l][i]/x;}
{ if (j !=l)
{ y=p[j][l];
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:
Function details:
10 43 8
(
Initializing object with 3x3 Matrix which have the following values 15.6 30 9
32 43 2 )
Implementation details:
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
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();
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