Cytnx v1.0.0
Loading...
Searching...
No Matches
Symmetry.hpp
Go to the documentation of this file.
1#ifndef CYTNX_SYMMETRY_H_
2#define CYTNX_SYMMETRY_H_
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"
12
13namespace cytnx {
15 struct __sym {
16 enum __stype { U = -1, Z = 0 };
17 };
18
19 class SymmetryType_class {
20 public:
21 enum : int {
22 Void = -99,
23 U = -1,
24 Z = 0,
25 };
26 std::string getname(const int &stype);
27 };
29
42 extern SymmetryType_class SymType;
43
44 // helper class, has implicitly conversion to vector<int64>!
45 class Qs {
46 private:
47 std::vector<cytnx_int64> tmpQs;
48
49 public:
50 template <class... Ts>
51 Qs(const cytnx_int64 &e1, const Ts... elems) {
52 this->tmpQs = dynamic_arg_int64_resolver(e1, elems...);
53 }
54
55 Qs(const std::vector<cytnx_int64> &qin) { this->tmpQs = qin; }
56
57 // interprete as 2d vector directly implicitly convert!
58 explicit operator std::vector<cytnx_int64>() const { return this->tmpQs; };
59
60 std::pair<std::vector<cytnx_int64>, cytnx_uint64> operator>>(const cytnx_uint64 &dim) {
61 return make_pair(this->tmpQs, dim);
62 }
63 };
64
66 class Symmetry_base : public intrusive_ptr_base<Symmetry_base> {
67 public:
68 int stype_id;
69 int n;
70 Symmetry_base() : stype_id(SymType.Void){};
71 Symmetry_base(const int &n) : stype_id(SymType.Void) { this->Init(n); };
72 Symmetry_base(const Symmetry_base &rhs);
73 Symmetry_base &operator=(const Symmetry_base &rhs);
74
75 std::vector<cytnx_int64> combine_rule(const std::vector<cytnx_int64> &inL,
76 const std::vector<cytnx_int64> &inR);
77 cytnx_int64 combine_rule(const cytnx_int64 &inL, const cytnx_int64 &inR,
78 const bool &is_reverse);
79
80 cytnx_int64 reverse_rule(const cytnx_int64 &in);
81
82 virtual void Init(const int &n){};
83 virtual boost::intrusive_ptr<Symmetry_base> clone() { return nullptr; };
84 virtual bool check_qnum(
85 const cytnx_int64 &in_qnum); // check the passed in qnums satisfy the symmetry requirement.
86 virtual bool check_qnums(const std::vector<cytnx_int64> &in_qnums);
87 virtual void combine_rule_(std::vector<cytnx_int64> &out, const std::vector<cytnx_int64> &inL,
88 const std::vector<cytnx_int64> &inR);
89 virtual void combine_rule_(cytnx_int64 &out, const cytnx_int64 &inL, const cytnx_int64 &inR,
90 const bool &is_reverse);
91 virtual void reverse_rule_(cytnx_int64 &out, const cytnx_int64 &in);
92 virtual void print_info() const;
93 // virtual std::vector<cytnx_int64>& combine_rule(const std::vector<cytnx_int64> &inL, const
94 // std::vector<cytnx_int64> &inR);
95 };
97
99 class U1Symmetry : public Symmetry_base {
100 public:
101 U1Symmetry() { this->stype_id = SymType.U; };
102 U1Symmetry(const int &n) { this->Init(n); };
103 void Init(const int &n) {
104 this->stype_id = SymType.U;
105 this->n = n;
106 if (n != 1) cytnx_error_msg(1, "%s", "[ERROR] U1Symmetry should set n = 1");
107 }
108 boost::intrusive_ptr<Symmetry_base> clone() {
109 boost::intrusive_ptr<Symmetry_base> out(new U1Symmetry(this->n));
110 return out;
111 }
112 bool check_qnum(const cytnx_int64 &in_qnum);
113 bool check_qnums(const std::vector<cytnx_int64> &in_qnums);
114 void combine_rule_(std::vector<cytnx_int64> &out, const std::vector<cytnx_int64> &inL,
115 const std::vector<cytnx_int64> &inR);
116 void combine_rule_(cytnx_int64 &out, const cytnx_int64 &inL, const cytnx_int64 &inR,
117 const bool &is_reverse);
118 void reverse_rule_(cytnx_int64 &out, const cytnx_int64 &in);
119 void print_info() const;
120 };
122
124 class ZnSymmetry : public Symmetry_base {
125 public:
126 ZnSymmetry() { this->stype_id = SymType.Z; };
127 ZnSymmetry(const int &n) { this->Init(n); };
128 void Init(const int &n) {
129 this->stype_id = SymType.Z;
130 this->n = n;
131 if (n <= 1) cytnx_error_msg(1, "%s", "[ERROR] ZnSymmetry can only have n > 1");
132 }
133 boost::intrusive_ptr<Symmetry_base> clone() {
134 boost::intrusive_ptr<Symmetry_base> out(new ZnSymmetry(this->n));
135 return out;
136 }
137 bool check_qnum(const cytnx_int64 &in_qnum);
138 bool check_qnums(const std::vector<cytnx_int64> &in_qnums);
139 void combine_rule_(std::vector<cytnx_int64> &out, const std::vector<cytnx_int64> &inL,
140 const std::vector<cytnx_int64> &inR);
141 void combine_rule_(cytnx_int64 &out, const cytnx_int64 &inL, const cytnx_int64 &inR,
142 const bool &is_reverse);
143 void reverse_rule_(cytnx_int64 &out, const cytnx_int64 &in);
144 void print_info() const;
145 };
147
148 //=====================================
149 // this is API
151 class Symmetry {
152 public:
153 //[Note] these two are hide from user.
155 boost::intrusive_ptr<Symmetry_base> _impl;
156
157 Symmetry(const int &stype = -1, const int &n = 0) : _impl(new Symmetry_base()) {
158 this->Init(stype, n);
159 }; // default is U1Symmetry
160
161 void Init(const int &stype = -1, const int &n = 0) {
162 if (stype == SymType.U) {
163 boost::intrusive_ptr<Symmetry_base> tmp(new U1Symmetry(1));
164 this->_impl = tmp;
165 } else if (stype == SymType.Z) {
166 boost::intrusive_ptr<Symmetry_base> tmp(new ZnSymmetry(n));
167 this->_impl = tmp;
168 } else {
169 cytnx_error_msg(1, "%s", "[ERROR] invalid symmetry type.");
170 }
171 }
172 Symmetry &operator=(const Symmetry &rhs) {
173 this->_impl = rhs._impl;
174 return *this;
175 }
176 Symmetry(const Symmetry &rhs) { this->_impl = rhs._impl; }
178
179 //[genenrators]
208 static Symmetry U1() { return Symmetry(SymType.U, 1); }
209
239 static Symmetry Zn(const int &n) { return Symmetry(SymType.Z, n); }
240
255 Symmetry clone() const {
256 Symmetry out;
257 out._impl = this->_impl->clone();
258 return out;
259 }
260
267 int stype() const { return this->_impl->stype_id; }
268
278 int &n() const { return this->_impl->n; }
279
287 std::string stype_str() const {
288 return SymType.getname(this->_impl->stype_id) + std::to_string(this->_impl->n);
289 }
290
297 bool check_qnum(const cytnx_int64 &qnum) { return this->_impl->check_qnum(qnum); }
298
306 bool check_qnums(const std::vector<cytnx_int64> &qnums) {
307 return this->_impl->check_qnums(qnums);
308 }
309
316 std::vector<cytnx_int64> combine_rule(const std::vector<cytnx_int64> &inL,
317 const std::vector<cytnx_int64> &inR) {
318 return this->_impl->combine_rule(inL, inR);
319 }
320
328 void combine_rule_(std::vector<cytnx_int64> &out, const std::vector<cytnx_int64> &inL,
329 const std::vector<cytnx_int64> &inR) {
330 this->_impl->combine_rule_(out, inL, inR);
331 }
332
339 cytnx_int64 combine_rule(const cytnx_int64 &inL, const cytnx_int64 &inR,
340 const bool &is_reverse = false) const {
341 return this->_impl->combine_rule(inL, inR, is_reverse);
342 }
343
351 void combine_rule_(cytnx_int64 &out, const cytnx_int64 &inL, const cytnx_int64 &inR,
352 const bool &is_reverse = false) {
353 this->_impl->combine_rule_(out, inL, inR, is_reverse);
354 }
355
364 void reverse_rule_(cytnx_int64 &out, const cytnx_int64 &in) {
365 this->_impl->reverse_rule_(out, in);
366 }
367
375 cytnx_int64 reverse_rule(const cytnx_int64 &in) const { return this->_impl->reverse_rule(in); }
376
382 void Save(const std::string &fname) const;
383
387 void Save(const char *fname) const;
388
395 static Symmetry Load(const std::string &fname);
396
400 static Symmetry Load(const char *fname);
401
403 void _Save(std::fstream &f) const;
404 void _Load(std::fstream &f);
406
410 void print_info() const { this->_impl->print_info(); }
411
415 bool operator==(const Symmetry &rhs) const;
416
420 bool operator!=(const Symmetry &rhs) const;
421 };
422
424 std::ostream &operator<<(std::ostream &os, const Symmetry &in);
426
427} // namespace cytnx
428
429#endif // CYTNX_SYMMETRY_H_
Definition Symmetry.hpp:45
Qs(const cytnx_int64 &e1, const Ts... elems)
Definition Symmetry.hpp:51
Qs(const std::vector< cytnx_int64 > &qin)
Definition Symmetry.hpp:55
std::pair< std::vector< cytnx_int64 >, cytnx_uint64 > operator>>(const cytnx_uint64 &dim)
Definition Symmetry.hpp:60
the symmetry object
Definition Symmetry.hpp:151
static Symmetry Zn(const int &n)
create a Zn descrete symmetry object with
Definition Symmetry.hpp:239
bool check_qnum(const cytnx_int64 &qnum)
check the quantum number qnum is within the valid value range of current Symmetry.
Definition Symmetry.hpp:297
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:316
std::string stype_str() const
return the symmetry type name of current Symmetry object in string form, see cytnx::SymType.
Definition Symmetry.hpp:287
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:339
Symmetry clone() const
return a clone instance of current Symmetry object.
Definition Symmetry.hpp:255
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:306
int stype() const
return the symmetry type-id of current Symmetry object, see cytnx::SymType.
Definition Symmetry.hpp:267
int & n() const
return the descrete n of current Symmetry object.
Definition Symmetry.hpp:278
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:328
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:351
void print_info() const
Print the information of current Symmetry object.
Definition Symmetry.hpp:410
static Symmetry U1()
create a U1 symmetry object
Definition Symmetry.hpp:208
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:375
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:364
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:18
Definition Accessor.hpp:12
SymmetryType_class SymType
Symmetry type.