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 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 enum { DontAlignCols = 1 };
19 enum { StreamPrecision = -1,
20  FullPrecision = -2 };
21 
22 namespace internal {
23 template<typename Derived>
24 std::ostream & print_matrix(std::ostream & s, const Derived& _m, const IOFormat& fmt);
25 }
26 
53 struct IOFormat
54 {
56  IOFormat(int _precision = StreamPrecision, int _flags = 0,
57  const std::string& _coeffSeparator = " ",
58  const std::string& _rowSeparator = "\n", const std::string& _rowPrefix="", const std::string& _rowSuffix="",
59  const std::string& _matPrefix="", const std::string& _matSuffix="", const char _fill=' ')
60  : matPrefix(_matPrefix), matSuffix(_matSuffix), rowPrefix(_rowPrefix), rowSuffix(_rowSuffix), rowSeparator(_rowSeparator),
61  rowSpacer(""), coeffSeparator(_coeffSeparator), fill(_fill), precision(_precision), flags(_flags)
62  {
63  // TODO check if rowPrefix, rowSuffix or rowSeparator contains a newline
64  // don't add rowSpacer if columns are not to be aligned
65  if((flags & DontAlignCols))
66  return;
67  int i = int(matSuffix.length())-1;
68  while (i>=0 && matSuffix[i]!='\n')
69  {
70  rowSpacer += ' ';
71  i--;
72  }
73  }
74  std::string matPrefix, matSuffix;
76  std::string coeffSeparator;
77  char fill;
78  int precision;
79  int flags;
80 };
81 
97 template<typename ExpressionType>
99 {
100  public:
101 
102  WithFormat(const ExpressionType& matrix, const IOFormat& format)
103  : m_matrix(matrix), m_format(format)
104  {}
105 
106  friend std::ostream & operator << (std::ostream & s, const WithFormat& wf)
107  {
108  return internal::print_matrix(s, wf.m_matrix.eval(), wf.m_format);
109  }
110 
111  protected:
112  typename ExpressionType::Nested m_matrix;
114 };
115 
116 namespace internal {
117 
118 // NOTE: This helper is kept for backward compatibility with previous code specializing
119 // this internal::significant_decimals_impl structure. In the future we should directly
120 // call digits10() which has been introduced in July 2016 in 3.3.
121 template<typename Scalar>
122 struct significant_decimals_impl
123 {
124  static inline int run()
125  {
127  }
128 };
129 
132 template<typename Derived>
133 std::ostream & print_matrix(std::ostream & s, const Derived& _m, const IOFormat& fmt)
134 {
135  using internal::is_same;
136 
137  if(_m.size() == 0)
138  {
139  s << fmt.matPrefix << fmt.matSuffix;
140  return s;
141  }
142 
143  typename Derived::Nested m = _m;
144  typedef typename Derived::Scalar Scalar;
145  typedef std::conditional_t<
146  is_same<Scalar, char>::value ||
147  is_same<Scalar, unsigned char>::value ||
148  is_same<Scalar, numext::int8_t>::value ||
149  is_same<Scalar, numext::uint8_t>::value,
150  int,
151  std::conditional_t<
152  is_same<Scalar, std::complex<char> >::value ||
153  is_same<Scalar, std::complex<unsigned char> >::value ||
154  is_same<Scalar, std::complex<numext::int8_t> >::value ||
155  is_same<Scalar, std::complex<numext::uint8_t> >::value,
156  std::complex<int>,
157  const Scalar&
158  >
159  > 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 template <typename Derived>
257 std::ostream& operator<<(std::ostream& s, const DiagonalBase<Derived>& m) {
259 }
260 
261 } // end namespace Eigen
262 
263 #endif // EIGEN_IO_H
Matrix3f m
#define EIGEN_DEFAULT_IO_FORMAT
Definition: Macros.h:1065
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:42
EvalReturnType eval() const
Definition: DenseBase.h:405
Base class for diagonal matrices and expressions.
Pseudo expression providing matrix output with given format.
Definition: IO.h:99
WithFormat(const ExpressionType &matrix, const IOFormat &format)
Definition: IO.h:102
IOFormat m_format
Definition: IO.h:113
ExpressionType::Nested m_matrix
Definition: IO.h:112
friend std::ostream & operator<<(std::ostream &s, const WithFormat &wf)
Definition: IO.h:106
std::ostream & print_matrix(std::ostream &s, const Derived &_m, const IOFormat &fmt)
Definition: IO.h:133
: InteropHeaders
Definition: Core:139
std::ostream & operator<<(std::ostream &s, const DiagonalBase< Derived > &m)
Definition: IO.h:257
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
@ StreamPrecision
Definition: IO.h:19
@ FullPrecision
Definition: IO.h:20
@ DontAlignCols
Definition: IO.h:18
Stores a set of parameters controlling the way matrices are printed.
Definition: IO.h:54
std::string rowPrefix
Definition: IO.h:75
std::string rowSuffix
Definition: IO.h:75
std::string coeffSeparator
Definition: IO.h:76
char fill
Definition: IO.h:77
std::string rowSpacer
Definition: IO.h:75
std::string matPrefix
Definition: IO.h:74
std::string rowSeparator
Definition: IO.h:75
std::string matSuffix
Definition: IO.h:74
int precision
Definition: IO.h:78
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:56
int flags
Definition: IO.h:79
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:231
std::ptrdiff_t j