4. Einsum operation by DelayedTensor

Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2025-11-02 14:46:33.503126
Compiled: Sun Nov 2 14:49:55 2025

What is einsum

einsum is an easy and intuitive way to write tensor operations.

It was originally introduced by Numpy1 package of Python but similar tools have been implemented in other languages (e.g. R, Julia) inspired by Numpy. In this vignette, we will use CRAN einsum package first.

einsum is named after Einstein summation2 introduced by Albert Einstein, which is a notational convention that implies summation over a set of indexed terms in a formula.

Here, we consider a simple example of einsum; matrix multiplication. If we naively implement the matrix multiplication, the calculation would look like the following in a for loop.

A <- matrix(runif(3*4), nrow=3, ncol=4)
B <- matrix(runif(4*5), nrow=4, ncol=5)
C <- matrix(0, nrow=3, ncol=5)

I <- nrow(A)
J <- ncol(A)
K <- ncol(B)

for(i in 1:I){
  for(j in 1:J){
    for(k in 1:K){
      C[i,k] = C[i,k] + A[i,j] * B[j,k]
    }
  }
}

Therefore, any programming language can implement this. However, when analyzing tensor data, such operations tend to be more complicated and increase the possibility of causing bugs because the order of tensors is larger or more tensors are handled simultaneously. In addition, several programming languages, especially R, are known to significantly slow down the speed of computation if the code is written in for loop.

Obviously, in the case of the R language, it should be executed using the built-in matrix multiplication function (%*%) prepared by the R, as shown below.

C <- A %*% B

However, more complex operations than matrix multiplication are not always provided by programming languages as standard.

einsum is a function that solves such a problem. To put it simply, einsum is a wrapper for the for loop above. Like the Einstein summation, it omits many notations such as for, array size (e.g. I, J, and K), brackets (e.g. {}, (), and []), and even addition operator (+) and extracts the array subscripts (e.g. i, j, and k) to concisely express the tensor operation as follows.

suppressPackageStartupMessages(library("einsum"))
C <- einsum('ij,jk->ik', A, B)

Einsum of DelayedTensor

CRAN einsum is easy to use because the syntax is almost the same as that of Numpy‘s einsum, except that it prohibits the implicit modes that do not use’->’. It is extremely fast because the internal calculation is actually performed by C++. When the input tensor is huge, however, it is not scalable because it assumes that the input is R’s standard array.

Using einsum of DelayedTensor, we can augment the CRAN einsum’s functionality; in DelayedTensor, the input DelayedArray objects are divided into multiple block tensors and the CRAN einsum is incremently applied in the block processing.

Typical operations of einsum

A surprisingly large number of tensor operations can be handled uniformly in einsum.

In more detail, einsum is capable of performing any tensor operation that can be described by a combination of the following three operations3.

  1. Multiplication: the element values of the tensors on the left side of -> are multiplied by each other
  2. Summation: when comparing the left and right sides of ->, if there is a missing subscript on the right, the summation is done in the direction of the subscript
  3. Permutation: the subscripts to the right of -> can be rearranged in any order

Some typical operations are introduced below. Here we use the arrays and DelayedArray objects below.

suppressPackageStartupMessages(library("DelayedTensor"))
suppressPackageStartupMessages(library("DelayedArray"))

arrA <- array(runif(3), dim=c(3))
arrB <- array(runif(3*3), dim=c(3,3))
arrC <- array(runif(3*4), dim=c(3,4))
arrD <- array(runif(3*3*3), dim=c(3,3,3))
arrE <- array(runif(3*4*5), dim=c(3,4,5))

darrA <- DelayedArray(arrA)
darrB <- DelayedArray(arrB)
darrC <- DelayedArray(arrC)
darrD <- DelayedArray(arrD)
darrE <- DelayedArray(arrE)

No Operation

diag

We can also extract the diagonal elements as follows.

einsum::einsum('ii->i', arrB)
## [1] 0.7704871 0.1410138 0.1775558
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.7704871 0.1410138 0.1775558
einsum::einsum('iii->i', arrD)
## [1] 0.0757876 0.7473744 0.1300598
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.0757876 0.7473744 0.1300598

Multiplication

By using multiple arrays or DelayedArray objects as input and writing “,” on the right side of ->, multiplication will be performed.

Hadamard Product

Hadamard Product can also be implemented in einsum, multiplying by the product of each element.

