This repository was archived by the owner on May 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
307 lines (259 loc) · 10.8 KB
/
main.cpp
File metadata and controls
307 lines (259 loc) · 10.8 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
#include <chrono>
#include <cmath>
#include "matrix_modules/matrix_operations.h"
#include "matrix_modules/verify_positive_definite_matrix.h"
#include "LUP/LUP_solver.h"
#include "SOR/SOR_solver.h"
#include "CG/CG_solver.h"
#include "PCG/PCG_solver.h"
#include "PI/power_iterations.h"
#include "II/inverse_iterations.h"
int main() {
std::ifstream inFile("input.txt");
std::ofstream outFile("output.txt");
if (!inFile || !outFile) {
std::cerr << "Error opening files!" << std::endl;
return 1;
}
// Read method flag and eigenvalue/parameter
int methodFlag;
double secondParam;
inFile >> methodFlag >> secondParam;
// Read stopping criterion and maximum iterations
double epsilon;
int maxIter;
inFile >> epsilon >> maxIter;
// Read matrix order
int n;
inFile >> n;
// Read matrix A
std::vector<std::vector<double>> A(n, std::vector<double>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
inFile >> A[i][j];
}
}
// Read vector b (or initial guess for eigenvalue methods)
std::vector<double> b(n);
for (int i = 0; i < n; i++) {
inFile >> b[i];
}
// Write header to output file
outFile << "NE 591 - Outlab 12 Code" << std::endl;
outFile << "Implemented by Hasibul Hossain Rasheeq, April 07, 2025" << std::endl;
outFile << "------------------------------------------------------" << std::endl << std::endl;
if (methodFlag == 4 || methodFlag == 5) {
outFile << "Compute fundamental eigenvector with Power Iterations" << std::endl;
outFile << "Compute fundamental eigenvalue: ";
if (methodFlag == 4) {
outFile << "PI";
} else {
outFile << "Rayleigh Quotient";
}
outFile << std::endl;
outFile << "-------------------------------------------------------" << std::endl << std::endl;
}
else if (methodFlag == 6) {
outFile << "Compute eigenmode with Inverse Iteration" << std::endl;
outFile << "------------------------------------------------------" << std::endl << std::endl;
}
else {
// Check matrix properties
bool symmetric = isSymmetric(A);
bool diagonallyDominant = isDiagonallyDominant(A);
if (!symmetric) {
outFile << "Error: Matrix is not symmetric!" << std::endl;
return 1;
}
outFile << "Solve Symmetric Positive Definite Matrix" << std::endl;
outFile << "Equation with Linear Solvers" << std::endl << std::endl;
outFile << "Matrix symmetry checked" << std::endl;
if (diagonallyDominant) {
outFile << "Matrix is diagonally dominant" << std::endl;
} else {
outFile << "Warning: Matrix is not diagonally dominant" << std::endl;
}
outFile << "User must ensure it is positive definite" << std::endl;
outFile << "-----------------------------------------" << std::endl << std::endl;
}
// Echo input data to output file
if (methodFlag == 4 || methodFlag == 5 || methodFlag == 6) {
outFile << "stopping criterion on eigen-vector/value = " << std::scientific << std::setprecision(2) << epsilon << std::endl;
} else {
outFile << "stopping criterion on residual norm = " << std::scientific << std::setprecision(2) << epsilon << std::endl;
}
outFile << "max number of iterations = " << maxIter << std::endl << std::endl;
outFile << "matrix is of order: " << n << std::endl << std::endl;
outFile << "Matrix A:" << std::endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
outFile << std::scientific << std::setprecision(2) << A[i][j] << " ";
}
outFile << std::endl;
}
outFile << std::endl;
if (methodFlag == 4 || methodFlag == 5) {
// For Power Iterations, b is the initial guess
outFile << "Initial guess:" << std::endl;
}
else if (methodFlag == 6) {
// For Inverse Iteration, b is the initial guess of eigenvector
outFile << "Initial guess of eigenvector:" << std::endl;
}
else {
outFile << "RHS vector b:" << std::endl;
}
for (int i = 0; i < n; i++) {
outFile << std::scientific << std::setprecision(2) << b[i] << " ";
}
outFile << std::endl << std::endl;
// Solve the system based on method flag
std::vector<double> x(n, 0.0);
// Start the timer
std::chrono::duration<double> elapsed;
auto start = std::chrono::high_resolution_clock::now();
if (methodFlag == 0) {
// LUP method
double maxResidual;
bool success = solveLUP(A, b, x, maxResidual);
// Record execution time
auto end = std::chrono::high_resolution_clock::now();
elapsed = end - start;
if (success) {
writeLUPResults(outFile, x, maxResidual);
} else {
outFile << "Error: LUP factorization failed!" << std::endl;
}
}
else if (methodFlag == 1) {
// SOR method
int iterations;
double residualNorm;
bool converged = solveSOR(A, b, x, secondParam, epsilon, maxIter, iterations, residualNorm);
// Record execution time
auto end = std::chrono::high_resolution_clock::now();
elapsed = end - start;
writeSORResults(outFile, x, iterations, residualNorm, secondParam);
if (!converged) {
outFile << "Warning: SOR method did not converge within maximum iterations!" << std::endl;
}
}
else if (methodFlag == 2) {
// CG method
int iterations;
double residualNorm;
bool converged = solveCG(A, b, x, epsilon, maxIter, iterations, residualNorm);
// Record execution time
auto end = std::chrono::high_resolution_clock::now();
elapsed = end - start;
writeCGResults(outFile, x, iterations, residualNorm);
if (!converged) {
outFile << "Warning: CG method did not converge within maximum iterations!" << std::endl;
}
}
else if (methodFlag == 3) {
// PCG method with Jacobi preconditioner
int iterations;
double residualNorm;
bool converged = solveJacobiPCG(A, b, x, epsilon, maxIter, iterations, residualNorm);
// Record execution time
auto end = std::chrono::high_resolution_clock::now();
elapsed = end - start;
writePCGResults(outFile, x, iterations, residualNorm, A);
if (!converged) {
outFile << "Warning: PCG method did not converge within maximum iterations!" << std::endl;
}
}
else if (methodFlag == 4 || methodFlag == 5) {
// Power Iterations method
int iterations;
double error;
double eigenvalue;
double eigenvalueError;
bool useRayleighQuotient = (methodFlag == 5);
bool converged = solvePowerIterations(A, b, epsilon, maxIter, x, iterations, error,
eigenvalue, eigenvalueError, useRayleighQuotient);
// Record execution time
auto end = std::chrono::high_resolution_clock::now();
elapsed = end - start;
writePowerIterationsResults(outFile, x, iterations, error);
// Output eigenvalue information
outFile << "Eigenvalue computed by ";
if (useRayleighQuotient) {
outFile << "Rayleigh Quotient" << std::endl;
} else {
outFile << "Power Iterations" << std::endl;
}
outFile << "Last iterate of eigenvalue = " << std::scientific << std::setprecision(4)
<< eigenvalue << std::endl;
outFile << "Iterative error in eigenvalue = " << std::scientific << std::setprecision(4)
<< eigenvalueError << std::endl << std::endl;
// Compute and output residual vector
std::vector<double> Ax = matrix_vector_product(A, x);
std::vector<double> lambdaX(n);
for (int i = 0; i < n; i++) {
lambdaX[i] = eigenvalue * x[i];
}
std::vector<double> residual = vector_subtract(Ax, lambdaX);
double residualNorm = calculate_Linf_norm(residual);
outFile << "Infinity norm of residual = " << std::scientific << std::setprecision(4)
<< residualNorm << std::endl;
outFile << "Residual vector:" << std::endl;
for (int i = 0; i < n; i++) {
outFile << std::scientific << std::setprecision(4) << residual[i] << " ";
}
outFile << std::endl << std::endl;
if (!converged) {
outFile << "Warning: Power Iterations method did not converge within maximum iterations!" << std::endl;
}
}
else if (methodFlag == 6) {
// Inverse Iteration method
int iterations;
double error;
double eigenvalue;
double eigenvalueError;
bool converged = solveInverseIterations(A, b, secondParam, epsilon, maxIter, x, iterations, error,
eigenvalue, eigenvalueError);
// Record execution time
auto end = std::chrono::high_resolution_clock::now();
elapsed = end - start;
writeInverseIterationsResults(outFile, x, iterations, error, secondParam);
// Output eigenvalue information
outFile << "Eigenvalue computed by Power Iterations" << std::endl;
outFile << "Last iterate of eigenvalue = " << std::scientific << std::setprecision(4)
<< eigenvalue << std::endl;
outFile << "Iterative error in eigenvalue = " << std::scientific << std::setprecision(4)
<< eigenvalueError << std::endl << std::endl;
// Compute and output residual vector
std::vector<double> Ax = matrix_vector_product(A, x);
std::vector<double> lambdaX(n);
for (int i = 0; i < n; i++) {
lambdaX[i] = eigenvalue * x[i];
}
std::vector<double> residual = vector_subtract(Ax, lambdaX);
double residualNorm = calculate_Linf_norm(residual);
outFile << "Infinity norm of residual = " << std::scientific << std::setprecision(4)
<< residualNorm << std::endl;
outFile << "Residual vector:" << std::endl;
for (int i = 0; i < n; i++) {
outFile << std::scientific << std::setprecision(4) << residual[i] << " ";
}
outFile << std::endl << std::endl;
if (!converged) {
outFile << "Warning: Inverse Iteration method did not converge within maximum iterations!" << std::endl;
}
}
else {
outFile << "Error: Invalid method flag!" << std::endl;
return 1;
}
outFile << "Execution time (ms) = " << std::fixed << std::setprecision(8) << elapsed.count() * 1000.0 << std::endl;
inFile.close();
outFile.close();
return 0;
}