| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "kotelnikova_a_double_matr_mult/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <vector> | ||
| 6 | |||
| 7 | #include "kotelnikova_a_double_matr_mult/common/include/common.hpp" | ||
| 8 | |||
| 9 | namespace kotelnikova_a_double_matr_mult { | ||
| 10 | |||
| 11 |
1/2✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
|
40 | KotelnikovaATaskSEQ::KotelnikovaATaskSEQ(const InType &in) { |
| 12 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 13 | GetInput() = in; | ||
| 14 | 40 | GetOutput() = SparseMatrixCCS(); | |
| 15 | 40 | } | |
| 16 | |||
| 17 | 80 | bool KotelnikovaATaskSEQ::IsMatrixValid(const SparseMatrixCCS &matrix) { | |
| 18 |
2/4✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 80 times.
|
80 | if (matrix.rows < 0 || matrix.cols < 0) { |
| 19 | return false; | ||
| 20 | } | ||
| 21 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
|
80 | if (matrix.col_ptrs.size() != static_cast<size_t>(matrix.cols) + 1) { |
| 22 | return false; | ||
| 23 | } | ||
| 24 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
|
80 | if (matrix.values.size() != matrix.row_indices.size()) { |
| 25 | return false; | ||
| 26 | } | ||
| 27 | |||
| 28 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 80 times.
|
80 | if (matrix.col_ptrs.empty() || matrix.col_ptrs[0] != 0) { |
| 29 | return false; | ||
| 30 | } | ||
| 31 | |||
| 32 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
|
80 | const int total_elements = static_cast<int>(matrix.values.size()); |
| 33 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
|
80 | if (matrix.col_ptrs[matrix.cols] != total_elements) { |
| 34 | return false; | ||
| 35 | } | ||
| 36 | |||
| 37 |
2/2✓ Branch 0 taken 232 times.
✓ Branch 1 taken 80 times.
|
312 | for (size_t i = 0; i < matrix.col_ptrs.size() - 1; ++i) { |
| 38 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 232 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 232 times.
|
232 | if (matrix.col_ptrs[i] > matrix.col_ptrs[i + 1] || matrix.col_ptrs[i] < 0) { |
| 39 | return false; | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 |
2/2✓ Branch 0 taken 288 times.
✓ Branch 1 taken 80 times.
|
368 | for (size_t i = 0; i < matrix.row_indices.size(); ++i) { |
| 44 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 288 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 288 times.
|
288 | if (matrix.row_indices[i] < 0 || matrix.row_indices[i] >= matrix.rows) { |
| 45 | return false; | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | return true; | ||
| 50 | } | ||
| 51 | |||
| 52 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | bool KotelnikovaATaskSEQ::ValidationImpl() { |
| 53 | const auto &[a, b] = GetInput(); | ||
| 54 | |||
| 55 |
2/4✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
|
40 | if (!IsMatrixValid(a) || !IsMatrixValid(b)) { |
| 56 | return false; | ||
| 57 | } | ||
| 58 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
|
40 | if (a.cols != b.rows) { |
| 59 | ✗ | return false; | |
| 60 | } | ||
| 61 | |||
| 62 | return true; | ||
| 63 | } | ||
| 64 | |||
| 65 | 40 | bool KotelnikovaATaskSEQ::PreProcessingImpl() { | |
| 66 | const auto &[a, b] = GetInput(); | ||
| 67 | 40 | GetOutput() = SparseMatrixCCS(a.rows, b.cols); | |
| 68 | 40 | return true; | |
| 69 | } | ||
| 70 | |||
| 71 | namespace { | ||
| 72 | 112 | void ProcessColumn(const SparseMatrixCCS &a, const SparseMatrixCCS &b, int j, std::vector<double> &temp) { | |
| 73 |
2/2✓ Branch 0 taken 352 times.
✓ Branch 1 taken 112 times.
|
464 | for (int k = 0; k < a.cols; ++k) { |
| 74 | double b_val = 0.0; | ||
| 75 |
2/2✓ Branch 0 taken 352 times.
✓ Branch 1 taken 240 times.
|
592 | for (int b_idx = b.col_ptrs[j]; b_idx < b.col_ptrs[j + 1]; ++b_idx) { |
| 76 |
2/2✓ Branch 0 taken 112 times.
✓ Branch 1 taken 240 times.
|
352 | if (b.row_indices[b_idx] == k) { |
| 77 | 112 | b_val = b.values[b_idx]; | |
| 78 | 112 | break; | |
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 |
2/2✓ Branch 0 taken 240 times.
✓ Branch 1 taken 112 times.
|
352 | if (b_val == 0.0) { |
| 83 | 240 | continue; | |
| 84 | } | ||
| 85 | |||
| 86 |
2/2✓ Branch 0 taken 168 times.
✓ Branch 1 taken 112 times.
|
280 | for (int a_idx = a.col_ptrs[k]; a_idx < a.col_ptrs[k + 1]; ++a_idx) { |
| 87 | 168 | const int i = a.row_indices[a_idx]; | |
| 88 | 168 | const double a_val = a.values[a_idx]; | |
| 89 | 168 | temp[i] += a_val * b_val; | |
| 90 | } | ||
| 91 | } | ||
| 92 | 112 | } | |
| 93 | |||
| 94 | 112 | void SaveColumnResults(int rows, std::vector<double> &temp, SparseMatrixCCS &result) { | |
| 95 |
2/2✓ Branch 0 taken 336 times.
✓ Branch 1 taken 112 times.
|
448 | for (int i = 0; i < rows; ++i) { |
| 96 |
2/2✓ Branch 0 taken 168 times.
✓ Branch 1 taken 168 times.
|
336 | if (std::abs(temp[i]) > 1e-10) { |
| 97 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 120 times.
|
168 | result.values.push_back(temp[i]); |
| 98 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 120 times.
|
168 | result.row_indices.push_back(i); |
| 99 | 168 | temp[i] = 0.0; | |
| 100 | } | ||
| 101 | } | ||
| 102 | 112 | } | |
| 103 | } // namespace | ||
| 104 | |||
| 105 | 40 | SparseMatrixCCS KotelnikovaATaskSEQ::MultiplyMatrices(const SparseMatrixCCS &a, const SparseMatrixCCS &b) { | |
| 106 | 40 | SparseMatrixCCS result(a.rows, b.cols); | |
| 107 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | std::vector<double> temp(a.rows, 0.0); |
| 108 | |||
| 109 |
2/2✓ Branch 0 taken 112 times.
✓ Branch 1 taken 40 times.
|
152 | for (int j = 0; j < b.cols; ++j) { |
| 110 | 112 | result.col_ptrs[j] = static_cast<int>(result.values.size()); | |
| 111 | 112 | ProcessColumn(a, b, j, temp); | |
| 112 |
1/2✓ Branch 1 taken 112 times.
✗ Branch 2 not taken.
|
112 | SaveColumnResults(a.rows, temp, result); |
| 113 | } | ||
| 114 | |||
| 115 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | result.col_ptrs[b.cols] = static_cast<int>(result.values.size()); |
| 116 | 40 | return result; | |
| 117 | ✗ | } | |
| 118 | |||
| 119 | 40 | bool KotelnikovaATaskSEQ::RunImpl() { | |
| 120 | const auto &[a, b] = GetInput(); | ||
| 121 | 40 | GetOutput() = MultiplyMatrices(a, b); | |
| 122 | 40 | return true; | |
| 123 | } | ||
| 124 | |||
| 125 | 40 | bool KotelnikovaATaskSEQ::PostProcessingImpl() { | |
| 126 | 40 | return true; | |
| 127 | } | ||
| 128 | |||
| 129 | } // namespace kotelnikova_a_double_matr_mult | ||
| 130 |