10 #ifndef EIGEN_CXX11_TENSOR_TENSOR_IO_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_IO_H
17 struct TensorIOFormat;
20 template <
typename Tensor, std::
size_t rank>
25 TensorIOFormat(
const std::vector<std::string>& _separator,
const std::vector<std::string>& _prefix,
26 const std::vector<std::string>& _suffix,
int _precision =
StreamPrecision,
int _flags = 0,
27 const std::string& _tenPrefix =
"",
const std::string& _tenSuffix =
"",
const char _fill =
' ')
40 const std::string& _tenSuffix =
"",
const char _fill =
' ')
60 for (std::size_t k = 1; k <
prefix.size(); k++) {
61 int j = int(
prefix[k].length()) - 1;
62 while (
j >= 0 &&
prefix[k][
j] !=
'\n') {
70 std::vector<std::string>
prefix = {
"",
"["};
71 std::vector<std::string>
suffix = {
"",
"]"};
72 std::vector<std::string>
separator = {
" ",
"\n"};
77 std::vector<std::string>
separator = {
" ",
"\n",
"\n",
""};
78 std::vector<std::string>
prefix = {
""};
79 std::vector<std::string>
suffix = {
""};
84 std::vector<std::string>
separator = {
", ",
",\n",
"\n"};
85 std::vector<std::string>
prefix = {
"",
"{"};
86 std::vector<std::string>
suffix = {
"",
"}"};
108 template <
typename T,
int Layout,
int rank>
111 template <
typename T,
int rank>
121 tensor.evalSubExprsIfNeeded(NULL);
122 internal::TensorPrinter<Evaluator, rank>::run(os, tensor, wf.
t_format);
133 template <
typename T,
int rank>
140 typedef typename T::Index IndexType;
141 std::array<IndexType, rank> shuffle;
142 std::array<IndexType, rank> id;
143 std::iota(
id.begin(),
id.
end(), IndexType(0));
144 std::copy(
id.begin(),
id.
end(), shuffle.rbegin());
145 auto tensor_row_major = wf.
t_tensor.swap_layout().shuffle(shuffle);
151 tensor.evalSubExprsIfNeeded(NULL);
152 internal::TensorPrinter<Evaluator, rank>::run(os, tensor, wf.
t_format);
163 template <
typename T>
173 tensor.evalSubExprsIfNeeded(NULL);
174 internal::TensorPrinter<Evaluator, 0>::run(os, tensor, wf.
t_format);
186 template <
typename Tensor, std::
size_t rank>
187 struct TensorPrinter {
189 typedef std::remove_const_t<typename Tensor::Scalar> Scalar;
196 if (total_size > 0) {
197 const IndexType first_dim = Eigen::internal::array_get<0>(_t.
dimensions());
199 total_size / first_dim);
206 typedef std::conditional_t<is_same<Scalar, char>::value || is_same<Scalar, unsigned char>::value ||
207 is_same<Scalar, numext::int8_t>::value || is_same<Scalar, numext::uint8_t>::value,
209 std::conditional_t<is_same<Scalar, std::complex<char> >::value ||
210 is_same<Scalar, std::complex<unsigned char> >::value ||
211 is_same<Scalar, std::complex<numext::int8_t> >::value ||
212 is_same<Scalar, std::complex<numext::uint8_t> >::value,
213 std::complex<int>,
const Scalar&>> PrintType;
217 std::streamsize explicit_precision;
219 explicit_precision = 0;
222 explicit_precision = 0;
224 explicit_precision = significant_decimals_impl<Scalar>::run();
230 std::streamsize old_precision = 0;
231 if (explicit_precision) old_precision = s.precision(explicit_precision);
238 for (IndexType
i = 0;
i < total_size;
i++) {
239 std::stringstream sstr;
241 sstr << static_cast<PrintType>(_t.
data()[
i]);
242 width = std::max<IndexType>(width, IndexType(sstr.str().length()));
245 std::streamsize old_width = s.width();
246 char old_fill_character = s.fill();
249 for (IndexType
i = 0;
i < total_size;
i++) {
250 std::array<bool, rank> is_at_end{};
251 std::array<bool, rank> is_at_begin{};
254 for (std::size_t k = 0; k < rank; k++) {
256 std::multiplies<IndexType>())) ==
263 for (std::size_t k = 0; k < rank; k++) {
265 std::multiplies<IndexType>())) ==
267 is_at_begin[k] =
true;
272 bool is_at_begin_after_newline =
false;
273 for (std::size_t k = 0; k < rank; k++) {
274 if (is_at_begin[k]) {
275 std::size_t separator_index = (k < fmt.
separator.size()) ? k : fmt.
separator.size() - 1;
276 if (fmt.
separator[separator_index].find(
'\n') != std::string::npos) {
277 is_at_begin_after_newline =
true;
282 bool is_at_end_before_newline =
false;
283 for (std::size_t k = 0; k < rank; k++) {
285 std::size_t separator_index = (k < fmt.
separator.size()) ? k : fmt.
separator.size() - 1;
286 if (fmt.
separator[separator_index].find(
'\n') != std::string::npos) {
287 is_at_end_before_newline =
true;
292 std::stringstream suffix, prefix, separator;
293 for (std::size_t k = 0; k < rank; k++) {
294 std::size_t suffix_index = (k < fmt.
suffix.size()) ? k : fmt.
suffix.size() - 1;
296 suffix << fmt.
suffix[suffix_index];
299 for (std::size_t k = 0; k < rank; k++) {
300 std::size_t separator_index = (k < fmt.
separator.size()) ? k : fmt.
separator.size() - 1;
302 (!is_at_end_before_newline || fmt.
separator[separator_index].find(
'\n') != std::string::npos)) {
303 separator << fmt.
separator[separator_index];
306 for (std::size_t k = 0; k < rank; k++) {
307 std::size_t spacer_index = (k < fmt.
spacer.size()) ? k : fmt.
spacer.size() - 1;
308 if (
i != 0 && is_at_begin_after_newline && (!is_at_begin[k] || k == 0)) {
309 prefix << fmt.
spacer[spacer_index];
312 for (
int k = rank - 1; k >= 0; k--) {
313 std::size_t prefix_index = (
static_cast<std::size_t
>(k) < fmt.
prefix.size()) ? k : fmt.
prefix.size() - 1;
314 if (is_at_begin[k]) {
315 prefix << fmt.
prefix[prefix_index];
327 if (
i < total_size - 1) {
328 s << separator.str();
332 if (explicit_precision) s.precision(old_precision);
334 s.fill(old_fill_character);
340 template <
typename Tensor>
341 struct TensorPrinter<Tensor, 0> {
342 static void run(std::ostream& s,
const Tensor& _t,
const TensorIOFormat& fmt) {
345 std::streamsize explicit_precision;
347 explicit_precision = 0;
350 explicit_precision = 0;
352 explicit_precision = significant_decimals_impl<Scalar>::run();
355 explicit_precision = fmt.precision;
358 std::streamsize old_precision = 0;
359 if (explicit_precision) old_precision = s.precision(explicit_precision);
361 s << fmt.tenPrefix << _t.coeff(0) << fmt.tenSuffix;
362 if (explicit_precision) s.precision(old_precision);
367 template <
typename T>
static constexpr int Layout
internal::traits< Self >::Index Index
const Dimensions & dimensions() const
static const lastp1_t end
constexpr auto array_prod(const array< T, N > &arr) -> decltype(array_reduce< product_op, T, N >(arr, static_cast< T >(1)))
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
std::ostream & operator<<(std::ostream &s, const DiagonalBase< Derived > &m)
A cost model used to limit the number of threads used for evaluating tensor expression.