alpaqa 1.0.0a11
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) {
61 return float_to_str_vw_snprintf(quadmath_snprintf, buf, value, precision,
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::floating_point F>
78void print_elem(auto &buf, std::complex<F> value, std::ostream &os) {
79 os << float_to_str_vw(buf, value.real()) << " + "
80 << float_to_str_vw(buf, value.imag()) << 'j';
81}
82
83template <class T>
84std::ostream &print_csv_impl(std::ostream &os, const T &M, std::string_view sep,
85 std::string_view begin, std::string_view end) {
86 std::array<char, 64> buf;
87 if (M.cols() == 1) {
88 os << begin;
89 for (decltype(M.rows()) r{}; r < M.rows(); ++r) {
90 print_elem(buf, M(r, 0), os);
91 if (r != M.rows() - 1)
92 os << sep;
93 }
94 return os << end;
95 } else {
96 for (decltype(M.rows()) r{}; r < M.rows(); ++r) {
97 os << begin;
98 for (decltype(M.cols()) c{}; c < M.cols(); ++c) {
99 print_elem(buf, M(r, c), os);
100 if (c != M.cols() - 1)
101 os << sep;
102 }
103 os << end;
104 }
105 return os;
106 }
107}
108
109template <class T>
110std::ostream &print_matlab_impl(std::ostream &os, const T &M,
111 std::string_view end) {
112 if (M.cols() == 1) {
113 return print_csv_impl<T>(os, M, " ", "[", "]") << end;
114 } else {
115 os << '[';
116 std::array<char, 64> buf;
117 for (decltype(M.rows()) r{}; r < M.rows(); ++r) {
118 for (decltype(M.cols()) c{}; c < M.cols(); ++c) {
119 print_elem(buf, M(r, c), os);
120 if (c != M.cols() - 1)
121 os << ' ';
122 }
123 if (r != M.rows() - 1)
124 os << ";\n ";
125 }
126 return os << ']' << end;
127 }
128}
129
130template <class T>
131std::ostream &print_python_impl(std::ostream &os, const T &M,
132 std::string_view end) {
133 if (M.cols() == 1) {
134 return print_csv_impl<T>(os, M, ", ", "[", "]") << end;
135 } else {
136 os << "[[";
137 std::array<char, 64> buf;
138 for (decltype(M.rows()) r{}; r < M.rows(); ++r) {
139 for (decltype(M.cols()) c{}; c < M.cols(); ++c) {
140 print_elem(buf, M(r, c), os);
141 if (c != M.cols() - 1)
142 os << ", ";
143 }
144 if (r != M.rows() - 1)
145 os << "],\n [";
146 }
147 return os << "]]" << end;
148 }
149}
150
151} // 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:14
std::ostream & print_matlab_impl(std::ostream &os, const T &M, std::string_view end)
Definition: print.tpp:110
std::ostream & print_python_impl(std::ostream &os, const T &M, std::string_view end)
Definition: print.tpp:131
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
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:84