tensor: index name preservation in tucker

This commit is contained in:
2025-11-20 16:41:57 +01:00
parent e867a7a8c9
commit d136c2314d
2 changed files with 18 additions and 0 deletions

1
t.cc
View File

@@ -4440,6 +4440,7 @@ for(int i=0; i<r; ++i)
} }
Tensor<double> x(shape); Tensor<double> x(shape);
x.randomize(1.); x.randomize(1.);
x.defaultnames();
cout<<x; cout<<x;
Tensor<double> x0(x); Tensor<double> x0(x);
x0.copyonwrite(); x0.copyonwrite();

View File

@@ -1723,6 +1723,7 @@ if(r==1) //create an analogous output for the trivial case
//loop over all indices; relies on the fact that unwinding does not change order of remaining indices //loop over all indices; relies on the fact that unwinding does not change order of remaining indices
//for inverseorder=false, to avoid inverting order by permute_index_groups we repeatedly unwind the LAST index, and all indices rotate at this position with the first one in the last iteration due to the unwind! //for inverseorder=false, to avoid inverting order by permute_index_groups we repeatedly unwind the LAST index, and all indices rotate at this position with the first one in the last iteration due to the unwind!
NRVec<INDEXNAME> names_saved = names;
for(int i=0; i<r; ++i) for(int i=0; i<r; ++i)
{ {
INDEX I= inverseorder? indexposition(i,shape) : indexposition(r-1,shape); INDEX I= inverseorder? indexposition(i,shape) : indexposition(r-1,shape);
@@ -1765,9 +1766,17 @@ for(int i=0; i<r; ++i)
NRVec<T> newdata(umnew); NRVec<T> newdata(umnew);
umnew.resize(0,0);//deallocate umnew.resize(0,0);//deallocate
*this = Tensor(ushape,newdata); *this = Tensor(ushape,newdata);
names=names_saved;
} }
} }
if(!is_flat()) laerror("this should not happen"); if(!is_flat()) laerror("this should not happen");
if(inverseorder)
{
NRPerm<int> p(rank()); p.identity();
names=names_saved.permuted(p.reverse());
}
else
names=names_saved;
return ret; return ret;
} }
@@ -1776,6 +1785,7 @@ template<typename T>
Tensor<T> Tensor<T>::inverseTucker(const NRVec<NRMat<T> > &x, bool inverseorder) const Tensor<T> Tensor<T>::inverseTucker(const NRVec<NRMat<T> > &x, bool inverseorder) const
{ {
if(rank()!=x.size()) laerror("input of inverseTucker does not match rank"); if(rank()!=x.size()) laerror("input of inverseTucker does not match rank");
NRVec<INDEXNAME> names_saved = names;
Tensor<T> tmp(*this); Tensor<T> tmp(*this);
Tensor<T> r; Tensor<T> r;
if(!is_flat()) laerror("inverseTucker only for flat tensors as produced by Tucker"); if(!is_flat()) laerror("inverseTucker only for flat tensors as produced by Tucker");
@@ -1805,6 +1815,13 @@ for(int i=rank()-1; i>=0; --i)
} }
} }
} }
if(inverseorder)
{
NRPerm<int> p(rank()); p.identity();
r.names=names_saved.permuted(p.reverse());
}
else
r.names=names_saved;
return r; return r;
} }