| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "pylaeva_s_simple_iteration_method/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <cctype> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "pylaeva_s_simple_iteration_method/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace pylaeva_s_simple_iteration_method { | ||
| 11 | namespace { | ||
| 12 | |||
| 13 | constexpr double kEps = 1e-6; | ||
| 14 | constexpr int kMaxIterations = 10000; | ||
| 15 | |||
| 16 | 88 | bool DiagonalDominance(const std::vector<double> &a, size_t n) { | |
| 17 |
2/2✓ Branch 0 taken 256 times.
✓ Branch 1 taken 88 times.
|
344 | for (size_t i = 0; i < n; i++) { |
| 18 | 256 | double diag = std::fabs(a[(i * n) + i]); // Модуль диагонального элемента | |
| 19 | double row_sum = 0.0; // Сумма модулей недиагональных элементов строки | ||
| 20 | |||
| 21 |
2/2✓ Branch 0 taken 928 times.
✓ Branch 1 taken 256 times.
|
1184 | for (size_t j = 0; j < n; j++) { |
| 22 |
2/2✓ Branch 0 taken 672 times.
✓ Branch 1 taken 256 times.
|
928 | if (j != i) { |
| 23 | 672 | row_sum += std::fabs(a[(i * n) + j]); | |
| 24 | } | ||
| 25 | } | ||
| 26 | // Проверка строгого диагонального преобладания: | ||
| 27 | // Диагональный элемент должен быть БОЛЬШЕ суммы остальных элементов строки | ||
| 28 |
1/2✓ Branch 0 taken 256 times.
✗ Branch 1 not taken.
|
256 | if (diag <= row_sum) { |
| 29 | return false; | ||
| 30 | } | ||
| 31 | } | ||
| 32 | return true; | ||
| 33 | } | ||
| 34 | } // namespace | ||
| 35 | |||
| 36 |
1/2✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
|
88 | PylaevaSSimpleIterationMethodSEQ::PylaevaSSimpleIterationMethodSEQ(const InType &in) { |
| 37 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 38 | GetInput() = in; | ||
| 39 | 88 | } | |
| 40 | |||
| 41 |
1/2✓ Branch 0 taken 88 times.
✗ Branch 1 not taken.
|
88 | bool PylaevaSSimpleIterationMethodSEQ::ValidationImpl() { |
| 42 | const auto &n = std::get<0>(GetInput()); | ||
| 43 | const auto &a = std::get<1>(GetInput()); | ||
| 44 | const auto &b = std::get<2>(GetInput()); | ||
| 45 |
3/6✓ Branch 0 taken 88 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 88 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 88 times.
✗ Branch 5 not taken.
|
88 | return ((n > 0) && (a.size() == n * n) && (b.size() == n) && (DiagonalDominance(a, n))); |
| 46 | } | ||
| 47 | |||
| 48 | 88 | bool PylaevaSSimpleIterationMethodSEQ::PreProcessingImpl() { | |
| 49 | 88 | return true; | |
| 50 | } | ||
| 51 | |||
| 52 | 88 | bool PylaevaSSimpleIterationMethodSEQ::RunImpl() { | |
| 53 | const auto &n = std::get<0>(GetInput()); | ||
| 54 | const auto &a = std::get<1>(GetInput()); | ||
| 55 | const auto &b = std::get<2>(GetInput()); | ||
| 56 | |||
| 57 | 88 | std::vector<double> x(n, 0.0); | |
| 58 |
1/4✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
88 | std::vector<double> x_new(n, 0.0); |
| 59 | |||
| 60 |
1/2✓ Branch 0 taken 688 times.
✗ Branch 1 not taken.
|
688 | for (int iter = 0; iter < kMaxIterations; ++iter) { |
| 61 |
2/2✓ Branch 0 taken 2312 times.
✓ Branch 1 taken 688 times.
|
3000 | for (size_t i = 0; i < n; ++i) { |
| 62 | double sum = 0.0; | ||
| 63 |
2/2✓ Branch 0 taken 8984 times.
✓ Branch 1 taken 2312 times.
|
11296 | for (size_t j = 0; j < n; ++j) { |
| 64 |
2/2✓ Branch 0 taken 6672 times.
✓ Branch 1 taken 2312 times.
|
8984 | if (j != i) { |
| 65 | 6672 | sum += a[(i * n) + j] * x[j]; | |
| 66 | } | ||
| 67 | } | ||
| 68 | 2312 | x_new[i] = (b[i] - sum) / a[(i * n) + i]; | |
| 69 | } | ||
| 70 | |||
| 71 | double norm = 0.0; | ||
| 72 |
2/2✓ Branch 0 taken 2312 times.
✓ Branch 1 taken 688 times.
|
3000 | for (size_t i = 0; i < n; ++i) { |
| 73 | 2312 | double diff = x_new[i] - x[i]; | |
| 74 | 2312 | norm += diff * diff; | |
| 75 | } | ||
| 76 | |||
| 77 |
1/2✓ Branch 1 taken 688 times.
✗ Branch 2 not taken.
|
688 | x = x_new; |
| 78 | |||
| 79 |
2/2✓ Branch 0 taken 600 times.
✓ Branch 1 taken 88 times.
|
688 | if (std::sqrt(norm) < kEps) { |
| 80 | break; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 |
1/2✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
|
88 | GetOutput() = x; |
| 85 | 88 | return true; | |
| 86 | } | ||
| 87 | |||
| 88 | 88 | bool PylaevaSSimpleIterationMethodSEQ::PostProcessingImpl() { | |
| 89 | 88 | return !GetOutput().empty(); | |
| 90 | } | ||
| 91 | |||
| 92 | } // namespace pylaeva_s_simple_iteration_method | ||
| 93 |