From c7c21ee1a8b3ed654049103bccfc12305f371765 Mon Sep 17 00:00:00 2001 From: Jiri Pittner Date: Thu, 13 Nov 2025 17:57:26 +0100 Subject: [PATCH] tensor: name treatment in apply_permutation_algebra --- t.cc | 4 ++-- tensor.cc | 54 +++++++++++++++++++++++++++++++++++++++++++++--------- tensor.h | 2 ++ 3 files changed, 49 insertions(+), 11 deletions(-) diff --git a/t.cc b/t.cc index dcbbd8d..78e50db 100644 --- a/t.cc +++ b/t.cc @@ -3568,7 +3568,7 @@ v.printsorted(cout,1,false); } -if(0) +if(1) { //grassmann product of n identical rank=2 tensors in m-dim space int n,m; @@ -4140,7 +4140,7 @@ cout <(nn,mm,kk,&data[0],&u.data[0], &rhsu.data[0],alpha,beta,conjugate); } +template +bool Tensor::is_uniquely_named() const +{ +if(!is_named()) return false; +bool r=true; +for(int i=1; i void Tensor::addcontractions(const Tensor &rhs1, const INDEXLIST &il1, const Tensor &rhs2, const INDEXLIST &il2, T alpha, T beta, bool doresize, bool conjugate1, bool conjugate2) @@ -1406,14 +1415,14 @@ FLATINDEX J = superindex2flat(I); for(int p=0; p->size(); ++p) { FLATINDEX Jp = J.permuted((*help_pa)[p].perm,help_inverse); - *v += help_alpha * (*help_pa)[p].weight * (*help_t)(Jp); + *v += help_alpha * (*help_pa)[p].weight * (*help_tt)(Jp); } } static int help_tn; template -static Tensor *help_tv; +static const Tensor *help_tv; template static void permutationalgebra_callback2(const SUPERINDEX &I, T *v) @@ -1447,7 +1456,18 @@ copyonwrite(); if(rank()!=rhs.rank()) laerror("rank mismatch in apply_permutation_algebra"); -help_t = const_cast *>(&rhs); +if(rhs.is_named()) + { + NRVec namperm = rhs.names.permuted(pa[0].perm,inverse); + if(is_named()) + { + if(names!=namperm) laerror("inconsistent index names in apply_permutation_algebra"); + } + else + names=namperm; + } + +help_tt = &rhs; help_pa = &pa; help_inverse = inverse; help_alpha = alpha; @@ -1466,7 +1486,22 @@ int totrank=0; for(int i=0; i = const_cast *>(&rhsvec[0]); +bool allnamed=true; +for(int i=0; i allrhsnames=rhsvec[0].names; + for(int i=1; i namperm = allrhsnames.permuted(pa[0].perm,inverse); + if(is_named()) + { + if(names!=namperm) laerror("inconsistent index names in apply_permutation_algebra"); + } + else + names=namperm; + } + +help_tv = &rhsvec[0]; help_tn = rhsvec.size(); help_pa = &pa; help_inverse = inverse; @@ -1512,9 +1547,10 @@ if(data.size()!=newsize) laerror("internal error in split_index_group"); template void Tensor::split_index_group1(int group) { +const INDEXGROUP *sh = &(* const_cast *>(&shape))[0]; if(group<0||group >= shape.size()) laerror("illegal index group number"); -if(shape[group].number==1) return; //nothing to split -if(shape[group].symmetry!=0) laerror("only non-symmetric index group can be splitted, use flatten instead"); +if(sh[group].number==1) return; //nothing to split +if(sh[group].symmetry!=0) laerror("only non-symmetric index group can be splitted, use flatten instead"); NRVec newshape(shape.size()+1); int gg=0; @@ -1522,15 +1558,15 @@ for(int g=0; g matrix() const {return NRMat(data,data.size()/groupsizes[0],groupsizes[0],0);}; //reinterpret as matrix with column index being the tensor's leftmost index group (typically the unwound single index) bool is_named() const {if(names.size()==0) return false; if(names.size()!=myrank) laerror("bad number of index names"); return true;}; + bool is_uniquely_named() const; //no repeated names bool is_flat() const {for(int i=0; i1) return false; return true;}; bool is_compressed() const {for(int i=0; i1&&shape[i].symmetry!=0) return true; return false;}; bool has_symmetry() const {for(int i=0; i findindexlist(const NRVec &names) const; + void renameindex(const INDEXNAME namfrom, const INDEXNAME nameto) {int i=findflatindex(namfrom); names[i]=nameto;}; inline Tensor& operator+=(const Tensor &rhs) {