| 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_seq { | ||
| 14 | |||
| 15 | const double kEPS = 1e-8; | ||
| 16 | |||
| 17 | class 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 | public: | ||
| 26 | 48 | SparseMatrix(unsigned rows, unsigned cols) : cols_(cols), rows_(rows) { | |
| 27 | col_index_.clear(); | ||
| 28 | row_.clear(); | ||
| 29 | value_.clear(); | ||
| 30 | } | ||
| 31 | |||
| 32 | ✗ | SparseMatrix() : cols_(0), rows_(0) { | |
| 33 | col_index_.clear(); | ||
| 34 | row_.clear(); | ||
| 35 | value_.clear(); | ||
| 36 | } | ||
| 37 | |||
| 38 |
1/2✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
|
96 | SparseMatrix(const std::vector<double> &matrix, unsigned rows, unsigned cols) : cols_(cols), rows_(rows) { |
| 39 | col_index_.clear(); | ||
| 40 | row_.clear(); | ||
| 41 | value_.clear(); | ||
| 42 | |||
| 43 |
2/6✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 96 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
96 | Sparse(matrix); |
| 44 | 96 | } | |
| 45 | |||
| 46 | ✗ | void GenLineMatrix(unsigned rows, unsigned cols) { | |
| 47 | col_index_.clear(); | ||
| 48 | row_.clear(); | ||
| 49 | value_.clear(); | ||
| 50 | |||
| 51 | ✗ | rows_ = rows; | |
| 52 | ✗ | cols_ = cols; | |
| 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, unsigned cols) { | |
| 67 | col_index_.clear(); | ||
| 68 | row_.clear(); | ||
| 69 | value_.clear(); | ||
| 70 | |||
| 71 | ✗ | rows_ = rows; | |
| 72 | ✗ | cols_ = cols; | |
| 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 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | return cols_; |
| 113 | } | ||
| 114 | |||
| 115 | [[nodiscard]] unsigned GetRows() const { | ||
| 116 |
2/4✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 48 times.
|
96 | return rows_; |
| 117 | } | ||
| 118 | |||
| 119 | std::vector<double> GetVal() { | ||
| 120 | return value_; | ||
| 121 | } | ||
| 122 | |||
| 123 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | bool operator==(const SparseMatrix &b) const { |
| 124 | bool tmp = false; | ||
| 125 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | if (value_.size() == b.value_.size()) { |
| 126 | tmp = true; | ||
| 127 |
2/2✓ Branch 0 taken 240 times.
✓ Branch 1 taken 48 times.
|
288 | for (size_t long_i = 0; long_i < value_.size(); long_i++) { |
| 128 |
1/2✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
|
240 | 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 48 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 48 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 48 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 48 times.
|
48 | 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 | 96 | void Sparse(std::vector<double> matrix) { | |
| 147 | 96 | col_index_.push_back(0); | |
| 148 | bool flag = false; | ||
| 149 |
2/2✓ Branch 0 taken 280 times.
✓ Branch 1 taken 96 times.
|
376 | for (unsigned j = 0; j < cols_; j++) { |
| 150 | 280 | col_index_.push_back(value_.size()); | |
| 151 | |||
| 152 |
2/2✓ Branch 0 taken 992 times.
✓ Branch 1 taken 280 times.
|
1272 | for (unsigned i = 0; i < rows_; i++) { |
| 153 |
2/2✓ Branch 0 taken 376 times.
✓ Branch 1 taken 616 times.
|
992 | if (fabs(matrix[(i * cols_) + j]) > kEPS) { |
| 154 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 232 times.
|
376 | value_.push_back(matrix[(i * cols_) + j]); |
| 155 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 232 times.
|
376 | row_.push_back(i); |
| 156 | flag = true; | ||
| 157 | } | ||
| 158 | } | ||
| 159 |
2/2✓ Branch 0 taken 176 times.
✓ Branch 1 taken 104 times.
|
280 | if (flag) { |
| 160 | col_index_.pop_back(); | ||
| 161 | 176 | col_index_.push_back(value_.size()); | |
| 162 | flag = false; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | 96 | } | |
| 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 | 48 | void GetSparsedMatrixFromFile(std::ifstream &file) { | |
| 201 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (!file) { |
| 202 | ✗ | throw std::runtime_error("Cannot open file with sparsed matrix"); | |
| 203 | } | ||
| 204 | 48 | unsigned n = 0; | |
| 205 | 48 | file >> n >> rows_ >> cols_; | |
| 206 | |||
| 207 | 48 | double tmp_val = 0; | |
| 208 |
2/2✓ Branch 0 taken 240 times.
✓ Branch 1 taken 48 times.
|
288 | for (unsigned i = 0; i < n; i++) { |
| 209 | file >> tmp_val; | ||
| 210 |
2/2✓ Branch 0 taken 104 times.
✓ Branch 1 taken 136 times.
|
240 | value_.push_back(tmp_val); |
| 211 | } | ||
| 212 | |||
| 213 | 48 | unsigned tmp = 0; | |
| 214 |
2/2✓ Branch 0 taken 240 times.
✓ Branch 1 taken 48 times.
|
288 | for (unsigned i = 0; i < n; i++) { |
| 215 | file >> tmp; | ||
| 216 |
2/2✓ Branch 0 taken 104 times.
✓ Branch 1 taken 136 times.
|
240 | row_.push_back(tmp); |
| 217 | } | ||
| 218 | |||
| 219 |
2/2✓ Branch 0 taken 176 times.
✓ Branch 1 taken 48 times.
|
224 | for (unsigned i = 0; i < cols_ + 1; i++) { |
| 220 | file >> tmp; | ||
| 221 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 144 times.
|
176 | col_index_.push_back(tmp); |
| 222 | } | ||
| 223 | 48 | } | |
| 224 | }; | ||
| 225 | |||
| 226 | 96 | inline SparseMatrix GetFromFile(std::ifstream &file) { | |
| 227 | 96 | size_t r = 0; | |
| 228 | 96 | size_t c = 0; | |
| 229 | 96 | file >> r >> c; | |
| 230 | |||
| 231 | 96 | std::vector<double> dense(r * c); | |
| 232 | |||
| 233 |
2/2✓ Branch 0 taken 320 times.
✓ Branch 1 taken 96 times.
|
416 | for (unsigned i = 0; i < r; i++) { |
| 234 |
2/2✓ Branch 0 taken 992 times.
✓ Branch 1 taken 320 times.
|
1312 | for (unsigned j = 0; j < c; j++) { |
| 235 |
1/2✓ Branch 1 taken 992 times.
✗ Branch 2 not taken.
|
992 | file >> dense[(i * c) + j]; |
| 236 | } | ||
| 237 | } | ||
| 238 |
1/2✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
|
96 | SparseMatrix a(dense, r, c); |
| 239 | 96 | return a; | |
| 240 | }; | ||
| 241 | |||
| 242 | using InType = std::tuple<SparseMatrix, SparseMatrix>; | ||
| 243 | using OutType = SparseMatrix; | ||
| 244 | using TestType = std::tuple<std::string, std::string>; | ||
| 245 | using BaseTask = ppc::task::Task<InType, OutType>; | ||
| 246 | |||
| 247 | } // namespace luzan_e_double_sparse_matrix_mult_seq | ||
| 248 |