kuwisdelu / matter

Out-of-core statistical computing and signal processing
http://www.cardinalmsi.org
54 stars 4 forks source link

Small inconsistency between `[<-` methods for matter_arr and array #6

Closed PeteHaitch closed 5 years ago

PeteHaitch commented 6 years ago

Hi Kylie,

The [<- replacement method seems to work the same for base::matrix and _mattermat objects, but not so for base::array and _matterarr objects. An example is shown below. Is this fixable? I hit this when using programatically constructed indices passed to the [<- method for the _matterarr class via a do.call().

suppressPackageStartupMessages(library(matter))

m <- matrix(1:6, nrow = 2, ncol = 3)
M <- matter_mat(m)
M[]
#>      [,1] [,2] [,3]
#> [1,]    1    3    5
#> [2,]    2    4    6
# works
M[] <- matrix(6:1, nrow = 2, ncol = 3) 
M[]
#>      [,1] [,2] [,3]
#> [1,]    6    4    2
#> [2,]    5    3    1
# works
M[, ] <- matrix(-(1:6), nrow = 2, ncol = 3)
M[]
#>      [,1] [,2] [,3]
#> [1,]   -1   -3   -5
#> [2,]   -2   -4   -6

a <- array(1:8, dim = c(2, 2, 2))
A <- matter_arr(a)
A[]
#> , , 1
#> 
#>      [,1] [,2]
#> [1,]    1    3
#> [2,]    2    4
#> 
#> , , 2
#> 
#>      [,1] [,2]
#> [1,]    5    7
#> [2,]    6    8
# works
A[] <- array(8:1, dim = c(2, 2, 2))
A[]
#> , , 1
#> 
#>      [,1] [,2]
#> [1,]    8    6
#> [2,]    7    5
#> 
#> , , 2
#> 
#>      [,1] [,2]
#> [1,]    4    2
#> [2,]    3    1
# errors for matter_arr (whereas works for base::array)
A[, , ] <- array(-(1:8), dim = c(2, 2, 2))
#> Error in `[<-`(`*tmp*`, , , , value = structure(-1:-8, .Dim = c(2L, 2L, : argument is missing, with no default
a[, , ] <- array(-(1:8), dim = c(2, 2, 2))
a
#> , , 1
#> 
#>      [,1] [,2]
#> [1,]   -1   -3
#> [2,]   -2   -4
#> 
#> , , 2
#> 
#>      [,1] [,2]
#> [1,]   -5   -7
#> [2,]   -6   -8
PeteHaitch commented 6 years ago

Another small inconsistency: the [<- method for base::array supports NULL indices but the method for matter does not:

suppressPackageStartupMessages(library(matter))

array <- array(1:8, dim = c(2, 2, 2))
matter <- matter_arr(array, dim = dim(array))

# base: Returns empty array of appropriate dimension
array[NULL, NULL, NULL]
#> <0 x 0 x 0 array of integer>
#> 
# matter: errors
matter[NULL, NULL, NULL]
#> Error in matter[NULL, NULL, NULL]: incorrect number of dimensions

# base: Returns empty array of appropriate dimension
array[NULL, NULL, 1]
#> <0 x 0 matrix>
# matter: errors
matter[NULL, NULL, 1]
#> Error in matter[NULL, NULL, 1]: incorrect number of dimensions

# base: Returns empty array of appropriate dimension
array[NULL, , NULL]
#> <0 x 2 x 0 array of integer>
#>      [,1] [,2]
# matter: errors
matter[NULL, , NULL]
#> Error in FUN(if (length(d.call) < 2L) newX[, 1] else array(newX[, 1L], : subscript out of bounds
Session info ``` r devtools::session_info() #> Session info ------------------------------------------------------------- #> setting value #> version R Under development (unstable) (2017-12-12 r73903) #> system x86_64, darwin15.6.0 #> ui X11 #> language (EN) #> collate en_AU.UTF-8 #> tz Australia/Melbourne #> date 2017-12-27 #> Packages ----------------------------------------------------------------- #> package * version date source #> backports 1.1.2 2017-12-13 CRAN (R 3.5.0) #> base * 3.5.0 2017-12-13 local #> biglm * 0.9-1 2013-05-16 CRAN (R 3.5.0) #> BiocGenerics 0.25.1 2017-12-17 Bioconductor #> compiler 3.5.0 2017-12-13 local #> datasets * 3.5.0 2017-12-13 local #> DBI * 0.7 2017-06-18 CRAN (R 3.5.0) #> devtools 1.13.4 2017-11-09 CRAN (R 3.5.0) #> digest 0.6.13 2017-12-14 CRAN (R 3.5.0) #> evaluate 0.10.1 2017-06-24 CRAN (R 3.5.0) #> graphics * 3.5.0 2017-12-13 local #> grDevices * 3.5.0 2017-12-13 local #> grid 3.5.0 2017-12-13 local #> htmltools 0.3.6 2017-04-28 CRAN (R 3.5.0) #> irlba 2.3.1 2017-10-18 CRAN (R 3.5.0) #> knitr 1.17 2017-08-10 CRAN (R 3.5.0) #> lattice 0.20-35 2017-03-25 CRAN (R 3.5.0) #> magrittr 1.5 2014-11-22 CRAN (R 3.5.0) #> Matrix 1.2-12 2017-11-20 CRAN (R 3.5.0) #> matter * 1.5.4 2017-12-04 Bioconductor #> memoise 1.1.0 2017-04-21 CRAN (R 3.5.0) #> methods * 3.5.0 2017-12-13 local #> parallel 3.5.0 2017-12-13 local #> Rcpp 0.12.14 2017-11-23 CRAN (R 3.5.0) #> rmarkdown 1.8 2017-11-17 CRAN (R 3.5.0) #> rprojroot 1.3-1 2017-12-18 CRAN (R 3.5.0) #> stats * 3.5.0 2017-12-13 local #> stringi 1.1.6 2017-11-17 CRAN (R 3.5.0) #> stringr 1.2.0 2017-02-18 CRAN (R 3.5.0) #> tools 3.5.0 2017-12-13 local #> utils * 3.5.0 2017-12-13 local #> withr 2.1.1 2017-12-19 CRAN (R 3.5.0) #> yaml 2.1.16 2017-12-12 CRAN (R 3.5.0) ```
PeteHaitch commented 6 years ago

Sorry, another corner case: the [<- method for base::array supports integer(0) indices but the method for matter does not:

suppressPackageStartupMessages(library(matter))

array <- array(1:8, dim = c(2, 2, 2))
matter <- matter_arr(array)
# base: Returns an array of appropriate dimension
array[integer(0), integer(0), integer(0)]
#> <0 x 0 x 0 array of integer>
#> 
# matter: errors
matter[integer(0), integer(0), integer()]
#> Error in FUN(if (length(d.call) < 2L) newX[, 1] else array(newX[, 1L], : subscript out of bounds

# base: Returns an array (I don't quite understand the dimensions)
array[integer(0), 1, integer(0)]
#> <0 x 0 matrix>
# matter: errors
matter[1, 1, integer(0)]
#> Error in FUN(if (length(d.call) < 2L) newX[, 1] else array(newX[, 1L], : subscript out of bounds
Session info ``` r devtools::session_info() #> Session info ------------------------------------------------------------- #> setting value #> version R Under development (unstable) (2017-12-12 r73903) #> system x86_64, darwin15.6.0 #> ui X11 #> language (EN) #> collate en_AU.UTF-8 #> tz Australia/Melbourne #> date 2017-12-27 #> Packages ----------------------------------------------------------------- #> package * version date source #> backports 1.1.2 2017-12-13 CRAN (R 3.5.0) #> base * 3.5.0 2017-12-13 local #> biglm * 0.9-1 2013-05-16 CRAN (R 3.5.0) #> BiocGenerics 0.25.1 2017-12-17 Bioconductor #> compiler 3.5.0 2017-12-13 local #> datasets * 3.5.0 2017-12-13 local #> DBI * 0.7 2017-06-18 CRAN (R 3.5.0) #> devtools 1.13.4 2017-11-09 CRAN (R 3.5.0) #> digest 0.6.13 2017-12-14 CRAN (R 3.5.0) #> evaluate 0.10.1 2017-06-24 CRAN (R 3.5.0) #> graphics * 3.5.0 2017-12-13 local #> grDevices * 3.5.0 2017-12-13 local #> grid 3.5.0 2017-12-13 local #> htmltools 0.3.6 2017-04-28 CRAN (R 3.5.0) #> irlba 2.3.1 2017-10-18 CRAN (R 3.5.0) #> knitr 1.17 2017-08-10 CRAN (R 3.5.0) #> lattice 0.20-35 2017-03-25 CRAN (R 3.5.0) #> magrittr 1.5 2014-11-22 CRAN (R 3.5.0) #> Matrix 1.2-12 2017-11-20 CRAN (R 3.5.0) #> matter * 1.5.4 2017-12-04 Bioconductor #> memoise 1.1.0 2017-04-21 CRAN (R 3.5.0) #> methods * 3.5.0 2017-12-13 local #> parallel 3.5.0 2017-12-13 local #> Rcpp 0.12.14 2017-11-23 CRAN (R 3.5.0) #> rmarkdown 1.8 2017-11-17 CRAN (R 3.5.0) #> rprojroot 1.3-1 2017-12-18 CRAN (R 3.5.0) #> stats * 3.5.0 2017-12-13 local #> stringi 1.1.6 2017-11-17 CRAN (R 3.5.0) #> stringr 1.2.0 2017-02-18 CRAN (R 3.5.0) #> tools 3.5.0 2017-12-13 local #> utils * 3.5.0 2017-12-13 local #> withr 2.1.1 2017-12-19 CRAN (R 3.5.0) #> yaml 2.1.16 2017-12-12 CRAN (R 3.5.0) ```
kuwisdelu commented 5 years ago

These should behave more as expected in version >= 1.9.8, with the exception that subsetting a matter_arr always produces an array and never a matrix.