Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2023-10-20 14:48:21.036969
Compiled: Fri Apr 19 16:51:27 2024

1 What is einsum

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

It was originally introduced by Numpy1 https://numpy.org/doc/stable/reference/generated/numpy.einsum.html package of Python but similar tools have been implemented in other languages (e.g. R, Julia) inspired by Numpy. In this vignette, we will use CRAN einsum package first.

einsum is named after Einstein summation2 https://en.wikipedia.org/wiki/Einstein_notation introduced by Albert Einstein, which is a notational convention that implies summation over a set of indexed terms in a formula.

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

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

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

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

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

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

C <- A %*% B

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

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

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

2 Einsum of DelayedTensor

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

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

3 Typical operations of einsum

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

In more detail, einsum is capable of performing any tensor operation that can be described by a combination of the following three operations3 https://ajcr.net/Basic-guide-to-einsum/.

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

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

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

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

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

3.1 No Operation

3.1.2 diag

We can also extract the diagonal elements as follows.

einsum::einsum('ii->i', arrB)
## [1] 0.3388087 0.9851086 0.2574171
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.3388087 0.9851086 0.2574171
einsum::einsum('iii->i', arrD)
## [1] 0.6119565 0.3330158 0.2592453
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.6119565 0.3330158 0.2592453

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.66141799 0.71234415 0.02143458
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
##        [1]        [2]        [3] 
## 0.66141799 0.71234415 0.02143458
einsum::einsum('ij,ij->ij', arrC, arrC)
##            [,1]       [,2]        [,3]      [,4]
## [1,] 0.32071929 0.01115164 0.007985533 0.2508450
## [2,] 0.05191041 0.93419984 0.533029755 0.3367642
## [3,] 0.28478105 0.53098536 0.049474156 0.7214921
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.320719287 0.011151639 0.007985533 0.250844993
## [2,] 0.051910415 0.934199837 0.533029755 0.336764183
## [3,] 0.284781045 0.530985356 0.049474156 0.721492089
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
## 
##             [,1]         [,2]        [,3]       [,4]
## [1,] 0.005992979 7.666254e-03 0.007128405 0.01938683
## [2,] 0.046360276 2.308597e-05 0.429454277 0.12205949
## [3,] 0.393552682 6.963147e-03 0.395736860 0.63834030
## 
## , , 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.15117792 0.91030796 0.01770746 0.06150576
## [2,] 0.03823925 0.01261157 0.89846222 0.03379877
## [3,] 0.07003954 0.80272372 0.72435173 0.02082136
## 
## , , 3
## 
##            [,1]      [,2]       [,3]        [,4]
## [1,] 0.03020626 0.1563198 0.85774724 0.977036529
## [2,] 0.11990614 0.3498541 0.03326672 0.269242305
## [3,] 0.24579851 0.3217147 0.87213064 0.007313806
## 
## , , 4
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.4664562 0.01835518 0.00300879 0.86855456
## [2,] 0.7866947 0.08537157 0.43891462 0.08838578
## [3,] 0.4443289 0.82974798 0.08984957 0.88008647
## 
## , , 5
## 
##           [,1]       [,2]        [,3]        [,4]
## [1,] 0.7767202 0.03734126 0.001730342 0.753340757
## [2,] 0.1017214 0.60686146 0.001223533 0.013415000
## [3,] 0.9230085 0.72502637 0.462036619 0.002560642
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
##              [,1]         [,2]         [,3]         [,4]
## [1,] 5.992979e-03 7.666254e-03 7.128405e-03 1.938683e-02
## [2,] 4.636028e-02 2.308597e-05 4.294543e-01 1.220595e-01
## [3,] 3.935527e-01 6.963147e-03 3.957369e-01 6.383403e-01
## 
## ,,2
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.15117792 0.91030796 0.01770746 0.06150576
## [2,] 0.03823925 0.01261157 0.89846222 0.03379877
## [3,] 0.07003954 0.80272372 0.72435173 0.02082136
## 
## ,,3
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.030206258 0.156319780 0.857747239 0.977036529
## [2,] 0.119906139 0.349854070 0.033266717 0.269242305
## [3,] 0.245798512 0.321714739 0.872130643 0.007313806
## 
## ,,4
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.46645616 0.01835518 0.00300879 0.86855456
## [2,] 0.78669467 0.08537157 0.43891462 0.08838578
## [3,] 0.44432892 0.82974798 0.08984957 0.88008647
## 
## ,,5
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.776720169 0.037341261 0.001730342 0.753340757
## [2,] 0.101721402 0.606861457 0.001223533 0.013415000
## [3,] 0.923008496 0.725026365 0.462036619 0.002560642

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.6614180 0.6864089 0.11906812
## [2,] 0.6864089 0.7123441 0.12356697
## [3,] 0.1190681 0.1235670 0.02143458
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.66141799 0.68640894 0.11906812
## [2,] 0.68640894 0.71234415 0.12356697
## [3,] 0.11906812 0.12356697 0.02143458
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
## 
##            [,1]        [,2]        [,3]       [,4]
## [1,] 0.04384135 0.008175055 0.006917885 0.03877252
## [2,] 0.01763797 0.074824058 0.056519341 0.04492461
## [3,] 0.04131206 0.056410849 0.017219104 0.06575627
## 
## , , 2, 1, 1
## 
##            [,1]       [,2]       [,3]      [,4]
## [1,] 0.12193701 0.02273748 0.01924088 0.1078390
## [2,] 0.04905692 0.20810998 0.15719862 0.1249499
## [3,] 0.11490225 0.15689687 0.04789192 0.1828895
## 
## , , 3, 1, 1
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.3552745 0.0662477 0.05606004 0.3141985
## [2,] 0.1429317 0.6063471 0.45801233 0.3640528
## [3,] 0.3347781 0.4571331 0.13953740 0.5328650
## 
## , , 1, 2, 1
## 
##            [,1]       [,2]        [,3]       [,4]
## [1,] 0.04958544 0.00924615 0.007824265 0.04385250
## [2,] 0.01994890 0.08462750 0.063924498 0.05081063
## [3,] 0.04672477 0.06380179 0.019475150 0.07437165
## 
## , , 2, 2, 1
## 
##             [,1]         [,2]         [,3]        [,4]
## [1,] 0.002721050 0.0005073917 0.0004293644 0.002406450
## [2,] 0.001094716 0.0046440186 0.0035079208 0.002788284
## [3,] 0.002564068 0.0035011872 0.0010687183 0.004081218
## 
## , , 3, 2, 1
## 
##            [,1]        [,2]        [,3]       [,4]
## [1,] 0.04725691 0.008811952 0.007456838 0.04179319
## [2,] 0.01901210 0.080653400 0.060922612 0.04842457
## [3,] 0.04453058 0.060805667 0.018560599 0.07087916
## 
## , , 1, 3, 1
## 
##            [,1]        [,2]        [,3]       [,4]
## [1,] 0.04781440 0.008915907 0.007544807 0.04228622
## [2,] 0.01923638 0.081604869 0.061641317 0.04899583
## [3,] 0.04505591 0.061522993 0.018779559 0.07171532
## 
## , , 2, 3, 1
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.3711257 0.06920346 0.05856126 0.3282171
## [2,] 0.1493089 0.63340044 0.47844739 0.3802957
## [3,] 0.3497148 0.47752899 0.14576312 0.5566398
## 
## , , 3, 3, 1
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.3562590 0.06643128 0.05621539 0.3150692
## [2,] 0.1433278 0.60802739 0.45928153 0.3650616
## [3,] 0.3357058 0.45839991 0.13992408 0.5343417
## 
## , , 1, 4, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.07885259 0.01470357 0.01244244 0.06973586
## [2,] 0.03172347 0.13457777 0.10165510 0.08080094
## [3,] 0.07430345 0.10145997 0.03097010 0.11826854
## 
## , , 2, 4, 1
## 
##            [,1]       [,2]       [,3]      [,4]
## [1,] 0.19785559 0.03689395 0.03122035 0.1749800
## [2,] 0.07959999 0.33768026 0.25507125 0.2027443
## [3,] 0.18644095 0.25458162 0.07770965 0.2967574
## 
## , , 3, 4, 1
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.4524688 0.08437144 0.07139669 0.4001556
## [2,] 0.1820344 0.77222886 0.58331327 0.4636487
## [3,] 0.4263651 0.58219357 0.17771142 0.6786439
## 
## , , 1, 1, 2
## 
##            [,1]       [,2]       [,3]      [,4]
## [1,] 0.22019463 0.04105949 0.03474531 0.1947363
## [2,] 0.08858729 0.37580631 0.28387027 0.2256353
## [3,] 0.20749122 0.28332536 0.08648352 0.3302630
## 
## , , 2, 1, 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.11074324 0.02065019 0.01747458 0.09793939
## [2,] 0.04455351 0.18900555 0.14276784 0.11347955
## [3,] 0.10435427 0.14249379 0.04349545 0.16610032
## 
## , , 3, 1, 2
## 
##            [,1]       [,2]       [,3]      [,4]
## [1,] 0.14987672 0.02794737 0.02364959 0.1325484
## [2,] 0.06029744 0.25579470 0.19321790 0.1535800
## [3,] 0.14123007 0.19284701 0.05886550 0.2247954
## 
## , , 1, 2, 2
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.5403270 0.1007543 0.08526016 0.4778558
## [2,] 0.2173809 0.9221765 0.69657823 0.5536778
## [3,] 0.5091546 0.6952411 0.21221856 0.8104196
## 
## , , 2, 2, 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.06359853 0.01185916 0.01003544 0.05624543
## [2,] 0.02558655 0.10854364 0.08198988 0.06516996
## [3,] 0.05992942 0.08183249 0.02497892 0.09538944
## 
## , , 3, 2, 2
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.5073943 0.09461334 0.08006358 0.4487307
## [2,] 0.2041316 0.86597019 0.65412203 0.5199313
## [3,] 0.4781218 0.65286641 0.19928392 0.7610248
## 
## , , 1, 3, 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.07535996 0.01405230 0.01189132 0.06664703
## [2,] 0.03031833 0.12861689 0.09715247 0.07722200
## [3,] 0.07101231 0.09696598 0.02959834 0.11303004
## 
## , , 2, 3, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5367999 0.1000966 0.0847036 0.4747365
## [2,] 0.2159619 0.9161568 0.6920311 0.5500635
## [3,] 0.5058310 0.6907027 0.2108333 0.8051294
## 
## , , 3, 3, 2
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.4819892 0.08987607 0.07605481 0.4262628
## [2,] 0.1939108 0.82261125 0.62137028 0.4938985
## [3,] 0.4541824 0.62017752 0.18930581 0.7229205
## 
## , , 1, 4, 2
## 
##            [,1]      [,2]       [,3]      [,4]
## [1,] 0.14044958 0.0261895 0.02216205 0.1242112
## [2,] 0.05650477 0.2397054 0.18106463 0.1439199
## [3,] 0.13234680 0.1807171 0.05516290 0.2106559
## 
## , , 2, 4, 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.10411493 0.01941421 0.01642867 0.09207743
## [2,] 0.04188685 0.17769302 0.13422277 0.10668747
## [3,] 0.09810836 0.13396512 0.04089212 0.15615872
## 
## , , 3, 4, 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.08171787 0.01523786 0.01289456 0.07226986
## [2,] 0.03287621 0.13946794 0.10534895 0.08373701
## [3,] 0.07700342 0.10514673 0.03209547 0.12256608
## 
## , , 1, 1, 3
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.09842626 0.01835345 0.01553104 0.08704647
## [2,] 0.03959822 0.16798417 0.12688906 0.10085825
## [3,] 0.09274788 0.12664549 0.03865785 0.14762648
## 
## , , 2, 1, 3
## 
##            [,1]       [,2]       [,3]      [,4]
## [1,] 0.19610255 0.03656706 0.03094373 0.1734297
## [2,] 0.07889472 0.33468836 0.25281127 0.2009480
## [3,] 0.18478906 0.25232599 0.07702113 0.2941281
## 
## , , 3, 1, 3
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.2807709 0.0523551 0.04430386 0.2483089
## [2,] 0.1129580 0.4791920 0.36196398 0.2877084
## [3,] 0.2645728 0.3612692 0.11027545 0.4211196
## 
## , , 1, 2, 3
## 
##            [,1]       [,2]       [,3]      [,4]
## [1,] 0.22390795 0.04175191 0.03533124 0.1980203
## [2,] 0.09008121 0.38214384 0.28865740 0.2294404
## [3,] 0.21099031 0.28810330 0.08794197 0.3358325
## 
## , , 2, 2, 3
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.3349701 0.06246156 0.05285614 0.2962417
## [2,] 0.1347630 0.57169364 0.43183635 0.3432467
## [3,] 0.3156451 0.43100741 0.13156267 0.5024111
## 
## , , 3, 2, 3
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.3212166 0.05989697 0.05068593 0.2840784
## [2,] 0.1292298 0.54822063 0.41410570 0.3291535
## [3,] 0.3026851 0.41331080 0.12616087 0.4817828
## 
## , , 1, 3, 3
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.5244960 0.09780229 0.08276212 0.4638552
## [2,] 0.2110119 0.89515771 0.67616921 0.5374556
## [3,] 0.4942369 0.67487126 0.20600078 0.7866752
## 
## , , 2, 3, 3
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.10329220 0.0192608 0.01629885 0.09134982
## [2,] 0.04155585 0.1762889 0.13316212 0.10584441
## [3,] 0.09733309 0.1329065 0.04056899 0.15492473
## 
## , , 3, 3, 3
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.5288753 0.09861889 0.08345315 0.4677281
## [2,] 0.2127737 0.90263188 0.68181492 0.5419431
## [3,] 0.4983636 0.68050614 0.20772079 0.7932436
## 
## , , 1, 4, 3
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.5597807 0.1043818 0.08832982 0.4950603
## [2,] 0.2252074 0.9553781 0.72165750 0.5736122
## [3,] 0.5274860 0.7202722 0.21985918 0.8395976
## 
## , , 2, 4, 3
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.2938557 0.05479501 0.04636856 0.2598809
## [2,] 0.1182222 0.50152380 0.37883263 0.3011165
## [3,] 0.2769027 0.37810544 0.11541463 0.4407450
## 
## , , 3, 4, 3
## 
##            [,1]        [,2]        [,3]       [,4]
## [1,] 0.04843221 0.009031108 0.007642293 0.04283260
## [2,] 0.01948494 0.082659278 0.062437778 0.04962890
## [3,] 0.04563807 0.062317926 0.019022207 0.07264195
## 
## , , 1, 1, 4
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.3867835 0.07212316 0.06103197 0.3420646
## [2,] 0.1556083 0.66012367 0.49863314 0.3963404
## [3,] 0.3644693 0.49767599 0.15191289 0.5801245
## 
## , , 2, 1, 4
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.5023029 0.09366395 0.07926018 0.4442279
## [2,] 0.2020833 0.85728060 0.64755823 0.5147141
## [3,] 0.4733241 0.64631521 0.19728420 0.7533883
## 
## , , 3, 1, 4
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.3774982 0.07039173 0.0595668 0.3338528
## [2,] 0.1518726 0.64427634 0.4866626 0.3868256
## [3,] 0.3557196 0.48572847 0.1482660 0.5661977
## 
## , , 1, 2, 4
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.07672587 0.0143070 0.01210685 0.06785502
## [2,] 0.03086786 0.1309481 0.09891337 0.07862167
## [3,] 0.07229942 0.0987235 0.03013481 0.11507874
## 
## , , 2, 2, 4
## 
##            [,1]       [,2]       [,3]      [,4]
## [1,] 0.16546996 0.03085503 0.02611010 0.1463388
## [2,] 0.06657082 0.28240770 0.21332039 0.1695585
## [3,] 0.15592372 0.21291091 0.06498989 0.2481832
## 
## , , 3, 2, 4
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.5158645 0.09619277 0.08140012 0.4562216
## [2,] 0.2075393 0.88042627 0.66504162 0.5286108
## [3,] 0.4861034 0.66376504 0.20261066 0.7737290
## 
## , , 1, 3, 4
## 
##            [,1]       [,2]        [,3]       [,4]
## [1,] 0.03106408 0.00579249 0.004901713 0.02747253
## [2,] 0.01249750 0.05301708 0.040047154 0.03183163
## [3,] 0.02927194 0.03997028 0.012200711 0.04659204
## 
## , , 2, 3, 4
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.3751911 0.06996154 0.05920276 0.3318125
## [2,] 0.1509445 0.64033895 0.48368849 0.3844616
## [3,] 0.3535457 0.48276002 0.14735987 0.5627374
## 
## , , 3, 3, 4
## 
##            [,1]       [,2]       [,3]      [,4]
## [1,] 0.16975421 0.03165391 0.02678613 0.1501277
## [2,] 0.06829443 0.28971962 0.21884354 0.1739486
## [3,] 0.15996079 0.21842346 0.06667257 0.2546090
## 
## , , 1, 4, 4
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.5277899 0.0984165 0.08328188 0.4667682
## [2,] 0.2123371 0.9007794 0.68041563 0.5408309
## [3,] 0.4973408 0.6791095 0.20729449 0.7916156
## 
## , , 2, 4, 4
## 
##            [,1]      [,2]       [,3]      [,4]
## [1,] 0.16836575 0.0313950 0.02656704 0.1488997
## [2,] 0.06773583 0.2873499 0.21705357 0.1725258
## [3,] 0.15865244 0.2166369 0.06612724 0.2525265
## 
## , , 3, 4, 4
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.5312821 0.09906769 0.08383293 0.4698567
## [2,] 0.2137420 0.90673956 0.68491771 0.5444094
## [3,] 0.5006315 0.68360297 0.20866609 0.7968535
## 
## , , 1, 1, 5
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.4991083 0.09306827 0.07875611 0.4414027
## [2,] 0.2007981 0.85182854 0.64343994 0.5114406
## [3,] 0.4703139 0.64220482 0.19602953 0.7485970
## 
## , , 2, 1, 5
## 
##            [,1]       [,2]       [,3]      [,4]
## [1,] 0.18062119 0.03368027 0.02850087 0.1597382
## [2,] 0.07266636 0.30826631 0.23285303 0.1850841
## [3,] 0.17020084 0.23240606 0.07094068 0.2709081
## 
## , , 3, 1, 5
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.5440833 0.1014547 0.08585287 0.4811778
## [2,] 0.2188921 0.9285873 0.70142070 0.5575269
## [3,] 0.5126942 0.7000743 0.21369386 0.8160535
## 
## , , 1, 2, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.10943520 0.02040628 0.01726818 0.09678258
## [2,] 0.04402727 0.18677312 0.14108155 0.11213920
## [3,] 0.10312169 0.14081073 0.04298171 0.16413843
## 
## , , 2, 2, 5
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.4411714 0.08226481 0.06961402 0.3901643
## [2,] 0.1774892 0.75294746 0.56874881 0.4520721
## [3,] 0.4157194 0.56765707 0.17327423 0.6616991
## 
## , , 3, 2, 5
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.4822136 0.08991792 0.07609022 0.4264613
## [2,] 0.1940011 0.82299424 0.62165957 0.4941284
## [3,] 0.4543938 0.62046626 0.18939395 0.7232571
## 
## , , 1, 3, 5
## 
##             [,1]        [,2]        [,3]       [,4]
## [1,] 0.023557463 0.004392738 0.003717217 0.02083381
## [2,] 0.009477488 0.040205537 0.030369784 0.02413954
## [3,] 0.022198392 0.030311487 0.009252416 0.03533310
## 
## , , 2, 3, 5
## 
##             [,1]        [,2]        [,3]       [,4]
## [1,] 0.019809355 0.003693832 0.003125790 0.01751905
## [2,] 0.007969573 0.033808640 0.025537803 0.02029882
## [3,] 0.018666519 0.025488782 0.007780312 0.02971143
## 
## , , 3, 3, 5
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.3849468 0.07178068 0.06074215 0.3404403
## [2,] 0.1548693 0.65698899 0.49626532 0.3944583
## [3,] 0.3627386 0.49531271 0.15119151 0.5773697
## 
## , , 1, 4, 5
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.4915393 0.09165688 0.07756176 0.4347088
## [2,] 0.1977530 0.83891049 0.63368213 0.5036846
## [3,] 0.4631816 0.63246574 0.19305672 0.7372445
## 
## , , 2, 4, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.06559306 0.01223108 0.01035017 0.05800936
## [2,] 0.02638898 0.11194772 0.08456119 0.06721378
## [3,] 0.06180888 0.08439887 0.02576229 0.09838098
## 
## , , 3, 4, 5
## 
##            [,1]        [,2]        [,3]       [,4]
## [1,] 0.02865741 0.005343721 0.004521956 0.02534411
## [2,] 0.01152927 0.048909620 0.036944528 0.02936550
## [3,] 0.02700411 0.036873611 0.011255469 0.04298235
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.043841348 0.008175055 0.006917885 0.038772525
## [2,] 0.017637971 0.074824058 0.056519341 0.044924609
## [3,] 0.041312065 0.056410849 0.017219104 0.065756267
## 
## ,,2,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.12193701 0.02273748 0.01924088 0.10783897
## [2,] 0.04905692 0.20810998 0.15719862 0.12494991
## [3,] 0.11490225 0.15689687 0.04789192 0.18288951
## 
## ,,3,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.35527445 0.06624770 0.05606004 0.31419854
## [2,] 0.14293174 0.60634714 0.45801233 0.36405281
## [3,] 0.33477805 0.45713314 0.13953740 0.53286504
## 
## ...
## 
## ,,1,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.49153933 0.09165688 0.07756176 0.43470882
## [2,] 0.19775295 0.83891049 0.63368213 0.50368461
## [3,] 0.46318157 0.63246574 0.19305672 0.73724446
## 
## ,,2,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.06559306 0.01223108 0.01035017 0.05800936
## [2,] 0.02638898 0.11194772 0.08456119 0.06721378
## [3,] 0.06180888 0.08439887 0.02576229 0.09838098
## 
## ,,3,4,5
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.028657410 0.005343721 0.004521956 0.025344114
## [2,] 0.011529266 0.048909620 0.036944528 0.029365496
## [3,] 0.027004114 0.036873611 0.011255469 0.042982353

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

