Eigen  5.0.1
 
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 // TODO: check if rowPrefix, rowSuffix or rowSeparator contains a newline
70 // don't add rowSpacer if columns are not to be aligned
71 if ((flags & DontAlignCols)) return;
72 int i = int(matPrefix.length()) - 1;
73 while (i >= 0 && matPrefix[i] != '\n') {
74 rowSpacer += ' ';
75 i--;
76 }
77 }
78 std::string matPrefix, matSuffix;
79 std::string rowPrefix, rowSuffix, rowSeparator, rowSpacer;
80 std::string coeffSeparator;
81 char fill;
82 int precision;
83 int flags;
84};
85
101template <typename ExpressionType>
102class WithFormat {
103 public:
104 WithFormat(const ExpressionType& matrix, const IOFormat& format) : m_matrix(matrix), m_format(format) {}
105
106 friend std::ostream& operator<<(std::ostream& s, const WithFormat& wf) {
107 return internal::print_matrix(s, wf.m_matrix.eval(), wf.m_format);
108 }
109
110 protected:
111 typename ExpressionType::Nested m_matrix;
112 IOFormat m_format;
113};
114
115namespace internal {
116
117// NOTE: This helper is kept for backward compatibility with previous code specializing
118// this internal::significant_decimals_impl structure. In the future we should directly
119// call max_digits10().
120template <typename Scalar>
121struct significant_decimals_impl {
122 static inline int run() { return NumTraits<Scalar>::max_digits10(); }
123};
124
127template <typename Derived>
128std::ostream& print_matrix(std::ostream& s, const Derived& _m, const IOFormat& fmt) {
129 if (_m.size() == 0) {
130 s << fmt.matPrefix << fmt.matSuffix;
131 return s;
132 }
133
134 typename Derived::Nested m = _m;
135 using Scalar = typename Derived::Scalar;
136 using PrintType =
137 std::conditional_t<std::is_same<Scalar, char>::value || std::is_same<Scalar, unsigned char>::value ||
138 std::is_same<Scalar, numext::int8_t>::value ||
139 std::is_same<Scalar, numext::uint8_t>::value,
140 int,
141 std::conditional_t<std::is_same<Scalar, std::complex<char> >::value ||
142 std::is_same<Scalar, std::complex<unsigned char> >::value ||
143 std::is_same<Scalar, std::complex<numext::int8_t> >::value ||
144 std::is_same<Scalar, std::complex<numext::uint8_t> >::value,
145 std::complex<int>, const Scalar&> >;
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 EIGEN_IF_CONSTEXPR (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
221template <typename Derived>
222std::ostream& operator<<(std::ostream& s, const DenseBase<Derived>& m) {
223 return internal::print_matrix(s, m.eval(), EIGEN_DEFAULT_IO_FORMAT);
224}
225
226template <typename Derived>
227std::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
std::ostream & operator<<(std::ostream &s, const DenseBase< Derived > &m)
Definition IO.h:222
EvalReturnType eval() const
Definition DenseBase.h:385
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