einsum::einsum('i,i->i', arrA, arrA)
## [1] 0.9615068 0.6182858 0.6054507
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.9615068 0.6182858 0.6054507
einsum::einsum('ij,ij->ij', arrC, arrC)
##            [,1]      [,2]       [,3]      [,4]
## [1,] 0.01748867 0.3972445 0.27477460 0.9568725
## [2,] 0.52514101 0.1134319 0.06240549 0.1550772
## [3,] 0.14230491 0.3573611 0.87853452 0.1265719
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01748867 0.39724448 0.27477460 0.95687252
## [2,] 0.52514101 0.11343190 0.06240549 0.15507724
## [3,] 0.14230491 0.35736114 0.87853452 0.12657193
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
## 
##            [,1]         [,2]       [,3]         [,4]
## [1,] 0.22021441 0.8631826527 0.11991001 9.874849e-05
## [2,] 0.01244374 0.0006919546 0.75810189 4.419707e-01
## [3,] 0.35483421 0.0138178516 0.01336396 2.844564e-03
## 
## , , 2
## 
##              [,1]      [,2]        [,3]         [,4]
## [1,] 8.106745e-03 0.1912599 0.002001539 0.2928500346
## [2,] 2.464781e-02 0.0204093 0.533022372 0.0001776202
## [3,] 1.217855e-05 0.9650184 0.947125721 0.8789912184
## 
## , , 3
## 
##            [,1]        [,2]      [,3]       [,4]
## [1,] 0.03018505 0.006736384 0.5463655 0.12101693
## [2,] 0.39840213 0.393340917 0.3567100 0.96480369
## [3,] 0.70628284 0.999656107 0.2851632 0.05662554
## 
## , , 4
## 
##            [,1]      [,2]       [,3]         [,4]
## [1,] 0.30473777 0.1571415 0.00311275 8.407881e-05
## [2,] 0.59036024 0.5000883 0.25291061 7.915905e-01
## [3,] 0.05863888 0.2589764 0.44607089 4.438236e-01
## 
## , , 5
## 
##            [,1]        [,2]       [,3]       [,4]
## [1,] 0.07378493 0.004364925 0.03617858 0.06221855
## [2,] 0.34172212 0.964778111 0.02606606 0.24687948
## [3,] 0.81056571 0.600394688 0.69806593 0.06786983
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
##              [,1]         [,2]         [,3]         [,4]
## [1,] 2.202144e-01 8.631827e-01 1.199100e-01 9.874849e-05
## [2,] 1.244374e-02 6.919546e-04 7.581019e-01 4.419707e-01
## [3,] 3.548342e-01 1.381785e-02 1.336396e-02 2.844564e-03
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]
## [1,] 8.106745e-03 1.912599e-01 2.001539e-03 2.928500e-01
## [2,] 2.464781e-02 2.040930e-02 5.330224e-01 1.776202e-04
## [3,] 1.217855e-05 9.650184e-01 9.471257e-01 8.789912e-01
## 
## ,,3
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.030185052 0.006736384 0.546365550 0.121016926
## [2,] 0.398402125 0.393340917 0.356709960 0.964803692
## [3,] 0.706282836 0.999656107 0.285163213 0.056625544
## 
## ,,4
##              [,1]         [,2]         [,3]         [,4]
## [1,] 3.047378e-01 1.571415e-01 3.112750e-03 8.407881e-05
## [2,] 5.903602e-01 5.000883e-01 2.529106e-01 7.915905e-01
## [3,] 5.863888e-02 2.589764e-01 4.460709e-01 4.438236e-01
## 
## ,,5
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.073784926 0.004364925 0.036178583 0.062218546
## [2,] 0.341722122 0.964778111 0.026066056 0.246879475
## [3,] 0.810565709 0.600394688 0.698065932 0.067869829

Outer Product

The outer product can also be implemented in einsum, in which the subscripts in the input array are all different, and all of them are kept.

