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 return cast<RealScalar,int>(std::ceil(-log(NumTraits<RealScalar>::epsilon())/log(RealScalar(10))));
00145 }
00146 };
00147
00148 template<typename Scalar>
00149 struct significant_decimals_default_impl<Scalar, true>
00150 {
00151 static inline int run()
00152 {
00153 return 0;
00154 }
00155 };
00156
00157 template<typename Scalar>
00158 struct significant_decimals_impl
00159 : significant_decimals_default_impl<Scalar, NumTraits<Scalar>::IsInteger>
00160 {};
00161
00164 template<typename Derived>
00165 std::ostream & print_matrix(std::ostream & s, const Derived& _m, const IOFormat& fmt)
00166 {
00167 if(_m.size() == 0)
00168 {
00169 s << fmt.matPrefix << fmt.matSuffix;
00170 return s;
00171 }
00172
00173 const typename Derived::Nested m = _m;
00174 typedef typename Derived::Scalar Scalar;
00175 typedef typename Derived::Index Index;
00176
00177 Index width = 0;
00178
00179 std::streamsize explicit_precision;
00180 if(fmt.precision == StreamPrecision)
00181 {
00182 explicit_precision = 0;
00183 }
00184 else if(fmt.precision == FullPrecision)
00185 {
00186 if (NumTraits<Scalar>::IsInteger)
00187 {
00188 explicit_precision = 0;
00189 }
00190 else
00191 {
00192 explicit_precision = significant_decimals_impl<Scalar>::run();
00193 }
00194 }
00195 else
00196 {
00197 explicit_precision = fmt.precision;
00198 }
00199
00200 bool align_cols = !(fmt.flags & DontAlignCols);
00201 if(align_cols)
00202 {
00203
00204 for(Index j = 1; j < m.cols(); ++j)
00205 for(Index i = 0; i < m.rows(); ++i)
00206 {
00207 std::stringstream sstr;
00208 if(explicit_precision) sstr.precision(explicit_precision);
00209 sstr << m.coeff(i,j);
00210 width = std::max<Index>(width, Index(sstr.str().length()));
00211 }
00212 }
00213 std::streamsize old_precision = 0;
00214 if(explicit_precision) old_precision = s.precision(explicit_precision);
00215 s << fmt.matPrefix;
00216 for(Index i = 0; i < m.rows(); ++i)
00217 {
00218 if (i)
00219 s << fmt.rowSpacer;
00220 s << fmt.rowPrefix;
00221 if(width) s.width(width);
00222 s << m.coeff(i, 0);
00223 for(Index j = 1; j < m.cols(); ++j)
00224 {
00225 s << fmt.coeffSeparator;
00226 if (width) s.width(width);
00227 s << m.coeff(i, j);
00228 }
00229 s << fmt.rowSuffix;
00230 if( i < m.rows() - 1)
00231 s << fmt.rowSeparator;
00232 }
00233 s << fmt.matSuffix;
00234 if(explicit_precision) s.precision(old_precision);
00235 return s;
00236 }
00237
00238 }
00239
00251 template<typename Derived>
00252 std::ostream & operator <<
00253 (std::ostream & s,
00254 const DenseBase<Derived> & m)
00255 {
00256 return internal::print_matrix(s, m.eval(), EIGEN_DEFAULT_IO_FORMAT);
00257 }
00258
00259 #endif // EIGEN_IO_H