~choreonoid/choreonoid/debian

« back to all changes in this revision

Viewing changes to thirdparty/eigen3/Eigen/src/Core/IO.h

  • Committer: Thomas Moulard
  • Date: 2012-10-23 12:43:24 UTC
  • Revision ID: git-v1:351cf736ad49bc7a9a7b9767dee760a013517a5d
Tags: upstream/1.1.0
ImportedĀ UpstreamĀ versionĀ 1.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
// 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.
 
11
//
 
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.
 
16
//
 
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.
 
21
//
 
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/>.
 
25
 
 
26
#ifndef EIGEN_IO_H
 
27
#define EIGEN_IO_H
 
28
 
 
29
enum { DontAlignCols = 1 };
 
30
enum { StreamPrecision = -1,
 
31
       FullPrecision = -2 };
 
32
 
 
33
namespace internal {
 
34
template<typename Derived>
 
35
std::ostream & print_matrix(std::ostream & s, const Derived& _m, const IOFormat& fmt);
 
36
}
 
37
 
 
38
/** \class IOFormat
 
39
  * \ingroup Core_Module
 
40
  *
 
41
  * \brief Stores a set of parameters controlling the way matrices are printed
 
42
  *
 
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
 
48
  *                 type.
 
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
 
57
  *
 
58
  * Example: \include IOFormat.cpp
 
59
  * Output: \verbinclude IOFormat.out
 
60
  *
 
61
  * \sa DenseBase::format(), class WithFormat
 
62
  */
 
63
struct IOFormat
 
64
{
 
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)
 
72
  {
 
73
    rowSpacer = "";
 
74
    int i = int(matSuffix.length())-1;
 
75
    while (i>=0 && matSuffix[i]!='\n')
 
76
    {
 
77
      rowSpacer += ' ';
 
78
      i--;
 
79
    }
 
80
  }
 
81
  std::string matPrefix, matSuffix;
 
82
  std::string rowPrefix, rowSuffix, rowSeparator, rowSpacer;
 
83
  std::string coeffSeparator;
 
84
  int precision;
 
85
  int flags;
 
86
};
 
87
 
 
88
/** \class WithFormat
 
89
  * \ingroup Core_Module
 
90
  *
 
91
  * \brief Pseudo expression providing matrix output with given format
 
92
  *
 
93
  * \param ExpressionType the type of the object on which IO stream operations are performed
 
94
  *
 
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.
 
98
  *
 
99
  * See class IOFormat for some examples.
 
100
  *
 
101
  * \sa DenseBase::format(), class IOFormat
 
102
  */
 
103
template<typename ExpressionType>
 
104
class WithFormat
 
105
{
 
106
  public:
 
107
 
 
108
    WithFormat(const ExpressionType& matrix, const IOFormat& format)
 
109
      : m_matrix(matrix), m_format(format)
 
110
    {}
 
111
 
 
112
    friend std::ostream & operator << (std::ostream & s, const WithFormat& wf)
 
113
    {
 
114
      return internal::print_matrix(s, wf.m_matrix.eval(), wf.m_format);
 
115
    }
 
116
 
 
117
  protected:
 
118
    const typename ExpressionType::Nested m_matrix;
 
119
    IOFormat m_format;
 
120
};
 
121
 
 
122
/** \returns a WithFormat proxy object allowing to print a matrix the with given
 
123
  * format \a fmt.
 
124
  *
 
125
  * See class IOFormat for some examples.
 
126
  *
 
127
  * \sa class IOFormat, class WithFormat
 
128
  */
 
129
template<typename Derived>
 
130
inline const WithFormat<Derived>
 
131
DenseBase<Derived>::format(const IOFormat& fmt) const
 
132
{
 
133
  return WithFormat<Derived>(derived(), fmt);
 
134
}
 
