Medial Code Documentation
Loading...
Searching...
No Matches
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 rowSpacer(""), coeffSeparator(_coeffSeparator), precision(_precision), flags(_flags)
59 {
60 // TODO check if rowPrefix, rowSuffix or rowSeparator contains a newline
61 // don't add rowSpacer if columns are not to be aligned
62 if((flags & DontAlignCols))
63 return;
64 int i = int(matSuffix.length())-1;
65 while (i>=0 && matSuffix[i]!='\n')
66 {
67 rowSpacer += ' ';
68 i--;
69 }
70 }
71 std::string matPrefix, matSuffix;
72 std::string rowPrefix, rowSuffix, rowSeparator, rowSpacer;
73 std::string coeffSeparator;
74 int precision;
75 int flags;
76};
77
93template<typename ExpressionType>
95{
96 public:
97
98 WithFormat(const ExpressionType& matrix, const IOFormat& format)
99 : m_matrix(matrix), m_format(format)
100 {}
101
102 friend std::ostream & operator << (std::ostream & s, const WithFormat& wf)
103 {
104 return internal::print_matrix(s, wf.m_matrix.eval(), wf.m_format);
105 }
106
107 protected:
108 const typename ExpressionType::Nested m_matrix;
109 IOFormat m_format;
110};
111
119template<typename Derived>
120inline const WithFormat<Derived>
122{
123 return WithFormat<Derived>(derived(), fmt);
124}
125
126namespace internal {
127
128template<typename Scalar, bool IsInteger>
130{
131 typedef typename NumTraits<Scalar>::Real RealScalar;
132 static inline int run()
133 {
134 using std::ceil;
135 using std::log;
137 }
138};
139
140template<typename Scalar>
142{
143 static inline int run()
144 {
145 return 0;
146 }
147};
148
149template<typename Scalar>
151 : significant_decimals_default_impl<Scalar, NumTraits<Scalar>::IsInteger>
152{};
153
156template<typename Derived>
157std::ostream & print_matrix(std::ostream & s, const Derived& _m, const IOFormat& fmt)
158{
159 if(_m.size() == 0)
160 {
161 s << fmt.matPrefix << fmt.matSuffix;
162 return s;
163 }
164
165 typename Derived::Nested m = _m;
166 typedef typename Derived::Scalar Scalar;
167
168 Index width = 0;
169
170 std::streamsize explicit_precision;
171 if(fmt.precision == StreamPrecision)
172 {
174 }
175 else if(fmt.precision == FullPrecision)
176 {
177 if (NumTraits<Scalar>::IsInteger)
178 {
179 explicit_precision = 0;
180 }
181 else
182 {
183 explicit_precision = significant_decimals_impl<Scalar>::run();
184 }
185 }
186 else
187 {
188 explicit_precision = fmt.precision;
189 }
190
191 std::streamsize old_precision = 0;
192 if(explicit_precision) old_precision = s.precision(explicit_precision);
193
194 bool align_cols = !(fmt.flags & DontAlignCols);
195 if(align_cols)
196 {
197 // compute the largest width
198 for(Index j = 0; j < m.cols(); ++j)
199 for(Index i = 0; i < m.rows(); ++i)
200 {
201 std::stringstream sstr;
202 sstr.copyfmt(s);
203 sstr << m.coeff(i,j);
204 width = std::max<Index>(width, Index(sstr.str().length()));
205 }
206 }
207 s << fmt.matPrefix;
208 for(Index i = 0; i < m.rows(); ++i)
209 {
210 if (i)
211 s << fmt.rowSpacer;
212 s << fmt.rowPrefix;
213 if(width) s.width(width);
214 s << m.coeff(i, 0);
215 for(Index j = 1; j < m.cols(); ++j)
216 {
217 s << fmt.coeffSeparator;
218 if (width) s.width(width);
219 s << m.coeff(i, j);
220 }
221 s << fmt.rowSuffix;
222 if( i < m.rows() - 1)
223 s << fmt.rowSeparator;
224 }
225 s << fmt.matSuffix;
226 if(explicit_precision) s.precision(old_precision);
227 return s;
228}
229
230} // end namespace internal
231
243template<typename Derived>
244std::ostream & operator <<
245(std::ostream & s,
246 const DenseBase<Derived> & m)
247{
248 return internal::print_matrix(s, m.eval(), EIGEN_DEFAULT_IO_FORMAT);
249}
250
251} // end namespace Eigen
252
253#endif // EIGEN_IO_H
Base class for all dense matrices, vectors, and arrays.
Definition DenseBase.h:49
Pseudo expression representing a solving operation.
Definition Solve.h:63
Pseudo expression providing matrix output with given format.
Definition IO.h:95
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="")
Default constructor, see class IOFormat for the meaning of the parameters.
Definition IO.h:53
Holds information about the various numeric (i.e.
Definition NumTraits.h:108