Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2023-04-25 14:41:07.038824
Compiled: Tue Apr 25 16:52:49 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.848674765 0.005582589 0.622618967
DelayedTensor::einsum('ii->i', darrB)
## <3> array of class HDF5Array and type "double":
##         [1]         [2]         [3] 
## 0.848674765 0.005582589 0.622618967
einsum::einsum('iii->i', arrD)
## [1] 0.8355310 0.5134344 0.8110374
DelayedTensor::einsum('iii->i', darrD)
## <3> array of class HDF5Array and type "double":
##       [1]       [2]       [3] 
## 0.8355310 0.5134344 0.8110374

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.19670811 0.06824449 0.02859921
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> array of class HDF5Array and type "double":
##        [1]        [2]        [3] 
## 0.19670811 0.06824449 0.02859921
einsum::einsum('ij,ij->ij', arrC, arrC)
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.05320037 0.3133579 0.3735597 0.05369451
## [2,] 0.31317281 0.7792788 0.4319039 0.30265232
## [3,] 0.39137871 0.5024979 0.4069787 0.13595593
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> matrix of class HDF5Matrix and type "double":
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.05320037 0.31335786 0.37355968 0.05369451
## [2,] 0.31317281 0.77927880 0.43190392 0.30265232
## [3,] 0.39137871 0.50249792 0.40697873 0.13595593
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
## 
##            [,1]      [,2]       [,3]      [,4]
## [1,] 0.15532515 0.8726566 0.02121121 0.1190534
## [2,] 0.02787959 0.2643906 0.12368617 0.5625239
## [3,] 0.09167795 0.5914290 0.92937299 0.7513639
## 
## , , 2
## 
##            [,1]      [,2]        [,3]       [,4]
## [1,] 0.31772663 0.1300590 0.099799681 0.02319555
## [2,] 0.04174943 0.6177677 0.749824387 0.43191583
## [3,] 0.10870628 0.3240103 0.006177919 0.04259317
## 
## , , 3
## 
##            [,1]      [,2]       [,3]      [,4]
## [1,] 0.45251246 0.1805830 0.57028940 0.4771233
## [2,] 0.27827287 0.6213553 0.06617791 0.9224183
## [3,] 0.01471947 0.2150542 0.47672598 0.7765695
## 
## , , 4
## 
##            [,1]      [,2]       [,3]      [,4]
## [1,] 0.21824841 0.1348724 0.10534484 0.6263926
## [2,] 0.06525275 0.4286035 0.04334682 0.5680015
## [3,] 0.18881364 0.1289873 0.97755922 0.2635057
## 
## , , 5
## 
##            [,1]      [,2]        [,3]      [,4]
## [1,] 0.45650143 0.6300802 0.001465064 0.4086435
## [2,] 0.02262755 0.8238954 0.741566679 0.3725769
## [3,] 0.97174682 0.5626500 0.046849572 0.4984282
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> array of class HDF5Array and type "double":
## ,,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.15532515 0.87265664 0.02121121 0.11905337
## [2,] 0.02787959 0.26439064 0.12368617 0.56252392
## [3,] 0.09167795 0.59142900 0.92937299 0.75136385
## 
## ,,2
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.317726628 0.130059001 0.099799681 0.023195547
## [2,] 0.041749430 0.617767662 0.749824387 0.431915827
## [3,] 0.108706276 0.324010259 0.006177919 0.042593170
## 
## ,,3
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.45251246 0.18058297 0.57028940 0.47712325
## [2,] 0.27827287 0.62135530 0.06617791 0.92241825
## [3,] 0.01471947 0.21505423 0.47672598 0.77656948
## 
## ,,4
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.21824841 0.13487242 0.10534484 0.62639261
## [2,] 0.06525275 0.42860345 0.04334682 0.56800153
## [3,] 0.18881364 0.12898731 0.97755922 0.26350566
## 
## ,,5
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.456501431 0.630080214 0.001465064 0.408643452
## [2,] 0.022627549 0.823895418 0.741566679 0.372576941
## [3,] 0.971746820 0.562650046 0.046849572 0.498428248

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.19670811 0.11586305 0.07500464
## [2,] 0.11586305 0.06824449 0.04417848
## [3,] 0.07500464 0.04417848 0.02859921
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> matrix of class HDF5Matrix and type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.19670811 0.11586305 0.07500464
## [2,] 0.11586305 0.06824449 0.04417848
## [3,] 0.07500464 0.04417848 0.02859921
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.0909030 0.2206181 0.2408801 0.09132419
## [2,] 0.2205530 0.3479103 0.2590088 0.21681679
## [3,] 0.2465582 0.2793753 0.2514240 0.14531819
## 
## , , 2, 1, 1
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.03851239 0.09346811 0.1020524 0.03869084
## [2,] 0.09344051 0.14739733 0.1097329 0.09185762
## [3,] 0.10445802 0.11836146 0.1065195 0.06156619
## 
## , , 3, 1, 1
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.06983767 0.1694934 0.1850600 0.07016126
## [2,] 0.16944333 0.2672876 0.1989876 0.16657294
## [3,] 0.18942227 0.2146345 0.1931605 0.11164301
## 
## , , 1, 2, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.2154661 0.5229281 0.5709548 0.2164645
## [2,] 0.5227737 0.8246471 0.6139249 0.5139179
## [3,] 0.5844136 0.6621995 0.5959469 0.3444457
## 
## , , 2, 2, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1185988 0.2878348 0.3142701 0.1191483
## [2,] 0.2877498 0.4539097 0.3379221 0.2828753
## [3,] 0.3216782 0.3644938 0.3280265 0.1895929
## 
## , , 3, 2, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1773816 0.4304985 0.4700362 0.1782035
## [2,] 0.4303713 0.6788874 0.5054112 0.4230808
## [3,] 0.4811161 0.5451530 0.4906109 0.2835635
## 
## , , 1, 3, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.03359232 0.08152728 0.08901489 0.03374797
## [2,] 0.08150321 0.12856688 0.09571417 0.08012254
## [3,] 0.09111320 0.10324043 0.09291130 0.05370092
## 
## , , 2, 3, 1
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.08111812 0.1968706 0.2149515 0.08149398
## [2,] 0.19681247 0.3104610 0.2311288 0.19347844
## [3,] 0.22001849 0.2493031 0.2243605 0.12967602
## 
## , , 3, 3, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.2223578 0.5396539 0.5892167 0.2233881
## [2,] 0.5394945 0.8510233 0.6335612 0.5303554
## [3,] 0.6031060 0.6833798 0.6150082 0.3554628
## 
## , , 1, 4, 1
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.07958444 0.1931484 0.2108875 0.07995319
## [2,] 0.19309137 0.3045911 0.2267589 0.18982039
## [3,] 0.21585864 0.2445896 0.2201186 0.12722426
## 
## , , 2, 4, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1729927 0.4198467 0.4584062 0.1737943
## [2,] 0.4197228 0.6620899 0.4929060 0.4126126
## [3,] 0.4692120 0.5316645 0.4784718 0.2765474
## 
## , , 3, 4, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1999321 0.4852275 0.5297917 0.2008584
## [2,] 0.4850843 0.7651940 0.5696639 0.4768669
## [3,] 0.5422802 0.6144581 0.5529820 0.3196128
## 
## , , 1, 1, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1300122 0.3155347 0.3445139 0.1306146
## [2,] 0.3154415 0.4975918 0.3704421 0.3100979
## [3,] 0.3526350 0.3995710 0.3595942 0.2078384
## 
## , , 2, 1, 2
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.04712839 0.1143788 0.1248836 0.04734675
## [2,] 0.11434503 0.1803731 0.1342823 0.11240802
## [3,] 0.12782738 0.1448413 0.1303500 0.07533978
## 
## , , 3, 1, 2
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.07604745 0.1845643 0.2015150 0.0763998
## [2,] 0.18450976 0.2910541 0.2166810 0.1813841
## [3,] 0.20626517 0.2337192 0.2103358 0.1215700
## 
## , , 1, 2, 2
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.08318165 0.2018787 0.2204196 0.08356706
## [2,] 0.20181909 0.3183586 0.2370084 0.19840025
## [3,] 0.22561543 0.2556450 0.2300679 0.13297478
## 
## , , 2, 2, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1812884 0.4399799 0.4803885 0.1821283
## [2,] 0.4398500 0.6938395 0.5165426 0.4323989
## [3,] 0.4917124 0.5571597 0.5014163 0.2898089
## 
## , , 3, 2, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1312915 0.3186395 0.3479040 0.1318999
## [2,] 0.3185455 0.5024881 0.3740873 0.3131493
## [3,] 0.3561049 0.4035028 0.3631326 0.2098836
## 
## , , 1, 3, 2
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.07286549 0.1768418 0.1930832 0.07320311
## [2,] 0.17678955 0.2788759 0.2076147 0.17379472
## [3,] 0.19763469 0.2239400 0.2015350 0.11648330
## 
## , , 2, 3, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1997271 0.4847302 0.5292487 0.2006526
## [2,] 0.4845871 0.7644097 0.5690800 0.4763781
## [3,] 0.5417244 0.6138283 0.5524152 0.3192853
## 
## , , 3, 3, 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01812919 0.04399886 0.04803979 0.01821319
## [2,] 0.04398587 0.06938531 0.05165528 0.04324074
## [3,] 0.04917221 0.05571707 0.05014261 0.02898146
## 
## , , 1, 4, 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.03512850 0.08525554 0.09308556 0.03529127
## [2,] 0.08523036 0.13444626 0.10009120 0.08378655
## [3,] 0.09527981 0.10796163 0.09716015 0.05615668
## 
## , , 2, 4, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1515852 0.3678916 0.4016794 0.1522876
## [2,] 0.3677829 0.5801576 0.4319099 0.3615527
## [3,] 0.4111480 0.4658721 0.4192619 0.2423252
## 
## , , 3, 4, 2
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.04760223 0.1155288 0.1261392 0.04782279
## [2,] 0.11549469 0.1821866 0.1356324 0.11353820
## [3,] 0.12911259 0.1462976 0.1316606 0.07609727
## 
## , , 1, 1, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1551574 0.3765612 0.4111452 0.1558763
## [2,] 0.3764500 0.5938294 0.4420881 0.3700729
## [3,] 0.4208370 0.4768507 0.4291421 0.2480358
## 
## , , 2, 1, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1216726 0.2952948 0.3224151 0.1222363
## [2,] 0.2952076 0.4656739 0.3466802 0.2902067
## [3,] 0.3300153 0.3739406 0.3365281 0.1945067
## 
## , , 3, 1, 3
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02798359 0.06791510 0.07415255 0.02811325
## [2,] 0.06789505 0.10710075 0.07973328 0.06674490
## [3,] 0.07590051 0.08600292 0.07739839 0.04473477
## 
## , , 1, 2, 3
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.09801572 0.2378804 0.2597278 0.09846986
## [2,] 0.23781017 0.3751326 0.2792749 0.23378164
## [3,] 0.26585020 0.3012351 0.2710967 0.15668863
## 
## , , 2, 2, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1818140 0.4412557 0.4817814 0.1826564
## [2,] 0.4411254 0.6958513 0.5180403 0.4336527
## [3,] 0.4931382 0.5587752 0.5028702 0.2906492
## 
## , , 3, 2, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1069624 0.2595938 0.2834353 0.1074580
## [2,] 0.2595171 0.4093742 0.3047667 0.2551209
## [3,] 0.2901166 0.3287314 0.2958420 0.1709909
## 
## , , 1, 3, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1741827 0.4227347 0.4615595 0.1749897
## [2,] 0.4226099 0.6666442 0.4962965 0.4154509
## [3,] 0.4724396 0.5353216 0.4817631 0.2784497
## 
## , , 2, 3, 3
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.0593354 0.1440048 0.1572304 0.05961032
## [2,] 0.1439622 0.2270926 0.1690636 0.14152349
## [3,] 0.1609367 0.1823575 0.1641128 0.09485399
## 
## , , 3, 3, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1592545 0.3865046 0.4220019 0.1599924
## [2,] 0.3863905 0.6095100 0.4537619 0.3798450
## [3,] 0.4319495 0.4894424 0.4404740 0.2545854
## 
## , , 1, 4, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1593208 0.3866656 0.4221777 0.1600590
## [2,] 0.3865515 0.6097639 0.4539509 0.3800032
## [3,] 0.4321295 0.4896462 0.4406575 0.2546915
## 
## , , 2, 4, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.2215242 0.5376309 0.5870079 0.2225507
## [2,] 0.5374722 0.8478331 0.6311862 0.5283673
## [3,] 0.6008451 0.6808181 0.6127027 0.3541303
## 
## , , 3, 4, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.2032579 0.4932992 0.5386047 0.2041997
## [2,] 0.4931536 0.7779230 0.5791402 0.4847995
## [3,] 0.5513010 0.6246796 0.5621808 0.3249296
## 
## , , 1, 1, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1077539 0.2615145 0.2855325 0.1082531
## [2,] 0.2614373 0.4124032 0.3070217 0.2570085
## [3,] 0.2922632 0.3311637 0.2980310 0.1722561
## 
## , , 2, 1, 4
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.05891919 0.1429946 0.1561275 0.05919218
## [2,] 0.14295240 0.2254996 0.1678777 0.14053077
## [3,] 0.15980782 0.1810784 0.1629616 0.09418864
## 
## , , 3, 1, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1002245 0.2432411 0.2655808 0.1006889
## [2,] 0.2431693 0.3835863 0.2855685 0.2390500
## [3,] 0.2718412 0.3080235 0.2772059 0.1602196
## 
## , , 1, 2, 4
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.08470692 0.2055805 0.2244614 0.0850994
## [2,] 0.20551977 0.3241963 0.2413544 0.2020382
## [3,] 0.22975247 0.2603327 0.2342866 0.1354131
## 
## , , 2, 2, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1510029 0.3664782 0.4001362 0.1517025
## [2,] 0.3663700 0.5779287 0.4302505 0.3601636
## [3,] 0.4095684 0.4640823 0.4176512 0.2413942
## 
## , , 3, 2, 4
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.08283823 0.2010452 0.2195096 0.08322205
## [2,] 0.20098587 0.3170443 0.2360299 0.19758115
## [3,] 0.22468397 0.2545896 0.2291181 0.13242579
## 
## , , 1, 3, 4
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.07486244 0.1816883 0.1983749 0.0752093
## [2,] 0.18163463 0.2865188 0.2133046 0.1785577
## [3,] 0.20305104 0.2300773 0.2070582 0.1196756
## 
## , , 2, 3, 4
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.04802152 0.1165464 0.1272502 0.04824403
## [2,] 0.11651199 0.1837913 0.1368271 0.11453827
## [3,] 0.13024984 0.1475862 0.1328203 0.07676755
## 
## , , 3, 3, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.2280494 0.5534671 0.6042985 0.2291060
## [2,] 0.5533037 0.8728065 0.6497782 0.5439307
## [3,] 0.6185433 0.7008719 0.6307502 0.3645613
## 
## , , 1, 4, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1825495 0.4430407 0.4837303 0.1833953
## [2,] 0.4429099 0.6986662 0.5201360 0.4354069
## [3,] 0.4951330 0.5610356 0.5049044 0.2918249
## 
## , , 2, 4, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1738329 0.4218859 0.4606327 0.1746384
## [2,] 0.4217613 0.6653056 0.4953000 0.4146167
## [3,] 0.4714909 0.5342467 0.4807957 0.2778906
## 
## , , 3, 4, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1184002 0.2873527 0.3137437 0.1189488
## [2,] 0.2872678 0.4531494 0.3373561 0.2824015
## [3,] 0.3211394 0.3638833 0.3274770 0.1892753
## 
## , , 1, 1, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1558398 0.3782173 0.4129534 0.1565619
## [2,] 0.3781056 0.5964410 0.4440324 0.3717004
## [3,] 0.4226878 0.4789478 0.4310294 0.2491266
## 
## , , 2, 1, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.03469573 0.08420523 0.09193878 0.03485649
## [2,] 0.08418036 0.13278994 0.09885812 0.08275434
## [3,] 0.09410601 0.10663159 0.09596318 0.05546485
## 
## , , 3, 1, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.2273704 0.5518193 0.6024993 0.2284239
## [2,] 0.5516563 0.8702078 0.6478435 0.5423112
## [3,] 0.6167017 0.6987852 0.6288722 0.3634759
## 
## , , 1, 2, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1830860 0.4443429 0.4851521 0.1839344
## [2,] 0.4442117 0.7007197 0.5216648 0.4366867
## [3,] 0.4965883 0.5626846 0.5063884 0.2926827
## 
## , , 2, 2, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.2093598 0.5081084 0.5547739 0.2103299
## [2,] 0.5079583 0.8012766 0.5965263 0.4993534
## [3,] 0.5678513 0.6434328 0.5790578 0.3346841
## 
## , , 3, 2, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1730121 0.4198938 0.4584576 0.1738137
## [2,] 0.4197698 0.6621641 0.4929612 0.4126589
## [3,] 0.4692646 0.5317241 0.4785254 0.2765784
## 
## , , 1, 3, 5
## 
##             [,1]       [,2]       [,3]        [,4]
## [1,] 0.008828473 0.02142637 0.02339420 0.008869379
## [2,] 0.021420040 0.03378895 0.02515486 0.021057183
## [3,] 0.023945662 0.02713285 0.02441823 0.014113260
## 
## , , 2, 3, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1986243 0.4820537 0.5263263 0.1995446
## [2,] 0.4819113 0.7601889 0.5659378 0.4737477
## [3,] 0.5387332 0.6104390 0.5493650 0.3175223
## 
## , , 3, 3, 5
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.04992409 0.1211639 0.1322918 0.05015541
## [2,] 0.12112808 0.1910730 0.1422481 0.11907616
## [3,] 0.13541021 0.1534334 0.1380825 0.07980901
## 
## , , 1, 4, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1474448 0.3578430 0.3907080 0.1481280
## [2,] 0.3577374 0.5643112 0.4201127 0.3516773
## [3,] 0.3999179 0.4531473 0.4078102 0.2357064
## 
## , , 2, 4, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1407879 0.3416869 0.3730680 0.1414402
## [2,] 0.3415860 0.5388333 0.4011452 0.3357995
## [3,] 0.3818621 0.4326883 0.3893981 0.2250645
## 
## , , 3, 4, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1628391 0.3952043 0.4315005 0.1635936
## [2,] 0.3950876 0.6232291 0.4639753 0.3883947
## [3,] 0.4416721 0.5004589 0.4503884 0.2603157
DelayedTensor::einsum('ij,klm->ijklm', darrC, darrE)
## <3 x 4 x 3 x 4 x 5> array of class HDF5Array and type "double":
## ,,1,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.09090300 0.22061813 0.24088009 0.09132419
## [2,] 0.22055298 0.34791033 0.25900877 0.21681679
## [3,] 0.24655823 0.27937532 0.25142401 0.14531819
## 
## ,,2,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.03851239 0.09346811 0.10205239 0.03869084
## [2,] 0.09344051 0.14739733 0.10973287 0.09185762
## [3,] 0.10445802 0.11836146 0.10651948 0.06156619
## 
## ,,3,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.06983767 0.16949338 0.18505995 0.07016126
## [2,] 0.16944333 0.26728764 0.19898760 0.16657294
## [3,] 0.18942227 0.21463452 0.19316049 0.11164301
## 
## ...
## 
## ,,1,4,5
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1474448 0.3578430 0.3907080 0.1481280
## [2,] 0.3577374 0.5643112 0.4201127 0.3516773
## [3,] 0.3999179 0.4531473 0.4078102 0.2357064
## 
## ,,2,4,5
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1407879 0.3416869 0.3730680 0.1414402
## [2,] 0.3415860 0.5388333 0.4011452 0.3357995
## [3,] 0.3818621 0.4326883 0.3893981 0.2250645
## 
## ,,3,4,5
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1628391 0.3952043 0.4315005 0.1635936
## [2,] 0.3950876 0.6232291 0.4639753 0.3883947
## [3,] 0.4416721 0.5004589 0.4503884 0.2603157

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] 0.8738674
DelayedTensor::einsum('i->', darrA)
## <1> array of class HDF5Array and type "double":
##       [1] 
## 0.8738674
einsum::einsum('ij->', arrC)
## [1] 6.624215
DelayedTensor::einsum('ij->', darrC)
## <1> array of class HDF5Array and type "double":
##      [1] 
## 6.624215
einsum::einsum('ijk->', arrE)
## [1] 32.28118
DelayedTensor::einsum('ijk->', darrE)
## <1> array of class HDF5Array and type "double":
##      [1] 
## 32.28118

