DelayedArray icon indicating copy to clipboard operation
DelayedArray copied to clipboard

Summation of ConstantArray of 0 with vector of 0's fails

Open const-ae opened this issue 9 months ago • 0 comments

Hi,

I ran into a curious edge case, where summing a ConstantArray with a vector fails if both are zero and then writing it with HDF5Array::writeHDF5Array, but not in any other combination:

mat <- DelayedArray::ConstantArray(c(2, 4), value = 0)
res <- mat + rep(0, 2)
res
#> <2 x 4> sparse DelayedMatrix object of type "double":
#>      [,1] [,2] [,3] [,4]
#> [1,]    0    0    0    0
#> [2,]    0    0    0    0
HDF5Array::writeHDF5Array(res)
#> Error in .Arith_SVT1_v2(.Generic, e1, e2): "+" is not supported between a SparseArray object and a numeric vector
#>   (result wouldn't be sparse in general)

# All other combination seems to work
mat <- DelayedArray::ConstantArray(c(2, 4), value = 0)
res <- mat + rep(3, 2)
HDF5Array::writeHDF5Array(res)
#> <2 x 4> HDF5Matrix object of type "double":
#>      [,1] [,2] [,3] [,4]
#> [1,]    3    3    3    3
#> [2,]    3    3    3    3

mat <- DelayedArray::ConstantArray(c(2, 4), value = 17)
res <- mat + rep(0, 2)
HDF5Array::writeHDF5Array(res)
#> <2 x 4> HDF5Matrix object of type "double":
#>      [,1] [,2] [,3] [,4]
#> [1,]   17   17   17   17
#> [2,]   17   17   17   17

mat <- DelayedArray::ConstantArray(c(2, 4), value = 0)
res <- mat + 0
HDF5Array::writeHDF5Array(res)
#> <2 x 4> sparse HDF5Matrix object of type "double":
#>      [,1] [,2] [,3] [,4]
#> [1,]    0    0    0    0
#> [2,]    0    0    0    0

Created on 2025-03-05 with reprex v2.1.1

I am not actually 100% this is an DelayedArray issue or if it might rather need to be moved to HDF5Array.

Best, Constantin

const-ae avatar Mar 05 '25 13:03 const-ae