IO.h
Go to the documentation of this file.
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 
14 namespace Eigen {
15 
16 enum { DontAlignCols = 1 };
17 enum { StreamPrecision = -1,
18  FullPrecision = -2 };
19 
20 namespace internal {
21 template<typename Derived>
22 std::ostream & print_matrix(std::ostream & s, const Derived& _m, const IOFormat& fmt);
23 }
24 
50 struct IOFormat
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  int i = int(matSuffix.length())-1;
61  while (i>=0 && matSuffix[i]!='\n')
62  {
63  rowSpacer += ' ';
64  i--;
65  }
66  }
67  std::string matPrefix, matSuffix;
68  std::string rowPrefix, rowSuffix, rowSeparator, rowSpacer;
69  std::string coeffSeparator;
70  int precision;
71  int flags;
72 };
73 
89 template<typename ExpressionType>
91 {
92  public:
93 
94  WithFormat(const ExpressionType& matrix, const IOFormat& format)
95  : m_matrix(matrix), m_format(format)
96  {}
97 
98  friend std::ostream & operator << (std::ostream & s, const WithFormat& wf)
99  {
100  return internal::print_matrix(s, wf.m_matrix.eval(), wf.m_format);
101  }
102 
103  protected:
104  const typename ExpressionType::Nested m_matrix;
106 };
107 
115 template<typename Derived>
116 inline const WithFormat<Derived>
118 {
119  return WithFormat<Derived>(derived(), fmt);
120 }
121 
122 namespace internal {
123 
124 template<typename Scalar, bool IsInteger>
126 {
128  static inline int run()
129  {
130  using std::ceil;
131  using std::log;
132  return cast<RealScalar,int>(ceil(-log(NumTraits<RealScalar>::epsilon())/log(RealScalar(10))));
133  }
134 };
135 
136 template<typename Scalar>
138 {
139  static inline int run()
140  {
141  return 0;
142  }
143 };
144 
145 template<typename Scalar>
147  : significant_decimals_default_impl<Scalar, NumTraits<Scalar>::IsInteger>
148 {};
149 
152 template<typename Derived>
153 std::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  {
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 
239 template<typename Derived>
240 std::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
WithFormat(const ExpressionType &matrix, const IOFormat &format)
Definition: IO.h:94
std::string rowSeparator
Definition: IO.h:68
XmlRpcServer s
Definition: LDLT.h:16
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
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:88
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:41
std::string coeffSeparator
Definition: IO.h:69
int flags
Definition: IO.h:71
std::string rowSpacer
Definition: IO.h:68
std::ostream & print_matrix(std::ostream &s, const Derived &_m, const IOFormat &fmt)
Definition: IO.h:153
std::string matPrefix
Definition: IO.h:67
std::string rowPrefix
Definition: IO.h:68
const CwiseUnaryOp< internal::scalar_log_op< Scalar >, const Derived > log() const
std::string matSuffix
Definition: IO.h:67
const ExpressionType::Nested m_matrix
Definition: IO.h:104
#define EIGEN_DEFAULT_IO_FORMAT
int precision
Definition: IO.h:70
IOFormat m_format
Definition: IO.h:105
NumTraits< Scalar >::Real RealScalar
Definition: IO.h:127
const WithFormat< Derived > format(const IOFormat &fmt) const
Definition: IO.h:117
Pseudo expression providing matrix output with given format.
Definition: IO.h:90
Stores a set of parameters controlling the way matrices are printed.
Definition: IO.h:50
std::string rowSuffix
Definition: IO.h:68


tuw_aruco
Author(s): Lukas Pfeifhofer
autogenerated on Mon Jun 10 2019 15:40:51