| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "papulina_y_simple_iteration/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <cstdlib> | ||
| 7 | #include <iostream> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "papulina_y_simple_iteration/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace papulina_y_simple_iteration { | ||
| 13 | |||
| 14 |
1/2✓ Branch 1 taken 204 times.
✗ Branch 2 not taken.
|
204 | PapulinaYSimpleIterationSEQ::PapulinaYSimpleIterationSEQ(const InType &in) { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 | GetInput() = in; | ||
| 17 | ✗ | GetOutput() = std::vector<double>(0); | |
| 18 | 204 | } | |
| 19 | 172 | bool PapulinaYSimpleIterationSEQ::ValidationImpl() { | |
| 20 |
2/2✓ Branch 0 taken 164 times.
✓ Branch 1 taken 8 times.
|
172 | size_t n = std::get<0>(GetInput()); |
| 21 | const auto &a_matrix = std::get<1>(GetInput()); | ||
| 22 |
6/6✓ Branch 0 taken 164 times.
✓ Branch 1 taken 8 times.
✓ Branch 3 taken 146 times.
✓ Branch 4 taken 18 times.
✓ Branch 5 taken 18 times.
✓ Branch 6 taken 128 times.
|
172 | if ((n < 1) || (!DetermChecking(a_matrix, n)) || (!DiagonalDominance(a_matrix, n))) { |
| 23 | 44 | return false; | |
| 24 | } | ||
| 25 | 128 | double norm_b = CalculateNormB(a_matrix, n); | |
| 26 | |||
| 27 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
|
128 | if (norm_b >= 1.0) { |
| 28 | ✗ | std::cout << "WARNING: sufficient condition for convergence may not hold (norm_b = " << norm_b << " >= 1)\n"; | |
| 29 | } | ||
| 30 | return true; | ||
| 31 | } | ||
| 32 | 128 | double PapulinaYSimpleIterationSEQ::CalculateNormB(const std::vector<double> &a, size_t n) { | |
| 33 | 128 | double max_row_sum = 0.0; | |
| 34 | |||
| 35 |
2/2✓ Branch 0 taken 536 times.
✓ Branch 1 taken 128 times.
|
664 | for (size_t i = 0; i < n; i++) { |
| 36 | 536 | double diag = a[(i * n) + i]; | |
| 37 | 536 | double row_sum = 0.0; | |
| 38 | |||
| 39 |
2/2✓ Branch 0 taken 2808 times.
✓ Branch 1 taken 536 times.
|
3344 | for (size_t j = 0; j < n; j++) { |
| 40 |
2/2✓ Branch 0 taken 2272 times.
✓ Branch 1 taken 536 times.
|
2808 | if (j != i) { |
| 41 | 2272 | row_sum += std::abs(a[(i * n) + j] / diag); | |
| 42 | } | ||
| 43 | } | ||
| 44 | 536 | max_row_sum = std::max(row_sum, max_row_sum); | |
| 45 | } | ||
| 46 | |||
| 47 | 128 | return max_row_sum; | |
| 48 | } | ||
| 49 | 128 | bool PapulinaYSimpleIterationSEQ::PreProcessingImpl() { | |
| 50 | 128 | n_ = static_cast<size_t>(std::get<0>(GetInput())); | |
| 51 | 128 | A_.assign(n_ * n_, 0.0); | |
| 52 |
2/2✓ Branch 1 taken 120 times.
✓ Branch 2 taken 8 times.
|
128 | b_.assign(n_, 0.0); |
| 53 | // copying input data | ||
| 54 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 8 times.
|
128 | std::copy(std::get<1>(GetInput()).data(), std::get<1>(GetInput()).data() + (n_ * n_), A_.data()); |
| 55 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 8 times.
|
128 | std::copy(std::get<2>(GetInput()).data(), std::get<2>(GetInput()).data() + n_, b_.data()); |
| 56 | 128 | return true; | |
| 57 | } | ||
| 58 | 162 | bool PapulinaYSimpleIterationSEQ::DiagonalDominance(const std::vector<double> &a_matrix, const size_t &n) { | |
| 59 | bool flag = true; | ||
| 60 |
2/2✓ Branch 0 taken 588 times.
✓ Branch 1 taken 128 times.
|
716 | for (size_t i = 0; i < n; i++) { |
| 61 | double sum = 0.0; | ||
| 62 |
2/2✓ Branch 0 taken 3004 times.
✓ Branch 1 taken 588 times.
|
3592 | for (size_t j = 0; j < n; j++) { |
| 63 |
2/2✓ Branch 0 taken 2416 times.
✓ Branch 1 taken 588 times.
|
3004 | if (j != i) { |
| 64 | 2416 | sum += abs(a_matrix[(i * n) + j]); | |
| 65 | } | ||
| 66 | } | ||
| 67 |
2/2✓ Branch 0 taken 554 times.
✓ Branch 1 taken 34 times.
|
588 | if (sum > abs(a_matrix[(i * n) + i])) { |
| 68 | flag = false; | ||
| 69 | break; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | 162 | return flag; | |
| 73 | } | ||
| 74 | 16 | bool PapulinaYSimpleIterationSEQ::GetDetermCheckingResult(const std::vector<double> &a_matrix, const size_t &n) { | |
| 75 | 16 | return DetermChecking(a_matrix, n); | |
| 76 | } | ||
| 77 | 16 | bool PapulinaYSimpleIterationSEQ::GetDiagonalDominanceResult(const std::vector<double> &a_matrix, const size_t &n) { | |
| 78 | 16 | return DiagonalDominance(a_matrix, n); | |
| 79 | } | ||
| 80 | 180 | bool PapulinaYSimpleIterationSEQ::DetermChecking(const std::vector<double> &a, const size_t &n) { | |
| 81 | 180 | std::vector<double> tmp = a; | |
| 82 | |||
| 83 |
2/2✓ Branch 0 taken 686 times.
✓ Branch 1 taken 146 times.
|
832 | for (size_t i = 0; i < n; i++) { |
| 84 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 636 times.
|
686 | if (std::fabs(tmp[(i * n) + i]) < 1e-10) { // проверка и замена нулевого диагонального элемента |
| 85 |
2/2✓ Branch 1 taken 34 times.
✓ Branch 2 taken 16 times.
|
50 | if (!FindAndSwapRow(tmp, i, n)) { |
| 86 |
1/2✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
|
34 | std::cout << "Determinant is zero\n"; |
| 87 | return false; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | 652 | double pivot = tmp[(i * n) + i]; | |
| 91 |
2/2✓ Branch 0 taken 1978 times.
✓ Branch 1 taken 652 times.
|
2630 | for (size_t j = i; j < n; j++) { // нормализация текущей строки |
| 92 | 1978 | tmp[(i * n) + j] /= pivot; | |
| 93 | } | ||
| 94 |
2/2✓ Branch 0 taken 1326 times.
✓ Branch 1 taken 652 times.
|
1978 | for (size_t j = i + 1; j < n; j++) { // вычитание из остальных строк (зануление) |
| 95 | 1326 | double factor = tmp[(j * n) + i]; | |
| 96 |
2/2✓ Branch 0 taken 6516 times.
✓ Branch 1 taken 1326 times.
|
7842 | for (size_t k = i; k < n; k++) { |
| 97 | 6516 | tmp[(j * n) + k] -= tmp[(i * n) + k] * factor; | |
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | return true; | ||
| 103 | } | ||
| 104 | 50 | bool PapulinaYSimpleIterationSEQ::FindAndSwapRow(std::vector<double> &tmp, size_t i, | |
| 105 | size_t n) { // вспомогательная функция для поиска и замены строки | ||
| 106 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 34 times.
|
66 | for (size_t j = i + 1; j < n; j++) { |
| 107 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
|
32 | if (std::fabs(tmp[(j * n) + i]) > 1e-10) { |
| 108 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 16 times.
|
64 | for (size_t k = i; k < n; k++) { |
| 109 | 48 | std::swap(tmp[(i * n) + k], tmp[(j * n) + k]); | |
| 110 | } | ||
| 111 | return true; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | return false; | ||
| 115 | } | ||
| 116 | 128 | bool PapulinaYSimpleIterationSEQ::RunImpl() { | |
| 117 | 128 | std::vector<double> b_matrix(n_ * n_, 0.0); | |
| 118 |
1/4✓ Branch 1 taken 128 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
128 | std::vector<double> d(n_, 0.0); |
| 119 | // преобразование из Ax=b -> x =d+Bx | ||
| 120 |
2/2✓ Branch 0 taken 536 times.
✓ Branch 1 taken 128 times.
|
664 | for (size_t i = 0; i < n_; i++) { |
| 121 |
2/2✓ Branch 0 taken 2808 times.
✓ Branch 1 taken 536 times.
|
3344 | for (size_t j = 0; j < n_; j++) { |
| 122 |
2/2✓ Branch 0 taken 2272 times.
✓ Branch 1 taken 536 times.
|
2808 | if (i != j) { |
| 123 | 2272 | b_matrix[(i * n_) + j] = -A_[(i * n_) + j] / A_[(i * n_) + i]; | |
| 124 | } | ||
| 125 | } | ||
| 126 | 536 | d[i] = b_[i] / A_[(i * n_) + i]; | |
| 127 | } | ||
| 128 |
1/2✓ Branch 1 taken 128 times.
✗ Branch 2 not taken.
|
128 | std::vector<double> x = d; |
| 129 |
1/2✓ Branch 0 taken 1992 times.
✗ Branch 1 not taken.
|
1992 | for (size_t step = 0; step < steps_count_; step++) { |
| 130 |
1/4✓ Branch 1 taken 1992 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1992 | std::vector<double> x_new(n_, 0.0); |
| 131 | 1992 | ComputeNewX(b_matrix, d, x, x_new, n_); | |
| 132 | double sum_sq = 0.0; | ||
| 133 |
2/2✓ Branch 0 taken 10560 times.
✓ Branch 1 taken 1992 times.
|
12552 | for (size_t i = 0; i < n_; i++) { |
| 134 | 10560 | double diff = x_new[i] - x[i]; | |
| 135 | 10560 | sum_sq += diff * diff; | |
| 136 | } | ||
| 137 | 1992 | double diff = std::sqrt(sum_sq); | |
| 138 | |||
| 139 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 1864 times.
|
1992 | if (diff < eps_) { // сравниваем норму разницы |
| 140 |
1/2✓ Branch 1 taken 128 times.
✗ Branch 2 not taken.
|
128 | GetOutput() = x_new; |
| 141 | return true; | ||
| 142 | } | ||
| 143 |
1/2✓ Branch 1 taken 1864 times.
✗ Branch 2 not taken.
|
1864 | x = x_new; |
| 144 | } | ||
| 145 | ✗ | std::cout << "Result isn't found\n"; | |
| 146 | return false; | ||
| 147 | } | ||
| 148 | 128 | bool PapulinaYSimpleIterationSEQ::PostProcessingImpl() { | |
| 149 | 128 | return true; | |
| 150 | } | ||
| 151 | 1992 | void PapulinaYSimpleIterationSEQ::ComputeNewX(const std::vector<double> &b_matrix, const std::vector<double> &d, | |
| 152 | const std::vector<double> &x, std::vector<double> &x_new, size_t n) { | ||
| 153 |
2/2✓ Branch 0 taken 10560 times.
✓ Branch 1 taken 1992 times.
|
12552 | for (size_t i = 0; i < n; i++) { |
| 154 | double sum = 0.0; | ||
| 155 |
2/2✓ Branch 0 taken 68208 times.
✓ Branch 1 taken 10560 times.
|
78768 | for (size_t j = 0; j < n; j++) { |
| 156 | 68208 | sum += b_matrix[(i * n) + j] * x[j]; | |
| 157 | } | ||
| 158 | 10560 | x_new[i] = sum + d[i]; | |
| 159 | } | ||
| 160 | 1992 | } | |
| 161 | } // namespace papulina_y_simple_iteration | ||
| 162 |