einsum::einsum('i,j->ij', arrA, arrA)
##           [,1]      [,2]      [,3]
## [1,] 0.9615068 0.7710292 0.7629843
## [2,] 0.7710292 0.6182858 0.6118346
## [3,] 0.7629843 0.6118346 0.6054507
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 0.9615068 0.7710292 0.7629843
## [2,] 0.7710292 0.6182858 0.6118346
## [3,] 0.7629843 0.6118346 0.6054507
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.0620585 0.2957684 0.2459864 0.4590393
## [2,] 0.3400641 0.1580485 0.1172288 0.1847978
## [3,] 0.1770243 0.2805282 0.4398477 0.1669520
## 
## , , 2, 1, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01475210 0.07030794 0.05847412 0.10911952
## [2,] 0.08083760 0.03757016 0.02786678 0.04392881
## [3,] 0.04208093 0.06668514 0.10455741 0.03968662
## 
## , , 3, 1, 1
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.07877549 0.3754410 0.3122490 0.5826930
## [2,] 0.43166885 0.2006228 0.1488073 0.2345777
## [3,] 0.22471015 0.3560954 0.5583315 0.2119246
## 
## , , 1, 2, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1228654 0.5855720 0.4870120 0.9088211
## [2,] 0.6732701 0.3129096 0.2320934 0.3658688
## [3,] 0.3504784 0.5553989 0.8708248 0.3305370
## 
## , , 2, 2, 1
## 
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.003478702 0.016579359 0.013788820 0.025731544
## [2,] 0.019062365 0.008859443 0.006571284 0.010358881
## [3,] 0.009923132 0.015725066 0.024655750 0.009358527
## 
## , , 3, 2, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01554528 0.07408823 0.06161814 0.11498662
## [2,] 0.08518404 0.03959021 0.02936511 0.04629076
## [3,] 0.04434352 0.07027064 0.11017922 0.04182047
## 
## , , 1, 3, 1
## 
##            [,1]      [,2]       [,3]      [,4]
## [1,] 0.04579374 0.2182512 0.18151646 0.3387309
## [2,] 0.25093757 0.1166260 0.08650458 0.1363646
## [3,] 0.13062842 0.2070053 0.32456907 0.1231959
## 
## , , 2, 3, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1151442 0.5487730 0.4564068 0.8517082
## [2,] 0.6309599 0.2932455 0.2175080 0.3428766
## [3,] 0.3284534 0.5204961 0.8160997 0.3097651
## 
## , , 3, 3, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01528783 0.07286123 0.06059766 0.11308229
## [2,] 0.08377328 0.03893455 0.02887879 0.04552412
## [3,] 0.04360914 0.06910687 0.10835450 0.04112787
## 
## , , 1, 4, 1
## 
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.001314146 0.006263170 0.005208990 0.009720582
## [2,] 0.007201172 0.003346824 0.002482428 0.003913265
## [3,] 0.003748652 0.005940444 0.009314180 0.003535362
## 
## , , 2, 4, 1
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.08791746 0.4190112 0.3484858 0.6503150
## [2,] 0.48176440 0.2239053 0.1660765 0.2618007
## [3,] 0.25078796 0.3974206 0.6231264 0.2365187
## 
## , , 3, 4, 1
## 
##             [,1]       [,2]       [,3]       [,4]
## [1,] 0.007053201 0.03361529 0.02795736 0.05217169
## [2,] 0.038649675 0.01796286 0.01332353 0.02100303
## [3,] 0.020119530 0.03188317 0.04999048 0.01897477
## 
## , , 1, 1, 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01190698 0.05674821 0.04719669 0.08807452
## [2,] 0.06524710 0.03032431 0.02249234 0.03545662
## [3,] 0.03396512 0.05382412 0.08439227 0.03203258
## 
## , , 2, 1, 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02076192 0.09895052 0.08229576 0.15357347
## [2,] 0.11376983 0.05287578 0.03921936 0.06182487
## [3,] 0.05922419 0.09385185 0.14715281 0.05585446
## 
## , , 3, 1, 2
## 
##              [,1]        [,2]         [,3]        [,4]
## [1,] 0.0004615047 0.002199514 0.0018293047 0.003413696
## [2,] 0.0025289237 0.001175345 0.0008717845 0.001374269
## [3,] 0.0013164601 0.002086178 0.0032709746 0.001241556
## 
## , , 1, 2, 2
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.05783494 0.2756391 0.2292452 0.4277982
## [2,] 0.31692019 0.1472921 0.1092505 0.1722209
## [3,] 0.16497642 0.2614361 0.4099127 0.1555896
## 
## , , 2, 2, 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01889263 0.09004156 0.07488629 0.13974655
## [2,] 0.10352662 0.04811513 0.03568827 0.05625849
## [3,] 0.05389196 0.08540194 0.13390397 0.05082563
## 
## , , 3, 2, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1299111 0.6191512 0.5149394 0.9609368
## [2,] 0.7118783 0.3308532 0.2454026 0.3868493
## [3,] 0.3705764 0.5872479 0.9207616 0.3494914
## 
## , , 1, 3, 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.00591644 0.02819753 0.02345149 0.04376320
## [2,] 0.03242052 0.01506779 0.01117618 0.01761798
## [3,] 0.01687687 0.02674458 0.04193354 0.01591662
## 
## , , 2, 3, 2
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.09654974 0.4601524 0.3827022 0.7141670
## [2,] 0.52906702 0.2458897 0.1823829 0.2875059
## [3,] 0.27541187 0.4364418 0.6843088 0.2597415
## 
## , , 3, 3, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1287011 0.6133844 0.5101432 0.9519866
## [2,] 0.7052479 0.3277717 0.2431169 0.3832462
## [3,] 0.3671248 0.5817782 0.9121856 0.3462362
## 
## , , 1, 4, 2
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.07156506 0.3410763 0.2836684 0.5293582
## [2,] 0.39215758 0.1822595 0.1351867 0.2131065
## [3,] 0.20414210 0.3235015 0.5072266 0.1925269
## 
## , , 2, 4, 2
## 
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.001762482 0.008399920 0.006986095 0.013036868
## [2,] 0.009657933 0.004488630 0.003329336 0.005248319
## [3,] 0.005027547 0.007967093 0.012491818 0.004741491
## 
## , , 3, 4, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1239854 0.5909098 0.4914514 0.9171055
## [2,] 0.6794073 0.3157620 0.2342090 0.3692039
## [3,] 0.3536732 0.5604617 0.8787628 0.3335500
## 
## , , 1, 1, 3
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02297600 0.10950272 0.09107187 0.16995072
## [2,] 0.12590238 0.05851451 0.04340176 0.06841794
## [3,] 0.06553992 0.10386031 0.16284536 0.06181084
## 
## , , 2, 1, 3
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.08347169 0.3978229 0.3308637 0.6174302
## [2,] 0.45740277 0.2125829 0.1576784 0.2485621
## [3,] 0.23810623 0.3773240 0.5916164 0.2245585
## 
## , , 3, 1, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1111393 0.5296857 0.4405322 0.8220843
## [2,] 0.6090140 0.2830459 0.2099427 0.3309507
## [3,] 0.3170292 0.5023923 0.7877143 0.2989909
## 
## , , 1, 2, 3
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01085405 0.05172999 0.04302310 0.08028612
## [2,] 0.05947732 0.02764274 0.02050335 0.03232120
## [3,] 0.03096160 0.04906447 0.07692949 0.02919995
## 
## , , 2, 2, 3
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.08293979 0.3952879 0.3287554 0.6134958
## [2,] 0.45448812 0.2112283 0.1566737 0.2469782
## [3,] 0.23658898 0.3749197 0.5878466 0.2231276
## 
## , , 3, 2, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1322220 0.6301650 0.5240993 0.9780304
## [2,] 0.7245415 0.3367386 0.2497680 0.3937308
## [3,] 0.3771684 0.5976941 0.9371405 0.3557083
## 
## , , 1, 3, 3
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.09775074 0.4658763 0.3874627 0.7230506
## [2,] 0.53564817 0.2489483 0.1846516 0.2910822
## [3,] 0.27883777 0.4418708 0.6928210 0.2629725
## 
## , , 2, 3, 3
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.07898343 0.3764320 0.3130732 0.5842311
## [2,] 0.43280831 0.2011524 0.1492001 0.2351969
## [3,] 0.22530330 0.3570354 0.5598053 0.2124840
## 
## , , 3, 3, 3
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.07061958 0.3365702 0.2799207 0.5223647
## [2,] 0.38697661 0.1798516 0.1334007 0.2102910
## [3,] 0.20144509 0.3192276 0.5005254 0.1899833
## 
## , , 1, 4, 3
## 
##            [,1]      [,2]       [,3]      [,4]
## [1,] 0.04600462 0.2192563 0.18235234 0.3402907
## [2,] 0.25209314 0.1171630 0.08690294 0.1369926
## [3,] 0.13122996 0.2079585 0.32606371 0.1237633
## 
## , , 2, 4, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1298966 0.6190823 0.5148821 0.9608299
## [2,] 0.7117991 0.3308164 0.2453753 0.3868063
## [3,] 0.3705352 0.5871826 0.9206592 0.3494525
## 
## , , 3, 4, 3
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.03146912 0.14998061 0.12473677 0.23277334
## [2,] 0.17244244 0.08014451 0.05944531 0.09370877
## [3,] 0.08976688 0.14225248 0.22304146 0.08465934
## 
## , , 1, 1, 4
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.07300314 0.3479302 0.2893686 0.5399956
## [2,] 0.40003788 0.1859220 0.1379033 0.2173888
## [3,] 0.20824428 0.3300022 0.5174192 0.1963956
## 
## , , 2, 1, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1016101 0.4842699 0.4027605 0.7515980
## [2,] 0.5567965 0.2587773 0.1919420 0.3025747
## [3,] 0.2898468 0.4593167 0.7201749 0.2733551
## 
## , , 3, 1, 4
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.03202368 0.15262363 0.12693493 0.23687536
## [2,] 0.17548129 0.08155685 0.06049288 0.09536014
## [3,] 0.09134879 0.14475931 0.22697199 0.08615124
## 
## , , 1, 2, 4
## 
##            [,1]      [,2]       [,3]      [,4]
## [1,] 0.05242323 0.2498471 0.20779433 0.3877684
## [2,] 0.28726544 0.1335098 0.09902773 0.1561059
## [3,] 0.14953930 0.2369731 0.37155647 0.1410308
## 
## , , 2, 2, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.0935194 0.4457099 0.3706906 0.6917519
## [2,] 0.5124616 0.2381721 0.1766586 0.2784821
## [3,] 0.2667677 0.4227436 0.6628309 0.2515892
## 
## , , 3, 2, 4
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.06729899 0.3207444 0.2667586 0.4978026
## [2,] 0.36878063 0.1713948 0.1271281 0.2004030
## [3,] 0.19197297 0.3042172 0.4769903 0.1810501
## 
## , , 1, 3, 4
## 
##             [,1]       [,2]       [,3]       [,4]
## [1,] 0.007378202 0.03516423 0.02924559 0.05457568
## [2,] 0.040430593 0.01879056 0.01393746 0.02197082
## [3,] 0.021046607 0.03335230 0.05229396 0.01984910
## 
## , , 2, 3, 4
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.06650617 0.3169658 0.2636160 0.4919382
## [2,] 0.36443619 0.1693757 0.1256305 0.1980421
## [3,] 0.18971142 0.3006334 0.4713711 0.1789173
## 
## , , 3, 3, 4
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.08832432 0.4209504 0.3500985 0.6533246
## [2,] 0.48399392 0.2249415 0.1668451 0.2630122
## [3,] 0.25194856 0.3992598 0.6260101 0.2376132
## 
## , , 1, 4, 4
## 
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.001212611 0.005779260 0.004806529 0.008969543
## [2,] 0.006644790 0.003088239 0.002290629 0.003610915
## [3,] 0.003459021 0.005481469 0.008594541 0.003262210
## 
## , , 2, 4, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1176599 0.5607628 0.4663785 0.8703167
## [2,] 0.6447454 0.2996525 0.2222602 0.3503679
## [3,] 0.3356296 0.5318681 0.8339302 0.3165330
## 
## , , 3, 4, 4
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.08810155 0.4198886 0.3492155 0.6516768
## [2,] 0.48277321 0.2243741 0.1664242 0.2623489
## [3,] 0.25131310 0.3982528 0.6244312 0.2370139
## 
## , , 1, 1, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.03592214 0.17120355 0.14238758 0.26571181
## [2,] 0.19684382 0.09148532 0.06785709 0.10696898
## [3,] 0.10246930 0.16238185 0.25460284 0.09663902
## 
## , , 2, 1, 5
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.07730631 0.3684389 0.3064255 0.5718256
## [2,] 0.42361811 0.1968812 0.1460320 0.2302028
## [3,] 0.22051924 0.3494542 0.5479185 0.2079722
## 
## , , 3, 1, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1190618 0.5674441 0.4719352 0.8806861
## [2,] 0.6524272 0.3032227 0.2249083 0.3545424
## [3,] 0.3396284 0.5382051 0.8438661 0.3203043
## 
## , , 1, 2, 5
## 
##             [,1]       [,2]       [,3]       [,4]
## [1,] 0.008737089 0.04164063 0.03463193 0.06462721
## [2,] 0.047876936 0.02225133 0.01650440 0.02601731
## [3,] 0.024922886 0.03949499 0.06192525 0.02350483
## 
## , , 2, 2, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1298949 0.6190741 0.5148752 0.9608172
## [2,] 0.7117897 0.3308120 0.2453721 0.3868011
## [3,] 0.3705302 0.5871748 0.9206470 0.3494479
## 
## , , 3, 2, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1024700 0.4883682 0.4061689 0.7579586
## [2,] 0.5615086 0.2609673 0.1935663 0.3051353
## [3,] 0.2922997 0.4632038 0.7262695 0.2756685
## 
## , , 1, 3, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02515383 0.11988220 0.09970434 0.18605991
## [2,] 0.13783634 0.06406095 0.04751571 0.07490310
## [3,] 0.07175228 0.11370497 0.17828105 0.06766973
## 
## , , 2, 3, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02135089 0.10175754 0.08463031 0.15793003
## [2,] 0.11699724 0.05437575 0.04033194 0.06357871
## [3,] 0.06090425 0.09651423 0.15132723 0.05743893
## 
## , , 3, 3, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1104909 0.5265955 0.4379621 0.8172883
## [2,] 0.6054610 0.2813946 0.2087179 0.3290200
## [3,] 0.3151796 0.4994613 0.7831188 0.2972466
## 
## , , 1, 4, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.03298666 0.15721315 0.13075196 0.24399840
## [2,] 0.18075815 0.08400933 0.06231195 0.09822770
## [3,] 0.09409572 0.14911234 0.23379722 0.08874188
## 
## , , 2, 4, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.0657084 0.3131637 0.2604539 0.4860372
## [2,] 0.3600646 0.1673440 0.1241235 0.1956665
## [3,] 0.1874358 0.2970272 0.4657168 0.1767711
## 
## , , 3, 4, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.03445218 0.16419779 0.13656099 0.25483872
## [2,] 0.18878885 0.08774169 0.06508034 0.10259174
## [3,] 0.09827619 0.15573709 0.24418433 0.09268449
DelayedTensor::einsum('ij,klm->ijklm', darrC, darrE)
## <3 x 4 x 3 x 4 x 5> HDF5Array object of type "double":
## ,,1,1,1
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.0620585 0.2957684 0.2459864 0.4590393
## [2,] 0.3400641 0.1580485 0.1172288 0.1847978
## [3,] 0.1770243 0.2805282 0.4398477 0.1669520
## 
## ,,2,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01475210 0.07030794 0.05847412 0.10911952
## [2,] 0.08083760 0.03757016 0.02786678 0.04392881
## [3,] 0.04208093 0.06668514 0.10455741 0.03968662
## 
## ,,3,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.07877549 0.37544098 0.31224898 0.58269297
## [2,] 0.43166885 0.20062282 0.14880727 0.23457773
## [3,] 0.22471015 0.35609543 0.55833153 0.21192463
## 
## ...
## 
## ,,1,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.03298666 0.15721315 0.13075196 0.24399840
## [2,] 0.18075815 0.08400933 0.06231195 0.09822770
## [3,] 0.09409572 0.14911234 0.23379722 0.08874188
## 
## ,,2,4,5
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.0657084 0.3131637 0.2604539 0.4860372
## [2,] 0.3600646 0.1673440 0.1241235 0.1956665
## [3,] 0.1874358 0.2970272 0.4657168 0.1767711
## 
## ,,3,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.03445218 0.16419779 0.13656099 0.25483872
## [2,] 0.18878885 0.08774169 0.06508034 0.10259174
## [3,] 0.09827619 0.15573709 0.24418433 0.09268449