3.3.1 Row-wise / Column-wise Summation

einsum::einsum('ij->i', arrC)
## [1] 1.633352 2.649719 2.341144
DelayedTensor::einsum('ij->i', darrC)
## <3> array of class HDF5Array and type "double":
##      [1]      [2]      [3] 
## 1.633352 2.649719 2.341144
einsum::einsum('ij->j', arrC)
## [1] 1.415873 2.151422 1.906338 1.150581
DelayedTensor::einsum('ij->j', darrC)
## <4> array of class HDF5Array and type "double":
##      [1]      [2]      [3]      [4] 
## 1.415873 2.151422 1.906338 1.150581

3.3.2 Mode-wise Vectorization

einsum::einsum('ijk->i', arrE)
## [1]  9.852426 11.231384 11.197368
DelayedTensor::einsum('ijk->i', darrE)
## <3> array of class HDF5Array and type "double":
##       [1]       [2]       [3] 
##  9.852426 11.231384 11.197368
einsum::einsum('ijk->j', arrE)
## [1] 6.252094 9.442822 7.062035 9.524228
DelayedTensor::einsum('ijk->j', darrE)
## <4> array of class HDF5Array and type "double":
##      [1]      [2]      [3]      [4] 
## 6.252094 9.442822 7.062035 9.524228
einsum::einsum('ijk->k', arrE)
## [1] 6.504504 5.089863 7.233759 6.118140 7.334912
DelayedTensor::einsum('ijk->k', darrE)
## <5> array of class HDF5Array and type "double":
##      [1]      [2]      [3]      [4]      [5] 
## 6.504504 5.089863 7.233759 6.118140 7.334912

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,] 2.773296 2.880774 1.579571 2.618785
## [2,] 1.304685 3.650797 2.544207 3.731695
## [3,] 2.174113 2.911251 2.938257 3.173748
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> matrix of class HDF5Matrix and type "double":
##          [,1]     [,2]     [,3]     [,4]
## [1,] 2.773296 2.880774 1.579571 2.618785
## [2,] 1.304685 3.650797 2.544207 3.731695
## [3,] 2.174113 2.911251 2.938257 3.173748
einsum::einsum('ijk->jk', arrE)
##           [,1]     [,2]     [,3]     [,4]     [,5]
## [1,] 0.8638686 1.097706 1.321530 1.157144 1.811846
## [2,] 2.2173944 1.715838 1.676951 1.381076 2.451563
## [3,] 1.4613711 1.260435 1.702879 1.521484 1.115866
## [4,] 1.9618695 1.015885 2.532399 2.058436 1.955638
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.8638686 1.0977056 1.3215300 1.1571437 1.8118457
## [2,] 2.2173944 1.7158379 1.6769509 1.3810760 2.4515628
## [3,] 1.4613711 1.2604346 1.7028792 1.5214836 1.1158664
## [4,] 1.9618695 1.0158850 2.5323990 2.0584365 1.9556376
einsum::einsum('ijk->jk', arrE)
##           [,1]     [,2]     [,3]     [,4]     [,5]
## [1,] 0.8638686 1.097706 1.321530 1.157144 1.811846
## [2,] 2.2173944 1.715838 1.676951 1.381076 2.451563
## [3,] 1.4613711 1.260435 1.702879 1.521484 1.115866
## [4,] 1.9618695 1.015885 2.532399 2.058436 1.955638
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.8638686 1.0977056 1.3215300 1.1571437 1.8118457
## [2,] 2.2173944 1.7158379 1.6769509 1.3810760 2.4515628
## [3,] 1.4613711 1.2604346 1.7028792 1.5214836 1.1158664
## [4,] 1.9618695 1.0158850 2.5323990 2.0584365 1.9556376

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.476876
DelayedTensor::einsum('ii->', darrB)
## <1> array of class HDF5Array and type "double":
##      [1] 
## 1.476876

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.8486748 0.610428120 0.8016776
## [2,] 0.5055964 0.005582589 0.9735176
## [3,] 0.4763574 0.433875050 0.6226190
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> matrix of class DelayedArray and type "double":
##             [,1]        [,2]        [,3]
## [1,] 0.848674765 0.610428120 0.801677572
## [2,] 0.505596436 0.005582589 0.973517601
## [3,] 0.476357396 0.433875050 0.622618967
einsum::einsum('ijk->jki', arrD)
## , , 1
## 
##           [,1]      [,2]      [,3]
## [1,] 0.8355310 0.7260636 0.9468250
## [2,] 0.4983903 0.8234425 0.6118174
## [3,] 0.6909214 0.3490949 0.1348019
## 
## , , 2
## 
##            [,1]      [,2]      [,3]
## [1,] 0.27195408 0.3570537 0.7993990
## [2,] 0.06854225 0.5134344 0.8875421
## [3,] 0.10257546 0.7728944 0.3233688
## 
## , , 3
## 
##           [,1]      [,2]      [,3]
## [1,] 0.2368680 0.3383344 0.5961123
## [2,] 0.0444261 0.3617052 0.6819026
## [3,] 0.5979155 0.3627868 0.8110374
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> array of class DelayedArray and type "double":
## ,,1
##           [,1]      [,2]      [,3]
## [1,] 0.8355310 0.7260636 0.9468250
## [2,] 0.4983903 0.8234425 0.6118174
## [3,] 0.6909214 0.3490949 0.1348019
## 
## ,,2
##            [,1]       [,2]       [,3]
## [1,] 0.27195408 0.35705370 0.79939901
## [2,] 0.06854225 0.51343445 0.88754213
## [3,] 0.10257546 0.77289439 0.32336877
## 
## ,,3
##           [,1]      [,2]      [,3]
## [1,] 0.2368680 0.3383344 0.5961123
## [2,] 0.0444261 0.3617052 0.6819026
## [3,] 0.5979155 0.3627868 0.8110374

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.2935518
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> array of class HDF5Array and type "double":
##       [1] 
## 0.2935518
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 4.057632
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> array of class HDF5Array and type "double":
##      [1] 
## 4.057632
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 21.74186
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> array of class HDF5Array and type "double":
##      [1] 
## 21.74186

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,] 0.2748827 0.4681823 0.7455048 0.4723148 1.4508758
## [2,] 1.7284763 1.0718369 1.0169925 0.6924632 2.0166257
## [3,] 1.0742704 0.8558020 1.1131933 1.1262509 0.7898813
## [4,] 1.4329411 0.4977045 2.1761110 1.4578998 1.2796486
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.2748827 0.4681823 0.7455048 0.4723148 1.4508758
## [2,] 1.7284763 1.0718369 1.0169925 0.6924632 2.0166257
## [3,] 1.0742704 0.8558020 1.1131933 1.1262509 0.7898813
## [4,] 1.4329411 0.4977045 2.1761110 1.4578998 1.2796486

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.7938124 1.152389 1.016463
## [2,] 1.1523885 1.827008 1.597971
## [3,] 1.0164628 1.597971 1.436811
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> matrix of class HDF5Matrix and type "double":
##           [,1]      [,2]      [,3]
## [1,] 0.7938124 1.1523885 1.0164628
## [2,] 1.1523885 1.8270079 1.5979714
## [3,] 1.0164628 1.5979714 1.4368113

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.05320037 0.3131728 0.3913787
## [2,] 0.31335786 0.7792788 0.5024979
## [3,] 0.37355968 0.4319039 0.4069787
## [4,] 0.05369451 0.3026523 0.1359559
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> matrix of class HDF5Matrix and type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.05320037 0.31317281 0.39137871
## [2,] 0.31335786 0.77927880 0.50249792
## [3,] 0.37355968 0.43190392 0.40697873
## [4,] 0.05369451 0.30265232 0.13595593
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
## 
##            [,1]       [,2]      [,3]      [,4]        [,5]
## [1,] 0.15532515 0.31772663 0.4525125 0.2182484 0.456501431
## [2,] 0.87265664 0.13005900 0.1805830 0.1348724 0.630080214
## [3,] 0.02121121 0.09979968 0.5702894 0.1053448 0.001465064
## [4,] 0.11905337 0.02319555 0.4771233 0.6263926 0.408643452
## 
## , , 2
## 
##            [,1]       [,2]       [,3]       [,4]       [,5]
## [1,] 0.02787959 0.04174943 0.27827287 0.06525275 0.02262755
## [2,] 0.26439064 0.61776766 0.62135530 0.42860345 0.82389542
## [3,] 0.12368617 0.74982439 0.06617791 0.04334682 0.74156668
## [4,] 0.56252392 0.43191583 0.92241825 0.56800153 0.37257694
## 
## , , 3
## 
##            [,1]        [,2]       [,3]      [,4]       [,5]
## [1,] 0.09167795 0.108706276 0.01471947 0.1888136 0.97174682
## [2,] 0.59142900 0.324010259 0.21505423 0.1289873 0.56265005
## [3,] 0.92937299 0.006177919 0.47672598 0.9775592 0.04684957
## [4,] 0.75136385 0.042593170 0.77656948 0.2635057 0.49842825
DelayedTensor::einsum('ijk,ijk->jki', darrE, darrE)
## <4 x 5 x 3> array of class HDF5Array and type "double":
## ,,1
##             [,1]        [,2]        [,3]        [,4]        [,5]
## [1,] 0.155325155 0.317726628 0.452512462 0.218248413 0.456501431
## [2,] 0.872656637 0.130059001 0.180582969 0.134872425 0.630080214
## [3,] 0.021211206 0.099799681 0.570289399 0.105344836 0.001465064
## [4,] 0.119053370 0.023195547 0.477123251 0.626392608 0.408643452
## 
## ,,2
##            [,1]       [,2]       [,3]       [,4]       [,5]
## [1,] 0.02787959 0.04174943 0.27827287 0.06525275 0.02262755
## [2,] 0.26439064 0.61776766 0.62135530 0.42860345 0.82389542
## [3,] 0.12368617 0.74982439 0.06617791 0.04334682 0.74156668
## [4,] 0.56252392 0.43191583 0.92241825 0.56800153 0.37257694
## 
## ,,3
##             [,1]        [,2]        [,3]        [,4]        [,5]
## [1,] 0.091677947 0.108706276 0.014719470 0.188813636 0.971746820
## [2,] 0.591429003 0.324010259 0.215054231 0.128987309 0.562650046
## [3,] 0.929372992 0.006177919 0.476725978 0.977559223 0.046849572
## [4,] 0.751363852 0.042593170 0.776569477 0.263505657 0.498428248

