IO.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
5// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
11#ifndef EIGEN_IO_H
12#define EIGEN_IO_H
13
14namespace Eigen {
15
16enum { DontAlignCols = 1 };
17enum { StreamPrecision = -1,
18 FullPrecision = -2 };
19
20namespace internal {
21template<typename Derived>
22std::ostream & print_matrix(std::ostream & s, const Derived& _m, const IOFormat& fmt);
23}
24
51{
53 IOFormat(int _precision = StreamPrecision, int _flags = 0,
54 const std::string& _coeffSeparator = " ",
55 const std::string& _rowSeparator = "\n", const std::string& _rowPrefix="", const std::string& _rowSuffix="",
56 const std::string& _matPrefix="", const std::string& _matSuffix="")
57 : matPrefix(_matPrefix), matSuffix(_matSuffix), rowPrefix(_rowPrefix), rowSuffix(_rowSuffix), rowSeparator(_rowSeparator),
58 coeffSeparator(_coeffSeparator), precision(_precision), flags(_flags)
59 {
60 rowSpacer = "";
61 int i = int(matSuffix.length())-1;
62 while (i>=0 && matSuffix[i]!='\n')
63 {
64 rowSpacer += ' ';
65 i--;
66 }
67 }
68 std::string matPrefix, matSuffix;
69 std::string rowPrefix, rowSuffix, rowSeparator, rowSpacer;
70 std::string coeffSeparator;
71 int precision;
72 int flags;
73};
74
90template<typename ExpressionType>
91class WithFormat
92{
93 public:
94
95 WithFormat(const ExpressionType& matrix, const IOFormat& format)
96 : m_matrix(matrix), m_format(format)
97 {}
98
99 friend std::ostream & operator << (std::ostream & s, const WithFormat& wf)
100 {
101 return internal::print_matrix(s, wf.m_matrix.eval(), wf.m_format);
102 }
103
104 protected:
105 const typename ExpressionType::Nested m_matrix;
106 IOFormat m_format;
107};
108
116template<typename Derived>
117inline const WithFormat<Derived>
119{
120 return WithFormat<Derived>(derived(), fmt);
121}
122
123namespace internal {
124
125template<typename Scalar, bool IsInteger>
126struct significant_decimals_default_impl
127{
128 typedef typename NumTraits<Scalar>::Real RealScalar;
129 static inline int run()
130 {
131 using std::ceil;
132 return cast<RealScalar,int>(ceil(-log(NumTraits<RealScalar>::epsilon())/log(RealScalar(10))));
133 }
134};
135
136template<typename Scalar>
137struct significant_decimals_default_impl<Scalar, true>
138{
139 static inline int run()
140 {
141 return 0;
142 }
143};
144
145template<typename Scalar>
146struct significant_decimals_impl
147 : significant_decimals_default_impl<Scalar, NumTraits<Scalar>::IsInteger>
148{};
149
152template<typename Derived>
153std::ostream & print_matrix(std::ostream & s, const Derived& _m, const IOFormat& fmt)
154{
155 if(_m.size() == 0)
156 {
157 s << fmt.matPrefix << fmt.matSuffix;
158 return s;
159 }
160
161 typename Derived::Nested m = _m;
162 typedef typename Derived::Scalar Scalar;
163 typedef typename Derived::Index Index;
164
165 Index width = 0;
166
167 std::streamsize explicit_precision;
168 if(fmt.precision == StreamPrecision)
169 {
170 explicit_precision = 0;
171 }
172 else if(fmt.precision == FullPrecision)
173 {
174 if (NumTraits<Scalar>::IsInteger)
175 {
176 explicit_precision = 0;
177 }
178 else
179 {
180 explicit_precision = significant_decimals_impl<Scalar>::run();
181 }
182 }
183 else
184 {
185 explicit_precision = fmt.precision;
186 }
187
188 bool align_cols = !(fmt.flags & DontAlignCols);
189 if(align_cols)
190 {
191 // compute the largest width
192 for(Index j = 1; j < m.cols(); ++j)
193 for(Index i = 0; i < m.rows(); ++i)
194 {
195 std::stringstream sstr;
196 if(explicit_precision) sstr.precision(explicit_precision);
197 sstr << m.coeff(i,j);
198 width = std::max<Index>(width, Index(sstr.str().length()));
199 }
200 }
201 std::streamsize old_precision = 0;
202 if(explicit_precision) old_precision = s.precision(explicit_precision);
203 s << fmt.matPrefix;
204 for(Index i = 0; i < m.rows(); ++i)
205 {
206 if (i)
207 s << fmt.rowSpacer;
208 s << fmt.rowPrefix;
209 if(width) s.width(width);
210 s << m.coeff(i, 0);
211 for(Index j = 1; j < m.cols(); ++j)
212 {
213 s << fmt.coeffSeparator;
214 if (width) s.width(width);
215 s << m.coeff(i, j);
216 }
217 s << fmt.rowSuffix;
218 if( i < m.rows() - 1)
219 s << fmt.rowSeparator;
220 }
221 s << fmt.matSuffix;
222 if(explicit_precision) s.precision(old_precision);
223 return s;
224}
225
226} // end namespace internal
227
239template<typename Derived>
240std::ostream & operator <<
241(std::ostream & s,
242 const DenseBase<Derived> & m)
243{
244 return internal::print_matrix(s, m.eval(), EIGEN_DEFAULT_IO_FORMAT);
245}
246
247} // end namespace Eigen
248
249#endif // EIGEN_IO_H
const WithFormat< Derived > format(const IOFormat &fmt) const
Definition IO.h:118
Pseudo expression providing matrix output with given format.
Definition IO.h:92
Definition LDLT.h:18
Stores a set of parameters controlling the way matrices are printed.
Definition IO.h:51
IOFormat(int _precision=StreamPrecision, int _flags=0, const std::string &_coeffSeparator=" ", const std::string &_rowSeparator="\n", const std::string &_rowPrefix="", const std::string &_rowSuffix="", const std::string &_matPrefix="", const std::string &_matSuffix="")
Definition IO.h:53