@@ -60,6 +60,18 @@ namespace numcpp
60
60
return norm;
61
61
}
62
62
63
+ FP error (const std::vector<FP>& v1, const std::vector<FP>& v2)
64
+ {
65
+ FP error = 0.0 ;
66
+
67
+ #pragma omp parallel for reduction(max: error)
68
+ for (size_t i = 0 ; i < v1.size (); ++i)
69
+ {
70
+ error = std::max (std::abs (v1[i] - v2[i]), error);
71
+ }
72
+ return error;
73
+ }
74
+
63
75
std::vector<FP> vector_FMA (const std::vector<FP>& lhs, FP coef, const std::vector<FP>& rhs)
64
76
{
65
77
const size_t size = lhs.size ();
@@ -210,15 +222,19 @@ namespace numcpp
210
222
211
223
for (size_t i = 0 ; i < max_iterations; ++i)
212
224
{
225
+ std::vector<FP> saved_approximation = approximation;
226
+
213
227
std::vector<FP> residual = (*system_matrix) * approximation - b;
214
228
215
- FP residual_norm = norm (residual);
216
- if (residual_norm <= required_precision) break ;
229
+ // FP residual_norm = norm(residual);
217
230
218
231
std::vector<FP> Ar = (*system_matrix) * residual;
219
232
FP tau = scalar_product (Ar, residual) / scalar_product (Ar, Ar);
220
233
221
234
approximation = vector_FMA (residual, -tau, approximation);
235
+
236
+ FP approximation_error = error (saved_approximation, approximation);
237
+ if (approximation_error <= required_precision) break ;
222
238
}
223
239
224
240
return approximation;
@@ -439,10 +455,11 @@ namespace numcpp
439
455
440
456
for (size_t i = 1 ; i < max_iterations; ++i)
441
457
{
458
+ std::vector<FP> saved_approximation = approximation;
459
+
442
460
residual = (*system_matrix) * approximation - b;
443
461
444
- FP residual_norm = norm (residual);
445
- if (residual_norm <= required_precision) break ;
462
+ // FP residual_norm = norm(residual);
446
463
447
464
FP beta = scalar_product (Ah, residual) / scalar_product (Ah, h);
448
465
@@ -453,6 +470,9 @@ namespace numcpp
453
470
alpha = -scalar_product (residual, h) / scalar_product (Ah, h);
454
471
455
472
approximation = vector_FMA (h, alpha, approximation);
473
+
474
+ FP approximation_error = error (saved_approximation, approximation);
475
+ if (approximation_error <= required_precision) break ;
456
476
}
457
477
458
478
return approximation;
0 commit comments