Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2024-10-29 16:15:12.703724
Compiled: Tue Oct 29 22:15:52 2024

1 What is einsum

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

It was originally introduced by Numpy1 https://numpy.org/doc/stable/reference/generated/numpy.einsum.html 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 https://en.wikipedia.org/wiki/Einstein_notation 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)

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

3 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 https://ajcr.net/Basic-guide-to-einsum/.

  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)

3.1 No Operation

3.1.2 diag

We can also extract the diagonal elements as follows.

einsum::einsum('ii->i', arrB)
## [1] 0.9877677 0.2391816 0.5983478
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.9877677 0.2391816 0.5983478
einsum::einsum('iii->i', arrD)
## [1] 0.1701862 0.2919543 0.1221751
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.1701862 0.2919543 0.1221751

3.2 Multiplication

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

3.2.1 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.33476257 0.04310246 0.32096546
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
##        [1]        [2]        [3] 
## 0.33476257 0.04310246 0.32096546
einsum::einsum('ij,ij->ij', arrC, arrC)
##             [,1]       [,2]       [,3]        [,4]
## [1,] 0.002904961 0.09761232 0.15714152 0.001248763
## [2,] 0.204162273 0.05375536 0.32223896 0.019461028
## [3,] 0.651039385 0.02517817 0.03206219 0.943704899
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.002904961 0.097612319 0.157141516 0.001248763
## [2,] 0.204162273 0.053755364 0.322238956 0.019461028
## [3,] 0.651039385 0.025178172 0.032062195 0.943704899
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
## 
##            [,1]       [,2]        [,3]      [,4]
## [1,] 0.93843271 0.01485504 0.705568684 0.5996748
## [2,] 0.49710614 0.12288967 0.187346535 0.4897131
## [3,] 0.01156643 0.17334489 0.008489066 0.1564913
## 
## , , 2
## 
##           [,1]        [,2]         [,3]        [,4]
## [1,] 0.3400208 0.135979337 4.555041e-02 0.856234685
## [2,] 0.1186677 0.890021044 5.394646e-05 0.101662316
## [3,] 0.7769967 0.006419624 8.749685e-03 0.003134904
## 
## , , 3
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.07495286 0.1952309 0.11844719 0.03553491
## [2,] 0.93461045 0.2533178 0.59116746 0.92892241
## [3,] 0.38653825 0.8416840 0.01717973 0.82553899
## 
## , , 4
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.3853957 0.07194288 0.01174385 0.4459602
## [2,] 0.2362752 0.68683314 0.58143689 0.3069611
## [3,] 0.3030058 0.20522524 0.04760720 0.5663685
## 
## , , 5
## 
##            [,1]      [,2]       [,3]         [,4]
## [1,] 0.48196251 0.6540234 0.08377452 0.2974794497
## [2,] 0.01130023 0.9555569 0.00301200 0.6286880725
## [3,] 0.71525180 0.4142781 0.73077525 0.0005177727
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.938432711 0.014855044 0.705568684 0.599674849
## [2,] 0.497106140 0.122889667 0.187346535 0.489713074
## [3,] 0.011566434 0.173344890 0.008489066 0.156491342
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]
## [1,] 3.400208e-01 1.359793e-01 4.555041e-02 8.562347e-01
## [2,] 1.186677e-01 8.900210e-01 5.394646e-05 1.016623e-01
## [3,] 7.769967e-01 6.419624e-03 8.749685e-03 3.134904e-03
## 
## ,,3
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.07495286 0.19523095 0.11844719 0.03553491
## [2,] 0.93461045 0.25331784 0.59116746 0.92892241
## [3,] 0.38653825 0.84168398 0.01717973 0.82553899
## 
## ,,4
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.38539569 0.07194288 0.01174385 0.44596024
## [2,] 0.23627517 0.68683314 0.58143689 0.30696110
## [3,] 0.30300584 0.20522524 0.04760720 0.56636851
## 
## ,,5
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.4819625052 0.6540233926 0.0837745242 0.2974794497
## [2,] 0.0113002287 0.9555568636 0.0030119998 0.6286880725
## [3,] 0.7152517994 0.4142780611 0.7307752507 0.0005177727

3.2.2 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.3347626 0.12012115 0.3277914
## [2,] 0.1201211 0.04310246 0.1176197
## [3,] 0.3277914 0.11761973 0.3209655
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.33476257 0.12012115 0.32779143
## [2,] 0.12012115 0.04310246 0.11761973
## [3,] 0.32779143 0.11761973 0.32096546
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.05221217 0.3026592 0.3840140 0.03423274
## [2,] 0.43771287 0.2246014 0.5499087 0.13514017
## [3,] 0.78163716 0.1537141 0.1734595 0.94106511
## 
## , , 2, 1, 1
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.03800098 0.2202809 0.2794924 0.02491521
## [2,] 0.31857545 0.1634690 0.4002336 0.09835749
## [3,] 0.56888986 0.1118759 0.1262470 0.68492445
## 
## , , 3, 1, 1
## 
##             [,1]       [,2]       [,3]        [,4]
## [1,] 0.005796555 0.03360099 0.04263293 0.003800492
## [2,] 0.048594542 0.02493507 0.06105044 0.015003157
## [3,] 0.086776750 0.01706522 0.01925734 0.104476317
## 
## , , 1, 2, 1
## 
##             [,1]       [,2]       [,3]        [,4]
## [1,] 0.006569119 0.03807933 0.04831505 0.004307021
## [2,] 0.055071223 0.02825842 0.06918724 0.017002777
## [3,] 0.098342355 0.01933967 0.02182396 0.118400921
## 
## , , 2, 2, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01889417 0.10952418 0.13896427 0.01238790
## [2,] 0.15839645 0.08127717 0.19899708 0.04890357
## [3,] 0.28285334 0.05562497 0.06277031 0.34054600
## 
## , , 3, 2, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02244015 0.13007919 0.16504447 0.01471281
## [2,] 0.18812359 0.09653092 0.23634398 0.05808158
## [3,] 0.33593802 0.06606442 0.07455077 0.40445818
## 
## , , 1, 3, 1
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.04527306 0.2624351 0.3329777 0.02968313
## [2,] 0.37953986 0.1947514 0.4768246 0.11717974
## [3,] 0.67775586 0.1332851 0.1504064 0.81599548
## 
## , , 2, 3, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02332883 0.13523065 0.17158065 0.01529547
## [2,] 0.19557376 0.10035378 0.24570379 0.06038175
## [3,] 0.34924200 0.06868073 0.07750317 0.42047573
## 
## , , 3, 3, 1
## 
##             [,1]       [,2]       [,3]        [,4]
## [1,] 0.004965925 0.02878606 0.03652376 0.003255892
## [2,] 0.041631082 0.02136195 0.05230208 0.012853247
## [3,] 0.074341888 0.01461982 0.01649782 0.089505158
## 
## , , 1, 4, 1
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.04173766 0.2419414 0.3069753 0.02736516
## [2,] 0.34990139 0.1795431 0.4395891 0.10802911
## [3,] 0.62482953 0.1228768 0.1386611 0.75227395
## 
## , , 2, 4, 1
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.03771734 0.2186368 0.2774063 0.02472925
## [2,] 0.31619762 0.1622489 0.3972463 0.09762336
## [3,] 0.56464369 0.1110409 0.1253047 0.67981220
## 
## , , 3, 4, 1
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.02132138 0.12359402 0.1568161 0.01397929
## [2,] 0.17874459 0.09171831 0.2245609 0.05518589
## [3,] 0.31918964 0.06277074 0.0708340 0.38429370
## 
## , , 1, 1, 2
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.03142845 0.18218182 0.2311523 0.02060596
## [2,] 0.26347565 0.13519594 0.3310105 0.08134589
## [3,] 0.47049646 0.09252622 0.1044117 0.56646207
## 
## , , 2, 1, 2
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.01856677 0.1076263 0.13655630 0.01217324
## [2,] 0.15565176 0.0798688 0.19554887 0.04805617
## [3,] 0.27795207 0.0546611 0.06168263 0.33464503
## 
## , , 3, 1, 2
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.04750942 0.2753987 0.3494259 0.0311494
## [2,] 0.39828809 0.2043716 0.5003785 0.1229681
## [3,] 0.71123514 0.1398691 0.1578360 0.8563034
## 
## , , 1, 2, 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01987498 0.11520963 0.14617797 0.01303096
## [2,] 0.16661888 0.08549631 0.20932711 0.05144218
## [3,] 0.29753639 0.05851249 0.06602875 0.35822391
## 
## , , 2, 2, 2
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.05084758 0.2947491 0.3739776 0.03333805
## [2,] 0.42627306 0.2187314 0.5355366 0.13160822
## [3,] 0.76120874 0.1496967 0.1689261 0.91646998
## 
## , , 3, 2, 2
## 
##             [,1]       [,2]       [,3]        [,4]
## [1,] 0.004318421 0.02503267 0.03176145 0.002831358
## [2,] 0.036202831 0.01857658 0.04548245 0.011177320
## [3,] 0.064648495 0.01271355 0.01434668 0.077834635
## 
## , , 1, 3, 2
## 
##            [,1]       [,2]       [,3]        [,4]
## [1,] 0.01150314 0.06668044 0.08460414 0.007541994
## [2,] 0.09643482 0.04948312 0.12115327 0.029773440
## [3,] 0.17220659 0.03386556 0.03821578 0.207330997
## 
## , , 2, 3, 2
## 
##              [,1]        [,2]        [,3]         [,4]
## [1,] 0.0003958691 0.002294742 0.002911568 0.0002595503
## [2,] 0.0033187093 0.001702913 0.004169370 0.0010246236
## [3,] 0.0059263200 0.001165450 0.001315158 0.0071350919
## 
## , , 3, 3, 2
## 
##             [,1]       [,2]       [,3]        [,4]
## [1,] 0.005041577 0.02922460 0.03708017 0.003305493
## [2,] 0.042265299 0.02168738 0.05309886 0.013049056
## [3,] 0.075474428 0.01484254 0.01674915 0.090868698
## 
## , , 1, 4, 2
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.04987313 0.2891004 0.3668106 0.03269915
## [2,] 0.41810384 0.2145395 0.5252734 0.12908605
## [3,] 0.74662072 0.1468279 0.1656888 0.89890648
## 
## , , 2, 4, 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01718503 0.09961674 0.12639371 0.01126731
## [2,] 0.14406807 0.07392493 0.18099602 0.04447981
## [3,] 0.25726673 0.05059319 0.05709218 0.30974058
## 
## , , 3, 4, 2
## 
##             [,1]        [,2]       [,3]        [,4]
## [1,] 0.003017744 0.017493007 0.02219513 0.001978574
## [2,] 0.025298799 0.012981446 0.03178346 0.007810791
## [3,] 0.045176833 0.008884321 0.01002556 0.054391403
## 
## , , 1, 1, 3
## 
##            [,1]       [,2]       [,3]        [,4]
## [1,] 0.01475585 0.08553550 0.10852744 0.009674626
## [2,] 0.12370346 0.06347534 0.15541149 0.038192404
## [3,] 0.22090103 0.04344164 0.04902197 0.265957479
## 
## , , 2, 1, 3
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.05210573 0.3020422 0.3832311 0.03416295
## [2,] 0.43682055 0.2241435 0.5487877 0.13486467
## [3,] 0.78004372 0.1534007 0.1731059 0.93914667
## 
## , , 3, 1, 3
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.03350938 0.19424442 0.2464573 0.02197032
## [2,] 0.28092086 0.14414751 0.3529273 0.08673195
## [3,] 0.50164890 0.09865255 0.1113250 0.60396857
## 
## , , 1, 2, 3
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02381467 0.13804690 0.17515390 0.01561401
## [2,] 0.19964667 0.10244370 0.25082069 0.06163923
## [3,] 0.35651513 0.07011104 0.07911721 0.42923234
## 
## , , 2, 2, 3
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02712708 0.15724803 0.19951629 0.01778578
## [2,] 0.22741580 0.11669273 0.28570768 0.07021272
## [3,] 0.40610330 0.07986288 0.09012173 0.48893485
## 
## , , 3, 2, 3
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.04944754 0.2866334 0.3636805 0.03242012
## [2,] 0.41453602 0.2127088 0.5207911 0.12798451
## [3,] 0.74024957 0.1455749 0.1642749 0.89123582
## 
## , , 1, 3, 3
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.01854952 0.10752630 0.1364294 0.01216193
## [2,] 0.15550707 0.07979456 0.1953671 0.04801150
## [3,] 0.27769370 0.05461029 0.0616253 0.33433396
## 
## , , 2, 3, 3
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.04144054 0.2402191 0.3047900 0.02717036
## [2,] 0.34741055 0.1782650 0.4364598 0.10726009
## [3,] 0.62038158 0.1220021 0.1376740 0.74691876
## 
## , , 3, 3, 3
## 
##            [,1]       [,2]       [,3]        [,4]
## [1,] 0.00706445 0.04095062 0.05195814 0.004631783
## [2,] 0.05922375 0.03038919 0.07440415 0.018284835
## [3,] 0.10575765 0.02079794 0.02346955 0.127328688
## 
## , , 1, 4, 3
## 
##            [,1]       [,2]       [,3]        [,4]
## [1,] 0.01016009 0.05889520 0.07472623 0.006661433
## [2,] 0.08517563 0.04370574 0.10700809 0.026297259
## [3,] 0.15210070 0.02991160 0.03375392 0.183124180
## 
## , , 2, 4, 3
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.05194693 0.3011217 0.3820632 0.03405883
## [2,] 0.43548928 0.2234604 0.5471152 0.13445365
## [3,] 0.77766643 0.1529332 0.1725784 0.93628448
## 
## , , 3, 4, 3
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.0489710 0.2838711 0.3601756 0.03210767
## [2,] 0.4105410 0.2106588 0.5157721 0.12675108
## [3,] 0.7331155 0.1441720 0.1626917 0.88264669
## 
## , , 1, 1, 4
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.03345982 0.19395713 0.2460928 0.02193782
## [2,] 0.28050537 0.14393431 0.3524053 0.08660367
## [3,] 0.50090695 0.09850664 0.1111604 0.60307529
## 
## , , 2, 1, 4
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02619867 0.15186628 0.19268793 0.01717707
## [2,] 0.21963259 0.11269897 0.27592946 0.06780972
## [3,] 0.39220459 0.07712961 0.08703735 0.47220127
## 
## , , 3, 1, 4
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02966851 0.17197995 0.21820815 0.01945206
## [2,] 0.24872145 0.12762519 0.31247446 0.07679066
## [3,] 0.44414945 0.08734491 0.09856486 0.53474115
## 
## , , 1, 2, 4
## 
##            [,1]       [,2]       [,3]        [,4]
## [1,] 0.01445653 0.08380042 0.10632597 0.009478377
## [2,] 0.12119415 0.06218775 0.15225898 0.037417674
## [3,] 0.21642007 0.04256043 0.04802756 0.260562558
## 
## , , 2, 2, 4
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.04466793 0.2589274 0.3285270 0.02928638
## [2,] 0.37446684 0.1921483 0.4704513 0.11561349
## [3,] 0.66869681 0.1315036 0.1483960 0.80508869
## 
## , , 3, 2, 4
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02441662 0.14153625 0.17958119 0.01600868
## [2,] 0.20469306 0.10503313 0.25716058 0.06319726
## [3,] 0.36552662 0.07188321 0.08111702 0.44008188
## 
## , , 1, 3, 4
## 
##             [,1]       [,2]       [,3]        [,4]
## [1,] 0.005840843 0.03385772 0.04295867 0.003829529
## [2,] 0.048965825 0.02512559 0.06151689 0.015117787
## [3,] 0.087439761 0.01719560 0.01940448 0.105274561
## 
## , , 2, 3, 4
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.04109807 0.2382339 0.3022712 0.02694582
## [2,] 0.34453951 0.1767918 0.4328529 0.10637368
## [3,] 0.61525468 0.1209939 0.1365362 0.74074614
## 
## , , 3, 3, 4
## 
##            [,1]       [,2]       [,3]        [,4]
## [1,] 0.01175998 0.06816927 0.08649317 0.007710391
## [2,] 0.09858800 0.05058797 0.12385836 0.030438217
## [3,] 0.17605159 0.03462170 0.03906906 0.211960251
## 
## , , 1, 4, 4
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.03599302 0.2086414 0.2647241 0.02359870
## [2,] 0.30174203 0.1548314 0.3790854 0.09316032
## [3,] 0.53882992 0.1059644 0.1195762 0.64873327
## 
## , , 2, 4, 4
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02986152 0.17309877 0.21962772 0.01957860
## [2,] 0.25033952 0.12845546 0.31450727 0.07729022
## [3,] 0.44703889 0.08791314 0.09920608 0.53821993
## 
## , , 3, 4, 4
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.04056203 0.2351267 0.2983287 0.02659436
## [2,] 0.34004571 0.1744859 0.4272072 0.10498625
## [3,] 0.60722995 0.1194158 0.1347554 0.73108463
## 
## , , 1, 1, 5
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.03741768 0.2168997 0.2752023 0.02453278
## [2,] 0.31368545 0.1609598 0.3940902 0.09684775
## [3,] 0.56015763 0.1101587 0.1243092 0.67441113
## 
## , , 2, 1, 5
## 
##             [,1]       [,2]       [,3]        [,4]
## [1,] 0.005729461 0.03321207 0.04213947 0.003756502
## [2,] 0.048032077 0.02464646 0.06034380 0.014829500
## [3,] 0.085772338 0.01686769 0.01903445 0.103267038
## 
## , , 3, 1, 5
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.04558266 0.2642298 0.3352548 0.02988612
## [2,] 0.38213536 0.1960832 0.4800854 0.11798108
## [3,] 0.68239072 0.1341966 0.1514349 0.82157570
## 
## , , 1, 2, 5
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.04358799 0.2526673 0.3205842 0.02857832
## [2,] 0.36541333 0.1875027 0.4590771 0.11281829
## [3,] 0.65252968 0.1283243 0.1448082 0.78562401
## 
## , , 2, 2, 5
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.05268639 0.3054081 0.3875018 0.03454366
## [2,] 0.44168842 0.2266414 0.5549033 0.13636759
## [3,] 0.78873643 0.1551102 0.1750350 0.94961239
## 
## , , 3, 2, 5
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.03469095 0.2010936 0.2551476 0.02274500
## [2,] 0.29082632 0.1492303 0.3653718 0.08979018
## [3,] 0.51933740 0.1021311 0.1152504 0.62526493
## 
## , , 1, 3, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01560006 0.09042912 0.11473646 0.01022813
## [2,] 0.13078072 0.06710686 0.16430282 0.04037745
## [3,] 0.23353911 0.04592700 0.05182659 0.28117331
## 
## , , 2, 3, 5
## 
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.002957996 0.017146670 0.021755694 0.001939401
## [2,] 0.024797918 0.012724431 0.031154192 0.007656149
## [3,] 0.044282395 0.008708424 0.009827071 0.053314528
## 
## , , 3, 3, 5
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.04607466 0.2670818 0.3388733 0.0302087
## [2,] 0.38625993 0.1981996 0.4852672 0.1192545
## [3,] 0.68975609 0.1356451 0.1530695 0.8304434
## 
## , , 1, 4, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02939671 0.17040440 0.21620909 0.01927385
## [2,] 0.24644285 0.12645599 0.30961180 0.07608716
## [3,] 0.44008049 0.08654472 0.09766189 0.52984225
## 
## , , 2, 4, 5
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.0427354 0.2477251 0.3143135 0.02801933
## [2,] 0.3582658 0.1838351 0.4500975 0.11061156
## [3,] 0.6397661 0.1258142 0.1419758 0.77025711
## 
## , , 3, 4, 5
## 
##             [,1]        [,2]        [,3]         [,4]
## [1,] 0.001226421 0.007109219 0.009020176 0.0008040992
## [2,] 0.010281520 0.005275705 0.012916908 0.0031743328
## [3,] 0.018360022 0.003610619 0.004074424 0.0221048548
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.05221217 0.30265920 0.38401398 0.03423274
## [2,] 0.43771287 0.22460141 0.54990870 0.13514017
## [3,] 0.78163716 0.15371409 0.17345954 0.94106511
## 
## ,,2,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.03800098 0.22028092 0.27949242 0.02491521
## [2,] 0.31857545 0.16346902 0.40023364 0.09835749
## [3,] 0.56888986 0.11187593 0.12624703 0.68492445
## 
## ,,3,1,1
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.005796555 0.033600990 0.042632934 0.003800492
## [2,] 0.048594542 0.024935074 0.061050436 0.015003157
## [3,] 0.086776750 0.017065218 0.019257343 0.104476317
## 
## ...
## 
## ,,1,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02939671 0.17040440 0.21620909 0.01927385
## [2,] 0.24644285 0.12645599 0.30961180 0.07608716
## [3,] 0.44008049 0.08654472 0.09766189 0.52984225
## 
## ,,2,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.04273540 0.24772505 0.31431353 0.02801933
## [2,] 0.35826580 0.18383513 0.45009753 0.11061156
## [3,] 0.63976613 0.12581421 0.14197577 0.77025711
## 
## ,,3,4,5
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.0012264215 0.0071092188 0.0090201764 0.0008040992
## [2,] 0.0102815196 0.0052757046 0.0129169085 0.0031743328
## [3,] 0.0183600218 0.0036106190 0.0040744237 0.0221048548

3.3 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] 1.352736
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
##      [1] 
## 1.352736
einsum::einsum('ij->', arrC)
## [1] 4.304985
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
##      [1] 
## 4.304985
einsum::einsum('ijk->', arrE)
## [1] 30.65775
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
##      [1] 
## 30.65775

3.3.1 Row-wise / Column-wise Summation

einsum::einsum('ij->i', arrC)
## [1] 0.798076 1.390859 2.116050
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
##      [1]      [2]      [3] 
## 0.798076 1.390859 2.116050
einsum::einsum('ij->j', arrC)
## [1] 1.312611 0.702958 1.143131 1.146285
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 1.312611 0.702958 1.143131 1.146285

3.3.2 Mode-wise Vectorization

einsum::einsum('ijk->i', arrE)
## [1] 10.046895 11.568070  9.042787
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 10.046895 11.568070  9.042787
einsum::einsum('ijk->j', arrE)
## [1] 8.756258 8.123541 5.211624 8.566329
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 8.756258 8.123541 5.211624 8.566329
einsum::einsum('ijk->k', arrE)
## [1] 5.904841 4.815830 7.029852 6.270835 6.636395
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4]      [5] 
## 5.904841 4.815830 7.029852 6.270835 6.636395

3.3.3 Mode-wise Summation

These are the same as what the modeSum function does.

einsum::einsum('ijk->ij', arrE)
##          [,1]     [,2]     [,3]     [,4]
## [1,] 3.140653 2.009423 1.795376 3.101443
## [2,] 2.608676 3.603552 2.026456 3.329386
## [3,] 3.006929 2.510565 1.389792 2.135501
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]     [,4]
## [1,] 3.140653 2.009423 1.795376 3.101443
## [2,] 2.608676 3.603552 2.026456 3.329386
## [3,] 3.006929 2.510565 1.389792 2.135501
einsum::einsum('ijk->jk', arrE)
##           [,1]     [,2]     [,3]     [,4]     [,5]
## [1,] 1.7813323 1.809069 1.862250 1.657344 1.646263
## [2,] 0.8887846 1.392286 1.862590 1.549994 2.429887
## [3,] 1.3649529 0.314310 1.244107 1.089080 1.199174
## [4,] 1.8697716 1.300165 2.060905 1.974418 1.361070
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.7813323 1.8090690 1.8622497 1.6573436 1.6462633
## [2,] 0.8887846 1.3922856 1.8625897 1.5499936 2.4298874
## [3,] 1.3649529 0.3143100 1.2441072 1.0890799 1.1991739
## [4,] 1.8697716 1.3001650 2.0609050 1.9744178 1.3610699
einsum::einsum('ijk->jk', arrE)
##           [,1]     [,2]     [,3]     [,4]     [,5]
## [1,] 1.7813323 1.809069 1.862250 1.657344 1.646263
## [2,] 0.8887846 1.392286 1.862590 1.549994 2.429887
## [3,] 1.3649529 0.314310 1.244107 1.089080 1.199174
## [4,] 1.8697716 1.300165 2.060905 1.974418 1.361070
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.7813323 1.8090690 1.8622497 1.6573436 1.6462633
## [2,] 0.8887846 1.3922856 1.8625897 1.5499936 2.4298874
## [3,] 1.3649529 0.3143100 1.2441072 1.0890799 1.1991739
## [4,] 1.8697716 1.3001650 2.0609050 1.9744178 1.3610699

3.3.4 Trace

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

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

3.4 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.9877677 0.01029162 0.1245146
## [2,] 0.1562359 0.23918162 0.2348336
## [3,] 0.2610605 0.41233991 0.5983478
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.98776774 0.01029162 0.12451460
## [2,] 0.15623589 0.23918162 0.23483357
## [3,] 0.26106050 0.41233991 0.59834779
einsum::einsum('ijk->jki', arrD)
## , , 1
## 
##           [,1]      [,2]       [,3]
## [1,] 0.1701862 0.3770715 0.70118656
## [2,] 0.5464314 0.2476802 0.05040287
## [3,] 0.9360206 0.8680640 0.42246223
## 
## , , 2
## 
##            [,1]      [,2]      [,3]
## [1,] 0.58311678 0.9694385 0.5898792
## [2,] 0.38972621 0.2919543 0.3158753
## [3,] 0.08887588 0.1741818 0.1989646
## 
## , , 3
## 
##           [,1]      [,2]        [,3]
## [1,] 0.1987036 0.8417214 0.095208058
## [2,] 0.5466009 0.6746026 0.005617083
## [3,] 0.2231422 0.7677760 0.122175079
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
##            [,1]       [,2]       [,3]
## [1,] 0.17018616 0.37707149 0.70118656
## [2,] 0.54643144 0.24768021 0.05040287
## [3,] 0.93602061 0.86806403 0.42246223
## 
## ,,2
##            [,1]       [,2]       [,3]
## [1,] 0.58311678 0.96943855 0.58987916
## [2,] 0.38972621 0.29195428 0.31587526
## [3,] 0.08887588 0.17418184 0.19896460
## 
## ,,3
##             [,1]        [,2]        [,3]
## [1,] 0.198703590 0.841721351 0.095208058
## [2,] 0.546600928 0.674602573 0.005617083
## [3,] 0.223142179 0.767776028 0.122175079

