alpaqa 1.0.0a13
Nonconvex constrained optimization
Loading...
Searching...
No Matches
print.tpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <cassert>
6#include <charconv>
7#include <cmath>
8#include <limits>
9#include <ostream>
10#include <string_view>
11
12namespace alpaqa {
13
14inline std::string_view float_to_str_vw_snprintf(auto &&print, auto &buf,
15 std::floating_point auto value,
16 int precision,
17 const char *fmt) {
18 int n = print(buf.data(), buf.size(), fmt, precision, value);
19 assert((size_t)n < buf.size());
20 return {buf.data(), (size_t)n};
21}
22
23#if __cpp_lib_to_chars
24template <std::floating_point F>
25std::string_view
26float_to_str_vw(auto &buf, F value,
27 int precision = std::numeric_limits<F>::max_digits10) {
28 auto begin = buf.data();
29 if (!std::signbit(value))
30 *begin++ = '+';
31 auto [end, _] = std::to_chars(begin, buf.data() + buf.size(), value,
32 std::chars_format::scientific, precision);
33 return std::string_view{buf.data(), end};
34}
35#else
36#pragma message("Using std::snprintf as a fallback to replace std::to_chars")
37
38inline std::string_view
39float_to_str_vw(auto &buf, double value,
40 int precision = std::numeric_limits<double>::max_digits10) {
41 return float_to_str_vw_snprintf(std::snprintf, buf, value, precision,
42 "%+-#.*e");
43}
44inline std::string_view
45float_to_str_vw(auto &buf, float value,
46 int precision = std::numeric_limits<float>::max_digits10) {
47 return float_to_str_vw(buf, static_cast<double>(value), precision);
48}
49inline std::string_view float_to_str_vw(
50 auto &buf, long double value,
51 int precision = std::numeric_limits<long double>::max_digits10) {
52 return float_to_str_vw_snprintf(std::snprintf, buf, value, precision,
53 "%+-#.*Le");
54}
55#endif
56
57#ifdef ALPAQA_WITH_QUAD_PRECISION
58std::string_view
59float_to_str_vw(auto &buf, __float128 value,
60 int precision = std::numeric_limits<__float128>::max_digits10) {
62 "%+-#.*Qe");
63}
64#endif
65
66template <std::floating_point F>
67std::string float_to_str(F value, int precision) {
68 std::array<char, 64> buf;
69 return std::string{float_to_str_vw(buf, value, precision)};
70}
71
72template <std::floating_point F>
73void print_elem(auto &buf, F value, std::ostream &os) {
74 os << float_to_str_vw(buf, value);
75}
76
77template <std::integral I>
78void print_elem(auto &, I value, std::ostream &os) {
79 os << value;
80}
81
82template <std::floating_point F>
83void print_elem(auto &buf, std::complex<F> value, std::ostream &os) {
84 os << float_to_str_vw(buf, value.real()) << " + "
85 << float_to_str_vw(buf, value.imag()) << 'j';
86}
87
88namespace detail {
89
90template <class T>
91std::ostream &print_csv_impl(std::ostream &os, const T &M, std::string_view sep,
92 std::string_view begin, std::string_view end) {
93 std::array<char, 64> buf;
94 if (M.cols() == 1) {
95 os << begin;
96 for (decltype(M.rows()) r{}; r < M.rows(); ++r) {
97 print_elem(buf, M(r, 0), os);
98 if (r != M.rows() - 1)
99 os << sep;
100 }
101 return os << end;
102 } else {
103 for (decltype(M.rows()) r{}; r < M.rows(); ++r) {
104 os << begin;
105 for (decltype(M.cols()) c{}; c < M.cols(); ++c) {
106 print_elem(buf, M(r, c), os);
107 if (c != M.cols() - 1)
108 os << sep;
109 }
110 os << end;
111 }
112 return os;
113 }
114}
115
116template <class T>
117std::ostream &print_matlab_impl(std::ostream &os, const T &M,
118 std::string_view end) {
119 if (M.cols() == 1) {
120 return print_csv_impl<T>(os, M, " ", "[", "]") << end;
121 } else {
122 os << '[';
123 std::array<char, 64> buf;
124 for (decltype(M.rows()) r{}; r < M.rows(); ++r) {
125 for (decltype(M.cols()) c{}; c < M.cols(); ++c) {
126 print_elem(buf, M(r, c), os);
127 if (c != M.cols() - 1)
128 os << ' ';
129 }
130 if (r != M.rows() - 1)
131 os << ";\n ";
132 }
133 return os << ']' << end;
134 }
135}
136
137template <class T>
138std::ostream &print_python_impl(std::ostream &os, const T &M,
139 std::string_view end) {
140 if (M.cols() == 1) {
141 return print_csv_impl<T>(os, M, ", ", "[", "]") << end;
142 } else {
143 os << "[[";
144 std::array<char, 64> buf;
145 for (decltype(M.rows()) r{}; r < M.rows(); ++r) {
146 for (decltype(M.cols()) c{}; c < M.cols(); ++c) {
147 print_elem(buf, M(r, c), os);
148 if (c != M.cols() - 1)
149 os << ", ";
150 }
151 if (r != M.rows() - 1)
152 os << "],\n [";
153 }
154 return os << "]]" << end;
155 }
156}
157
158} // namespace detail
159
160} // namespace alpaqa
std::ostream & print_matlab_impl(std::ostream &os, const T &M, std::string_view end)
Definition print.tpp:117
std::ostream & print_python_impl(std::ostream &os, const T &M, std::string_view end)
Definition print.tpp:138
std::ostream & print_csv_impl(std::ostream &os, const T &M, std::string_view sep, std::string_view begin, std::string_view end)
Definition print.tpp:91
std::string_view float_to_str_vw_snprintf(auto &&print, auto &buf, std::floating_point auto value, int precision, const char *fmt)
Definition print.tpp:14
constexpr const auto inf
Definition config.hpp:85
std::string_view float_to_str_vw(auto &buf, double value, int precision=std::numeric_limits< double >::max_digits10)
Definition print.tpp:39
void print_elem(auto &buf, F value, std::ostream &os)
Definition print.tpp:73
std::string float_to_str(F value, int precision)
Definition print.tpp:67