Cytnx v0.9.1
Loading...
Searching...
No Matches
Symmetry.hpp
Go to the documentation of this file.
1#ifndef _H_Symmetry
2#define _H_Symmetry
3
4#include "Type.hpp"
5#include "cytnx_error.hpp"
7#include <string>
8#include <cstdio>
9#include <iostream>
10#include <ostream>
11#include "utils/vec_clone.hpp"
12namespace cytnx {
14 struct __sym {
15 enum __stype { U = -1, Z = 0 };
16 };
17
18 class SymmetryType_class {
19 public:
20 enum : int {
21 Void = -99,
22 U = -1,
23 Z = 0,
24 };
25 std::string getname(const int &stype);
26 };
28
41 extern SymmetryType_class SymType;
42
43 // helper class, has implicitly conversion to vector<int64>!
44 class Qs {
45 private:
46 std::vector<cytnx_int64> tmpQs;
47
48 public:
49 template <class... Ts>
50 Qs(const cytnx_int64 &e1, const Ts... elems) {
51 this->tmpQs = dynamic_arg_int64_resolver(e1, elems...);
52 }
53
54 Qs(const std::vector<cytnx_int64> &qin) { this->tmpQs = qin; }
55
56 // interprete as 2d vector directly implicitly convert!
57 explicit operator std::vector<cytnx_int64>() const { return this->tmpQs; };
58
59 std::pair<std::vector<cytnx_int64>, cytnx_uint64> operator>>(const cytnx_uint64 &dim) {
60 return make_pair(this->tmpQs, dim);
61 }
62 };
63
64 /*
65 template<class... Ts>
66 std::vector<cytnx_int64> Qs(const cytnx_int64 &e1, const Ts &...elems){
67 std::vector<cytnx_int64> argv = dynamic_arg_int64_resolver(e1, elems...);
68 return argv;
69 }
70 */
71
73 class Symmetry_base : public intrusive_ptr_base<Symmetry_base> {
74 public:
75 int stype_id;
76 int n;
77 Symmetry_base() : stype_id(SymType.Void){};
78 Symmetry_base(const int &n) : stype_id(SymType.Void) { this->Init(n); };
79 Symmetry_base(const Symmetry_base &rhs);
80 Symmetry_base &operator=(const Symmetry_base &rhs);
81
82 std::vector<cytnx_int64> combine_rule(const std::vector<cytnx_int64> &inL,
83 const std::vector<cytnx_int64> &inR);
84 cytnx_int64 combine_rule(const cytnx_int64 &inL, const cytnx_int64 &inR,
85 const bool &is_reverse);
86
87 cytnx_int64 reverse_rule(const cytnx_int64 &in);
88
89 virtual void Init(const int &n){};
90 virtual boost::intrusive_ptr<Symmetry_base> clone() { return nullptr; };
91 virtual bool check_qnum(
92 const cytnx_int64 &in_qnum); // check the passed in qnums satisfy the symmetry requirement.
93 virtual bool check_qnums(const std::vector<cytnx_int64> &in_qnums);
94 virtual void combine_rule_(std::vector<cytnx_int64> &out, const std::vector<cytnx_int64> &inL,
95 const std::vector<cytnx_int64> &inR);
96 virtual void combine_rule_(cytnx_int64 &out, const cytnx_int64 &inL, const cytnx_int64 &inR,
97 const bool &is_reverse);
98 virtual void reverse_rule_(cytnx_int64 &out, const cytnx_int64 &in);
99 virtual void print_info() const;
100 // virtual std::vector<cytnx_int64>& combine_rule(const std::vector<cytnx_int64> &inL, const
101 // std::vector<cytnx_int64> &inR);
102 };
104
106 class U1Symmetry : public Symmetry_base {
107 public:
108 U1Symmetry() { this->stype_id = SymType.U; };
109 U1Symmetry(const int &n) { this->Init(n); };
110 void Init(const int &n) {
111 this->stype_id = SymType.U;
112 this->n = n;
113 if (n != 1) cytnx_error_msg(1, "%s", "[ERROR] U1Symmetry should set n = 1");
114 }
115 boost::intrusive_ptr<Symmetry_base> clone() {
116 boost::intrusive_ptr<Symmetry_base> out(new U1Symmetry(this->n));
117 return out;
118 }
119 bool check_qnum(const cytnx_int64 &in_qnum);
120 bool check_qnums(const std::vector<cytnx_int64> &in_qnums);
121 void combine_rule_(std::vector<cytnx_int64> &out, const std::vector<cytnx_int64> &inL,
122 const std::vector<cytnx_int64> &inR);
123 void combine_rule_(cytnx_int64 &out, const cytnx_int64 &inL, const cytnx_int64 &inR,
124 const bool &is_reverse);
125 void reverse_rule_(cytnx_int64 &out, const cytnx_int64 &in);
126 void print_info() const;
127 };
129
131 class ZnSymmetry : public Symmetry_base {
132 public:
133 ZnSymmetry() { this->stype_id = SymType.Z; };
134 ZnSymmetry(const int &n) { this->Init(n); };
135 void Init(const int &n) {
136 this->stype_id = SymType.Z;
137 this->n = n;
138 if (n <= 1) cytnx_error_msg(1, "%s", "[ERROR] ZnSymmetry can only have n > 1");
139 }
140 boost::intrusive_ptr<Symmetry_base> clone() {
141 boost::intrusive_ptr<Symmetry_base> out(new ZnSymmetry(this->n));
142 return out;
143 }
144 bool check_qnum(const cytnx_int64 &in_qnum);
145 bool check_qnums(const std::vector<cytnx_int64> &in_qnums);
146 void combine_rule_(std::vector<cytnx_int64> &out, const std::vector<cytnx_int64> &inL,
147 const std::vector<cytnx_int64> &inR);
148 void combine_rule_(cytnx_int64 &out, const cytnx_int64 &inL, const cytnx_int64 &inR,
149 const bool &is_reverse);
150 void reverse_rule_(cytnx_int64 &out, const cytnx_int64 &in);
151 void print_info() const;
152 };
154
155 //=====================================
156 // this is API
158 class Symmetry {
159 public:
160 //[Note] these two are hide from user.
162 boost::intrusive_ptr<Symmetry_base> _impl;
163
164 Symmetry(const int &stype = -1, const int &n = 0) : _impl(new Symmetry_base()) {
165 this->Init(stype, n);
166 }; // default is U1Symmetry
167
168 void Init(const int &stype = -1, const int &n = 0) {
169 if (stype == SymType.U) {
170 boost::intrusive_ptr<Symmetry_base> tmp(new U1Symmetry(1));
171 this->_impl = tmp;
172 } else if (stype == SymType.Z) {
173 boost::intrusive_ptr<Symmetry_base> tmp(new ZnSymmetry(n));
174 this->_impl = tmp;
175 } else {
176 cytnx_error_msg(1, "%s", "[ERROR] invalid symmetry type.");
177 }
178 }
179 Symmetry &operator=(const Symmetry &rhs) {
180 this->_impl = rhs._impl;
181 return *this;
182 }
183 Symmetry(const Symmetry &rhs) { this->_impl = rhs._impl; }
185
186 //[genenrators]
215 static Symmetry U1() { return Symmetry(SymType.U, 1); }
216
246 static Symmetry Zn(const int &n) { return Symmetry(SymType.Z, n); }
247
262 Symmetry clone() const {
263 Symmetry out;
264 out._impl = this->_impl->clone();
265 return out;
266 }
267
274 int stype() const { return this->_impl->stype_id; }
275
285 int &n() const { return this->_impl->n; }
286
294 std::string stype_str() const {
295 return SymType.getname(this->_impl->stype_id) + std::to_string(this->_impl->n);
296 }
297
304 bool check_qnum(const cytnx_int64 &qnum) { return this->_impl->check_qnum(qnum); }
305
313 bool check_qnums(const std::vector<cytnx_int64> &qnums) {
314 return this->_impl->check_qnums(qnums);
315 }
316
323 std::vector<cytnx_int64> combine_rule(const std::vector<cytnx_int64> &inL,
324 const std::vector<cytnx_int64> &inR) {
325 return this->_impl->combine_rule(inL, inR);
326 }
327
335 void combine_rule_(std::vector<cytnx_int64> &out, const std::vector<cytnx_int64> &inL,
336 const std::vector<cytnx_int64> &inR) {
337 this->_impl->combine_rule_(out, inL, inR);
338 }
339
347 const bool &is_reverse = false) const {
348 return this->_impl->combine_rule(inL, inR, is_reverse);
349 }
350
358 void combine_rule_(cytnx_int64 &out, const cytnx_int64 &inL, const cytnx_int64 &inR,
359 const bool &is_reverse = false) {
360 this->_impl->combine_rule_(out, inL, inR, is_reverse);
361 }
362
371 void reverse_rule_(cytnx_int64 &out, const cytnx_int64 &in) {
372 this->_impl->reverse_rule_(out, in);
373 }
374
382 cytnx_int64 reverse_rule(const cytnx_int64 &in) const { return this->_impl->reverse_rule(in); }
383
389 void Save(const std::string &fname) const;
390
394 void Save(const char *fname) const;
395
402 static Symmetry Load(const std::string &fname);
403
407 static Symmetry Load(const char *fname);
408
410 void _Save(std::fstream &f) const;
411 void _Load(std::fstream &f);
413
417 void print_info() const { this->_impl->print_info(); }
418
422 bool operator==(const Symmetry &rhs) const;
423
427 bool operator!=(const Symmetry &rhs) const;
428 };
429
431 std::ostream &operator<<(std::ostream &os, const Symmetry &in);
433
434} // namespace cytnx
435#endif
Definition Symmetry.hpp:44
Qs(const cytnx_int64 &e1, const Ts... elems)
Definition Symmetry.hpp:50
Qs(const std::vector< cytnx_int64 > &qin)
Definition Symmetry.hpp:54
std::pair< std::vector< cytnx_int64 >, cytnx_uint64 > operator>>(const cytnx_uint64 &dim)
Definition Symmetry.hpp:59
the symmetry object
Definition Symmetry.hpp:158
static Symmetry Zn(const int &n)
create a Zn descrete symmetry object with
Definition Symmetry.hpp:246
bool check_qnum(const cytnx_int64 &qnum)
check the quantum number qnum is within the valid value range of current Symmetry.
Definition Symmetry.hpp:304
std::vector< cytnx_int64 > combine_rule(const std::vector< cytnx_int64 > &inL, const std::vector< cytnx_int64 > &inR)
apply combine rule of current symmetry to two quantum number lists.
Definition Symmetry.hpp:323
std::string stype_str() const
return the symmetry type name of current Symmetry object in string form, see cytnx::SymType.
Definition Symmetry.hpp:294
void Save(const char *fname) const
Same as Save(const std::string &fname) const;.
cytnx_int64 combine_rule(const cytnx_int64 &inL, const cytnx_int64 &inR, const bool &is_reverse=false) const
apply combine rule of current symmetry to two quantum numbers.
Definition Symmetry.hpp:346
Symmetry clone() const
return a clone instance of current Symmetry object.
Definition Symmetry.hpp:262
bool check_qnums(const std::vector< cytnx_int64 > &qnums)
check all the quantum numbers \qnums are within the valid value range of current Symmetry.
Definition Symmetry.hpp:313
int stype() const
return the symmetry type-id of current Symmetry object, see cytnx::SymType.
Definition Symmetry.hpp:274
int & n() const
return the descrete n of current Symmetry object.
Definition Symmetry.hpp:285
bool operator==(const Symmetry &rhs) const
the equality operator of the Symmetry object.
void combine_rule_(std::vector< cytnx_int64 > &out, const std::vector< cytnx_int64 > &inL, const std::vector< cytnx_int64 > &inR)
apply combine rule of current symmetry to two quantum number lists, and store it into parameter out.
Definition Symmetry.hpp:335
void combine_rule_(cytnx_int64 &out, const cytnx_int64 &inL, const cytnx_int64 &inR, const bool &is_reverse=false)
apply combine rule of current symmetry to two quantum numbers, and store the combined quntun number i...
Definition Symmetry.hpp:358
void print_info() const
Print the information of current Symmetry object.
Definition Symmetry.hpp:417
static Symmetry U1()
create a U1 symmetry object
Definition Symmetry.hpp:215
cytnx_int64 reverse_rule(const cytnx_int64 &in) const
Apply reverse rule of current symmetry to a given quantum number and return the result.
Definition Symmetry.hpp:382
static Symmetry Load(const char *fname)
Same as static Symmetry Load(const std::string &fname);.
void reverse_rule_(cytnx_int64 &out, const cytnx_int64 &in)
Apply reverse rule of current symmetry to a given quantum number and store in parameter out.
Definition Symmetry.hpp:371
bool operator!=(const Symmetry &rhs) const
the inequality operator of the Symmetry object.
void Save(const std::string &fname) const
Save the current Symmetry object to a file.
static Symmetry Load(const std::string &fname)
Load a Symmetry object from a file.
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:16
Definition Accessor.hpp:12
uint64_t cytnx_uint64
Definition Type.hpp:45
int64_t cytnx_int64
Definition Type.hpp:48
SymmetryType_class SymType
Symmetry type.