Skip to content

Add row and column functions #1189

Description

@JohanMabille

Row and column shortcuts should be added for building views on 2-dimensional tensors. Typing row(e, 1) is more convenient and expressive than xt::view(e, 1, xt::all())

Activity

  1. changed the title [-]Add row and column[/-] [+]Add row and column functions[/+] on Oct 18, 2018
  2. SylvainCorlay commented on Oct 18, 2018

    @SylvainCorlay
    Member

    Well, since trailing alls can be ommitted, row should be less necessary.

  3. JohanMabille commented on Oct 18, 2018

    @JohanMabille
    MemberAuthor

    I agree, that's for completness and also that would be weird to mix view and col in the code instead of using row and col.

  4. cha-ku commented on May 17, 2019

    @cha-ku

    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?

  5. JohanMabille commented on May 17, 2019

    @JohanMabille
    MemberAuthor

    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.

    row and column functions should be free functions in xview.hpp that simply call the view function with the right slices. You should start with row which is pretty straightforward, column would require a little more metaprogramming.

  6. kolibri91 commented on Dec 27, 2019

    @kolibri91
    Contributor

    I assume this issue is still open and I like to contribute to it.

    Just to clarify, what should happen if the row or col function 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 than xt::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 row0 and row1 seems 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 function row and col and therefore I should know what the expected behavior of such code should be.

  7. kolibri91 commented on Dec 27, 2019

    @kolibri91
    Contributor

    I checked the code and I see now why I have this behavior.
    Obviously, xt::view(e, 1, xt::all()) will just return a xt::view to 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 calling row or col on a tensor with more than 2 dimensions?

    • Should not be possible?
    • row is always first dimension, col is always second dimension?
    • row is always first dimension, col is always last dimension?
  8. JohanMabille commented on Dec 28, 2019

    @JohanMabille
    MemberAuthor

    Indeed xt::view appends xt::all slices to its arguments list until it matches the number of dimensions of the underlying expression.

    Regarding the behavior of row and col on 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.

  9. KappaDistributive commented on Jul 4, 2020

    @KappaDistributive

    It seems to me that this issue can be closed. Or am I missing something?

  10. JohanMabille commented on Jul 5, 2020

    @JohanMabille
    MemberAuthor

    Indeed, good catch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions