| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "klimenko_v_seidel_method/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <climits> | ||
| 4 | #include <cmath> | ||
| 5 | #include <numeric> | ||
| 6 | #include <random> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "klimenko_v_seidel_method/common/include/common.hpp" | ||
| 10 | |||
| 11 | namespace klimenko_v_seidel_method { | ||
| 12 | |||
| 13 | 80 | KlimenkoVSeidelMethodSEQ::KlimenkoVSeidelMethodSEQ(const InType &in) { | |
| 14 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 15 | 80 | GetInput() = in; | |
| 16 | GetOutput() = 0; | ||
| 17 | 80 | } | |
| 18 | |||
| 19 | 80 | bool KlimenkoVSeidelMethodSEQ::ValidationImpl() { | |
| 20 | 80 | n_ = GetInput(); | |
| 21 | 80 | return n_ > 0; | |
| 22 | } | ||
| 23 | |||
| 24 | 80 | bool KlimenkoVSeidelMethodSEQ::PreProcessingImpl() { | |
| 25 | 80 | return true; | |
| 26 | } | ||
| 27 | |||
| 28 | 80 | bool KlimenkoVSeidelMethodSEQ::RunImpl() { | |
| 29 | 80 | GenerateRandomMatrix(n_, A_, b_); | |
| 30 | |||
| 31 | 80 | ComputeRightHandSide(n_, A_, b_); | |
| 32 | |||
| 33 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | if (!CheckDiagonalElements(n_, A_)) { |
| 34 | return false; | ||
| 35 | } | ||
| 36 | |||
| 37 | 80 | epsilon_ = 1e-6; | |
| 38 | 80 | max_iterations_ = 10000; | |
| 39 | 80 | x_.assign(n_, 0.0); | |
| 40 | |||
| 41 | // Итерационный процесс | ||
| 42 | int iteration = 0; | ||
| 43 | bool converged = false; | ||
| 44 | |||
| 45 |
3/4✓ Branch 0 taken 1010 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 930 times.
✓ Branch 3 taken 80 times.
|
1010 | while (iteration < max_iterations_ && !converged) { |
| 46 | // Выполнение одной итерации метода Зейделя | ||
| 47 | 930 | double diff_sq = PerformSeidelIteration(n_, A_, b_, x_); | |
| 48 | |||
| 49 | // Проверка сходимости | ||
| 50 | 930 | double diff_norm = std::sqrt(diff_sq); | |
| 51 | 930 | converged = (diff_norm < epsilon_); | |
| 52 | |||
| 53 | 930 | ++iteration; | |
| 54 | } | ||
| 55 | |||
| 56 | // Вычисление результата | ||
| 57 | double sum = std::accumulate(x_.begin(), x_.end(), 0.0); | ||
| 58 | 80 | GetOutput() = static_cast<int>(std::round(sum)); | |
| 59 | 80 | return true; | |
| 60 | } | ||
| 61 | |||
| 62 | 80 | bool KlimenkoVSeidelMethodSEQ::PostProcessingImpl() { | |
| 63 | 80 | return true; | |
| 64 | } | ||
| 65 | |||
| 66 | 80 | void KlimenkoVSeidelMethodSEQ::GenerateRandomMatrix(int size, std::vector<std::vector<double>> &matrix, | |
| 67 | std::vector<double> &vector) { | ||
| 68 | 80 | matrix.resize(size); | |
| 69 |
2/2✓ Branch 0 taken 1144 times.
✓ Branch 1 taken 80 times.
|
1224 | for (int i = 0; i < size; ++i) { |
| 70 | 1144 | matrix[i].assign(size, 0.0); | |
| 71 | } | ||
| 72 | 80 | vector.resize(size, 0.0); | |
| 73 | |||
| 74 | 80 | std::random_device rd; | |
| 75 | 80 | std::mt19937 gen(rd()); | |
| 76 | std::uniform_int_distribution<> dist(1, 10); | ||
| 77 | std::uniform_int_distribution<> dist_diag(1, 5); | ||
| 78 | |||
| 79 |
2/2✓ Branch 0 taken 1144 times.
✓ Branch 1 taken 80 times.
|
1224 | for (int i = 0; i < size; ++i) { |
| 80 | double row_sum = 0.0; | ||
| 81 |
2/2✓ Branch 0 taken 33704 times.
✓ Branch 1 taken 1144 times.
|
34848 | for (int j = 0; j < size; ++j) { |
| 82 |
2/2✓ Branch 0 taken 32560 times.
✓ Branch 1 taken 1144 times.
|
33704 | if (i != j) { |
| 83 | 32560 | matrix[i][j] = static_cast<double>(dist(gen)); | |
| 84 | 32560 | row_sum += std::abs(matrix[i][j]); | |
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | 1144 | matrix[i][i] = row_sum + static_cast<double>(dist_diag(gen)); | |
| 89 | } | ||
| 90 | 80 | } | |
| 91 | |||
| 92 | 80 | void KlimenkoVSeidelMethodSEQ::ComputeRightHandSide(int n, const std::vector<std::vector<double>> &a, | |
| 93 | std::vector<double> &b) { | ||
| 94 | 80 | std::vector<double> x_exact(n, 1.0); | |
| 95 |
1/4✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
80 | b.assign(n, 0.0); |
| 96 | |||
| 97 |
2/2✓ Branch 0 taken 1144 times.
✓ Branch 1 taken 80 times.
|
1224 | for (int i = 0; i < n; ++i) { |
| 98 |
2/2✓ Branch 0 taken 33704 times.
✓ Branch 1 taken 1144 times.
|
34848 | for (int j = 0; j < n; ++j) { |
| 99 | 33704 | b[i] += a[i][j] * x_exact[j]; | |
| 100 | } | ||
| 101 | } | ||
| 102 | 80 | } | |
| 103 | |||
| 104 | ✗ | bool KlimenkoVSeidelMethodSEQ::CheckDiagonalElements(int n, const std::vector<std::vector<double>> &a) { | |
| 105 |
2/4✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 1144 times.
✓ Branch 3 taken 80 times.
|
1224 | for (int i = 0; i < n; ++i) { |
| 106 |
1/4✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 1144 times.
✗ Branch 3 not taken.
|
1144 | if (std::abs(a[i][i]) < 1e-12) { |
| 107 | return false; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | return true; | ||
| 111 | } | ||
| 112 | |||
| 113 | 930 | double KlimenkoVSeidelMethodSEQ::PerformSeidelIteration(int n, const std::vector<std::vector<double>> &a, | |
| 114 | const std::vector<double> &b, std::vector<double> &x) { | ||
| 115 | double diff_sq = 0.0; | ||
| 116 | |||
| 117 |
2/2✓ Branch 0 taken 13695 times.
✓ Branch 1 taken 930 times.
|
14625 | for (int i = 0; i < n; ++i) { |
| 118 | 13695 | double old = x[i]; | |
| 119 | double sum_off_diag = 0.0; | ||
| 120 | |||
| 121 |
2/2✓ Branch 0 taken 403553 times.
✓ Branch 1 taken 13695 times.
|
417248 | for (int j = 0; j < n; ++j) { |
| 122 |
2/2✓ Branch 0 taken 389858 times.
✓ Branch 1 taken 13695 times.
|
403553 | if (i != j) { |
| 123 | 389858 | sum_off_diag += a[i][j] * x[j]; | |
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | 13695 | x[i] = (b[i] - sum_off_diag) / a[i][i]; | |
| 128 | 13695 | double diff = x[i] - old; | |
| 129 | 13695 | diff_sq += diff * diff; | |
| 130 | } | ||
| 131 | |||
| 132 | 930 | return diff_sq; | |
| 133 | } | ||
| 134 | |||
| 135 | } // namespace klimenko_v_seidel_method | ||
| 136 |