-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Investigate unravel_indices()
/ ravel_indices()
functions
#187
Comments
As you commented in #77 (comment)
And the companion unravel would be:
|
Some thougths about indexing: Let X be an (a)rray and dim(x)=c(d1,...,dn) To subset it, there can be just one parameter, called positions as it refers to flat position in the data. So FUN(X, pos) can be: And FUN(X,I1,...,In) can be: The last one, numpy array indexing, is quite powerfull and seems to be able to emulate any of the others choosing appropiate arrays (achieves crossproducts because of broadcasting rules) Rray doesn't have this numpy function, and perhaps it shouldn't (I find it very difficult to think of cases of actual use of it). library(rray)
library(purrr)
rray_i2p <- function(mat,dim){
stopifnot(dim >= apply(mat,2,max))
if(ncol(mat) == 1) as.vector(mat) else
mat[,1]+dim[[1]]*(rray_i2p(mat[,2:ncol(mat),drop = FALSE], dim[-1])-1)
}
### # rray Super SubSet
rray_sss <-function(x, ...){
library(purrr)
indexList <- list(...)
commonDim <- rray_dim_common(...)
homogeneousList <- map(indexList, function(ind) rray_broadcast(ind,commonDim))
lengths <- map(homogeneousList, length)
flattenList <- map2(homogeneousList, lengths, rray_reshape)
coordMatrix <- as.matrix( reduce(flattenList, rray_cbind) )
positions <- rray_i2p(coordMatrix, dim(x))
flatResult<- rray_yank(x, positions)
finalResult <- rray_reshape(flatResult,commonDim )
finalResult
}
rr <- rray(1:36, c(6,3,2))
rray_sss(rr,1,1,2)
#> <rray<int>[1]>
#> [1] 19
rray_sss(rr, rray(1:6,c(2,3)),1,2)
#> <rray<int>[,3][6]>
#> [,1] [,2] [,3]
#> [1,] 19 21 23
#> [2,] 20 22 24
rray_sss(rr, rray(1:6,c(2,3)),rray(1:2,c(1,1,2)),2)
#> <rray<int>[,3,2][12]>
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] 19 21 23
#> [2,] 20 22 24
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] 25 27 29
#> [2,] 26 28 30 Created on 2019-05-26 by the reprex package (v0.2.1) |
https://xtensor.readthedocs.io/en/latest/indices.html?highlight=unravel_indices
The text was updated successfully, but these errors were encountered: