Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef EIGEN_IO_H
00027 #define EIGEN_IO_H
00028
00029 enum { DontAlignCols = 1 };
00030 enum { StreamPrecision = -1,
00031 FullPrecision = -2 };
00032
00033 namespace internal {
00034 template<typename Derived>
00035 std::ostream & print_matrix(std::ostream & s, const Derived& _m, const IOFormat& fmt);
00036 }
00037
00063 struct IOFormat
00064 {
00066 IOFormat(int _precision = StreamPrecision, int _flags = 0,
00067 const std::string& _coeffSeparator = " ",
00068 const std::string& _rowSeparator = "\n", const std::string& _rowPrefix="", const std::string& _rowSuffix="",
00069 const std::string& _matPrefix="", const std::string& _matSuffix="")
00070 : matPrefix(_matPrefix), matSuffix(_matSuffix), rowPrefix(_rowPrefix), rowSuffix(_rowSuffix), rowSeparator(_rowSeparator),
00071 coeffSeparator(_coeffSeparator), precision(_precision), flags(_flags)
00072 {
00073 rowSpacer = "";
00074 int i = int(matSuffix.length())-1;
00075 while (i>=0 && matSuffix[i]!='\n')
00076 {
00077 rowSpacer += ' ';
00078 i--;
00079 }
00080 }
00081 std::string matPrefix, matSuffix;
00082 std::string rowPrefix, rowSuffix, rowSeparator, rowSpacer;
00083 std::string coeffSeparator;
00084 int precision;
00085 int flags;
00086 };
00087
00103 template<typename ExpressionType>
00104 class WithFormat
00105 {
00106 public:
00107
00108 WithFormat(const ExpressionType& matrix, const IOFormat& format)
00109 : m_matrix(matrix), m_format(format)
00110 {}
00111
00112 friend std::ostream & operator << (std::ostream & s, const WithFormat& wf)
00113 {
00114 return internal::print_matrix(s, wf.m_matrix.eval(), wf.m_format);
00115 }
00116
00117 protected:
00118 const typename ExpressionType::Nested m_matrix;
00119 IOFormat m_format;
00120 };
00121
00129 template<typename Derived>
00130 inline const WithFormat<Derived>
00131 DenseBase<Derived>::format(const IOFormat& fmt) const
00132 {
00133 return WithFormat<Derived>(derived(), fmt);
00134 }
00135
00136 namespace internal {
00137
00138 template<typename Scalar, bool IsInteger>
00139 struct significant_decimals_default_impl
00140 {
00141 typedef typename NumTraits<Scalar>::Real RealScalar;
00142 static inline int run()
00143 {
00144 using std::ceil;
00145 return cast<RealScalar,int>(ceil(-log(NumTraits<RealScalar>::epsilon())/log(RealScalar(10))));
00146 }
00147 };
00148
00149 template<typename Scalar>
00150 struct significant_decimals_default_impl<Scalar, true>
00151 {
00152 static inline int run()
00153 {
00154 return 0;
00155 }
00156 };
00157
00158 template<typename Scalar>
00159 struct significant_decimals_impl
00160 : significant_decimals_default_impl<Scalar, NumTraits<Scalar>::IsInteger>
00161 {};
00162
00165 template<typename Derived>
00166 std::ostream & print_matrix(std::ostream & s, const Derived& _m, const IOFormat& fmt)
00167 {
00168 if(_m.size() == 0)
00169 {
00170 s << fmt.matPrefix << fmt.matSuffix;
00171 return s;
00172 }
00173
00174 const typename Derived::Nested m = _m;
00175 typedef typename Derived::Scalar Scalar;
00176 typedef typename Derived::Index Index;
00177
00178 Index width = 0;
00179
00180 std::streamsize explicit_precision;
00181 if(fmt.precision == StreamPrecision)
00182 {
00183 explicit_precision = 0;
00184 }
00185 else if(fmt.precision == FullPrecision)
00186 {
00187 if (NumTraits<Scalar>::IsInteger)
00188 {
00189 explicit_precision = 0;
00190 }
00191 else
00192 {
00193 explicit_precision = significant_decimals_impl<Scalar>::run();
00194 }
00195 }
00196 else
00197 {
00198 explicit_precision = fmt.precision;
00199 }
00200
00201 bool align_cols = !(fmt.flags & DontAlignCols);
00202 if(align_cols)
00203 {
00204
00205 for(Index j = 1; j < m.cols(); ++j)
00206 for(Index i = 0; i < m.rows(); ++i)
00207 {
00208 std::stringstream sstr;
00209 if(explicit_precision) sstr.precision(explicit_precision);
00210 sstr << m.coeff(i,j);
00211 width = std::max<Index>(width, Index(sstr.str().length()));
00212 }
00213 }
00214 std::streamsize old_precision = 0;
00215 if(explicit_precision) old_precision = s.precision(explicit_precision);
00216 s << fmt.matPrefix;
00217 for(Index i = 0; i < m.rows(); ++i)
00218 {
00219 if (i)
00220 s << fmt.rowSpacer;
00221 s << fmt.rowPrefix;
00222 if(width) s.width(width);
00223 s << m.coeff(i, 0);
00224 for(Index j = 1; j < m.cols(); ++j)
00225 {
00226 s << fmt.coeffSeparator;
00227 if (width) s.width(width);
00228 s << m.coeff(i, j);
00229 }
00230 s << fmt.rowSuffix;
00231 if( i < m.rows() - 1)
00232 s << fmt.rowSeparator;
00233 }
00234 s << fmt.matSuffix;
00235 if(explicit_precision) s.precision(old_precision);
00236 return s;
00237 }
00238
00239 }
00240
00252 template<typename Derived>
00253 std::ostream & operator <<
00254 (std::ostream & s,
00255 const DenseBase<Derived> & m)
00256 {
00257 return internal::print_matrix(s, m.eval(), EIGEN_DEFAULT_IO_FORMAT);
00258 }
00259
00260 #endif // EIGEN_IO_H