Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let xt::diagonal() return results in a column major fashion #1570

Open
DavisVaughan opened this issue May 8, 2019 · 0 comments
Open

Let xt::diagonal() return results in a column major fashion #1570

DavisVaughan opened this issue May 8, 2019 · 0 comments

Comments

@DavisVaughan
Copy link
Contributor

I think xt::diagonal() (and the rest of the builders, like xt::eye()), need a layout argument somehow.

Consider this example:

// [[Rcpp::depends(xtensor)]]
// [[Rcpp::plugins(cpp14)]]

#include <xtensor-r/rarray.hpp>
#include <xtensor/xbuilder.hpp>

// [[Rcpp::export]]
xt::rarray<int> diagonal_test(const xt::rarray<int>& x) {
  return xt::diagonal(x);
}
Rcpp::sourceCpp("~/Desktop/test.cpp")

x <- matrix(1:9, 3)
x
#>      [,1] [,2] [,3]
#> [1,]    1    4    7
#> [2,]    2    5    8
#> [3,]    3    6    9

# all good
diagonal_test(x)
#> [1] 1 5 9

y <- array(1:12, c(2, 2, 3))
y
#> , , 1
#> 
#>      [,1] [,2]
#> [1,]    1    3
#> [2,]    2    4
#> 
#> , , 2
#> 
#>      [,1] [,2]
#> [1,]    5    7
#> [2,]    6    8
#> 
#> , , 3
#> 
#>      [,1] [,2]
#> [1,]    9   11
#> [2,]   10   12

# Wrong!
diagonal_test(y)
#>      [,1] [,2]
#> [1,]    1    4
#> [2,]    5    8
#> [3,]    9   12

# I expect
matrix(c(1, 4, 5, 8, 9, 12), nrow = 2)
#>      [,1] [,2] [,3]
#> [1,]    1    5    9
#> [2,]    4    8   12

Hopefully you can see here that with the 3D array, two things are wrong:

  • The shape of the container is wrong because the new dimension is added to the RHS, rather than the LHS
  • Results are filled in in a row major fashion, when I wanted them to be filled in a column major fashion

I think that xtensor-stack/xtensor-r#90 might be a similar issue.

@DavisVaughan DavisVaughan mentioned this issue May 8, 2019
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant