tensor.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2015 Virginia Tech
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Justus Calvin
19  * Department of Chemistry, Virginia Tech
20  *
21  * tensor.h
22  * Jun 16, 2015
23  *
24  */
25 
26 #ifndef TILEDARRAY_TENSOR_H__INCLUDED
27 #define TILEDARRAY_TENSOR_H__INCLUDED
28 
29 #include <TiledArray/block_range.h>
35 
36 namespace TiledArray {
37 
38 // Template aliases for TensorInterface objects
39 
40 template <typename T>
42 
43 template <typename T>
46 
48 
54 template <typename T, typename std::enable_if<detail::is_tensor<T>::value &&
56  T>::value>::type* = nullptr>
57 inline std::ostream& operator<<(std::ostream& os, const T& t) {
58  os << t.range() << " { ";
59  const auto n = t.range().volume();
60  for (auto i = 0ul; i < n; ++i) os << t[i] << " ";
61 
62  os << "}";
63 
64  return os;
65 }
66 
68 
74 template <typename T, typename std::enable_if<detail::is_tensor<T>::value &&
75  !detail::is_contiguous_tensor<
76  T>::value>::type* = nullptr>
77 inline std::ostream& operator<<(std::ostream& os, const T& t) {
78  const auto stride = inner_size(t);
79  const auto volume = t.range().volume();
80 
81  auto tensor_print_range =
82  [&os, stride](typename T::const_pointer MADNESS_RESTRICT const t_data) {
83  for (decltype(t.range().volume()) i = 0ul; i < stride; ++i)
84  os << t_data[i] << " ";
85  };
86 
87  os << t.range() << " { ";
88 
89  for (decltype(t.range().volume()) i = 0ul; i < volume; i += stride)
90  tensor_print_range(t.data() + t.range().ordinal(i));
91 
92  os << "}";
93 
94  return os;
95 }
96 
97 template<typename T,
98  typename = std::enable_if_t<detail::is_tensor_of_tensor_v<T>>>
99 inline std::ostream& operator<<(std::ostream& os, const T& t){
100  os << t.range() << " {" << std::endl; // Outer tensor's range
101  for(auto idx : t.range()){ // Loop over inner tensors
102  const auto& inner_t = t(idx);
103  os << " " << idx << ":" << inner_t << std::endl;
104  }
105  os << "}"; // End outer tensor
106  return os;
107 }
108 
109 } // namespace TiledArray
110 
111 #endif // TILEDARRAY_SRC_TILEDARRAY_TENSOR_H__INCLUDED
Range that references a subblock of another range.
Definition: block_range.h:34
auto inner_size(const Permutation &p)
Definition: permutation.h:822
std::ostream & operator<<(std::ostream &os, const DistArray< Tile, Policy > &a)
Add the tensor to an output stream.
Definition: dist_array.h:1602
Tensor interface for external data.
size_t volume(const DistArray< Tile, Policy > &a)
Definition: dist_array.h:1622