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 { Raw, AlignCols };
00030
00050 struct IOFormat
00051 {
00053 IOFormat(int _precision=4, int _flags=Raw,
00054 const std::string& _coeffSeparator = " ",
00055 const std::string& _rowSeparator = "\n", const std::string& _rowPrefix="", const std::string& _rowSuffix="",
00056 const std::string& _matPrefix="", const std::string& _matSuffix="")
00057 : matPrefix(_matPrefix), matSuffix(_matSuffix), rowPrefix(_rowPrefix), rowSuffix(_rowSuffix), rowSeparator(_rowSeparator),
00058 coeffSeparator(_coeffSeparator), precision(_precision), flags(_flags)
00059 {
00060 rowSpacer = "";
00061 int i = int(matSuffix.length())-1;
00062 while (i>=0 && matSuffix[i]!='\n')
00063 {
00064 rowSpacer += ' ';
00065 i--;
00066 }
00067 }
00068 std::string matPrefix, matSuffix;
00069 std::string rowPrefix, rowSuffix, rowSeparator, rowSpacer;
00070 std::string coeffSeparator;
00071 int precision;
00072 int flags;
00073 };
00074
00089 template<typename ExpressionType>
00090 class WithFormat
00091 {
00092 public:
00093
00094 WithFormat(const ExpressionType& matrix, const IOFormat& format)
00095 : m_matrix(matrix), m_format(format)
00096 {}
00097
00098 friend std::ostream & operator << (std::ostream & s, const WithFormat& wf)
00099 {
00100 return ei_print_matrix(s, wf.m_matrix.eval(), wf.m_format);
00101 }
00102
00103 protected:
00104 const typename ExpressionType::Nested m_matrix;
00105 IOFormat m_format;
00106 };
00107
00115 template<typename Derived>
00116 inline const WithFormat<Derived>
00117 MatrixBase<Derived>::format(const IOFormat& fmt) const
00118 {
00119 return WithFormat<Derived>(derived(), fmt);
00120 }
00121
00124 template<typename Derived>
00125 std::ostream & ei_print_matrix(std::ostream & s, const Derived& _m, const IOFormat& fmt)
00126 {
00127 const typename Derived::Nested m = _m;
00128
00129 int width = 0;
00130 if (fmt.flags & AlignCols)
00131 {
00132
00133 for(int j = 1; j < m.cols(); ++j)
00134 for(int i = 0; i < m.rows(); ++i)
00135 {
00136 std::stringstream sstr;
00137 sstr.precision(fmt.precision);
00138 sstr << m.coeff(i,j);
00139 width = std::max<int>(width, int(sstr.str().length()));
00140 }
00141 }
00142 s.precision(fmt.precision);
00143 s << fmt.matPrefix;
00144 for(int i = 0; i < m.rows(); ++i)
00145 {
00146 if (i)
00147 s << fmt.rowSpacer;
00148 s << fmt.rowPrefix;
00149 if(width) s.width(width);
00150 s << m.coeff(i, 0);
00151 for(int j = 1; j < m.cols(); ++j)
00152 {
00153 s << fmt.coeffSeparator;
00154 if (width) s.width(width);
00155 s << m.coeff(i, j);
00156 }
00157 s << fmt.rowSuffix;
00158 if( i < m.rows() - 1)
00159 s << fmt.rowSeparator;
00160 }
00161 s << fmt.matSuffix;
00162 return s;
00163 }
00164
00176 template<typename Derived>
00177 std::ostream & operator <<
00178 (std::ostream & s,
00179 const MatrixBase<Derived> & m)
00180 {
00181 return ei_print_matrix(s, m.eval(), EIGEN_DEFAULT_IO_FORMAT);
00182 }
00183
00184 #endif // EIGEN_IO_H