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
| #include <iostream>
#include "kmath.h"
#include <vector>
#include <iomanip>
#include <chrono>
void GenerateMatrix(kmMatrixSparse & A, const int k);
void GenerateVectorB(std::vector<double> & b, const int k);
void GenerateReal(std::vector<double> & b, const int k);
double u(const double x);
std::vector<double> MGM(const int k, const std::vector<double> & z, const std::vector<double> & b,
const int smoothCount, const int recursionCount);
std::vector<double> r(const std::vector<double> & y);
std::vector<double> p(const std::vector<double> & y);
int main() {
std::vector<double> z, z_, b, y, U, x;
kmMatrixSparse A;
double eps, eps__ = 1;
unsigned long K, m;
int i = 0;
std::cin >> K;
//std::cin >> eps;
eps = 0.0000000001;
m = (unsigned long)pow(2, K) - 1;
z.resize(m);
z_.resize(m);
GenerateMatrix(A, K);
GenerateVectorB(b, K);
GenerateReal(U, K);
auto start = std::chrono::steady_clock::now();
while (eps__ > eps) {
i++;
z = MGM(K, z_, b, 1, 2);
eps__ = sqrt(1.0 / m) * NormEuclidean(z_ - z);
z_ = z;
}
auto end = std::chrono::steady_clock::now();
auto elapsed_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
std::cout << "MGM = " << elapsed_ns.count() / 1000000000.0 << " s" << std::endl;
start = std::chrono::steady_clock::now();
x.resize(m);
TDMAlgorithm(A, b, x);
end = std::chrono::steady_clock::now();
elapsed_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
std::cout << "TDMa = " << elapsed_ns.count() / 1000000000.0 << " s" << std::endl;
std::cout << DifferenceInfinity(x, z);
return 0;
}
void GenerateMatrix(kmMatrixSparse & A, const int k) {
int m = (int)pow(2, k) - 1;
double h = 1.0 / ((double)m + 1);
//h = 1;
A = kmMatrixSparse(m, m);
for (int i = 0; i < m; i++) {
if (i < m / 2) {
A.Insert(i, i, 2.0 / Square(h));
if (i > 0) {
A.Insert(i, i - 1, -1.0 / Square(h));
}
if (i != m - 1) {
A.Insert(i, i + 1, -1.0 / Square(h));
}
}
if (i == m / 2) {
A.Insert(i, i, 2.0 * 1.5 / Square(h));
if (i > 0) {
A.Insert(i, i - 1, -1.0 / Square(h));
}
if (i != m - 1) {
A.Insert(i, i + 1, -1.0 * 2 / Square(h));
}
}
if (i > m / 2) {
A.Insert(i, i, 2.0 * 2.0 / Square(h));
if (i > 0) {
A.Insert(i, i - 1, -1.0 * 2.0 / Square(h));
}
if (i != m - 1) {
A.Insert(i, i + 1, -1.0 * 2.0 / Square(h));
}
}
}
}
void GenerateVectorB(std::vector<double> & b, const int k) {
int m = (int)pow(2, k) - 1;
double h = 1.0 / ((double)m + 1);
h = 1;
b.resize((unsigned long)m);
for (int i = 0; i < m; i++) {
//int j = i + 1;
if (i < m / 2) {
b[i] = 2.0 * h * h;
}
if (i == m / 2) {
b[i] = 2.0 * 1.5 * h * h;
}
if (i > m / 2) {
b[i] = 2.0 * 2.0 * h * h;
}
}
}
void GenerateReal(std::vector<double> & b, const int k) {
int m = (int)pow(2, k) - 1;
double h = 1.0 / ((double)m + 1);
b.resize((unsigned long)m);
for (int i = 0; i < m; i++) {
//int j = i + 1;
b[i] = u((i + 1) * h);
}
}
double u(const double x) {
return x * (1 - x);
}
std::vector<double> MGM(const int k, const std::vector<double> & z, const std::vector<double> & b,
const int smoothCount, const int recursionCount) {
std::vector<double> result, d, y;
kmMatrixSparse A;
int m = (int)pow(2, k) - 1;
result.resize((unsigned long)m);
result = z;
GenerateMatrix(A, k);
if (k <= 1) {
std::vector<double> c = b;
A.Gauss(c, result);
}
else {
Jacobi(A, b, result, smoothCount);
d = r(A * result - b);
y.resize(d.size());
for (int i = 0; i < recursionCount; i++) {
y = MGM(k - 1, y, d, smoothCount, recursionCount);
}
result = result - p(y);
Jacobi(A, b, result, smoothCount);
}
return result;
}
std::vector<double> r(const std::vector<double> & y) {
std::vector<double> result;
result.resize((y.size() + 1) / 2 - 1);
for (int i = 0; i < result.size(); i++) {
result[i] = 0.5 * y[2 * i + 1] + 0.25 * (y[2 * i] + y[2 * i + 2]);
}
return result;
}
std::vector<double> p(const std::vector<double> & y) {
std::vector<double> result;
result.resize((y.size() + 1) * 2 - 1);
for (int i = 0; i < y.size(); i++) {
result[2 * i] += 0.5 * y[i];
result[2 * i + 1] += y[i];
result[2 * i + 2] += 0.5 * y[i];
}
return result;
} |