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 // IWYU pragma: private
15 #include "./InternalHeaderCheck.h"
16 
17 namespace Eigen {
18 
19 enum { DontAlignCols = 1 };
20 enum { StreamPrecision = -1, 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 
52 struct IOFormat {
54  IOFormat(int _precision = StreamPrecision, int _flags = 0, const std::string& _coeffSeparator = " ",
55  const std::string& _rowSeparator = "\n", const std::string& _rowPrefix = "",
56  const std::string& _rowSuffix = "", const std::string& _matPrefix = "", const std::string& _matSuffix = "",
57  const char _fill = ' ')
58  : matPrefix(_matPrefix),
59  matSuffix(_matSuffix),
60  rowPrefix(_rowPrefix),
61  rowSuffix(_rowSuffix),
62  rowSeparator(_rowSeparator),
63  rowSpacer(""),
64  coeffSeparator(_coeffSeparator),
65  fill(_fill),
66  precision(_precision),
67  flags(_flags) {
68  // TODO check if rowPrefix, rowSuffix or rowSeparator contains a newline
69  // don't add rowSpacer if columns are not to be aligned
70  if ((flags & DontAlignCols)) return;
71  int i = int(matPrefix.length()) - 1;
72  while (i >= 0 && matPrefix[i] != '\n') {
73  rowSpacer += ' ';
74  i--;
75  }
76  }
80  char fill;
81  int precision;
82  int flags;
83 };
84 
100 template <typename ExpressionType>
101 class WithFormat {
102  public:
103  WithFormat(const ExpressionType& matrix, const IOFormat& format) : m_matrix(matrix), m_format(format) {}
104 
105  friend std::ostream& operator<<(std::ostream& s, const WithFormat& wf) {
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 max_digits10().
119 template <typename Scalar>
121  static inline int run() { return NumTraits<Scalar>::max_digits10(); }
122 };
123 
126 template <typename Derived>
127 std::ostream& print_matrix(std::ostream& s, const Derived& _m, const IOFormat& fmt) {
128  using internal::is_same;
129 
130  if (_m.size() == 0) {
131  s << fmt.matPrefix << fmt.matSuffix;
132  return s;
133  }
134 
135  typename Derived::Nested m = _m;
136  typedef typename Derived::Scalar Scalar;
139  int,
140  std::conditional_t<is_same<Scalar, std::complex<char> >::value ||
144  std::complex<int>, const Scalar&> >
145  PrintType;
146 
147  Index width = 0;
148 
149  std::streamsize explicit_precision;
150  if (fmt.precision == StreamPrecision) {
151  explicit_precision = 0;
152  } else if (fmt.precision == FullPrecision) {
154  explicit_precision = 0;
155  } else {
156  explicit_precision = significant_decimals_impl<Scalar>::run();
157  }
158  } else {
159  explicit_precision = fmt.precision;
160  }
161 
162  std::streamsize old_precision = 0;
163  if (explicit_precision) old_precision = s.precision(explicit_precision);
164 
165  bool align_cols = !(fmt.flags & DontAlignCols);
166  if (align_cols) {
167  // compute the largest width
168  for (Index j = 0; j < m.cols(); ++j)
169  for (Index i = 0; i < m.rows(); ++i) {
170  std::stringstream sstr;
171  sstr.copyfmt(s);
172  sstr << static_cast<PrintType>(m.coeff(i, j));
173  width = std::max<Index>(width, Index(sstr.str().length()));
174  }
175  }
176  std::streamsize old_width = s.width();
177  char old_fill_character = s.fill();
178  s << fmt.matPrefix;
179  for (Index i = 0; i < m.rows(); ++i) {
180  if (i) s << fmt.rowSpacer;
181  s << fmt.rowPrefix;
182  if (width) {
183  s.fill(fmt.fill);
184  s.width(width);
185  }
186  s << static_cast<PrintType>(m.coeff(i, 0));
187  for (Index j = 1; j < m.cols(); ++j) {
188  s << fmt.coeffSeparator;
189  if (width) {
190  s.fill(fmt.fill);
191  s.width(width);
192  }
193  s << static_cast<PrintType>(m.coeff(i, j));
194  }
195  s << fmt.rowSuffix;
196  if (i < m.rows() - 1) s << fmt.rowSeparator;
197  }
198  s << fmt.matSuffix;
199  if (explicit_precision) s.precision(old_precision);
200  if (width) {
201  s.fill(old_fill_character);
202  s.width(old_width);
203  }
204  return s;
205 }
206 
207 } // end namespace internal
208 
221 template <typename Derived>
222 std::ostream& operator<<(std::ostream& s, const DenseBase<Derived>& m) {
224 }
225 
226 template <typename Derived>
227 std::ostream& operator<<(std::ostream& s, const DiagonalBase<Derived>& m) {
229 }
230 
231 } // end namespace Eigen
232 
233 #endif // EIGEN_IO_H
int i
Definition: BiCGSTAB_step_by_step.cpp:9
#define EIGEN_DEFAULT_IO_FORMAT
Definition: Macros.h:1076
SCALAR Scalar
Definition: bench_gemm.cpp:45
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:44
std::ostream & operator<<(std::ostream &s, const DenseBase< Derived > &m)
Definition: IO.h:222
Base class for diagonal matrices and expressions.
Definition: DiagonalMatrix.h:33
Pseudo expression providing matrix output with given format.
Definition: IO.h:101
WithFormat(const ExpressionType &matrix, const IOFormat &format)
Definition: IO.h:103
IOFormat m_format
Definition: IO.h:111
ExpressionType::Nested m_matrix
Definition: IO.h:110
friend std::ostream & operator<<(std::ostream &s, const WithFormat &wf)
Definition: IO.h:105
Eigen::Map< Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor >, 0, Eigen::OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: common.h:85
RealScalar s
Definition: level1_cplx_impl.h:130
return int(ret)+1
int * m
Definition: level2_cplx_impl.h:294
std::ostream & print_matrix(std::ostream &s, const Derived &_m, const IOFormat &fmt)
Definition: IO.h:127
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
squared absolute value
Definition: GlobalFunctions.h:87
std::ostream & operator<<(std::ostream &s, const DiagonalBase< Derived > &m)
Definition: IO.h:227
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
@ StreamPrecision
Definition: IO.h:20
@ FullPrecision
Definition: IO.h:20
@ DontAlignCols
Definition: IO.h:19
Definition: Eigen_Colamd.h:49
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286
std::string format(const std::string &str, const std::vector< std::string > &find, const std::vector< std::string > &replace)
Definition: openglsupport.cpp:217
Stores a set of parameters controlling the way matrices are printed.
Definition: IO.h:52
std::string rowPrefix
Definition: IO.h:78
std::string rowSuffix
Definition: IO.h:78
std::string coeffSeparator
Definition: IO.h:79
char fill
Definition: IO.h:80
std::string rowSpacer
Definition: IO.h:78
std::string matPrefix
Definition: IO.h:77
std::string rowSeparator
Definition: IO.h:78
std::string matSuffix
Definition: IO.h:77
int precision
Definition: IO.h:81
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
int flags
Definition: IO.h:82
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
Definition: Meta.h:205
static int run()
Definition: IO.h:121
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2