tensor: permute index group to a given index order

This commit is contained in:
2025-11-12 15:28:32 +01:00
parent ee3687ac36
commit d73c539e43
3 changed files with 43 additions and 4 deletions

25
t.cc
View File

@@ -4120,7 +4120,7 @@ Tensor<double> ss(s);
cout <<"Error= "<<ss-xc<<endl; cout <<"Error= "<<ss-xc<<endl;
} }
if(1) if(0)
{ {
int r=1; int r=1;
int n=5; int n=5;
@@ -4140,6 +4140,29 @@ cout <<x*s1<<endl;
cout<<s1*s2<<endl; cout<<s1*s2<<endl;
} }
if(1)
{
int r;
int n;
cin>>r>>n;
NRVec<INDEXGROUP> s(r);
for(int i=0; i<r; ++i)
{
s[i].number=1;
s[i].symmetry=0;
s[i].range=n;
s[i].offset=0;
}
Tensor<double> x(s); x.randomize(1.);
x.defaultnames();
NRVec<INDEXNAME> names(r);
NRPerm<int> p(r); p.randomize();
for(int i=0; i<r; ++i) sprintf(names[i].name,"i%03d",p[i+1]-1);
cout <<"requested index order = "<<names<<endl;
Tensor<double> y = x.permute_index_groups(names);
cout<<"resulting index order = "<<y.names<<endl;
}
}//main }//main

View File

@@ -705,6 +705,23 @@ return r;
} }
template<typename T>
Tensor<T> Tensor<T>::permute_index_groups(const NRVec<INDEXNAME> &names) const
{
NRPerm<int> p(shape.size());
if(names.size()!=shape.size()) laerror("names list does not match shape in permute_index_groups");
for(int i=0; i<names.size(); ++i)
{
INDEX ii=findindex(names[i]);
if(ii.index>0) laerror("indices inside groups cannot be permuted in permute_index_groups, define permutation by first group's index name or flatten the tensor first");
p[1+i] = 1+ii.group;
}
if(!p.is_valid()) laerror("illegal permutation from names in permute_index_groups, perhaps repeated or not unique names?");
return permute_index_groups(p);
}
FLATINDEX superindex2flat(const SUPERINDEX &I) FLATINDEX superindex2flat(const SUPERINDEX &I)
{ {
int rank=0; int rank=0;

View File

@@ -40,8 +40,6 @@
#include "miscfunc.h" #include "miscfunc.h"
//TODO: //TODO:
//@@@?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
//@@@ is that needed? we can flatten the relevant groups and permute index groups alternatively - maybe implement on high level this way for convenience
//?maybe optional negative range for beta spin handling in some cases of fourindex-tensor conversions combining AA and AB etc. cases - if needed //?maybe optional negative range for beta spin handling in some cases of fourindex-tensor conversions combining AA and AB etc. cases - if needed
@@ -299,7 +297,8 @@ public:
void grouploopover(void (*callback)(const GROUPINDEX &, T *)); //loop over all elements disregarding the internal structure of index groups 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 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<int> &p) const; //rearrange the tensor storage permuting index groups as a whole Tensor permute_index_groups(const NRPerm<int> &p) const; //rearrange the tensor storage permuting index groups as a whole: result_i = source_p_i
Tensor permute_index_groups(const NRVec<INDEXNAME> &names) const; //permute to requested order of group's first indices (or permute individual indices of a flat tensor)
Tensor unwind_index_group(int group) const; //make the index group leftmost (least significant) Tensor unwind_index_group(int group) const; //make the index group leftmost (least significant)
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) 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)