romeric / Fastor

A lightweight high performance tensor algebra framework for modern C++
MIT License
752 stars 70 forks source link

Comparison operator and return type #137

Closed Bbllaaddee closed 3 years ago

Bbllaaddee commented 3 years ago

Hey there!

I am wondering about return type of comparison operator for two Fastor objects and its ability to be converted to bool.

Here are two little programs:

int main()
{
    Tensor<double> a = 1;
    Tensor<double> b = 2;
    std::cout << (a<b) << std::endl;
}

This one outputs 1 and two (!) newline characters. In other words, (a<b) in output already has a newline character (which is a little strange, I suppose).

May be that's a reason this expression cannot be implicitly (or explicitly, if you try to) converted to bool type, and the following program doesn't compile:

int main()
{
    Tensor<double> a = 1;
    Tensor<double> b = 2;
    std::vector vec = {a,b};
    std::sort(vec.begin(), vec.end());
}

So, I wanted to address this in this issue. Should it probably be fixed, or there's a specific idea behind it?

Cheers)

romeric commented 3 years ago

The return type of a<b is not a scalar but rather a non-evaluated tensor expression (BinaryCmpOp). Making Fastor tensors work with C++ iterators would require a good amount of work.

This certainly needs to be fixed. I will see what I can do. Thanks for reporting.

Bbllaaddee commented 3 years ago

Yeah, std::sort() might have been an overkill for the purpose of this example, probably just if(a<b) { /*do something*/ } is simpler. Thank you!)

romeric commented 3 years ago

For your simpler case you can use any_of, all_of or none_of based on your need. if (any_of(a<b)) /* do something*/.