Summation

If there is a vanishing subscript on the left or right side of ->, the summation is done for that subscript.

einsum::einsum('i->', arrA)
## [1] 2.544983
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
##      [1] 
## 2.544983
einsum::einsum('ij->', arrC)
## [1] 6.23808
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
##     [1] 
## 6.23808
einsum::einsum('ijk->', arrE)
## [1] 27.89645
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
##      [1] 
## 27.89645

Row-wise / Column-wise Summation

einsum::einsum('ij->i', arrC)
## [1] 2.264906 1.705072 2.268101
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
##      [1]      [2]      [3] 
## 2.264906 1.705072 2.268101
einsum::einsum('ij->j', arrC)
## [1] 1.234144 1.564867 1.711302 1.727767
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 1.234144 1.564867 1.711302 1.727767

Mode-wise Vectorization

einsum::einsum('ijk->i', arrE)
## [1]  6.00143 10.64774 11.24729
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
##      [1]      [2]      [3] 
##  6.00143 10.64774 11.24729
einsum::einsum('ijk->j', arrE)
## [1] 6.391413 7.780178 7.364763 6.360098
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 6.391413 7.780178 7.364763 6.360098
einsum::einsum('ijk->k', arrE)
## [1] 4.310086 5.053126 6.792913 5.966675 5.773653
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4]      [5] 
## 4.310086 5.053126 6.792913 5.966675 5.773653

Mode-wise Summation

These are the same as what the modeSum function does.

einsum::einsum('ijk->ij', arrE)
##          [,1]     [,2]     [,3]     [,4]
## [1,] 1.556710 1.910963 1.376183 1.157574
## [2,] 2.252658 2.485735 2.862378 3.046964
## [3,] 2.582045 3.383480 3.126202 2.155560
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]     [,4]
## [1,] 1.556710 1.910963 1.376183 1.157574
## [2,] 2.252658 2.485735 2.862378 3.046964
## [3,] 2.582045 3.383480 3.126202 2.155560
einsum::einsum('ijk->jk', arrE)
##           [,1]      [,2]     [,3]     [,4]     [,5]
## [1,] 1.1765012 0.2505234 1.645336 1.562534 1.756518
## [2,] 1.0729306 1.5625473 1.709072 1.612477 1.823150
## [3,] 1.3325733 1.7480262 1.870424 1.226580 1.187160
## [4,] 0.7280805 1.4920289 1.568080 1.565084 1.006825
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.1765012 0.2505234 1.6453358 1.5625343 1.7565181
## [2,] 1.0729306 1.5625473 1.7090725 1.6124772 1.8231502
## [3,] 1.3325733 1.7480262 1.8704242 1.2265796 1.1871599
## [4,] 0.7280805 1.4920289 1.5680803 1.5650840 1.0068245
einsum::einsum('ijk->jk', arrE)
##           [,1]      [,2]     [,3]     [,4]     [,5]
## [1,] 1.1765012 0.2505234 1.645336 1.562534 1.756518
## [2,] 1.0729306 1.5625473 1.709072 1.612477 1.823150
## [3,] 1.3325733 1.7480262 1.870424 1.226580 1.187160
## [4,] 0.7280805 1.4920289 1.568080 1.565084 1.006825
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.1765012 0.2505234 1.6453358 1.5625343 1.7565181
## [2,] 1.0729306 1.5625473 1.7090725 1.6124772 1.8231502
## [3,] 1.3325733 1.7480262 1.8704242 1.2265796 1.1871599
## [4,] 0.7280805 1.4920289 1.5680803 1.5650840 1.0068245

