| 274 | /// \param w the width |
| 275 | template <typename T> |
| 276 | inline void print_matrix(const buffer<T> matrix, int n_cols, |
| 277 | int n_printable_rows = 10, int n_printable_cols = 10, |
| 278 | std::ostream &ostream = std::cout, |
| 279 | const char col_sep[] = " ", const char elide_sym[] = " ... ", |
| 280 | int w = -1) { |
| 281 | assert(matrix.size() % n_cols == 0); |
| 282 | |
| 283 | if (w == -1) { |
| 284 | w = 6; |
| 285 | } |
| 286 | int n_rows = matrix.size() / n_cols; |
| 287 | DO_VERBOSE(1, { |
| 288 | header_print("info", "Matrix size: " << "[" << n_rows << ", " << n_cols << "]"); |
| 289 | }); |
| 290 | |
| 291 | n_printable_rows = std::min(n_rows, n_printable_rows); |
| 292 | n_printable_cols = std::min(n_cols, n_printable_cols); |
| 293 | |
| 294 | const bool elide_rows = n_printable_rows < n_rows; |
| 295 | const bool elide_cols = n_printable_cols < n_cols; |
| 296 | |
| 297 | if (elide_rows || elide_cols) { |
| 298 | w = std::max((int)w, (int)strlen(elide_sym)); |
| 299 | } |
| 300 | |
| 301 | w += 3; // for decimal point and two decimal digits |
| 302 | ostream << std::fixed << std::setprecision(2); |
| 303 | |
| 304 | #define print_row(what) \ |
| 305 | for (int col = 0; col < (n_printable_cols + 1) / 2; col++) { \ |
| 306 | ostream << std::right << std::setw(w) << std::scientific << std::setprecision(2) << (what); \ |
| 307 | ostream << std::setw(0) << col_sep; \ |
| 308 | } \ |
| 309 | if (elide_cols) { \ |
| 310 | ostream << std::setw(0) << elide_sym; \ |
| 311 | } \ |
| 312 | for (int i = 0; i < n_printable_cols / 2; i++) { \ |
| 313 | [[maybe_unused]]int col = n_cols - n_printable_cols / 2 + i; \ |
| 314 | ostream << std::right << std::setw(w) << std::scientific << std::setprecision(2) << (what); \ |
| 315 | ostream << std::setw(0) << col_sep; \ |
| 316 | } |
| 317 | |
| 318 | for (int row = 0; row < (n_printable_rows + 1) / 2; row++) { |
| 319 | print_row(matrix[row * n_cols + col]); |
| 320 | ostream << std::endl; |
| 321 | } |
| 322 | if (elide_rows) { |
| 323 | print_row(elide_sym); |
| 324 | ostream << std::endl; |
| 325 | } |
| 326 | for (int i = 0; i < n_printable_rows / 2; i++) { |
| 327 | int row = n_rows - n_printable_rows / 2 + i; |
| 328 | print_row(matrix[row * n_cols + col]); |
| 329 | ostream << std::endl; |
| 330 | } |
| 331 | |
| 332 | #undef print_row |
| 333 | } |