-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobal.cpp
More file actions
162 lines (148 loc) · 4.83 KB
/
global.cpp
File metadata and controls
162 lines (148 loc) · 4.83 KB
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
#include "./header/global.h"
// INPUT MATRIX (AUGMENTED MATRIX) => n x (m + 1)
void inputMatrix(vector<vector<float>> &matrix, int n, int m) {
for (int i = 0; i < n; i++) {
vector<float> row;
float constantValue;
for (int j = 0; j < m; j++) {
float x;
cin >> x;
row.push_back(x);
}
cin >> constantValue;
row.push_back(constantValue);
matrix.push_back(row);
}
}
// PRINT MATRIX (AUGMENTED MATRIX) => n x (m + 1)
void printMatrix(vector<vector<float>> &matrix) {
for (int i = 0; i < matrix.size(); i++) {
for (int j = 0; j < matrix[i].size() - 1; j++) {
cout << matrix[i][j] << " "; // basic operation
}
cout << matrix[i][matrix[i].size() - 1];
cout << endl;
}
}
// swap rows
void swapRows(vector<vector<float>> &matrix, int row1, int row2) {
int m = matrix[0].size();
for (int j = 0; j < m; j++) {
float temp = matrix[row1][j];
matrix[row1][j] = matrix[row2][j];
matrix[row2][j] = temp;
}
}
// divide row by divisor to do basic row operation (row operation) => row = row / divisor
void divideRow(vector<vector<float>> &matrix, int row, float divisor) {
int m = matrix[0].size();
for (int j = 0; j < m; j++) {
matrix[row][j] /= divisor;
}
}
// substraction of two rows
void subtractRows(vector<vector<float>> &matrix, int row, int leadRow, int lead) {
int m = matrix[0].size(); // Besar kolom
float val = matrix[row][lead];
for (int j = 0; j < m; j++) {
matrix[row][j] -= val * matrix[leadRow][j];
}
}
// find the reduced row echelon form of the matrix
void findReducedRowEchelonForm(vector<vector<float>> &matrix) {
int n = matrix.size(); // n is the number of rows
int m = matrix[0].size(); // m is the number of columns
int lead = 0; // start from the first column
// r = row, lead = column, i = iterator
for (int r = 0; r < n; r++) {
if (lead >= m) break; // to ensure that the lead is within the column
int i = r;
// find the first non-zero element in the column
while (matrix[i][lead] == 0) {
i++;
if (i == n) { // if the lead is zero, move to the next column
i = r;
lead++;
if (lead == m) break; // to ensure that the lead is within the column
}
}
if (lead < m) {
swapRows(matrix, i, r);
divideRow(matrix, r, matrix[r][lead]);
for (int i = 0; i < n; i++) {
if (i != r) {
subtractRows(matrix, i, r, lead);
}
}
lead++;
}
}
}
void printSolvedLinearSystem(vector<vector<float>> &matrix) {
int n = matrix.size();
int m = matrix[0].size();
// check if the row is all zeros except the last element
for (int i = 0; i < n; i++) {
bool isZero = true;
for (int j = 0; j < m - 1; j++) {
if (matrix[i][j] != 0) {
isZero = false;
break;
}
}
if (isZero && matrix[i][m - 1] != 0) {
cout << "NO SOLUTION" << endl;
return;
}
}
for (int i = 0; i < n; i++) {
bool isZero = true;
// check if the row is all zeros
for (int j = 0; j < m - 1; j++) {
if (matrix[i][j] != 0) {
isZero = false;
break;
}
}
if (isZero && matrix[i][m - 1] == 0) {
cout << "INFINITE SOLUTION" << endl;
return;
}
}
// print the solution
for (int i = 0; i < n; i++) {
cout << "x" << i + 1 << " = " << matrix[i][m - 1] << endl;
}
}
vector<vector<float>> createDummyMatrix(int x, int y) {
vector<vector<float>> matrix;
for (int i = 0; i < x; i++) {
vector<float> row;
for (int j = 0; j < y; j++) {
row.push_back(rand() % 10);
}
matrix.push_back(row);
}
return matrix;
}
void plotData() {
// Create gnuplot script
ofstream scriptFile("plot_script.plt");
scriptFile << "set title 'Execution Time Comparison'\n"
<< "set xlabel 'Test Number'\n"
<< "set ylabel 'Time (seconds)'\n"
<< "plot 'execution_times.txt' using 1:2 title 'Iterative' with lines,\\\n"
<< " 'execution_times.txt' using 1:3 title 'Recursive' with lines\n"
<< "pause -1\n";
scriptFile.close();
// Execute gnuplot
system("gnuplot plot_script.plt");
}
void saveDataToFile(const vector<double>& iterative_times, const vector<double>& recursive_times) {
ofstream dataFile("execution_times.txt");
for (size_t i = 0; i < iterative_times.size(); i++) {
dataFile << i << " " << iterative_times[i]
<< " " << recursive_times[i] << endl;
}
dataFile.close();
}