3.3.1 Row-wise / Column-wise Summation

einsum::einsum('ij->i', arrC)
## [1] 1.262128 2.504781 2.334171
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
##      [1]      [2]      [3] 
## 1.262128 2.504781 2.334171
einsum::einsum('ij->j', arrC)
## [1] 1.327808 1.800829 1.041879 1.930565
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 1.327808 1.800829 1.041879 1.930565

3.3.2 Mode-wise Vectorization

einsum::einsum('ijk->i', arrE)
## [1]  8.385776  7.691119 11.735101
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
##  8.385776  7.691119 11.735101
einsum::einsum('ijk->j', arrE)
## [1] 7.182438 6.854522 7.116699 6.658338
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 7.182438 6.854522 7.116699 6.658338
einsum::einsum('ijk->k', arrE)
## [1] 3.752276 5.319539 6.205187 6.759582 5.775412
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4]      [5] 
## 3.752276 5.319539 6.205187 6.759582 5.775412

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.204323 1.765751 1.240096 3.175607
## [2,] 1.963034 1.779788 2.483077 1.465221
## [3,] 3.015082 3.308983 3.393526 2.017511
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]     [,4]
## [1,] 2.204323 1.765751 1.240096 3.175607
## [2,] 1.963034 1.779788 2.483077 1.465221
## [3,] 3.015082 3.308983 3.393526 2.017511
einsum::einsum('ijk->jk', arrE)
##           [,1]      [,2]     [,3]     [,4]      [,5]
## [1,] 0.9200665 0.8490145 1.015855 2.236514 2.1609885
## [2,] 0.1758074 1.9623503 1.554057 1.338570 1.8237368
## [3,] 1.3688337 1.9320304 2.042417 1.017108 0.7563092
## [4,] 1.2875688 0.5761440 1.592858 2.167389 1.0343780
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.9200665 0.8490145 1.0158548 2.2365141 2.1609885
## [2,] 0.1758074 1.9623503 1.5540566 1.3385704 1.8237368
## [3,] 1.3688337 1.9320304 2.0424175 1.0171079 0.7563092
## [4,] 1.2875688 0.5761440 1.5928580 2.1673895 1.0343780
einsum::einsum('ijk->jk', arrE)
##           [,1]      [,2]     [,3]     [,4]      [,5]
## [1,] 0.9200665 0.8490145 1.015855 2.236514 2.1609885
## [2,] 0.1758074 1.9623503 1.554057 1.338570 1.8237368
## [3,] 1.3688337 1.9320304 2.042417 1.017108 0.7563092
## [4,] 1.2875688 0.5761440 1.592858 2.167389 1.0343780
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.9200665 0.8490145 1.0158548 2.2365141 2.1609885
## [2,] 0.1758074 1.9623503 1.5540566 1.3385704 1.8237368
## [3,] 1.3688337 1.9320304 2.0424175 1.0171079 0.7563092
## [4,] 1.2875688 0.5761440 1.5928580 2.1673895 1.0343780

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

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.3388087 0.07643278 0.4705341
## [2,] 0.4566356 0.98510864 0.9706010
## [3,] 0.5833659 0.23154603 0.2574171
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.33880871 0.07643278 0.47053410
## [2,] 0.45663561 0.98510864 0.97060103
## [3,] 0.58336587 0.23154603 0.25741709
einsum::einsum('ijk->jki', arrD)
## , , 1
## 
##           [,1]       [,2]      [,3]
## [1,] 0.6119565 0.94279386 0.6368407
## [2,] 0.9846637 0.96663758 0.6357265
## [3,] 0.8821443 0.06458412 0.5043059
## 
## , , 2
## 
##           [,1]      [,2]      [,3]
## [1,] 0.2691625 0.2219062 0.1964229
## [2,] 0.9319940 0.3330158 0.7411989
## [3,] 0.5088740 0.7530398 0.5271144
## 
## , , 3
## 
##            [,1]      [,2]       [,3]
## [1,] 0.27711173 0.4775506 0.20419603
## [2,] 0.03461886 0.2658436 0.07866031
## [3,] 0.26164131 0.2748289 0.25924531
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
##            [,1]       [,2]       [,3]
## [1,] 0.61195647 0.94279386 0.63684072
## [2,] 0.98466372 0.96663758 0.63572653
## [3,] 0.88214425 0.06458412 0.50430585
## 
## ,,2
##           [,1]      [,2]      [,3]
## [1,] 0.2691625 0.2219062 0.1964229
## [2,] 0.9319940 0.3330158 0.7411989
## [3,] 0.5088740 0.7530398 0.5271144
## 
## ,,3
##            [,1]       [,2]       [,3]
## [1,] 0.27711173 0.47755065 0.20419603
## [2,] 0.03461886 0.26584356 0.07866031
## [3,] 0.26164131 0.27482891 0.25924531

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

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.44590594 0.2594567 0.3959109 1.6974797 1.8014501
## [2,] 0.01465249 1.7256433 0.8278886 0.9334747 1.3692291
## [3,] 0.83231954 1.6405214 1.7631446 0.5317730 0.4649905
## [4,] 0.77978663 0.1161259 1.2535926 1.8370268 0.7693164
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]       [,4]       [,5]
## [1,] 0.44590594 0.25945670 0.39591091 1.69747974 1.80145007
## [2,] 0.01465249 1.72564325 0.82788859 0.93347472 1.36922908
## [3,] 0.83231954 1.64052141 1.76314460 0.53177299 0.46499049
## [4,] 0.77978663 0.11612589 1.25359264 1.83702681 0.76931640

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.5907015 0.5869866 0.8244639
## [2,] 0.5869866 1.8559042 1.4812061
## [3,] 0.8244639 1.4812061 1.5867326
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 0.5907015 0.5869866 0.8244639
## [2,] 0.5869866 1.8559042 1.4812061
## [3,] 0.8244639 1.4812061 1.5867326

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.320719287 0.05191041 0.28478105
## [2,] 0.011151639 0.93419984 0.53098536
## [3,] 0.007985533 0.53302976 0.04947416
## [4,] 0.250844993 0.33676418 0.72149209
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
##             [,1]        [,2]        [,3]
## [1,] 0.320719287 0.051910415 0.284781045
## [2,] 0.011151639 0.934199837 0.530985356
## [3,] 0.007985533 0.533029755 0.049474156
## [4,] 0.250844993 0.336764183 0.721492089
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
## 
##             [,1]       [,2]       [,3]       [,4]        [,5]
## [1,] 0.005992979 0.15117792 0.03020626 0.46645616 0.776720169
## [2,] 0.007666254 0.91030796 0.15631978 0.01835518 0.037341261
## [3,] 0.007128405 0.01770746 0.85774724 0.00300879 0.001730342
## [4,] 0.019386834 0.06150576 0.97703653 0.86855456 0.753340757
## 
## , , 2
## 
##              [,1]       [,2]       [,3]       [,4]        [,5]
## [1,] 4.636028e-02 0.03823925 0.11990614 0.78669467 0.101721402
## [2,] 2.308597e-05 0.01261157 0.34985407 0.08537157 0.606861457
## [3,] 4.294543e-01 0.89846222 0.03326672 0.43891462 0.001223533
## [4,] 1.220595e-01 0.03379877 0.26924230 0.08838578 0.013415000
## 
## , , 3
## 
##             [,1]       [,2]        [,3]       [,4]        [,5]
## [1,] 0.393552682 0.07003954 0.245798512 0.44432892 0.923008496
## [2,] 0.006963147 0.80272372 0.321714739 0.82974798 0.725026365
## [3,] 0.395736860 0.72435173 0.872130643 0.08984957 0.462036619
## [4,] 0.638340301 0.02082136 0.007313806 0.88008647 0.002560642
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.005992979 0.151177917 0.030206258 0.466456157 0.776720169
## [2,] 0.007666254 0.910307961 0.156319780 0.018355177 0.037341261
## [3,] 0.007128405 0.017707457 0.857747239 0.003008790 0.001730342
## [4,] 0.019386834 0.061505760 0.977036529 0.868554559 0.753340757
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 4.636028e-02 3.823925e-02 1.199061e-01 7.866947e-01 1.017214e-01
## [2,] 2.308597e-05 1.261157e-02 3.498541e-01 8.537157e-02 6.068615e-01
## [3,] 4.294543e-01 8.984622e-01 3.326672e-02 4.389146e-01 1.223533e-03
## [4,] 1.220595e-01 3.379877e-02 2.692423e-01 8.838578e-02 1.341500e-02
## 
## ,,3
##             [,1]        [,2]        [,3]        [,4]        [,5]
## [1,] 0.393552682 0.070039540 0.245798512 0.444328917 0.923008496
## [2,] 0.006963147 0.802723724 0.321714739 0.829747975 0.725026365
## [3,] 0.395736860 0.724351729 0.872130643 0.089849573 0.462036619
## [4,] 0.638340301 0.020821356 0.007313806 0.880086468 0.002560642

