| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "ivanova_p_simple_iteration_method/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <vector> | ||
| 6 | |||
| 7 | #include "ivanova_p_simple_iteration_method/common/include/common.hpp" | ||
| 8 | |||
| 9 | namespace ivanova_p_simple_iteration_method { | ||
| 10 | |||
| 11 | namespace { // Анонимный namespace для вспомогательных функций | ||
| 12 | |||
| 13 | // Новая функция: один шаг метода простой итерации | ||
| 14 | 1752 | void SimpleIterationStep(const std::vector<double> &a, const std::vector<double> &x, const std::vector<double> &b, | |
| 15 | std::vector<double> &x_new, int n, double tau) { | ||
| 16 |
2/2✓ Branch 0 taken 25912 times.
✓ Branch 1 taken 1752 times.
|
27664 | for (int i = 0; i < n; ++i) { |
| 17 | double ax = 0.0; | ||
| 18 |
2/2✓ Branch 0 taken 771768 times.
✓ Branch 1 taken 25912 times.
|
797680 | for (int j = 0; j < n; ++j) { |
| 19 | 771768 | ax += a[(static_cast<size_t>(i) * n) + j] * x[j]; | |
| 20 | } | ||
| 21 | 25912 | x_new[i] = x[i] - (tau * (ax - b[i])); | |
| 22 | } | ||
| 23 | 1752 | } | |
| 24 | |||
| 25 | // Новая функция: вычисление нормы разности | ||
| 26 | 1752 | double ComputeDiffNorm(const std::vector<double> &x, const std::vector<double> &x_new) { | |
| 27 | double diff = 0.0; | ||
| 28 |
2/2✓ Branch 0 taken 25912 times.
✓ Branch 1 taken 1752 times.
|
27664 | for (size_t i = 0; i < x.size(); ++i) { |
| 29 | 25912 | double d = x_new[i] - x[i]; | |
| 30 | 25912 | diff += d * d; | |
| 31 | } | ||
| 32 | 1752 | return std::sqrt(diff); | |
| 33 | } | ||
| 34 | |||
| 35 | } // namespace | ||
| 36 | |||
| 37 | 80 | IvanovaPSimpleIterationMethodSEQ::IvanovaPSimpleIterationMethodSEQ(const InType &in) { | |
| 38 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 39 | 80 | GetInput() = in; | |
| 40 | GetOutput() = 0; | ||
| 41 | 80 | } | |
| 42 | |||
| 43 | 80 | bool IvanovaPSimpleIterationMethodSEQ::ValidationImpl() { | |
| 44 |
2/4✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 80 times.
|
80 | return (GetInput() > 0) && (GetOutput() == 0); |
| 45 | } | ||
| 46 | |||
| 47 | 80 | bool IvanovaPSimpleIterationMethodSEQ::PreProcessingImpl() { | |
| 48 | 80 | GetOutput() = 0; | |
| 49 | 80 | return true; | |
| 50 | } | ||
| 51 | |||
| 52 | 80 | bool IvanovaPSimpleIterationMethodSEQ::RunImpl() { | |
| 53 | 80 | int n = GetInput(); | |
| 54 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | if (n <= 0) { |
| 55 | return false; | ||
| 56 | } | ||
| 57 | |||
| 58 | // Создаем тестовую систему: A = I (единичная матрица), b = (1, 1, ..., 1) | ||
| 59 | 80 | const size_t matrix_size = static_cast<size_t>(n) * n; | |
| 60 | 80 | std::vector<double> a(matrix_size, 0.0); | |
| 61 |
2/2✓ Branch 0 taken 1144 times.
✓ Branch 1 taken 80 times.
|
1224 | for (int i = 0; i < n; ++i) { |
| 62 | 1144 | a[(static_cast<size_t>(i) * n) + i] = 1.0; | |
| 63 | } | ||
| 64 | |||
| 65 |
1/4✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
80 | std::vector<double> b(n, 1.0); |
| 66 |
1/4✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
80 | std::vector<double> x(n, 0.0); |
| 67 |
1/4✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
80 | std::vector<double> x_new(n, 0.0); |
| 68 | |||
| 69 | // Параметры метода | ||
| 70 | const double tau = 0.5; | ||
| 71 | const double epsilon = 1e-6; | ||
| 72 | const int max_iterations = 1000; | ||
| 73 | |||
| 74 | // Метод простой итерации | ||
| 75 |
1/2✓ Branch 0 taken 1752 times.
✗ Branch 1 not taken.
|
1752 | for (int iter = 0; iter < max_iterations; ++iter) { |
| 76 | 1752 | SimpleIterationStep(a, x, b, x_new, n, tau); | |
| 77 | |||
| 78 |
2/2✓ Branch 1 taken 80 times.
✓ Branch 2 taken 1672 times.
|
1752 | if (ComputeDiffNorm(x, x_new) < epsilon) { |
| 79 |
1/2✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
|
80 | x = x_new; |
| 80 | break; | ||
| 81 | } | ||
| 82 | |||
| 83 | x.swap(x_new); | ||
| 84 | } | ||
| 85 | |||
| 86 | // Вычисление суммы компонент | ||
| 87 | double sum = 0.0; | ||
| 88 |
2/2✓ Branch 0 taken 1144 times.
✓ Branch 1 taken 80 times.
|
1224 | for (int i = 0; i < n; ++i) { |
| 89 | 1144 | sum += x[i]; | |
| 90 | } | ||
| 91 | |||
| 92 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | GetOutput() = static_cast<int>(std::round(sum)); |
| 93 | |||
| 94 | return true; | ||
| 95 | } | ||
| 96 | |||
| 97 | 80 | bool IvanovaPSimpleIterationMethodSEQ::PostProcessingImpl() { | |
| 98 | 80 | return GetOutput() > 0; | |
| 99 | } | ||
| 100 | |||
| 101 | } // namespace ivanova_p_simple_iteration_method | ||
| 102 |