7#include <initializer_list>
13#include "utils/vec_clone.hpp"
41 class Bond_impl :
public intrusive_ptr_base<Bond_impl> {
47 std::vector<cytnx_uint64> _degs;
52 std::vector<std::vector<cytnx_int64>> _qnums;
53 std::vector<Symmetry> _syms;
55 Bond_impl() : _dim(0), _type(bondType::BD_REG){};
57 void _rm_qnum(
const cytnx_uint64 &q_index) {
59 this->_dim -= this->_degs[q_index];
60 this->_degs.erase(this->_degs.begin() + q_index);
61 this->_qnums.erase(this->_qnums.begin() + q_index);
64 void Init(
const cytnx_uint64 &dim,
const bondType &bd_type = bondType::BD_REG);
67 void Init(
const bondType &bd_type,
const std::vector<std::vector<cytnx_int64>> &in_qnums,
68 const std::vector<cytnx_uint64> °s,
const std::vector<Symmetry> &in_syms = {});
70 bondType type()
const {
return this->_type; };
71 const std::vector<std::vector<cytnx_int64>> &qnums()
const {
return this->_qnums; }
72 std::vector<std::vector<cytnx_int64>> &qnums() {
return this->_qnums; }
73 const cytnx_uint64 &dim()
const {
return this->_dim; }
74 cytnx_uint32 Nsym()
const {
return this->_syms.size(); }
75 const std::vector<Symmetry> &syms()
const {
return this->_syms; }
76 std::vector<Symmetry> &syms() {
return this->_syms; }
79 std::vector<std::vector<cytnx_int64>> qnums_clone()
const {
return this->_qnums; }
80 std::vector<Symmetry> syms_clone()
const {
return vec_clone(this->_syms); }
82 bool has_duplicate_qnums()
const {
83 if (this->_degs.size()) {
84 auto tmp = this->_qnums;
85 std::sort(tmp.begin(), tmp.end());
86 return std::adjacent_find(tmp.begin(), tmp.end()) != tmp.end();
92 void set_type(
const bondType &new_bondType) {
93 if ((this->_type != BD_REG)) {
94 if (new_bondType == BD_REG) {
96 "[ERROR] cannot change type to BD_REG for a symmetry bond.%s",
"\n");
98 if (std::abs(
int(this->_type)) != std::abs(
int(new_bondType))) {
100 "[ERROR] cannot exchange BDtype between BD_* <-> gBD_* .%s",
"\n");
104 this->_type = new_bondType;
108 if (this->_type != BD_REG) {
109 cytnx_error_msg(this->_qnums.size(),
"[ERROR] cannot clear type for a symmetry bond.%s",
112 this->_type = bondType::BD_REG;
115 boost::intrusive_ptr<Bond_impl> clone()
const {
116 boost::intrusive_ptr<Bond_impl> out(
new Bond_impl());
117 out->_dim = this->dim();
118 out->_type = this->type();
119 out->_qnums = this->qnums_clone();
120 out->_syms = this->syms_clone();
121 out->_degs = this->_degs;
127 void combineBond_(
const boost::intrusive_ptr<Bond_impl> &bd_in,
const bool &is_grp =
true);
129 boost::intrusive_ptr<Bond_impl> combineBond(
const boost::intrusive_ptr<Bond_impl> &bd_in,
130 const bool &is_grp =
true) {
131 boost::intrusive_ptr<Bond_impl> out = this->clone();
132 out->combineBond_(bd_in, is_grp);
137 std::vector<std::vector<cytnx_int64>> getUniqueQnums(std::vector<cytnx_uint64> &counts,
138 const bool &return_counts);
141 cytnx_uint64 getDegeneracy(
const std::vector<cytnx_int64> &qnum,
const bool &return_indices,
142 std::vector<cytnx_uint64> &indices);
145 std::vector<std::vector<cytnx_int64>> calc_reverse_qnums();
147 std::vector<cytnx_uint64> &getDegeneracies() {
return this->_degs; };
148 const std::vector<cytnx_uint64> &getDegeneracies()
const {
return this->_degs; };
150 std::vector<cytnx_uint64> group_duplicates_();
152 boost::intrusive_ptr<Bond_impl> group_duplicates(std::vector<cytnx_uint64> &mapper)
const {
153 boost::intrusive_ptr<Bond_impl> out = this->clone();
154 mapper = out->group_duplicates_();
158 void force_combineBond_(
const boost::intrusive_ptr<Bond_impl> &bd_in,
const bool &is_grp);
179 boost::intrusive_ptr<Bond_impl> _impl;
180 Bond() : _impl(
new Bond_impl()){};
181 Bond(
const Bond &rhs) { this->_impl = rhs._impl; }
183 this->_impl = rhs._impl;
201 : _impl(new Bond_impl()) {
202 this->_impl->Init(
dim, bd_type);
225 Bond(
const bondType &bd_type,
const std::vector<std::vector<cytnx_int64>> &in_qnums,
226 const std::vector<cytnx_uint64> °s,
const std::vector<Symmetry> &in_syms = {})
227 : _impl(new Bond_impl()) {
228 this->_impl->Init(bd_type, in_qnums, degs, in_syms);
238 Bond(
const bondType &bd_type,
const std::initializer_list<std::vector<cytnx_int64>> &in_qnums,
239 const std::vector<cytnx_uint64> °s,
const std::vector<Symmetry> &in_syms = {})
240 : _impl(new Bond_impl()) {
241 this->_impl->Init(bd_type, in_qnums, degs, in_syms);
253 const std::vector<cytnx_uint64> °s,
const std::vector<Symmetry> &in_syms = {})
254 : _impl(new Bond_impl()) {
255 vec2d<cytnx_int64>
qnums(in_qnums.begin(), in_qnums.end());
256 this->_impl->Init(bd_type,
qnums, degs, in_syms);
267 const std::vector<std::pair<std::vector<cytnx_int64>, cytnx_uint64>> &in_qnums_dims,
268 const std::vector<Symmetry> &in_syms = {})
269 : _impl(new Bond_impl()) {
270 this->
Init(bd_type, in_qnums_dims, in_syms);
301 this->_impl->Init(
dim, bd_type);
339 void Init(
const bondType &bd_type,
const std::vector<std::vector<cytnx_int64>> &in_qnums,
340 const std::vector<cytnx_uint64> °s,
const std::vector<Symmetry> &in_syms = {}) {
341 this->_impl->Init(bd_type, in_qnums, degs, in_syms);
350 const std::vector<std::pair<std::vector<cytnx_int64>, cytnx_uint64>> &in_qnums_dims,
351 const std::vector<Symmetry> &in_syms = {}) {
352 vec2d<cytnx_int64>
qnums(in_qnums_dims.size());
353 std::vector<cytnx_uint64> degs(in_qnums_dims.size());
354 for (
int i = 0; i < in_qnums_dims.size(); i++) {
355 qnums[i] = in_qnums_dims[i].first;
356 degs[i] = in_qnums_dims[i].second;
358 this->_impl->Init(bd_type,
qnums, degs, in_syms);
373 const std::vector<std::vector<cytnx_int64>> &
qnums()
const {
return this->_impl->qnums(); };
377 std::vector<std::vector<cytnx_int64>> &
qnums() {
return this->_impl->qnums(); };
386 return this->_impl->qnums_clone();
393 cytnx_uint64
dim()
const {
return this->_impl->dim(); };
399 cytnx_uint32
Nsym()
const {
return this->_impl->syms().size(); };
407 const std::vector<Symmetry> &
syms()
const {
return this->_impl->syms(); };
411 std::vector<Symmetry> &
syms() {
return this->_impl->syms(); };
419 std::vector<Symmetry>
syms_clone()
const {
return this->_impl->syms_clone(); };
429 this->_impl->
set_type(new_bondType);
443 auto out = this->
clone();
444 out.set_type(new_bondType);
453 auto out = this->
clone();
454 out.set_type(
bondType(
int(out.type()) * -1));
490 out._impl = this->_impl->
clone();
521 this->_impl->combineBond_(bd_in._impl, is_grp);
553 out._impl = this->_impl->
combineBond(bd_in._impl, is_grp);
586 for (cytnx_uint64 i = 0; i < bds.size(); i++) {
608 void combineBond_(
const std::vector<Bond> &bds,
const bool &is_grp =
true) {
609 for (cytnx_uint64 i = 0; i < bds.size(); i++) {
646 "Bond combineBond(const Bond &bd_in, const bool &is_grp) const "
648 combineBonds(
const std::vector<Bond> &bds,
const bool &is_grp =
true) {
650 for (cytnx_uint64 i = 0; i < bds.size(); i++) {
687 "combineBond_(const std::vector<Bond> &bds, const bool &is_grp) "
690 for (cytnx_uint64 i = 0; i < bds.size(); i++) {
702 std::vector<std::vector<cytnx_int64>>
getUniqueQnums(std::vector<cytnx_uint64> &counts) {
703 return this->_impl->getUniqueQnums(counts,
true);
713 std::vector<cytnx_uint64> tmp;
714 return this->_impl->getUniqueQnums(tmp,
false);
731 std::vector<cytnx_uint64> tmp;
732 return this->_impl->getDegeneracy(qnum,
false, tmp);
749 std::vector<cytnx_uint64> &indices)
const {
751 return this->_impl->getDegeneracy(qnum,
true, indices);
762 std::vector<cytnx_uint64> &
getDegeneracies() {
return this->_impl->getDegeneracies(); }
768 return this->_impl->getDegeneracies();
836 return this->_impl->calc_reverse_qnums();
846 void Save(
const std::string &fname)
const;
851 void Save(
const char *fname)
const;
867 void _Save(std::fstream &f)
const;
868 void _Load(std::fstream &f);
932 std::ostream &operator<<(std::ostream &os,
const Bond &bin);
the object contains auxiliary properties for each Tensor rank (bond)
Definition Bond.hpp:176
std::vector< std::vector< cytnx_int64 > > & qnums()
Definition Bond.hpp:377
Bond & set_type(const bondType &new_bondType)
change the tag-type of the instance Bond
Definition Bond.hpp:428
cytnx_uint64 getDegeneracy(const std::vector< cytnx_int64 > &qnum) const
return the degeneracy of specify qnum set.
Definition Bond.hpp:730
std::vector< cytnx_uint64 > group_duplicates_()
Group the duplicated quantum number, inplacely.
Definition Bond.hpp:787
Bond combineBond(const std::vector< Bond > &bds, const bool &is_grp=true)
combine multiple input bonds with self, and return a new combined Bond instance.
Definition Bond.hpp:584
static cytnx::Bond Load(const std::string &fname)
Load the Bond object from the file.
void Init(const bondType &bd_type, const std::vector< std::pair< std::vector< cytnx_int64 >, cytnx_uint64 > > &in_qnums_dims, const std::vector< Symmetry > &in_syms={})
Definition Bond.hpp:349
Bond retype(const bondType &new_bondType)
create a new instance of Bond with type changed to the new tag-type.
Definition Bond.hpp:442
void combineBonds_(const std::vector< Bond > &bds, const bool &is_grp=true)
combine multiple input bonds with self, inplacely
Definition Bond.hpp:689
Bond & operator*=(const Bond &rhs)
The multiplication assignment operator of the Bond object.
Definition Bond.hpp:925
const std::vector< Symmetry > & syms() const
return the vector of symmetry objects by reference.
Definition Bond.hpp:407
Bond(const cytnx_uint64 &dim, const bondType &bd_type=bondType::BD_REG)
The constructor of the Bond object.
Definition Bond.hpp:200
Bond combineBond(const Bond &bd_in, const bool &is_grp=true) const
combine the input bond with self, and return a new combined Bond instance.
Definition Bond.hpp:551
std::vector< std::vector< cytnx_int64 > > qnums_clone() const
return the clone (deep copy) of the current quantum number set(s)
Definition Bond.hpp:385
Bond combineBonds(const std::vector< Bond > &bds, const bool &is_grp=true)
combine multiple input bonds with self, and return a new combined Bond instance.
Definition Bond.hpp:648
Bond operator*(const Bond &rhs) const
The multiplication operator of the Bond object.
Definition Bond.hpp:906
Bond group_duplicates(std::vector< cytnx_uint64 > &mapper) const
Group the duplicated quantum number and return the new instance of the Bond ojbect.
Definition Bond.hpp:809
void Init(const cytnx_uint64 &dim, const bondType &bd_type=bondType::BD_REG)
init a bond object
Definition Bond.hpp:300
void clear_type()
change the tag-type to the default value bondType.BD_REG.
Definition Bond.hpp:472
Bond(const bondType &bd_type, const std::vector< cytnx::Qs > &in_qnums, const std::vector< cytnx_uint64 > °s, const std::vector< Symmetry > &in_syms={})
Definition Bond.hpp:252
Bond redirect() const
create a new instance of Bond with type changed in between bondType.BD_BRA / bondType....
Definition Bond.hpp:452
const std::vector< cytnx_uint64 > & getDegeneracies() const
Definition Bond.hpp:767
void combineBond_(const std::vector< Bond > &bds, const bool &is_grp=true)
combine multiple input bonds with self, inplacely
Definition Bond.hpp:608
std::vector< cytnx_uint64 > & getDegeneracies()
return all degeneracies.
Definition Bond.hpp:762
bondType type() const
return the current bond type (see cytnx::bondType).
Definition Bond.hpp:365
Bond clone() const
return a copy of the instance Bond
Definition Bond.hpp:488
cytnx_uint64 getDegeneracy(const std::vector< cytnx_int64 > &qnum, std::vector< cytnx_uint64 > &indices) const
return the degeneracy of specify qnum set.
Definition Bond.hpp:748
std::vector< Symmetry > syms_clone() const
return copy of the vector of symmetry objects.
Definition Bond.hpp:419
Bond & redirect_()
Change the bond type between bondType.BD_BRA and bondType.BD_KET in the Bond.
Definition Bond.hpp:462
cytnx_uint64 dim() const
return the dimension of the bond
Definition Bond.hpp:393
std::vector< std::vector< cytnx_int64 > > getUniqueQnums()
return a sorted qnum sets by removing all the duplicate qnum sets.
Definition Bond.hpp:712
const std::vector< std::vector< cytnx_int64 > > & qnums() const
return the current quantum number set(s) by reference
Definition Bond.hpp:373
void combineBond_(const Bond &bd_in, const bool &is_grp=true)
Combine the input bond with self, inplacely.
Definition Bond.hpp:520
std::vector< Symmetry > & syms()
Definition Bond.hpp:411
Bond(const bondType &bd_type, const std::vector< std::pair< std::vector< cytnx_int64 >, cytnx_uint64 > > &in_qnums_dims, const std::vector< Symmetry > &in_syms={})
Definition Bond.hpp:266
void Save(const char *fname) const
cytnx_uint32 Nsym() const
return the number of the symmetries
Definition Bond.hpp:399
bool has_duplicate_qnums() const
Check whether there is duplicated quantum numbers in the Bond.
Definition Bond.hpp:825
std::vector< std::vector< cytnx_int64 > > getUniqueQnums(std::vector< cytnx_uint64 > &counts)
return a sorted qnum sets by removing all the duplicate qnum sets.
Definition Bond.hpp:702
std::vector< std::vector< cytnx_int64 > > calc_reverse_qnums()
Calculate the reverse of the quantum numbers.
Definition Bond.hpp:835
Bond(const bondType &bd_type, const std::vector< std::vector< cytnx_int64 > > &in_qnums, const std::vector< cytnx_uint64 > °s, const std::vector< Symmetry > &in_syms={})
The constructor of the Bond object.
Definition Bond.hpp:225
bool operator!=(const Bond &rhs) const
The comparison operator 'not equal to'.
void Init(const bondType &bd_type, const std::vector< std::vector< cytnx_int64 > > &in_qnums, const std::vector< cytnx_uint64 > °s, const std::vector< Symmetry > &in_syms={})
init a bond object
Definition Bond.hpp:339
void Save(const std::string &fname) const
Save the Bond object to the file.
static cytnx::Bond Load(const char *fname)
Bond(const bondType &bd_type, const std::initializer_list< std::vector< cytnx_int64 > > &in_qnums, const std::vector< cytnx_uint64 > °s, const std::vector< Symmetry > &in_syms={})
Definition Bond.hpp:238
bool operator==(const Bond &rhs) const
The comparison operator 'equal to'.
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:18
Definition Accessor.hpp:12
bondType
bond type
Definition Bond.hpp:31
@ BD_IN
Definition Bond.hpp:36
@ BD_OUT
Definition Bond.hpp:37
@ BD_NONE
Definition Bond.hpp:35
@ BD_REG
Definition Bond.hpp:34
@ BD_BRA
Definition Bond.hpp:33
@ BD_KET
Definition Bond.hpp:32