Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2023-05-01 13:34:10
Compiled: Tue May 9 00:22:17 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.1510672 0.2246076 0.4261232
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.1510672 0.2246076 0.4261232
einsum::einsum('iii->i', arrD)
## [1] 0.6755720 0.6702890 0.6797056
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.6755720 0.6702890 0.6797056

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.69107148 0.61979847 0.05405736
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
##        [1]        [2]        [3] 
## 0.69107148 0.61979847 0.05405736
einsum::einsum('ij,ij->ij', arrC, arrC)
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.18070780 0.06717617 0.90261008 0.06069983
## [2,] 0.05419806 0.01730340 0.33532980 0.32371035
## [3,] 0.01348364 0.00112144 0.09089881 0.52514145
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.18070780 0.06717617 0.90261008 0.06069983
## [2,] 0.05419806 0.01730340 0.33532980 0.32371035
## [3,] 0.01348364 0.00112144 0.09089881 0.52514145
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
## 
##           [,1]         [,2]       [,3]       [,4]
## [1,] 0.2236511 0.5569194908 0.07754969 0.88457516
## [2,] 0.1148153 0.0007463863 0.10210111 0.08650255
## [3,] 0.1804164 0.2931529212 0.64616649 0.24263909
## 
## , , 2
## 
##            [,1]      [,2]       [,3]         [,4]
## [1,] 0.71095682 0.3291066 0.05849170 1.487335e-01
## [2,] 0.18209829 0.2693891 0.09777537 6.666486e-06
## [3,] 0.09225274 0.0698737 0.07174055 1.813436e-02
## 
## , , 3
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.1468196 0.24715121 0.2899868 0.8663305
## [2,] 0.9075201 0.05786292 0.2945959 0.1844369
## [3,] 0.1501081 0.09774479 0.1439349 0.4633417
## 
## , , 4
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.6396868 0.02332533 0.7020208 0.01209069
## [2,] 0.6538618 0.53767066 0.1255931 0.97098832
## [3,] 0.2792073 0.39132540 0.1663885 0.19621791
## 
## , , 5
## 
##           [,1]         [,2]       [,3]      [,4]
## [1,] 0.1328759 0.2161727905 0.01396859 0.1000163
## [2,] 0.3203467 0.0004533506 0.22747365 0.2035031
## [3,] 0.9476363 0.0107812457 0.42198434 0.4115914
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.2236511205 0.5569194908 0.0775496879 0.8845751646
## [2,] 0.1148152550 0.0007463863 0.1021011134 0.0865025457
## [3,] 0.1804163567 0.2931529212 0.6461664890 0.2426390935
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]
## [1,] 7.109568e-01 3.291066e-01 5.849170e-02 1.487335e-01
## [2,] 1.820983e-01 2.693891e-01 9.777537e-02 6.666486e-06
## [3,] 9.225274e-02 6.987370e-02 7.174055e-02 1.813436e-02
## 
## ,,3
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.14681957 0.24715121 0.28998677 0.86633048
## [2,] 0.90752007 0.05786292 0.29459590 0.18443688
## [3,] 0.15010810 0.09774479 0.14393489 0.46334173
## 
## ,,4
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.63968677 0.02332533 0.70202081 0.01209069
## [2,] 0.65386181 0.53767066 0.12559308 0.97098832
## [3,] 0.27920728 0.39132540 0.16638851 0.19621791
## 
## ,,5
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.1328759080 0.2161727905 0.0139685876 0.1000163367
## [2,] 0.3203466534 0.0004533506 0.2274736485 0.2035030722
## [3,] 0.9476362890 0.0107812457 0.4219843361 0.4115914302

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.6910715 0.6544655 0.19328088
## [2,] 0.6544655 0.6197985 0.18304281
## [3,] 0.1932809 0.1830428 0.05405736
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.69107148 0.65446547 0.19328088
## [2,] 0.65446547 0.61979847 0.18304281
## [3,] 0.19328088 0.18304281 0.05405736
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.20103607 0.12257253 0.4492992 0.1165143
## [2,] 0.11009749 0.06220871 0.2738556 0.2690691
## [3,] 0.05491476 0.01583702 0.1425820 0.3427076
## 
## , , 2, 1, 1
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.14404170 0.08782282 0.3219214 0.08348213
## [2,] 0.07888450 0.04457234 0.1962167 0.19278715
## [3,] 0.03934625 0.01134718 0.1021595 0.24554887
## 
## , , 3, 1, 1
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.18056202 0.11008941 0.4035414 0.1046482
## [2,] 0.09888487 0.05587321 0.2459654 0.2416664
## [3,] 0.04932210 0.01422414 0.1280610 0.3078053
## 
## , , 1, 2, 1
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.31723760 0.19342108 0.7090001 0.1838611
## [2,] 0.17373531 0.09816618 0.4321478 0.4245946
## [3,] 0.08665623 0.02499104 0.2249963 0.5407971
## 
## , , 2, 2, 1
## 
##             [,1]         [,2]        [,3]        [,4]
## [1,] 0.011613691 0.0070809157 0.025955650 0.006730937
## [2,] 0.006360243 0.0035937469 0.015820416 0.015543904
## [3,] 0.003172381 0.0009148921 0.008236845 0.019797938
## 
## , , 3, 2, 1
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.23016303 0.14033135 0.5143955 0.1333954
## [2,] 0.12604888 0.07122177 0.3135329 0.3080530
## [3,] 0.06287104 0.01813156 0.1632399 0.3923605
## 
## , , 1, 3, 1
## 
##            [,1]        [,2]       [,3]       [,4]
## [1,] 0.11838004 0.072176801 0.26456971 0.06860942
## [2,] 0.06483088 0.036631583 0.16125979 0.15844127
## [3,] 0.03233654 0.009325628 0.08395936 0.20180326
## 
## , , 2, 3, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.13583250 0.08281764 0.30357453 0.07872433
## [2,] 0.07438873 0.04203208 0.18503390 0.18179986
## [3,] 0.03710384 0.01070048 0.09633727 0.23155459
## 
## , , 3, 3, 1
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.34171234 0.20834343 0.7636991 0.1980459
## [2,] 0.18713891 0.10573965 0.4654878 0.4573519
## [3,] 0.09334172 0.02691908 0.2423546 0.5825194
## 
## , , 1, 4, 1
## 
##           [,1]     [,2]      [,3]      [,4]
## [1,] 0.3998120 0.243767 0.8935471 0.2317187
## [2,] 0.2189572 0.123718 0.5446324 0.5351132
## [3,] 0.1092121 0.031496 0.2835610 0.6815622
## 
## , , 2, 4, 1
## 
##            [,1]        [,2]       [,3]       [,4]
## [1,] 0.12502674 0.076229321 0.27942453 0.07246164
## [2,] 0.06847094 0.038688341 0.17031407 0.16733729
## [3,] 0.03415215 0.009849235 0.08867344 0.21313393
## 
## , , 3, 4, 1
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.20939622 0.12766975 0.4679834 0.1213596
## [2,] 0.11467593 0.06479568 0.2852440 0.2802584
## [3,] 0.05719841 0.01649561 0.1485113 0.3569592
## 
## , , 1, 1, 2
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.35843471 0.21853913 0.8010723 0.2077377
## [2,] 0.19629692 0.11091423 0.4882674 0.4797333
## [3,] 0.09790958 0.02823642 0.2542147 0.6110261
## 
## , , 2, 1, 2
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.18140171 0.11060138 0.4054180 0.1051348
## [2,] 0.09934472 0.05613304 0.2471093 0.2427902
## [3,] 0.04955146 0.01429029 0.1286566 0.3092367
## 
## , , 3, 1, 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.12911541 0.07872220 0.28856239 0.07483131
## [2,] 0.07071011 0.03995354 0.17588375 0.17280963
## [3,] 0.03526900 0.01017133 0.09157327 0.22010392
## 
## , , 1, 2, 2
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.24386908 0.14868799 0.5450275 0.1413390
## [2,] 0.13355501 0.07546298 0.3322036 0.3263973
## [3,] 0.06661497 0.01921128 0.1729607 0.4157253
## 
## , , 2, 2, 2
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.22063706 0.13452333 0.4931058 0.1278744
## [2,] 0.12083198 0.06827405 0.3005565 0.2953033
## [3,] 0.06026894 0.01738113 0.1564837 0.3761215
## 
## , , 3, 2, 2
## 
##            [,1]        [,2]       [,3]       [,4]
## [1,] 0.11236869 0.068511656 0.25113483 0.06512543
## [2,] 0.06153876 0.034771428 0.15307100 0.15039561
## [3,] 0.03069449 0.008852071 0.07969589 0.19155567
## 
## , , 1, 3, 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.10281005 0.06268371 0.22977205 0.05958554
## [2,] 0.05630397 0.03181360 0.14005003 0.13760221
## [3,] 0.02808346 0.00809907 0.07291657 0.17526099
## 
## , , 2, 3, 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.13292393 0.08104427 0.29707412 0.07703861
## [2,] 0.07279585 0.04113205 0.18107179 0.17790699
## [3,] 0.03630933 0.01047135 0.09427441 0.22659633
## 
## , , 3, 3, 2
## 
##            [,1]        [,2]       [,3]       [,4]
## [1,] 0.11385990 0.069420856 0.25446757 0.06598969
## [2,] 0.06235542 0.035232870 0.15510237 0.15239147
## [3,] 0.03110183 0.008969545 0.08075352 0.19409775
## 
## , , 1, 4, 2
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.16394297 0.09995671 0.3663991 0.09501629
## [2,] 0.08978344 0.05073060 0.2233266 0.21942325
## [3,] 0.04478246 0.01291494 0.1162742 0.27947470
## 
## , , 2, 4, 2
## 
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.0010975819 6.692003e-04 0.0024530059 0.0006361247
## [2,] 0.0006010912 3.396364e-04 0.0014951493 0.0014690169
## [3,] 0.0002998141 8.646424e-05 0.0007784444 0.0018710554
## 
## , , 3, 4, 2
## 
##            [,1]        [,2]       [,3]       [,4]
## [1,] 0.05724526 0.034902671 0.12793847 0.03317759
## [2,] 0.03135039 0.017714003 0.07798070 0.07661774
## [3,] 0.01563704 0.004509611 0.04060038 0.09758638
## 
## , , 1, 1, 3
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.16288475 0.09931151 0.3640341 0.09440298
## [2,] 0.08920390 0.05040315 0.2218851 0.21800692
## [3,] 0.04449339 0.01283158 0.1155237 0.27767075
## 
## , , 2, 1, 3
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.4049641 0.24690832 0.9050617 0.2347047
## [2,] 0.2217788 0.12531232 0.5516507 0.5420089
## [3,] 0.1106195 0.03190187 0.2872151 0.6903451
## 
## , , 3, 1, 3
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.16469883 0.10041756 0.3680884 0.09545436
## [2,] 0.09019738 0.05096449 0.2243562 0.22043490
## [3,] 0.04498893 0.01297448 0.1168103 0.28076322
## 
## , , 1, 2, 3
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.21133422 0.12885135 0.4723147 0.1224828
## [2,] 0.11573727 0.06539538 0.2878839 0.2828523
## [3,] 0.05772779 0.01664828 0.1498858 0.3602629
## 
## , , 2, 2, 3
## 
##            [,1]        [,2]       [,3]      [,4]
## [1,] 0.10225596 0.062345885 0.22853371 0.0592644
## [2,] 0.05600052 0.031642140 0.13929524 0.1368606
## [3,] 0.02793211 0.008055421 0.07252359 0.1743164
## 
## , , 3, 2, 3
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.13290314 0.08103160 0.29702766 0.07702657
## [2,] 0.07278446 0.04112562 0.18104347 0.17787917
## [3,] 0.03630366 0.01046971 0.09425967 0.22656089
## 
## , , 1, 3, 3
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.22891674 0.13957148 0.5116102 0.1326731
## [2,] 0.12536635 0.07083612 0.3118352 0.3063849
## [3,] 0.06253061 0.01803338 0.1623559 0.3902359
## 
## , , 2, 3, 3
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.23072880 0.14067630 0.5156600 0.1337233
## [2,] 0.12635872 0.07139684 0.3143036 0.3088102
## [3,] 0.06302559 0.01817613 0.1636411 0.3933249
## 
## , , 3, 3, 3
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.16127665 0.09833104 0.3604401 0.09347097
## [2,] 0.08832322 0.04990553 0.2196945 0.21585461
## [3,] 0.04405413 0.01270489 0.1143832 0.27492940
## 
## , , 1, 4, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3956674 0.2412400 0.8842842 0.2293166
## [2,] 0.2166874 0.1224355 0.5389865 0.5295660
## [3,] 0.1080800 0.0311695 0.2806215 0.6744969
## 
## , , 2, 4, 3
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.18256282 0.11130931 0.4080130 0.1058078
## [2,] 0.09998061 0.05649234 0.2486909 0.2443443
## [3,] 0.04986863 0.01438176 0.1294801 0.3112161
## 
## , , 3, 4, 3
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.28936044 0.17642426 0.6466969 0.1677044
## [2,] 0.15846837 0.08953985 0.3941729 0.3872835
## [3,] 0.07904134 0.02279496 0.2052248 0.4932747
## 
## , , 1, 1, 4
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.33999469 0.20729618 0.7598603 0.1970504
## [2,] 0.18619824 0.10520814 0.4631480 0.4550530
## [3,] 0.09287252 0.02678377 0.2411364 0.5795913
## 
## , , 2, 1, 4
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.34374108 0.2095804 0.7682332 0.1992217
## [2,] 0.18824995 0.1063674 0.4682514 0.4600672
## [3,] 0.09389588 0.0270789 0.2437935 0.5859778
## 
## , , 3, 1, 4
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.2246218 0.13695282 0.5020113 0.1301838
## [2,] 0.1230142 0.06950708 0.3059845 0.3006365
## [3,] 0.0613574 0.01769503 0.1593098 0.3829142
## 
## , , 1, 2, 4
## 
##            [,1]        [,2]       [,3]       [,4]
## [1,] 0.06492357 0.039584170 0.14509887 0.03762770
## [2,] 0.03555542 0.020089985 0.08844026 0.08689449
## [3,] 0.01773444 0.005114486 0.04604612 0.11067565
## 
## , , 2, 2, 4
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.31170705 0.19004908 0.6966398 0.1806558
## [2,] 0.17070649 0.09645480 0.4246139 0.4171925
## [3,] 0.08514551 0.02455535 0.2210738 0.5313691
## 
## , , 3, 2, 4
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.26592396 0.16213494 0.5943183 0.1541213
## [2,] 0.14563337 0.08228766 0.3622472 0.3559158
## [3,] 0.07263946 0.02094870 0.1886028 0.4533224
## 
## , , 1, 3, 4
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.35617501 0.21716138 0.7960220 0.2064281
## [2,] 0.19505939 0.11021499 0.4851891 0.4767089
## [3,] 0.09729232 0.02805841 0.2526121 0.6071740
## 
## , , 2, 3, 4
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.15065075 0.09185239 0.3366921 0.08731253
## [2,] 0.08250395 0.04661745 0.2052196 0.20163278
## [3,] 0.04115157 0.01186782 0.1068469 0.25681536
## 
## , , 3, 3, 4
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.17340041 0.10572295 0.3875357 0.1004975
## [2,] 0.09496281 0.05365712 0.2362097 0.2320812
## [3,] 0.04736584 0.01365997 0.1229818 0.2955969
## 
## , , 1, 4, 4
## 
##            [,1]        [,2]       [,3]       [,4]
## [1,] 0.04674272 0.028499229 0.10446615 0.02709064
## [2,] 0.02559867 0.014464092 0.06367392 0.06256102
## [3,] 0.01276818 0.003682252 0.03315161 0.07968263
## 
## , , 2, 4, 4
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.4188856 0.25539630 0.9361751 0.2427732
## [2,] 0.2294029 0.12962019 0.5706149 0.5606416
## [3,] 0.1144223 0.03299856 0.2970887 0.7140772
## 
## , , 3, 4, 4
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.18830323 0.11480926 0.4208423 0.1091347
## [2,] 0.10312434 0.05826865 0.2565106 0.2520273
## [3,] 0.05143667 0.01483397 0.1335514 0.3210018
## 
## , , 1, 1, 5
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.15495713 0.09447801 0.3463165 0.08980838
## [2,] 0.08486234 0.04795002 0.2110859 0.20739650
## [3,] 0.04232790 0.01220706 0.1099011 0.26415648
## 
## , , 2, 1, 5
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.24060162 0.14669581 0.5377249 0.1394453
## [2,] 0.13176558 0.07445190 0.3277526 0.3220241
## [3,] 0.06572244 0.01895388 0.1706433 0.4101552
## 
## , , 3, 1, 5
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.4138179 0.25230650 0.9248492 0.2398361
## [2,] 0.2266276 0.12805204 0.5637115 0.5538589
## [3,] 0.1130380 0.03259935 0.2934945 0.7054382
## 
## , , 1, 2, 5
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.19764643 0.12050585 0.4417236 0.1145498
## [2,] 0.10824115 0.06115982 0.2692381 0.2645324
## [3,] 0.05398885 0.01557000 0.1401779 0.3369292
## 
## , , 2, 2, 5
## 
##             [,1]         [,2]        [,3]        [,4]
## [1,] 0.009051187 0.0055185464 0.020228663 0.005245789
## [2,] 0.004956886 0.0028008043 0.012329719 0.012114218
## [3,] 0.002472411 0.0007130256 0.006419426 0.015429620
## 
## , , 3, 2, 5
## 
##            [,1]        [,2]       [,3]       [,4]
## [1,] 0.04413904 0.026911758 0.09864715 0.02558163
## [2,] 0.02417277 0.013658410 0.06012714 0.05907623
## [3,] 0.01205697 0.003477143 0.03130499 0.07524413
## 
## , , 1, 3, 5
## 
##            [,1]        [,2]       [,3]       [,4]
## [1,] 0.05024174 0.030632599 0.11228619 0.02911857
## [2,] 0.02751491 0.015546832 0.06844037 0.06724416
## [3,] 0.01372397 0.003957895 0.03563324 0.08564744
## 
## , , 2, 3, 5
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.20274680 0.12361556 0.4531225 0.1175058
## [2,] 0.11103437 0.06273808 0.2761860 0.2713588
## [3,] 0.05538206 0.01597179 0.1437953 0.3456238
## 
## , , 3, 3, 5
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.27614464 0.16836653 0.6171607 0.1600449
## [2,] 0.15123073 0.08545035 0.3761701 0.3695953
## [3,] 0.07543132 0.02175385 0.1958517 0.4707456
## 
## , , 1, 4, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.13443858 0.08196776 0.30045924 0.07791646
## [2,] 0.07362535 0.04160075 0.18313508 0.17993422
## [3,] 0.03672307 0.01059067 0.09534865 0.22917837
## 
## , , 2, 4, 5
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.19176703 0.11692115 0.4285836 0.1111423
## [2,] 0.10502129 0.05934049 0.2612291 0.2566633
## [3,] 0.05238284 0.01510684 0.1360080 0.3269066
## 
## , , 3, 4, 5
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.27272290 0.16628029 0.6095134 0.1580618
## [2,] 0.14935681 0.08439152 0.3715089 0.3650156
## [3,] 0.07449665 0.02148430 0.1934248 0.4649126
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.20103607 0.12257253 0.44929918 0.11651431
## [2,] 0.11009749 0.06220871 0.27385559 0.26906910
## [3,] 0.05491476 0.01583702 0.14258198 0.34270756
## 
## ,,2,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.14404170 0.08782282 0.32192143 0.08348213
## [2,] 0.07888450 0.04457234 0.19621666 0.19278715
## [3,] 0.03934625 0.01134718 0.10215953 0.24554887
## 
## ,,3,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.18056202 0.11008941 0.40354135 0.10464818
## [2,] 0.09888487 0.05587321 0.24596541 0.24166639
## [3,] 0.04932210 0.01422414 0.12806105 0.30780531
## 
## ...
## 
## ,,1,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.13443858 0.08196776 0.30045924 0.07791646
## [2,] 0.07362535 0.04160075 0.18313508 0.17993422
## [3,] 0.03672307 0.01059067 0.09534865 0.22917837
## 
## ,,2,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.19176703 0.11692115 0.42858363 0.11114226
## [2,] 0.10502129 0.05934049 0.26122910 0.25666330
## [3,] 0.05238284 0.01510684 0.13600804 0.32690656
## 
## ,,3,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.27272290 0.16628029 0.60951339 0.15806179
## [2,] 0.14935681 0.08439152 0.37150891 0.36501563
## [3,] 0.07449665 0.02148430 0.19342484 0.46491259

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