3.7 Summation + Permutation

Some examples of combining Summation and Permutation are shown below.

einsum::einsum('ijk->ki', arrE)
##          [,1]     [,2]     [,3]
## [1,] 1.818956 1.782868 2.902680
## [2,] 1.392521 2.513436 1.183906
## [3,] 2.543557 2.533453 2.156748
## [4,] 1.950439 1.871982 2.295719
## [5,] 2.146953 2.529645 2.658314
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> matrix of class HDF5Matrix and type "double":
##          [,1]     [,2]     [,3]
## [1,] 1.818956 1.782868 2.902680
## [2,] 1.392521 2.513436 1.183906
## [3,] 2.543557 2.533453 2.156748
## [4,] 1.950439 1.871982 2.295719
## [5,] 2.146953 2.529645 2.658314

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.003664946 0.0074968599 0.01067717 0.00514964 0.0107712951
## [2,] 0.121281657 0.0180755757 0.02509739 0.01874454 0.0875684308
## [3,] 0.003514281 0.0165348510 0.09448578 0.01745357 0.0002427323
## [4,] 0.002835193 0.0005523898 0.01136244 0.01491721 0.0097316291
## 
## , , 2
## 
##             [,1]        [,2]        [,3]        [,4]        [,5]
## [1,] 0.002280889 0.003415611 0.022766105 0.005338469 0.001851209
## [2,] 0.053823601 0.125762698 0.126493055 0.087253396 0.167725372
## [3,] 0.013955394 0.084601978 0.007466791 0.004890780 0.083670268
## [4,] 0.044475294 0.034148918 0.072929916 0.044908374 0.029457358
## 
## , , 3
## 
##             [,1]         [,2]         [,3]       [,4]       [,5]
## [1,] 0.006067909 0.0071949673 0.0009742409 0.01249705 0.06431723
## [2,] 0.050259006 0.0275340463 0.0182750793 0.01096120 0.04781340
## [3,] 0.063964464 0.0004251978 0.0328108542 0.06728090 0.00322444
## [4,] 0.017275295 0.0009792986 0.0178548205 0.00605850 0.01145982
DelayedTensor::einsum('i,ij,ijk,ijk,ji->jki',
    darrA, darrC, darrE, darrE, t(darrC))
