Eigen::internal::TensorPrinter< Tensor, rank, Format, EnableIf > Struct Template Reference

#include <TensorIO.h>

Public Types

using Scalar = std::remove_const_t< typename Tensor::Scalar >
 

Static Public Member Functions

static void run (std::ostream &s, const Tensor &tensor, const Format &fmt)
 

Member Typedef Documentation

◆ Scalar

template<typename Tensor , std::size_t rank, typename Format , typename EnableIf >
using Eigen::internal::TensorPrinter< Tensor, rank, Format, EnableIf >::Scalar = std::remove_const_t<typename Tensor::Scalar>

Member Function Documentation

◆ run()

template<typename Tensor , std::size_t rank, typename Format , typename EnableIf >
static void Eigen::internal::TensorPrinter< Tensor, rank, Format, EnableIf >::run ( std::ostream &  s,
const Tensor tensor,
const Format &  fmt 
)
inlinestatic
219  {
220  typedef typename Tensor::Index IndexType;
221 
225  int,
226  std::conditional_t<is_same<Scalar, std::complex<char>>::value ||
227  is_same<Scalar, std::complex<unsigned char>>::value ||
228  is_same<Scalar, std::complex<numext::int8_t>>::value ||
229  is_same<Scalar, std::complex<numext::uint8_t>>::value,
230  std::complex<int>, const Scalar&>>
231  PrintType;
232 
233  const IndexType total_size = array_prod(tensor.dimensions());
234 
235  std::streamsize explicit_precision;
236  if (fmt.precision == StreamPrecision) {
237  explicit_precision = 0;
238  } else if (fmt.precision == FullPrecision) {
240  explicit_precision = 0;
241  } else {
242  explicit_precision = significant_decimals_impl<Scalar>::run();
243  }
244  } else {
245  explicit_precision = fmt.precision;
246  }
247 
248  std::streamsize old_precision = 0;
249  if (explicit_precision) old_precision = s.precision(explicit_precision);
250 
251  IndexType width = 0;
252  bool align_cols = !(fmt.flags & DontAlignCols);
253  if (align_cols) {
254  // compute the largest width
255  for (IndexType i = 0; i < total_size; i++) {
256  std::stringstream sstr;
257  sstr.copyfmt(s);
258  ScalarPrinter<Scalar, Format>::run(sstr, static_cast<PrintType>(tensor.data()[i]), fmt);
259  width = std::max<IndexType>(width, IndexType(sstr.str().length()));
260  }
261  }
262  s << fmt.tenPrefix;
263  for (IndexType i = 0; i < total_size; i++) {
264  std::array<bool, rank> is_at_end{};
265  std::array<bool, rank> is_at_begin{};
266 
267  // is the ith element the end of an coeff (always true), of a row, of a matrix, ...?
268  for (std::size_t k = 0; k < rank; k++) {
269  if ((i + 1) % (std::accumulate(tensor.dimensions().rbegin(), tensor.dimensions().rbegin() + k, 1,
270  std::multiplies<IndexType>())) ==
271  0) {
272  is_at_end[k] = true;
273  }
274  }
275 
276  // is the ith element the begin of an coeff (always true), of a row, of a matrix, ...?
277  for (std::size_t k = 0; k < rank; k++) {
278  if (i % (std::accumulate(tensor.dimensions().rbegin(), tensor.dimensions().rbegin() + k, 1,
279  std::multiplies<IndexType>())) ==
280  0) {
281  is_at_begin[k] = true;
282  }
283  }
284 
285  // do we have a line break?
286  bool is_at_begin_after_newline = false;
287  for (std::size_t k = 0; k < rank; k++) {
288  if (is_at_begin[k]) {
289  std::size_t separator_index = (k < fmt.separator.size()) ? k : fmt.separator.size() - 1;
290  if (fmt.separator[separator_index].find('\n') != std::string::npos) {
291  is_at_begin_after_newline = true;
292  }
293  }
294  }
295 
296  bool is_at_end_before_newline = false;
297  for (std::size_t k = 0; k < rank; k++) {
298  if (is_at_end[k]) {
299  std::size_t separator_index = (k < fmt.separator.size()) ? k : fmt.separator.size() - 1;
300  if (fmt.separator[separator_index].find('\n') != std::string::npos) {
301  is_at_end_before_newline = true;
302  }
303  }
304  }
305 
306  std::stringstream suffix, prefix, separator;
307  for (std::size_t k = 0; k < rank; k++) {
308  std::size_t suffix_index = (k < fmt.suffix.size()) ? k : fmt.suffix.size() - 1;
309  if (is_at_end[k]) {
310  suffix << fmt.suffix[suffix_index];
311  }
312  }
313  for (std::size_t k = 0; k < rank; k++) {
314  std::size_t separator_index = (k < fmt.separator.size()) ? k : fmt.separator.size() - 1;
315  if (is_at_end[k] &&
316  (!is_at_end_before_newline || fmt.separator[separator_index].find('\n') != std::string::npos)) {
317  separator << fmt.separator[separator_index];
318  }
319  }
320  for (std::size_t k = 0; k < rank; k++) {
321  std::size_t spacer_index = (k < fmt.spacer.size()) ? k : fmt.spacer.size() - 1;
322  if (i != 0 && is_at_begin_after_newline && (!is_at_begin[k] || k == 0)) {
323  prefix << fmt.spacer[spacer_index];
324  }
325  }
326  for (int k = rank - 1; k >= 0; k--) {
327  std::size_t prefix_index = (static_cast<std::size_t>(k) < fmt.prefix.size()) ? k : fmt.prefix.size() - 1;
328  if (is_at_begin[k]) {
329  prefix << fmt.prefix[prefix_index];
330  }
331  }
332 
333  s << prefix.str();
334  // So we don't mess around with formatting, output scalar to a string stream, and adjust the width/fill manually.
335  std::stringstream sstr;
336  sstr.copyfmt(s);
337  ScalarPrinter<Scalar, Format>::run(sstr, static_cast<PrintType>(tensor.data()[i]), fmt);
338  std::string scalar_str = sstr.str();
339  IndexType scalar_width = scalar_str.length();
340  if (width && scalar_width < width) {
341  std::string filler;
342  for (IndexType j = scalar_width; j < width; ++j) {
343  filler.push_back(fmt.fill);
344  }
345  s << filler;
346  }
347  s << scalar_str;
348  s << suffix.str();
349  if (i < total_size - 1) {
350  s << separator.str();
351  }
352  }
353  s << fmt.tenSuffix;
354  if (explicit_precision) s.precision(old_precision);
355  }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
#define eigen_assert(x)
Definition: Macros.h:910
SCALAR Scalar
Definition: bench_gemm.cpp:45
static constexpr int Layout
Definition: Tensor.h:81
internal::traits< Self >::Index Index
Definition: Tensor.h:74
@ RowMajor
Definition: Constants.h:320
RealScalar s
Definition: level1_cplx_impl.h:130
return int(ret)+1
char char char int int * k
Definition: level2_impl.h:374
constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE auto array_prod(const array< T, N > &arr) -> decltype(array_reduce< product_op, T, N >(arr, static_cast< T >(1)))
Definition: MoreMeta.h:497
squared absolute value
Definition: GlobalFunctions.h:87
@ StreamPrecision
Definition: IO.h:20
@ FullPrecision
Definition: IO.h:20
@ DontAlignCols
Definition: IO.h:19
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286
@ IsInteger
Definition: NumTraits.h:174
static void run(std::ostream &stream, const Scalar &scalar, const Format &)
Definition: TensorIO.h:198
@ value
Definition: Meta.h:206
static int run()
Definition: IO.h:121
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References Eigen::internal::array_prod(), Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::data(), Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::dimensions(), Eigen::DontAlignCols, eigen_assert, Eigen::FullPrecision, i, int(), j, k, Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::Layout, Eigen::RowMajor, Eigen::internal::significant_decimals_impl< Scalar >::run(), Eigen::internal::ScalarPrinter< Scalar, Format, EnableIf >::run(), s, Eigen::StreamPrecision, oomph::Global_string_for_annotation::string(), and Eigen::value.


The documentation for this struct was generated from the following file: