# 2. Tensor arithmetic by DelayedTensor

Koki Tsuyuzaki1 and Itoshi Nikaido1*

1Laboratory for Bioinformatics Research, RIKEN Center for Biosystems Dynamics Research

*k.t.the-answer@hotmail.co.jp

#### 15 April 2025

#### Package

DelayedTensor 1.14.0

Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2025-04-15 15:16:31.229982
Compiled: Tue Apr 15 20:09:03 2025

# 1 Setting

    suppressPackageStartupMessages(library("DelayedTensor"))

    ## Warning: multiple methods tables found for 'kronecker'

    suppressPackageStartupMessages(library("DelayedArray"))
    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).

    DelayedTensor::setSparse(as.sparse=FALSE)

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).

    DelayedTensor::setVerbose(as.verbose=FALSE)

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`.

    setAutoBlockSize(size=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::getSparse()

    ## $delayedtensor.sparse
    ## [1] FALSE

    DelayedTensor::getVerbose()

    ## $delayedtensor.verbose
    ## [1] FALSE

    getAutoBlockSize()

    ## [1] 1e+08

    getHDF5DumpDir()

    ## [1] "E:/biocbuild/bbs-3.21-bioc/tmpdir/RtmpAj0mXo"

# 2 Tensor Arithmetic Operations

## 2.1 Unfold/Fold Operations

Unfold (a.k.a. matricizing) operations are used to reshape a tensor into a matrix.

Figure 1: Unfold/Fold Operasions

In `unfold`, `row_idx` and `col_idx` are specified to set which modes are used as the row/column.

    dmat1 <- DelayedTensor::unfold(darr1, row_idx=c(1,2), col_idx=3)
    dmat1

    ## <6 x 4> HDF5Matrix object of type "double":
    ##             [,1]        [,2]        [,3]        [,4]
    ## [1,] 0.272078626 0.992938249 0.651387919 0.937497646
    ## [2,] 0.808404365 0.758010132 0.548122090 0.914565134
    ## [3,] 0.837806182 0.539994433 0.920886425 0.597368544
    ## [4,] 0.204113832 0.818669382 0.013436593 0.802944523
    ## [5,] 0.459592781 0.498943449 0.301412657 0.556781983
    ## [6,] 0.009424502 0.754223996 0.300614066 0.079229992

`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.272078626 0.837806182 0.459592781
    ## [2,] 0.808404365 0.204113832 0.009424502
    ##
    ## ,,2
    ##           [,1]      [,2]      [,3]
    ## [1,] 0.9929382 0.5399944 0.4989434
    ## [2,] 0.7580101 0.8186694 0.7542240
    ##
    ## ,,3
    ##            [,1]       [,2]       [,3]
    ## [1,] 0.65138792 0.92088643 0.30141266
    ## [2,] 0.54812209 0.01343659 0.30061407
    ##
    ## ,,4
    ##            [,1]       [,2]       [,3]
    ## [1,] 0.93749765 0.59736854 0.55678198
    ## [2,] 0.91456513 0.80294452 0.07922999

    identical(as.array(darr1), as.array(dmat1_to_darr1))

    ## [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.

    dmatZ <- RandomUnifArray(c(10,4))
    DelayedTensor::ttm(darr1, dmatZ, m=3)

    ## <2 x 3 x 10> DelayedArray object of type "double":
    ## ,,1
    ##           [,1]      [,2]      [,3]
    ## [1,] 0.8500636 1.0164548 0.6584968
    ## [2,] 1.1196447 0.6103017 0.1393361
    ##
    ## ,,2
    ##           [,1]      [,2]      [,3]
    ## [1,] 0.9380648 0.8931252 0.4852106
    ## [2,] 0.8011017 0.4716701 0.5516365
    ##
    ## ,,3
    ##           [,1]      [,2]      [,3]
    ## [1,] 2.1266331 1.7867558 1.1844171
    ## [2,] 1.9388562 1.3905308 0.9783863
    ##
    ## ...
    ##
    ## ,,8
    ##           [,1]      [,2]      [,3]
    ## [1,] 0.7788718 0.5571730 0.4710654
    ## [2,] 0.7742842 0.6372172 0.1374566
    ##
    ## ,,9
    ##           [,1]      [,2]      [,3]
    ## [1,] 1.1430389 1.4531749 0.8744885
    ## [2,] 1.4695455 0.7560451 0.5278601
    ##
    ## ,,10
    ##          [,1]     [,2]     [,3]
    ## [1,] 1.603989 1.622329 1.107586
    ## [2,] 1.848362 1.176250 0.483372

