File indexing completed on 2025-01-18 09:56:14
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef EIGEN_IO_H
0012 #define EIGEN_IO_H
0013
0014 namespace Eigen {
0015
0016 enum { DontAlignCols = 1 };
0017 enum { StreamPrecision = -1,
0018 FullPrecision = -2 };
0019
0020 namespace internal {
0021 template<typename Derived>
0022 std::ostream & print_matrix(std::ostream & s, const Derived& _m, const IOFormat& fmt);
0023 }
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051 struct IOFormat
0052 {
0053
0054 IOFormat(int _precision = StreamPrecision, int _flags = 0,
0055 const std::string& _coeffSeparator = " ",
0056 const std::string& _rowSeparator = "\n", const std::string& _rowPrefix="", const std::string& _rowSuffix="",
0057 const std::string& _matPrefix="", const std::string& _matSuffix="", const char _fill=' ')
0058 : matPrefix(_matPrefix), matSuffix(_matSuffix), rowPrefix(_rowPrefix), rowSuffix(_rowSuffix), rowSeparator(_rowSeparator),
0059 rowSpacer(""), coeffSeparator(_coeffSeparator), fill(_fill), precision(_precision), flags(_flags)
0060 {
0061
0062
0063 if((flags & DontAlignCols))
0064 return;
0065 int i = int(matSuffix.length())-1;
0066 while (i>=0 && matSuffix[i]!='\n')
0067 {
0068 rowSpacer += ' ';
0069 i--;
0070 }
0071 }
0072 std::string matPrefix, matSuffix;
0073 std::string rowPrefix, rowSuffix, rowSeparator, rowSpacer;
0074 std::string coeffSeparator;
0075 char fill;
0076 int precision;
0077 int flags;
0078 };
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095 template<typename ExpressionType>
0096 class WithFormat
0097 {
0098 public:
0099
0100 WithFormat(const ExpressionType& matrix, const IOFormat& format)
0101 : m_matrix(matrix), m_format(format)
0102 {}
0103
0104 friend std::ostream & operator << (std::ostream & s, const WithFormat& wf)
0105 {
0106 return internal::print_matrix(s, wf.m_matrix.eval(), wf.m_format);
0107 }
0108
0109 protected:
0110 typename ExpressionType::Nested m_matrix;
0111 IOFormat m_format;
0112 };
0113
0114 namespace internal {
0115
0116
0117
0118
0119 template<typename Scalar>
0120 struct significant_decimals_impl
0121 {
0122 static inline int run()
0123 {
0124 return NumTraits<Scalar>::digits10();
0125 }
0126 };
0127
0128
0129
0130 template<typename Derived>
0131 std::ostream & print_matrix(std::ostream & s, const Derived& _m, const IOFormat& fmt)
0132 {
0133 using internal::is_same;
0134 using internal::conditional;
0135
0136 if(_m.size() == 0)
0137 {
0138 s << fmt.matPrefix << fmt.matSuffix;
0139 return s;
0140 }
0141
0142 typename Derived::Nested m = _m;
0143 typedef typename Derived::Scalar Scalar;
0144 typedef typename
0145 conditional<
0146 is_same<Scalar, char>::value ||
0147 is_same<Scalar, unsigned char>::value ||
0148 is_same<Scalar, numext::int8_t>::value ||
0149 is_same<Scalar, numext::uint8_t>::value,
0150 int,
0151 typename conditional<
0152 is_same<Scalar, std::complex<char> >::value ||
0153 is_same<Scalar, std::complex<unsigned char> >::value ||
0154 is_same<Scalar, std::complex<numext::int8_t> >::value ||
0155 is_same<Scalar, std::complex<numext::uint8_t> >::value,
0156 std::complex<int>,
0157 const Scalar&
0158 >::type
0159 >::type PrintType;
0160
0161 Index width = 0;
0162
0163 std::streamsize explicit_precision;
0164 if(fmt.precision == StreamPrecision)
0165 {
0166 explicit_precision = 0;
0167 }
0168 else if(fmt.precision == FullPrecision)
0169 {
0170 if (NumTraits<Scalar>::IsInteger)
0171 {
0172 explicit_precision = 0;
0173 }
0174 else
0175 {
0176 explicit_precision = significant_decimals_impl<Scalar>::run();
0177 }
0178 }
0179 else
0180 {
0181 explicit_precision = fmt.precision;
0182 }
0183
0184 std::streamsize old_precision = 0;
0185 if(explicit_precision) old_precision = s.precision(explicit_precision);
0186
0187 bool align_cols = !(fmt.flags & DontAlignCols);
0188 if(align_cols)
0189 {
0190
0191 for(Index j = 0; j < m.cols(); ++j)
0192 for(Index i = 0; i < m.rows(); ++i)
0193 {
0194 std::stringstream sstr;
0195 sstr.copyfmt(s);
0196 sstr << static_cast<PrintType>(m.coeff(i,j));
0197 width = std::max<Index>(width, Index(sstr.str().length()));
0198 }
0199 }
0200 std::streamsize old_width = s.width();
0201 char old_fill_character = s.fill();
0202 s << fmt.matPrefix;
0203 for(Index i = 0; i < m.rows(); ++i)
0204 {
0205 if (i)
0206 s << fmt.rowSpacer;
0207 s << fmt.rowPrefix;
0208 if(width) {
0209 s.fill(fmt.fill);
0210 s.width(width);
0211 }
0212 s << static_cast<PrintType>(m.coeff(i, 0));
0213 for(Index j = 1; j < m.cols(); ++j)
0214 {
0215 s << fmt.coeffSeparator;
0216 if(width) {
0217 s.fill(fmt.fill);
0218 s.width(width);
0219 }
0220 s << static_cast<PrintType>(m.coeff(i, j));
0221 }
0222 s << fmt.rowSuffix;
0223 if( i < m.rows() - 1)
0224 s << fmt.rowSeparator;
0225 }
0226 s << fmt.matSuffix;
0227 if(explicit_precision) s.precision(old_precision);
0228 if(width) {
0229 s.fill(old_fill_character);
0230 s.width(old_width);
0231 }
0232 return s;
0233 }
0234
0235 }
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248 template<typename Derived>
0249 std::ostream & operator <<
0250 (std::ostream & s,
0251 const DenseBase<Derived> & m)
0252 {
0253 return internal::print_matrix(s, m.eval(), EIGEN_DEFAULT_IO_FORMAT);
0254 }
0255
0256 }
0257
0258 #endif