113 void format_as_table(std::ostream &out, nda::Matrix
auto const &mat,
auto const &row_labels,
auto const &col_labels) {
114 namespace stdv = std::views;
115 namespace stdr = std::ranges;
116 auto [rows, cols] = mat.shape();
119 long first_col_width = stdr::max(row_labels | stdv::transform([](
auto &&s) {
return long(s.size()); }));
122 std::vector<long> col_widths(cols, 0);
123 for (
long j : range(cols)) {
124 auto formatted_column = stdv::iota(0L, rows) | stdv::transform([&](
long i) {
return long(fmt::format(
"{}", mat(i, j)).size()); });
125 col_widths[j] = std::max(
long(col_labels[j].size()), stdr::max(formatted_column));
129 out << fmt::format(
"{:<{}} | ",
"", first_col_width);
130 for (
long j : range(cols)) out << fmt::format(
"{:^{}} ", col_labels[j], col_widths[j]);
134 out << fmt::format(
"{:-<{}}-+-",
"", first_col_width);
135 for (
long j : range(cols)) out << fmt::format(
"{:-<{}}-+",
"", col_widths[j]);
139 for (
long i : range(rows)) {
140 if (mat(i, 0).imp_idx == -1)
continue;
141 out << fmt::format(
"{:<{}} | ", row_labels[i], first_col_width);
142 for (
long j : range(cols)) { out << fmt::format(
"{:>{}} ", fmt::format(
"{}", mat(i, j)), col_widths[j]); };
void format_as_table(std::ostream &out, nda::Matrix auto const &mat, auto const &row_labels, auto const &col_labels)
Format the matrix mat as a table, with row/col_labels.