3.5 Multiplication + Summation

Some examples of combining Multiplication and Summation are shown below.

3.5.1 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] 0.6988305
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
##       [1] 
## 0.6988305
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 2.51051
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
##     [1] 
## 2.51051
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 21.21747
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
##      [1] 
## 21.21747

3.5.2 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,] 1.4471053 1.23568515 1.3961016 0.9246767 1.2085145
## [2,] 0.3110896 1.03242000 1.2902328 0.9640013 2.0238583
## [3,] 0.9014043 0.05435404 0.7267944 0.6407879 0.8175618
## [4,] 1.2458793 0.96103191 1.7899963 1.3192899 0.9266853
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]       [,4]       [,5]
## [1,] 1.44710529 1.23568515 1.39610156 0.92467670 1.20851453
## [2,] 0.31108960 1.03242000 1.29023277 0.96400125 2.02385832
## [3,] 0.90140428 0.05435404 0.72679439 0.64078795 0.81756177
## [4,] 1.24587926 0.96103191 1.78999631 1.31928985 0.92668529

3.5.3 Matrix Multiplication

Matrix Multiplication is considered a contracted product.

einsum::einsum('ij,jk->ik', arrC, t(arrC))
##           [,1]      [,2]      [,3]
## [1,] 0.2589076 0.3267474 0.1983734
## [2,] 0.3267474 0.5996176 0.6385324
## [3,] 0.1983734 0.6385324 1.6519847
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 0.2589076 0.3267474 0.1983734
## [2,] 0.3267474 0.5996176 0.6385324
## [3,] 0.1983734 0.6385324 1.6519847

