$darkmode
Eigen  5.0.1-dev
IO.h
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  }
77  std::string matPrefix, matSuffix;
78  std::string rowPrefix, rowSuffix, rowSeparator, rowSpacer;
79  std::string coeffSeparator;
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;
111  IOFormat m_format;
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>
120 struct significant_decimals_impl {
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;
137  typedef std::conditional_t<is_same<Scalar, char>::value || is_same<Scalar, unsigned char>::value ||
138  is_same<Scalar, numext::int8_t>::value || is_same<Scalar, numext::uint8_t>::value,
139  int,
140  std::conditional_t<is_same<Scalar, std::complex<char> >::value ||
141  is_same<Scalar, std::complex<unsigned char> >::value ||
142  is_same<Scalar, std::complex<numext::int8_t> >::value ||
143  is_same<Scalar, std::complex<numext::uint8_t> >::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) {
153  if (NumTraits<Scalar>::IsInteger) {
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) {
223  return internal::print_matrix(s, m.eval(), EIGEN_DEFAULT_IO_FORMAT);
224 }
225 
226 template <typename Derived>
227 std::ostream& operator<<(std::ostream& s, const DiagonalBase<Derived>& m) {
228  return internal::print_matrix(s, m.derived(), EIGEN_DEFAULT_IO_FORMAT);
229 }
230 
231 } // end namespace Eigen
232 
233 #endif // EIGEN_IO_H
IOFormat(int _precision=StreamPrecision, int _flags=0, const std::string &_coeffSeparator=" ", const std::string &_rowSeparator="\, const std::string &_rowPrefix="", const std::string &_rowSuffix="", const std::string &_matPrefix="", const std::string &_matSuffix="", const char _fill=' ')
Definition: IO.h:54
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:232
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
Pseudo expression providing matrix output with given format.
Definition: IO.h:101
Stores a set of parameters controlling the way matrices are printed.
Definition: IO.h:52