Hello Everybody,
I have translated the following class diagram (click on the link below) into c++ code. This is my early stage experience indeed for such an implementation from UML class diagram. I am curious to share my improvement. If you have any advice for further improvement, please feel free to drop a line.
Code:
---------------------------DataSource.h---------------------------------
#ifndef DATASOURCE_H
#define DATASOURCE_H
class DataSource
{
public:
double** dataElement = nullptr;
int nRow, nColumn;
DataSource();
~DataSource();
protected:
private:
};
#endif
--------------------------DataSource.cpp--------------------------------
#include "DataSource.h"
DataSource :: DataSource()
{
nRow = 4;
nColumn = 4;
double dataFromFile[4][4] = { { 10.0, -1.0, 2.0, 0.0 },
{ -1.0, 11.0, -1.0, 3.0 },
{ 2.0, -1.0, 10.0, -1.0 },
{ 0.0, 3.0, -1.0, 8.0 } };
allocateData2DbyMalloc(dataElement, nRow, nColumn);
for (int row = 0; row < nRow; row++)
{
for (int col = 0; col < nColumn; col++)
{
dataElement[row][col] = dataFromFile[row][col];
}
}
}
DataSource :: ~DataSource()
{
deAllocateData2DbyFree(dataElement, nRow);
dataElement = nullptr;
}
--------------------------Matrix.h---------------------------------
#ifndef MATRIX_H
#define MATRIX_H
class Matrix
{
public:
int numberOfRow, numberOfColumn;
double** matrixElement = nullptr;
Matrix();
virtual ~Matrix();
void filterDataElement();
};
#endif
--------------------------Matrix.cpp---------------------------------
#include "DataSource.h"
#include "Matrix.h"
#include <iostream>
Matrix :: Matrix()
{
DataSource *dataObj = new DataSource;
numberOfRow = dataObj->nRow;
numberOfColumn = dataObj->nColumn;
allocateData2DbyMalloc(this->matrixElement, numberOfRow, numberOfColumn);
//..Copy the data from Data Source to Matrix array. Filtarazation of data might necessary before copying.
for (int row = 0; row < numberOfRow; row++)
{
for (int col = 0; col < numberOfColumn; col++)
{
this->matrixElement[row][col] = dataObj->dataElement[row][col];
}
}
delete dataObj;
dataObj = nullptr;
}
----------------------MatrixAlgorithm.h-----------------------------
Matrix :: ~Matrix()
{
deAllocateData2DbyFree(matrixElement, numberOfRow);
matrixElement = nullptr;
}
#ifndef MATRIXALGORITHM_H
#define MATRIXALGORITHM_H
class MatrixAlgorithm
{
public:
MatrixAlgorithm();
~MatrixAlgorithm();
bool checkDimOfMatrixAndConstrain();
};
#endif
----------------------MatrixAlgorithm.cpp-----------------------------
#include "Matrix.h"
#include "MatrixAlgorithm.h"
#include <iostream>
MatrixAlgorithm :: MatrixAlgorithm()
{
Matrix* matObj = new Matrix;
ItarativeAlgorithm* itarativeAlgorithmObj = new GaussSeidel(matObj->numberOfRow, matObj->numberOfColumn);
itarativeAlgorithmObj->matrixA = matObj->matrixElement;
itarativeAlgorithmObj->vectorB = userInputObj->constrainVector;
itarativeAlgorithmObj->vectorX = decisionObj->decisionVector;
itarativeAlgorithmObj->algorithm();
delete itarativeAlgorithmObj;
itarativeAlgorithmObj = nullptr;
delete matObj;
matObj = nullptr;
}
MatrixAlgorithm :: ~MatrixAlgorithm()
{
}
---------------------ItarativeAlgorithm.h---------------------------
#ifndef ITARATIVE_H
#define ITARATIVE_H
class ItarativeAlgorithm
{
public:
double** matrixA;
double* vectorB;
double* vectorX;
double* vectorXnext;
int numberOfRow;
int numberOfColumn;
int maxItarationNnumber;
bool converge;
double relativeError;
double maxTolerance;
double minTolerance;
ItarativeAlgorithm();
virtual ~ItarativeAlgorithm();
void itarativePrint();
virtual void algorithm();
};
#endif
---------------------ItarativeAlgorithm.cpp---------------------------
#include "ItarativeAlgorithm.h"
#include <iostream>
ItarativeAlgorithm::ItarativeAlgorithm()
{
matrixA = nullptr;
vectorB = nullptr;
vectorX = nullptr;
vectorXnext = nullptr;
converge = false;
accuracyTolerance = 0.0;
maxItarationNnumber = 10;
}
ItarativeAlgorithm::~ItarativeAlgorithm()
{
}
void ItarativeAlgorithm::algorithm()
{
}
---------------------GaussSeidel.h------------------------------------
#ifndef GAUSSSEIDEL_H
#define GAUSSSEIDEL_H
#include "ItarativeAlgorithm.h"
class GaussSeidel : public ItarativeAlgorithm
{
public:
GaussSeidel(int, int);
~GaussSeidel();
void algorithm();
};
#endif // !JACOBI_H
---------------------GaussSeidel.cpp------------------------------------
#include "GaussSeidel.h"
#include <iostream>
GaussSeidel :: GaussSeidel(int nRow, int nCol) // The reason to define the derive constructor is to observe the deallocation of matrix properly//
{
this->numberOfRow = nRow;
this->numberOfColumn = nCol;
}
GaussSeidel :: ~GaussSeidel()
{
}
void GaussSeidel :: algorithm()
{
int itarationNumber = 0;
double sum = 0.0;
while (itarationNumber < this->maxItarationNnumber)
{
std::cout << "Step == " << itarationNumber << std::endl;
for (int i = 0; i < this->numberOfRow; i++)
{
for (int j = 0; j < this->numberOfColumn; j++)
{
if (i != j)
sum = sum + matrixA[i][j] * vectorX[j] * (-1);
}
vectorX[i] = (1.0 / matrixA[i][i])*(sum + vectorB[i]); // The only line changed from Jacobi Algorithm
sum = 0;
}
for (int i = 0; i < this->numberOfRow; i++)
{
std::cout << vectorX[i] << std::endl;
}
itarationNumber = itarationNumber + 1;
}
vectorX = nullptr;
}