etaler/Etaler

Providing operator [] for et::Tensor (or at least a better way to handle tensor subscription)

marty1885 opened this issue · 2 comments

Numpy's way of doing ND array subscription is widely accepted and straight forward.

ex:

>>> a = np.zeros(4,4)
>>> a[:2, :2]

Due to the fact that C++'s operator [] can only take one argument. I ended up implementing a view() method to perform subscription.

ex:

[cling]$ auto a = ones({4,4})
[cling]$ a.view({range(2), range(2)})

But TBH, it gets annoying when I start to write more code with it. It would be great to have a [] equivalent in Etaler. We have a few solution.

  1. make Tensor Tensor::operator [] (svector<Range>)

Which will allow us to do

[cling]$ auto a = ones({4,4})
[cling]$ a[{range(2), range(2)})]

There is an extra bracket around the parameters.

  1. make Tensor Tensor::operator [] (svector<Range>) and overload Range::operator , ()

Which allows us to

[cling]$ auto a = ones({4,4})
[cling]$ a[range(2), range(2))]

It gives us the syntax we want. But messes with how C++ evaluates values.

  1. Use operator ()

This is ArrayFire's solution. It feels weird to subscript using () instead of [].

[cling]$ auto a = ones({4,4})
[cling]$ a(range(2), range(2)))

It feels like calling a function....

Any ideas?

I'm voting for option 1 :)

@alior101 should be implemented now :)