Repository navigation
Add row and column functions #1189
Description
Activity
Well, since trailing
allscan be ommitted, row should be less necessary.I agree, that's for completness and also that would be weird to mix
viewandcolin the code instead of usingrowandcol.Hi @JohanMabille , I'm here from https://twitter.com/JohanMabille/status/1052449785301614592
Would love to work on this. Any primers on how to get started on this?Hi @cha-ku and welcome!
Here are the steps I would recommend if you are totally new to
xtensor; if not, you can probably skip some of them:- read the Getting started section and start to play a bit with xtensor. If you're familiar with Numpy, the cheat sheet can be of great help
- The quick ref about views will give you more insights about what views are and how they work.
rowandcolumnfunctions should be free functions inxview.hppthat simply call the view function with the right slices. You should start withrowwhich is pretty straightforward,columnwould require a little more metaprogramming.Reacted by Chaitanya KukdeI assume this issue is still open and I like to contribute to it.
Just to clarify, what should happen if the
roworcolfunction is called on a tensor with more than 2 dimensions. I made a test with your starting point:Typing
row(e, 1)is more convenient and expressive thanxt::view(e, 1, xt::all())and the behavior is quite weird:
TEST(xview, row_on_3dim_array) { xt::xarray<int> arr{ { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } }; const auto row0 = xt::row(arr, 0); const auto row1 = xt::row(arr, 1); std::cout << row0 << std::endl; // prints: {{1, 2}, {3, 4}} std::cout << row1 << std::endl; // prints: {{5, 6}, {7, 8}} std::cout << row0(0) << std::endl; // prints: 1 std::cout << row0(1) << std::endl; // prints: 2 std::cout << row0(2) << std::endl; // prints: 3 std::cout << row0(3) << std::endl; // prints: 4 std::cout << row1(0) << std::endl; // prints: 3 std::cout << row1(1) << std::endl; // prints: 4 std::cout << row1(2) << std::endl; // prints: 5 std::cout << row1(3) << std::endl; // prints: 6 }So the print of
row0androw1seems to be right. Of course we could discuss what a row on a 3-dimensional tensor should be. But at least they access different values.When I access the elements in the rows, something went wrong. I hadn't the time to check it in detail.
I like to use the TDD approach to get the functionrowandcoland therefore I should know what the expected behavior of such code should be.I checked the code and I see now why I have this behavior.
Obviously,xt::view(e, 1, xt::all())will just return axt::viewto a tensor with one dimension less as the tensor has. Therefore, I have to use two indices for accessing the elements in the row of a tensor with 3 dimensions.The question remains the same:
What should be the behavior of callingroworcolon a tensor with more than 2 dimensions?- Should not be possible?
rowis always first dimension,colis always second dimension?rowis always first dimension,colis always last dimension?
Indeed
xt::viewappendsxt::allslices to its arguments list until it matches the number of dimensions of the underlying expression.Regarding the behavior of
rowandcolon a tensor with more than 2 dimensions, This has to be discussed, but I would be in favor of forbidding it for expressions with more than 2 dimensions.Reacted by Nicolas KollerIt seems to me that this issue can be closed. Or am I missing something?
Indeed, good catch!
Row and column shortcuts should be added for building views on 2-dimensional tensors. Typing
row(e, 1)is more convenient and expressive thanxt::view(e, 1, xt::all())