Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2023-10-24 14:41:21.735014
Compiled: Tue Oct 24 16:57:47 2023

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.7401374 0.0110181 0.5042455
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.7401374 0.0110181 0.5042455
einsum::einsum('iii->i', arrD)
## [1] 0.3630608 0.8410852 0.3574727
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.3630608 0.8410852 0.3574727

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.002459712 0.979776021 0.307407458
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
##         [1]         [2]         [3] 
## 0.002459712 0.979776021 0.307407458
einsum::einsum('ij,ij->ij', arrC, arrC)
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.02399705 0.76692491 0.6433028 0.3155440
## [2,] 0.18269220 0.70970071 0.3470703 0.0557737
## [3,] 0.56607292 0.03455067 0.9215323 0.9992411
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02399705 0.76692491 0.64330276 0.31554404
## [2,] 0.18269220 0.70970071 0.34707031 0.05577370
## [3,] 0.56607292 0.03455067 0.92153232 0.99924107
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.7217065 0.8630971 0.7117743 0.5808830
## [2,] 0.9142922 0.1226487 0.9902352 0.5922297
## [3,] 0.4815944 0.5031569 0.5163521 0.3600445
## 
## , , 2
## 
##            [,1]        [,2]      [,3]       [,4]
## [1,] 0.51842102 0.002992873 0.3690348 0.06774002
## [2,] 0.91092851 0.938689731 0.1877491 0.15595429
## [3,] 0.03263293 0.822423838 0.1241784 0.46815271
## 
## , , 3
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.34159756 0.54973846 0.4880162 0.22606403
## [2,] 0.64128390 0.01518326 0.1429912 0.02456636
## [3,] 0.03006748 0.42593561 0.2096076 0.95674472
## 
## , , 4
## 
##              [,1]        [,2]      [,3]      [,4]
## [1,] 3.691028e-01 0.081745741 0.1164198 0.8583221
## [2,] 9.751255e-01 0.007456486 0.1417196 0.2603763
## [3,] 4.144921e-05 0.489412412 0.3242515 0.0105644
## 
## , , 5
## 
##              [,1]        [,2]       [,3]       [,4]
## [1,] 7.232052e-01 0.319407919 0.18168693 0.19059846
## [2,] 3.575925e-07 0.772433171 0.04197077 0.03094537
## [3,] 2.847207e-01 0.001277117 0.45864553 0.28762811
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.7217065 0.8630971 0.7117743 0.5808830
## [2,] 0.9142922 0.1226487 0.9902352 0.5922297
## [3,] 0.4815944 0.5031569 0.5163521 0.3600445
## 
## ,,2
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.518421016 0.002992873 0.369034800 0.067740021
## [2,] 0.910928508 0.938689731 0.187749064 0.155954286
## [3,] 0.032632929 0.822423838 0.124178447 0.468152710
## 
## ,,3
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.34159756 0.54973846 0.48801618 0.22606403
## [2,] 0.64128390 0.01518326 0.14299119 0.02456636
## [3,] 0.03006748 0.42593561 0.20960758 0.95674472
## 
## ,,4
##              [,1]         [,2]         [,3]         [,4]
## [1,] 3.691028e-01 8.174574e-02 1.164198e-01 8.583221e-01
## [2,] 9.751255e-01 7.456486e-03 1.417196e-01 2.603763e-01
## [3,] 4.144921e-05 4.894124e-01 3.242515e-01 1.056440e-02
## 
## ,,5
##              [,1]         [,2]         [,3]         [,4]
## [1,] 7.232052e-01 3.194079e-01 1.816869e-01 1.905985e-01
## [2,] 3.575925e-07 7.724332e-01 4.197077e-02 3.094537e-02
## [3,] 2.847207e-01 1.277117e-03 4.586455e-01 2.876281e-01

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.002459712 0.04909142 0.02749789
## [2,] 0.049091418 0.97977602 0.54880821
## [3,] 0.027497890 0.54880821 0.30740746
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
##             [,1]        [,2]        [,3]
## [1,] 0.002459712 0.049091418 0.027497890
## [2,] 0.049091418 0.979776021 0.548808214
## [3,] 0.027497890 0.548808214 0.307407458
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1316010 0.7439722 0.6813779 0.4772108
## [2,] 0.3631118 0.7156784 0.5004827 0.2006296
## [3,] 0.6391702 0.1579096 0.8155218 0.8492107
## 
## , , 2, 1, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1481226 0.8373729 0.7669203 0.5371214
## [2,] 0.4086980 0.8055270 0.5633149 0.2258173
## [3,] 0.7194137 0.1777341 0.9179051 0.9558234
## 
## , , 3, 1, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1075028 0.6077391 0.5566067 0.3898259
## [2,] 0.2966202 0.5846263 0.4088363 0.1638911
## [3,] 0.5221279 0.1289938 0.6661868 0.6937067
## 
## , , 1, 2, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1439159 0.8135912 0.7451394 0.5218670
## [2,] 0.3970908 0.7826497 0.5473165 0.2194040
## [3,] 0.6989820 0.1726864 0.8918362 0.9286776
## 
## , , 2, 2, 1
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.05425134 0.30669587 0.2808919 0.19672588
## [2,] 0.14968956 0.29503200 0.2063195 0.08270775
## [3,] 0.26349216 0.06509681 0.3361915 0.35007947
## 
## , , 3, 2, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1098830 0.6211953 0.5689308 0.3984572
## [2,] 0.3031878 0.5975708 0.4178885 0.1675199
## [3,] 0.5336886 0.1318499 0.6809371 0.7090663
## 
## , , 1, 3, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1306923 0.7388352 0.6766730 0.4739157
## [2,] 0.3606045 0.7107367 0.4970269 0.1992443
## [3,] 0.6347568 0.1568192 0.8098907 0.8433470
## 
## , , 2, 3, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1541516 0.8714563 0.7981360 0.5589837
## [2,] 0.4253331 0.8383141 0.5862433 0.2350087
## [3,] 0.7486957 0.1849683 0.9552663 0.9947279
## 
## , , 3, 3, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1113145 0.6292879 0.5763426 0.4036482
## [2,] 0.3071376 0.6053557 0.4233326 0.1697023
## [3,] 0.5406412 0.1335676 0.6898081 0.7183037
## 
## , , 1, 4, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1180656 0.6674531 0.6112967 0.4281287
## [2,] 0.3257649 0.6420693 0.4490069 0.1799944
## [3,] 0.5734301 0.1416683 0.7316436 0.7618675
## 
## , , 2, 4, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1192131 0.6739404 0.6172382 0.4322899
## [2,] 0.3289312 0.6483100 0.4533711 0.1817439
## [3,] 0.5790036 0.1430452 0.7387549 0.7692725
## 
## , , 3, 4, 1
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.09295164 0.5254780 0.4812667 0.3370607
## [2,] 0.25647091 0.5054937 0.3534979 0.1417075
## [3,] 0.45145483 0.1115338 0.5760145 0.5998094
## 
## , , 1, 1, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1115373 0.6305474 0.5774960 0.4044560
## [2,] 0.3077523 0.6065672 0.4241798 0.1700419
## [3,] 0.5417233 0.1338349 0.6911886 0.7197413
## 
## , , 2, 1, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1478499 0.8358312 0.7655082 0.5361325
## [2,] 0.4079455 0.8040439 0.5622777 0.2254015
## [3,] 0.7180891 0.1774068 0.9162151 0.9540635
## 
## , , 3, 1, 2
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.02798382 0.15819926 0.1448891 0.10147476
## [2,] 0.07721257 0.15218283 0.1064233 0.04266215
## [3,] 0.13591401 0.03357811 0.1734137 0.18057731
## 
## , , 1, 2, 2
## 
##             [,1]       [,2]       [,3]       [,4]
## [1,] 0.008474676 0.04790938 0.04387851 0.03073082
## [2,] 0.023383212 0.04608735 0.03222945 0.01291989
## [3,] 0.041160472 0.01016886 0.05251694 0.05468640
## 
## , , 2, 2, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1500859 0.8484719 0.7770854 0.5442407
## [2,] 0.4141151 0.8162039 0.5707813 0.2288104
## [3,] 0.7289491 0.1800899 0.9300715 0.9684923
## 
## , , 3, 2, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1404840 0.7941897 0.7273703 0.5094222
## [2,] 0.3876215 0.7639861 0.5342648 0.2141719
## [3,] 0.6823136 0.1685684 0.8705689 0.9065317
## 
## , , 1, 3, 2
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.09410498 0.5319981 0.4872382 0.3412429
## [2,] 0.25965319 0.5117658 0.3578841 0.1434658
## [3,] 0.45705646 0.1129177 0.5831616 0.6072518
## 
## , , 2, 3, 2
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.06712245 0.37945940 0.3475334 0.2433991
## [2,] 0.18520337 0.36502828 0.2552687 0.1023302
## [3,] 0.32600561 0.08054102 0.4159529 0.4331357
## 
## , , 3, 3, 2
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.05458861 0.30860257 0.2826382 0.19794890
## [2,] 0.15062016 0.29686618 0.2076021 0.08322194
## [3,] 0.26513026 0.06550151 0.3382816 0.35225588
## 
## , , 1, 4, 2
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.04031824 0.22792874 0.2087519 0.14620178
## [2,] 0.11124555 0.21926044 0.1533315 0.06146634
## [3,] 0.19582081 0.04837833 0.2498492 0.26017035
## 
## , , 2, 4, 2
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.06117551 0.34583989 0.3167425 0.22183427
## [2,] 0.16879464 0.33268734 0.2326523 0.09326386
## [3,] 0.29712202 0.07340521 0.3791001 0.39476060
## 
## , , 3, 4, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1059919 0.5991978 0.5487840 0.3843472
## [2,] 0.2924514 0.5764098 0.4030904 0.1615878
## [3,] 0.5147898 0.1271809 0.6568241 0.6839572
## 
## , , 1, 1, 3
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.09053913 0.5118395 0.4687757 0.3283125
## [2,] 0.24981435 0.4923739 0.3443231 0.1380296
## [3,] 0.43973757 0.1086390 0.5610643 0.5842417
## 
## , , 2, 1, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1240521 0.7012964 0.6422925 0.4498370
## [2,] 0.3422829 0.6746256 0.4717739 0.1891211
## [3,] 0.6025060 0.1488516 0.7687417 0.8004981
## 
## , , 3, 1, 3
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.02686132 0.15185354 0.1390773 0.09740438
## [2,] 0.07411541 0.14607844 0.1021544 0.04095088
## [3,] 0.13046219 0.03223122 0.1664577 0.17333395
## 
## , , 1, 2, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1148569 0.6493136 0.5946833 0.4164933
## [2,] 0.3169115 0.6246197 0.4368042 0.1751027
## [3,] 0.5578459 0.1378181 0.7117596 0.7411621
## 
## , , 2, 2, 3
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01908805 0.10790933 0.09883034 0.06921696
## [2,] 0.05266748 0.10380546 0.07259242 0.02910029
## [3,] 0.09270833 0.02290397 0.11828723 0.12317362
## 
## , , 3, 2, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1010999 0.5715423 0.5234554 0.3666080
## [2,] 0.2789536 0.5498062 0.3844862 0.1541298
## [3,] 0.4910302 0.1213110 0.6265089 0.6523897
## 
## , , 1, 3, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1082171 0.6117775 0.5603054 0.3924164
## [2,] 0.2985913 0.5885112 0.4115531 0.1649802
## [3,] 0.5255975 0.1298510 0.6706137 0.6983164
## 
## , , 2, 3, 3
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.05857787 0.33115480 0.3032930 0.21241473
## [2,] 0.16162727 0.31856074 0.2227734 0.08930368
## [3,] 0.28450561 0.07028827 0.3630028 0.37799824
## 
## , , 3, 3, 3
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.07092224 0.40094048 0.3672072 0.2571778
## [2,] 0.19568768 0.38569242 0.2697194 0.1081230
## [3,] 0.34446070 0.08510042 0.4394999 0.4576554
## 
## , , 1, 4, 3
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.07365372 0.41638220 0.3813497 0.2670827
## [2,] 0.20322434 0.40054688 0.2801073 0.1122873
## [3,] 0.35772716 0.08837795 0.4564267 0.4752815
## 
## , , 2, 4, 3
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.02428004 0.1372609 0.12571240 0.08804413
## [2,] 0.06699315 0.1320408 0.09233772 0.03701563
## [3,] 0.11792519 0.0291339 0.15046161 0.15667711
## 
## , , 3, 4, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1515224 0.8565929 0.7845231 0.5494498
## [2,] 0.4180787 0.8240160 0.5762445 0.2310004
## [3,] 0.7359261 0.1818136 0.9389735 0.9777620
## 
## , , 1, 1, 4
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.09411364 0.5320471 0.4872831 0.3412743
## [2,] 0.25967709 0.5118129 0.3579170 0.1434790
## [3,] 0.45709854 0.1129281 0.5832153 0.6073077
## 
## , , 2, 1, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1529710 0.8647821 0.7920233 0.5547027
## [2,] 0.4220756 0.8318938 0.5817535 0.2332088
## [3,] 0.7429617 0.1835517 0.9479502 0.9871096
## 
## , , 3, 1, 4
## 
##              [,1]        [,2]        [,3]        [,4]
## [1,] 0.0009973259 0.005638123 0.005163758 0.003616497
## [2,] 0.0027518081 0.005423701 0.003792861 0.001520453
## [3,] 0.0048438907 0.001196703 0.006180355 0.006435663
## 
## , , 1, 2, 4
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.04429059 0.2503854 0.2293191 0.16060629
## [2,] 0.12220601 0.2408631 0.1684385 0.06752231
## [3,] 0.21511404 0.0531448 0.2744656 0.28580361
## 
## , , 2, 2, 4
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01337661 0.07562119 0.06925877 0.04850618
## [2,] 0.03690856 0.07274526 0.05087165 0.02039303
## [3,] 0.06496857 0.01605075 0.08289386 0.08631817
## 
## , , 3, 2, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1083718 0.6126521 0.5611064 0.3929773
## [2,] 0.2990181 0.5893525 0.4121414 0.1652160
## [3,] 0.5263488 0.1300366 0.6715723 0.6993147
## 
## , , 1, 3, 4
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.05285577 0.29880637 0.2736662 0.19166527
## [2,] 0.14583892 0.28744255 0.2010121 0.08058017
## [3,] 0.25671404 0.06342225 0.3275433 0.34107397
## 
## , , 2, 3, 4
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.05831683 0.32967907 0.3019414 0.21146814
## [2,] 0.16090700 0.31714113 0.2217807 0.08890571
## [3,] 0.28323775 0.06997504 0.3613851 0.37631375
## 
## , , 3, 3, 4
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.08821043 0.4986748 0.4567186 0.3198681
## [2,] 0.24338902 0.4797098 0.3354669 0.1344794
## [3,] 0.42842733 0.1058447 0.5466335 0.5692147
## 
## , , 1, 4, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1435172 0.8113376 0.7430754 0.5204214
## [2,] 0.3959909 0.7804818 0.5458005 0.2187963
## [3,] 0.6970458 0.1722080 0.8893658 0.9261051
## 
## , , 2, 4, 4
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.07904596 0.44686586 0.4092686 0.2866360
## [2,] 0.21810255 0.42987122 0.3006142 0.1205079
## [3,] 0.38391665 0.09484817 0.4898420 0.5100772
## 
## , , 3, 4, 4
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01592214 0.09001169 0.08243852 0.05773677
## [2,] 0.04393215 0.08658848 0.06055238 0.02427377
## [3,] 0.07733190 0.01910516 0.09866833 0.10274428
## 
## , , 1, 1, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1317376 0.7447443 0.6820850 0.4777061
## [2,] 0.3634886 0.7164211 0.5010021 0.2008378
## [3,] 0.6398335 0.1580735 0.8163682 0.8500920
## 
## , , 2, 1, 5
## 
##              [,1]         [,2]         [,3]         [,4]
## [1,] 9.263457e-05 0.0005236856 0.0004796251 0.0003359110
## [2,] 2.555961e-04 0.0005037694 0.0003522921 0.0001412241
## [3,] 4.499149e-04 0.0001111533 0.0005740497 0.0005977634
## 
## , , 3, 1, 5
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.08265868 0.46728944 0.4279739 0.2997364
## [2,] 0.22807073 0.44951808 0.3143535 0.1260156
## [3,] 0.40146319 0.09918312 0.5122298 0.5333898
## 
## , , 1, 2, 5
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.08754912 0.4949362 0.4532946 0.3174701
## [2,] 0.24156435 0.4761135 0.3329520 0.1334712
## [3,] 0.42521544 0.1050512 0.5425355 0.5649474
## 
## , , 2, 2, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1361474 0.7696741 0.7049173 0.4936970
## [2,] 0.3756561 0.7404028 0.5177728 0.2075607
## [3,] 0.6612515 0.1633649 0.8436955 0.8785482
## 
## , , 3, 2, 5
## 
##             [,1]        [,2]       [,3]        [,4]
## [1,] 0.005535978 0.031296216 0.02866310 0.020074531
## [2,] 0.015274796 0.030105998 0.02105349 0.008439761
## [3,] 0.026887573 0.006642685 0.03430605 0.035723216
## 
## , , 1, 3, 5
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.06602992 0.37328305 0.3418767 0.2394373
## [2,] 0.18218887 0.35908682 0.2511138 0.1006646
## [3,] 0.32069932 0.07923007 0.4091826 0.4260857
## 
## , , 2, 3, 5
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.03173601 0.17941133 0.1643165 0.11508095
## [2,] 0.08756558 0.17258819 0.1206930 0.04838248
## [3,] 0.15413797 0.03808041 0.1966657 0.20478992
## 
## , , 3, 3, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1049102 0.5930824 0.5431832 0.3804246
## [2,] 0.2894667 0.5705270 0.3989765 0.1599386
## [3,] 0.5095359 0.1258829 0.6501205 0.6769767
## 
## , , 1, 4, 5
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.06762988 0.38232801 0.3501607 0.2452391
## [2,] 0.18660346 0.36778780 0.2571985 0.1031037
## [3,] 0.32847013 0.08114989 0.4190974 0.4364101
## 
## , , 2, 4, 5
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.02725064 0.15405444 0.1410930 0.09881612
## [2,] 0.07518961 0.14819564 0.1036350 0.04154440
## [3,] 0.13235306 0.03269836 0.1688702 0.17584618
## 
## , , 3, 4, 5
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.08307964 0.46966921 0.4301534 0.3012629
## [2,] 0.22923222 0.45180734 0.3159544 0.1266573
## [3,] 0.40350772 0.09968823 0.5148384 0.5361062
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.1316010 0.7439722 0.6813779 0.4772108
## [2,] 0.3631118 0.7156784 0.5004827 0.2006296
## [3,] 0.6391702 0.1579096 0.8155218 0.8492107
## 
## ,,2,1,1
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1481226 0.8373729 0.7669203 0.5371214
## [2,] 0.4086980 0.8055270 0.5633149 0.2258173
## [3,] 0.7194137 0.1777341 0.9179051 0.9558234
## 
## ,,3,1,1
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1075028 0.6077391 0.5566067 0.3898259
## [2,] 0.2966202 0.5846263 0.4088363 0.1638911
## [3,] 0.5221279 0.1289938 0.6661868 0.6937067
## 
## ...
## 
## ,,1,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.06762988 0.38232801 0.35016070 0.24523909
## [2,] 0.18660346 0.36778780 0.25719850 0.10310374
## [3,] 0.32847013 0.08114989 0.41909742 0.43641014
## 
## ,,2,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02725064 0.15405444 0.14109302 0.09881612
## [2,] 0.07518961 0.14819564 0.10363502 0.04154440
## [3,] 0.13235306 0.03269836 0.16887023 0.17584618
## 
## ,,3,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.08307964 0.46966921 0.43015341 0.30126290
## [2,] 0.22923222 0.45180734 0.31595439 0.12665735
## [3,] 0.40350772 0.09968823 0.51483842 0.53610617

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.593875
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
##      [1] 
## 1.593875
einsum::einsum('ij->', arrC)
## [1] 7.387442
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
##      [1] 
## 7.387442
einsum::einsum('ijk->', arrE)
## [1] 32.73776
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
##      [1] 
## 32.73776

3.3.1 Row-wise / Column-wise Summation

einsum::einsum('ij->i', arrC)
## [1] 2.394447 2.095154 2.897841
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
##      [1]      [2]      [3] 
## 2.394447 2.095154 2.897841
einsum::einsum('ij->j', arrC)
## [1] 1.334713 1.904058 2.351153 1.797518
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 1.334713 1.904058 2.351153 1.797518

3.3.2 Mode-wise Vectorization

einsum::einsum('ijk->i', arrE)
## [1] 11.96632 10.50229 10.26915
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
##      [1]      [2]      [3] 
## 11.96632 10.50229 10.26915
einsum::einsum('ijk->j', arrE)
## [1] 8.899507 7.987946 8.080514 7.769796
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 8.899507 7.987946 8.080514 7.769796
einsum::einsum('ijk->k', arrE)
## [1] 9.177375 6.518098 6.220851 5.499906 5.321533
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4]      [5] 
## 9.177375 6.518098 6.220851 5.499906 5.321533

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.611964 2.576255 2.917182 2.860920
## [2,] 3.699496 2.407526 2.387872 2.007395
## [3,] 1.588046 3.004166 2.775460 2.901480
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]     [,4]
## [1,] 3.611964 2.576255 2.917182 2.860920
## [2,] 3.699496 2.407526 2.387872 2.007395
## [3,] 1.588046 3.004166 2.775460 2.901480
einsum::einsum('ijk->jk', arrE)
##          [,1]     [,2]     [,3]     [,4]     [,5]
## [1,] 2.499689 1.855086 1.558665 1.601461 1.384605
## [2,] 1.988578 1.930443 1.517301 1.071843 1.479780
## [3,] 2.557349 1.393172 1.534553 1.287091 1.308349
## [4,] 2.131759 1.339396 1.610332 1.539511 1.148799
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]     [,4]     [,5]
## [1,] 2.499689 1.855086 1.558665 1.601461 1.384605
## [2,] 1.988578 1.930443 1.517301 1.071843 1.479780
## [3,] 2.557349 1.393172 1.534553 1.287091 1.308349
## [4,] 2.131759 1.339396 1.610332 1.539511 1.148799
einsum::einsum('ijk->jk', arrE)
##          [,1]     [,2]     [,3]     [,4]     [,5]
## [1,] 2.499689 1.855086 1.558665 1.601461 1.384605
## [2,] 1.988578 1.930443 1.517301 1.071843 1.479780
## [3,] 2.557349 1.393172 1.534553 1.287091 1.308349
## [4,] 2.131759 1.339396 1.610332 1.539511 1.148799
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]     [,4]     [,5]
## [1,] 2.499689 1.855086 1.558665 1.601461 1.384605
## [2,] 1.988578 1.930443 1.517301 1.071843 1.479780
## [3,] 2.557349 1.393172 1.534553 1.287091 1.308349
## [4,] 2.131759 1.339396 1.610332 1.539511 1.148799

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.255401
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
##      [1] 
## 1.255401

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.7401374 0.5873052 0.1212580
## [2,] 0.5497703 0.0110181 0.4384597
## [3,] 0.1980925 0.2557684 0.5042455
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 0.7401374 0.5873052 0.1212580
## [2,] 0.5497703 0.0110181 0.4384597
## [3,] 0.1980925 0.2557684 0.5042455
einsum::einsum('ijk->jki', arrD)
## , , 1
## 
##           [,1]      [,2]      [,3]
## [1,] 0.3630608 0.6702782 0.3518509
## [2,] 0.9528344 0.8187300 0.8371587
## [3,] 0.6540892 0.9063767 0.9546343
## 
## , , 2
## 
##           [,1]      [,2]      [,3]
## [1,] 0.2362357 0.8085344 0.6007623
## [2,] 0.1379294 0.8410852 0.9351142
## [3,] 0.4892781 0.6023099 0.1815426
## 
## , , 3
## 
##           [,1]      [,2]      [,3]
## [1,] 0.5490168 0.3031695 0.6599408
## [2,] 0.7421601 0.6947430 0.6306946
## [3,] 0.8305864 0.9820445 0.3574727
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
##           [,1]      [,2]      [,3]
## [1,] 0.3630608 0.6702782 0.3518509
## [2,] 0.9528344 0.8187300 0.8371587
## [3,] 0.6540892 0.9063767 0.9546343
## 
## ,,2
##           [,1]      [,2]      [,3]
## [1,] 0.2362357 0.8085344 0.6007623
## [2,] 0.1379294 0.8410852 0.9351142
## [3,] 0.4892781 0.6023099 0.1815426
## 
## ,,3
##           [,1]      [,2]      [,3]
## [1,] 0.5490168 0.3031695 0.6599408
## [2,] 0.7421601 0.6947430 0.6306946
## [3,] 0.8305864 0.9820445 0.3574727

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

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,] 2.117593 1.4619825 1.0129489 1.3442697 1.0079263
## [2,] 1.488903 1.7641064 0.9908573 0.5786146 1.0931182
## [3,] 2.218362 0.6809623 0.8406149 0.5823909 0.6823032
## [4,] 1.533157 0.6918470 1.2073751 1.1292629 0.5091719
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 2.1175931 1.4619825 1.0129489 1.3442697 1.0079263
## [2,] 1.4889028 1.7641064 0.9908573 0.5786146 1.0931182
## [3,] 2.2183616 0.6809623 0.8406149 0.5823909 0.6823032
## [4,] 1.5331572 0.6918470 1.2073751 1.1292629 0.5091719

3.5.3 Matrix Multiplication

Matrix Multiplication is considered a contracted product.

einsum::einsum('ij,jk->ik', arrC, t(arrC))
##          [,1]     [,2]     [,3]
## [1,] 1.749769 1.409148 1.610803
## [2,] 1.409148 1.295237 1.279792
## [3,] 1.610803 1.279792 2.521397
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]
## [1,] 1.749769 1.409148 1.610803
## [2,] 1.409148 1.295237 1.279792
## [3,] 1.610803 1.279792 2.521397

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.02399705 0.1826922 0.56607292
## [2,] 0.76692491 0.7097007 0.03455067
## [3,] 0.64330276 0.3470703 0.92153232
## [4,] 0.31554404 0.0557737 0.99924107
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.02399705 0.18269220 0.56607292
## [2,] 0.76692491 0.70970071 0.03455067
## [3,] 0.64330276 0.34707031 0.92153232
## [4,] 0.31554404 0.05577370 0.99924107
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
## 
##           [,1]        [,2]      [,3]       [,4]      [,5]
## [1,] 0.7217065 0.518421016 0.3415976 0.36910276 0.7232052
## [2,] 0.8630971 0.002992873 0.5497385 0.08174574 0.3194079
## [3,] 0.7117743 0.369034800 0.4880162 0.11641981 0.1816869
## [4,] 0.5808830 0.067740021 0.2260640 0.85832214 0.1905985
## 
## , , 2
## 
##           [,1]      [,2]       [,3]        [,4]         [,5]
## [1,] 0.9142922 0.9109285 0.64128390 0.975125482 3.575925e-07
## [2,] 0.1226487 0.9386897 0.01518326 0.007456486 7.724332e-01
## [3,] 0.9902352 0.1877491 0.14299119 0.141719596 4.197077e-02
## [4,] 0.5922297 0.1559543 0.02456636 0.260376334 3.094537e-02
## 
## , , 3
## 
##           [,1]       [,2]       [,3]         [,4]        [,5]
## [1,] 0.4815944 0.03263293 0.03006748 4.144921e-05 0.284720735
## [2,] 0.5031569 0.82242384 0.42593561 4.894124e-01 0.001277117
## [3,] 0.5163521 0.12417845 0.20960758 3.242515e-01 0.458645534
## [4,] 0.3600445 0.46815271 0.95674472 1.056440e-02 0.287628111
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.721706493 0.518421016 0.341597564 0.369102760 0.723205212
## [2,] 0.863097106 0.002992873 0.549738462 0.081745741 0.319407919
## [3,] 0.711774272 0.369034800 0.488016178 0.116419806 0.181686932
## [4,] 0.580882954 0.067740021 0.226064029 0.858322137 0.190598464
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 9.142922e-01 9.109285e-01 6.412839e-01 9.751255e-01 3.575925e-07
## [2,] 1.226487e-01 9.386897e-01 1.518326e-02 7.456486e-03 7.724332e-01
## [3,] 9.902352e-01 1.877491e-01 1.429912e-01 1.417196e-01 4.197077e-02
## [4,] 5.922297e-01 1.559543e-01 2.456636e-02 2.603763e-01 3.094537e-02
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 4.815944e-01 3.263293e-02 3.006748e-02 4.144921e-05 2.847207e-01
## [2,] 5.031569e-01 8.224238e-01 4.259356e-01 4.894124e-01 1.277117e-03
## [3,] 5.163521e-01 1.241784e-01 2.096076e-01 3.242515e-01 4.586455e-01
## [4,] 3.600445e-01 4.681527e-01 9.567447e-01 1.056440e-02 2.876281e-01

3.7 Summation + Permutation

Some examples of combining Summation and Permutation are shown below.

einsum::einsum('ijk->ki', arrE)
##          [,1]     [,2]     [,3]
## [1,] 3.384387 3.071069 2.721919
## [2,] 1.642473 2.751496 2.124128
## [3,] 2.499950 1.458901 2.262000
## [4,] 2.161111 1.960563 1.378232
## [5,] 2.278400 1.260260 1.782873
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]
## [1,] 3.384387 3.071069 2.721919
## [2,] 1.642473 2.751496 2.124128
## [3,] 2.499950 1.458901 2.262000
## [4,] 2.161111 1.960563 1.378232
## [5,] 2.278400 1.260260 1.782873

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.0008589357 0.0006169964 0.0004065508 0.000439286 0.0008607194
## [2,] 0.0328287747 0.0001138370 0.0209098605 0.003109282 0.0121490045
## [3,] 0.0227090972 0.0117740237 0.0155701144 0.003714364 0.0057967060
## [4,] 0.0090905630 0.0010601016 0.0035378027 0.013432364 0.0029827822
## 
## , , 2
## 
##            [,1]        [,2]        [,3]        [,4]         [,5]
## [1,] 0.16533637 0.164728101 0.115966816 0.176337185 6.466537e-08
## [2,] 0.08615920 0.659417863 0.010666054 0.005238088 5.426247e-01
## [3,] 0.34018818 0.064499842 0.049123596 0.048686749 1.441875e-02
## [4,] 0.03269513 0.008609743 0.001356231 0.014374553 1.708396e-03
## 
## , , 3
## 
##            [,1]       [,2]        [,3]         [,4]         [,5]
## [1,] 0.15115101 0.01024202 0.009436839 1.300906e-05 8.936113e-02
## [2,] 0.00963867 0.01575467 0.008159388 9.375374e-03 2.446496e-05
## [3,] 0.26382366 0.06344742 0.107096369 1.656722e-01 2.343392e-01
## [4,] 0.19947281 0.25936718 0.530058192 5.852918e-03 1.593525e-01
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.0008589357 0.0006169964 0.0004065508 0.0004392860 0.0008607194
## [2,] 0.0328287747 0.0001138370 0.0209098605 0.0031092822 0.0121490045
## [3,] 0.0227090972 0.0117740237 0.0155701144 0.0037143639 0.0057967060
## [4,] 0.0090905630 0.0010601016 0.0035378027 0.0134323643 0.0029827822
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 1.653364e-01 1.647281e-01 1.159668e-01 1.763372e-01 6.466537e-08
## [2,] 8.615920e-02 6.594179e-01 1.066605e-02 5.238088e-03 5.426247e-01
## [3,] 3.401882e-01 6.449984e-02 4.912360e-02 4.868675e-02 1.441875e-02
## [4,] 3.269513e-02 8.609743e-03 1.356231e-03 1.437455e-02 1.708396e-03
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 1.511510e-01 1.024202e-02 9.436839e-03 1.300906e-05 8.936113e-02
## [2,] 9.638670e-03 1.575467e-02 8.159388e-03 9.375374e-03 2.446496e-05
## [3,] 2.638237e-01 6.344742e-02 1.070964e-01 1.656722e-01 2.343392e-01
## [4,] 1.994728e-01 2.593672e-01 5.300582e-01 5.852918e-03 1.593525e-01

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.3.1 (2023-06-16)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 22.04.3 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.18-bioc/R/lib/libRblas.so 
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_GB              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: America/New_York
## 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.10.0
##  [3] HDF5Array_1.30.0          rhdf5_2.46.0             
##  [5] DelayedArray_0.28.0       SparseArray_1.2.0        
##  [7] S4Arrays_1.2.0            abind_1.4-5              
##  [9] IRanges_2.36.0            S4Vectors_0.40.0         
## [11] MatrixGenerics_1.14.0     matrixStats_1.0.0        
## [13] BiocGenerics_0.48.0       Matrix_1.6-1.1           
## [15] DelayedTensor_1.8.0       BiocStyle_2.30.0         
## 
## loaded via a namespace (and not attached):
##  [1] jsonlite_1.8.7      compiler_4.3.1      BiocManager_1.30.22
##  [4] crayon_1.5.2        rsvd_1.0.5          Rcpp_1.0.11        
##  [7] rhdf5filters_1.14.0 parallel_4.3.1      jquerylib_0.1.4    
## [10] BiocParallel_1.36.0 yaml_2.3.7          fastmap_1.1.1      
## [13] lattice_0.22-5      R6_2.5.1            XVector_0.42.0     
## [16] ScaledMatrix_1.10.0 knitr_1.44          bookdown_0.36      
## [19] bslib_0.5.1         rlang_1.1.1         cachem_1.0.8       
## [22] xfun_0.40           sass_0.4.7          cli_3.6.1          
## [25] Rhdf5lib_1.24.0     BiocSingular_1.18.0 zlibbioc_1.48.0    
## [28] digest_0.6.33       grid_4.3.1          irlba_2.3.5.1      
## [31] rTensor_1.4.8       dqrng_0.3.1         evaluate_0.22      
## [34] codetools_0.2-19    beachmat_2.18.0     rmarkdown_2.25     
## [37] tools_4.3.1         htmltools_0.5.6.1