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 
51 struct IOFormat
52 {
54  IOFormat(int _precision = StreamPrecision, int _flags = 0,
55  const std::string& _coeffSeparator = " ",
56  const std::string& _rowSeparator = "\n", const std::string& _rowPrefix="", const std::string& _rowSuffix="",
57  const std::string& _matPrefix="", const std::string& _matSuffix="", const char _fill=' ')
58  : matPrefix(_matPrefix), matSuffix(_matSuffix), rowPrefix(_rowPrefix), rowSuffix(_rowSuffix), rowSeparator(_rowSeparator),
59  rowSpacer(""), coeffSeparator(_coeffSeparator), fill(_fill), precision(_precision), flags(_flags)
60  {
61  // TODO check if rowPrefix, rowSuffix or rowSeparator contains a newline
62  // don't add rowSpacer if columns are not to be aligned
63  if((flags & DontAlignCols))
64  return;
65  int i = int(matSuffix.length())-1;
66  while (i>=0 && matSuffix[i]!='\n')
67  {
68  rowSpacer += ' ';
69  i--;
70  }
71  }
72  std::string matPrefix, matSuffix;
74  std::string coeffSeparator;
75  char fill;
76  int precision;
77  int flags;
78 };
79 
95 template<typename ExpressionType>
97 {
98  public:
99 
100  WithFormat(const ExpressionType& matrix, const IOFormat& format)
102  {}
103 
104  friend std::ostream & operator << (std::ostream & s, const WithFormat& wf)
105  {
106  return internal::print_matrix(s, wf.m_matrix.eval(), wf.m_format);
107  }
108 
109  protected:
110  typename ExpressionType::Nested m_matrix;
112 };
113 
114 namespace internal {
115 
116 // NOTE: This helper is kept for backward compatibility with previous code specializing
117 // this internal::significant_decimals_impl structure. In the future we should directly
118 // call digits10() which has been introduced in July 2016 in 3.3.
119 template<typename Scalar>
121 {
122  static inline int run()
123  {
125  }
126 };
127 
130 template<typename Derived>
131 std::ostream & print_matrix(std::ostream & s, const Derived& _m, const IOFormat& fmt)
132 {
133  using internal::is_same;
134  using internal::conditional;
135 
136  if(_m.size() == 0)
137  {
138  s << fmt.matPrefix << fmt.matSuffix;
139  return s;
140  }
141 
142  typename Derived::Nested m = _m;
143  typedef typename Derived::Scalar Scalar;
144  typedef typename
145  conditional<
150  int,
151  typename conditional<
156  std::complex<int>,
157  const Scalar&
158  >::type
159  >::type PrintType;
160 
161  Index width = 0;
162 
163  std::streamsize explicit_precision;
164  if(fmt.precision == StreamPrecision)
165  {
166  explicit_precision = 0;
167  }
168  else if(fmt.precision == FullPrecision)
169  {
171  {
172  explicit_precision = 0;
173  }
174  else
175  {
176  explicit_precision = significant_decimals_impl<Scalar>::run();
177  }
178  }
179  else
180  {
181  explicit_precision = fmt.precision;
182  }
183 
184  std::streamsize old_precision = 0;
185  if(explicit_precision) old_precision = s.precision(explicit_precision);
186 
187  bool align_cols = !(fmt.flags & DontAlignCols);
188  if(align_cols)
189  {
190  // compute the largest width
191  for(Index j = 0; j < m.cols(); ++j)
192  for(Index i = 0; i < m.rows(); ++i)
193  {
194  std::stringstream sstr;
195  sstr.copyfmt(s);
196  sstr << static_cast<PrintType>(m.coeff(i,j));
197  width = std::max<Index>(width, Index(sstr.str().length()));
198  }
199  }
200  std::streamsize old_width = s.width();
201  char old_fill_character = s.fill();
202  s << fmt.matPrefix;
203  for(Index i = 0; i < m.rows(); ++i)
204  {
205  if (i)
206  s << fmt.rowSpacer;
207  s << fmt.rowPrefix;
208  if(width) {
209  s.fill(fmt.fill);
210  s.width(width);
211  }
212  s << static_cast<PrintType>(m.coeff(i, 0));
213  for(Index j = 1; j < m.cols(); ++j)
214  {
215  s << fmt.coeffSeparator;
216  if(width) {
217  s.fill(fmt.fill);
218  s.width(width);
219  }
220  s << static_cast<PrintType>(m.coeff(i, j));
221  }
222  s << fmt.rowSuffix;
223  if( i < m.rows() - 1)
224  s << fmt.rowSeparator;
225  }
226  s << fmt.matSuffix;
227  if(explicit_precision) s.precision(old_precision);
228  if(width) {
229  s.fill(old_fill_character);
230  s.width(old_width);
231  }
232  return s;
233 }
234 
235 } // end namespace internal
236 
248 template<typename Derived>
249 std::ostream & operator <<
250 (std::ostream & s,
251  const DenseBase<Derived> & m)
252 {
254 }
255 
256 } // end namespace Eigen
257 
258 #endif // EIGEN_IO_H
Eigen::WithFormat::m_matrix
ExpressionType::Nested m_matrix
Definition: IO.h:110
gtsam.examples.DogLegOptimizerExample.int
int
Definition: DogLegOptimizerExample.py:111
Eigen::IOFormat::rowSpacer
std::string rowSpacer
Definition: IO.h:73
Eigen::IOFormat::rowSeparator
std::string rowSeparator
Definition: IO.h:73
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::WithFormat::WithFormat
WithFormat(const ExpressionType &matrix, const IOFormat &format)
Definition: IO.h:100
Eigen::IOFormat
Stores a set of parameters controlling the way matrices are printed.
Definition: IO.h:51
format
std::string format(const std::string &str, const std::vector< std::string > &find, const std::vector< std::string > &replace)
Definition: openglsupport.cpp:226
Eigen::IOFormat::fill
char fill
Definition: IO.h:75
s
RealScalar s
Definition: level1_cplx_impl.h:126
type
Definition: pytypes.h:1525
Eigen::IOFormat::flags
int flags
Definition: IO.h:77
Eigen::IOFormat::IOFormat
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="", const char _fill=' ')
Definition: IO.h:54
Eigen::IOFormat::coeffSeparator
std::string coeffSeparator
Definition: IO.h:74
Eigen::internal::significant_decimals_impl
Definition: IO.h:120
Eigen::IOFormat::precision
int precision
Definition: IO.h:76
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
Eigen::DontAlignCols
@ DontAlignCols
Definition: IO.h:16
Eigen::internal::significant_decimals_impl::run
static int run()
Definition: IO.h:122
Eigen::WithFormat
Pseudo expression providing matrix output with given format.
Definition: IO.h:96
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
Eigen::IOFormat::matSuffix
std::string matSuffix
Definition: IO.h:72
matrix
Map< Matrix< T, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: gtsam/3rdparty/Eigen/blas/common.h:110
Eigen::IOFormat::rowPrefix
std::string rowPrefix
Definition: IO.h:73
Eigen::StreamPrecision
@ StreamPrecision
Definition: IO.h:17
Eigen::WithFormat::operator<<
friend std::ostream & operator<<(std::ostream &s, const WithFormat &wf)
Definition: IO.h:104
Eigen::DenseBase
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:41
Eigen::internal::conditional
Definition: Meta.h:109
Eigen::internal::is_same
Definition: Meta.h:148
Eigen::FullPrecision
@ FullPrecision
Definition: IO.h:18
internal
Definition: BandTriangularSolver.h:13
Eigen::WithFormat::m_format
IOFormat m_format
Definition: IO.h:111
EIGEN_DEFAULT_IO_FORMAT
#define EIGEN_DEFAULT_IO_FORMAT
Definition: Macros.h:1170
Eigen::IOFormat::rowSuffix
std::string rowSuffix
Definition: IO.h:73
Eigen::internal::print_matrix
std::ostream & print_matrix(std::ostream &s, const Derived &_m, const IOFormat &fmt)
Definition: IO.h:131
Eigen::NumTraits
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:232
test_callbacks.value
value
Definition: test_callbacks.py:160
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Eigen::IOFormat::matPrefix
std::string matPrefix
Definition: IO.h:72
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:02:31