3.3.1 Row-wise / Column-wise Summation

einsum::einsum('ij->i', arrC)
## [1] 1.880712 1.512379 1.175768
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
##      [1]      [2]      [3] 
## 1.880712 1.512379 1.175768
einsum::einsum('ij->j', arrC)
## [1] 0.7740212 0.4242139 1.8306289 1.5399953
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
##       [1]       [2]       [3]       [4] 
## 0.7740212 0.4242139 1.8306289 1.5399953

3.3.2 Mode-wise Vectorization

einsum::einsum('ijk->i', arrE)
## [1] 9.996404 8.803248 9.366643
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
##      [1]      [2]      [3] 
## 9.996404 8.803248 9.366643
einsum::einsum('ijk->j', arrE)
## [1] 8.574208 5.824015 6.529798 7.238274
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 8.574208 5.824015 6.529798 7.238274
einsum::einsum('ijk->k', arrE)
## [1] 5.680613 4.275976 6.275159 6.786844 5.147703
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4]      [5] 
## 5.680613 4.275976 6.275159 6.786844 5.147703

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.863596 2.434762 2.014888 2.683158
## [2,] 3.092821 1.541447 2.006323 2.162657
## [3,] 2.617790 1.847807 2.508587 2.392459
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]     [,4]
## [1,] 2.863596 2.434762 2.014888 2.683158
## [2,] 3.092821 1.541447 2.006323 2.162657
## [3,] 2.617790 1.847807 2.508587 2.392459
einsum::einsum('ijk->jk', arrE)
##          [,1]      [,2]     [,3]     [,4]      [,5]
## [1,] 1.236516 1.5736437 1.723247 2.136822 1.9039793
## [2,] 1.315026 1.3570415 1.050332 1.511547 0.5900687
## [3,] 1.401855 0.8223854 1.460659 1.600165 1.2447340
## [4,] 1.727216 0.5229056 2.040922 1.538310 1.4089207
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.2365161 1.5736437 1.7232468 2.1368221 1.9039793
## [2,] 1.3150264 1.3570415 1.0503318 1.5115467 0.5900687
## [3,] 1.4018549 0.8223854 1.4606586 1.6001654 1.2447340
## [4,] 1.7272158 0.5229056 2.0409220 1.5383100 1.4089207
einsum::einsum('ijk->jk', arrE)
##          [,1]      [,2]     [,3]     [,4]      [,5]
## [1,] 1.236516 1.5736437 1.723247 2.136822 1.9039793
## [2,] 1.315026 1.3570415 1.050332 1.511547 0.5900687
## [3,] 1.401855 0.8223854 1.460659 1.600165 1.2447340
## [4,] 1.727216 0.5229056 2.040922 1.538310 1.4089207
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.2365161 1.5736437 1.7232468 2.1368221 1.9039793
## [2,] 1.3150264 1.3570415 1.0503318 1.5115467 0.5900687
## [3,] 1.4018549 0.8223854 1.4606586 1.6001654 1.2447340
## [4,] 1.7272158 0.5229056 2.0409220 1.5383100 1.4089207

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

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.1510672 0.4619190 0.01416964
## [2,] 0.2511940 0.2246076 0.65651124
## [3,] 0.5405729 0.1780173 0.42612321
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.15106717 0.46191902 0.01416964
## [2,] 0.25119396 0.22460764 0.65651124
## [3,] 0.54057295 0.17801729 0.42612321
einsum::einsum('ijk->jki', arrD)
## , , 1
## 
##           [,1]      [,2]      [,3]
## [1,] 0.6755720 0.4685084 0.9096058
## [2,] 0.4874284 0.5640649 0.4744207
## [3,] 0.3713618 0.6930825 0.7874355
## 
## , , 2
## 
##           [,1]      [,2]      [,3]
## [1,] 0.4095722 0.6075899 0.7938976
## [2,] 0.4555237 0.6702890 0.5892084
## [3,] 0.1013367 0.3161663 0.7663911
## 
## , , 3
## 
##            [,1]      [,2]      [,3]
## [1,] 0.48375967 0.8548515 0.2909169
## [2,] 0.51505899 0.6619586 0.7830392
## [3,] 0.02876147 0.1999329 0.6797056
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
##           [,1]      [,2]      [,3]
## [1,] 0.6755720 0.4685084 0.9096058
## [2,] 0.4874284 0.5640649 0.4744207
## [3,] 0.3713618 0.6930825 0.7874355
## 
## ,,2
##           [,1]      [,2]      [,3]
## [1,] 0.4095722 0.6075899 0.7938976
## [2,] 0.4555237 0.6702890 0.5892084
## [3,] 0.1013367 0.3161663 0.7663911
## 
## ,,3
##            [,1]       [,2]       [,3]
## [1,] 0.48375967 0.85485151 0.29091685
## [2,] 0.51505899 0.66195860 0.78303919
## [3,] 0.02876147 0.19993293 0.67970562

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

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.5188827 0.9853078 1.2044477 1.5727559 1.4008589
## [2,] 0.8508188 0.6683694 0.4027589 0.9523214 0.2274074
## [3,] 0.8258173 0.2280076 0.7285176 0.9940024 0.6634266
## [4,] 1.2137168 0.1668745 1.5141091 1.1792969 0.7151108
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.5188827 0.9853078 1.2044477 1.5727559 1.4008589
## [2,] 0.8508188 0.6683694 0.4027589 0.9523214 0.2274074
## [3,] 0.8258173 0.2280076 0.7285176 0.9940024 0.6634266
## [4,] 1.2137168 0.1668745 1.5141091 1.1792969 0.7151108

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.2111939 0.8233902 0.5230170
## [2,] 0.8233902 0.7305416 0.6183294
## [3,] 0.5230170 0.6183294 0.6306453
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 1.2111939 0.8233902 0.5230170
## [2,] 0.8233902 0.7305416 0.6183294
## [3,] 0.5230170 0.6183294 0.6306453

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.18070780 0.05419806 0.01348364
## [2,] 0.06717617 0.01730340 0.00112144
## [3,] 0.90261008 0.33532980 0.09089881
## [4,] 0.06069983 0.32371035 0.52514145
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.18070780 0.05419806 0.01348364
## [2,] 0.06717617 0.01730340 0.00112144
## [3,] 0.90261008 0.33532980 0.09089881
## [4,] 0.06069983 0.32371035 0.52514145
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
## 
##            [,1]      [,2]      [,3]       [,4]       [,5]
## [1,] 0.22365112 0.7109568 0.1468196 0.63968677 0.13287591
## [2,] 0.55691949 0.3291066 0.2471512 0.02332533 0.21617279
## [3,] 0.07754969 0.0584917 0.2899868 0.70202081 0.01396859
## [4,] 0.88457516 0.1487335 0.8663305 0.01209069 0.10001634
## 
## , , 2
## 
##              [,1]         [,2]       [,3]      [,4]         [,5]
## [1,] 0.1148152550 1.820983e-01 0.90752007 0.6538618 0.3203466534
## [2,] 0.0007463863 2.693891e-01 0.05786292 0.5376707 0.0004533506
## [3,] 0.1021011134 9.777537e-02 0.29459590 0.1255931 0.2274736485
## [4,] 0.0865025457 6.666486e-06 0.18443688 0.9709883 0.2035030722
## 
## , , 3
## 
##           [,1]       [,2]       [,3]      [,4]       [,5]
## [1,] 0.1804164 0.09225274 0.15010810 0.2792073 0.94763629
## [2,] 0.2931529 0.06987370 0.09774479 0.3913254 0.01078125
## [3,] 0.6461665 0.07174055 0.14393489 0.1663885 0.42198434
## [4,] 0.2426391 0.01813436 0.46334173 0.1962179 0.41159143
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.22365112 0.71095682 0.14681957 0.63968677 0.13287591
## [2,] 0.55691949 0.32910660 0.24715121 0.02332533 0.21617279
## [3,] 0.07754969 0.05849170 0.28998677 0.70202081 0.01396859
## [4,] 0.88457516 0.14873346 0.86633048 0.01209069 0.10001634
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 1.148153e-01 1.820983e-01 9.075201e-01 6.538618e-01 3.203467e-01
## [2,] 7.463863e-04 2.693891e-01 5.786292e-02 5.376707e-01 4.533506e-04
## [3,] 1.021011e-01 9.777537e-02 2.945959e-01 1.255931e-01 2.274736e-01
## [4,] 8.650255e-02 6.666486e-06 1.844369e-01 9.709883e-01 2.035031e-01
## 
## ,,3
##            [,1]       [,2]       [,3]       [,4]       [,5]
## [1,] 0.18041636 0.09225274 0.15010810 0.27920728 0.94763629
## [2,] 0.29315292 0.06987370 0.09774479 0.39132540 0.01078125
## [3,] 0.64616649 0.07174055 0.14393489 0.16638851 0.42198434
## [4,] 0.24263909 0.01813436 0.46334173 0.19621791 0.41159143