## <4 x 5 x 3> array of class HDF5Array and type "double":
## ,,1
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.0036649460 0.0074968599 0.0106771741 0.0051496401 0.0107712951
## [2,] 0.1212816569 0.0180755757 0.0250973875 0.0187445445 0.0875684308
## [3,] 0.0035142811 0.0165348510 0.0944857755 0.0174535746 0.0002427323
## [4,] 0.0028351934 0.0005523898 0.0113624395 0.0149172108 0.0097316291
## 
## ,,2
##             [,1]        [,2]        [,3]        [,4]        [,5]
## [1,] 0.002280889 0.003415611 0.022766105 0.005338469 0.001851209
## [2,] 0.053823601 0.125762698 0.126493055 0.087253396 0.167725372
## [3,] 0.013955394 0.084601978 0.007466791 0.004890780 0.083670268
## [4,] 0.044475294 0.034148918 0.072929916 0.044908374 0.029457358
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.0060679094 0.0071949673 0.0009742409 0.0124970515 0.0643172303
## [2,] 0.0502590061 0.0275340463 0.0182750793 0.0109612040 0.0478134010
## [3,] 0.0639644642 0.0004251978 0.0328108542 0.0672809006 0.0032244404
## [4,] 0.0172752949 0.0009792986 0.0178548205 0.0060585000 0.0114598206

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.0 RC (2023-04-13 r84269)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 22.04.2 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.17-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.0             DelayedRandomArray_1.8.0 HDF5Array_1.28.0        
##  [4] rhdf5_2.44.0             DelayedArray_0.26.0      IRanges_2.34.0          
##  [7] S4Vectors_0.38.0         MatrixGenerics_1.12.0    matrixStats_0.63.0      
## [10] BiocGenerics_0.46.0      Matrix_1.5-4             DelayedTensor_1.6.0     
## [13] BiocStyle_2.28.0        
## 
## loaded via a namespace (and not attached):
##  [1] jsonlite_1.8.4      compiler_4.3.0      BiocManager_1.30.20
##  [4] Rcpp_1.0.10         rsvd_1.0.5          rhdf5filters_1.12.0
##  [7] parallel_4.3.0      jquerylib_0.1.4     BiocParallel_1.34.0
## [10] yaml_2.3.7          fastmap_1.1.1       lattice_0.21-8     
## [13] R6_2.5.1            ScaledMatrix_1.8.0  knitr_1.42         
## [16] bookdown_0.33       bslib_0.4.2         rlang_1.1.0        
## [19] cachem_1.0.7        xfun_0.39           sass_0.4.5         
## [22] cli_3.6.1           Rhdf5lib_1.22.0     BiocSingular_1.16.0
## [25] digest_0.6.31       grid_4.3.0          irlba_2.3.5.1      
## [28] rTensor_1.4.8       dqrng_0.3.0         evaluate_0.20      
## [31] codetools_0.2-19    beachmat_2.16.0     rmarkdown_2.21     
## [34] tools_4.3.0         htmltools_0.5.5