Fastor icon indicating copy to clipboard operation
Fastor copied to clipboard

Another Operation on Tensor View Bug

Open alex-fu27 opened this issue 3 years ago • 1 comments

I believe I have a similar bug like #140, but for me it fails at compile time:

Tensor<int, 4, 4> L {
  {1, 0, 0, 0},
  {1, 1, 0, 0},
  {0, 1, 1, 0},
  {0, 0, 1, 1}
};
Tensor<int, 4> x {1, 2, 3, 4};

// works
Tensor<int, 4> g = x;
g += 2 * L(0, all);

// fails
Tensor<int, 4> g = x + 2 * L(0, all);

fails with something like:

In file included from lib/Fastor/Fastor/expressions/expressions.h:4,
                 from lib/Fastor/Fastor/Fastor.h:28,
                 from test/desktop/math.cpp:1:
lib/Fastor/Fastor/expressions/binary_ops/binary_arithmetic_ops.h: In instantiation of 'Fastor::BinaryAddOp<TLhs, TRhs, DIM0>::scalar_type Fastor::BinaryAddOp<TLhs, TRhs, DIM0>::thelper_s(const std::array<int, DIM0>&) const [with LExpr = Fastor::Tensor<int, 4>; RExpr = Fastor::BinaryMulOp<int, Fastor::TensorViewExpr<Fastor::Tensor<int, 4, 4>, 2>, 2>; U = int; typename std::enable_if<((! is_primitive_v_<LExpr>) && (! is_primitive_v_<RExpr>)), bool>::type <anonymous> = false; TLhs = Fastor::Tensor<int, 4>; TRhs = Fastor::BinaryMulOp<int, Fastor::TensorViewExpr<Fastor::Tensor<int, 4, 4>, 2>, 2>; long unsigned int DIM0 = 1; Fastor::BinaryAddOp<TLhs, TRhs, DIM0>::scalar_type = int]':
lib/Fastor/Fastor/expressions/binary_ops/binary_arithmetic_ops.h:215:1:   required from 'Fastor::BinaryAddOp<TLhs, TRhs, DIM0>::scalar_type Fastor::BinaryAddOp<TLhs, TRhs, DIM0>::teval_s(const std::array<int, DIM0>&) const [with U = int; TLhs = Fastor::Tensor<int, 4>; TRhs = Fastor::BinaryMulOp<int, Fastor::TensorViewExpr<Fastor::Tensor<int, 4, 4>, 2>, 2>; long unsigned int DIM0 = 1; Fastor::BinaryAddOp<TLhs, TRhs, DIM0>::scalar_type = int]'
lib/Fastor/Fastor/tensor/SpecialisedConstructors.h:324:49:   required from 'Fastor::Tensor<T, Rest>::Tensor(const Fastor::AbstractTensor<Derived, DIMS>&) [with Derived = Fastor::BinaryAddOp<Fastor::Tensor<int, 4>, Fastor::BinaryMulOp<int, Fastor::TensorViewExpr<Fastor::Tensor<int, 4, 4>, 2>, 2>, 1>; long unsigned int DIMS = 1; typename std::enable_if<(((has_tensor_view_v<Derived> && (DIMS != 2)) && (DIMS == sizeof... (Rest))) && (! requires_evaluation_v<Derived>)), bool>::type <anonymous> = false; T = int; long unsigned int ...Rest = {4}]'
test/desktop/math.cpp:39:37:   required from here
lib/Fastor/Fastor/expressions/binary_ops/binary_arithmetic_ops.h:172:88: error: cannot convert 'const std::array<int, 1>' to 'const std::array<int, 2>&'
  172 |         return _lhs.template teval_s<EVAL_TYPE>(as) OP _rhs.template teval_s<EVAL_TYPE>(as);\
      |                                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
lib/Fastor/Fastor/expressions/binary_ops/binary_arithmetic_ops.h:215:1: note: in expansion of macro 'FASTOR_MAKE_BINARY_ARITHMETIC_OPS'
  215 | FASTOR_MAKE_BINARY_ARITHMETIC_OPS(+, Add, scalar_type)
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib/Fastor/Fastor/expressions/binary_ops/binary_arithmetic_ops.h:165:65: note:   initializing argument 1 of 'Fastor::BinaryMulOp<TLhs, TRhs, DIM0>::scalar_type Fastor::BinaryMulOp<TLhs, TRhs, DIM0>::teval_s(const std::array<int, DIM0>&) const [with U = int; TLhs = int; TRhs = Fastor::TensorViewExpr<Fastor::Tensor<int, 4, 4>, 2>; long unsigned int DIM0 = 2; Fastor::BinaryMulOp<TLhs, TRhs, DIM0>::scalar_type = int]'
  165 |     FASTOR_INLINE EVAL_TYPE teval_s(const std::array<int,DIM0> &as) const {\
      |                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
lib/Fastor/Fastor/expressions/binary_ops/binary_arithmetic_ops.h:217:1: note: in expansion of macro 'FASTOR_MAKE_BINARY_ARITHMETIC_OPS'
  217 | FASTOR_MAKE_BINARY_ARITHMETIC_OPS(*, Mul, scalar_type)
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*** [.pio/build/native/test/desktop/math.o] Error 1

( compiling in platformio, gcc, std=gnu++17 )

alex-fu27 avatar Apr 27 '22 09:04 alex-fu27

In general, this is a limitation unfortunately.

The first example works but it really shouldn't. That is because the right hand side creates a dynamic tensor L(0, all) whose size can only be determined during runtime. Assigning that to a static size tensor should not work.

Of course there is a solution to this depending on the context that is, if the bounds of your views are known at compile time then you can do:

Tensor<int, 4> g = x + 2 * L(fix<0>, fall);

The above should create a static view that should bind to the tensor.

romeric avatar Apr 28 '22 14:04 romeric

I have created a specific issue to track the development of this. Closing this one since a solution already exists and was provided to the user.

romeric avatar Mar 20 '24 06:03 romeric