From 3baced9adbf25bedf034eef4fab74a12e32b575a Mon Sep 17 00:00:00 2001 From: Jiri Pittner Date: Thu, 23 Oct 2025 16:15:42 +0200 Subject: [PATCH] const version of loopover and grouploopoevr in tensor class --- tensor.cc | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- tensor.h | 4 +-- 2 files changed, 94 insertions(+), 7 deletions(-) diff --git a/tensor.cc b/tensor.cc index 1314923..16ab17a 100644 --- a/tensor.cc +++ b/tensor.cc @@ -376,6 +376,7 @@ calcsize(); } + template void loopingroups(Tensor &t, int ngroup, int igroup, T **p, SUPERINDEX &I, void (*callback)(const SUPERINDEX &, T *)) { @@ -441,14 +442,78 @@ loopingroups(*this,ss,sh->number-1,&pp,I,callback); } +template +void constloopingroups(const Tensor &t, int ngroup, int igroup, const T **p, SUPERINDEX &I, void (*callback)(const SUPERINDEX &, const T *)) +{ +LA_index istart,iend; +const indexgroup *sh = &t.shape[ngroup]; +switch(sh->symmetry) + { + case 0: + istart= sh->offset; + iend= sh->offset+sh->range-1; + break; + case 1: + istart= sh->offset; + if(igroup==sh->number-1) iend= sh->offset+sh->range-1; + else iend = I[ngroup][igroup+1]; + break; + case -1: + istart= sh->offset + igroup; + if(igroup==sh->number-1) iend= sh->offset+sh->range-1; + else iend = I[ngroup][igroup+1]-1; + break; + } + +for(LA_index i = istart; i<=iend; ++i) + { + I[ngroup][igroup]=i; + if(ngroup==0 && igroup==0) + { + int sign; + //std::cout <<"TEST "< *>(&t.shape))[newngroup]; + newigroup=sh2->number-1; + } + constloopingroups(t,newngroup,newigroup,p,I,callback); + } + } +} + + +template +void Tensor::constloopover(void (*callback)(const SUPERINDEX &, const T *)) const +{ +SUPERINDEX I(shape.size()); +for(int i=0; inumber); + I[i] = sh->offset; + } +const T *pp=&data[0]; +int ss=shape.size()-1; +const indexgroup *sh = &shape[ss]; +constloopingroups(*this,ss,sh->number-1,&pp,I,callback); +} + + static std::ostream *sout; template -static void outputcallback(const SUPERINDEX &I, T *v) +static void outputcallback(const SUPERINDEX &I, const T *v) { //print indices flat for(int i=0; i &x) { s< *>(&x)->loopover(&outputcallback); +x.constloopover(&outputcallback); return s; } @@ -521,6 +586,28 @@ loopovergroups(*this,shape.size()-1,&pp,I,callback); } + + +template +void constloopovergroups(const Tensor &t, int ngroup, const T **p, GROUPINDEX &I, void (*callback)(const GROUPINDEX &, const T *)) +{ +for(LA_largeindex i = 0; i +void Tensor::constgrouploopover(void (*callback)(const GROUPINDEX &, const T *)) const +{ +GROUPINDEX I(shape.size()); +const T *pp= &data[0]; +constloopovergroups(*this,shape.size()-1,&pp,I,callback); +} + + const NRPerm *help_p; template Tensor *help_t; @@ -528,7 +615,7 @@ template const Tensor *help_tt; template -static void permutecallback(const GROUPINDEX &I, T *v) +static void permutecallback(const GROUPINDEX &I, const T *v) { LA_largeindex target=0; for(int i=0; i< help_t->shape.size(); ++i) @@ -553,7 +640,7 @@ help_p = &p; help_t = &r; //now rearrange the data -const_cast *>(this)->grouploopover(permutecallback); +const_cast *>(this)->constgrouploopover(permutecallback); return r; } diff --git a/tensor.h b/tensor.h index 83becb9..f06e6ec 100644 --- a/tensor.h +++ b/tensor.h @@ -49,8 +49,6 @@ //@@@conversions to/from fourindex, optional negarive rande for beta spin handling //@@@ optional distinguish covariant and contravariant check in contraction // -//maybe const loopover and grouploopover to avoid problems with shallowly copied tensors -// //@@@?general permutation of individual indices - check the indices in sym groups remain adjacent, calculate result's shape, loopover the result and permute using unwind_callback // // @@ -224,7 +222,9 @@ public: inline void randomize(const typename LA_traits::normtype &x) {data.randomize(x);}; void loopover(void (*callback)(const SUPERINDEX &, T *)); //loop over all elements + void constloopover(void (*callback)(const SUPERINDEX &, const T *)) const; //loop over all elements void grouploopover(void (*callback)(const GROUPINDEX &, T *)); //loop over all elements disregarding the internal structure of index groups + void constgrouploopover(void (*callback)(const GROUPINDEX &, const T *)) const; //loop over all elements disregarding the internal structure of index groups Tensor permute_index_groups(const NRPerm &p) const; //rearrange the tensor storage permuting index groups as a whole Tensor unwind_index(int group, int index) const; //separate an index from a group and expand it to full range as the least significant one (the leftmost one)