Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
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// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_IO_H
13#define EIGEN_IO_H
14
15// IWYU pragma: private
16#include "./InternalHeaderCheck.h"
17
18namespace Eigen {
19
20enum { DontAlignCols = 1 };
21enum { StreamPrecision = -1, FullPrecision = -2 };
22
23namespace internal {
24template <typename Derived>
25std::ostream& print_matrix(std::ostream& s, const Derived& _m, const IOFormat& fmt);
26}
27
53struct IOFormat {
55 IOFormat(int _precision = StreamPrecision, int _flags = 0, const std::string& _coeffSeparator = " ",
56 const std::string& _rowSeparator = "\n", const std::string& _rowPrefix = "",
57 const std::string& _rowSuffix = "", const std::string& _matPrefix = "", const std::string& _matSuffix = "",
58 const char _fill = ' ')
59 : matPrefix(_matPrefix),
60 matSuffix(_matSuffix),
61 rowPrefix(_rowPrefix),
62 rowSuffix(_rowSuffix),
63 rowSeparator(_rowSeparator),
64 rowSpacer(""),
65 coeffSeparator(_coeffSeparator),
66 fill(_fill),
67 precision(_precision),
68 flags(_flags) {
69 if ((flags & DontAlignCols)) return;
70
71 // Find last newline between consecutive rows.
72 const std::string between{rowSuffix + rowSeparator};
73 const std::size_t nlLastPos{between.rfind('\n')};
74 if (nlLastPos == std::string::npos) return;
75
76 // Align subsequent rows, accounting for characters after last newline.
77 const std::size_t indent{matPrefix.length() - (matPrefix.rfind('\n') + 1)};
78 const std::size_t carried{between.length() - (nlLastPos + 1)};
79
80 if (indent > carried) rowSpacer.assign(indent - carried, ' ');
81 }
82 std::string matPrefix, matSuffix;
83 std::string rowPrefix, rowSuffix, rowSeparator, rowSpacer;
84 std::string coeffSeparator;
85 char fill;
86 int precision;
87 int flags;
88};
89
105template <typename ExpressionType>
106class WithFormat {
107 public:
108 WithFormat(const ExpressionType& matrix, const IOFormat& format) : m_matrix(matrix), m_format(format) {}
109
110 friend std::ostream& operator<<(std::ostream& s, const WithFormat& wf) {
111 return internal::print_matrix(s, wf.m_matrix.eval(), wf.m_format);
112 }
113
114 protected:
115 typename ExpressionType::Nested m_matrix;
116 IOFormat m_format;
117};
118
119namespace internal {
120
121// NOTE: This helper is kept for backward compatibility with previous code specializing
122// this internal::significant_decimals_impl structure. In the future we should directly
123// call max_digits10().
124template <typename Scalar>
125struct significant_decimals_impl {
126 static inline int run() { return NumTraits<Scalar>::max_digits10(); }
127};
128
131template <typename Derived>
132std::ostream& print_matrix(std::ostream& s, const Derived& _m, const IOFormat& fmt) {
133 if (_m.size() == 0) {
134 s << fmt.matPrefix << fmt.matSuffix;
135 return s;
136 }
137
138 typename Derived::Nested m = _m;
139 using Scalar = typename Derived::Scalar;
140 using PrintType =
141 std::conditional_t<std::is_same<Scalar, char>::value || std::is_same<Scalar, unsigned char>::value ||
142 std::is_same<Scalar, numext::int8_t>::value ||
143 std::is_same<Scalar, numext::uint8_t>::value,
144 int,
145 std::conditional_t<std::is_same<Scalar, std::complex<char> >::value ||
146 std::is_same<Scalar, std::complex<unsigned char> >::value ||
147 std::is_same<Scalar, std::complex<numext::int8_t> >::value ||
148 std::is_same<Scalar, std::complex<numext::uint8_t> >::value,
149 std::complex<int>, const Scalar&> >;
150
151 Index width = 0;
152
153 std::streamsize explicit_precision;
154 if (fmt.precision == StreamPrecision) {
155 explicit_precision = 0;
156 } else if (fmt.precision == FullPrecision) {
157 EIGEN_IF_CONSTEXPR (NumTraits<Scalar>::IsInteger) {
158 explicit_precision = 0;
159 } else {
160 explicit_precision = significant_decimals_impl<Scalar>::run();
161 }
162 } else {
163 explicit_precision = fmt.precision;
164 }
165
166 std::streamsize old_precision = 0;
167 if (explicit_precision) old_precision = s.precision(explicit_precision);
168
169 bool align_cols = !(fmt.flags & DontAlignCols);
170 if (align_cols) {
171 // compute the largest width
172 for (Index j = 0; j < m.cols(); ++j)
173 for (Index i = 0; i < m.rows(); ++i) {
174 std::stringstream sstr;
175 sstr.copyfmt(s);
176 sstr << static_cast<PrintType>(m.coeff(i, j));
177 width = std::max<Index>(width, Index(sstr.str().length()));
178 }
179 }
180 std::streamsize old_width = s.width();
181 char old_fill_character = s.fill();
182 s << fmt.matPrefix;
183 for (Index i = 0; i < m.rows(); ++i) {
184 if (i) s << fmt.rowSpacer;
185 s << fmt.rowPrefix;
186 if (width) {
187 s.fill(fmt.fill);
188 s.width(width);
189 }
190 s << static_cast<PrintType>(m.coeff(i, 0));
191 for (Index j = 1; j < m.cols(); ++j) {
192 s << fmt.coeffSeparator;
193 if (width) {
194 s.fill(fmt.fill);
195 s.width(width);
196 }
197 s << static_cast<PrintType>(m.coeff(i, j));
198 }
199 s << fmt.rowSuffix;
200 if (i < m.rows() - 1) s << fmt.rowSeparator;
201 }
202 s << fmt.matSuffix;
203 if (explicit_precision) s.precision(old_precision);
204 if (width) {
205 s.fill(old_fill_character);
206 s.width(old_width);
207 }
208 return s;
209}
210
211} // end namespace internal
212
225template <typename Derived>
226std::ostream& operator<<(std::ostream& s, const DenseBase<Derived>& m) {
227 return internal::print_matrix(s, m.eval(), EIGEN_DEFAULT_IO_FORMAT);
228}
229
230template <typename Derived>
231std::ostream& operator<<(std::ostream& s, const DiagonalBase<Derived>& m) {
232 return internal::print_matrix(s, m.derived(), EIGEN_DEFAULT_IO_FORMAT);
233}
234
235} // end namespace Eigen
236
237#endif // EIGEN_IO_H
std::ostream & operator<<(std::ostream &s, const DenseBase< Derived > &m)
Definition IO.h:226
EvalReturnType eval() const
Definition DenseBase.h:389
Base class for diagonal matrices and expressions.
Definition DiagonalMatrix.h:34
const Derived & derived() const
Definition DiagonalMatrix.h:60
Stores a set of parameters controlling the way matrices are printed.
Definition IO.h:53
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:55