In the following function using xtensor, I was expecting the compiler to express the computation graph to auto z = 3.0 * u + 100 * xt::cos(u); and evaluate the result at xt::xtensor_fixed<T, xt::xshape<nn>> out = z; when assigning the expression to an exact tensor.
template<typename T, size_t nn>
T func_cos(xt::xtensor_fixed<T, xt::xshape<nn>> &u) {
auto z = 3.0 * u;
for (auto k = 0; k < 100; ++k){
z += xt::cos(u);
}
xt::xtensor_fixed<T, xt::xshape<nn>> out = z;
return out(0);
}
However, I got an error
views_xtensor.cpp:76:11: error: no viable overloaded '+='
z += xt::cos(u);
[....]
Did I use auto in an wrong way? How could I use lazy evaluation in a for-loop?
Thank you for your help!
When you write
The type of
zencodes the operation, it is something likeThis class does not hold any value, so it is not possible to assign it anything (even if it is a computed assignment). Now even if iw was possible, consider the following expression
The type of this expression is something like
So when you write
You try to assign
zsomething of a completely different type.Lazy evaluation cannot be used in for loops for these reasons.