guanaqo 1.0.0-alpha.25
Utilities for scientific software
Loading...
Searching...
No Matches
print.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// @ingroup io
5/// Matrix/vector printing helpers and float formatting.
6
7#include <guanaqo/export.h>
8#include <guanaqo/float.hpp>
10
11#include <cstdlib>
12#include <iosfwd>
13#include <limits>
14#include <span>
15#include <string>
16#include <string_view>
17
18namespace guanaqo {
19
20/// @addtogroup io
21/// @{
22
23template <class T>
25 public:
26 using value_type = T;
27 using index_type = ptrdiff_t;
29
30 public:
31 PrintMatrixView() = default;
32 template <class U, class I, class S>
35 template <class U, class I, class S>
38 template <class U, std::size_t E>
39 PrintMatrixView(std::span<U, E> x) noexcept
40 : view{{
41 .data = x.data(),
42 .rows = static_cast<index_type>(x.size()),
43 }} {}
44
45 [[nodiscard]] index_type rows() const noexcept {
46 return transposed ? view.cols : view.rows;
47 }
48 [[nodiscard]] index_type cols() const noexcept {
49 return transposed ? view.rows : view.cols;
50 }
52 index_type j) const noexcept {
53 return transposed ? view(j, i) : view(i, j);
54 }
55
56 private:
58 bool transposed = false;
59
60 template <class U, class I, class S>
62 bool transposed) noexcept
63 : view{{
64 .data = v.data,
65 .rows = static_cast<index_type>(v.rows),
66 .cols = static_cast<index_type>(v.cols),
67 .inner_stride = static_cast<inner_stride_type>(v.inner_stride),
68 .outer_stride = static_cast<index_type>(v.outer_stride),
69 }},
71};
72
73struct GUANAQO_EXPORT PrintOpts {
74 int precision = 0;
75 std::string_view delimiter = ",";
76 std::string_view line_start = {};
77 std::string_view line_end = "\n";
78 std::string_view start = {};
79 std::string_view end = "\n";
80 unsigned indent = 0;
81 char indent_char = ' ';
83 bool row_vector_as_1d = true;
84};
85
86template <std::floating_point F>
87GUANAQO_EXPORT std::string_view
88float_to_str_vw(std::span<char> buf, F value,
89 int precision = std::numeric_limits<F>::max_digits10);
90
91template <std::floating_point F>
92GUANAQO_EXPORT std::string
93float_to_str(F value, int precision = std::numeric_limits<F>::max_digits10);
94
95/// @}
96
97namespace detail {
98
99template <class T>
100GUANAQO_EXPORT std::ostream &print_csv_impl(std::ostream &os,
102 PrintOpts opts = {});
103
104template <class T>
105GUANAQO_EXPORT std::ostream &print_matlab_impl(std::ostream &os,
106 PrintMatrixView<const T> M,
107 std::string_view end = ";\n");
108
109template <class T>
110GUANAQO_EXPORT std::ostream &
111print_python_impl(std::ostream &os, PrintMatrixView<const T> M,
112 std::string_view end = "\n", bool squeeze = true);
113
114} // namespace detail
115
116/// @addtogroup io
117/// @{
118
119template <class T, std::size_t E>
120std::ostream &print_csv(std::ostream &os, std::span<T, E> x,
121 PrintOpts opts = {}) {
122 return guanaqo::detail::print_csv_impl(os, PrintMatrixView<const T>{x},
123 opts);
124}
125
126template <class T, class I, class S, StorageOrder O>
127std::ostream &print_csv(std::ostream &os, MatrixView<T, I, S, O> X,
128 PrintOpts opts = {}) {
129 return guanaqo::detail::print_csv_impl(os, PrintMatrixView<const T>{X},
130 opts);
131}
132
133template <class T, std::size_t E>
134std::ostream &print_matlab(std::ostream &os, std::span<T, E> x,
135 std::string_view end = ";\n") {
137 end);
138}
139
140template <class T, class I, class S, StorageOrder O>
141std::ostream &print_matlab(std::ostream &os, MatrixView<T, I, S, O> X,
142 std::string_view end = ";\n") {
144 end);
145}
146
147template <class T, std::size_t E>
148std::ostream &print_python(std::ostream &os, std::span<T, E> x,
149 std::string_view end = "\n", bool squeeze = true) {
151 end, squeeze);
152}
153
154template <class T, class I, class S, StorageOrder O>
155std::ostream &print_python(std::ostream &os, MatrixView<T, I, S, O> X,
156 std::string_view end = "\n", bool squeeze = true) {
158 end, squeeze);
159}
160
161/// @}
162
163} // namespace guanaqo
PrintMatrixView(MatrixView< U, I, S, StorageOrder::ColMajor > v) noexcept
Definition print.hpp:33
MatrixView< T, index_type, index_type, StorageOrder::ColMajor > view
Definition print.hpp:57
value_type & operator()(index_type i, index_type j) const noexcept
Definition print.hpp:51
PrintMatrixView(std::span< U, E > x) noexcept
Definition print.hpp:39
index_type cols() const noexcept
Definition print.hpp:48
index_type inner_stride_type
Definition print.hpp:28
index_type rows() const noexcept
Definition print.hpp:45
PrintMatrixView(MatrixView< U, I, S, StorageOrder::RowMajor > v) noexcept
Definition print.hpp:36
PrintMatrixView(MatrixView< U, I, S, StorageOrder::ColMajor > v, bool transposed) noexcept
Definition print.hpp:61
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::ostream & print_csv(std::ostream &os, std::span< T, E > x, PrintOpts opts={})
Definition print.hpp:120
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::ostream & print_python(std::ostream &os, std::span< T, E > x, std::string_view end="\n", bool squeeze=true)
Definition print.hpp:148
std::ostream & print_matlab(std::ostream &os, std::span< T, E > x, std::string_view end=";\n")
Definition print.hpp:134
std::string float_to_str(F value, int precision)
Definition print.tpp:15
Non-owning matrix view.
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
A lightweight view of a 2D matrix.
Definition mat-view.hpp:68