+1 vote
asked by (760 points)

I wonder if there is a way to distinguish whether an ITensor contains real or complex elements in compiling. I found that I often get a problem of whether to use elt or eltC. For example if I make a function to print the value of a zero-dimension tensor, I will need to use either one of them

void printTensor (ITensor T)
    cout << elt(T) << endl;
    // cout << eltC(T) << endl;

and will get an compiling error if I use the wrong one. If would be useful if the type (real or complex) can be automatically determined. I am thinking about something like

void printTensor (ITensor T)
    constexpr bool is_real = T.something();
    if constexpr (is_real)
        cout << elt(T) << endl;
        cout << eltC(T) << endl;

Do you think this is possible?

1 Answer

+1 vote
answered by (70.1k points)

Hi Chiamin,
It's a good question but no, there is not a way at compile time to determine the element type, since it is handled dynamically in the way ITensor works.

What I would recommend doing inside your printing function is getting the element using eltC, which will always succeed. Then you can either print that number (call it "z") and just observe in the print out that it has a zero imaginary part, or you can use the functions std::imag(z) and std::real(z) to see if the absolute value of imaginary part is zero (or below some small threshold) and if so, only print the real part.

If you want to do the same for a more general kind of tensor, note that there are functions isReal(T) and isComplex(T) you can call on an ITensor T to see if it has purely real elements or has complex elements: http://itensor.org/docs.cgi?vers=cppv3&page=classes/itensor


commented by (760 points)
It is good to know that eltC will always work. Never noticed that. Thanks for the answer!
Welcome to ITensor Support Q&A, where you can ask questions and receive answers from other members of the community.

Formatting Tips:
  • To format code, indent by four spaces
  • To format inline LaTeX, surround it by @@ on both sides
  • To format LaTeX on its own line, surround it by $$ above and below
  • For LaTeX, it may be necessary to backslash-escape underscore characters to obtain proper formatting. So for example writing \sum\_i to represent a sum over i.
If you cannot register due to firewall issues (e.g. you cannot see the capcha box) please email Miles Stoudenmire to ask for an account.

To report ITensor bugs, please use the issue tracker.