Trace

If we take the diagonal elements of a matrix and add them together, we get trace.

einsum::einsum('ii->', arrB)
## [1] 1.089057
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
##      [1] 
## 1.089057

Permutation

By changing the order of the indices on the left and right side of ->, we can get a sorted array or DelayedArray.

einsum::einsum('ij->ji', arrB)
##           [,1]      [,2]      [,3]
## [1,] 0.7704871 0.7729497 0.5127875
## [2,] 0.5716402 0.1410138 0.3547696
## [3,] 0.9977060 0.4411085 0.1775558
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 0.7704871 0.7729497 0.5127875
## [2,] 0.5716402 0.1410138 0.3547696
## [3,] 0.9977060 0.4411085 0.1775558
einsum::einsum('ijk->jki', arrD)
## , , 1
## 
##           [,1]      [,2]      [,3]
## [1,] 0.0757876 0.9044335 0.3480071
## [2,] 0.3362713 0.4808168 0.2979838
## [3,] 0.7582242 0.4507621 0.2586458
## 
## , , 2
## 
##            [,1]      [,2]      [,3]
## [1,] 0.03998589 0.7700434 0.4694730
## [2,] 0.11160044 0.7473744 0.5280919
## [3,] 0.02244408 0.9488448 0.8254163
## 
## , , 3
## 
##             [,1]      [,2]       [,3]
## [1,] 0.001118543 0.4364238 0.07800157
## [2,] 0.250970994 0.2653693 0.56496308
## [3,] 0.946297226 0.8083483 0.13005976
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
##           [,1]      [,2]      [,3]
## [1,] 0.0757876 0.9044335 0.3480071
## [2,] 0.3362713 0.4808168 0.2979838
## [3,] 0.7582242 0.4507621 0.2586458
## 
## ,,2
##            [,1]       [,2]       [,3]
## [1,] 0.03998589 0.77004341 0.46947300
## [2,] 0.11160044 0.74737442 0.52809195
## [3,] 0.02244408 0.94884483 0.82541631
## 
## ,,3
##             [,1]        [,2]        [,3]
## [1,] 0.001118543 0.436423755 0.078001566
## [2,] 0.250970994 0.265369277 0.564963075
## [3,] 0.946297226 0.808348252 0.130059760

Multiplication + Summation

Some examples of combining Multiplication and Summation are shown below.

Inner Product (Squared Frobenius Norm)

Inner Product first calculate Hadamard Product and collapses it to 0D tensor (norm).

einsum::einsum('i,i->', arrA, arrA)
## [1] 2.185243
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
##      [1] 
## 2.185243
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 4.007208
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
##      [1] 
## 4.007208
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 19.27081
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
##      [1] 
## 19.27081

Contracted Product

The inner product is an operation that eliminates all subscripts, while the outer product is an operation that leaves all subscripts intact. In the middle of the two, the operation that eliminates some subscripts while keeping others by summing them is called contracted product.

einsum::einsum('ijk,ijk->jk', arrE, arrE)
##           [,1]       [,2]     [,3]      [,4]      [,5]
## [1,] 0.5874924 0.03276673 1.134870 0.9537369 1.2260728
## [2,] 0.8776925 1.17668761 1.399733 0.9162062 1.5695377
## [3,] 0.8913759 1.48214963 1.188239 0.7020942 0.7603106
## [4,] 0.4449140 1.17201887 1.142446 1.2354981 0.3769679
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]       [,4]       [,5]
## [1,] 0.58749235 0.03276673 1.13487001 0.95373690 1.22607276
## [2,] 0.87769246 1.17668761 1.39973341 0.91620618 1.56953772
## [3,] 0.89137586 1.48214963 1.18823872 0.70209425 0.76031057
## [4,] 0.44491401 1.17201887 1.14244616 1.23549813 0.37696785

