Skip to content

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

Description

@const-ae

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions