| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <fstream> | ||
| 6 | #include <stdexcept> | ||
| 7 | #include <string> | ||
| 8 | #include <tuple> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "task/include/task.hpp" | ||
| 12 | |||
| 13 | namespace luzan_e_double_sparse_matrix_mult { | ||
| 14 | |||
| 15 | const double kEPS = 1e-8; | ||
| 16 | |||
| 17 | struct SparseMatrix { | ||
| 18 | std::vector<double> value; | ||
| 19 | std::vector<unsigned> row; | ||
| 20 | std::vector<unsigned> col_index; | ||
| 21 | |||
| 22 | unsigned cols; | ||
| 23 | unsigned rows; | ||
| 24 | |||
| 25 |
1/2✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
|
144 | SparseMatrix(unsigned rows_out, unsigned cols_out) : cols(cols_out), rows(rows_out) { |
| 26 | col_index.clear(); | ||
| 27 | row.clear(); | ||
| 28 | value.clear(); | ||
| 29 | } | ||
| 30 | |||
| 31 |
2/5✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
24 | SparseMatrix() : cols(0), rows(0) { |
| 32 | col_index.clear(); | ||
| 33 | row.clear(); | ||
| 34 | value.clear(); | ||
| 35 | 12 | } | |
| 36 | |||
| 37 | 312 | SparseMatrix(const std::vector<double> &matrix, unsigned rows_out, unsigned cols_out) | |
| 38 |
1/2✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
|
312 | : cols(cols_out), rows(rows_out) { |
| 39 | col_index.clear(); | ||
| 40 | row.clear(); | ||
| 41 | value.clear(); | ||
| 42 | |||
| 43 |
1/2✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
|
312 | Sparse(matrix); |
| 44 | 312 | } | |
| 45 | |||
| 46 | ✗ | void GenLineMatrix(unsigned rows_out, unsigned cols_out) { | |
| 47 | col_index.clear(); | ||
| 48 | row.clear(); | ||
| 49 | value.clear(); | ||
| 50 | |||
| 51 | ✗ | rows = rows_out; | |
| 52 | ✗ | cols = cols_out; | |
| 53 | |||
| 54 | ✗ | col_index.push_back(0); | |
| 55 | ✗ | for (unsigned j = 0; j < cols; j++) { | |
| 56 | ✗ | for (unsigned i = 0; i < rows; i++) { | |
| 57 | ✗ | if (i % 5 == 0) { | |
| 58 | ✗ | value.push_back(1.0); | |
| 59 | ✗ | row.push_back(i); | |
| 60 | } | ||
| 61 | } | ||
| 62 | ✗ | col_index.push_back(value.size()); | |
| 63 | } | ||
| 64 | ✗ | } | |
| 65 | |||
| 66 | ✗ | void GenColsMatrix(unsigned rows_out, unsigned cols_out) { | |
| 67 | col_index.clear(); | ||
| 68 | row.clear(); | ||
| 69 | value.clear(); | ||
| 70 | |||
| 71 | ✗ | rows = rows_out; | |
| 72 | ✗ | cols = cols_out; | |
| 73 | ✗ | col_index.push_back(0); | |
| 74 | |||
| 75 | ✗ | for (unsigned j = 0; j < cols; j++) { | |
| 76 | ✗ | if (j % 5 == 0) { | |
| 77 | ✗ | for (unsigned i = 0; i < rows; i++) { | |
| 78 | ✗ | value.push_back(1.0); | |
| 79 | ✗ | row.push_back(i); | |
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | ✗ | col_index.push_back(value.size()); | |
| 84 | } | ||
| 85 | ✗ | } | |
| 86 | |||
| 87 | ✗ | void GenPerfAns(unsigned n, unsigned m, unsigned k) { | |
| 88 | col_index.clear(); | ||
| 89 | row.clear(); | ||
| 90 | value.clear(); | ||
| 91 | ✗ | rows = n; | |
| 92 | ✗ | cols = m; | |
| 93 | |||
| 94 | ✗ | col_index.push_back(0); | |
| 95 | ✗ | for (unsigned j = 0; j < m; j++) { | |
| 96 | ✗ | if (j % 5 == 0) // только чётные столбцы ненулевые | |
| 97 | { | ||
| 98 | ✗ | for (unsigned i = 0; i < n; i++) { | |
| 99 | ✗ | if (i % 5 == 0) // только чётные строки | |
| 100 | { | ||
| 101 | ✗ | value.push_back(static_cast<double>(k)); | |
| 102 | ✗ | row.push_back(i); | |
| 103 | } | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | ✗ | col_index.push_back(value.size()); | |
| 108 | } | ||
| 109 | ✗ | } | |
| 110 | |||
| 111 | [[nodiscard]] unsigned GetCols() const { | ||
| 112 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 144 times.
|
150 | return cols; |
| 113 | } | ||
| 114 | |||
| 115 | [[nodiscard]] unsigned GetRows() const { | ||
| 116 |
3/4✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 144 times.
|
300 | return rows; |
| 117 | } | ||
| 118 | |||
| 119 | [[nodiscard]] std::vector<double> GetVal() const { | ||
| 120 | return value; | ||
| 121 | } | ||
| 122 | |||
| 123 |
1/2✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
|
150 | bool operator==(const SparseMatrix &b) const { |
| 124 | bool tmp = false; | ||
| 125 |
1/2✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
|
150 | if (value.size() == b.value.size()) { |
| 126 | tmp = true; | ||
| 127 |
2/2✓ Branch 0 taken 750 times.
✓ Branch 1 taken 150 times.
|
900 | for (size_t long_i = 0; long_i < value.size(); long_i++) { |
| 128 |
1/2✓ Branch 0 taken 750 times.
✗ Branch 1 not taken.
|
750 | if (fabs(value[long_i] - b.value[long_i]) > kEPS) { |
| 129 | tmp = false; | ||
| 130 | break; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 |
4/8✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 150 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 150 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 150 times.
|
150 | return tmp && (row == b.row) && (col_index == b.col_index) && (cols == b.cols) && (rows == b.rows); |
| 136 | } | ||
| 137 | |||
| 138 | double GetXy(unsigned x = 1, unsigned y = 2) { | ||
| 139 | for (unsigned verylongs = col_index[y]; verylongs < col_index[y + 1]; verylongs++) { | ||
| 140 | if (row[verylongs] == x) { | ||
| 141 | return value[verylongs]; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | return 0.0; | ||
| 145 | } | ||
| 146 | 312 | void Sparse(const std::vector<double> &matrix) { | |
| 147 | 312 | col_index.push_back(0); | |
| 148 | bool flag = false; | ||
| 149 |
2/2✓ Branch 0 taken 910 times.
✓ Branch 1 taken 312 times.
|
1222 | for (unsigned j = 0; j < cols; j++) { |
| 150 | 910 | col_index.push_back(value.size()); | |
| 151 | |||
| 152 |
2/2✓ Branch 0 taken 3224 times.
✓ Branch 1 taken 910 times.
|
4134 | for (unsigned i = 0; i < rows; i++) { |
| 153 |
2/2✓ Branch 0 taken 1222 times.
✓ Branch 1 taken 2002 times.
|
3224 | if (fabs(matrix[(i * cols) + j]) > kEPS) { |
| 154 |
2/2✓ Branch 0 taken 468 times.
✓ Branch 1 taken 754 times.
|
1222 | value.push_back(matrix[(i * cols) + j]); |
| 155 |
2/2✓ Branch 0 taken 468 times.
✓ Branch 1 taken 754 times.
|
1222 | row.push_back(i); |
| 156 | flag = true; | ||
| 157 | } | ||
| 158 | } | ||
| 159 |
2/2✓ Branch 0 taken 572 times.
✓ Branch 1 taken 338 times.
|
910 | if (flag) { |
| 160 | col_index.pop_back(); | ||
| 161 | 572 | col_index.push_back(value.size()); | |
| 162 | flag = false; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | 312 | } | |
| 166 | |||
| 167 | 48 | SparseMatrix operator*(const SparseMatrix &b) const { | |
| 168 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | SparseMatrix c(rows, b.cols); |
| 169 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | c.col_index.push_back(0); |
| 170 | |||
| 171 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 48 times.
|
176 | for (unsigned b_col = 0; b_col < b.cols; b_col++) { |
| 172 |
1/2✓ Branch 1 taken 128 times.
✗ Branch 2 not taken.
|
128 | std::vector<double> tmp_col(rows, 0); |
| 173 | 128 | unsigned b_rows_start = b.col_index[b_col]; | |
| 174 | 128 | unsigned b_rows_end = b.col_index[b_col + 1]; | |
| 175 | |||
| 176 |
2/2✓ Branch 0 taken 160 times.
✓ Branch 1 taken 128 times.
|
288 | for (unsigned b_pos = b_rows_start; b_pos < b_rows_end; b_pos++) { |
| 177 | 160 | double b_val = b.value[b_pos]; | |
| 178 | 160 | unsigned b_row = b.row[b_pos]; | |
| 179 | |||
| 180 | 160 | unsigned a_rows_start = col_index[b_row]; | |
| 181 | 160 | unsigned a_rows_end = col_index[b_row + 1]; | |
| 182 | |||
| 183 |
2/2✓ Branch 0 taken 376 times.
✓ Branch 1 taken 160 times.
|
536 | for (unsigned a_pos = a_rows_start; a_pos < a_rows_end; a_pos++) { |
| 184 | 376 | double a_val = value[a_pos]; | |
| 185 | 376 | unsigned a_row = row[a_pos]; | |
| 186 | 376 | tmp_col[a_row] += a_val * b_val; | |
| 187 | } | ||
| 188 | } | ||
| 189 |
2/2✓ Branch 0 taken 480 times.
✓ Branch 1 taken 128 times.
|
608 | for (unsigned i = 0; i < rows; i++) { |
| 190 |
2/2✓ Branch 0 taken 240 times.
✓ Branch 1 taken 240 times.
|
480 | if (fabs(tmp_col[i]) > kEPS) { |
| 191 |
2/2✓ Branch 0 taken 104 times.
✓ Branch 1 taken 136 times.
|
240 | c.value.push_back(tmp_col[i]); |
| 192 |
2/2✓ Branch 0 taken 104 times.
✓ Branch 1 taken 136 times.
|
240 | c.row.push_back(i); |
| 193 | } | ||
| 194 | } | ||
| 195 |
2/6✓ Branch 1 taken 128 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 128 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
128 | c.col_index.push_back(c.value.size()); |
| 196 | } | ||
| 197 | 48 | return c; | |
| 198 | ✗ | } | |
| 199 | |||
| 200 | 156 | void GetSparsedMatrixFromFile(std::ifstream &file) { | |
| 201 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
|
156 | if (!file) { |
| 202 | ✗ | throw std::runtime_error("Cannot open file with sparsed matrix"); | |
| 203 | } | ||
| 204 | value.clear(); | ||
| 205 | row.clear(); | ||
| 206 | col_index.clear(); | ||
| 207 | 156 | unsigned n = 0; | |
| 208 | 156 | file >> n >> rows >> cols; | |
| 209 | |||
| 210 | 156 | double tmp_val = 0; | |
| 211 |
2/2✓ Branch 0 taken 780 times.
✓ Branch 1 taken 156 times.
|
936 | for (unsigned i = 0; i < n; i++) { |
| 212 | file >> tmp_val; | ||
| 213 |
2/2✓ Branch 0 taken 338 times.
✓ Branch 1 taken 442 times.
|
780 | value.push_back(tmp_val); |
| 214 | } | ||
| 215 | |||
| 216 | 156 | unsigned tmp = 0; | |
| 217 |
2/2✓ Branch 0 taken 780 times.
✓ Branch 1 taken 156 times.
|
936 | for (unsigned i = 0; i < n; i++) { |
| 218 | file >> tmp; | ||
| 219 |
2/2✓ Branch 0 taken 338 times.
✓ Branch 1 taken 442 times.
|
780 | row.push_back(tmp); |
| 220 | } | ||
| 221 | |||
| 222 |
2/2✓ Branch 0 taken 572 times.
✓ Branch 1 taken 156 times.
|
728 | for (unsigned i = 0; i < cols + 1; i++) { |
| 223 | file >> tmp; | ||
| 224 |
2/2✓ Branch 0 taken 104 times.
✓ Branch 1 taken 468 times.
|
572 | col_index.push_back(tmp); |
| 225 | } | ||
| 226 | 156 | } | |
| 227 | }; | ||
| 228 | |||
| 229 | 312 | inline SparseMatrix GetFromFile(std::ifstream &file) { | |
| 230 | 312 | size_t r = 0; | |
| 231 | 312 | size_t c = 0; | |
| 232 | 312 | file >> r >> c; | |
| 233 | |||
| 234 | 312 | std::vector<double> dense(r * c); | |
| 235 | |||
| 236 |
2/2✓ Branch 0 taken 1040 times.
✓ Branch 1 taken 312 times.
|
1352 | for (unsigned i = 0; i < r; i++) { |
| 237 |
2/2✓ Branch 0 taken 3224 times.
✓ Branch 1 taken 1040 times.
|
4264 | for (unsigned j = 0; j < c; j++) { |
| 238 |
1/2✓ Branch 1 taken 3224 times.
✗ Branch 2 not taken.
|
3224 | file >> dense[(i * c) + j]; |
| 239 | } | ||
| 240 | } | ||
| 241 |
1/2✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
|
312 | SparseMatrix a(dense, r, c); |
| 242 | 312 | return a; | |
| 243 | }; | ||
| 244 | |||
| 245 | using InType = std::tuple<SparseMatrix, SparseMatrix>; | ||
| 246 | using OutType = SparseMatrix; | ||
| 247 | using TestType = std::tuple<std::string, std::string>; | ||
| 248 | using BaseTask = ppc::task::Task<InType, OutType>; | ||
| 249 | |||
| 250 | } // namespace luzan_e_double_sparse_matrix_mult | ||
| 251 |