| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "shilin_n_gauss_band_horizontal_scheme/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "shilin_n_gauss_band_horizontal_scheme/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace shilin_n_gauss_band_horizontal_scheme { | ||
| 11 | |||
| 12 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | ShilinNGaussBandHorizontalSchemeSEQ::ShilinNGaussBandHorizontalSchemeSEQ(const InType &in) { |
| 13 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 14 | InType &input_ref = GetInput(); | ||
| 15 | input_ref.clear(); | ||
| 16 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | input_ref.reserve(in.size()); |
| 17 |
2/2✓ Branch 0 taken 288 times.
✓ Branch 1 taken 48 times.
|
336 | for (const auto &row : in) { |
| 18 |
1/2✓ Branch 1 taken 288 times.
✗ Branch 2 not taken.
|
288 | input_ref.push_back(row); |
| 19 | } | ||
| 20 | 48 | GetOutput() = std::vector<double>(); | |
| 21 | 48 | } | |
| 22 | |||
| 23 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | bool ShilinNGaussBandHorizontalSchemeSEQ::ValidationImpl() { |
| 24 | const InType &input = GetInput(); | ||
| 25 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (input.empty()) { |
| 26 | return false; | ||
| 27 | } | ||
| 28 | |||
| 29 | size_t n = input.size(); | ||
| 30 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (n == 0) { |
| 31 | return false; | ||
| 32 | } | ||
| 33 | |||
| 34 | size_t cols = input[0].size(); | ||
| 35 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (cols < n + 1) { |
| 36 | return false; | ||
| 37 | } | ||
| 38 |
2/2✓ Branch 0 taken 240 times.
✓ Branch 1 taken 48 times.
|
288 | for (size_t i = 1; i < n; ++i) { |
| 39 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 240 times.
|
240 | if (input[i].size() != cols) { |
| 40 | return false; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | return true; | ||
| 45 | } | ||
| 46 | |||
| 47 | 48 | bool ShilinNGaussBandHorizontalSchemeSEQ::PreProcessingImpl() { | |
| 48 | 48 | GetOutput() = std::vector<double>(); | |
| 49 | 48 | return true; | |
| 50 | } | ||
| 51 | |||
| 52 | 48 | bool ShilinNGaussBandHorizontalSchemeSEQ::RunImpl() { | |
| 53 | 48 | InType augmented_matrix = GetInput(); | |
| 54 | size_t n = augmented_matrix.size(); | ||
| 55 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (n == 0) { |
| 56 | return false; | ||
| 57 | } | ||
| 58 | |||
| 59 | size_t cols = augmented_matrix[0].size(); | ||
| 60 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (cols < n + 1) { |
| 61 | return false; | ||
| 62 | } | ||
| 63 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
|
48 | if (!ForwardElimination(augmented_matrix, n, cols)) { |
| 64 | return false; | ||
| 65 | } | ||
| 66 | |||
| 67 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | std::vector<double> x = BackSubstitution(augmented_matrix, n, cols); |
| 68 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | GetOutput() = x; |
| 69 | return true; | ||
| 70 | 48 | } | |
| 71 | |||
| 72 | 48 | bool ShilinNGaussBandHorizontalSchemeSEQ::ForwardElimination(InType &augmented_matrix, size_t n, size_t cols) { | |
| 73 |
2/2✓ Branch 0 taken 288 times.
✓ Branch 1 taken 48 times.
|
336 | for (size_t k = 0; k < n; ++k) { |
| 74 | 288 | size_t max_row = FindPivotRow(augmented_matrix, k, n); | |
| 75 | |||
| 76 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 288 times.
|
288 | if (max_row != k) { |
| 77 | std::swap(augmented_matrix[k], augmented_matrix[max_row]); | ||
| 78 | } | ||
| 79 | |||
| 80 | // проверка на вырожденность матрицы | ||
| 81 |
1/2✓ Branch 0 taken 288 times.
✗ Branch 1 not taken.
|
288 | if (std::abs(augmented_matrix[k][k]) < 1e-10) { |
| 82 | return false; | ||
| 83 | } | ||
| 84 | |||
| 85 | 288 | EliminateColumn(augmented_matrix, k, n, cols); | |
| 86 | } | ||
| 87 | return true; | ||
| 88 | } | ||
| 89 | |||
| 90 | 288 | size_t ShilinNGaussBandHorizontalSchemeSEQ::FindPivotRow(const InType &augmented_matrix, size_t k, size_t n) { | |
| 91 | // поиск ведущего элемента для уменьшения ошибок округления | ||
| 92 | size_t max_row = k; | ||
| 93 | 288 | double max_val = std::abs(augmented_matrix[k][k]); | |
| 94 | |||
| 95 |
2/2✓ Branch 0 taken 856 times.
✓ Branch 1 taken 288 times.
|
1144 | for (size_t i = k + 1; i < n; ++i) { |
| 96 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 856 times.
|
856 | if (std::abs(augmented_matrix[i][k]) > max_val) { |
| 97 | max_val = std::abs(augmented_matrix[i][k]); | ||
| 98 | max_row = i; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | 288 | return max_row; | |
| 103 | } | ||
| 104 | |||
| 105 | 288 | void ShilinNGaussBandHorizontalSchemeSEQ::EliminateColumn(InType &augmented_matrix, size_t k, size_t n, size_t cols) { | |
| 106 |
2/2✓ Branch 0 taken 856 times.
✓ Branch 1 taken 288 times.
|
1144 | for (size_t i = k + 1; i < n; ++i) { |
| 107 |
2/2✓ Branch 0 taken 504 times.
✓ Branch 1 taken 352 times.
|
856 | if (std::abs(augmented_matrix[i][k]) > 1e-10) { |
| 108 | 504 | double factor = augmented_matrix[i][k] / augmented_matrix[k][k]; | |
| 109 |
2/2✓ Branch 0 taken 3152 times.
✓ Branch 1 taken 504 times.
|
3656 | for (size_t j = k; j < cols; ++j) { |
| 110 | 3152 | augmented_matrix[i][j] -= factor * augmented_matrix[k][j]; | |
| 111 | } | ||
| 112 | } | ||
| 113 | } | ||
| 114 | 288 | } | |
| 115 | |||
| 116 | 48 | std::vector<double> ShilinNGaussBandHorizontalSchemeSEQ::BackSubstitution(const InType &augmented_matrix, size_t n, | |
| 117 | size_t cols) { | ||
| 118 | // обратный ход метода гаусса | ||
| 119 | 48 | std::vector<double> x(n, 0.0); | |
| 120 |
2/2✓ Branch 0 taken 288 times.
✓ Branch 1 taken 48 times.
|
336 | for (int i = static_cast<int>(n) - 1; i >= 0; --i) { |
| 121 | double sum = 0.0; | ||
| 122 |
2/2✓ Branch 0 taken 856 times.
✓ Branch 1 taken 288 times.
|
1144 | for (size_t j = static_cast<size_t>(i) + 1; j < n; ++j) { |
| 123 | 856 | sum += augmented_matrix[static_cast<size_t>(i)][j] * x[j]; | |
| 124 | } | ||
| 125 | |||
| 126 | 288 | x[static_cast<size_t>(i)] = (augmented_matrix[static_cast<size_t>(i)][cols - 1] - sum) / | |
| 127 | augmented_matrix[static_cast<size_t>(i)][static_cast<size_t>(i)]; | ||
| 128 | } | ||
| 129 | 48 | return x; | |
| 130 | } | ||
| 131 | |||
| 132 | 48 | bool ShilinNGaussBandHorizontalSchemeSEQ::PostProcessingImpl() { | |
| 133 | 48 | return !GetOutput().empty(); | |
| 134 | } | ||
| 135 | |||
| 136 | } // namespace shilin_n_gauss_band_horizontal_scheme | ||
| 137 |