16 enum { DontAlignCols = 1 };
17 enum { StreamPrecision = -1,
21 template<
typename Derived>
22 std::ostream & print_matrix(std::ostream & s,
const Derived& _m,
const IOFormat& fmt);
54 IOFormat(
int _precision = StreamPrecision,
int _flags = 0,
55 const std::string& _coeffSeparator =
" ",
56 const std::string& _rowSeparator =
"\n",
const std::string& _rowPrefix=
"",
const std::string& _rowSuffix=
"",
57 const std::string& _matPrefix=
"",
const std::string& _matSuffix=
"",
const char _fill=
' ')
58 : matPrefix(_matPrefix), matSuffix(_matSuffix), rowPrefix(_rowPrefix), rowSuffix(_rowSuffix), rowSeparator(_rowSeparator),
59 rowSpacer(
""), coeffSeparator(_coeffSeparator), fill(_fill), precision(_precision), flags(_flags)
63 if((flags & DontAlignCols))
65 int i = int(matSuffix.length())-1;
66 while (i>=0 && matSuffix[i]!=
'\n')
72 std::string matPrefix, matSuffix;
73 std::string rowPrefix, rowSuffix, rowSeparator, rowSpacer;
74 std::string coeffSeparator;
95 template<
typename ExpressionType>
101 : m_matrix(matrix), m_format(format)
104 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;
119 template<
typename Scalar>
120 struct significant_decimals_impl
122 static inline int run()
130 template<
typename Derived>
131 std::ostream & print_matrix(std::ostream & s,
const Derived& _m,
const IOFormat& fmt)
133 using internal::is_same;
134 using internal::conditional;
138 s << fmt.matPrefix << fmt.matSuffix;
142 typename Derived::Nested m = _m;
143 typedef typename Derived::Scalar Scalar;
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,
151 typename conditional<
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:47
Namespace containing all symbols from the Eigen library.
Definition: Core:134
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:213