This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
141 lines (120 loc) · 6.15 KB
/
main.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
/*
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2017 Ivan Vaccari <[email protected]>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
*/
#include <iostream>
#include <string>
#include <cstring>
#include <map>
#include <cstdio>
#include <ctime>
#include "include/Matrix.h"
#include "include/ColumnVector.h"
#include "include/CommandParser.h"
#include "include/Algorithms.h"
int main(int argc, char* argv[]){
//checking command line parameters
if (argc<2){
std::cerr << "Linear system solver.\nA simple linear system solver for learning purposes.\n" << std::endl;
std::cerr << "Usage: " << argv[0] << " -f datafile.txt\n" << std::endl;
std::cerr << "Parameter list\n -h Show a small man page\n -f Specify the input definition file\n" << std::endl;
std::cerr << "This version support these solving algorithms: cramer rule, triangular rule\n" << std::endl;
return 1;
}
std::string fileName;
for(int i=1;i<argc;i++){
if (strcmp(argv[i],"-f")==0){
i++;
fileName=std::string(argv[i]);
}
if (strcmp(argv[i],"-h")==0){
//print a help then exit
std::cerr << "Linear system solver.\nA simple linear system solver for learning purposes.\n" << std::endl;
std::cerr << "Definition file syntax" << std::endl;
std::cerr << "-Define a matrix\n [matrix NAME]\n a11 a12 a13 ... a1n\n a21 a22 a23 ... a2n\n ... ... ... ... ...\n an1 an2 an3 ... ann\n\n\n-Define a column vector\n[colvector name]\n a1\n a2\n ...\n an\n\n\n-Define commands\n[commands]\ntargetname1=cramer(MATRIXNAME1,colvectorname1)\ntargetname2=cramer(MATRIXNAME2,colvectorname2)\n...\n\n\nEach definition section must be separated by an empty line\n" << std::endl;
return 1;
}
}
//matrix and vector cache. Key is the name of the matrix/vector, value is
//the effective object. Just like a couple name and data
std::map<std::string,Matrix> matrixes;
std::map<std::string,ColumnVector> columnVectors;
try{
CommandParser p;
p.loadFromFile(fileName);
if (p.operations.size()==0){
throw std::string("No operations defined. Stopping here");
}
//loading matrixes
std::list<std::string> matrixNames=p.getMatrixesToBeLoaded();
while (!matrixNames.empty()){
std::string matrixName=matrixNames.front();
matrixNames.pop_front();
Matrix m;
if (!m.loadFromFile(fileName,matrixName))
throw std::string("Can't load matrix ").append(matrixName).append(". Matrix not defined");
else
std::cout<<"Matrix "<<matrixName<<" loaded"<<std::endl;
matrixes[matrixName]=m;
}
//loading vectors
std::list<std::string> columnVectorNames=p.getColumnVectorToBeLoaded();
while (!columnVectorNames.empty()){
std::string colVectorName=columnVectorNames.front();
columnVectorNames.pop_front();
ColumnVector v;
if (!v.loadFromFile(fileName,colVectorName))
throw std::string("Can't load column vector ").append(colVectorName).append(". Column vector not defined");
else
std::cout<<"Column vector "<<colVectorName<<" loaded"<<std::endl;
columnVectors[colVectorName]=v;
}
//running commands
while (!p.operations.empty()){
Operation op=p.operations.front();
p.operations.pop_front();
op.execute(matrixes,columnVectors);
/*if (op.operation=="cramer"){
std::cout<<std::endl;
std::cout<<"Solving "<<op.targetVector<<"=cramer("<<op.operatorMatrix<<","<<op.operatorVector<<")..."<<std::endl;
ColumnVector Xi;
float d=matrixes[op.operatorMatrix].determinant();
std::vector<float> result=Algorithms::cramer_solve(matrixes[op.operatorMatrix],columnVectors[op.operatorVector].toStdVector(),d);
columnVectors[op.targetVector]=ColumnVector(result);
}else if (op.operation=="triangularSolve"){
std::cout<<std::endl;
std::cout<<"Solving "<<op.targetVector<<"=triangolarSolve("<<op.operatorMatrix<<","<<op.operatorVector<<")..."<<std::endl;
ColumnVector Xi;
std::vector<float> result=Algorithms::triangular_matrix_solve(matrixes[op.operatorMatrix],columnVectors[op.operatorVector].toStdVector());
columnVectors[op.targetVector]=ColumnVector(result);
}else if (op.operation=="gauss"){
std::cout<<std::endl;
std::cout<<"Building reduct system gauss("<<op.operatorMatrix<<","<<op.operatorVector<<")..."<<std::endl;
ColumnVector Xi;
std::pair<Matrix,std::vector<float> > result=Algorithms::gauss_reduction(matrixes[op.operatorMatrix],columnVectors[op.operatorVector].toStdVector());
matrixes[op.targetMatrix]=result.first;
columnVectors[op.targetVector]=ColumnVector(result.second);
}else if (op.operation=="print"){
if(columnVectors.find(op.operatorVector)!=columnVectors.end()){
std::cout<<"Printing vector "<<op.operatorVector<<":"<<std::endl;
columnVectors[op.operatorVector].print();
}else if(matrixes.find(op.operatorMatrix)!=matrixes.end()){
std::cout<<"Printing matrix "<<op.operatorMatrix<<":"<<std::endl;
matrixes[op.operatorMatrix].print();
}
}*/
}
}catch(std::string e){
//just show the error message before exit
std::cout <<e;
return 2;
}
return 0;
}