Arithmetic

library(dbMatrix)

dbMatrix arithmetic

dbMatrix objects support Arith and Ops operations. We will demonstrate how to perform arithmetic operations on dbSparseMatrix objects.

Note: Some operations with zero values are not yet supported with dbMatrix objects. In addition, certain arithmetic operations between dbMatrix objects are also not yet supported. We welcome user feedback and reporting issues on the Github page.

Create test data

Let’s create a simple sparse matrix for demonstration:

The matrix contains 100 rows (genes) and 50 columns (cells). Like most single-cell RNA-seq data, the matrix is sparse.

Create a dbMatrix object

Let’s create a dbSparseMatrix object from the above dgc object.

Scalar Arithmetic

dbMatrix emulates scalar arithmetic in the Matrix package.

Note: Addition or subtraction with non-zero addends on a dbSparseMatrix results in a dbDenseMatrix.

Matrix Arithmetic

dbMatrix also supports matrix arithmetic for dbMatrix objects that are conformable.

Matrix Multiplication

Matrix product

TODO

Cleanup

Session Info

sessionInfo()
#> R version 4.5.2 (2025-10-31)
#> Platform: x86_64-pc-linux-gnu
#> Running under: AlmaLinux 8.10 (Cerulean Leopard)
#> 
#> Matrix products: default
#> BLAS/LAPACK: FlexiBLAS NETLIB;  LAPACK version 3.12.0
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
#>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
#>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
#> 
#> time zone: America/New_York
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] dbMatrix_0.1.0
#> 
#> loaded via a namespace (and not attached):
#>  [1] bit_4.6.0             Matrix_1.7-4          jsonlite_2.0.0       
#>  [4] dplyr_1.1.4           compiler_4.5.2        tidyselect_1.2.1     
#>  [7] Rcpp_1.1.0            blob_1.2.4            nanoarrow_0.7.0-1    
#> [10] pins_1.4.1            assertthat_0.2.1      dbProject_0.0.0.9002 
#> [13] jquerylib_0.1.4       arrow_22.0.0          yaml_2.3.10          
#> [16] fastmap_1.2.0         lattice_0.22-7        R6_2.6.1             
#> [19] generics_0.1.4        knitr_1.50            tibble_3.3.0         
#> [22] MatrixGenerics_1.22.0 DBI_1.2.3             bslib_0.9.0          
#> [25] pillar_1.11.1         connections_0.2.1     rlang_1.1.6          
#> [28] cachem_1.1.0          xfun_0.54             sass_0.4.10          
#> [31] bit64_4.6.0-1         cli_3.6.5             withr_3.0.2          
#> [34] magrittr_2.0.4        digest_0.6.38         grid_4.5.2           
#> [37] rscontract_0.1.2      dbplyr_2.5.1          lifecycle_1.0.4      
#> [40] vctrs_0.6.5           evaluate_1.0.5        glue_1.8.0           
#> [43] data.table_1.17.8     duckdb_1.4.2          rmarkdown_2.30       
#> [46] purrr_1.2.0           matrixStats_1.5.0     tools_4.5.2          
#> [49] pkgconfig_2.0.3       htmltools_0.5.8.1