| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "makoveeva_s_simple_iteration/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <vector> | ||
| 6 | |||
| 7 | #include "makoveeva_s_simple_iteration/common/include/common.hpp" | ||
| 8 | |||
| 9 | namespace makoveeva_s_simple_iteration { | ||
| 10 | |||
| 11 | namespace { | ||
| 12 | |||
| 13 | constexpr double kW = 0.5; | ||
| 14 | constexpr double kEps = 1e-6; | ||
| 15 | constexpr int kMaxIter = 1000; | ||
| 16 | |||
| 17 | 80 | void BuildSystem(int n, std::vector<double> *a, std::vector<double> *b) { | |
| 18 | 80 | const auto n_sz = static_cast<size_t>(n); | |
| 19 | 80 | a->assign(n_sz * n_sz, 0.0); | |
| 20 | 80 | b->assign(n_sz, 0.0); | |
| 21 | |||
| 22 |
2/2✓ Branch 0 taken 1144 times.
✓ Branch 1 taken 80 times.
|
1224 | for (int i = 0; i < n; ++i) { |
| 23 | 1144 | const auto i_idx = static_cast<size_t>(i); | |
| 24 | |||
| 25 | 1144 | (*a)[(i_idx * n_sz) + i_idx] = static_cast<double>(n) + 5.0; | |
| 26 | |||
| 27 |
2/2✓ Branch 0 taken 33704 times.
✓ Branch 1 taken 1144 times.
|
34848 | for (int j = 0; j < n; ++j) { |
| 28 |
2/2✓ Branch 0 taken 1144 times.
✓ Branch 1 taken 32560 times.
|
33704 | if (i == j) { |
| 29 | 1144 | continue; | |
| 30 | } | ||
| 31 | 32560 | const auto j_idx = static_cast<size_t>(j); | |
| 32 | 32560 | (*a)[(i_idx * n_sz) + j_idx] = 1.0 / (static_cast<double>(std::abs(i - j)) + 1.0); | |
| 33 | } | ||
| 34 | |||
| 35 | double bi = 0.0; | ||
| 36 |
2/2✓ Branch 0 taken 33704 times.
✓ Branch 1 taken 1144 times.
|
34848 | for (int j = 0; j < n; ++j) { |
| 37 | 33704 | const auto j_idx = static_cast<size_t>(j); | |
| 38 | 33704 | bi += (*a)[(i_idx * n_sz) + j_idx] * static_cast<double>(j + 1); | |
| 39 | } | ||
| 40 | 1144 | (*b)[i_idx] = bi; | |
| 41 | } | ||
| 42 | 80 | } | |
| 43 | |||
| 44 | 80 | void Iterate(int n, const std::vector<double> &a, const std::vector<double> &b, std::vector<double> *x) { | |
| 45 | 80 | const auto n_sz = static_cast<size_t>(n); | |
| 46 | 80 | std::vector<double> x_new(n_sz, 0.0); | |
| 47 | |||
| 48 |
1/2✓ Branch 0 taken 1840 times.
✗ Branch 1 not taken.
|
1840 | for (int iter = 0; iter < kMaxIter; ++iter) { |
| 49 | double err_sq = 0.0; | ||
| 50 | |||
| 51 |
2/2✓ Branch 0 taken 28048 times.
✓ Branch 1 taken 1840 times.
|
29888 | for (int i = 0; i < n; ++i) { |
| 52 | 28048 | const auto i_idx = static_cast<size_t>(i); | |
| 53 | |||
| 54 | double sum = 0.0; | ||
| 55 |
2/2✓ Branch 0 taken 852464 times.
✓ Branch 1 taken 28048 times.
|
880512 | for (int j = 0; j < n; ++j) { |
| 56 | 852464 | const auto j_idx = static_cast<size_t>(j); | |
| 57 | 852464 | sum += a[(i_idx * n_sz) + j_idx] * (*x)[j_idx]; | |
| 58 | } | ||
| 59 | |||
| 60 | 28048 | const double denom = a[(i_idx * n_sz) + i_idx]; | |
| 61 | 28048 | const double xi = (*x)[i_idx] + (kW * (b[i_idx] - sum) / denom); | |
| 62 | 28048 | x_new[i_idx] = xi; | |
| 63 | |||
| 64 | 28048 | const double diff = xi - (*x)[i_idx]; | |
| 65 | 28048 | err_sq += diff * diff; | |
| 66 | } | ||
| 67 | |||
| 68 | x->swap(x_new); | ||
| 69 |
2/2✓ Branch 0 taken 1760 times.
✓ Branch 1 taken 80 times.
|
1840 | if (std::sqrt(err_sq) < kEps) { |
| 70 | break; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | 80 | } | |
| 74 | |||
| 75 | int SumRounded(const std::vector<double> &x) { | ||
| 76 | double sum = 0.0; | ||
| 77 |
2/2✓ Branch 0 taken 1144 times.
✓ Branch 1 taken 80 times.
|
1224 | for (double v : x) { |
| 78 | 1144 | sum += v; | |
| 79 | } | ||
| 80 | 80 | return static_cast<int>(std::round(sum)); | |
| 81 | } | ||
| 82 | |||
| 83 | } // namespace | ||
| 84 | |||
| 85 | 80 | MakoveevaSSimpleIterationSEQ::MakoveevaSSimpleIterationSEQ(const InType &in) { | |
| 86 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 87 | 80 | GetInput() = in; | |
| 88 | GetOutput() = 0; | ||
| 89 | 80 | } | |
| 90 | |||
| 91 | 80 | bool MakoveevaSSimpleIterationSEQ::ValidationImpl() { | |
| 92 | 80 | return GetInput() > 0; | |
| 93 | } | ||
| 94 | |||
| 95 | 80 | bool MakoveevaSSimpleIterationSEQ::PreProcessingImpl() { | |
| 96 | 80 | GetOutput() = 0; | |
| 97 | 80 | return true; | |
| 98 | } | ||
| 99 | |||
| 100 | 80 | bool MakoveevaSSimpleIterationSEQ::RunImpl() { | |
| 101 | 80 | const int n = GetInput(); | |
| 102 | |||
| 103 | 80 | std::vector<double> a; | |
| 104 | 80 | std::vector<double> b; | |
| 105 |
1/2✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
|
80 | BuildSystem(n, &a, &b); |
| 106 | |||
| 107 |
1/4✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
80 | std::vector<double> x(static_cast<size_t>(n), 0.0); |
| 108 |
1/2✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
|
80 | Iterate(n, a, b, &x); |
| 109 | |||
| 110 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | GetOutput() = SumRounded(x); |
| 111 | 80 | return true; | |
| 112 | } | ||
| 113 | |||
| 114 | 80 | bool MakoveevaSSimpleIterationSEQ::PostProcessingImpl() { | |
| 115 | 80 | return GetOutput() > 0; | |
| 116 | } | ||
| 117 | |||
| 118 | } // namespace makoveeva_s_simple_iteration | ||
| 119 |