7#include <initializer_list>
13#include "utils/vec_clone.hpp"
36 class Bond_impl :
public intrusive_ptr_base<Bond_impl> {
42 std::vector<cytnx_uint64> _degs;
47 std::vector<std::vector<cytnx_int64>> _qnums;
48 std::vector<Symmetry> _syms;
50 Bond_impl() : _dim(0), _type(bondType::BD_REG){};
52 void _rm_qnum(
const cytnx_uint64 &q_index){
54 this->_dim -= this->_degs[q_index];
55 this->_degs.erase(this->_degs.begin() + q_index);
56 this->_qnums.erase(this->_qnums.begin() + q_index);
59 void Init(
const cytnx_uint64 &dim,
const bondType &bd_type = bondType::BD_REG,
60 const std::vector<std::vector<cytnx_int64>> &in_qnums = {},
61 const std::vector<Symmetry> &in_syms = {});
64 void Init(
const bondType &bd_type,
const std::vector<std::vector<cytnx_int64>> &in_qnums,
65 const std::vector<cytnx_uint64> °s,
const std::vector<Symmetry> &in_syms = {});
67 bondType type()
const {
return this->_type; };
68 const std::vector<std::vector<cytnx_int64>> &qnums()
const {
return this->_qnums; }
69 std::vector<std::vector<cytnx_int64>> &qnums() {
return this->_qnums; }
72 const std::vector<Symmetry> &syms()
const {
return this->_syms; }
73 std::vector<Symmetry> &syms() {
return this->_syms; }
76 std::vector<std::vector<cytnx_int64>> qnums_clone()
const {
return this->_qnums; }
77 std::vector<Symmetry> syms_clone()
const {
return vec_clone(this->_syms); }
79 bool has_duplicate_qnums()
const{
80 if(this->_degs.size()){
81 auto tmp = this->_qnums;
82 std::sort(
tmp.begin(),
tmp.end());
83 return std::adjacent_find(
tmp.begin(),
tmp.end()) !=
tmp.end();
89 void set_type(
const bondType &new_bondType) {
90 if ((this->_type != BD_REG)) {
91 if (new_bondType == BD_REG) {
93 "[ERROR] cannot change type to BD_REG for a symmetry bond.%s",
"\n");
95 if (std::abs(
int(this->_type)) != std::abs(
int(new_bondType))) {
97 "[ERROR] cannot exchange BDtype between BD_* <-> gBD_* .%s",
"\n");
101 this->_type = new_bondType;
105 if (this->_type != BD_REG) {
106 cytnx_error_msg(this->_qnums.size(),
"[ERROR] cannot clear type for a symmetry bond.%s",
109 this->_type = bondType::BD_REG;
112 boost::intrusive_ptr<Bond_impl> clone()
const {
113 boost::intrusive_ptr<Bond_impl> out(
new Bond_impl());
114 out->_dim = this->dim();
115 out->_type = this->type();
116 out->_qnums = this->qnums_clone();
117 out->_syms = this->syms_clone();
118 out->_degs = this->_degs;
124 void combineBond_(
const boost::intrusive_ptr<Bond_impl> &bd_in,
const bool &is_grp =
true);
126 boost::intrusive_ptr<Bond_impl> combineBond(
const boost::intrusive_ptr<Bond_impl> &bd_in,
127 const bool &is_grp =
true) {
128 boost::intrusive_ptr<Bond_impl> out = this->clone();
129 out->combineBond_(bd_in, is_grp);
134 std::vector<std::vector<cytnx_int64>> getUniqueQnums(std::vector<cytnx_uint64> &counts,
135 const bool &return_counts);
138 cytnx_uint64 getDegeneracy(
const std::vector<cytnx_int64> &qnum,
const bool &return_indices,
139 std::vector<cytnx_uint64> &indices);
142 std::vector<std::vector<cytnx_int64>> calc_reverse_qnums();
145 std::vector<cytnx_uint64>& getDegeneracies(){
return this->_degs;};
146 const std::vector<cytnx_uint64>& getDegeneracies()
const{
return this->_degs;};
148 std::vector<cytnx_uint64> group_duplicates_();
150 boost::intrusive_ptr<Bond_impl> group_duplicates(std::vector<cytnx_uint64> &mapper)
const{
151 boost::intrusive_ptr<Bond_impl> out = this->clone();
152 mapper = out->group_duplicates_();
156 void force_combineBond_(
const boost::intrusive_ptr<Bond_impl> &bd_in,
const bool &is_grp);
177 boost::intrusive_ptr<Bond_impl> _impl;
178 Bond() : _impl(
new Bond_impl()){};
179 Bond(
const Bond &rhs) { this->_impl = rhs._impl; }
181 this->_impl = rhs._impl;
210 const std::vector<std::vector<cytnx_int64>> &in_qnums = {},
211 const std::vector<Symmetry> &in_syms = {})
212 : _impl(new Bond_impl()) {
213 this->_impl->Init(
dim, bd_type, in_qnums, in_syms);
236 Bond(
const bondType &bd_type,
const std::vector<std::vector<cytnx_int64>> &in_qnums,
237 const std::vector<cytnx_uint64> °s,
const std::vector<Symmetry> &in_syms = {})
238 : _impl(new Bond_impl()) {
239 this->_impl->Init(bd_type, in_qnums, degs, in_syms);
250 Bond(
const bondType &bd_type,
const std::initializer_list<std::vector<cytnx_int64>> &in_qnums,
251 const std::vector<cytnx_uint64> °s,
const std::vector<Symmetry> &in_syms = {})
252 : _impl(new Bond_impl()) {
253 this->_impl->Init(bd_type, in_qnums, degs, in_syms);
265 const std::vector<cytnx_uint64> °s,
const std::vector<Symmetry> &in_syms = {})
266 : _impl(new Bond_impl()) {
268 this->_impl->Init(bd_type,
qnums, degs, in_syms);
279 const std::vector< std::pair<std::vector<cytnx_int64>,
cytnx_uint64> > &in_qnums_dims,
280 const std::vector<Symmetry> &in_syms = {})
281 : _impl(new Bond_impl()) {
282 this->
Init(bd_type, in_qnums_dims, in_syms);
326 const std::vector<std::vector<cytnx_int64>> &in_qnums = {},
327 const std::vector<Symmetry> &in_syms = {}) {
328 this->_impl->Init(
dim, bd_type, in_qnums, in_syms);
366 void Init(
const bondType &bd_type,
const std::vector<std::vector<cytnx_int64>> &in_qnums,
367 const std::vector<cytnx_uint64> °s,
const std::vector<Symmetry> &in_syms = {}) {
368 this->_impl->Init(bd_type, in_qnums, degs, in_syms);
377 const std::vector< std::pair<std::vector<cytnx_int64>,
cytnx_uint64> > &in_qnums_dims,
378 const std::vector<Symmetry> &in_syms = {}) {
380 std::vector<cytnx_uint64> degs(in_qnums_dims.size());
381 for(
int i=0;i<in_qnums_dims.size();i++){
382 qnums[i] = in_qnums_dims[i].first;
383 degs[i] = in_qnums_dims[i].second;
385 this->_impl->Init(bd_type,
qnums, degs, in_syms);
400 const std::vector<std::vector<cytnx_int64>> &
qnums()
const {
return this->_impl->qnums(); };
404 std::vector<std::vector<cytnx_int64>> &
qnums() {
return this->_impl->qnums(); };
413 return this->_impl->qnums_clone();
434 const std::vector<Symmetry> &
syms()
const {
return this->_impl->syms(); };
438 std::vector<Symmetry> &
syms() {
return this->_impl->syms(); };
446 std::vector<Symmetry>
syms_clone()
const {
return this->_impl->syms_clone(); };
456 this->_impl->
set_type(new_bondType);
470 auto out = this->
clone();
471 out.set_type(new_bondType);
480 auto out = this->
clone();
481 out.set_type(
bondType(
int(out.type()) * -1));
517 out._impl = this->_impl->
clone();
544 this->_impl->combineBond_(bd_in._impl, is_grp);
572 out._impl = this->_impl->
combineBond(bd_in._impl, is_grp);
629 void combineBonds_(
const std::vector<Bond> &bds,
const bool &is_grp =
true) {
642 std::vector<std::vector<cytnx_int64>>
getUniqueQnums(std::vector<cytnx_uint64> &counts) {
643 return this->_impl->getUniqueQnums(counts,
true);
653 std::vector<cytnx_uint64> tmp;
654 return this->_impl->getUniqueQnums(tmp,
false);
671 std::vector<cytnx_uint64> tmp;
672 return this->_impl->getDegeneracy(qnum,
false, tmp);
689 std::vector<cytnx_uint64> &indices)
const {
691 return this->_impl->getDegeneracy(qnum,
true, indices);
703 return this->_impl->getDegeneracies();
710 return this->_impl->getDegeneracies();
730 return this->_impl->group_duplicates_();
770 return this->_impl->has_duplicate_qnums();
782 return this->_impl->calc_reverse_qnums();
791 void Save(
const std::string &fname)
const;
796 void Save(
const char *fname)
const;
811 void _Save(std::fstream &f)
const;
812 void _Load(std::fstream &f);
874 std::ostream &
operator<<(std::ostream &os,
const Bond &bin);
the object contains auxiliary properties for each Tensor rank (bond)
Definition Bond.hpp:174
std::vector< std::vector< cytnx_int64 > > & qnums()
Definition Bond.hpp:404
Bond & set_type(const bondType &new_bondType)
change the tag-type of the instance Bond
Definition Bond.hpp:455
cytnx_uint64 getDegeneracy(const std::vector< cytnx_int64 > &qnum) const
return the degeneracy of specify qnum set.
Definition Bond.hpp:670
std::vector< cytnx_uint64 > group_duplicates_()
Group the duplicated quantum number, inplacely.
Definition Bond.hpp:729
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:376
Bond retype(const bondType &new_bondType)
create a new instance of Bond with type changed to the new tag-type.
Definition Bond.hpp:469
void combineBonds_(const std::vector< Bond > &bds, const bool &is_grp=true)
combine multiple input bonds with self, inplacely
Definition Bond.hpp:629
Bond & operator*=(const Bond &rhs)
The multiplication assignment operator of the Bond object.
Definition Bond.hpp:867
const std::vector< Symmetry > & syms() const
return the vector of symmetry objects by reference.
Definition Bond.hpp:434
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:570
std::vector< std::vector< cytnx_int64 > > qnums_clone() const
return the clone (deep copy) of the current quantum number set(s)
Definition Bond.hpp:412
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:599
static cytnx::Bond Load(const std::string &fname)
Load the Bond object from the file.
Definition Bond.cpp:604
Bond operator*(const Bond &rhs) const
The multiplication operator of the Bond object.
Definition Bond.hpp:848
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:753
void clear_type()
change the tag-type to the default value bondType.BD_REG.
Definition Bond.hpp:499
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:264
Bond redirect() const
create a new instance of Bond with type changed in between bondType.BD_BRA / bondType....
Definition Bond.hpp:479
void Init(const cytnx_uint64 &dim, const bondType &bd_type=bondType::BD_REG, const std::vector< std::vector< cytnx_int64 > > &in_qnums={}, const std::vector< Symmetry > &in_syms={})
init a bond object
Definition Bond.hpp:325
const std::vector< cytnx_uint64 > & getDegeneracies() const
Definition Bond.hpp:709
std::vector< cytnx_uint64 > & getDegeneracies()
return all degeneracies.
Definition Bond.hpp:702
Bond(const cytnx_uint64 &dim, const bondType &bd_type=bondType::BD_REG, const std::vector< std::vector< cytnx_int64 > > &in_qnums={}, const std::vector< Symmetry > &in_syms={})
The constructor of the Bond object.
Definition Bond.hpp:209
bondType type() const
return the current tag type
Definition Bond.hpp:392
Bond clone() const
return a copy of the instance Bond
Definition Bond.hpp:515
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:688
std::vector< Symmetry > syms_clone() const
return copy of the vector of symmetry objects.
Definition Bond.hpp:446
Bond & redirect_()
Change the bond type between bondType.BD_BRA and bondType.BD_KET in the Bond.
Definition Bond.hpp:489
cytnx_uint64 dim() const
return the dimension of the bond
Definition Bond.hpp:420
std::vector< std::vector< cytnx_int64 > > getUniqueQnums()
return a sorted qnum sets by removing all the duplicate qnum sets.
Definition Bond.hpp:652
const std::vector< std::vector< cytnx_int64 > > & qnums() const
return the current quantum number set(s) by reference
Definition Bond.hpp:400
void combineBond_(const Bond &bd_in, const bool &is_grp=true)
Combine the input bond with self, inplacely.
Definition Bond.hpp:543
std::vector< Symmetry > & syms()
Definition Bond.hpp:438
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:278
cytnx_uint32 Nsym() const
return the number of the symmetries
Definition Bond.hpp:426
bool has_duplicate_qnums() const
Check whether there is duplicated quantum numbers in the Bond.
Definition Bond.hpp:769
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:642
std::vector< std::vector< cytnx_int64 > > calc_reverse_qnums()
Calculate the reverse of the quantum numbers.
Definition Bond.hpp:781
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:236
bool operator!=(const Bond &rhs) const
The comparison operator 'not equal to'.
Definition Bond.cpp:582
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:366
void Save(const std::string &fname) const
Save the Bond object to the file.
Definition Bond.cpp:584
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:250
bool operator==(const Bond &rhs) const
The comparison operator 'equal to'.
Definition Bond.cpp:563
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:16
Definition Accessor.hpp:12
uint32_t cytnx_uint32
Definition Type.hpp:46
std::ostream & operator<<(std::ostream &os, const Scalar &in)
The stream operator for Scalar objects.
Definition Scalar.cpp:10
uint64_t cytnx_uint64
Definition Type.hpp:45
std::vector< std::vector< T > > vec2d
Definition Type.hpp:40
bondType
bond type
Definition Bond.hpp:26
@ BD_IN
Definition Bond.hpp:31
@ BD_OUT
Definition Bond.hpp:32
@ BD_NONE
Definition Bond.hpp:30
@ BD_REG
Definition Bond.hpp:29
@ BD_BRA
Definition Bond.hpp:28
@ BD_KET
Definition Bond.hpp:27