ScaledMatrix classScaledMatrix 1.19.0
The ScaledMatrix provides yet another method of running scale() on a matrix.
In other words, these three operations are equivalent:
mat <- matrix(rnorm(10000), ncol=10)
smat1 <- scale(mat)
head(smat1)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1.6072423 0.55387687 1.3060202 0.8878116 -1.09405337 1.39320311
## [2,] -0.5281831 1.19501973 1.4608470 0.2797784 -1.33110607 0.04359519
## [3,] -1.2236190 0.41835106 1.0057325 0.2676306 1.55592020 -1.62682168
## [4,] -1.5105457 -0.03914992 0.8281096 -0.7274358 -0.55911973 0.90864646
## [5,] 0.3518864 -0.47610575 0.2387136 -1.6860028 1.07471383 -1.32833941
## [6,] -0.4211827 -1.37287703 0.3400162 -1.2531290 -0.06658498 -0.73804449
## [,7] [,8] [,9] [,10]
## [1,] 0.6097230 1.8020581 -0.83989928 0.09926027
## [2,] -0.9902057 0.4142821 -0.26338951 -0.51597468
## [3,] -1.1163040 -2.0586919 -0.08017127 -0.94197637
## [4,] 0.6969247 -0.9030848 0.62775857 -1.24504489
## [5,] -1.5033423 -0.9796331 -1.10289056 0.11669370
## [6,] 2.1257719 1.1605020 -0.09165229 -1.45552831
library(DelayedArray)
smat2 <- scale(DelayedArray(mat))
head(smat2)
## <6 x 10> DelayedMatrix object of type "double":
## [,1] [,2] [,3] ... [,9] [,10]
## [1,] 1.60724230 0.55387687 1.30602023 . -0.83989928 0.09926027
## [2,] -0.52818307 1.19501973 1.46084696 . -0.26338951 -0.51597468
## [3,] -1.22361896 0.41835106 1.00573248 . -0.08017127 -0.94197637
## [4,] -1.51054568 -0.03914992 0.82810957 . 0.62775857 -1.24504489
## [5,] 0.35188640 -0.47610575 0.23871362 . -1.10289056 0.11669370
## [6,] -0.42118272 -1.37287703 0.34001618 . -0.09165229 -1.45552831
library(ScaledMatrix)
smat3 <- ScaledMatrix(mat, center=TRUE, scale=TRUE)
head(smat3)
## <6 x 10> ScaledMatrix object of type "double":
## [,1] [,2] [,3] ... [,9] [,10]
## [1,] 1.60724230 0.55387687 1.30602023 . -0.83989928 0.09926027
## [2,] -0.52818307 1.19501973 1.46084696 . -0.26338951 -0.51597468
## [3,] -1.22361896 0.41835106 1.00573248 . -0.08017127 -0.94197637
## [4,] -1.51054568 -0.03914992 0.82810957 . 0.62775857 -1.24504489
## [5,] 0.35188640 -0.47610575 0.23871362 . -1.10289056 0.11669370
## [6,] -0.42118272 -1.37287703 0.34001618 . -0.09165229 -1.45552831
The biggest difference lies in how they behave in downstream matrix operations.
smat1 is an ordinary matrix, with the scaled and centered values fully realized in memory.
Nothing too unusual here.smat2 is a DelayedMatrix and undergoes block processing whereby chunks are realized and operated on, one at a time.
This sacrifices speed for greater memory efficiency by avoiding a copy of the entire matrix.
In particular, it preserves the structure of the original mat, e.g., from a sparse or file-backed representation.smat3 is a ScaledMatrix that refactors certain operations so that they can be applied to the original mat without any scaling or centering.
This takes advantage of the original data structure to speed up matrix multiplication and row/column sums,
albeit at the cost of numerical precision.Given an original matrix \(\mathbf{X}\) with \(n\) columns, a vector of column centers \(\mathbf{c}\) and a vector of column scaling values \(\mathbf{s}\), our scaled matrix can be written as:
\[ \mathbf{Y} = (\mathbf{X} - \mathbf{c} \cdot \mathbf{1}_n^T) \mathbf{S} \]
where \(\mathbf{S} = \text{diag}(s_1^{-1}, ..., s_n^{-1})\). If we wanted to right-multiply it with another matrix \(\mathbf{A}\), we would have:
\[ \mathbf{YA} = \mathbf{X}\mathbf{S}\mathbf{A} - \mathbf{c} \cdot \mathbf{1}_n^T \mathbf{S}\mathbf{A} \]
The right-most expression is simply the outer product of \(\mathbf{c}\) with the column sums of \(\mathbf{SA}\). More important is the fact that we can use the matrix multiplication operator for \(\mathbf{X}\) with \(\mathbf{SA}\), as this allows us to use highly efficient algorithms for certain data representations, e.g., sparse matrices.
library(Matrix)
mat <- rsparsematrix(20000, 10000, density=0.01)
smat <- ScaledMatrix(mat, center=TRUE, scale=TRUE)
blob <- matrix(runif(ncol(mat) * 5), ncol=5)
system.time(out <- smat %*% blob)
## user system elapsed
## 0.023 0.000 0.024
# The slower way with block processing.
da <- scale(DelayedArray(mat))
system.time(out2 <- da %*% blob)
## user system elapsed
## 22.284 1.866 26.062
The same logic applies for left-multiplication and cross-products.
This allows us to easily speed up high-level operations involving matrix multiplication by just switching to a ScaledMatrix,
e.g., in approximate PCA algorithms from the BiocSingular package.
library(BiocSingular)
set.seed(1000)
system.time(pcs <- runSVD(smat, k=10, BSPARAM=IrlbaParam()))
## user system elapsed
## 11.028 1.785 20.388
Row and column sums are special cases of matrix multiplication and can be computed quickly:
system.time(rowSums(smat))
## user system elapsed
## 0.007 0.000 0.007
system.time(rowSums(da))
## user system elapsed
## 21.422 1.889 23.517
Subsetting, transposition and renaming of the dimensions are all supported without loss of the ScaledMatrix representation:
smat[,1:5]
## <20000 x 5> ScaledMatrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] -0.0002484841 0.0051915033 0.0091845126 0.0088619285 -0.0069561280
## [2,] -0.0002484841 0.0051915033 0.0091845126 0.0088619285 -0.0069561280
## [3,] -0.0002484841 0.0051915033 0.0091845126 0.0088619285 -0.0069561280
## [4,] -0.0002484841 0.0051915033 0.0091845126 0.0088619285 -0.0069561280
## [5,] -0.0002484841 0.0051915033 0.0091845126 0.0088619285 -0.0069561280
## ... . . . . .
## [19996,] -0.0002484841 0.0051915033 0.0091845126 0.0088619285 -0.0069561280
## [19997,] -0.0002484841 0.0051915033 0.0091845126 0.0088619285 -0.0069561280
## [19998,] -0.0002484841 0.0051915033 0.0091845126 0.0088619285 -0.0069561280
## [19999,] -0.0002484841 0.0051915033 0.0091845126 0.0088619285 -0.0069561280
## [20000,] -0.0002484841 0.0051915033 0.0091845126 0.0088619285 -0.0069561280
t(smat)
## <10000 x 20000> ScaledMatrix object of type "double":
## [,1] [,2] [,3] ... [,19999]
## [1,] -0.0002484841 -0.0002484841 -0.0002484841 . -0.0002484841
## [2,] 0.0051915033 0.0051915033 0.0051915033 . 0.0051915033
## [3,] 0.0091845126 0.0091845126 0.0091845126 . 0.0091845126
## [4,] 0.0088619285 0.0088619285 0.0088619285 . 0.0088619285
## [5,] -0.0069561280 -0.0069561280 -0.0069561280 . -0.0069561280
## ... . . . . .
## [9996,] -0.003577931 -0.003577931 -0.003577931 . -0.003577931
## [9997,] 0.007849041 0.007849041 0.007849041 . 0.007849041
## [9998,] 0.011593948 0.011593948 0.011593948 . 0.011593948
## [9999,] -0.003892202 -0.003892202 -0.003892202 . -0.003892202
## [10000,] -0.010782578 -0.010782578 -0.010782578 . -0.010782578
## [,20000]
## [1,] -0.0002484841
## [2,] 0.0051915033
## [3,] 0.0091845126
## [4,] 0.0088619285
## [5,] -0.0069561280
## ... .
## [9996,] -0.003577931
## [9997,] 0.007849041
## [9998,] 0.011593948
## [9999,] -0.003892202
## [10000,] -0.010782578
rownames(smat) <- paste0("GENE_", 1:20000)
smat
## <20000 x 10000> ScaledMatrix object of type "double":
## [,1] [,2] [,3] ... [,9999]
## GENE_1 -0.0002484841 0.0051915033 0.0091845126 . -0.003892202
## GENE_2 -0.0002484841 0.0051915033 0.0091845126 . -0.003892202
## GENE_3 -0.0002484841 0.0051915033 0.0091845126 . -0.003892202
## GENE_4 -0.0002484841 0.0051915033 0.0091845126 . -0.003892202
## GENE_5 -0.0002484841 0.0051915033 0.0091845126 . -0.003892202
## ... . . . . .
## GENE_19996 -0.0002484841 0.0051915033 0.0091845126 . -0.003892202
## GENE_19997 -0.0002484841 0.0051915033 0.0091845126 . -0.003892202
## GENE_19998 -0.0002484841 0.0051915033 0.0091845126 . -0.003892202
## GENE_19999 -0.0002484841 0.0051915033 0.0091845126 . -0.003892202
## GENE_20000 -0.0002484841 0.0051915033 0.0091845126 . -0.003892202
## [,10000]
## GENE_1 -0.010782578
## GENE_2 -0.010782578
## GENE_3 -0.010782578
## GENE_4 -0.010782578
## GENE_5 -0.010782578
## ... .
## GENE_19996 -0.010782578
## GENE_19997 -0.010782578
## GENE_19998 -0.010782578
## GENE_19999 -0.010782578
## GENE_20000 -0.010782578
Other operations will cause the ScaledMatrix to collapse to the general DelayedMatrix representation, after which point block processing will be used.
smat + 1
## <20000 x 10000> DelayedMatrix object of type "double":
## [,1] [,2] [,3] ... [,9999] [,10000]
## GENE_1 0.9997515 1.0051915 1.0091845 . 0.9961078 0.9892174
## GENE_2 0.9997515 1.0051915 1.0091845 . 0.9961078 0.9892174
## GENE_3 0.9997515 1.0051915 1.0091845 . 0.9961078 0.9892174
## GENE_4 0.9997515 1.0051915 1.0091845 . 0.9961078 0.9892174
## GENE_5 0.9997515 1.0051915 1.0091845 . 0.9961078 0.9892174
## ... . . . . . .
## GENE_19996 0.9997515 1.0051915 1.0091845 . 0.9961078 0.9892174
## GENE_19997 0.9997515 1.0051915 1.0091845 . 0.9961078 0.9892174
## GENE_19998 0.9997515 1.0051915 1.0091845 . 0.9961078 0.9892174
## GENE_19999 0.9997515 1.0051915 1.0091845 . 0.9961078 0.9892174
## GENE_20000 0.9997515 1.0051915 1.0091845 . 0.9961078 0.9892174
For most part, the implementation of the multiplication assumes that the \(\mathbf{A}\) matrix and the matrix product are small compared to \(\mathbf{X}\).
It is also possible to multiply two ScaledMatrixes together if the underlying matrices have efficient operators for their product.
However, if this is not the case, the ScaledMatrix offers little benefit for increased overhead.
It is also worth noting that this speed-up is not entirely free.
The expression above involves subtracting two matrix with potentially large values, which runs the risk of catastrophic cancellation.
The example below demonstrates how ScaledMatrix is more susceptible to loss of precision than a normal DelayedArray:
set.seed(1000)
mat <- matrix(rnorm(1000000), ncol=100000)
big.mat <- mat + 1e12
# The 'correct' value, unaffected by numerical precision.
ref <- rowMeans(scale(mat))
head(ref)
## [1] -0.0025584703 -0.0008570664 -0.0019225335 -0.0001039903 0.0024761772
## [6] 0.0032943203
# The value from scale'ing a DelayedArray.
library(DelayedArray)
smat2 <- scale(DelayedArray(big.mat))
head(rowMeans(smat2))
## [1] -0.0025583534 -0.0008571123 -0.0019226040 -0.0001039539 0.0024761618
## [6] 0.0032943783
# The value from a ScaledMatrix.
library(ScaledMatrix)
smat3 <- ScaledMatrix(big.mat, center=TRUE, scale=TRUE)
head(rowMeans(smat3))
## [1] -0.00480 0.00848 0.00544 -0.00976 -0.01056 0.01520
In most practical applications, though, this does not seem to be a major concern, especially as most values (e.g., log-normalized expression matrices) lie close to zero anyway.
sessionInfo()
## R Under development (unstable) (2025-10-21 r88958)
## Platform: x86_64-apple-darwin20
## Running under: macOS Ventura 13.7.8
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.6-x86_64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.6-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.1
##
## locale:
## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## time zone: America/New_York
## tzcode source: internal
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] BiocSingular_1.27.1 ScaledMatrix_1.19.0 DelayedArray_0.37.0
## [4] SparseArray_1.11.2 S4Arrays_1.11.0 abind_1.4-8
## [7] IRanges_2.45.0 S4Vectors_0.49.0 MatrixGenerics_1.23.0
## [10] matrixStats_1.5.0 BiocGenerics_0.57.0 generics_0.1.4
## [13] Matrix_1.7-4 BiocStyle_2.39.0
##
## loaded via a namespace (and not attached):
## [1] jsonlite_2.0.0 compiler_4.6.0
## [3] BiocManager_1.30.27 rsvd_1.0.5
## [5] Rcpp_1.1.0 DelayedMatrixStats_1.33.0
## [7] parallel_4.6.0 jquerylib_0.1.4
## [9] BiocParallel_1.45.0 yaml_2.3.10
## [11] fastmap_1.2.0 lattice_0.22-7
## [13] R6_2.6.1 XVector_0.51.0
## [15] knitr_1.50 bookdown_0.45
## [17] bslib_0.9.0 rlang_1.1.6
## [19] cachem_1.1.0 xfun_0.54
## [21] sass_0.4.10 cli_3.6.5
## [23] digest_0.6.38 grid_4.6.0
## [25] irlba_2.3.5.1 sparseMatrixStats_1.23.0
## [27] lifecycle_1.0.4 evaluate_1.0.5
## [29] codetools_0.2-20 beachmat_2.27.0
## [31] rmarkdown_2.30 tools_4.6.0
## [33] htmltools_0.5.8.1