3.7 Summation + Permutation

Some examples of combining Summation and Permutation are shown below.

einsum::einsum('ijk->ki', arrE)
##          [,1]     [,2]     [,3]
## [1,] 0.388638 1.224817 2.138821
## [2,] 1.723990 1.439567 2.155983
## [3,] 2.483770 1.639037 2.082380
## [4,] 1.805272 2.138946 2.815363
## [5,] 1.984106 1.248753 2.542554
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]
## [1,] 0.388638 1.224817 2.138821
## [2,] 1.723990 1.439567 2.155983
## [3,] 2.483770 1.639037 2.082380
## [4,] 1.805272 2.138946 2.815363
## [5,] 1.984106 1.248753 2.542554

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,] 1.563169e-03 0.0394322389 0.007878799 1.216673e-01 2.025945e-01
## [2,] 6.952803e-05 0.0082559115 0.001417721 1.664697e-04 3.386614e-04
## [3,] 4.629502e-05 0.0001150001 0.005570590 1.954041e-05 1.123761e-05
## [4,] 3.955035e-03 0.0125475584 0.199321543 1.771905e-01 1.536862e-01
## 
## , , 2
## 
##              [,1]        [,2]        [,3]       [,4]         [,5]
## [1,] 2.031166e-03 0.001675362 0.005253405 0.03446717 0.0044566832
## [2,] 1.820257e-05 0.009943831 0.275849149 0.06731285 0.4784915505
## [3,] 1.932028e-01 0.404199904 0.014966020 0.19745877 0.0005504425
## [4,] 3.469304e-02 0.009606645 0.076526901 0.02512194 0.0038129535
## 
## , , 3
## 
##              [,1]        [,2]         [,3]         [,4]         [,5]
## [1,] 0.0164085965 0.002920195 0.0102482051 0.0185256364 0.0384834728
## [2,] 0.0005413094 0.062403094 0.0250098438 0.0645039371 0.0563629638
## [3,] 0.0028664368 0.005246690 0.0063170952 0.0006508065 0.0033466652
## [4,] 0.0674281612 0.002199369 0.0007725605 0.0929639129 0.0002704817
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,] 1.563169e-03 3.943224e-02 7.878799e-03 1.216673e-01 2.025945e-01
## [2,] 6.952803e-05 8.255912e-03 1.417721e-03 1.664697e-04 3.386614e-04
## [3,] 4.629502e-05 1.150001e-04 5.570590e-03 1.954041e-05 1.123761e-05
## [4,] 3.955035e-03 1.254756e-02 1.993215e-01 1.771905e-01 1.536862e-01
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 2.031166e-03 1.675362e-03 5.253405e-03 3.446717e-02 4.456683e-03
## [2,] 1.820257e-05 9.943831e-03 2.758491e-01 6.731285e-02 4.784916e-01
## [3,] 1.932028e-01 4.041999e-01 1.496602e-02 1.974588e-01 5.504425e-04
## [4,] 3.469304e-02 9.606645e-03 7.652690e-02 2.512194e-02 3.812954e-03
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.0164085965 0.0029201949 0.0102482051 0.0185256364 0.0384834728
## [2,] 0.0005413094 0.0624030937 0.0250098438 0.0645039371 0.0563629638
## [3,] 0.0028664368 0.0052466896 0.0063170952 0.0006508065 0.0033466652
## [4,] 0.0674281612 0.0021993688 0.0007725605 0.0929639129 0.0002704817

