romeric / Fastor

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

Investigate overloading operator() with std::initializer_list #2

Open romeric opened 7 years ago

romeric commented 7 years ago

The operator() for tensors could be overloaded to provide a similar functionality to seq, fseq and iseq, like so

operator()(std::initializer_list<int> _s1, std::initializer_list<int> _s2);

The tensor could then be indexed like

Tensor<double,4,4> A;
A({0,3},{1,3,2}) = 42.;

This would be pretty easy to achieve by just providing a std::initializer_list constructor to seq. Note that std::initializer_list constructors themselves are constexpr.

This should however, be investigated, as at the moment Tensor class also has std::initializer_list constructor and the above syntax already works but gives incorrect results and also this syntax seems more appropriate for TensorRandomViews than TensorViews. In the sense, that it alludes to slicing a tensor with two vectors rather than with sequentially ascending ranges.

romeric commented 7 years ago

901f471 fixes the bug part of this issue.

pauljurczak commented 4 years ago

This would also allow for code like this:

  Tensor<int,9> v{0, 1, 2, 3, 4, 5, 6, 7, 8};
  v({6, 3, 0}) = v({1, 2, 3});

which is possible with Eigen3. Right now I have to define intermediate tensors to accomplish this:

  Tensor<int,9> v{0, 1, 2, 3, 4, 5, 6, 7, 8};
  Tensor<int,3> i{6, 3, 0};
  Tensor<int,3> j{1, 2, 3};
  v(i) = v(j);