Matrix Multiplication

Matrix Multiplication is considered a contracted product.

einsum::einsum('ij,jk->ik', arrC, t(arrC))
##           [,1]      [,2]      [,3]
## [1,] 1.6463803 0.8242685 1.2659995
## [2,] 0.8242685 0.8560556 0.8489537
## [3,] 1.2659995 0.8489537 1.5047725
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 1.6463803 0.8242685 1.2659995
## [2,] 0.8242685 0.8560556 0.8489537
## [3,] 1.2659995 0.8489537 1.5047725

Multiplication + Permutation

Some examples of combining Multiplication and Permutation are shown below.

einsum::einsum('ij,ij->ji', arrC, arrC)
##            [,1]       [,2]      [,3]
## [1,] 0.01748867 0.52514101 0.1423049
## [2,] 0.39724448 0.11343190 0.3573611
## [3,] 0.27477460 0.06240549 0.8785345
## [4,] 0.95687252 0.15507724 0.1265719
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.01748867 0.52514101 0.14230491
## [2,] 0.39724448 0.11343190 0.35736114
## [3,] 0.27477460 0.06240549 0.87853452
## [4,] 0.95687252 0.15507724 0.12657193
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
## 
##              [,1]        [,2]        [,3]         [,4]        [,5]
## [1,] 2.202144e-01 0.008106745 0.030185052 3.047378e-01 0.073784926
## [2,] 8.631827e-01 0.191259882 0.006736384 1.571415e-01 0.004364925
## [3,] 1.199100e-01 0.002001539 0.546365550 3.112750e-03 0.036178583
## [4,] 9.874849e-05 0.292850035 0.121016926 8.407881e-05 0.062218546
## 
## , , 2
## 
##              [,1]         [,2]      [,3]      [,4]       [,5]
## [1,] 0.0124437374 0.0246478084 0.3984021 0.5903602 0.34172212
## [2,] 0.0006919546 0.0204092997 0.3933409 0.5000883 0.96477811
## [3,] 0.7581018919 0.5330223716 0.3567100 0.2529106 0.02606606
## [4,] 0.4419706944 0.0001776202 0.9648037 0.7915905 0.24687948
## 
## , , 3
## 
##             [,1]         [,2]       [,3]       [,4]       [,5]
## [1,] 0.354834206 1.217855e-05 0.70628284 0.05863888 0.81056571
## [2,] 0.013817852 9.650184e-01 0.99965611 0.25897645 0.60039469
## [3,] 0.013363958 9.471257e-01 0.28516321 0.44607089 0.69806593
## [4,] 0.002844564 8.789912e-01 0.05662554 0.44382359 0.06786983
DelayedTensor::einsum('ijk,ijk->jki', darrE, darrE)
## <4 x 5 x 3> HDF5Array object of type "double":
## ,,1
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 2.202144e-01 8.106745e-03 3.018505e-02 3.047378e-01 7.378493e-02
## [2,] 8.631827e-01 1.912599e-01 6.736384e-03 1.571415e-01 4.364925e-03
## [3,] 1.199100e-01 2.001539e-03 5.463655e-01 3.112750e-03 3.617858e-02
## [4,] 9.874849e-05 2.928500e-01 1.210169e-01 8.407881e-05 6.221855e-02
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.0124437374 0.0246478084 0.3984021254 0.5903602434 0.3417221216
## [2,] 0.0006919546 0.0204092997 0.3933409175 0.5000882558 0.9647781110
## [3,] 0.7581018919 0.5330223716 0.3567099597 0.2529106096 0.0260660564
## [4,] 0.4419706944 0.0001776202 0.9648036919 0.7915904583 0.2468794752
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 3.548342e-01 1.217855e-05 7.062828e-01 5.863888e-02 8.105657e-01
## [2,] 1.381785e-02 9.650184e-01 9.996561e-01 2.589764e-01 6.003947e-01
## [3,] 1.336396e-02 9.471257e-01 2.851632e-01 4.460709e-01 6.980659e-01
## [4,] 2.844564e-03 8.789912e-01 5.662554e-02 4.438236e-01 6.786983e-02

Summation + Permutation

Some examples of combining Summation and Permutation are shown below.

einsum::einsum('ijk->ki', arrE)
##           [,1]     [,2]     [,3]
## [1,] 1.7545638 1.673356 0.882166
## [2,] 1.1132649 1.043269 2.896592
## [3,] 1.3428542 2.837856 2.612202
## [4,] 1.0134028 2.868134 2.085138
## [5,] 0.7773445 2.225121 2.771187
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 1.7545638 1.6733558 0.8821660
## [2,] 1.1132649 1.0432686 2.8965925
## [3,] 1.3428542 2.8378562 2.6122024
## [4,] 1.0134028 2.8681341 2.0851382
## [5,] 0.7773445 2.2251208 2.7711874

Multiplication + Summation + Permutation

Finally, we will show a more complex example, combining Multiplication, Summation, and Permutation.

einsum::einsum('i,ij,ijk,ijk,ji->jki',
    arrA, arrC, arrE, arrE, t(arrC))
