16#include "./InternalHeaderCheck.h"
20enum { DontAlignCols = 1 };
21enum { StreamPrecision = -1, FullPrecision = -2 };
24template <
typename Derived>
25std::ostream& print_matrix(std::ostream& s,
const Derived& _m,
const IOFormat& fmt);
55 IOFormat(
int _precision = StreamPrecision,
int _flags = 0,
const std::string& _coeffSeparator =
" ",
56 const std::string& _rowSeparator =
"\n",
const std::string& _rowPrefix =
"",
57 const std::string& _rowSuffix =
"",
const std::string& _matPrefix =
"",
const std::string& _matSuffix =
"",
58 const char _fill =
' ')
59 : matPrefix(_matPrefix),
60 matSuffix(_matSuffix),
61 rowPrefix(_rowPrefix),
62 rowSuffix(_rowSuffix),
63 rowSeparator(_rowSeparator),
65 coeffSeparator(_coeffSeparator),
67 precision(_precision),
69 if ((flags & DontAlignCols))
return;
72 const std::string between{rowSuffix + rowSeparator};
73 const std::size_t nlLastPos{between.rfind(
'\n')};
74 if (nlLastPos == std::string::npos)
return;
77 const std::size_t indent{matPrefix.length() - (matPrefix.rfind(
'\n') + 1)};
78 const std::size_t carried{between.length() - (nlLastPos + 1)};
80 if (indent > carried) rowSpacer.assign(indent - carried,
' ');
82 std::string matPrefix, matSuffix;
83 std::string rowPrefix, rowSuffix, rowSeparator, rowSpacer;
84 std::string coeffSeparator;
105template <
typename ExpressionType>
108 WithFormat(
const ExpressionType& matrix,
const IOFormat& format) : m_matrix(matrix), m_format(format) {}
110 friend std::ostream& operator<<(std::ostream& s,
const WithFormat& wf) {
111 return internal::print_matrix(s, wf.m_matrix.eval(), wf.m_format);
115 typename ExpressionType::Nested m_matrix;
124template <
typename Scalar>
125struct significant_decimals_impl {
126 static inline int run() {
return NumTraits<Scalar>::max_digits10(); }
131template <
typename Derived>
132std::ostream& print_matrix(std::ostream& s,
const Derived& _m,
const IOFormat& fmt) {
133 if (_m.size() == 0) {
134 s << fmt.matPrefix << fmt.matSuffix;
138 typename Derived::Nested m = _m;
139 using Scalar =
typename Derived::Scalar;
141 std::conditional_t<std::is_same<Scalar, char>::value || std::is_same<Scalar, unsigned char>::value ||
142 std::is_same<Scalar, numext::int8_t>::value ||
143 std::is_same<Scalar, numext::uint8_t>::value,
145 std::conditional_t<std::is_same<Scalar, std::complex<char> >::value ||
146 std::is_same<Scalar, std::complex<unsigned char> >::value ||
147 std::is_same<Scalar, std::complex<numext::int8_t> >::value ||
148 std::is_same<Scalar, std::complex<numext::uint8_t> >::value,
149 std::complex<int>,
const Scalar&> >;
153 std::streamsize explicit_precision;
154 if (fmt.precision == StreamPrecision) {
155 explicit_precision = 0;
156 }
else if (fmt.precision == FullPrecision) {
157 EIGEN_IF_CONSTEXPR (NumTraits<Scalar>::IsInteger) {
158 explicit_precision = 0;
160 explicit_precision = significant_decimals_impl<Scalar>::run();
163 explicit_precision = fmt.precision;
166 std::streamsize old_precision = 0;
167 if (explicit_precision) old_precision = s.precision(explicit_precision);
169 bool align_cols = !(fmt.flags & DontAlignCols);
172 for (Index j = 0; j < m.cols(); ++j)
173 for (Index i = 0; i < m.rows(); ++i) {
174 std::stringstream sstr;
176 sstr << static_cast<PrintType>(m.coeff(i, j));
177 width = std::max<Index>(width, Index(sstr.str().length()));
180 std::streamsize old_width = s.width();
181 char old_fill_character = s.fill();
183 for (Index i = 0; i < m.rows(); ++i) {
184 if (i) s << fmt.rowSpacer;
190 s << static_cast<PrintType>(m.coeff(i, 0));
191 for (Index j = 1; j < m.cols(); ++j) {
192 s << fmt.coeffSeparator;
197 s << static_cast<PrintType>(m.coeff(i, j));
200 if (i < m.rows() - 1) s << fmt.rowSeparator;
203 if (explicit_precision) s.precision(old_precision);
205 s.fill(old_fill_character);
225template <
typename Derived>
227 return internal::print_matrix(s, m.
eval(), EIGEN_DEFAULT_IO_FORMAT);
230template <
typename Derived>
232 return internal::print_matrix(s, m.
derived(), EIGEN_DEFAULT_IO_FORMAT);
std::ostream & operator<<(std::ostream &s, const DenseBase< Derived > &m)
Definition IO.h:226
constexpr DenseBase()=default
EvalReturnType eval() const
Definition DenseBase.h:389
Base class for diagonal matrices and expressions.
Definition DiagonalMatrix.h:34
const Derived & derived() const
Definition DiagonalMatrix.h:60