-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzeydel_relaxation.cpp
337 lines (243 loc) · 11.2 KB
/
zeydel_relaxation.cpp
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "zeydel_relaxation.h"
#include "convinient_system.h"
#include "jacoby.h"
#include "iter_solution.h"
#include "norm_error.h"
#include "eps.h"
double norm_zeidel(vector<vector<double>> &matrix, double w){
vector<vector<double>> diag_down_matrix(matrix.size(), vector<double> (matrix.size()));
for (int i = 0; i < matrix.size(); i ++){
for (int j = 0; j < matrix.size(); j ++){
diag_down_matrix[i][j] = matrix[i][j];
if (i != j){
diag_down_matrix[i][j] = w * diag_down_matrix[i][j]; //ДОМНОЖАЕМ НА ПАРАМЕТР W МАТРИЦУ D
}
if (j > i){
diag_down_matrix[i][j] = 0;
}
}
}
// cout << "НИЖНЕТРЕУГОЛЬНАЯ" << endl;
// for (const auto &v:diag_down_matrix){
// for (const auto &n: v){
// cout << n << " ";
// }
// cout << endl;
// }
// cout << endl;
vector<vector<double>> diag_up_matrix(matrix.size(), vector<double> (matrix.size()));
for (int i = 0; i < matrix.size(); i ++){
for (int j = 0; j < matrix.size(); j ++){
diag_up_matrix[j][i] = diag_down_matrix[i][j];//ТРАНСПОНИРУЕМ
}
}
// cout << "ВЕРХНЕТРЕУГОЛЬНАЯ" << endl;
// for (const auto &v:diag_up_matrix){
// for (const auto &n: v){
// cout << n << " ";
// }
// cout << endl;
// }
// cout << endl;
vector<vector<double>> reverse_up_matrix(matrix.size(), vector<double> (matrix.size()));
for (int i = 0; i < matrix.size(); i ++){
reverse_up_matrix[i][i] = 1/diag_up_matrix[i][i];// ОБРАТИЛИ ЭЛЕМЕНТЫ НА ГЛАВНОЙ ДИАГОНАЛИ
}
//НАЙДЕМ ОБРАТНУЮ К ВЕРХНЕТРЕУГОЛЬНОЙ МАТРИЦЕ
for (int i = 0; i < matrix.size(); i ++){
for (int j = 0; j < matrix.size(); j ++){
double sum = 0;
for (int k = 0; k <= j; k ++){
sum += reverse_up_matrix[i][k] * diag_up_matrix[k][j];
}
reverse_up_matrix[i][j] = (-1/diag_up_matrix[j][j] * sum);
}
}
// cout << "ПОЛУЧЕННАЯ МАТРИЦА. ОБРАТНАЯ" << endl;
// for (const auto &v:reverse_up_matrix){
// for (const auto &n: v){
// cout << -n << " ";
// }
// cout << endl;
// }
// cout << endl;
//ТРАНСПОНИРУЕМ ПОЛУЧЕННУЮ МАТРИЦУ
vector<vector<double>> reverse_down_matrix(matrix.size(), vector<double> (matrix.size()));
for (int i = 0; i < matrix.size(); i ++){
for (int j = 0; j < matrix.size(); j ++){
reverse_down_matrix[i][j] = -reverse_up_matrix[j][i];//СРАЗУ ТРАНСПОНИРУЕМ
}
}
// cout << "ПОЛУЧЕННАЯ МАТРИЦА. НИЖНЕТРЕУГОЛЬНАЯ" << endl;
// for (const auto &v:reverse_down_matrix){
// for (const auto &n: v){
// cout << n << " ";
// }
// cout << endl;
// }
// cout << endl;
//НАЙДЕМ МАТРИЦУ (D + wL - A)
vector<vector<double>> d_l_a_matrix(matrix.size(), vector<double> (matrix.size()));
for (int i = 0; i < d_l_a_matrix.size(); i++){
for (int j = 0 ; j < d_l_a_matrix.size(); j ++){
d_l_a_matrix[i][j] = diag_down_matrix[i][j] - matrix[i][j];
}
}
// cout << "МАТРИЦА D + L + A" << endl;
// for (const auto &v:d_l_a_matrix){
// for (const auto &n: v){
// cout << n << " ";
// }
// cout << endl;
// }
// cout << endl;
//ПЕРЕМНОЖИМ ПОЛУЧЕННЫЕ МАТРИЦЫ
vector<vector<double>> product_matrix(matrix.size(), vector<double> (matrix.size()));
for (int i = 0; i < matrix.size(); i++){
for (int j = 0; j < matrix.size(); j++){
for (int k = 0; k < matrix.size(); k++){
product_matrix[i][j] += reverse_down_matrix[i][k] * d_l_a_matrix[k][j];
}
}
}
// cout << "МАТРИЦА С" << endl;
// for (const auto &v:product_matrix){
// for (const auto &n: v){
// cout << n << " ";
// }
// cout << endl;
// }
// cout << endl;
//НАЙДЕМ НОРМУ МАТРИЦЫ С
double norm = 0;
double max_row_c = 0;
double sum_row_c= 0;
for (int i = 0; i < product_matrix.size(); i++){
sum_row_c = 0;
for (int j = 0; j < product_matrix.size(); j++){
sum_row_c += fabs(product_matrix[i][j]);
}
if (sum_row_c > max_row_c){
max_row_c = sum_row_c;
}
}
cout << "НОРМА МАТРИЦЫ С(МЕТОДА ЗЕЙДЕЛЯ, РЕЛАКСАЦИИ) = " << max_row_c << endl;
return max_row_c;
}
//ФУНКЦИЯ ПРИВЕДЕНИЯ МАТРИЦЫ К УДОБНОМУ ВИДУ (МЕТОД ЗЕЙДЕЛЯ, РЕЛАКСАЦИИ)
std::vector<std::vector<double>> zeydel_relaxation_matrix(std::vector<std::vector<double>> &matrix, vector<double> &changed_vector_zeydel, double &norm_zeydelC){
std::vector<std::vector<double>> result_convinient_matrix_zeydel = convinient_matrix_jacoby(matrix, changed_vector_zeydel, norm_zeydelC);
return result_convinient_matrix_zeydel;
}
//НОРМА КУБИЧЕСКАЯ ВЕРХНЕТРЕУГОЛЬНОЙ МАТРИЦЫ
double norm_up_triangle(std::vector<std::vector<double>> &matrix){
double max_row = 0;
for (int i = 0; i < matrix.size(); i++){
double temp_max_row = 0;
for (int j = 1 + i; j < matrix.size(); j ++){
temp_max_row += fabs(matrix[i][j]);
}
if (temp_max_row > max_row){
max_row = temp_max_row;
}
}
return max_row;
}
//ФУНКЦИЯ НАХОЖДЕНИЯ РЕШЕНИЯ(МЕТОД ЗЕЙДЕЛЯ, РЕЛАКСАЦИИ)
vector<double> zeydel_relax_solution(double ¶metr, std::vector<std::vector<double>> &matrix_zeydel, vector<double> &changed_vector_zeydel, double norm_B, double ¶metr_apost){
// cout << "Норма матрицы, приведенной к удобному виду" << norm_B << endl;
// cout << "МАТРИЦА B" << endl;
// for (const auto &v:matrix_zeydel){
// for (const auto &n: v){
// cout << n << " ";
// }
// cout << endl;
// }
// cout << endl;
//НОРМА ВЕРХНЕТРЕУГОЛЬНОЙ МАТРИЦЫ
double norm_B2 = norm_up_triangle(matrix_zeydel);
//НОВОЕ ЗНАЧЕНИЕ ЭПСИОЛОН
double EPS2 = (1 - norm_B)/(norm_B2)* EPSILON;
parametr_apost = (1 - norm_B)/(norm_B2);
//ВЫЛЕЛЯЕМ ПАМЯТЬ ПОД ВЕКТОРА РЕШЕНИЙ(ТЕКУЩИЙ И ПРЕДЫДУЩИЙ)
vector<double> init_solution(matrix_zeydel.size());
vector<double> new_solution(matrix_zeydel.size());
//ПУСТЬ НАЧАЛЬНЫМ ПРИБЛЕЖНИЕМ БУДЕТ НУЛЕВОЙ ВЕКТОР
for (int i = 0; i < matrix_zeydel.size(); i ++){
init_solution[i] = 0;
new_solution[i] = 0;
}
//РАЗНОСТЬ X^N И X^(N-1) РЕШЕНИЙ
std::vector<double> vector_difference(matrix_zeydel.size());
//НОРМА РАЗНОСТИ X^N И X^(N-1) РЕШЕНИЙ
double norm_difference = 1;
int amount_iter = 0;
while(norm_difference >= EPS2){
//ОБНУЛЯЕМ НОВЫЙ ВЕКТОР РЕШЕНИЙ
for (int i = 0; i < matrix_zeydel.size(); i ++){
init_solution[i] = new_solution[i];
new_solution[i] = 0;
}
for (int i = 0; i < matrix_zeydel.size(); i ++){
for (int j = 0; j < matrix_zeydel.size(); j ++){
//ПОДСЧЕТ УМНОЖЕНИЯ НИЖНЕТРЕУГОЛЬНОЙ МАТРИЦЫ НА ИЗМЕНЕННЫЙ ВЕКТОР
double product_B1_xnew = 0;
for (int k = 0; k < i; k ++){
product_B1_xnew += matrix_zeydel[i][k] * new_solution[k];
}
//ПОДСЧЕТ УМНОЖЕНИЯ ВЕРХНЕТРЕУГОЛЬНОЙ МАТРИЦЫ НА ПЕРВОНАЧАЛЬНЫЙ ВЕКТОР
double product_B2_xold = 0;
for (int k = 1 + i; k < matrix_zeydel.size(); k++){
product_B2_xold += matrix_zeydel[i][k] * init_solution[k];
}
new_solution[i] = (1 - parametr) * init_solution[i] + parametr * product_B1_xnew + parametr * product_B2_xold + parametr * changed_vector_zeydel[i];
}
double l = norm_error(new_solution);
if (l <= EPSILON){
cout << "достииигнууууууууууутаааааааааа " << amount_iter << endl;
}
}
for (int i = 0; i < matrix_zeydel.size(); i++){
vector_difference[i] = new_solution[i] - init_solution[i];
}
//НОРМА ВЕКТОРА РАЗНОСТИ
norm_difference = norm_vector(vector_difference);
amount_iter++;
}
cout << "Количество итераций = " << amount_iter << endl;
//Для априорной оценки
// int k =0;
// double norm_y = norm_vector_right_part(changed_vector_zeydel);
// while (k <= (log(EPS2 * (1 - norm_B)/(norm_y))/log(norm_B))){ //ОБНУЛЯЕМ НОВЫЙ ВЕКТОР РЕШЕНИЙ
// for (int i = 0; i < matrix_zeydel.size(); i ++){
// init_solution[i] = new_solution[i];
// new_solution[i] = 0;
// }
// for (int i = 0; i < matrix_zeydel.size(); i ++){
// for (int j = 0; j < matrix_zeydel.size(); j ++){
// //ПОДСЧЕТ УМНОЖЕНИЯ НИЖНЕТРЕУГОЛЬНОЙ МАТРИЦЫ НА ИЗМЕНЕННЫЙ ВЕКТОР
// double product_B1_xnew = 0;
// for (int k = 0; k < i; k ++){
// product_B1_xnew += matrix_zeydel[i][k] * new_solution[k];
// }
// //ПОДСЧЕТ УМНОЖЕНИЯ ВЕРХНЕТРЕУГОЛЬНОЙ МАТРИЦЫ НА ПЕРВОНАЧАЛЬНЫЙ ВЕКТОР
// double product_B2_xold = 0;
// for (int k = 1 + i; k < matrix_zeydel.size(); k++){
// product_B2_xold += matrix_zeydel[i][k] * init_solution[k];
// }
// // cout << "changed_vector_zeydel" << changed_vector_zeydel[i] << endl;
// new_solution[i] = (1 - parametr) * init_solution[i] + parametr * product_B1_xnew + parametr * product_B2_xold + parametr * changed_vector_zeydel[i];
// }
// }
// for (int i = 0; i < matrix_zeydel.size(); i++){
// vector_difference[i] = new_solution[i] - init_solution[i];
// }
// //НОРМА ВЕКТОРА РАЗНОСТИ
// norm_difference = norm_vector(vector_difference);
// k++;
// }
// cout << "Количество итераций, априорная оценка = " << k << endl;
// double aprior_error = (pow(norm_B, k))/(1 - norm_B)*norm_y;
// cout << "Априорная оценка погрешности = " << aprior_error << endl;
return new_solution;
}