135
 
 
136
namespace internal {
 
137
 
 
138
template<typename Scalar, bool IsInteger>
 
139
struct significant_decimals_default_impl
 
140
{
 
141
  typedef typename NumTraits<Scalar>::Real RealScalar;
 
142
  static inline int run()
 
143
  {
 
144
    using std::ceil;
 
145
    return cast<RealScalar,int>(ceil(-log(NumTraits<RealScalar>::epsilon())/log(RealScalar(10))));
 
146
  }
 
147
};
 
148
 
 
149
template<typename Scalar>
 
150
struct significant_decimals_default_impl<Scalar, true>
 
151
{
 
152
  static inline int run()
 
153
  {
 
154
    return 0;
 
155
  }
 
156
};
 
157
 
 
158
template<typename Scalar>
 
159
struct significant_decimals_impl
 
160
  : significant_decimals_default_impl<Scalar, NumTraits<Scalar>::IsInteger>
 
161
{};
 
162
 
 
163
/** \internal
 
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)
 
167
{
 
168
  if(_m.size() == 0)
 
169
  {
 
170
    s << fmt.matPrefix << fmt.matSuffix;
 
171
    return s;
 
172
  }
 
173
  
 
174
  const typename Derived::Nested m = _m;
 
175
  typedef typename Derived::Scalar Scalar;
 
176
  typedef typename Derived::Index Index;
 
177
 
 
178
  Index width = 0;
 
179
 
 
180
  std::streamsize explicit_precision;
 
181
  if(fmt.precision == StreamPrecision)
 
182
  {
 
183
    explicit_precision = 0;
 
184
  }
 
185
  else if(fmt.precision == FullPrecision)
 
186
  {
 
187
    if (NumTraits<Scalar>::IsInteger)
 
188
    {
 
189
      explicit_precision = 0;
 
190
    }
 
191
    else
 
192
    {
 
193
      explicit_precision = significant_decimals_impl<Scalar>::run();
 
194
    }
 
195
  }
 
196
  else
 
197
  {
 
198
    explicit_precision = fmt.precision;
 
199
  }
 
200
 
 
201
  bool align_cols = !(fmt.flags & DontAlignCols);
 
202
  if(align_cols)
 
203
  {
 
204
    // compute the largest width
 
205
    for(Index j = 1; j < m.cols(); ++j)
 
206
      for(Index i = 0; i < m.rows(); ++i)
 
207
      {
 
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()));
 
212
      }
 
213
  }
 
214
  std::streamsize old_precision = 0;
 
215
  if(explicit_precision) old_precision = s.precision(explicit_precision);
 
216
  s << fmt.matPrefix;
 
217
  for(Index i = 0; i < m.rows(); ++i)
 
218
  {
 
219
    if (i)
 
220
      s << fmt.rowSpacer;
 
221
    s << fmt.rowPrefix;
 
222
    if(width) s.width(width);
 
223
    s << m.coeff(i, 0);
 
224
    for(Index j = 1; j < m.cols(); ++j)
 
225
    {
 
226
      s << fmt.coeffSeparator;
 
227
      if (width) s.width(width);
 
228
      s << m.coeff(i, j);
 
229
    }
 
230
    s << fmt.rowSuffix;
 
231
    if( i < m.rows() - 1)
 
232
      s << fmt.rowSeparator;
 
233
  }
 
234
  s << fmt.matSuffix;
 
235
  if(explicit_precision) s.precision(old_precision);
 
236
  return s;
 
237
}
 
238
 
 
239
} // end namespace internal
 
240
 
 
241
/** \relates DenseBase
 
242
  *
 
243
  * Outputs the matrix, to the given stream.
 
244
  *
 
245
  * If you wish to print the matrix with a format different than the default, use DenseBase::format().
 
246
  *
 
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.
 
249
  *
 
250
  * \sa DenseBase::format()
 
251
  */
 
252
template<typename Derived>
 
253
std::ostream & operator <<
 
254
(std::ostream & s,
 
255
 const DenseBase<Derived> & m)
 
256
{
 
257
  return internal::print_matrix(s, m.eval(), EIGEN_DEFAULT_IO_FORMAT);
 
258
}
 
259
 
 
260
#endif // EIGEN_IO_H