# accessing maximal tensor element and scalings

+1 vote

Hi all,

I am currently implementing a CTM method for iPEPS in ITensor. For this, and similar algorithms, I want to absorb a big bunch of tensors and truncate the result in order to approximate the environment.

For numerical stability, one way to do this is by dividing the updated tensors by its norm or maximal element etc in order to not diverge to infinity or zero. However, for some purpose it might be good to have the tensor not divided by its norm. To stabilize this, I would expect the scaleTo( ) method to do the job. Playing around with it naively, however,
tensor.scaleTo(norm(tensor))
does only work every now and then. I am not yet sure why that is, but also have not yet got through the whole details behind this method. Moreover, I have the feeling that probably using something like
tensor.scaleTo(maxElm(tensor))
should work more accurately. Therefore: is there some method that finds the maximal (in absolute value) element within the tensor data?

Best and thanks a lot!
Alex :)

+1 vote

Hi Alex,
Good question. So there isn't a method that specifically gets the maximal element of an ITensor or IQTensor. However, there is a facility that lets you write your own such method pretty easily. The key is to use the ITensor::visit (or IQTensor::visit) method. You pass this method any function which takes either a Real or Cplx argument (or both) and it plugs each element of the tensor into the function you provide. If the function has internal state (like if it holds a reference to an outside variable) you can use this to compute a max.

Here's an example:

auto T = ITensor(i,j,k);
randomize(T);

Real max = -1E12.; //use std::limits double min if you really want to be thorough

auto getMax = [&max](Real x)
{
if(x > max) max = x;
};

T.visit(getMax);

Print(max);