guanaqo 1.0.0-alpha.25
Utilities for scientific software
Loading...
Searching...
No Matches
print.tpp
Go to the documentation of this file.
1#pragma once
2
3#include <guanaqo/print.hpp>
4
5#include <array>
6#include <cassert>
7#include <cmath>
8#include <ostream>
9#include <span>
10#include <string_view>
11
12namespace guanaqo {
13
14template <std::floating_point F>
15std::string float_to_str(F value, int precision) {
16 std::array<char, 64> buf;
17 return std::string{float_to_str_vw(buf, value, precision)};
18}
19
20template <std::floating_point F>
21void print_elem(std::span<char> buf, F value, std::ostream &os) {
22 os << float_to_str_vw(buf, value);
23}
24
25template <std::integral I>
26void print_elem(auto &, I value, std::ostream &os) {
27 os << value;
28}
29
30template <std::floating_point F>
31void print_elem(std::span<char> buf, std::complex<F> value, std::ostream &os) {
32 os << float_to_str_vw(buf, value.real()) << " + "
33 << float_to_str_vw(buf, value.imag()) << 'j';
34}
35
36namespace detail {
37
38template <class T> // NOLINTNEXTLINE(*-cognitive-complexity)
39std::ostream &print_csv_impl(std::ostream &os, PrintMatrixView<const T> M,
40 PrintOpts opts) {
41 using index_t = decltype(M)::index_type;
42 auto indent = std::string(opts.indent, opts.indent_char);
43 std::array<char, 64> buf;
44 if ((M.cols() == 1 && opts.column_vector_as_1d) ||
45 (M.rows() == 1 && opts.row_vector_as_1d)) {
46 os << indent << opts.start;
47 for (index_t r = 0; r < M.rows(); ++r) {
48 for (index_t c = 0; c < M.cols(); ++c) {
49 print_elem(buf, M(r, c), os);
50 if (r != M.rows() - 1 || c != M.cols() - 1)
51 os << opts.delimiter;
52 }
53 }
54 return os << opts.end;
55 } else {
56 for (index_t r = 0; r < M.rows(); ++r) {
57 os << indent << (r == 0 ? opts.start : opts.line_start);
58 for (index_t c = 0; c < M.cols(); ++c) {
59 print_elem(buf, M(r, c), os);
60 if (c != M.cols() - 1)
61 os << opts.delimiter;
62 }
63 if (r != M.rows() - 1)
64 os << opts.line_end;
65 }
66 return os << opts.end;
67 }
68}
69
70template <class T>
71std::ostream &print_matlab_impl(std::ostream &os, PrintMatrixView<const T> M,
72 std::string_view end) {
73 auto opts = [&] {
74 if (M.cols() == 1)
75 return PrintOpts{
76 .delimiter = "; ",
77 .start = "[",
78 .end = "]",
79 };
80 else
81 return PrintOpts{
82 .delimiter = " ",
83 .line_start = " ",
84 .line_end = ";\n",
85 .start = "[",
86 .end = "]",
87 };
88 }();
89 return print_csv_impl<T>(os, M, opts) << end;
90}
91
92template <class T>
93std::ostream &print_python_impl(std::ostream &os, PrintMatrixView<const T> M,
94 std::string_view end, bool squeeze) {
95 auto opts = [&] {
96 if ((M.cols() == 1 && squeeze) || (M.rows() == 1 && squeeze))
97 return PrintOpts{
98 .delimiter = ", ",
99 .start = "[",
100 .end = "]",
101 };
102 else
103 return PrintOpts{
104 .delimiter = ", ",
105 .line_start = " [",
106 .line_end = "],\n",
107 .start = "[[",
108 .end = "]]",
109 .column_vector_as_1d = false,
110 .row_vector_as_1d = false,
111 };
112 }();
113 return print_csv_impl<T>(os, M, opts) << end;
114}
115
116} // namespace detail
117
118} // namespace guanaqo
index_type cols() const noexcept
Definition print.hpp:48
index_type rows() const noexcept
Definition print.hpp:45
std::string_view start
Definition print.hpp:78
std::string_view line_start
Definition print.hpp:76
std::string_view line_end
Definition print.hpp:77
unsigned indent
Definition print.hpp:80
std::string_view delimiter
Definition print.hpp:75
bool row_vector_as_1d
Definition print.hpp:83
std::string_view end
Definition print.hpp:79
bool column_vector_as_1d
Definition print.hpp:82
std::string_view float_to_str_vw(std::span< char > buf, F value, int precision=std::numeric_limits< F >::max_digits10)
Definition print.cpp:77
std::string float_to_str(F value, int precision)
Definition print.tpp:15
std::ostream & print_csv_impl(std::ostream &os, PrintMatrixView< const T > M, PrintOpts opts)
Definition print.tpp:39
std::ostream & print_matlab_impl(std::ostream &os, PrintMatrixView< const T > M, std::string_view end)
Definition print.tpp:71
std::ostream & print_python_impl(std::ostream &os, PrintMatrixView< const T > M, std::string_view end, bool squeeze)
Definition print.tpp:93
void print_elem(std::span< char > buf, F value, std::ostream &os)
Definition print.tpp:21
Matrix/vector printing helpers and float formatting.