3.6 Multiplication + Permutation

Some examples of combining Multiplication and Permutation are shown below.

einsum::einsum('ij,ij->ji', arrC, arrC)
##             [,1]       [,2]       [,3]
## [1,] 0.002904961 0.20416227 0.65103938
## [2,] 0.097612319 0.05375536 0.02517817
## [3,] 0.157141516 0.32223896 0.03206219
## [4,] 0.001248763 0.01946103 0.94370490
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
##             [,1]        [,2]        [,3]
## [1,] 0.002904961 0.204162273 0.651039385
## [2,] 0.097612319 0.053755364 0.025178172
## [3,] 0.157141516 0.322238956 0.032062195
## [4,] 0.001248763 0.019461028 0.943704899
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
## 
##            [,1]       [,2]       [,3]       [,4]       [,5]
## [1,] 0.93843271 0.34002078 0.07495286 0.38539569 0.48196251
## [2,] 0.01485504 0.13597934 0.19523095 0.07194288 0.65402339
## [3,] 0.70556868 0.04555041 0.11844719 0.01174385 0.08377452
## [4,] 0.59967485 0.85623468 0.03553491 0.44596024 0.29747945
## 
## , , 2
## 
##           [,1]         [,2]      [,3]      [,4]       [,5]
## [1,] 0.4971061 1.186677e-01 0.9346104 0.2362752 0.01130023
## [2,] 0.1228897 8.900210e-01 0.2533178 0.6868331 0.95555686
## [3,] 0.1873465 5.394646e-05 0.5911675 0.5814369 0.00301200
## [4,] 0.4897131 1.016623e-01 0.9289224 0.3069611 0.62868807
## 
## , , 3
## 
##             [,1]        [,2]       [,3]      [,4]         [,5]
## [1,] 0.011566434 0.776996663 0.38653825 0.3030058 0.7152517994
## [2,] 0.173344890 0.006419624 0.84168398 0.2052252 0.4142780611
## [3,] 0.008489066 0.008749685 0.01717973 0.0476072 0.7307752507
## [4,] 0.156491342 0.003134904 0.82553899 0.5663685 0.0005177727
DelayedTensor::einsum('ijk,ijk->jki', darrE, darrE)
## <4 x 5 x 3> HDF5Array object of type "double":
## ,,1
##            [,1]       [,2]       [,3]       [,4]       [,5]
## [1,] 0.93843271 0.34002078 0.07495286 0.38539569 0.48196251
## [2,] 0.01485504 0.13597934 0.19523095 0.07194288 0.65402339
## [3,] 0.70556868 0.04555041 0.11844719 0.01174385 0.08377452
## [4,] 0.59967485 0.85623468 0.03553491 0.44596024 0.29747945
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 4.971061e-01 1.186677e-01 9.346104e-01 2.362752e-01 1.130023e-02
## [2,] 1.228897e-01 8.900210e-01 2.533178e-01 6.868331e-01 9.555569e-01
## [3,] 1.873465e-01 5.394646e-05 5.911675e-01 5.814369e-01 3.012000e-03
## [4,] 4.897131e-01 1.016623e-01 9.289224e-01 3.069611e-01 6.286881e-01
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.0115664345 0.7769966625 0.3865382487 0.3030058431 0.7152517994
## [2,] 0.1733448896 0.0064196238 0.8416839797 0.2052252367 0.4142780611
## [3,] 0.0084890661 0.0087496846 0.0171797293 0.0476072002 0.7307752507
## [4,] 0.1564913417 0.0031349044 0.8255389882 0.5663685108 0.0005177727

3.7 Summation + Permutation

Some examples of combining Summation and Permutation are shown below.

einsum::einsum('ijk->ki', arrE)
##          [,1]     [,2]     [,3]
## [1,] 2.704977 2.188244 1.011620
## [2,] 2.090622 1.614081 1.111127
## [3,] 1.248293 3.202740 2.578819
## [4,] 1.665196 2.631396 1.974243
## [5,] 2.337807 1.931609 2.366979
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]
## [1,] 2.704977 2.188244 1.011620
## [2,] 2.090622 1.614081 1.111127
## [3,] 1.248293 3.202740 2.578819
## [4,] 1.665196 2.631396 1.974243
## [5,] 2.337807 1.931609 2.366979

