@@ -60,6 +60,18 @@ namespace numcpp
6060 return norm;
6161 }
6262
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+
6375 std::vector<FP > vector_FMA (const std::vector<FP >& lhs, FP coef, const std::vector<FP >& rhs)
6476 {
6577 const size_t size = lhs.size ();
@@ -210,15 +222,19 @@ namespace numcpp
210222
211223 for (size_t i = 0 ; i < max_iterations; ++i)
212224 {
225+ std::vector<FP > saved_approximation = approximation;
226+
213227 std::vector<FP > residual = (*system_matrix) * approximation - b;
214228
215- FP residual_norm = norm (residual);
216- if (residual_norm <= required_precision) break ;
229+ // FP residual_norm = norm(residual);
217230
218231 std::vector<FP > Ar = (*system_matrix) * residual;
219232 FP tau = scalar_product (Ar, residual) / scalar_product (Ar, Ar);
220233
221234 approximation = vector_FMA (residual, -tau, approximation);
235+
236+ FP approximation_error = error (saved_approximation, approximation);
237+ if (approximation_error <= required_precision) break ;
222238 }
223239
224240 return approximation;
@@ -439,10 +455,11 @@ namespace numcpp
439455
440456 for (size_t i = 1 ; i < max_iterations; ++i)
441457 {
458+ std::vector<FP > saved_approximation = approximation;
459+
442460 residual = (*system_matrix) * approximation - b;
443461
444- FP residual_norm = norm (residual);
445- if (residual_norm <= required_precision) break ;
462+ // FP residual_norm = norm(residual);
446463
447464 FP beta = scalar_product (Ah, residual) / scalar_product (Ah, h);
448465
@@ -453,6 +470,9 @@ namespace numcpp
453470 alpha = -scalar_product (residual, h) / scalar_product (Ah, h);
454471
455472 approximation = vector_FMA (h, alpha, approximation);
473+
474+ FP approximation_error = error (saved_approximation, approximation);
475+ if (approximation_error <= required_precision) break ;
456476 }
457477
458478 return approximation;
0 commit comments