`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,] 0.5700402 0.4818151 0.6206494   . 0.8990212 0.5917492
    ##  [2,] 0.3454471 0.3488374 0.3929501   . 0.5540800 0.4017506
    ##   ...         .         .         .   .         .         .
    ##  [9,] 0.4431244 0.2637647 0.4496671   . 0.6808005 0.3759303
    ## [10,] 0.8860218 1.1411541 1.0808246   . 1.4613103 1.2174549
    ##
    ## ...
    ##
    ## ,,4
    ##            [,1]      [,2]      [,3] ...      [,9]     [,10]
    ##  [1,] 0.9110291 0.9192931 1.0266617   . 1.4183625 1.0688671
    ##  [2,] 0.5412825 0.5622126 0.6178300   . 0.8593718 0.6439785
    ##   ...         .         .         .   .         .         .
    ##  [9,] 0.7292481 0.7046494 0.8065227   . 1.1028906 0.8382172
    ## [10,] 1.3414749 1.4641714 1.5658676   . 2.2034585 1.6354133

## 2.2 Vectorization

`vec` collapses a DelayedArray into a 1D DelayedArray (vector).

Figure 2: Vectorization

    DelayedTensor::vec(darr1)

    ## <24> HDF5Array object of type "double":
    ##        [1]        [2]        [3]          .       [23]       [24]
    ##  0.2720786  0.8084044  0.8378062          . 0.55678198 0.07922999

## 2.3 Norm Operations

`fnorm` calculates the Frobenius norm of a DelayedArray.

Figure 3: Norm Operations

    DelayedTensor::fnorm(darr1)

    ## [1] 3.132079

`innerProd` calculates the inner product value of two DelayedArray.

    DelayedTensor::innerProd(darr1, darr2)

    ## [1] 7.809336

## 2.4 Outer Product

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.

Figure 4: Outer Product

    DelayedTensor::outerProd(darr1[,,1], darr2[,,1])

    ## <2 x 3 x 2 x 3> HDF5Array object of type "double":
    ## ,,1,1
    ##             [,1]        [,2]        [,3]
    ## [1,] 0.261348845 0.804766185 0.441468131
    ## [2,] 0.776523868 0.196064333 0.009052834
    ##
    ## ,,2,1
    ##             [,1]        [,2]        [,3]
    ## [1,] 0.044525059 0.137105109 0.075211331
    ## [2,] 0.132293567 0.033402772 0.001542299
    ##
    ## ,,1,2
    ##             [,1]        [,2]        [,3]
    ## [1,] 0.143668701 0.442396112 0.242683885
    ## [2,] 0.426870744 0.107780496 0.004976524
    ##
    ## ,,2,2
    ##             [,1]        [,2]        [,3]
    ## [1,] 0.140525122 0.432716153 0.237373780
    ## [2,] 0.417530491 0.105422178 0.004867634
    ##
    ## ,,1,3
    ##             [,1]        [,2]        [,3]
    ## [1,] 0.202008726 0.622041365 0.341231334
    ## [2,] 0.600211559 0.151547279 0.006997358
    ##
    ## ,,2,3
    ##             [,1]        [,2]        [,3]
    ## [1,] 0.159018538 0.489662553 0.268612693
    ## [2,] 0.472478425 0.119295969 0.005508226

## 2.5 Diagonal Operations

Using `DelayedDiagonalArray`, we can originally create a diagonal DelayedArray by specifying the dimensions (modes) and the values.

Figure 5: Diagonal Operations

    dgdarr <- DelayedTensor::DelayedDiagonalArray(c(5,6,7), 1:5)
    dgdarr

    ## <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.

    DelayedTensor::diag(dgdarr)

    ## <5> DelayedArray object of type "integer":
    ## [1] [2] [3] [4] [5]
    ##   1   2   3   4   5

    DelayedTensor::diag(dgdarr) <- c(1111, 2222, 3333, 4444, 5555)
    DelayedTensor::diag(dgdarr)

    ## <5> DelayedArray object of type "double":
    ##  [1]  [2]  [3]  [4]  [5]
    ## 1111 2222 3333 4444 5555

## 2.6 Mode-wise Operations

`modeSum` calculates the summation for a given mode `m` of a DelayedArray. The mode specified as `m` is collapsed into 1D as follows.

Figure 6: Mode-wise Operations

    DelayedTensor::modeSum(darr1, m=1)

    ## <1 x 3 x 4> DelayedArray object of type "double":
    ## ,,1
    ##           [,1]      [,2]      [,3]
    ## [1,] 1.0804830 1.0419200 0.4690173
    ##
    ## ,,2
    ##          [,1]     [,2]     [,3]
    ## [1,] 1.750948 1.358664 1.253167
    ##
    ## ,,3
    ##           [,1]      [,2]      [,3]
    ## [1,] 1.1995100 0.9343230 0.6020267
    ##
    ## ,,4
    ##          [,1]     [,2]     [,3]
    ## [1,] 1.852063 1.400313 0.636012

    DelayedTensor::modeSum(darr1, m=2)

    ## <2 x 1 x 4> DelayedArray object of type "double":
    ## ,,1
    ##          [,1]
    ## [1,] 1.569478
    ## [2,] 1.021943
    ##
    ## ,,2
    ##          [,1]
    ## [1,] 2.031876
    ## [2,] 2.330904
    ##
    ## ,,3
    ##           [,1]
    ## [1,] 1.8736870
    ## [2,] 0.8621727
    ##
    ## ,,4
    ##          [,1]
    ## [1,] 2.091648
    ## [2,] 1.796740

    DelayedTensor::modeSum(darr1, m=3)

    ## <2 x 3 x 1> DelayedArray object of type "double":
    ## ,,1
    ##          [,1]     [,2]     [,3]
    ## [1,] 2.853902 2.896056 1.816731
    ## [2,] 3.029102 1.839164 1.143493

Similar to `modeSum`, `modeMean` calculates the average value for a given mode `m` of a DelayedArray.

    DelayedTensor::modeMean(darr1, m=1)

    ## <1 x 3 x 4> DelayedArray object of type "double":
    ## ,,1
    ##           [,1]      [,2]      [,3]
    ## [1,] 0.5402415 0.5209600 0.2345086
    ##
    ## ,,2
    ##           [,1]      [,2]      [,3]
    ## [1,] 0.8754742 0.6793319 0.6265837
    ##
    ## ,,3
    ##           [,1]      [,2]      [,3]
    ## [1,] 0.5997550 0.4671615 0.3010134
    ##
    ## ,,4
    ##           [,1]      [,2]      [,3]
    ## [1,] 0.9260314 0.7001565 0.3180060

    DelayedTensor::modeMean(darr1, m=2)

    ## <2 x 1 x 4> DelayedArray object of type "double":
    ## ,,1
    ##           [,1]
    ## [1,] 0.5231592
    ## [2,] 0.3406476
    ##
    ## ,,2
    ##           [,1]
    ## [1,] 0.6772920
    ## [2,] 0.7769678
    ##
    ## ,,3
    ##           [,1]
    ## [1,] 0.6245623
    ## [2,] 0.2873909
    ##
    ## ,,4
    ##           [,1]
    ## [1,] 0.6972161
    ## [2,] 0.5989132

    DelayedTensor::modeMean(darr1, m=3)

    ## <2 x 3 x 1> DelayedArray object of type "double":
    ## ,,1
    ##           [,1]      [,2]      [,3]
    ## [1,] 0.7134756 0.7240139 0.4541827
    ## [2,] 0.7572754 0.4597911 0.2858731

## 2.7 Tensor Product Operations

There are some tensor specific product such as Hadamard product, Kronecker product, and Khatri-Rao product.

### 2.7.1 Hadamard 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\).

Figure 7: Hadamard Product

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.

    prod_h <- DelayedTensor::hadamard(darr1, darr2)
    dim(prod_h)

    ## [1] 2 3 4

`hadamard_list` calculates Hadamard product of multiple DelayedArray objects.

    prod_hl <- DelayedTensor::hadamard_list(list(darr1, darr2))
    dim(prod_hl)

    ## [1] 2 3 4

### 2.7.2 Kronecker Product

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}\).

Figure 8: Kronecker Product

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.

    prod_kron <- DelayedTensor::kronecker(darr1, darr2)
    dim(prod_kron)

    ## [1]  4  9 16

`kronecker_list` calculates Kronecker product of multiple DelayedArray objects.

    prod_kronl <- DelayedTensor::kronecker_list(list(darr1, darr2))
    dim(prod_kronl)

    ## [1]  4  9 16

### 2.7.3 Khatri-Rao Product

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} \]

Figure 9: Khatri-Rao Product

Khatri-Rao product can only be used for 2D tensors (matrices).

`khatri_rao` calculates Khatri-Rao product of two DelayedArray objects.

    prod_kr <- DelayedTensor::khatri_rao(darr1[,,1], darr2[,,1])
    dim(prod_kr)

    ## [1] 4 3

`khatri_rao_list` calculates Khatri-Rao product of multiple DelayedArray objects.

    prod_krl <- DelayedTensor::khatri_rao_list(list(darr1[,,1], darr2[,,1]))
    dim(prod_krl)

    ## [1] 4 3

## 2.8 Utilities Functions

`list_rep` replicates an arbitrary number of any R object.

    str(DelayedTensor::list_rep(darr1, 3))

    ## 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] -699045550 -1867578555
    ##   .. .. .. ..@ 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] -699045550 -1867578555
    ##   .. .. .. ..@ 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] -699045550 -1867578555
    ##   .. .. .. ..@ sparse  : logi FALSE

### 2.8.1 Bind Operations

`modebind_list` collapses multiple DelayedArray objects into single DelayedArray object.

`m` specifies the collapsed dimension.

Figure 10: Bind Operations

    dim(DelayedTensor::modebind_list(list(darr1, darr2), m=1))

    ## [1] 4 3 4

    dim(DelayedTensor::modebind_list(list(darr1, darr2), m=2))

    ## [1] 2 6 4

    dim(DelayedTensor::modebind_list(list(darr1, darr2), m=3))

    ## [1] 2 3 8

`rbind_list` is the row-wise `modebind_list` and collapses multiple 2D DelayedArray objects into single DelayedArray object.

    dim(DelayedTensor::rbind_list(list(darr1[,,1], darr2[,,1])))

    ## [1] 4 3

`cbind_list` is the column-wise `modebind_list` and collapses multiple 2D DelayedArray objects into single DelayedArray object.

    dim(DelayedTensor::cbind_list(list(darr1[,,1], darr2[,,1])))

    ## [1] 2 6

# Session information

    ## R version 4.5.0 RC (2025-04-04 r88126 ucrt)
    ## Platform: x86_64-w64-mingw32/x64
    ## Running under: Windows Server 2022 x64 (build 20348)
    ##
    ## Matrix products: default
    ##   LAPACK version 3.12.1
    ##
    ## locale:
    ## [1] LC_COLLATE=C
    ## [2] LC_CTYPE=English_United States.utf8
    ## [3] LC_MONETARY=English_United States.utf8
    ## [4] LC_NUMERIC=C
    ## [5] LC_TIME=English_United States.utf8
    ##
    ## 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] DelayedRandomArray_1.16.0 HDF5Array_1.36.0
    ##  [3] h5mread_1.0.0             rhdf5_2.52.0
    ##  [5] DelayedArray_0.34.0       SparseArray_1.8.0
    ##  [7] S4Arrays_1.8.0            abind_1.4-8
    ##  [9] IRanges_2.42.0            S4Vectors_0.46.0
    ## [11] MatrixGenerics_1.20.0     matrixStats_1.5.0
    ## [13] BiocGenerics_0.54.0       generics_0.1.3
    ## [15] Matrix_1.7-3              DelayedTensor_1.14.0
    ## [17] BiocStyle_2.36.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.3      grid_4.5.0
    ##  [7] bookdown_0.43       fastmap_1.2.0       jsonlite_2.0.0
    ## [10] BiocManager_1.30.25 codetools_0.2-20    jquerylib_0.1.4
    ## [13] cli_3.6.4           rlang_1.1.6         crayon_1.5.3
    ## [16] XVector_0.48.0      cachem_1.1.0        yaml_2.3.10
    ## [19] tools_4.5.0         beachmat_2.24.0     parallel_4.5.0
    ## [22] BiocParallel_1.42.0 einsum_0.1.2        Rhdf5lib_1.30.0
    ## [25] rsvd_1.0.5          R6_2.6.1            lifecycle_1.0.4
    ## [28] BiocSingular_1.24.0 irlba_2.3.5.1       ScaledMatrix_1.16.0
    ## [31] rTensor_1.4.8       bslib_0.9.0         Rcpp_1.0.14
    ## [34] xfun_0.52           knitr_1.50          rhdf5filters_1.20.0
    ## [37] htmltools_0.5.8.1   rmarkdown_2.29      compiler_4.5.0