| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "smetanin_d_gauss_vert_sch/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <limits> | ||
| 7 | #include <utility> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "smetanin_d_gauss_vert_sch/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace smetanin_d_gauss_vert_sch { | ||
| 13 | |||
| 14 | namespace { | ||
| 15 | |||
| 16 | double &At(std::vector<double> &data, int n, int row, int col) { | ||
| 17 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 216 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 216 times.
|
1168 | return data[(static_cast<std::size_t>(row) * static_cast<std::size_t>(n + 1)) + static_cast<std::size_t>(col)]; |
| 18 | } | ||
| 19 | |||
| 20 | const double &At(const std::vector<double> &data, int n, int row, int col) { | ||
| 21 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
|
472 | return data[(static_cast<std::size_t>(row) * static_cast<std::size_t>(n + 1)) + static_cast<std::size_t>(col)]; |
| 22 | } | ||
| 23 | |||
| 24 | 128 | bool FindAndSwapPivot(std::vector<double> &a, int n, int i, int bw, double eps) { | |
| 25 | int max_row = i; | ||
| 26 | 128 | double max_val = std::abs(At(a, n, i, i)); | |
| 27 | 128 | const int pivot_row_end = std::min(n - 1, i + bw); | |
| 28 |
2/2✓ Branch 0 taken 216 times.
✓ Branch 1 taken 128 times.
|
344 | for (int ri = i + 1; ri <= pivot_row_end; ++ri) { |
| 29 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 216 times.
|
216 | const double val = std::abs(At(a, n, ri, i)); |
| 30 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 216 times.
|
216 | if (val > max_val) { |
| 31 | max_val = val; | ||
| 32 | max_row = ri; | ||
| 33 | } | ||
| 34 | } | ||
| 35 |
1/2✓ Branch 0 taken 128 times.
✗ Branch 1 not taken.
|
128 | if (max_val <= eps) { |
| 36 | return false; | ||
| 37 | } | ||
| 38 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
|
128 | if (max_row != i) { |
| 39 | const int col_swap_end = std::min(n - 1, i + bw); | ||
| 40 | ✗ | for (int cj = i; cj <= col_swap_end; ++cj) { | |
| 41 | std::swap(At(a, n, i, cj), At(a, n, max_row, cj)); | ||
| 42 | } | ||
| 43 | std::swap(At(a, n, i, n), At(a, n, max_row, n)); | ||
| 44 | } | ||
| 45 | return true; | ||
| 46 | } | ||
| 47 | |||
| 48 | 128 | void EliminateBelow(std::vector<double> &a, int n, int i, int bw, double eps) { | |
| 49 |
2/2✓ Branch 0 taken 216 times.
✓ Branch 1 taken 128 times.
|
344 | for (int ri = i + 1; ri <= std::min(n - 1, i + bw); ++ri) { |
| 50 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 216 times.
|
216 | const double factor = At(a, n, ri, i) / At(a, n, i, i); |
| 51 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 216 times.
|
216 | if (std::abs(factor) <= eps) { |
| 52 | ✗ | continue; | |
| 53 | } | ||
| 54 | 216 | At(a, n, ri, i) = 0.0; | |
| 55 | const int col_end = std::min(n - 1, i + bw); | ||
| 56 |
2/2✓ Branch 0 taken 520 times.
✓ Branch 1 taken 216 times.
|
736 | for (int cj = i + 1; cj <= col_end; ++cj) { |
| 57 | 520 | At(a, n, ri, cj) -= factor * At(a, n, i, cj); | |
| 58 | } | ||
| 59 | 216 | At(a, n, ri, n) -= factor * At(a, n, i, n); | |
| 60 | } | ||
| 61 | 128 | } | |
| 62 | |||
| 63 | 24 | OutType BackSubstitute(const std::vector<double> &a, int n, int bw, double eps) { | |
| 64 | 24 | OutType x(static_cast<std::size_t>(n), 0.0); | |
| 65 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 24 times.
|
152 | for (int i = n - 1; i >= 0; --i) { |
| 66 | 128 | double sum = At(a, n, i, n); | |
| 67 | 128 | const int col_end = std::min(n - 1, i + bw); | |
| 68 |
2/2✓ Branch 0 taken 216 times.
✓ Branch 1 taken 128 times.
|
344 | for (int cj = i + 1; cj <= col_end; ++cj) { |
| 69 | 216 | sum -= At(a, n, i, cj) * x[static_cast<std::size_t>(cj)]; | |
| 70 | } | ||
| 71 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
|
128 | const double diag = At(a, n, i, i); |
| 72 | if (std::abs(diag) <= eps) { | ||
| 73 | ✗ | return OutType{}; | |
| 74 | } | ||
| 75 | 128 | x[static_cast<std::size_t>(i)] = sum / diag; | |
| 76 | } | ||
| 77 | return x; | ||
| 78 | } | ||
| 79 | |||
| 80 | } // namespace | ||
| 81 | |||
| 82 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | SmetaninDGaussVertSchSEQ::SmetaninDGaussVertSchSEQ(const InType &in) { |
| 83 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 84 | GetInput() = in; | ||
| 85 | GetOutput().clear(); | ||
| 86 | 24 | } | |
| 87 | |||
| 88 | 24 | bool SmetaninDGaussVertSchSEQ::ValidationImpl() { | |
| 89 | const InType &input = GetInput(); | ||
| 90 | const OutType &output = GetOutput(); | ||
| 91 | |||
| 92 |
3/6✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
|
24 | if (input.n <= 0 || input.bandwidth < 0 || input.bandwidth >= input.n) { |
| 93 | return false; | ||
| 94 | } | ||
| 95 | |||
| 96 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | const std::size_t expected_size = static_cast<std::size_t>(input.n) * static_cast<std::size_t>(input.n + 1); |
| 97 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | if (input.augmented_matrix.size() != expected_size) { |
| 98 | return false; | ||
| 99 | } | ||
| 100 | |||
| 101 | 24 | return output.empty(); | |
| 102 | } | ||
| 103 | |||
| 104 | 24 | bool SmetaninDGaussVertSchSEQ::PreProcessingImpl() { | |
| 105 | 24 | return true; | |
| 106 | } | ||
| 107 | |||
| 108 | 24 | bool SmetaninDGaussVertSchSEQ::RunImpl() { | |
| 109 | const InType &input = GetInput(); | ||
| 110 | 24 | const int n = input.n; | |
| 111 | 24 | const int bw = input.bandwidth; | |
| 112 | |||
| 113 | 24 | std::vector<double> a = input.augmented_matrix; | |
| 114 | |||
| 115 | const double eps = std::numeric_limits<double>::epsilon() * 100.0; | ||
| 116 | |||
| 117 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 24 times.
|
152 | for (int i = 0; i < n; ++i) { |
| 118 |
1/2✓ Branch 1 taken 128 times.
✗ Branch 2 not taken.
|
128 | if (!FindAndSwapPivot(a, n, i, bw, eps)) { |
| 119 | return false; | ||
| 120 | } | ||
| 121 | 128 | EliminateBelow(a, n, i, bw, eps); | |
| 122 | } | ||
| 123 | |||
| 124 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | OutType x = BackSubstitute(a, n, bw, eps); |
| 125 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | if (x.empty()) { |
| 126 | return false; | ||
| 127 | } | ||
| 128 | GetOutput() = std::move(x); | ||
| 129 | 24 | return true; | |
| 130 | } | ||
| 131 | |||
| 132 | 24 | bool SmetaninDGaussVertSchSEQ::PostProcessingImpl() { | |
| 133 | 24 | return !GetOutput().empty(); | |
| 134 | } | ||
| 135 | |||
| 136 | } // namespace smetanin_d_gauss_vert_sch | ||
| 137 |