14 #include "./InternalHeaderCheck.h"
18 enum { DontAlignCols = 1 };
19 enum { StreamPrecision = -1,
23 template<
typename Derived>
24 std::ostream & print_matrix(std::ostream & s,
const Derived& _m,
const IOFormat& fmt);
56 IOFormat(
int _precision = StreamPrecision,
int _flags = 0,
57 const std::string& _coeffSeparator =
" ",
58 const std::string& _rowSeparator =
"\n",
const std::string& _rowPrefix=
"",
const std::string& _rowSuffix=
"",
59 const std::string& _matPrefix=
"",
const std::string& _matSuffix=
"",
const char _fill=
' ')
60 : matPrefix(_matPrefix), matSuffix(_matSuffix), rowPrefix(_rowPrefix), rowSuffix(_rowSuffix), rowSeparator(_rowSeparator),
61 rowSpacer(
""), coeffSeparator(_coeffSeparator), fill(_fill), precision(_precision), flags(_flags)
65 if((flags & DontAlignCols))
67 int i = int(matSuffix.length())-1;
68 while (i>=0 && matSuffix[i]!=
'\n')
74 std::string matPrefix, matSuffix;
75 std::string rowPrefix, rowSuffix, rowSeparator, rowSpacer;
76 std::string coeffSeparator;
97 template<
typename ExpressionType>
103 : m_matrix(matrix), m_format(format)
106 friend std::ostream & operator << (std::ostream & s,
const WithFormat& wf)
108 return internal::print_matrix(s, wf.m_matrix.eval(), wf.m_format);
112 typename ExpressionType::Nested m_matrix;
121 template<
typename Scalar>
122 struct significant_decimals_impl
124 static inline int run()
132 template<
typename Derived>
133 std::ostream & print_matrix(std::ostream & s,
const Derived& _m,
const IOFormat& fmt)
135 using internal::is_same;
139 s << fmt.matPrefix << fmt.matSuffix;
143 typename Derived::Nested m = _m;
144 typedef typename Derived::Scalar Scalar;
145 typedef std::conditional_t<
146 is_same<Scalar, char>::value ||
147 is_same<Scalar, unsigned char>::value ||
148 is_same<Scalar, numext::int8_t>::value ||
149 is_same<Scalar, numext::uint8_t>::value,
152 is_same<Scalar, std::complex<char> >::value ||
153 is_same<Scalar, std::complex<unsigned char> >::value ||
154 is_same<Scalar, std::complex<numext::int8_t> >::value ||
155 is_same<Scalar, std::complex<numext::uint8_t> >::value,
163 std::streamsize explicit_precision;
164 if(fmt.precision == StreamPrecision)
166 explicit_precision = 0;
168 else if(fmt.precision == FullPrecision)
170 if (NumTraits<Scalar>::IsInteger)
172 explicit_precision = 0;
176 explicit_precision = significant_decimals_impl<Scalar>::run();
181 explicit_precision = fmt.precision;
184 std::streamsize old_precision = 0;
185 if(explicit_precision) old_precision = s.precision(explicit_precision);
187 bool align_cols = !(fmt.flags & DontAlignCols);
191 for(
Index j = 0; j < m.cols(); ++j)
192 for(
Index i = 0; i < m.rows(); ++i)
194 std::stringstream sstr;
196 sstr << static_cast<PrintType>(m.coeff(i,j));
197 width = std::max<Index>(width,
Index(sstr.str().length()));
200 std::streamsize old_width = s.width();
201 char old_fill_character = s.fill();
203 for(
Index i = 0; i < m.rows(); ++i)
212 s << static_cast<PrintType>(m.coeff(i, 0));
213 for(
Index j = 1; j < m.cols(); ++j)
215 s << fmt.coeffSeparator;
220 s << static_cast<PrintType>(m.coeff(i, j));
223 if( i < m.rows() - 1)
224 s << fmt.rowSeparator;
227 if(explicit_precision) s.precision(old_precision);
229 s.fill(old_fill_character);
248 template<
typename Derived>
249 std::ostream &
operator <<
253 return internal::print_matrix(s, m.eval(), EIGEN_DEFAULT_IO_FORMAT);
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:42
Namespace containing all symbols from the Eigen library.
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:231