-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJacobiMethod.cpp
More file actions
146 lines (123 loc) · 3.31 KB
/
JacobiMethod.cpp
File metadata and controls
146 lines (123 loc) · 3.31 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
// Ekaterina Mozhegova
#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
using namespace std;
class Matrix {
public:
Matrix(int n) : data(n, vector<double>(n)) {}
int size() const { return data.size(); }
vector<double>& operator[](int i) { return data[i]; }
const vector<double>& operator[](int i) const { return data[i]; }
bool isDiagonallyDominant() const {
int n = data.size();
for (int i = 0; i < n; i++) {
double diagonal = abs(data[i][i]);
double sum = 0.0;
for (int j = 0; j < n; j++) {
if (i != j) {
sum += abs(data[i][j]);
}
}
if (diagonal <= sum) {
return false;
}
}
return true;
}
private:
vector<vector<double>> data;
};
class ColumnVector {
public:
ColumnVector(int n) : data(n) {}
int size() const { return data.size(); }
double& operator[](int i) { return data[i]; }
const double& operator[](int i) const { return data[i]; }
double getLengthOfVectorSubtraction(const ColumnVector& other) const {
double sum = 0;
for (int i = 0; i < data.size(); i++) {
sum += (data[i] - other.data[i]) * (data[i] - other.data[i]);
}
return sqrt(sum);
}
private:
vector<double> data;
};
int main() {
int n;
cin >> n;
Matrix A(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> A[i][j];
}
}
int n2;
cin >> n2;
ColumnVector b(n);
for (int i = 0; i < n; i++) {
cin >> b[i];
}
double epsilon;
cin >> epsilon;
if (!A.isDiagonallyDominant()) {
cout << "The method is not applicable!";
return 0;
}
Matrix alpha(n);
ColumnVector beta(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
alpha[i][j] = 0.0;
} else {
alpha[i][j] = -A[i][j] / A[i][i];
}
}
beta[i] = b[i] / A[i][i];
}
cout << "alpha:" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << fixed << setprecision(4) << alpha[i][j] << " ";
}
cout << endl;
}
cout << "beta:" << endl;
for (int i = 0; i < n; i++) {
cout << fixed << setprecision(4) << beta[i] << endl;
}
ColumnVector x(n);
int iteration = 0;
double max_error = 0.0;
while (true) {
ColumnVector next_x(n);
for (int i = 0; i < n; i++) {
next_x[i] = beta[i];
for (int j = 0; j < n; j++) {
next_x[i] += alpha[i][j] * x[j];
}
}
max_error = x.getLengthOfVectorSubtraction(next_x);
if (iteration != 0)
cout << "e: " << fixed << setprecision(4) << max_error << endl;
x = next_x;
cout << "x(" << iteration << "):" << endl;
bool exit = false;
if (max_error < epsilon) {
exit = true;
}
for (int i = 0; i < n; i++) {
cout << fixed << setprecision(4) << x[i];
if (!(exit && i == n-1))
cout << endl;
}
iteration++;
if (exit) {
break;
}
}
return 0;
}