Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2025-11-02 14:46:33.503126
Compiled: Sun Nov 2 14:48:20 2025
## Warning: multiple methods tables found for 'kronecker'
## Warning: package 'MatrixGenerics' was built under R version 4.5.2
suppressPackageStartupMessages(library("HDF5Array"))
suppressPackageStartupMessages(library("DelayedRandomArray"))
darr1 <- RandomUnifArray(c(2,3,4))
darr2 <- RandomUnifArray(c(2,3,4))There are several settings in DelayedTensor.
First, the sparsity of the intermediate DelayedArray
objects calculated inside DelayedTensor
is set by setSparse.
Note that the sparse mode is experimental.
Whether it contributes to higher speed and lower memory is quite dependent on the sparsity of the DelayedArray, and the current implementation does not recognize the block size, which may cause out-of-memory errors, when the data is extremely huge.
Here, we specify as.sparse as FALSE (this
is also the default value for now).
Next, the verbose message is suppressed by setVerbose.
This is useful when we want to monitor the calculation process.
Here we specify as.verbose as FALSE (this
is also the default value for now).
The block size of block processing is specified by
setAutoBlockSize. When the sparse mode is off, all the
functions of DelayedTensor
are performed as block processing, in which each block
vector/matrix/tensor is expanded to memory space from on-disk file
incrementally so as not to exceed the specified size.
Here, we specify the block size as 1E+8.
## automatic block size set to 1e+08 bytes (was 1e+08)
Finally, the temporal directory to store the intermediate HDF5 files
during running DelayedTensor
is specified by setHDF5DumpDir.
Note that in many systems the /var directory has the
storage limitation, so if there is no enough space, user should specify
the other directory.
# tmpdir <- paste(sample(c(letters,1:9), 10), collapse="")
# dir.create(tmpdir, recursive=TRUE))
tmpdir <- tempdir()
setHDF5DumpDir(tmpdir)These specified values are also extracted by each getter function.
## $delayedtensor.sparse
## [1] FALSE
## $delayedtensor.verbose
## [1] FALSE
## [1] 1e+08
## [1] "/tmp/Rtmpbj1kgR"
Unfold (a.k.a. matricizing) operations are used to reshape a tensor into a matrix.
In unfold, row_idx and col_idx
are specified to set which modes are used as the row/column.
## <6 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.73150141 0.79477772 0.16861208 0.83076943
## [2,] 0.92136916 0.23518031 0.72449091 0.22117949
## [3,] 0.56485097 0.23952140 0.59129711 0.38918445
## [4,] 0.40591160 0.71274117 0.05594483 0.78804981
## [5,] 0.59482283 0.53034212 0.62684825 0.20189629
## [6,] 0.80289311 0.49371215 0.63243856 0.88556719
fold is the inverse operation of unfold,
which is used to reshape a matrix into a tensor.
In fold, row_idx/col_idx are
specified to set which modes correspond the row/column of the output
tensor and modes is specified to set the mode of the output
tensor.
dmat1_to_darr1 <- DelayedTensor::fold(dmat1,
row_idx=c(1,2), col_idx=3, modes=dim(darr1))
dmat1_to_darr1## <2 x 3 x 4> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.7315014 0.5648510 0.5948228
## [2,] 0.9213692 0.4059116 0.8028931
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.7947777 0.2395214 0.5303421
## [2,] 0.2351803 0.7127412 0.4937122
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.16861208 0.59129711 0.62684825
## [2,] 0.72449091 0.05594483 0.63243856
##
## ,,4
## [,1] [,2] [,3]
## [1,] 0.8307694 0.3891845 0.2018963
## [2,] 0.2211795 0.7880498 0.8855672
## [1] TRUE
There are some wrapper functions of unfold and
fold.
For example, in k_unfold, mode m is used as
the row, and the other modes are is used as the column.
k_fold is the inverse operation of
k_unfold.
dmat2 <- DelayedTensor::k_unfold(darr1, m=1)
dmat2_to_darr1 <- k_fold(dmat2, m=1, modes=dim(darr1))
identical(as.array(darr1), as.array(dmat2_to_darr1))## [1] TRUE
dmat3 <- DelayedTensor::k_unfold(darr1, m=2)
dmat3_to_darr1 <- k_fold(dmat3, m=2, modes=dim(darr1))
identical(as.array(darr1), as.array(dmat3_to_darr1))## [1] TRUE
dmat4 <- DelayedTensor::k_unfold(darr1, m=3)
dmat4_to_darr1 <- k_fold(dmat4, m=3, modes=dim(darr1))
identical(as.array(darr1), as.array(dmat4_to_darr1))## [1] TRUE
In rs_unfold, mode m is used as the row,
and the other modes are is used as the column.
rs_fold and rs_unfold also perform the same
operations.
On the other hand, cs_unfold specifies the mode
m as the column and the other modes are specified as the
column.
cs_fold is the inverse operation of
cs_unfold.
dmat8 <- DelayedTensor::cs_unfold(darr1, m=1)
dmat8_to_darr1 <- DelayedTensor::cs_fold(dmat8, m=1, modes=dim(darr1))
identical(as.array(darr1), as.array(dmat8_to_darr1))## [1] TRUE
dmat9 <- DelayedTensor::cs_unfold(darr1, m=2)
dmat9_to_darr1 <- DelayedTensor::cs_fold(dmat9, m=2, modes=dim(darr1))
identical(as.array(darr1), as.array(dmat9_to_darr1))## [1] TRUE
dmat10 <- DelayedTensor::cs_unfold(darr1, m=3)
dmat10_to_darr1 <- DelayedTensor::cs_fold(dmat10, m=3, modes=dim(darr1))
identical(as.array(darr1), as.array(dmat10_to_darr1))## [1] TRUE
In matvec, m=2 is specified as unfold.
unmatvec is the inverse operation of
matvec.
dmat11 <- DelayedTensor::matvec(darr1)
dmat11_darr1 <- DelayedTensor::unmatvec(dmat11, modes=dim(darr1))
identical(as.array(darr1), as.array(dmat11_darr1))## [1] TRUE
ttm multiplies a tensor by a matrix.
m specifies in which mode the matrix will be
multiplied.
## <2 x 3 x 10> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 1.2106004 0.8312372 0.8970348
## [2,] 0.8412638 1.0252662 1.3667765
##
## ,,2
## [,1] [,2] [,3]
## [1,] 1.0520512 0.9798175 1.1496666
## [2,] 1.2693965 0.7282153 1.3358128
##
## ,,3
## [,1] [,2] [,3]
## [1,] 1.3364005 1.2253643 1.4695393
## [2,] 1.6630911 0.8863759 1.6483559
##
## ...
##
## ,,8
## [,1] [,2] [,3]
## [1,] 1.758260 1.150391 1.295881
## [2,] 1.276656 1.428325 1.879212
##
## ,,9
## [,1] [,2] [,3]
## [1,] 0.9195885 0.8789550 1.0400453
## [2,] 1.1646293 0.6177706 1.1774457
##
## ,,10
## [,1] [,2] [,3]
## [1,] 1.376282 1.123234 1.163910
## [2,] 1.260302 1.082361 1.716407
ttl multiplies a tensor by multiple matrices.
ms specifies in which mode these matrices will be
multiplied.
dmatX <- RandomUnifArray(c(10,2))
dmatY <- RandomUnifArray(c(10,3))
dlizt <- list(dmatX = dmatX, dmatY = dmatY)
DelayedTensor::ttl(darr1, dlizt, ms=c(1,2))## <10 x 10 x 4> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] ... [,9] [,10]
## [1,] 1.3665359 1.3170548 0.6197307 . 0.7135144 1.0028178
## [2,] 1.9791870 1.9100166 0.8973157 . 1.0316369 1.4496407
## ... . . . . . .
## [9,] 1.7005531 1.6420424 0.7708955 . 0.8857494 1.2445358
## [10,] 1.0977076 1.0672147 0.4968686 . 0.5666080 0.7952841
##
## ...
##
## ,,4
## [,1] [,2] [,3] ... [,9] [,10]
## [1,] 1.1475555 1.1076508 0.4837987 . 0.4888148 0.7557166
## [2,] 1.6336371 1.5854470 0.7084154 . 0.7513950 1.1139996
## ... . . . . . .
## [9,] 1.3931573 1.3553008 0.6115354 . 0.6616622 0.9643660
## [10,] 0.8164554 0.8200358 0.4172598 . 0.5537956 0.6793124
vec collapses a DelayedArray
into a 1D DelayedArray
(vector).
## <24> HDF5Array object of type "double":
## [1] [2] [3] . [23] [24]
## 0.7315014 0.9213692 0.5648510 . 0.2018963 0.8855672
fnorm calculates the Frobenius norm of a DelayedArray.
## [1] 2.946053
innerProd calculates the inner product value of two
DelayedArray.
## [1] 7.246441
Inner product multiplies two tensors and collapses to 0D tensor (norm). On the other hand, the outer product is an operation that leaves all subscripts intact.
## <2 x 3 x 2 x 3> HDF5Array object of type "double":
## ,,1,1
## [,1] [,2] [,3]
## [1,] 0.4752483 0.3669774 0.3864498
## [2,] 0.5986033 0.2637162 0.5216307
##
## ,,2,1
## [,1] [,2] [,3]
## [1,] 0.4136847 0.3194392 0.3363891
## [2,] 0.5210602 0.2295545 0.4540587
##
## ,,1,2
## [,1] [,2] [,3]
## [1,] 0.3887104 0.3001545 0.3160812
## [2,] 0.4896037 0.2156962 0.4266470
##
## ,,2,2
## [,1] [,2] [,3]
## [1,] 0.2046313 0.1580122 0.1663966
## [2,] 0.2577451 0.1135503 0.2246025
##
## ,,1,3
## [,1] [,2] [,3]
## [1,] 0.3973715 0.3068425 0.3231240
## [2,] 0.5005129 0.2205023 0.4361534
##
## ,,2,3
## [,1] [,2] [,3]
## [1,] 0.2229758 0.1721775 0.1813135
## [2,] 0.2808512 0.1237297 0.2447374
Using DelayedDiagonalArray, we can originally create a
diagonal DelayedArray
by specifying the dimensions (modes) and the values.
## <5 x 6 x 7> sparse DelayedArray object of type "integer":
## ,,1
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 0 0 0 0 0
## [2,] 0 0 0 0 0 0
## [3,] 0 0 0 0 0 0
## [4,] 0 0 0 0 0 0
## [5,] 0 0 0 0 0 0
##
## ...
##
## ,,7
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 0 0 0 0 0 0
## [2,] 0 0 0 0 0 0
## [3,] 0 0 0 0 0 0
## [4,] 0 0 0 0 0 0
## [5,] 0 0 0 0 0 0
Similar to the diag of the base package,
the diag of DelayedTensor
is used to extract and assign values to DelayedArray.
## <5> DelayedArray object of type "integer":
## [1] [2] [3] [4] [5]
## 1 2 3 4 5
## <5> DelayedArray object of type "double":
## [1] [2] [3] [4] [5]
## 1111 2222 3333 4444 5555
modeSum calculates the summation for a given mode
m of a DelayedArray.
The mode specified as m is collapsed into 1D as
follows.
## <1 x 3 x 4> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 1.6528706 0.9707626 1.3977159
##
## ,,2
## [,1] [,2] [,3]
## [1,] 1.0299580 0.9522626 1.0240543
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.8931030 0.6472419 1.2592868
##
## ,,4
## [,1] [,2] [,3]
## [1,] 1.051949 1.177234 1.087463
## <2 x 1 x 4> DelayedArray object of type "double":
## ,,1
## [,1]
## [1,] 1.891175
## [2,] 2.130174
##
## ,,2
## [,1]
## [1,] 1.564641
## [2,] 1.441634
##
## ,,3
## [,1]
## [1,] 1.386757
## [2,] 1.412874
##
## ,,4
## [,1]
## [1,] 1.421850
## [2,] 1.894796
## <2 x 3 x 1> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 2.525661 1.784854 1.953909
## [2,] 2.102220 1.962647 2.814611
Similar to modeSum, modeMean calculates the
average value for a given mode m of a DelayedArray.
## <1 x 3 x 4> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.8264353 0.4853813 0.6988580
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.5149790 0.4761313 0.5120271
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.4465515 0.3236210 0.6296434
##
## ,,4
## [,1] [,2] [,3]
## [1,] 0.5259745 0.5886171 0.5437317
## <2 x 1 x 4> DelayedArray object of type "double":
## ,,1
## [,1]
## [1,] 0.6303917
## [2,] 0.7100580
##
## ,,2
## [,1]
## [1,] 0.5215471
## [2,] 0.4805445
##
## ,,3
## [,1]
## [1,] 0.4622525
## [2,] 0.4709581
##
## ,,4
## [,1]
## [1,] 0.4739501
## [2,] 0.6315988
## <2 x 3 x 1> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.6314152 0.4462135 0.4884774
## [2,] 0.5255550 0.4906619 0.7036528
There are some tensor specific product such as Hadamard product, Kronecker product, and Khatri-Rao product.
Suppose a tensor \(A \in \Re ^{I \times J}\) and a tensor \(B \in \Re ^{I \times J}\).
Hadamard product is defined as the element-wise product of \(A\) and \(B\).
Hadamard product can be extended to higher-order tensors.
\[ A \circ B = \begin{bmatrix} a_{11}b_{11} & a_{12}b_{12} & \cdots & a_{1J}b_{1J} \\ a_{21}b_{21} & a_{22}b_{22} & \cdots & a_{2J}b_{2J} \\ \vdots & \vdots & \ddots & \vdots \\ a_{I1}b_{I1} & a_{I2}b_{I2} & \cdots & a_{IJ}b_{IJ} \\ \end{bmatrix} \]
hadamard calculates Hadamard product of two DelayedArray
objects.
## [1] 2 3 4
hadamard_list calculates Hadamard product of multiple
DelayedArray
objects.
## [1] 2 3 4
Suppose a tensor \(A \in \Re ^{I \times J}\) and a tensor \(B \in \Re ^{K \times L}\).
Kronecker product is defined as all the possible combination of element-wise product and the dimensions of output tensor are \({IK \times JL}\).
Kronecker product can be extended to higher-order tensors.
\[ A \otimes B = \begin{bmatrix} a_{11}B & a_{12}B & \cdots & a_{1J}B \\ a_{21}B & a_{22}B & \cdots & a_{2J}B \\ \vdots & \vdots & \ddots & \vdots \\ a_{I1}B & a_{I2}B & \cdots & a_{IJ}B \\ \end{bmatrix} \]
kronecker calculates Kronecker product of two DelayedArray
objects.
## [1] 4 9 16
kronecker_list calculates Kronecker product of multiple
DelayedArray
objects.
## [1] 4 9 16
Suppose a tensor \(A \in \Re ^{I \times J}\) and a tensor \(B \in \Re ^{K \times J}\).
Khatri-Rao product is defined as the column-wise Kronecker product and the dimensions of output tensor is \({IK \times J}\).
\[ A \odot B = \begin{bmatrix} a_{1} \otimes a_{1} & a_{2} \otimes a_{2} & \cdots & a_{J} \otimes a_{J} \\ \end{bmatrix} \]
Khatri-Rao product can only be used for 2D tensors (matrices).
khatri_rao calculates Khatri-Rao product of two DelayedArray
objects.
## [1] 4 3
khatri_rao_list calculates Khatri-Rao product of
multiple DelayedArray
objects.
## [1] 4 3
list_rep replicates an arbitrary number of any R
object.
## List of 3
## $ :Formal class 'RandomUnifArray' [package "DelayedRandomArray"] with 1 slot
## .. ..@ seed:Formal class 'RandomUnifArraySeed' [package "DelayedRandomArray"] with 6 slots
## .. .. .. ..@ min : num 0
## .. .. .. ..@ max : num 1
## .. .. .. ..@ dim : int [1:3] 2 3 4
## .. .. .. ..@ chunkdim: int [1:3] 2 3 4
## .. .. .. ..@ seeds :List of 1
## .. .. .. .. ..$ : int [1:2] 231895604 958095632
## .. .. .. ..@ sparse : logi FALSE
## $ :Formal class 'RandomUnifArray' [package "DelayedRandomArray"] with 1 slot
## .. ..@ seed:Formal class 'RandomUnifArraySeed' [package "DelayedRandomArray"] with 6 slots
## .. .. .. ..@ min : num 0
## .. .. .. ..@ max : num 1
## .. .. .. ..@ dim : int [1:3] 2 3 4
## .. .. .. ..@ chunkdim: int [1:3] 2 3 4
## .. .. .. ..@ seeds :List of 1
## .. .. .. .. ..$ : int [1:2] 231895604 958095632
## .. .. .. ..@ sparse : logi FALSE
## $ :Formal class 'RandomUnifArray' [package "DelayedRandomArray"] with 1 slot
## .. ..@ seed:Formal class 'RandomUnifArraySeed' [package "DelayedRandomArray"] with 6 slots
## .. .. .. ..@ min : num 0
## .. .. .. ..@ max : num 1
## .. .. .. ..@ dim : int [1:3] 2 3 4
## .. .. .. ..@ chunkdim: int [1:3] 2 3 4
## .. .. .. ..@ seeds :List of 1
## .. .. .. .. ..$ : int [1:2] 231895604 958095632
## .. .. .. ..@ sparse : logi FALSE
modebind_list collapses multiple DelayedArray
objects into single DelayedArray
object.
m specifies the collapsed dimension.
## [1] 4 3 4
## [1] 2 6 4
## [1] 2 3 8
rbind_list is the row-wise modebind_list
and collapses multiple 2D DelayedArray
objects into single DelayedArray
object.
## [1] 4 3
cbind_list is the column-wise modebind_list
and collapses multiple 2D DelayedArray
objects into single DelayedArray
object.
## [1] 2 6
## R version 4.5.1 (2025-06-13)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.3 LTS
##
## Matrix products: default
## BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so; 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=C
## [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: Etc/UTC
## tzcode source: system (glibc)
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] DelayedRandomArray_1.17.0 HDF5Array_1.37.0
## [3] h5mread_1.1.1 rhdf5_2.55.4
## [5] DelayedArray_0.37.0 SparseArray_1.11.1
## [7] S4Arrays_1.11.0 abind_1.4-8
## [9] IRanges_2.45.0 S4Vectors_0.49.0
## [11] MatrixGenerics_1.23.0 matrixStats_1.5.0
## [13] BiocGenerics_0.57.0 generics_0.1.4
## [15] Matrix_1.7-4 DelayedTensor_1.17.0
## [17] BiocStyle_2.39.0
##
## loaded via a namespace (and not attached):
## [1] dqrng_0.4.1 sass_0.4.10 lattice_0.22-7
## [4] digest_0.6.37 evaluate_1.0.5 grid_4.5.1
## [7] fastmap_1.2.0 jsonlite_2.0.0 BiocManager_1.30.26
## [10] codetools_0.2-20 jquerylib_0.1.4 cli_3.6.5
## [13] rlang_1.1.6 XVector_0.51.0 cachem_1.1.0
## [16] yaml_2.3.10 tools_4.5.1 beachmat_2.25.5
## [19] parallel_4.5.1 BiocParallel_1.45.0 einsum_0.1.2
## [22] Rhdf5lib_1.33.0 rsvd_1.0.5 buildtools_1.0.0
## [25] R6_2.6.1 lifecycle_1.0.4 BiocSingular_1.25.1
## [28] irlba_2.3.5.1 ScaledMatrix_1.19.0 rTensor_1.4.9
## [31] bslib_0.9.0 Rcpp_1.1.0 xfun_0.54
## [34] sys_3.4.3 knitr_1.50 rhdf5filters_1.23.0
## [37] htmltools_0.5.8.1 rmarkdown_2.30 maketools_1.3.2
## [40] compiler_4.5.1