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