#include "Matrix.h"
#include <iostream>
#include <ctime> // Needed for the true randomization
#include <cstdlib>
#include <cmath>
#include <fstream>
usingnamespace std;
Matrix::Matrix(){
vector<vector<double> > grid;
row = 0;
col = 0;
vector <double> ranSolution;
vector <double> jb_solution;
vector <double> solution;
vector <double> b_solution;
vector <double> J_solution;
vector <double> GS_solution;
vector <double> SOR_solution;
}
Matrix::Matrix(int row, int col){
row = row;
col = col;
for(int i = 0; i < row; i++)
{
vector <double> cols;
solution.push_back(0);
jb_solution.push_back(0);
J_solution.push_back(0);
GS_solution.push_back(0);
SOR_solution.push_back(0);
for(int j = 0; j < col; j++)
{
int x = rand()%20000 + 1;
double y = x / 100.00;
y = y - 100.00;
cols.push_back(y);
}
grid.push_back(cols);
}
generateSol();
generateJB();
}
Matrix::Matrix(const Matrix& m){
row = m.row;
col = m.col;
b_solution = m.b_solution;
//cout << "testing testing testing3 " << endl;
//ranSolution = rhs.getRanSolution();
//cout << "testing testing testing4 " << endl;
solution = m.solution;
//cout << "testing testing testing5 " << endl;
grid = m.grid;
//cout << "testing testing testing6 " << endl;
J_solution = m.J_solution;
GS_solution = m.GS_solution;
SOR_solution = m.SOR_solution;
}
Matrix::Matrix(ifstream& readFile, string& fileName){
readFile.open(fileName.c_str());
readFile >> row;
readFile >> col;
for(int i = 0; i < row; i++)
{
vector <double> cols;
jb_solution.push_back(0);
solution.push_back(0);
J_solution.push_back(0);
GS_solution.push_back(0);
SOR_solution.push_back(0);
for(int j = 0; j < col; j++)
{
double y = 0;
readFile >> y;
cols.push_back(y);
}
grid.push_back(cols);
}
for(int k = 0; k < row; k++)
{
double x = 0;
readFile >> x;
b_solution.push_back(x);
}
generateSol();
generateJB();
}
int Matrix::getRow() const{
return grid.size();
}
int Matrix::getCol() const{
return grid[0].size();
}
vector <double> Matrix::getB() const{
return b_solution;
}
vector <double> Matrix::getJB() const{
return jb_solution;
}
vector <double> Matrix::getSolution() const{
return solution;
}
vector <double> Matrix::getJsol() const{
return J_solution;
}
void Matrix::setJsol(int i, double x)
{
J_solution[i] = x;
}
vector <double> Matrix::getGSsol() const{
return GS_solution;
}
void Matrix::setGSsol(int i, double x)
{
GS_solution[i] = x;
}
vector <double> Matrix::getSORsol() const{
return SOR_solution;
}
void Matrix::setSORsol(int i, double x)
{
SOR_solution[i] = x;
}
vector <vector <double> > Matrix::getGrid() const{
return grid;
}
void Matrix::generateSol(){
cout << "What should you solution be?" << endl;
for (int i = 0; i < getRow(); i++)
{
cin >> solution[i];
}
}
void Matrix::generateB(){
for (int i = 0; i < getRow(); i++)
{
for (int j = 0; j < getCol(); j++)
{
b_solution[i] = ((grid[i][j] * solution[j]) + b_solution[i]);
}
}
}
void Matrix::generateJB(){
for (int i = 0; i < getRow(); i++)
{
for (int j = 0; j < getCol(); j++)
{
jb_solution[i] += (grid[i][j] * J_solution[j]);
}
}
}
void Matrix::printMatrixInfo(){
for (int i = 0; i < grid.size(); i++)
{
for (int j = 0; j < grid[i].size(); j++)
{
cout << grid[i][j] << " ";
}
cout << endl;
}
cout << "B is followed by Solution" << endl;
for (int i = 0; i < b_solution.size(); i++)
{
cout << b_solution[i] << " ";
cout << solution[i];
cout << endl;
}
cout << "J solution is followed by J_B" << endl;
for (int i = 0; i < J_solution.size(); i++)
{
cout << J_solution[i] << ", " << jb_solution[i];
cout << endl;
}
cout << "GS solution" << endl;
for (int i = 0; i < J_solution.size(); i++)
{
cout << GS_solution[i];
cout << endl;
}
}
void Matrix::diagMatrix(){
for (int i = 0; i < getRow(); i++)
{
for (int j = 0; j < getCol(); j++)
{
if (j == i)
{
for (int l = 0; l < getCol(); l++)
{
if (l != i)
grid[i][j] = fabs(grid[i][j]) + fabs(grid[i][l]);
}
}
}
}
}
void Matrix::writeFile(){
ofstream myFile;
myFile.open("MatrixFile.txt");
if (myFile.is_open())
{
myFile << getRow() << " " << getCol() << endl;
for (int i = 0; i < getRow(); i++)
{
for (int j = 0; j < getCol(); j++)
{
myFile << grid[i][j] << " ";
}
}
myFile << endl;
for (int i = 0; i < getRow(); i++)
{
myFile << b_solution[i] << " ";
}
}
}
Matrix Matrix::operator=(const Matrix& rhs){
row = rhs.getRow();
//cout << "testing testing testing1 " << row << endl;
col = rhs.getCol();
//cout << "testing testing testing2 " << col << endl;
b_solution = rhs.getB();
//cout << "testing testing testing3 " << endl;
//ranSolution = rhs.getRanSolution();
//cout << "testing testing testing4 " << endl;
solution = rhs.getSolution();
//cout << "testing testing testing5 " << endl;
grid = rhs.grid;
//cout << "testing testing testing6 " << endl;
}
the problem that I am having is that it won't work on any other matrix other than the basic one provided by my professor. I was hoping to get a second set of eyes on this thing thanks in advance