matrix class project final report

Upload: shehab-ahmed

Post on 08-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 Matrix Class Project Final Report

    1/23

  • 8/7/2019 Matrix Class Project Final Report

    2/23

    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 Languageand how to use its Data Structures in Implementation this program

    IMPLEMENTATION:

    MAIN CLASSES

    Heres 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 everyinstances of this class and these attributes are needed to do matrix operations

    Declare all Methods (Functions) like Matrices Addition , Substraction & all othersbasic operations

    Private members:

    Attributes: row (Integer) : Store the number of rows for each matrixs object col (Integer) : Store the number of columns for each matrixs object **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:

    Theres no private methods

  • 8/7/2019 Matrix Class Project Final Report

    3/23

    P ublic members: Attributes:

    Theres no public attributes

    Methods: matrix (int a,int b) (Constructor) ~matrix () (Destructor) matrix (const matrix& m) (Copy Constructor) trans() det() printing() writing() aexample() 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 object

    Creating 2D Dynamic Array

    Inialize matrix values with zero

    int i,j;

    row=a; col=b;

    p= new float *[row];

    for (i=0;i

  • 8/7/2019 Matrix Class Project Final Report

    4/23

    {

    for (j=0;j

  • 8/7/2019 Matrix Class Project Final Report

    5/23

    1. Destructor:Syntax: ~ matrix ()Inputs: No inputsOutputs: No outputsMain 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

  • 8/7/2019 Matrix Class Project Final Report

    6/23

    p[i]= new float [col];

    for (i=0;i

  • 8/7/2019 Matrix Class Project Final Report

    7/23

    1. Transpose:Syntax: matrix trans()Inputs: No Inputs

    Outputs: Matrix objectMain 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 matrixand second row in (this) matrix in second column in new matrix and soon then return back new matrix.

    Implementation details:Description matrix trans()

    Declare i & j for loops Declare New matrix object called

    temp Creating 2D Dynamic Array for

    temp Object

    Copy each row of (this) matrix toeach column of temp object

    int i,j;

    matrix temp(row,col);

    temp.p= new float *[row];

    for ( i=0;i

  • 8/7/2019 Matrix Class Project Final Report

    8/23

    Main Function: Print the Contents of (this)Matrix

    Implementation details:

    Description void printing()

    Print Matrix = in screen Declare i & j for loops

    Move to new line to print new row Print the contents of each row

    Move to new line after finishing

    cout

  • 8/7/2019 Matrix Class Project Final Report

    9/23

    Use nested loops to insert value foreach element of matrix cout

  • 8/7/2019 Matrix Class Project Final Report

    10/23

    1. Overloading + Operator :Syntax: matrix operator +(const matrix& a)Inputs: m (matrix passed by Reference)Outputs: Matrix objectMain Function: Addition operation between two

    objects of class matrix

    Function details:This function is used to do addition operation between 2 objects frommatrix class and it can be done by add each element of first object tocorresponding element in second object where the first object is (this)

    matrix and second object is (a) noting that if the 2 objects haventsame dimensions the function will return matrix with zero elementsand has same dimension of Matrix a.

    Implementation details:

    Description matrix operator+(const matrix& a)

    Declare new matrix called temp Notify user if there dimensions

    difference between (this) matrix and(a) matrix

    Return matrix temp which is initializedwith zero values in case of dimensions difference

    In case of same dimensions doaddition operation

    Use nested loops to add eachelement in (this) to correspondingelement in (a) and put the result in(temp)

    Return temp matrix

    matrix temp(a.row,a.col);

    if ((a.col!=col)||(a.row!=row))

    { cout

  • 8/7/2019 Matrix Class Project Final Report

    11/23

    1. Overloading - Operator :Syntax: matrix operator -(const matrix& a)Inputs: m (matrix passed by Reference)Outputs:

    Matrix objectMain Function: Substraction operation between twoobjects of class matrix

    Function details:This function is used to do subtraction operation between 2 objectsfrom matrix class and this can be done by doing this operationbetween each element of first object and corresponding element insecond object where the first object is (this) matrix and second object

    is (a) noting that if the 2 objects havent same dimensions the functionwill 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 Notify user if there dimensions

    difference between (this) matrix and(a) matrix

    Return matrix temp which is initializedwith zero values in case of dimensions difference

    In case of same dimensions dosubtraction

    Use nested loops to do subtractionbetween each element in (this) andcorresponding element in (a) and putthe result in (temp)

    Return temp matrix

    matrix temp(a.row,a.col);

    if ((a.col!=col)||(a.row!=row))

    { cout

  • 8/7/2019 Matrix Class Project Final Report

    12/23

    1. Overloading = Operator :Syntax: matrix operator =(const matrix& a)Inputs: m (matrix passed by Reference)Outputs:

    Matrix objectMain Function: Assign object from class matrix toanother one

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

    Implementation details:

    Description matrix operator=(const matrix& a)

    Check dimensions difference Notify user if there dimensions

    difference between (this) matrix and(a) matrix

    Return (this) matrix with no changein its value

    In case of same dimensions assignpassed object to (this) object

    Use nested loops to do assignmentoperation for each element in (this)and corresponding element in (a)

    Return (this) matrix

    if ((a.col!=col)||(a.row!=row))

    { cout

  • 8/7/2019 Matrix Class Project Final Report

    13/23

    1. Determinant of matrix :Syntax: double det()Inputs: No InputsOutputs:

    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 matrixor not

    Check if the determinant equal zero

    Calculate the determinant usinggauss elimination

    matrix tmp(row,col);

    tmp=* this ;

    double det_value=1.0;

    float *temp,A;

    int r=1;

    temp= new float [col];

    if (row!=col){ cout

  • 8/7/2019 Matrix Class Project Final Report

    14/23

    Return det_value { for ( int r=k;r

  • 8/7/2019 Matrix Class Project Final Report

    15/23

    1. Inverse of matrix :Syntax: matrix inv()Inputs: No InputsOutputs:

    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()

    Check if the matrix is square matrixor not

    Check if the determinant equal zero

    Calculate the inverse using gausselimination

    float m; int d=0; int l=0;

    float x=0; float y=0;

    if (row != col)

    { cout

  • 8/7/2019 Matrix Class Project Final Report

    16/23

    Return (b)matrix

    for ( int i=0;i

  • 8/7/2019 Matrix Class Project Final Report

    17/23

    1. Initializing Matrix with specific values (1) :Syntax: void aexample()Inputs: No InputsOutputs:

    No OutputsMain Function: initialize object with specficvalues for testing purpose

    Function details:Initializing object with 3x3 Matrix which have the following values2.4530.220.5449455.512Implementation details:

    Description void aexample()

    Print Inialize Matrix inscreen

    Assigning values to (this)matrix object

    cout

  • 8/7/2019 Matrix Class Project Final Report

    18/23

    in screen

    Assigning values to (this)matrix object

    cout

  • 8/7/2019 Matrix Class Project Final Report

    19/23

    PERFORMANCE & FUNCTIONALITY TEST

    Functionality Test: The functionality test done by using two member functions

    aexample() and bexample() which are used to initialize 3x3matrix 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() andbexample() then put the result of any operation in the thirdone.

    After that we will test each operation that class can do andthen 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:

    where will be replaced by one of matrix memberfunctions

    and execution time of operation will appear will running theprogram

    To measure the performance of same operations in MATLABwe will use this code:

    clock_t start =clock();

    clock_t end =clock(); cout

  • 8/7/2019 Matrix Class Project Final Report

    20/23

    where will be replaced by one of MATLABfunctions which do the same operation that matrix class do ,noting that the syntax of same operation is different betweenMatrix 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 Matrix Class Syntax MATLAB Syntax

    Declaring 3x3

    Matrix

    matrix A(3,3);

    A.writing();

    A=[row1;row2;row3]

    ;

    AdditionC=A+B;C.printing();

    C=A+B

    SubtractionC=A-B;C.printing();

    C=A-B

    MultiplicationC=A*B;

    C.printing();

    C=A*B

    Transpose C=A.trans(); C=A

    Determinantdouble D;D=A.det();cout

  • 8/7/2019 Matrix Class Project Final Report

    21/23

    cout

  • 8/7/2019 Matrix Class Project Final Report

    22/23

    Examples: Here is a complete example code to do addition operation This example will be used also to measure performance of

    each operationMatrix 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

  • 8/7/2019 Matrix Class Project Final Report

    23/23