3.7 Summation + Permutation

Some examples of combining Summation and Permutation are shown below.

einsum::einsum('ijk->ki', arrE)
##          [,1]      [,2]      [,3]
## [1,] 2.438184 0.9798099 2.2626194
## [2,] 2.044371 1.2610293 0.9705758
## [3,] 2.349586 2.1654137 1.7601591
## [4,] 1.900355 2.8816560 2.0048331
## [5,] 1.263908 1.5153389 2.3684560
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 2.4381841 0.9798099 2.2626194
## [2,] 2.0443710 1.2610293 0.9705758
## [3,] 2.3495864 2.1654137 1.7601591
## [4,] 1.9003550 2.8816560 2.0048331
## [5,] 1.2639077 1.5153389 2.3684560

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.03359769 0.10680255 0.02205578 0.0960960998 0.019961108
## [2,] 0.03110062 0.01837864 0.01380192 0.0013025805 0.012071958
## [3,] 0.05818911 0.04388902 0.21759046 0.5267586229 0.010481276
## [4,] 0.04463584 0.00750512 0.04371521 0.0006100985 0.005046844
## 
## , , 2
## 
##              [,1]         [,2]         [,3]        [,4]         [,5]
## [1,] 4.899013e-03 7.769890e-03 0.0387226655 0.027899407 1.366876e-02
## [2,] 1.016764e-05 3.669751e-03 0.0007882373 0.007324415 6.175765e-06
## [3,] 2.695429e-02 2.581231e-02 0.0777721477 0.033156074 6.005214e-02
## [4,] 2.204503e-02 1.698943e-06 0.0470034370 0.247454778 5.186242e-02
## 
## , , 3
## 
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 5.656014e-04 2.892103e-04 4.705856e-04 0.0008753088 2.970819e-03
## [2,] 7.643596e-05 1.821869e-05 2.548573e-05 0.0001020332 2.811075e-06
## [3,] 1.365621e-02 1.516178e-03 3.041947e-03 0.0035164862 8.918297e-03
## [4,] 2.962542e-02 2.214144e-03 5.657247e-02 0.0239575475 5.025393e-02
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.0335976940 0.1068025487 0.0220557765 0.0960960998 0.0199611076
## [2,] 0.0311006247 0.0183786362 0.0138019179 0.0013025805 0.0120719582
## [3,] 0.0581891107 0.0438890220 0.2175904591 0.5267586229 0.0104812761
## [4,] 0.0446358380 0.0075051201 0.0437152076 0.0006100985 0.0050468442
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 4.899013e-03 7.769890e-03 3.872267e-02 2.789941e-02 1.366876e-02
## [2,] 1.016764e-05 3.669751e-03 7.882373e-04 7.324415e-03 6.175765e-06
## [3,] 2.695429e-02 2.581231e-02 7.777215e-02 3.315607e-02 6.005214e-02
## [4,] 2.204503e-02 1.698943e-06 4.700344e-02 2.474548e-01 5.186242e-02
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 5.656014e-04 2.892103e-04 4.705856e-04 8.753088e-04 2.970819e-03
## [2,] 7.643596e-05 1.821869e-05 2.548573e-05 1.020332e-04 2.811075e-06
## [3,] 1.365621e-02 1.516178e-03 3.041947e-03 3.516486e-03 8.918297e-03
## [4,] 2.962542e-02 2.214144e-03 5.657247e-02 2.395755e-02 5.025393e-02

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 r84266)
## Platform: aarch64-apple-darwin20 (64-bit)
## Running under: macOS Monterey 12.6.1
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib 
## LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0
## 
## locale:
## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## 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.0             DelayedRandomArray_1.8.0 HDF5Array_1.28.1        
##  [4] rhdf5_2.44.0             DelayedArray_0.26.2      S4Arrays_1.0.1          
##  [7] IRanges_2.34.0           S4Vectors_0.38.1         MatrixGenerics_1.12.0   
## [10] matrixStats_0.63.0       BiocGenerics_0.46.0      Matrix_1.5-4            
## [13] DelayedTensor_1.6.0      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] crayon_1.5.2        rsvd_1.0.5          Rcpp_1.0.10        
##  [7] rhdf5filters_1.12.1 parallel_4.3.0      jquerylib_0.1.4    
## [10] BiocParallel_1.34.1 yaml_2.3.7          fastmap_1.1.1      
## [13] lattice_0.21-8      R6_2.5.1            ScaledMatrix_1.8.1 
## [16] knitr_1.42          bookdown_0.33       bslib_0.4.2        
## [19] rlang_1.1.0         cachem_1.0.7        xfun_0.38          
## [22] sass_0.4.5          cli_3.6.1           Rhdf5lib_1.22.0    
## [25] BiocSingular_1.16.0 digest_0.6.31       grid_4.3.0         
## [28] irlba_2.3.5.1       rTensor_1.4.8       dqrng_0.3.0        
## [31] evaluate_0.20       codetools_0.2-19    beachmat_2.16.0    
## [34] rmarkdown_2.21      tools_4.3.0         htmltools_0.5.5