4 Create your original function by einsum

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

For example, kronecker can be easily implmented by eimsum and other DelayedTensor functions4 https://stackoverflow.com/ questions/56067643/speeding-up-kronecker-products-numpy (the kronecker function inside DelayedTensor has a more efficient implementation though).

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

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

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

Session information

## R version 4.4.0 beta (2024-04-15 r86425)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 22.04.4 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.19-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.11.0
##  [3] HDF5Array_1.31.6          rhdf5_2.47.7             
##  [5] DelayedArray_0.29.9       SparseArray_1.3.5        
##  [7] S4Arrays_1.3.7            abind_1.4-5              
##  [9] IRanges_2.37.1            S4Vectors_0.41.6         
## [11] MatrixGenerics_1.15.1     matrixStats_1.3.0        
## [13] BiocGenerics_0.49.1       Matrix_1.7-0             
## [15] DelayedTensor_1.9.0       BiocStyle_2.31.0         
## 
## loaded via a namespace (and not attached):
##  [1] jsonlite_1.8.8      compiler_4.4.0      BiocManager_1.30.22
##  [4] crayon_1.5.2        rsvd_1.0.5          Rcpp_1.0.12        
##  [7] rhdf5filters_1.15.5 parallel_4.4.0      jquerylib_0.1.4    
## [10] BiocParallel_1.37.1 yaml_2.3.8          fastmap_1.1.1      
## [13] lattice_0.22-6      R6_2.5.1            XVector_0.43.1     
## [16] ScaledMatrix_1.11.1 knitr_1.46          bookdown_0.39      
## [19] bslib_0.7.0         rlang_1.1.3         cachem_1.0.8       
## [22] xfun_0.43           sass_0.4.9          cli_3.6.2          
## [25] Rhdf5lib_1.25.3     BiocSingular_1.19.0 zlibbioc_1.49.3    
## [28] digest_0.6.35       grid_4.4.0          irlba_2.3.5.1      
## [31] rTensor_1.4.8       dqrng_0.3.2         lifecycle_1.0.4    
## [34] evaluate_0.23       codetools_0.2-20    beachmat_2.19.4    
## [37] rmarkdown_2.26      tools_4.4.0         htmltools_0.5.8.1