## , , 1
## 
##              [,1]         [,2]         [,3]         [,4]        [,5]
## [1,] 3.776406e-03 0.0001390207 0.0005176365 5.225878e-03 0.001265321
## [2,] 3.362302e-01 0.0745002857 0.0026239822 6.121035e-02 0.001700242
## [3,] 3.230786e-02 0.0005392832 0.1472095783 8.386815e-04 0.009747748
## [4,] 9.265327e-05 0.2747739430 0.1135471877 7.888907e-05 0.058378123
## 
## , , 2
## 
##              [,1]         [,2]       [,3]       [,4]        [,5]
## [1,] 5.138323e-03 1.017768e-02 0.16450997 0.24377416 0.141105409
## [2,] 6.171737e-05 1.820363e-03 0.03508318 0.04460427 0.086051266
## [3,] 3.720018e-02 2.615549e-02 0.01750381 0.01241036 0.001279065
## [4,] 5.389347e-02 2.165884e-05 0.11764721 0.09652576 0.030104241
## 
## , , 3
## 
##              [,1]         [,2]        [,3]        [,4]        [,5]
## [1,] 0.0392902482 1.348512e-06 0.078205617 0.006492994 0.089752699
## [2,] 0.0038422645 2.683381e-01 0.277969636 0.072012353 0.166948905
## [3,] 0.0091355217 6.474495e-01 0.194935859 0.304931378 0.477193676
## [4,] 0.0002801513 8.656879e-02 0.005576853 0.043710643 0.006684264
DelayedTensor::einsum('i,ij,ijk,ijk,ji->jki',
    darrA, darrC, darrE, darrE, t(darrC))
## <4 x 5 x 3> HDF5Array object of type "double":
## ,,1
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 3.776406e-03 1.390207e-04 5.176365e-04 5.225878e-03 1.265321e-03
## [2,] 3.362302e-01 7.450029e-02 2.623982e-03 6.121035e-02 1.700242e-03
## [3,] 3.230786e-02 5.392832e-04 1.472096e-01 8.386815e-04 9.747748e-03
## [4,] 9.265327e-05 2.747739e-01 1.135472e-01 7.888907e-05 5.837812e-02
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 5.138323e-03 1.017768e-02 1.645100e-01 2.437742e-01 1.411054e-01
## [2,] 6.171737e-05 1.820363e-03 3.508318e-02 4.460427e-02 8.605127e-02
## [3,] 3.720018e-02 2.615549e-02 1.750381e-02 1.241036e-02 1.279065e-03
## [4,] 5.389347e-02 2.165884e-05 1.176472e-01 9.652576e-02 3.010424e-02
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 3.929025e-02 1.348512e-06 7.820562e-02 6.492994e-03 8.975270e-02
## [2,] 3.842264e-03 2.683381e-01 2.779696e-01 7.201235e-02 1.669489e-01
## [3,] 9.135522e-03 6.474495e-01 1.949359e-01 3.049314e-01 4.771937e-01
## [4,] 2.801513e-04 8.656879e-02 5.576853e-03 4.371064e-02 6.684264e-03

Create your original function by einsum

By using einsum and other DelayedTensor functions, it is possible to implement your original tensor calculation functions. It is intended to be applied to Delayed Arrays, which can scale to large-scale data since the calculation is performed internally by block processing.

For example, kronecker can be easily implmented by eimsum and other DelayedTensor functions4 (the kronecker function inside DelayedTensor has a more efficient implementation though).

darr1 <- DelayedArray(array(1:6, dim=c(2,3)))
darr2 <- DelayedArray(array(20:1, dim=c(4,5)))

mykronecker <- function(darr1, darr2){
    stopifnot((length(dim(darr1)) == 2) && (length(dim(darr2)) == 2))
    # Outer Product
    tmpdarr <- DelayedTensor::einsum('ij,kl->ikjl', darr1, darr2)
    # Reshape
    DelayedTensor::unfold(tmpdarr, row_idx=c(2,1), col_idx=c(4,3))
}

identical(as.array(DelayedTensor::kronecker(darr1, darr2)),
    as.array(mykronecker(darr1, darr2)))
## [1] TRUE

Session information

## 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] einsum_0.1.2              DelayedRandomArray_1.17.0
##  [3] HDF5Array_1.37.0          h5mread_1.1.1            
##  [5] rhdf5_2.55.4              DelayedArray_0.37.0      
##  [7] SparseArray_1.11.1        S4Arrays_1.11.0          
##  [9] abind_1.4-8               IRanges_2.45.0           
## [11] S4Vectors_0.49.0          MatrixGenerics_1.23.0    
## [13] matrixStats_1.5.0         BiocGenerics_0.57.0      
## [15] generics_0.1.4            Matrix_1.7-4             
## [17] DelayedTensor_1.17.0      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 Rhdf5lib_1.33.0    
## [22] rsvd_1.0.5          buildtools_1.0.0    R6_2.6.1           
## [25] lifecycle_1.0.4     BiocSingular_1.25.1 irlba_2.3.5.1      
## [28] ScaledMatrix_1.19.0 rTensor_1.4.9       bslib_0.9.0        
## [31] Rcpp_1.1.0          xfun_0.54           sys_3.4.3          
## [34] knitr_1.50          rhdf5filters_1.23.0 htmltools_0.5.8.1  
## [37] rmarkdown_2.30      maketools_1.3.2     compiler_4.5.1