3.8 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,] 0.0015772914 0.0005714974 0.0001259787 0.0006477623 0.0008100691
## [2,] 0.0008389712 0.0076797308 0.0110260953 0.0040631316 0.0369374032
## [3,] 0.0641502984 0.0041414426 0.0107692178 0.0010677512 0.0076167790
## [4,] 0.0004332758 0.0006186448 0.0000256746 0.0003222142 0.0002149342
## 
## , , 2
## 
##             [,1]         [,2]        [,3]        [,4]         [,5]
## [1,] 0.021070539 5.029897e-03 0.039614772 0.010014854 0.0004789760
## [2,] 0.001371476 9.932833e-03 0.002827083 0.007665210 0.0106642270
## [3,] 0.012533569 3.609043e-06 0.039549373 0.038898393 0.0002015042
## [4,] 0.001978602 4.107493e-04 0.003753153 0.001240224 0.0025401071
## 
## , , 3
## 
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.0042661480 2.865864e-01 0.1425702423 0.1117602634 0.2638125017
## [2,] 0.0024726599 9.157205e-05 0.0120061124 0.0029274138 0.0059094257
## [3,] 0.0001541993 1.589333e-04 0.0003120604 0.0008647589 0.0132741349
## [4,] 0.0836672847 1.676060e-03 0.4413701410 0.3028059886 0.0002768245
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,] 0.0015772914 0.0005714974 0.0001259787 0.0006477623 0.0008100691
## [2,] 0.0008389712 0.0076797308 0.0110260953 0.0040631316 0.0369374032
## [3,] 0.0641502984 0.0041414426 0.0107692178 0.0010677512 0.0076167790
## [4,] 0.0004332758 0.0006186448 0.0000256746 0.0003222142 0.0002149342
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 2.107054e-02 5.029897e-03 3.961477e-02 1.001485e-02 4.789760e-04
## [2,] 1.371476e-03 9.932833e-03 2.827083e-03 7.665210e-03 1.066423e-02
## [3,] 1.253357e-02 3.609043e-06 3.954937e-02 3.889839e-02 2.015042e-04
## [4,] 1.978602e-03 4.107493e-04 3.753153e-03 1.240224e-03 2.540107e-03
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 4.266148e-03 2.865864e-01 1.425702e-01 1.117603e-01 2.638125e-01
## [2,] 2.472660e-03 9.157205e-05 1.200611e-02 2.927414e-03 5.909426e-03
## [3,] 1.541993e-04 1.589333e-04 3.120604e-04 8.647589e-04 1.327413e-02
## [4,] 8.366728e-02 1.676060e-03 4.413701e-01 3.028060e-01 2.768245e-04

4 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 https://stackoverflow.com/ questions/56067643/speeding-up-kronecker-products-numpy (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.4.1 (2024-06-14 ucrt)
## Platform: x86_64-w64-mingw32/x64
## Running under: Windows Server 2022 x64 (build 20348)
## 
## Matrix products: default
## 
## 
## 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] einsum_0.1.2              DelayedRandomArray_1.14.0
##  [3] HDF5Array_1.34.0          rhdf5_2.50.0             
##  [5] DelayedArray_0.32.0       SparseArray_1.6.0        
##  [7] S4Arrays_1.6.0            abind_1.4-8              
##  [9] IRanges_2.40.0            S4Vectors_0.44.0         
## [11] MatrixGenerics_1.18.0     matrixStats_1.4.1        
## [13] BiocGenerics_0.52.0       Matrix_1.7-1             
## [15] DelayedTensor_1.12.0      BiocStyle_2.34.0         
## 
## loaded via a namespace (and not attached):
##  [1] jsonlite_1.8.9      compiler_4.4.1      BiocManager_1.30.25
##  [4] crayon_1.5.3        rsvd_1.0.5          Rcpp_1.0.13        
##  [7] rhdf5filters_1.18.0 parallel_4.4.1      jquerylib_0.1.4    
## [10] BiocParallel_1.40.0 yaml_2.3.10         fastmap_1.2.0      
## [13] lattice_0.22-6      R6_2.5.1            XVector_0.46.0     
## [16] ScaledMatrix_1.14.0 knitr_1.48          bookdown_0.41      
## [19] bslib_0.8.0         rlang_1.1.4         cachem_1.1.0       
## [22] xfun_0.48           sass_0.4.9          cli_3.6.3          
## [25] Rhdf5lib_1.28.0     BiocSingular_1.22.0 zlibbioc_1.52.0    
## [28] digest_0.6.37       grid_4.4.1          irlba_2.3.5.1      
## [31] rTensor_1.4.8       dqrng_0.4.1         lifecycle_1.0.4    
## [34] evaluate_1.0.1      codetools_0.2-20    beachmat_2.22.0    
## [37] rmarkdown_2.28      tools_4.4.1         htmltools_0.5.8.1