| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "gaivoronskiy_m_gauss_jordan/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "gaivoronskiy_m_gauss_jordan/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace gaivoronskiy_m_gauss_jordan { | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | |||
| 14 | bool IsZero(double value) { | ||
| 15 |
10/12✓ Branch 0 taken 352 times.
✓ Branch 1 taken 336 times.
✓ Branch 2 taken 336 times.
✓ Branch 3 taken 352 times.
✓ Branch 4 taken 336 times.
✓ Branch 5 taken 352 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 32 times.
✓ Branch 9 taken 352 times.
✓ Branch 10 taken 216 times.
✓ Branch 11 taken 456 times.
|
3120 | return std::fabs(value) < 1e-10; |
| 16 | } | ||
| 17 | |||
| 18 | int FindPivot(const std::vector<std::vector<double>> &matrix, int row, int col, int n) { | ||
| 19 |
1/2✓ Branch 0 taken 384 times.
✗ Branch 1 not taken.
|
384 | for (int i = row; i < n; i++) { |
| 20 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 352 times.
|
384 | if (!IsZero(matrix[i][col])) { |
| 21 | return i; | ||
| 22 | } | ||
| 23 | } | ||
| 24 | return -1; | ||
| 25 | } | ||
| 26 | |||
| 27 | void SwapRows(std::vector<std::vector<double>> &matrix, int row1, int row2, int m) { | ||
| 28 |
2/2✓ Branch 0 taken 88 times.
✓ Branch 1 taken 24 times.
|
112 | for (int j = 0; j < m; j++) { |
| 29 | 88 | std::swap(matrix[row1][j], matrix[row2][j]); | |
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | void DivideRow(std::vector<std::vector<double>> &matrix, int row, double divisor, int m) { | ||
| 34 |
2/2✓ Branch 0 taken 1376 times.
✓ Branch 1 taken 352 times.
|
1728 | for (int j = 0; j < m; j++) { |
| 35 | 1376 | matrix[row][j] /= divisor; | |
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | void SubtractRows(std::vector<std::vector<double>> &matrix, int target_row, int source_row, double coefficient, int m) { | ||
| 40 |
2/2✓ Branch 0 taken 744 times.
✓ Branch 1 taken 216 times.
|
960 | for (int j = 0; j < m; j++) { |
| 41 | 744 | matrix[target_row][j] -= coefficient * matrix[source_row][j]; | |
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | 136 | void PerformGaussJordanElimination(std::vector<std::vector<double>> &matrix, int n, int m) { | |
| 46 | int row = 0; | ||
| 47 | int col = 0; | ||
| 48 | |||
| 49 |
3/4✓ Branch 0 taken 352 times.
✓ Branch 1 taken 136 times.
✓ Branch 2 taken 352 times.
✗ Branch 3 not taken.
|
488 | while (row < n && col < m - 1) { |
| 50 | int pivot_row = FindPivot(matrix, row, col, n); | ||
| 51 | |||
| 52 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 352 times.
|
352 | if (pivot_row == -1) { |
| 53 | ✗ | col++; | |
| 54 | ✗ | continue; | |
| 55 | } | ||
| 56 | |||
| 57 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 328 times.
|
352 | if (pivot_row != row) { |
| 58 | SwapRows(matrix, row, pivot_row, m); | ||
| 59 | } | ||
| 60 | |||
| 61 |
1/2✓ Branch 0 taken 352 times.
✗ Branch 1 not taken.
|
352 | double pivot_value = matrix[row][col]; |
| 62 |
1/2✓ Branch 0 taken 352 times.
✗ Branch 1 not taken.
|
352 | if (!IsZero(pivot_value)) { |
| 63 | DivideRow(matrix, row, pivot_value, m); | ||
| 64 | } | ||
| 65 | |||
| 66 |
2/2✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 352 times.
|
1376 | for (int i = 0; i < n; i++) { |
| 67 |
4/4✓ Branch 0 taken 672 times.
✓ Branch 1 taken 352 times.
✓ Branch 2 taken 216 times.
✓ Branch 3 taken 456 times.
|
1024 | if (i != row && !IsZero(matrix[i][col])) { |
| 68 | double coeff = matrix[i][col]; | ||
| 69 | SubtractRows(matrix, i, row, coeff, m); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | 352 | row++; | |
| 74 | 352 | col++; | |
| 75 | } | ||
| 76 | 136 | } | |
| 77 | |||
| 78 | bool IsRowAllZeros(const std::vector<double> &row, int cols) { | ||
| 79 |
2/4✓ Branch 0 taken 688 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 688 times.
✗ Branch 3 not taken.
|
1376 | for (int j = 0; j < cols; j++) { |
| 80 |
4/4✓ Branch 0 taken 336 times.
✓ Branch 1 taken 352 times.
✓ Branch 2 taken 336 times.
✓ Branch 3 taken 352 times.
|
1376 | if (!IsZero(row[j])) { |
| 81 | return false; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | return true; | ||
| 85 | } | ||
| 86 | |||
| 87 | 136 | bool CheckInconsistency(const std::vector<std::vector<double>> &matrix, int n, int m) { | |
| 88 |
2/2✓ Branch 0 taken 352 times.
✓ Branch 1 taken 136 times.
|
488 | for (int i = 0; i < n; i++) { |
| 89 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 352 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
704 | if (IsRowAllZeros(matrix[i], m - 1) && !IsZero(matrix[i][m - 1])) { |
| 90 | return true; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | return false; | ||
| 94 | } | ||
| 95 | |||
| 96 | 136 | int CalculateRank(const std::vector<std::vector<double>> &matrix, int n, int m) { | |
| 97 | int rank = 0; | ||
| 98 |
2/2✓ Branch 0 taken 352 times.
✓ Branch 1 taken 136 times.
|
488 | for (int i = 0; i < n; i++) { |
| 99 |
1/2✓ Branch 0 taken 352 times.
✗ Branch 1 not taken.
|
704 | if (!IsRowAllZeros(matrix[i], m - 1)) { |
| 100 | 352 | rank++; | |
| 101 | } | ||
| 102 | } | ||
| 103 | 136 | return rank; | |
| 104 | } | ||
| 105 | |||
| 106 | 136 | std::vector<double> ExtractSolution(const std::vector<std::vector<double>> &matrix, int n, int m) { | |
| 107 | 136 | std::vector<double> solution(m - 1, 0.0); | |
| 108 |
2/2✓ Branch 0 taken 352 times.
✓ Branch 1 taken 136 times.
|
488 | for (int i = 0; i < std::min(n, m - 1); i++) { |
| 109 | bool found = false; | ||
| 110 |
1/2✓ Branch 0 taken 688 times.
✗ Branch 1 not taken.
|
688 | for (int j = 0; j < m - 1; j++) { |
| 111 |
2/2✓ Branch 0 taken 352 times.
✓ Branch 1 taken 336 times.
|
688 | if (!IsZero(matrix[i][j])) { |
| 112 | 352 | solution[j] = matrix[i][m - 1]; | |
| 113 | found = true; | ||
| 114 | break; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | ✗ | if (!found && i < m - 1) { | |
| 118 | ✗ | solution[i] = 0.0; | |
| 119 | } | ||
| 120 | } | ||
| 121 | 136 | return solution; | |
| 122 | } | ||
| 123 | |||
| 124 | } // namespace | ||
| 125 | |||
| 126 |
1/2✓ Branch 1 taken 136 times.
✗ Branch 2 not taken.
|
136 | GaivoronskiyMGaussJordanSEQ::GaivoronskiyMGaussJordanSEQ(const InType &in) { |
| 127 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 128 |
1/2✓ Branch 1 taken 136 times.
✗ Branch 2 not taken.
|
136 | InType tmp(in); |
| 129 | GetInput().swap(tmp); | ||
| 130 | 136 | } | |
| 131 | |||
| 132 |
1/2✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
|
136 | bool GaivoronskiyMGaussJordanSEQ::ValidationImpl() { |
| 133 |
1/2✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
|
136 | if (GetInput().empty()) { |
| 134 | return true; | ||
| 135 | } | ||
| 136 | size_t cols = GetInput()[0].size(); | ||
| 137 | std::size_t total = 0; | ||
| 138 |
2/2✓ Branch 0 taken 352 times.
✓ Branch 1 taken 136 times.
|
488 | for (const auto &row : GetInput()) { |
| 139 | 352 | total += row.size(); | |
| 140 | } | ||
| 141 |
3/6✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 136 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 136 times.
✗ Branch 5 not taken.
|
136 | return GetOutput().empty() && (cols != 0) && ((cols * GetInput().size()) == total); |
| 142 | } | ||
| 143 | |||
| 144 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
|
136 | bool GaivoronskiyMGaussJordanSEQ::PreProcessingImpl() { |
| 145 | GetOutput().clear(); | ||
| 146 | 136 | return true; | |
| 147 | } | ||
| 148 | |||
| 149 |
1/2✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
|
136 | bool GaivoronskiyMGaussJordanSEQ::RunImpl() { |
| 150 |
1/2✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
|
136 | if (GetInput().empty()) { |
| 151 | return true; | ||
| 152 | } | ||
| 153 | |||
| 154 | 136 | std::vector<std::vector<double>> matrix = GetInput(); | |
| 155 | 136 | int n = static_cast<int>(matrix.size()); | |
| 156 |
1/2✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
|
136 | int m = (n > 0) ? static_cast<int>(matrix[0].size()) : 0; |
| 157 | |||
| 158 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
|
136 | if (m == 0) { |
| 159 | return false; | ||
| 160 | } | ||
| 161 | |||
| 162 | 136 | PerformGaussJordanElimination(matrix, n, m); | |
| 163 | |||
| 164 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
|
136 | if (CheckInconsistency(matrix, n, m)) { |
| 165 | ✗ | GetOutput() = std::vector<double>(); | |
| 166 | ✗ | return false; | |
| 167 | } | ||
| 168 | |||
| 169 | 136 | int rank = CalculateRank(matrix, n, m); | |
| 170 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
136 | if (rank < m - 1 && rank < n) { |
| 171 | ✗ | GetOutput() = std::vector<double>(); | |
| 172 | ✗ | return false; | |
| 173 | } | ||
| 174 | |||
| 175 |
1/2✓ Branch 1 taken 136 times.
✗ Branch 2 not taken.
|
136 | GetOutput() = ExtractSolution(matrix, n, m); |
| 176 | 136 | return true; | |
| 177 | 136 | } | |
| 178 | |||
| 179 | 136 | bool GaivoronskiyMGaussJordanSEQ::PostProcessingImpl() { | |
| 180 | 136 | return true; | |
| 181 | } | ||
| 182 | |||
| 183 | } // namespace gaivoronskiy_m_gauss_jordan | ||
| 184 |