Xped
Loading...
Searching...
No Matches
Qbasis.hpp
Go to the documentation of this file.
1#ifndef QBASIS_H_
2#define QBASIS_H_
3
5#include <numeric>
6#include <set>
7#include <unordered_map>
8#include <unordered_set>
9
10#include "tabulate/table.hpp"
11// #include "tabulate/tabulate.hpp"
12
13#include "yas/serialize.hpp"
14#include "yas/std_types.hpp"
16
17#include "Xped/Core/Basis.hpp"
20#include "Xped/Hash/hash.hpp"
21
22namespace Xped {
23
37template <typename Symmetry, std::size_t depth = 1, typename AllocationPolicy = HeapPolicy>
38class Qbasis
39{
40 template <typename Symmetry_, std::size_t depth_, typename AllocationPolicy_>
41 friend class Qbasis;
42
43 typedef typename Symmetry::qType qType;
44
45 using ContainerType =
46 std::vector<std::tuple<qType, size_t, Basis>, typename AllocationPolicy::template Allocator<std::tuple<qType, std::size_t, Basis>>>;
47 template <std::size_t tree_depth>
48 using TreeVector = std::vector<FusionTree<tree_depth, Symmetry>, typename AllocationPolicy::template Allocator<FusionTree<tree_depth, Symmetry>>>;
49 using TreeType = std::unordered_map<
50 qType,
51 TreeVector<depth>,
52 std::hash<qType>,
53 std::equal_to<qType>,
54 typename AllocationPolicy::template Allocator<
55 std::pair<const qType,
56 std::vector<FusionTree<depth, Symmetry>, typename AllocationPolicy::template Allocator<FusionTree<depth, Symmetry>>>>>>;
57
58public:
60 Qbasis() = default;
61
65 template <typename Container>
66 Qbasis(const Container& qins, const size_t& dim)
67 {
68 for(const auto& qin : qins) { push_back(qin, dim); }
69 }
70
72
74
75 inline std::size_t dim() const
76 {
77 std::size_t out = 0;
78 for(const auto& [qVal, num, plain] : data_) { out += plain.dim(); }
79 return out;
80 }
81
86 inline std::size_t fullDim() const
87 {
88 std::size_t out = 0;
89 for(const auto& [qVal, num, plain] : data_) { out += plain.dim() * Symmetry::degeneracy(qVal); }
90 return out;
91 }
92
94 std::size_t largestSector() const;
95
97 inline std::size_t Nq() const { return data_.size(); }
99
103 qType getQ(const size_t& num) const;
104
106 inline std::vector<qType> qs() const
107 {
108 std::vector<qType> out;
109 out.reserve(data_.size());
110 for(auto triple : data_) { out.push_back(std::get<0>(triple)); }
111 return out;
112 }
113 inline std::unordered_set<qType> unordered_qs() const
114 {
115 std::unordered_set<qType> out;
116 for(auto triple : data_) { out.insert(std::get<0>(triple)); }
117 return out;
118 }
120
122 inline std::vector<std::size_t> dims() const
123 {
124 std::vector<std::size_t> out;
125 out.reserve(data_.size());
126 for(auto triple : data_) { out.push_back(std::get<2>(triple).dim()); }
127 return out;
128 }
130
132
133 bool IS_PRESENT(const qType& q) const { return cfind(q) != data_.end(); }
135 bool NOT_PRESENT(const qType& q) const { return !IS_PRESENT(q); }
137
139
140 size_t inner_num(const size_t& outer_num) const;
142 size_t inner_dim(const size_t& num_in) const;
144 size_t inner_dim(const qType& q) const;
146 size_t outer_num(const qType& q) const;
148 size_t full_outer_num(const qType& q) const;
150 size_t index(const qType& q) const;
152
154 qType qn(std::size_t index) const
155 {
156 assert(index < data_.size());
157 return std::get<0>(data_[index]);
158 };
159
160 void remove(const qType& q);
161
162 inline qType maxQ() const
163 {
164 auto qs = this->qs();
165 return *std::max_element(qs.begin(), qs.end());
166 }
167 inline qType minQ() const
168 {
169 auto qs = this->qs();
170 return *std::min_element(qs.begin(), qs.end());
171 }
172 // template<typename Iterator>
173 // void remove(Iterator& begin, Iterator& end);
174
175 void truncate(const std::unordered_set<qType>& qs, const std::size_t& M);
176
177 size_t leftOffset(const FusionTree<depth, Symmetry>& tree, const std::array<size_t, depth>& plain = std::array<std::size_t, depth>()) const;
178 size_t rightOffset(const FusionTree<depth, Symmetry>& tree, const std::array<size_t, depth>& plain = std::array<std::size_t, depth>()) const;
179
181 void push_back(const qType& q, const size_t& inner_dim);
182
183 void push_back(const qType& q, const size_t& inner_dim, const TreeVector<depth>& tree);
184
185 void setRandom(const std::size_t& fullSize, const std::size_t& max_sectorSize = 5ul);
186
188 inline void clear()
189 {
190 data_.clear();
191 trees.clear();
192 curr_dim = 0;
193 }
194
200
203
204 std::pair<std::vector<std::size_t>, Qbasis<Symmetry, depth, AllocationPolicy>> shift(qType qshift) const;
205
208
210
212
214 std::string print() const;
215
217 std::string info() const;
218
220 std::string printTrees() const;
221
223
224 inline auto begin() { return data_.begin(); }
225 inline auto end() { return data_.end(); }
226
227 inline auto cbegin() const { return data_.cbegin(); }
228 inline auto cend() const { return data_.cend(); }
229
231 // void swap (Qbasis<Symmetry,depth> &other) { this->data.swap(other.data()); }
232
233 std::vector<std::size_t> sort();
234
235 inline bool IS_SORTED() const { return IS_SORTED_; }
236 inline bool IS_CONJ() const { return CONJ; }
237
238 inline void SET_CONJ() { CONJ = true; }
239
240 inline const auto& tree(const qType& q) const
241 {
242 auto it = trees.find(q);
243 assert(it != trees.end());
244 return it->second;
245 }
246
247 inline TreeType& allTrees() { return trees; }
248
249 template <typename Ar>
250 void serialize(Ar& ar)
251 {
252 ar& YAS_OBJECT_NVP("Qbasis", ("data", data_), ("curr_dim", curr_dim), ("trees", trees), ("IS_SORTED", IS_SORTED_), ("CONJ", CONJ));
253 }
254
255private:
256 typename ContainerType::const_iterator cfind(const qType& q) const;
257
258 typename ContainerType::iterator find(const qType& q);
259
260 // vector with entry: {Quantumnumber (QN), state number of the first plain state for this QN, all plain states for this QN in a Basis object.}
261 //[{q1,0,plain_q1}, {q2,dim(plain_q1),plain_q2}, {q3,dim(plain_q1)+dim(plain_q2),plain_q3}, ..., {qi, sum_j^(i-1)dim(plain_qj), plain_qi}]
262 ContainerType data_{};
263 std::size_t curr_dim = 0;
264 TreeType trees{};
265
266 bool IS_SORTED_ = false;
267 bool CONJ = false;
268};
269
270template <typename Symmetry, std::size_t depth, typename AllocationPolicy>
271std::ostream& operator<<(std::ostream& os, const Qbasis<Symmetry, depth, AllocationPolicy>& basis)
272{
273 os << basis.print();
274 return os;
275}
276
277} // namespace Xped
278
279#ifndef XPED_COMPILED_LIB
280# include "Core/Qbasis.cpp"
281#endif
282
283#endif
Definition: Qbasis.hpp:39
void serialize(Ar &ar)
Definition: Qbasis.hpp:250
std::size_t fullDim() const
Definition: Qbasis.hpp:86
static Qbasis< Symmetry, 1, AllocationPolicy > TrivialBasis()
Definition: Qbasis.cpp:24
std::vector< std::size_t > sort()
Definition: Qbasis.cpp:304
size_t outer_num(const qType &q) const
Definition: Qbasis.cpp:165
std::string print() const
Definition: Qbasis.cpp:506
qType getQ(const size_t &num) const
Definition: Qbasis.cpp:132
auto cbegin() const
Definition: Qbasis.hpp:227
TreeType & allTrees()
Definition: Qbasis.hpp:247
size_t index(const qType &q) const
Definition: Qbasis.cpp:187
void push_back(const qType &q, const size_t &inner_dim)
Definition: Qbasis.cpp:32
Qbasis< Symmetry, 1, AllocationPolicy > forgetHistory() const
Definition: Qbasis.cpp:408
std::size_t Nq() const
Definition: Qbasis.hpp:97
qType qn(std::size_t index) const
Definition: Qbasis.hpp:154
Qbasis< Symmetry, depth, AllocationPolicy > conj() const
Definition: Qbasis.cpp:416
Qbasis< Symmetry, depth, AllocationPolicy > add(const Qbasis< Symmetry, depth, AllocationPolicy > &other) const
Definition: Qbasis.cpp:351
Qbasis()=default
std::string printTrees() const
Definition: Qbasis.cpp:543
void setRandom(const std::size_t &fullSize, const std::size_t &max_sectorSize=5ul)
Definition: Qbasis.cpp:91
std::string info() const
Definition: Qbasis.cpp:495
std::unordered_set< qType > unordered_qs() const
Definition: Qbasis.hpp:113
size_t full_outer_num(const qType &q) const
Definition: Qbasis.cpp:173
std::pair< std::vector< std::size_t >, Qbasis< Symmetry, depth, AllocationPolicy > > shift(qType qshift) const
Definition: Qbasis.cpp:375
bool IS_SORTED() const
Definition: Qbasis.hpp:235
auto begin()
Definition: Qbasis.hpp:224
size_t leftOffset(const FusionTree< depth, Symmetry > &tree, const std::array< size_t, depth > &plain=std::array< std::size_t, depth >()) const
Definition: Qbasis.cpp:206
std::size_t largestSector() const
Definition: Qbasis.cpp:103
qType maxQ() const
Definition: Qbasis.hpp:162
bool IS_CONJ() const
Definition: Qbasis.hpp:236
Qbasis(const Container &qins, const size_t &dim)
Definition: Qbasis.hpp:66
bool NOT_PRESENT(const qType &q) const
Definition: Qbasis.hpp:135
const auto & tree(const qType &q) const
Definition: Qbasis.hpp:240
size_t inner_num(const size_t &outer_num) const
Definition: Qbasis.cpp:143
void SET_CONJ()
Definition: Qbasis.hpp:238
auto cend() const
Definition: Qbasis.hpp:228
size_t inner_dim(const size_t &num_in) const
Definition: Qbasis.cpp:195
Qbasis< Symmetry, depth+1, AllocationPolicy > combine(const Qbasis< Symmetry, 1, AllocationPolicy > &other, bool CONJ=false) const
Definition: Qbasis.cpp:439
void clear()
Definition: Qbasis.hpp:188
size_t rightOffset(const FusionTree< depth, Symmetry > &tree, const std::array< size_t, depth > &plain=std::array< std::size_t, depth >()) const
Definition: Qbasis.cpp:225
bool operator==(const Qbasis< Symmetry, depth, AllocationPolicy > &other) const
Definition: Qbasis.cpp:331
void remove(const qType &q)
Definition: Qbasis.cpp:247
void truncate(const std::unordered_set< qType > &qs, const std::size_t &M)
Definition: Qbasis.cpp:275
Qbasis< Symmetry, depth, AllocationPolicy > intersection(const Qbasis< Symmetry, depth, AllocationPolicy > &other) const
Definition: Qbasis.cpp:395
qType minQ() const
Definition: Qbasis.hpp:167
std::size_t dim() const
Definition: Qbasis.hpp:75
auto end()
Definition: Qbasis.hpp:225
std::vector< std::size_t > dims() const
Definition: Qbasis.hpp:122
bool IS_PRESENT(const qType &q) const
Definition: Qbasis.hpp:133
std::vector< qType > qs() const
Definition: Qbasis.hpp:106
Definition: bench.cpp:62
std::ostream & operator<<(std::ostream &os, const FusionTree< depth, Symmetry > &tree)
Definition: FusionTree.hpp:93
Definition: FusionTree.hpp:24