[R] Unexpected errors in sparse Matrix arithmetic with zero-length dimensions
Aaron Lun
|n||n|te@monkey@@w|th@keybo@rd@ @end|ng |rom gm@||@com
Sun Feb 10 16:22:17 CET 2019
Dear list,
The Matrix package exhibits some unexpected behaviour in its arithmetic
methods for the edge case of a sparse matrix with a dimension of zero
length. The example below is the most illustrative, where changing the
contents of the vector causes the subtraction to fail for a sparse
matrix with no columns:
> library(Matrix)
> x <- rsparsematrix(10, 0, density=0.1)
>
> x - rep(1, nrow(x)) # OK
> x - rep(0, nrow(x)) # fails
Error in .Ops.recycle.ind(e1, len = l2) :
vector too long in Matrix - vector operation
This is presumably because Matrix recognizes that subtraction of zero
preserves sparsity and thus uses a different method in the second case.
However, I would have expected subtraction of a zero vector to work if
subtraction of a general vector is permissible. This is accompanied by
a host of related errors for sparsity-preserving arithmetic:
> x / 1 # OK
> x / rep(1, nrow(x)) # fails
Error in .Ops.recycle.ind(e1, len = l2) :
vector too long in Matrix - vector operation
>
> x * 1 # OK
> x * rep(1, nrow(x)) # fails
Error in .Ops.recycle.ind(e1, len = l2) :
vector too long in Matrix - vector operation
A different error is raised for a sparse matrix with no rows:
> y <- rsparsematrix(0, 10, density=0.1)
>
> y - numeric(1) # OK
> y - numeric(0) # fails
Error in y - numeric(0) : <Matrix> - numeric(0) is undefined
I would have expected to just get 'y' back, given that the same code
works fine for other Matrix classes:
> z <- as(y, "dgeMatrix")
> z - numeric(0) # OK
Correct behaviour of zero-dimension sparse matrices is practically
important to me; I develop a number of packages that rely on Matrix
classes, and in those packages, I do a lot of unit testing with zero-
dimension inputs. This ensures that my functions return sensible
results or fail gracefully in edge cases that might be encountered by
users. The current behaviour of sparse Matrix arithmetic causes my unit
tests to fail for no (obvious) good reason.
Best,
Aaron Lun
Research Associate
CRUK Cambridge Institute
University of Cambridge
> sessionInfo()
R Under development (unstable) (2019-01-14 r75992)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.5 LTS
Matrix products: default
BLAS: /home/cri.camres.org/lun01/Software/R/trunk/lib/libRblas.so
LAPACK: /home/cri.camres.org/lun01/Software/R/trunk/lib/libRlapack.so
locale:
[1] LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_GB.UTF-8 LC_COLLATE=en_GB.UTF-8
[5] LC_MONETARY=en_GB.UTF-8 LC_MESSAGES=en_GB.UTF-8
[7] LC_PAPER=en_GB.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices
utils datasets methods base
other attached packages:
[1] Matrix_1.2-15
loaded via a namespace (and not attached):
[1] compiler_3.6.0 grid_3.6.0 lattice_0.20-38
More information about the R-help
mailing list