Cytnx v0.7.4
Loading...
Searching...
No Matches
contraction_tree.hpp
Go to the documentation of this file.
1#ifndef _H_contraction_tree_
2#define _H_contraction_tree_
3
4#include "Type.hpp"
5#include "UniTensor.hpp"
6#include "cytnx_error.hpp"
7#include "utils/utils.hpp"
8#include <vector>
9#include <map>
10#include <string>
11
12namespace cytnx{
14 class Node{
15 public:
16 UniTensor utensor; //don't worry about copy, because everything are references in cytnx!
17 bool is_assigned;
18 Node *left;
19 Node *right;
20 std::string name;
21 Node *root;
22
23 Node():is_assigned(false), left(nullptr), right(nullptr), root(nullptr){};
24 Node(const Node &rhs){
25 this->left = rhs.left;
26 this->right = rhs.right;
27 this->root = rhs.root;
28 this->utensor = rhs.utensor;
29 this->is_assigned = rhs.is_assigned;
30 }
31 Node& operator==(const Node &rhs){
32 this->left = rhs.left;
33 this->right = rhs.right;
34 this->root = rhs.root;
35 this->utensor = rhs.utensor;
36 this->is_assigned = rhs.is_assigned;
37 return *this;
38 }
39 Node(Node *in_left, Node *in_right, const UniTensor &in_uten=UniTensor()):is_assigned(false), left(nullptr), right(nullptr), root(nullptr){
40 this->left = in_left;
41 this->right = in_right;
42 in_left->root = this;
43 in_right->root = this;
44 if(in_uten.uten_type()!=UTenType.Void) this->utensor = in_uten;
45 }
46 void assign_utensor(const UniTensor &in_uten){
47 this->utensor = in_uten;
48 this->is_assigned = true;
49 }
50 void clear_utensor(){
51 this->is_assigned = false;
52 this->utensor = UniTensor();
53 }
54
55 };
56
57 class ContractionTree{
58 public:
59 std::vector<Node> nodes_container; // this contains intermediate layer.
60 std::vector<Node> base_nodes; // this is the button layer.
61
62 ContractionTree(){};
63 ContractionTree(const ContractionTree &rhs){
64 this->nodes_container = rhs.nodes_container;
65 this->base_nodes = rhs.base_nodes;
66 }
67 ContractionTree& operator==(const ContractionTree &rhs){
68 this->nodes_container = rhs.nodes_container;
69 this->base_nodes = rhs.base_nodes;
70 return *this;
71 }
72
73 // clear all the elements in the whole tree.
74 void clear(){
75 nodes_container.clear();
76 base_nodes.clear();
77 //nodes_container.reserve(1024);
78 }
79 // clear all the intermediate layer, leave all the base_nodes intact.
80 // and reset the root pointer on the base ondes
81 void reset_contraction_order(){
82 nodes_container.clear();
83 for(cytnx_uint64 i=0;i<base_nodes.size();i++){
84 base_nodes[i].root = nullptr;
85 }
86 //nodes_container.reserve(1024);
87 }
88 void reset_nodes(){
89 //reset all nodes but keep the skeleton
90 for(cytnx_uint64 i=0;i<this->nodes_container.size();i++){
91 this->nodes_container[i].clear_utensor();
92 }
93 for(cytnx_uint64 i=0;i<this->base_nodes.size();i++){
94 this->base_nodes[i].clear_utensor();
95 }
96 }
97 void build_default_contraction_tree();
98 void build_contraction_tree_by_tokens(const std::map<std::string,cytnx_uint64> &name2pos, const std::vector<std::string> &tokens);
99
100 };
102}//namespace
103#endif
Definition Accessor.hpp:12
UniTensorType_class UTenType
Definition UniTensor_base.cpp:21
Tensor operator==(const Tensor &Lt, const Tensor &Rt)