| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "morozov_n_siedels_method/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <tuple> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "morozov_n_siedels_method/common/include/common.hpp" | ||
| 10 | |||
| 11 | namespace morozov_n_siedels_method { | ||
| 12 | |||
| 13 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | MorozovNSiedelsMethodSEQ::MorozovNSiedelsMethodSEQ(const InType &in) { |
| 14 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 15 | GetInput() = in; | ||
| 16 | 24 | } | |
| 17 | |||
| 18 | 24 | bool MorozovNSiedelsMethodSEQ::ValidationImpl() { | |
| 19 | bool matrix_correct = false; | ||
| 20 | 24 | std::size_t n = std::get<0>(GetInput()); | |
| 21 | 24 | std::vector<double> a = std::get<1>(GetInput()); | |
| 22 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | std::vector<double> b = std::get<2>(GetInput()); |
| 23 |
2/4✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
|
24 | if ((a.size() == (n * n)) && (b.size() == n)) { |
| 24 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | int rank_a = CalcMatrixRank(n, n, a); |
| 25 | |||
| 26 | 24 | std::vector<double> ext_a; | |
| 27 |
2/2✓ Branch 0 taken 192 times.
✓ Branch 1 taken 24 times.
|
216 | for (std::size_t i = 0; i < n; i++) { |
| 28 |
2/2✓ Branch 0 taken 2304 times.
✓ Branch 1 taken 192 times.
|
2496 | for (std::size_t j = 0; j < n; j++) { |
| 29 |
2/2✓ Branch 0 taken 2152 times.
✓ Branch 1 taken 152 times.
|
2304 | ext_a.push_back(a[(i * n) + j]); |
| 30 | } | ||
| 31 | ext_a.push_back(b[i]); | ||
| 32 | } | ||
| 33 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | int rank_ext_a = CalcMatrixRank(n, n + 1, ext_a); |
| 34 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | if (rank_a >= rank_ext_a) { |
| 35 | matrix_correct = true; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | 24 | return matrix_correct; | |
| 39 | } | ||
| 40 | |||
| 41 | 24 | bool MorozovNSiedelsMethodSEQ::PreProcessingImpl() { | |
| 42 | 24 | return true; | |
| 43 | } | ||
| 44 | |||
| 45 | 24 | bool MorozovNSiedelsMethodSEQ::RunImpl() { | |
| 46 | 24 | std::size_t n = std::get<0>(GetInput()); | |
| 47 | std::vector<double> &a = std::get<1>(GetInput()); | ||
| 48 | std::vector<double> &b = std::get<2>(GetInput()); | ||
| 49 | 24 | double eps = std::get<3>(GetInput()); | |
| 50 | |||
| 51 | 24 | std::vector<double> x(n, 0); | |
| 52 |
1/4✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
24 | std::vector<double> iter_eps(n, -1); |
| 53 | bool complete = false; | ||
| 54 |
2/2✓ Branch 0 taken 168 times.
✓ Branch 1 taken 24 times.
|
192 | while (!complete) { |
| 55 |
2/2✓ Branch 0 taken 1440 times.
✓ Branch 1 taken 168 times.
|
1608 | for (std::size_t i = 0; i < n; i++) { |
| 56 | 1440 | double iter_x = b[i]; | |
| 57 |
2/2✓ Branch 0 taken 8304 times.
✓ Branch 1 taken 1440 times.
|
9744 | for (std::size_t j = 0; j < i; j++) { |
| 58 | 8304 | iter_x = iter_x - (a[(i * n) + j] * x[j]); | |
| 59 | } | ||
| 60 |
2/2✓ Branch 0 taken 8304 times.
✓ Branch 1 taken 1440 times.
|
9744 | for (std::size_t j = i + 1; j < n; j++) { |
| 61 | 8304 | iter_x = iter_x - (a[(i * n) + j] * x[j]); | |
| 62 | } | ||
| 63 | 1440 | iter_x = iter_x / a[(i * n) + i]; | |
| 64 | |||
| 65 | // обновление погрешности | ||
| 66 | 1440 | iter_eps[i] = std::fabs(iter_x - x[i]); | |
| 67 | // обновление полученного корня | ||
| 68 | 1440 | x[i] = iter_x; | |
| 69 | } | ||
| 70 | 168 | complete = !EpsOutOfBound(iter_eps, eps); | |
| 71 | } | ||
| 72 | |||
| 73 | // for (size_t i = 0; i < x.size(); i++) { | ||
| 74 | // std::cout << x[i] << " "; | ||
| 75 | // } | ||
| 76 | // std::cout << "\n"; | ||
| 77 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | GetOutput() = x; |
| 78 | 24 | return true; | |
| 79 | } | ||
| 80 | |||
| 81 | 24 | bool MorozovNSiedelsMethodSEQ::PostProcessingImpl() { | |
| 82 | 24 | return true; | |
| 83 | } | ||
| 84 | |||
| 85 | ✗ | bool MorozovNSiedelsMethodSEQ::EpsOutOfBound(std::vector<double> &iter_eps, double correct_eps) { | |
| 86 | 168 | double max_in_iter = *std::ranges::max_element(iter_eps); | |
| 87 | 168 | return max_in_iter > correct_eps; | |
| 88 | } | ||
| 89 | 48 | int MorozovNSiedelsMethodSEQ::CalcMatrixRank(std::size_t n, std::size_t m, std::vector<double> &a) { | |
| 90 | const double e = 1e-9; | ||
| 91 |
1/2✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
|
48 | std::vector<std::vector<double>> mat(n, std::vector<double>(m)); |
| 92 |
2/2✓ Branch 0 taken 384 times.
✓ Branch 1 taken 48 times.
|
432 | for (std::size_t i = 0; i < n; i++) { |
| 93 |
2/2✓ Branch 0 taken 4800 times.
✓ Branch 1 taken 384 times.
|
5184 | for (std::size_t j = 0; j < m; j++) { |
| 94 | 4800 | mat[i][j] = a[(i * n) + j]; | |
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | int rank = 0; | ||
| 99 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | std::vector<bool> row_selected(n, false); |
| 100 | |||
| 101 |
2/2✓ Branch 0 taken 384 times.
✓ Branch 1 taken 48 times.
|
432 | for (std::size_t col = 0; col < n; col++) { |
| 102 | 384 | std::size_t pivot_row = 0; | |
| 103 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 384 times.
|
384 | if (!GetPivotRow(&pivot_row, row_selected, col, mat, e)) { |
| 104 | ✗ | continue; | |
| 105 | } | ||
| 106 | 384 | rank++; | |
| 107 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 384 times.
|
384 | row_selected[pivot_row] = true; |
| 108 | // Нормализация строки и вычитание строки из других строк | ||
| 109 | //-> приведение к степнчатому виду | ||
| 110 | 384 | SubRow(pivot_row, col, mat, e); | |
| 111 | } | ||
| 112 | 48 | return rank; | |
| 113 | 48 | } | |
| 114 | |||
| 115 | 384 | bool MorozovNSiedelsMethodSEQ::GetPivotRow(std::size_t *pivot_row, std::vector<bool> &row_selected, std::size_t col, | |
| 116 | std::vector<std::vector<double>> &mat, double e) { | ||
| 117 |
1/2✓ Branch 0 taken 2496 times.
✗ Branch 1 not taken.
|
2496 | for (std::size_t row = 0; row < mat.size(); row++) { |
| 118 |
3/4✓ Branch 0 taken 384 times.
✓ Branch 1 taken 2112 times.
✓ Branch 2 taken 384 times.
✗ Branch 3 not taken.
|
2496 | if (!row_selected[row] && std::fabs(mat[row][col]) > e) { |
| 119 | 384 | *pivot_row = row; | |
| 120 | 384 | return true; | |
| 121 | } | ||
| 122 | } | ||
| 123 | return false; | ||
| 124 | } | ||
| 125 | |||
| 126 | 384 | void MorozovNSiedelsMethodSEQ::SubRow(std::size_t pivot_row, std::size_t col, std::vector<std::vector<double>> &mat, | |
| 127 | double e) { | ||
| 128 | std::size_t n = mat.size(); | ||
| 129 | std::size_t m = mat[0].size(); | ||
| 130 | // Нормализация строки | ||
| 131 | 384 | double pivot = mat[pivot_row][col]; | |
| 132 |
2/2✓ Branch 0 taken 2496 times.
✓ Branch 1 taken 384 times.
|
2880 | for (std::size_t j = col; j < n; j++) { |
| 133 | 2496 | mat[pivot_row][j] /= pivot; | |
| 134 | } | ||
| 135 | // Вычитание текущей строки из других строк | ||
| 136 |
2/2✓ Branch 0 taken 4608 times.
✓ Branch 1 taken 384 times.
|
4992 | for (std::size_t row = 0; row < n; row++) { |
| 137 |
3/4✓ Branch 0 taken 4224 times.
✓ Branch 1 taken 384 times.
✓ Branch 2 taken 4224 times.
✗ Branch 3 not taken.
|
4608 | if (row != pivot_row && std::fabs(mat[row][col]) > e) { |
| 138 | double factor = mat[row][col]; | ||
| 139 |
2/2✓ Branch 0 taken 35712 times.
✓ Branch 1 taken 4224 times.
|
39936 | for (std::size_t j = col; j < m; j++) { |
| 140 | 35712 | mat[row][j] -= factor * mat[pivot_row][j]; | |
| 141 | } | ||
| 142 | } | ||
| 143 | } | ||
| 144 | 384 | } | |
| 145 | |||
| 146 | } // namespace morozov_n_siedels_method | ||
| 147 |