26#ifndef EMRYS_FILTER_NO_IO
33# include <string_view>
61operator<<(std::ostream & stream, Indent indent)
63 for (
int i = 0; i < indent.depth; ++i)
75concept Printable =
requires(std::ostream & stream, T
const & value) { describe(stream, value, Indent{}); };
80template <
typename Derived>
82printRow(std::ostream & stream, Indent indent, std::string_view label, Eigen::DenseBase<Derived>
const & vector)
84 static auto const row = Eigen::IOFormat{Eigen::StreamPrecision, Eigen::DontAlignCols,
" ",
" "};
85 stream << indent << label <<
':';
86 if (vector.size() > 0)
88 stream <<
' ' << vector.reshaped().transpose().format(row);
95template <
typename Derived>
97printMatrix(std::ostream & stream, Indent indent, std::string_view label, Eigen::DenseBase<Derived>
const & matrix)
99 stream << indent << label <<
':';
100 if (matrix.size() > 0)
102 auto prefix = std::ostringstream{};
103 prefix << indent.deeper();
104 auto const rows = Eigen::IOFormat{Eigen::StreamPrecision, 0,
" ",
"\n", prefix.str()};
105 stream <<
'\n' << matrix.format(rows);
122printCount(std::ostream & stream, Eigen::Index count, std::string_view noun)
124 stream << count <<
' ' << noun << (count == 1 ?
"" :
"s");
132printMoments(std::ostream & stream, Indent indent, M
const & moments)
135 printRow(stream, indent,
"mean", moments.mean());
137 printMatrix(stream, indent,
"covariance", moments.covarianceLower());
143template <
typename M,
typename E>
145printMoments(std::ostream & stream, Indent indent, std::expected<M, E>
const & moments)
149 printMoments(stream, indent, *moments);
153 stream <<
'\n' << indent <<
"no moments: ";
154 describe(stream, moments.error(), Indent{});
161template <
typename State>
163describeFilter(std::ostream & stream, std::string_view name, State
const & state, Indent indent)
165 stream << indent << name <<
'\n';
166 describe(stream, state, indent.deeper());
171describe(std::ostream & stream,
NumericError error, Indent indent)
177 stream <<
"the covariance is not positive definite";
180 stream <<
"the precision is singular";
183 stream <<
"a value is not finite";
186 stream <<
"no weights";
189 stream <<
"every weight is zero";
205template <
internal::Pr
intable T>
207operator<<(std::ostream & stream, T
const & value)
209 describe(stream, value, internal::Indent{});
230template <Emrys::
internal::Pr
intable T>
231struct std::formatter<T, char> : std::formatter<std::string_view, char>
236 template <
typename ParseContext>
237 constexpr typename ParseContext::iterator
240 auto const begin = context.begin();
242 while (end != context.end() && *end !=
'}')
246 auto const spec = std::string_view{begin, end};
248 auto const body = spec.ends_with(
's') ? spec.substr(0, spec.size() - 1) : spec;
251 auto digits = body.size();
252 while (digits > 0 && body[digits - 1] >=
'0' && body[digits - 1] <=
'9')
256 if (digits == body.size() || digits == 0 || body[digits - 1] !=
'.')
258 return std::formatter<std::string_view, char>::parse(context);
261 for (
char const digit : body.substr(digits))
263 precision = precision * 10 + (digit -
'0');
265 m_precision = precision;
267 auto rest = std::format_parse_context{body.substr(0, digits - 1)};
268 std::formatter<std::string_view, char>::parse(rest);
275 template <
typename Context>
276 [[nodiscard]]
typename Context::iterator
277 format(T
const & value, Context & context)
const
279 auto stream = std::ostringstream{};
280 if (m_precision >= 0)
282 stream.precision(m_precision);
285 return std::formatter<std::string_view, char>::format(stream.str(), context);
290 int m_precision = -1;
The macros every header includes first.
Log-weights: the WeightError a normalization can fail with (NumericError.h), and the streaming log-su...
std::ostream & operator<<(std::ostream &stream, T const &value)
Prints value, any type the library describes (internal::Printable): a distribution,...
Definition Print.h:207
NumericError
Reasons a computation from parameters the caller gave can fail: a matrix that does not factorize or a...
Definition NumericError.h:30
@ NotPositiveDefinite
the covariance is not positive definite, so it has no Cholesky factor or inverse
Definition NumericError.h:31
@ NonFinite
a matrix, a factor, or a log-weight holds NaN or infinity (+inf, for a log-weight)
Definition NumericError.h:33
@ ZeroSum
every weight is zero (every log-weight is -inf)
Definition NumericError.h:35
@ Empty
there are no weights
Definition NumericError.h:34
@ Singular
the precision is not positive definite, so the mean and covariance are undefined
Definition NumericError.h:32
Eigen::Index valueDimension(S const &space)
Definition Space.h:272
Eigen::Index differenceDimension(S const &space)
Definition Space.h:284