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