15#include "./InternalHeaderCheck.h"
19enum { DontAlignCols = 1 };
20enum { StreamPrecision = -1, FullPrecision = -2 };
23template <
typename Derived>
24std::ostream& print_matrix(std::ostream& s,
const Derived& _m,
const IOFormat& fmt);
54 IOFormat(
int _precision = StreamPrecision,
int _flags = 0,
const std::string& _coeffSeparator =
" ",
55 const std::string& _rowSeparator =
"\n",
const std::string& _rowPrefix =
"",
56 const std::string& _rowSuffix =
"",
const std::string& _matPrefix =
"",
const std::string& _matSuffix =
"",
57 const char _fill =
' ')
58 : matPrefix(_matPrefix),
59 matSuffix(_matSuffix),
60 rowPrefix(_rowPrefix),
61 rowSuffix(_rowSuffix),
62 rowSeparator(_rowSeparator),
64 coeffSeparator(_coeffSeparator),
66 precision(_precision),
70 if ((flags & DontAlignCols))
return;
71 int i = int(matSuffix.length()) - 1;
72 while (i >= 0 && matSuffix[i] !=
'\n') {
77 std::string matPrefix, matSuffix;
78 std::string rowPrefix, rowSuffix, rowSeparator, rowSpacer;
79 std::string coeffSeparator;
100template <
typename ExpressionType>
103 WithFormat(
const ExpressionType& matrix,
const IOFormat& format) : m_matrix(matrix), m_format(format) {}
105 friend std::ostream& operator<<(std::ostream& s,
const WithFormat& wf) {
106 return internal::print_matrix(s, wf.m_matrix.eval(), wf.m_format);
110 typename ExpressionType::Nested m_matrix;
119template <
typename Scalar>
120struct significant_decimals_impl {
126template <
typename Derived>
127std::ostream& print_matrix(std::ostream& s,
const Derived& _m,
const IOFormat& fmt) {
128 using internal::is_same;
130 if (_m.size() == 0) {
131 s << fmt.matPrefix << fmt.matSuffix;
135 typename Derived::Nested m = _m;
136 typedef typename Derived::Scalar Scalar;
137 typedef std::conditional_t<is_same<Scalar, char>::value || is_same<Scalar, unsigned char>::value ||
138 is_same<Scalar, numext::int8_t>::value || is_same<Scalar, numext::uint8_t>::value,
140 std::conditional_t<is_same<Scalar, std::complex<char> >::value ||
141 is_same<Scalar, std::complex<unsigned char> >::value ||
142 is_same<Scalar, std::complex<numext::int8_t> >::value ||
143 is_same<Scalar, std::complex<numext::uint8_t> >::value,
144 std::complex<int>,
const Scalar&> >
149 std::streamsize explicit_precision;
150 if (fmt.precision == StreamPrecision) {
151 explicit_precision = 0;
152 }
else if (fmt.precision == FullPrecision) {
153 if (NumTraits<Scalar>::IsInteger) {
154 explicit_precision = 0;
156 explicit_precision = significant_decimals_impl<Scalar>::run();
159 explicit_precision = fmt.precision;
162 std::streamsize old_precision = 0;
163 if (explicit_precision) old_precision = s.precision(explicit_precision);
165 bool align_cols = !(fmt.flags & DontAlignCols);
168 for (Index j = 0; j < m.cols(); ++j)
169 for (Index i = 0; i < m.rows(); ++i) {
170 std::stringstream sstr;
172 sstr << static_cast<PrintType>(m.coeff(i, j));
173 width = std::max<Index>(width,
Index(sstr.str().length()));
176 std::streamsize old_width = s.width();
177 char old_fill_character = s.fill();
179 for (Index i = 0; i < m.rows(); ++i) {
180 if (i) s << fmt.rowSpacer;
186 s << static_cast<PrintType>(m.coeff(i, 0));
187 for (Index j = 1; j < m.cols(); ++j) {
188 s << fmt.coeffSeparator;
193 s << static_cast<PrintType>(m.coeff(i, j));
196 if (i < m.rows() - 1) s << fmt.rowSeparator;
199 if (explicit_precision) s.precision(old_precision);
201 s.fill(old_fill_character);
221template <
typename Derived>
223 return internal::print_matrix(s, m.
eval(), EIGEN_DEFAULT_IO_FORMAT);
226template <
typename Derived>
228 return internal::print_matrix(s, m.
derived(), EIGEN_DEFAULT_IO_FORMAT);
Base class for all dense matrices, vectors, and arrays.
Definition DenseBase.h:44
std::ostream & operator<<(std::ostream &s, const DenseBase< Derived > &m)
Definition IO.h:222
EvalReturnType eval() const
Definition DenseBase.h:379
Base class for diagonal matrices and expressions.
Definition DiagonalMatrix.h:33
const Derived & derived() const
Definition DiagonalMatrix.h:57
Namespace containing all symbols from the Eigen library.
Definition Core:137
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:83
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition Meta.h:523