| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "afanasyev_a_it_seidel_method/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "afanasyev_a_it_seidel_method/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace afanasyev_a_it_seidel_method { | ||
| 11 | |||
| 12 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | AfanasyevAItSeidelMethodSEQ::AfanasyevAItSeidelMethodSEQ(const InType &in) { |
| 13 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 14 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | GetInput() = in; |
| 15 | 24 | GetOutput() = std::vector<double>(); | |
| 16 | 24 | } | |
| 17 | |||
| 18 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | bool AfanasyevAItSeidelMethodSEQ::ValidationImpl() { |
| 19 | const auto &input = GetInput(); | ||
| 20 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (input.size() < 3) { |
| 21 | return false; | ||
| 22 | } | ||
| 23 | |||
| 24 | 24 | int system_size = static_cast<int>(input[0]); | |
| 25 | 24 | double epsilon = input[1]; | |
| 26 | 24 | int max_iterations = static_cast<int>(input[2]); | |
| 27 | |||
| 28 |
3/6✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 24 times.
|
24 | return system_size > 0 && epsilon > 0 && max_iterations > 0; |
| 29 | } | ||
| 30 | |||
| 31 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | bool AfanasyevAItSeidelMethodSEQ::PreProcessingImpl() { |
| 32 | try { | ||
| 33 | 24 | int system_size = static_cast<int>(GetInput()[0]); | |
| 34 | 24 | epsilon_ = GetInput()[1]; | |
| 35 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | max_iterations_ = static_cast<int>(GetInput()[2]); |
| 36 | |||
| 37 | A_.clear(); | ||
| 38 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | A_.resize(system_size); |
| 39 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 24 times.
|
168 | for (int i = 0; i < system_size; ++i) { |
| 40 |
1/2✓ Branch 1 taken 144 times.
✗ Branch 2 not taken.
|
144 | A_[i].resize(system_size); |
| 41 |
2/2✓ Branch 0 taken 1072 times.
✓ Branch 1 taken 144 times.
|
1216 | for (int j = 0; j < system_size; ++j) { |
| 42 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 928 times.
|
1072 | if (i == j) { |
| 43 | 144 | A_[i][j] = system_size + 1.0; | |
| 44 | } else { | ||
| 45 | 928 | A_[i][j] = 1.0 / (std::abs(i - j) + 1.0); | |
| 46 | } | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | b_.clear(); | ||
| 51 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | b_.resize(system_size); |
| 52 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 24 times.
|
168 | for (int i = 0; i < system_size; ++i) { |
| 53 | 144 | b_[i] = i + 1.0; | |
| 54 | } | ||
| 55 | |||
| 56 | x_.clear(); | ||
| 57 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | x_.resize(system_size, 0.0); |
| 58 | |||
| 59 | 24 | return true; | |
| 60 | ✗ | } catch (...) { | |
| 61 | return false; | ||
| 62 | ✗ | } | |
| 63 | } | ||
| 64 | |||
| 65 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | bool AfanasyevAItSeidelMethodSEQ::RunImpl() { |
| 66 | try { | ||
| 67 | 24 | int system_size = static_cast<int>(A_.size()); | |
| 68 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | if (system_size == 0) { |
| 69 | return false; | ||
| 70 | } | ||
| 71 | |||
| 72 |
1/2✓ Branch 0 taken 160 times.
✗ Branch 1 not taken.
|
160 | for (int iter = 0; iter < max_iterations_; ++iter) { |
| 73 | 160 | double max_diff = 0.0; | |
| 74 | |||
| 75 |
2/2✓ Branch 0 taken 984 times.
✓ Branch 1 taken 160 times.
|
1144 | for (int i = 0; i < system_size; ++i) { |
| 76 | 984 | double old_x = x_[i]; | |
| 77 | 984 | double sum = b_[i]; | |
| 78 | |||
| 79 |
2/2✓ Branch 0 taken 3224 times.
✓ Branch 1 taken 984 times.
|
4208 | for (int j = 0; j < i; ++j) { |
| 80 | 3224 | sum -= A_[i][j] * x_[j]; | |
| 81 | } | ||
| 82 | |||
| 83 |
2/2✓ Branch 0 taken 3224 times.
✓ Branch 1 taken 984 times.
|
4208 | for (int j = i + 1; j < system_size; ++j) { |
| 84 | 3224 | sum -= A_[i][j] * x_[j]; | |
| 85 | } | ||
| 86 | |||
| 87 | 984 | x_[i] = sum / A_[i][i]; | |
| 88 | 984 | max_diff = std::max(max_diff, std::abs(x_[i] - old_x)); | |
| 89 | } | ||
| 90 | |||
| 91 |
2/2✓ Branch 0 taken 136 times.
✓ Branch 1 taken 24 times.
|
160 | if (max_diff < epsilon_) { |
| 92 | break; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | OutType output; |
| 97 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | output.reserve(x_.size()); |
| 98 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 24 times.
|
168 | for (const auto &val : x_) { |
| 99 | output.push_back(val); | ||
| 100 | } | ||
| 101 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | GetOutput() = output; |
| 102 | |||
| 103 | return true; | ||
| 104 | ✗ | } catch (...) { | |
| 105 | return false; | ||
| 106 | ✗ | } | |
| 107 | } | ||
| 108 | |||
| 109 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | bool AfanasyevAItSeidelMethodSEQ::PostProcessingImpl() { |
| 110 | try { | ||
| 111 | 24 | int system_size = static_cast<int>(A_.size()); | |
| 112 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | if (x_.size() != static_cast<std::size_t>(system_size)) { |
| 113 | return false; | ||
| 114 | } | ||
| 115 | |||
| 116 | double residual_norm = 0.0; | ||
| 117 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 24 times.
|
168 | for (int i = 0; i < system_size; ++i) { |
| 118 | double sum = 0.0; | ||
| 119 |
2/2✓ Branch 0 taken 1072 times.
✓ Branch 1 taken 144 times.
|
1216 | for (int j = 0; j < system_size; ++j) { |
| 120 | 1072 | sum += A_[i][j] * x_[j]; | |
| 121 | } | ||
| 122 | 144 | residual_norm += std::abs(sum - b_[i]); | |
| 123 | } | ||
| 124 | |||
| 125 | 24 | residual_norm /= system_size; | |
| 126 | 24 | return residual_norm < epsilon_ * 1000; | |
| 127 | } catch (...) { | ||
| 128 | return false; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | } // namespace afanasyev_a_it_seidel_method | ||
| 133 |