1
// This file is part of Eigen, a lightweight C++ template library
4
// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
5
// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
7
// Eigen is free software; you can redistribute it and/or
8
// modify it under the terms of the GNU Lesser General Public
9
// License as published by the Free Software Foundation; either
10
// version 3 of the License, or (at your option) any later version.
12
// Alternatively, you can redistribute it and/or
13
// modify it under the terms of the GNU General Public License as
14
// published by the Free Software Foundation; either version 2 of
15
// the License, or (at your option) any later version.
17
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20
// GNU General Public License for more details.
22
// You should have received a copy of the GNU Lesser General Public
23
// License and a copy of the GNU General Public License along with
24
// Eigen. If not, see <http://www.gnu.org/licenses/>.
29
enum { DontAlignCols = 1 };
30
enum { StreamPrecision = -1,
34
template<typename Derived>
35
std::ostream & print_matrix(std::ostream & s, const Derived& _m, const IOFormat& fmt);
39
* \ingroup Core_Module
41
* \brief Stores a set of parameters controlling the way matrices are printed
43
* List of available parameters:
44
* - \b precision number of digits for floating point values, or one of the special constants \c StreamPrecision and \c FullPrecision.
45
* The default is the special value \c StreamPrecision which means to use the
46
* stream's own precision setting, as set for instance using \c cout.precision(3). The other special value
47
* \c FullPrecision means that the number of digits will be computed to match the full precision of each floating-point
49
* - \b flags an OR-ed combination of flags, the default value is 0, the only currently available flag is \c DontAlignCols which
50
* allows to disable the alignment of columns, resulting in faster code.
51
* - \b coeffSeparator string printed between two coefficients of the same row
52
* - \b rowSeparator string printed between two rows
53
* - \b rowPrefix string printed at the beginning of each row
54
* - \b rowSuffix string printed at the end of each row
55
* - \b matPrefix string printed at the beginning of the matrix
56
* - \b matSuffix string printed at the end of the matrix
58
* Example: \include IOFormat.cpp
59
* Output: \verbinclude IOFormat.out
61
* \sa DenseBase::format(), class WithFormat
65
/** Default contructor, see class IOFormat for the meaning of the parameters */
66
IOFormat(int _precision = StreamPrecision, int _flags = 0,
67
const std::string& _coeffSeparator = " ",
68
const std::string& _rowSeparator = "\n", const std::string& _rowPrefix="", const std::string& _rowSuffix="",
69
const std::string& _matPrefix="", const std::string& _matSuffix="")
70
: matPrefix(_matPrefix), matSuffix(_matSuffix), rowPrefix(_rowPrefix), rowSuffix(_rowSuffix), rowSeparator(_rowSeparator),
71
coeffSeparator(_coeffSeparator), precision(_precision), flags(_flags)
74
int i = int(matSuffix.length())-1;
75
while (i>=0 && matSuffix[i]!='\n')
81
std::string matPrefix, matSuffix;
82
std::string rowPrefix, rowSuffix, rowSeparator, rowSpacer;
83
std::string coeffSeparator;
89
* \ingroup Core_Module
91
* \brief Pseudo expression providing matrix output with given format
93
* \param ExpressionType the type of the object on which IO stream operations are performed
95
* This class represents an expression with stream operators controlled by a given IOFormat.
96
* It is the return type of DenseBase::format()
97
* and most of the time this is the only way it is used.
99
* See class IOFormat for some examples.
101
* \sa DenseBase::format(), class IOFormat
103
template<typename ExpressionType>
108
WithFormat(const ExpressionType& matrix, const IOFormat& format)
109
: m_matrix(matrix), m_format(format)
112
friend std::ostream & operator << (std::ostream & s, const WithFormat& wf)
114
return internal::print_matrix(s, wf.m_matrix.eval(), wf.m_format);
118
const typename ExpressionType::Nested m_matrix;
122
/** \returns a WithFormat proxy object allowing to print a matrix the with given
125
* See class IOFormat for some examples.
127
* \sa class IOFormat, class WithFormat
129
template<typename Derived>
130
inline const WithFormat<Derived>
131
DenseBase<Derived>::format(const IOFormat& fmt) const
133
return WithFormat<Derived>(derived(), fmt);
138
template<typename Scalar, bool IsInteger>
139
struct significant_decimals_default_impl
141
typedef typename NumTraits<Scalar>::Real RealScalar;
142
static inline int run()
145
return cast<RealScalar,int>(ceil(-log(NumTraits<RealScalar>::epsilon())/log(RealScalar(10))));
149
template<typename Scalar>
150
struct significant_decimals_default_impl<Scalar, true>
152
static inline int run()
158
template<typename Scalar>
159
struct significant_decimals_impl
160
: significant_decimals_default_impl<Scalar, NumTraits<Scalar>::IsInteger>
164
* print the matrix \a _m to the output stream \a s using the output format \a fmt */
165
template<typename Derived>
166
std::ostream & print_matrix(std::ostream & s, const Derived& _m, const IOFormat& fmt)
170
s << fmt.matPrefix << fmt.matSuffix;
174
const typename Derived::Nested m = _m;
175
typedef typename Derived::Scalar Scalar;
176
typedef typename Derived::Index Index;
180
std::streamsize explicit_precision;
181
if(fmt.precision == StreamPrecision)
183
explicit_precision = 0;
185
else if(fmt.precision == FullPrecision)
187
if (NumTraits<Scalar>::IsInteger)
189
explicit_precision = 0;
193
explicit_precision = significant_decimals_impl<Scalar>::run();
198
explicit_precision = fmt.precision;
201
bool align_cols = !(fmt.flags & DontAlignCols);
204
// compute the largest width
205
for(Index j = 1; j < m.cols(); ++j)
206
for(Index i = 0; i < m.rows(); ++i)
208
std::stringstream sstr;
209
if(explicit_precision) sstr.precision(explicit_precision);
210
sstr << m.coeff(i,j);
211
width = std::max<Index>(width, Index(sstr.str().length()));
214
std::streamsize old_precision = 0;
215
if(explicit_precision) old_precision = s.precision(explicit_precision);
217
for(Index i = 0; i < m.rows(); ++i)
222
if(width) s.width(width);
224
for(Index j = 1; j < m.cols(); ++j)
226
s << fmt.coeffSeparator;
227
if (width) s.width(width);
231
if( i < m.rows() - 1)
232
s << fmt.rowSeparator;
235
if(explicit_precision) s.precision(old_precision);
239
} // end namespace internal
241
/** \relates DenseBase
243
* Outputs the matrix, to the given stream.
245
* If you wish to print the matrix with a format different than the default, use DenseBase::format().
247
* It is also possible to change the default format by defining EIGEN_DEFAULT_IO_FORMAT before including Eigen headers.
248
* If not defined, this will automatically be defined to Eigen::IOFormat(), that is the Eigen::IOFormat with default parameters.
250
* \sa DenseBase::format()
252
template<typename Derived>
253
std::ostream & operator <<
255
const DenseBase<Derived> & m)
257
return internal::print_matrix(s, m.eval(), EIGEN_DEFAULT_IO_FORMAT);