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  // 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 
93 template<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  typename ExpressionType::Nested m_matrix;
110 };
111 
112 namespace internal {
113 
114 // NOTE: This helper is kept for backward compatibility with previous code specializing
115 // this internal::significant_decimals_impl structure. In the future we should directly
116 // call digits10() which has been introduced in July 2016 in 3.3.
117 template<typename Scalar>
119 {
120  static inline int run()
121  {
123  }
124 };
125 
128 template<typename Derived>
129 std::ostream & print_matrix(std::ostream & s, const Derived& _m, const IOFormat& fmt)
130 {
131  if(_m.size() == 0)
132  {
133  s << fmt.matPrefix << fmt.matSuffix;
134  return s;
135  }
136 
137  typename Derived::Nested m = _m;
138  typedef typename Derived::Scalar Scalar;
139 
140  Index width = 0;
141 
142  std::streamsize explicit_precision;
143  if(fmt.precision == StreamPrecision)
144  {
145  explicit_precision = 0;
146  }
147  else if(fmt.precision == FullPrecision)
148  {
150  {
151  explicit_precision = 0;
152  }
153  else
154  {
155  explicit_precision = significant_decimals_impl<Scalar>::run();
156  }
157  }
158  else
159  {
160  explicit_precision = fmt.precision;
161  }
162 
163  std::streamsize old_precision = 0;
164  if(explicit_precision) old_precision = s.precision(explicit_precision);
165 
166  bool align_cols = !(fmt.flags & DontAlignCols);
167  if(align_cols)
168  {
169  // compute the largest width
170  for(Index j = 0; j < m.cols(); ++j)
171  for(Index i = 0; i < m.rows(); ++i)
172  {
173  std::stringstream sstr;
174  sstr.copyfmt(s);
175  sstr << m.coeff(i,j);
176  width = std::max<Index>(width, Index(sstr.str().length()));
177  }
178  }
179  s << fmt.matPrefix;
180  for(Index i = 0; i < m.rows(); ++i)
181  {
182  if (i)
183  s << fmt.rowSpacer;
184  s << fmt.rowPrefix;
185  if(width) s.width(width);
186  s << m.coeff(i, 0);
187  for(Index j = 1; j < m.cols(); ++j)
188  {
189  s << fmt.coeffSeparator;
190  if (width) s.width(width);
191  s << m.coeff(i, j);
192  }
193  s << fmt.rowSuffix;
194  if( i < m.rows() - 1)
195  s << fmt.rowSeparator;
196  }
197  s << fmt.matSuffix;
198  if(explicit_precision) s.precision(old_precision);
199  return s;
200 }
201 
202 } // end namespace internal
203 
215 template<typename Derived>
216 std::ostream & operator <<
217 (std::ostream & s,
218  const DenseBase<Derived> & m)
219 {
221 }
222 
223 } // end namespace Eigen
224 
225 #endif // EIGEN_IO_H
Matrix3f m
SCALAR Scalar
Definition: bench_gemm.cpp:33
return int(ret)+1
WithFormat(const ExpressionType &matrix, const IOFormat &format)
Definition: IO.h:98
std::string rowSeparator
Definition: IO.h:72
std::ostream & operator<<(std::ostream &s, const Packet16uc &v)
#define EIGEN_DEFAULT_IO_FORMAT
Definition: Macros.h:810
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
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:150
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:41
std::string coeffSeparator
Definition: IO.h:73
int flags
Definition: IO.h:75
ExpressionType::Nested m_matrix
Definition: IO.h:108
std::string rowSpacer
Definition: IO.h:72
std::ostream & print_matrix(std::ostream &s, const Derived &_m, const IOFormat &fmt)
Definition: IO.h:129
std::string matPrefix
Definition: IO.h:71
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
std::string rowPrefix
Definition: IO.h:72
RealScalar s
std::string matSuffix
Definition: IO.h:71
int precision
Definition: IO.h:74
cout precision(2)
IOFormat m_format
Definition: IO.h:109
Map< Matrix< T, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Pseudo expression providing matrix output with given format.
Definition: IO.h:94
Stores a set of parameters controlling the way matrices are printed.
Definition: IO.h:50
std::ptrdiff_t j
std::string rowSuffix
Definition: IO.h:72


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:42:16