| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "Rastvorov_K_Simple_iteration_method/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "Rastvorov_K_Simple_iteration_method/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace rastvorov_k_simple_iteration_method { | ||
| 11 | |||
| 12 | namespace {} // namespace | ||
| 13 | |||
| 14 | ✗ | RastvorovKSimpleIterationMethodSEQ::RastvorovKSimpleIterationMethodSEQ(const InType &in) { | |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 | ✗ | GetInput() = in; | |
| 17 | GetOutput().clear(); | ||
| 18 | ✗ | } | |
| 19 | |||
| 20 | ✗ | bool RastvorovKSimpleIterationMethodSEQ::ValidationImpl() { | |
| 21 | ✗ | return GetInput() > 0; | |
| 22 | } | ||
| 23 | |||
| 24 | ✗ | bool RastvorovKSimpleIterationMethodSEQ::PreProcessingImpl() { | |
| 25 | ✗ | GetOutput().assign(static_cast<std::size_t>(GetInput()), 0.0); | |
| 26 | ✗ | return true; | |
| 27 | } | ||
| 28 | |||
| 29 | ✗ | bool RastvorovKSimpleIterationMethodSEQ::RunImpl() { | |
| 30 | ✗ | const int n = GetInput(); | |
| 31 | ✗ | if (n <= 0) { | |
| 32 | GetOutput().clear(); | ||
| 33 | ✗ | return false; | |
| 34 | } | ||
| 35 | |||
| 36 | constexpr double kEps = 1e-9; | ||
| 37 | constexpr int kMaxIter = 2000; | ||
| 38 | |||
| 39 | ✗ | std::vector<double> x(static_cast<std::size_t>(n), 0.0); | |
| 40 | ✗ | std::vector<double> x_new(static_cast<std::size_t>(n), 0.0); | |
| 41 | |||
| 42 | ✗ | const auto denom = static_cast<double>(2 * n); | |
| 43 | |||
| 44 | ✗ | for (int iter = 0; iter < kMaxIter; ++iter) { | |
| 45 | double sum_all = 0.0; | ||
| 46 | ✗ | for (double v : x) { | |
| 47 | ✗ | sum_all += v; | |
| 48 | } | ||
| 49 | |||
| 50 | ✗ | double max_diff = 0.0; | |
| 51 | |||
| 52 | ✗ | for (int i = 0; i < n; ++i) { | |
| 53 | ✗ | const auto idx = static_cast<std::size_t>(i); | |
| 54 | ✗ | const double old = x[idx]; | |
| 55 | ✗ | const double xnew = (1.0 - sum_all + old) / denom; | |
| 56 | ✗ | x_new[idx] = xnew; | |
| 57 | ✗ | max_diff = std::max(max_diff, std::abs(xnew - old)); | |
| 58 | } | ||
| 59 | |||
| 60 | x.swap(x_new); | ||
| 61 | |||
| 62 | ✗ | if (max_diff < kEps) { | |
| 63 | break; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | ✗ | GetOutput() = x; | |
| 68 | return true; | ||
| 69 | } | ||
| 70 | |||
| 71 | ✗ | bool RastvorovKSimpleIterationMethodSEQ::PostProcessingImpl() { | |
| 72 | ✗ | return true; | |
| 73 | } | ||
| 74 | |||
| 75 | } // namespace rastvorov_k_simple_iteration_method | ||
| 76 |