| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "zaharov_g_seidel_int_met/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <limits> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "util/include/util.hpp" | ||
| 10 | #include "zaharov_g_seidel_int_met/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace zaharov_g_seidel_int_met { | ||
| 13 | |||
| 14 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | ZaharovGSeidelIntMetSEQ::ZaharovGSeidelIntMetSEQ(const InType &in) { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | GetInput() = in; |
| 17 | 48 | GetOutput() = OutType(); | |
| 18 | 48 | } | |
| 19 | |||
| 20 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | bool ZaharovGSeidelIntMetSEQ::ValidationImpl() { |
| 21 | const InType &input = GetInput(); | ||
| 22 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | if (input.size() < 3) { |
| 23 | return false; | ||
| 24 | } | ||
| 25 | |||
| 26 |
3/6✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 48 times.
✗ Branch 5 not taken.
|
48 | if (input[0] <= 0 || input[1] <= 0.0 || input[2] <= 0) { |
| 27 | return false; | ||
| 28 | } | ||
| 29 | |||
| 30 | 48 | int system_size = static_cast<int>(input[0]); | |
| 31 | 48 | int max_iterations = static_cast<int>(input[2]); | |
| 32 | |||
| 33 |
2/4✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 48 times.
|
48 | if (static_cast<double>(system_size) != input[0] || static_cast<double>(max_iterations) != input[2]) { |
| 34 | ✗ | return false; | |
| 35 | } | ||
| 36 | |||
| 37 | return true; | ||
| 38 | } | ||
| 39 | |||
| 40 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | bool ZaharovGSeidelIntMetSEQ::PreProcessingImpl() { |
| 41 | try { | ||
| 42 | 48 | const int system_size = static_cast<int>(GetInput()[0]); | |
| 43 | 48 | epsilon_ = GetInput()[1]; | |
| 44 | 48 | max_iterations_ = static_cast<int>(GetInput()[2]); | |
| 45 | |||
| 46 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | A_.resize(system_size); |
| 47 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | b_.resize(system_size); |
| 48 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | x_.assign(system_size, 0.0); |
| 49 | |||
| 50 |
2/2✓ Branch 0 taken 1760 times.
✓ Branch 1 taken 48 times.
|
1808 | for (int i = 0; i < system_size; ++i) { |
| 51 |
1/2✓ Branch 1 taken 1760 times.
✗ Branch 2 not taken.
|
1760 | A_[i].resize(system_size); |
| 52 | double *row = A_[i].data(); | ||
| 53 | |||
| 54 | 1760 | row[i] = system_size + 1.0; | |
| 55 | |||
| 56 |
2/2✓ Branch 0 taken 55120 times.
✓ Branch 1 taken 1760 times.
|
56880 | for (int j = 0; j < i; ++j) { |
| 57 | 55120 | row[j] = 1.0 / (i - j + 1.0); | |
| 58 | } | ||
| 59 | |||
| 60 |
2/2✓ Branch 0 taken 55120 times.
✓ Branch 1 taken 1760 times.
|
56880 | for (int j = i + 1; j < system_size; ++j) { |
| 61 | 55120 | row[j] = 1.0 / (j - i + 1.0); | |
| 62 | } | ||
| 63 | |||
| 64 | 1760 | b_[i] = static_cast<double>(i + 1); | |
| 65 | } | ||
| 66 | |||
| 67 | return true; | ||
| 68 | ✗ | } catch (...) { | |
| 69 | return false; | ||
| 70 | ✗ | } | |
| 71 | } | ||
| 72 | |||
| 73 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | bool ZaharovGSeidelIntMetSEQ::RunImpl() { |
| 74 | try { | ||
| 75 | 48 | const int system_size = static_cast<int>(A_.size()); | |
| 76 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | if (system_size == 0) { |
| 77 | return false; | ||
| 78 | } | ||
| 79 | |||
| 80 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | std::vector<double> old_x(system_size); |
| 81 | |||
| 82 |
1/2✓ Branch 0 taken 296 times.
✗ Branch 1 not taken.
|
296 | for (int iter = 0; iter < max_iterations_; ++iter) { |
| 83 | 296 | std::copy(x_.begin(), x_.end(), old_x.begin()); | |
| 84 | double max_diff = 0.0; | ||
| 85 | |||
| 86 |
2/2✓ Branch 0 taken 9840 times.
✓ Branch 1 taken 296 times.
|
10136 | for (int i = 0; i < system_size; ++i) { |
| 87 | 9840 | double sum = b_[i]; | |
| 88 | |||
| 89 |
2/2✓ Branch 0 taken 290680 times.
✓ Branch 1 taken 9840 times.
|
300520 | for (int j = 0; j < i; ++j) { |
| 90 | 290680 | sum -= A_[i][j] * x_[j]; | |
| 91 | } | ||
| 92 | |||
| 93 |
2/2✓ Branch 0 taken 290680 times.
✓ Branch 1 taken 9840 times.
|
300520 | for (int j = i + 1; j < system_size; ++j) { |
| 94 | 290680 | sum -= A_[i][j] * old_x[j]; | |
| 95 | } | ||
| 96 | |||
| 97 | 9840 | x_[i] = sum / A_[i][i]; | |
| 98 | 9840 | const double diff = std::abs(x_[i] - old_x[i]); | |
| 99 | if (diff > max_diff) { | ||
| 100 | max_diff = diff; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 |
2/2✓ Branch 0 taken 248 times.
✓ Branch 1 taken 48 times.
|
296 | if (max_diff < epsilon_) { |
| 105 | break; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 |
2/6✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 48 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
96 | GetOutput() = OutType(x_.begin(), x_.end()); |
| 110 | return true; | ||
| 111 | ✗ | } catch (...) { | |
| 112 | return false; | ||
| 113 | ✗ | } | |
| 114 | } | ||
| 115 | |||
| 116 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | bool ZaharovGSeidelIntMetSEQ::PostProcessingImpl() { |
| 117 | try { | ||
| 118 | 48 | const int system_size = static_cast<int>(A_.size()); | |
| 119 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | if (x_.size() != static_cast<std::size_t>(system_size)) { |
| 120 | return false; | ||
| 121 | } | ||
| 122 | |||
| 123 | double residual_norm = 0.0; | ||
| 124 |
2/2✓ Branch 0 taken 1760 times.
✓ Branch 1 taken 48 times.
|
1808 | for (int i = 0; i < system_size; ++i) { |
| 125 | double sum = 0.0; | ||
| 126 | |||
| 127 | 1760 | const auto &row = A_[i]; | |
| 128 |
2/2✓ Branch 0 taken 112000 times.
✓ Branch 1 taken 1760 times.
|
113760 | for (int j = 0; j < system_size; ++j) { |
| 129 | 112000 | sum += row[j] * x_[j]; | |
| 130 | } | ||
| 131 | |||
| 132 | 1760 | residual_norm += std::abs(sum - b_[i]); | |
| 133 | } | ||
| 134 | |||
| 135 | double b_norm = 0.0; | ||
| 136 |
2/2✓ Branch 0 taken 1760 times.
✓ Branch 1 taken 48 times.
|
1808 | for (const auto &bi : b_) { |
| 137 | 1760 | b_norm += std::abs(bi); | |
| 138 | } | ||
| 139 | |||
| 140 | 48 | return (residual_norm / (b_norm + std::numeric_limits<double>::epsilon())) < epsilon_; | |
| 141 | } catch (...) { | ||
| 142 | return false; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | } // namespace zaharov_g_seidel_int_met | ||
| 147 |