Tenncor
tensor.hpp
Go to the documentation of this file.
1 
9 #include <iostream>
10 #include <vector>
11 
12 #ifndef DBG_TENSOR_HPP
13 #define DBG_TENSOR_HPP
14 
16 template <typename T>
17 struct PrettyTensor final
18 {
25  PrettyTensor (std::vector<uint16_t> datalimit) : datalimit_(datalimit) {}
26 
31  void print (std::ostream& out, T* arr, std::vector<uint8_t> shape)
32  {
33  print_helper(out, shape, arr, shape.size() - 1);
34  }
35 
37  std::vector<uint16_t> datalimit_;
38 
39 private:
40  void print_helper (std::ostream& out, const std::vector<uint8_t>& shape,
41  T* arr, uint8_t rank)
42  {
43  out << "[";
44  uint16_t n = shape[rank];
45  // apply limit only if limit is available
46  if (rank < datalimit_.size())
47  {
48  n = std::min(n, datalimit_[rank]);
49  }
50  if (rank == 0)
51  {
52  out << arr[0];
53  for (uint16_t i = 1; i < n; ++i)
54  {
55  out << "," << arr[i];
56  }
57  }
58  else
59  {
60  auto it = shape.begin();
61  size_t before = std::accumulate(it, it + rank, (size_t) 1,
62  std::multiplies<size_t>());
63  print_helper(out, shape, arr, rank - 1);
64  for (uint16_t i = 1; i < n; ++i)
65  {
66  out << ",";
67  print_helper(out, shape, arr + i * before, rank - 1);
68  }
69  }
70  if (n < shape[rank])
71  {
72  out << "..";
73  }
74  out << "]";
75  }
76 };
77 
78 #endif // DBG_TENSOR_HPP
void print(std::ostream &out, T *arr, std::vector< uint8_t > shape)
Definition: tensor.hpp:31
void print_helper(std::ostream &out, const std::vector< uint8_t > &shape, T *arr, uint8_t rank)
Definition: tensor.hpp:40
PrettyTensor(std::vector< uint16_t > datalimit)
Definition: tensor.hpp:25
Draw data as a multi-dimension array (similar to python) according to shape.
Definition: tensor.hpp:17
EigenptrT< T > min(teq::Shape &outshape, const OpArg< T > &a, const OpArg< T > &b)
Definition: operator.hpp:939
std::vector< uint16_t > datalimit_
Number of elements to show for each dimension.
Definition: tensor.hpp:37