Tenncor
teq.hpp
Go to the documentation of this file.
1 
9 #include <unordered_map>
10 
11 #include "teq/ileaf.hpp"
12 #include "teq/functor.hpp"
13 
14 #include "dbg/stream/tree.hpp"
15 
16 #ifndef DBG_TEQ_HPP
17 #define DBG_TEQ_HPP
18 
20 using LabelsMapT = std::unordered_map<teq::iTensor*,std::string>;
21 
23 struct PrettyEquation final
24 {
26  [](teq::iTensor*& root) -> std::vector<teq::iTensor*>
27  {
28  if (teq::iFunctor* f = dynamic_cast<teq::iFunctor*>(root))
29  {
30  auto& children = f->get_children();
31  std::vector<teq::iTensor*> tens(children.size());
32  std::transform(children.begin(), children.end(), tens.begin(),
33  [](const teq::FuncArg& child)
34  {
35  return child.get_tensor().get();
36  });
37  return tens;
38  }
39  return {};
40  },
41  [this](std::ostream& out, teq::iTensor*& root)
42  {
43  if (root)
44  {
45  auto it = this->labels_.find(root);
46  if (this->labels_.end() != it)
47  {
48  out << it->second << "=";
49  }
50  if (auto var = dynamic_cast<teq::iLeaf*>(root))
51  {
52  out << (var->is_const() ? "constant:" : "variable:");
53  }
54  out << root->to_string();
55  if (showshape_)
56  {
57  out << root->shape().to_string();
58  }
59  }
60  }) {}
61 
63  void print (std::ostream& out, const teq::TensptrT& ptr)
64  {
65  drawer_.print(out, ptr.get());
66  }
67 
69  void print (std::ostream& out, teq::iTensor* ptr)
70  {
71  drawer_.print(out, ptr);
72  }
73 
76 
78  bool showshape_ = false;
79 
80 private:
83 };
84 
85 #endif // DBG_TEQ_HPP
void print(std::ostream &out, const teq::TensptrT &ptr)
Stream equation of ptr to out.
Definition: teq.hpp:63
PrettyEquation(void)
Definition: teq.hpp:25
LabelsMapT labels_
For every label associated with a tensor, show LABEL=value in the tree.
Definition: teq.hpp:75
Interface of iOperation-defined operation node.
Definition: ifunctor.hpp:28
PrettyTree< teq::iTensor * > drawer_
Actual ascii renderer.
Definition: teq.hpp:82
Coordinate mapper and tensor pair.
Definition: funcarg.hpp:21
void print(std::ostream &out, T root)
Definition: tree.hpp:33
bool showshape_
Print every tensor&#39;s shape if true, otherwise don&#39;t.
Definition: teq.hpp:78
std::unordered_map< teq::iTensor *, std::string > LabelsMapT
Map tensor to label.
Definition: teq.hpp:20
std::shared_ptr< iTensor > TensptrT
Tensor smart pointer.
Definition: itensor.hpp:51
Definition: tree.hpp:22
Use PrettyTree to render teq::TensptrT graph as an ascii art.
Definition: teq.hpp:23
Definition: coord.hpp:16
Interface of traversible and differentiable nodes with shape information.
Definition: itensor.hpp:36
void print(std::ostream &out, teq::iTensor *ptr)
Stream equation of raw ptr to out.
Definition: teq.hpp:69