-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHPCProg1.cpp
More file actions
112 lines (107 loc) · 3.73 KB
/
HPCProg1.cpp
File metadata and controls
112 lines (107 loc) · 3.73 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
// HPCProg1.cpp : Program 1 for High-Performance Computing, a DIS with Dr. Asai Asaithambi
// by James Daniel O'Conner, N01058671
// Purpose: 10 iterations of the Gauss-Jacobi method for iterative linear systems of equations.
#include <iostream>
using namespace std;
class Matrix {
int width, height;
double* body;
double* keys;
public:
Matrix(int rows, int cols) {
width = cols;
height = rows;
body = new double[width * height];
keys = new double[height];
}
double get_value(int rows, int cols) {
return body[cols + rows * width];
}
void set_value(int rows, int cols, double val) {
body[cols + rows * width] = val;
}
//prints the contents of the Matrix object in order.
void print_me() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
cout << body[j + i * width] << " ";
}
cout << endl;
}
}
//Solves the matrix for xn = 1.
void solve() {
for (int i = 0; i < height; i++) {
double solver = body[(i * width) + i];
if (solver == 0) {
cout << "can't solve for 0. Leaving line untouched." << endl;
continue;
}
for (int j = 0; j < width; j++) {
body[j + i * width] = body[j + i * width] / solver;
}
keys[i] = body[(i * width) + i];
}
}
//prints the solved-for (xn) keys
void print_keys() {
for (int i = 0; i < height; i++) {
cout << "x" << i + 1 << ":" << keys[i] << endl;
}
}
//Performs the Gauss-Jacobi method on the matrix.
void Gauss_Jacobi() {
for (int i = 0; i < height; i++) {
double sum = 0;
for (int j = 0; j < width; j++) {
if ((j + i * width) != ((i * width) + i)) {
if ((j + i * width) == (((i + 1) * width))-1) {
sum +=body[j+i*width];
}
else {
sum -= body[j + i * width];
}
}
}
keys[i] = sum;
}
for (int i = 0; i < width-1; i++) {
for (int j = i; j < width * height; j += width) {
if (j != (i * width) + 1&&j!=0) {
body[j] *= keys[i];
}
}
}
}
};
int main()
{
int n, m = 0;
cout << "Please input the number of rows in the matrix" << endl;
cin >> n;
cout << "Please input the number of columns the matrix (it is suggested that this be the number of rows + 1)" << endl;
cin >> m;
Matrix Matt(n, m);
cout << "Please input each element of the matrix row-major (it is suggested that values not be 0 initially.)" << endl;
//set the initial values of the matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
double val;
cin >> val;
Matt.set_value(i, j, val);
cout << "Set " << i+1 << " " << j+1 << " to " << val << endl;
}
}
Matt.print_me();
cout << "Solving for values..." << endl;
Matt.solve(); // standardizes matrix such that each row is solved for its key value. I.E. the primary diagonal row should be expressed as 1x(subnm)
Matt.print_me(); //we initially assume that each value is simply equal to 1.
Matt.print_keys();
for (int i = 0; i < 10; i++) {
cout << "Iteration: " << i + 1 << endl;
Matt.Gauss_Jacobi();
Matt.print_me();
Matt.print_keys();
} // Professor asks for 10 iterations initially... it is posssible to bound the calculation based on difference between previous values with a bit of extra work.
return 0;
}