-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatrixOperations.cpp
167 lines (152 loc) · 5.46 KB
/
MatrixOperations.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "MatrixOperations.h"
MatrixOperations::MatrixOperations()
:l(-100.0),h(100.0),omega(1.0) {
mat = new std::vector<std::vector<double> *>();
b = new std::vector<double>();
x = nullptr;
userFileArg = false;
first = true;
}
MatrixOperations::~MatrixOperations() {
delete b;
if(x != nullptr)
delete x;
if(!mat->empty())
for(auto v : *mat)
delete v;
delete mat;
}
std::string MatrixOperations::getFileName(){
std::string c;
std::cin>>c;
return c;
}
void MatrixOperations::createBuild(std::string& c) {
std::cout<<"\nIf a previous matrix A and vector b have been created they will be replaced now"<<std::endl;
if(c=="c") {
std::cout<<"\nPlease input size n a int for matrix A and vector b"<<std::endl;
n = getInputInt();
std::cout<<"\nDo you wish to input the lower and upper bounds of the random number generator? y/n"<<std::endl;
if(getInput() == "y") {
std::cout<<"\bPlease input the lower bounds"<<std::endl;
l = getInputDouble();
std::cout<<"\nPlease input the upper bounds"<<std::endl;
h = getInputDouble();
std::cout<<"\bThe values that will be used ar["<<l<<","<<h<<"]"<<std::endl;
} else {
std::cout<<"\nDefault values will be used which are [-100.0,100.0]"<<std::endl;
}
readWriteMatrix::getRandom(mat,b,n,n,l,h);
} else {
if(userFileArg){
if(first){
std::cout<<"\nUsing provided input file "<<file<<" to load matrix A and vector B"<<std::endl;
first = false;
}else{
std::cout<<"Use the file "<<file<<" to load the matrix A and vector B? y/n"<<std::endl;
c = getInput();
if(c != "y"){
std::cout<<"\nPlease input the name of the file to be used to load the matrix"<<std::endl;
file = getFileName();
}
}
}else{
if(first){
first = false;
std::cout<<"\nPlease input the name of the file to be used to load the matrix"<<std::endl;
file = getFileName();
}else{
std::cout<<"Use the file "<<file<<" to load matrix A and vector B? y/n"<<std::endl;
c = getInput();
if(c != "y"){
std::cout<<"\nPlease input the name of the file to be used to load the matrix"<<std::endl;
file = getFileName();
}
}
}
readWriteMatrix::getFromFile(file,mat,b);
}
std::cout<<"\nDo you wish to see the matrix A and vector b? y/n"<<std::endl;
c = getInput();
if(c == "y") {
std::cout<<"\nMatrix A:"<<std::endl;
MatVecUtil::print(mat);
std::cout<<"\nVector b:"<<std::endl;
MatVecUtil::print(b);
}
std::cout<<"\nDo you wish to save matrix A and vector b to disk? y/n \n"<<std::endl;
c = getInput();
if(c == "y") {
std::cout<<"The name of the file is matVec.txt"<<std::endl;
readWriteMatrix::writeMatVec("matVec.txt",mat,b);
}
}
bool MatrixOperations::operations(std::string& c) {
bool goToMatSolver = false;
if(c == "c" || c == "i"){
createBuild(c);
return goToMatSolver;
}
if(mat->empty() || b->empty()){
std::cout<<"\nYou have not created or imported a matrix yet please choose i for import or c for create random"<<std::endl;
c = getInput();
createBuild(c);
return true;
}
matrixMenu();
if(x != nullptr && !x->empty()) {
std::cout<<"The previous solution is now being erased\n"<<std::endl;
delete x;
}
switch(getMatrixMethodChoice()) {
case 1:
std::cout<<"Using Jacobi to solve Ax=b\n"<<std::endl;
meth = "JacobiSol";
x = iterativeSolvers::jacobi(mat,b);
break;
case 2:
std::cout<<"Using Gauss Seidel to solve Ax=b\n"<<std::endl;
meth = "GaussSeidelSol";
x = iterativeSolvers::gaussSeidel(mat,b);
break;
case 3:
meth = "SORSol";
std::cout<<"Using SOR to solve Ax=b"<<std::endl;
std::cout<<"Do you wish to use a custom omega value? y/n\n"<<std::endl;
c = getInput();
if(c == "y")
omega = getInputDouble();
std::cout<<"The value for omega is "<<omega<<"\n"<<std::endl;
x = iterativeSolvers::SOR(mat,b,omega);
break;
case 4:
meth = "GaussianElimSol";
std::cout<<"Using Gaussian Elimination to solve Ax=b\n"<<std::endl;
x = nonIterativeSolvers::gaussianElmination(mat,b);
break;
case 5:
meth = "LUSol";
std::cout<<"Using LU Decomposition to solve Ax=b\n"<<std::endl;
x = nonIterativeSolvers::luDecomposition(mat,b);
break;
case -1:
std::cout<<"The input was not valid please choose again\n"<<std::endl;
goToMatSolver = true;
break;
}
if(goToMatSolver)
return goToMatSolver;
std::cout<<"Do you wish to view the solution x? y/n \n"<<std::endl;
c = getInput();
if(c == "y") {
std::cout<<"Vector x: "<<std::endl;
MatVecUtil::print(x);
}
std::cout<<"Do you wish to save vector x to disk? y/n \n"<<std::endl;
c = getInput();
if(c == "y") {
std::cout<<"The name of the file is "+meth+".txt\n"<<std::endl;
readWriteMatrix::writeVector(meth+".txt",x);
}
return goToMatSolver;
}