Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2024-05-29 20:36:38.968287
Compiled: Mon Sep 23 17:27:21 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.62250098 0.17311354 0.06617263
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
##        [1]        [2]        [3] 
## 0.62250098 0.17311354 0.06617263
einsum::einsum('iii->i', arrD)
## [1] 0.03777065 0.87545184 0.87662636
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
##        [1]        [2]        [3] 
## 0.03777065 0.87545184 0.87662636

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.997208950 0.126997103 0.002076515
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
##         [1]         [2]         [3] 
## 0.997208950 0.126997103 0.002076515
einsum::einsum('ij,ij->ij', arrC, arrC)
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.3187399 0.9950355 0.28194671 0.9552801
## [2,] 0.8274130 0.4472352 0.18490956 0.1159629
## [3,] 0.4697988 0.1632895 0.03535626 0.1289397
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.31873994 0.99503545 0.28194671 0.95528007
## [2,] 0.82741298 0.44723515 0.18490956 0.11596287
## [3,] 0.46979877 0.16328952 0.03535626 0.12893968
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.23266876 0.8542840 0.8122099 0.03374295
## [2,] 0.68685853 0.9056516 0.5945596 0.88403975
## [3,] 0.05920108 0.4985944 0.2854523 0.23595538
## 
## , , 2
## 
##           [,1]       [,2]       [,3]        [,4]
## [1,] 0.1596346 0.71592946 0.08409356 0.877165797
## [2,] 0.1011838 0.40109009 0.32251125 0.308807220
## [3,] 0.3205937 0.04894131 0.11375365 0.002686796
## 
## , , 3
## 
##            [,1]       [,2]        [,3]       [,4]
## [1,] 0.18555133 0.02732866 0.170915715 0.05156817
## [2,] 0.07158531 0.21166701 0.001423795 0.34235149
## [3,] 0.38253243 0.03652727 0.780162380 0.65347706
## 
## , , 4
## 
##             [,1]      [,2]      [,3]         [,4]
## [1,] 0.002408805 0.3033502 0.7816635 0.2097784515
## [2,] 0.798262488 0.9000497 0.1058224 0.1788757382
## [3,] 0.630152225 0.9785251 0.4388229 0.0004015217
## 
## , , 5
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.11165000 0.03016160 0.7054506 0.4518157
## [2,] 0.01638863 0.64582978 0.1130124 0.5197640
## [3,] 0.40583719 0.01861192 0.4439323 0.1418144
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.23266876 0.85428399 0.81220994 0.03374295
## [2,] 0.68685853 0.90565162 0.59455957 0.88403975
## [3,] 0.05920108 0.49859438 0.28545227 0.23595538
## 
## ,,2
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.159634643 0.715929456 0.084093556 0.877165797
## [2,] 0.101183799 0.401090092 0.322511253 0.308807220
## [3,] 0.320593681 0.048941311 0.113753653 0.002686796
## 
## ,,3
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.185551331 0.027328657 0.170915715 0.051568166
## [2,] 0.071585313 0.211667013 0.001423795 0.342351491
## [3,] 0.382532433 0.036527272 0.780162380 0.653477058
## 
## ,,4
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.0024088051 0.3033501754 0.7816635099 0.2097784515
## [2,] 0.7982624884 0.9000497084 0.1058224040 0.1788757382
## [3,] 0.6301522251 0.9785250957 0.4388228799 0.0004015217
## 
## ,,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.11165000 0.03016160 0.70545056 0.45181572
## [2,] 0.01638863 0.64582978 0.11301242 0.51976397
## [3,] 0.40583719 0.01861192 0.44393227 0.14181437

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.99720895 0.35586886 0.045505162
## [2,] 0.35586886 0.12699710 0.016239195
## [3,] 0.04550516 0.01623919 0.002076515
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
##             [,1]        [,2]        [,3]
## [1,] 0.997208950 0.355868863 0.045505162
## [2,] 0.355868863 0.126997103 0.016239195
## [3,] 0.045505162 0.016239195 0.002076515
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.2723249 0.4811587 0.25612534 0.4714486
## [2,] 0.4387632 0.3225797 0.20741909 0.1642587
## [3,] 0.3306168 0.1949163 0.09069893 0.1732058
## 
## , , 2, 1, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4678988 0.8267095 0.4400653 0.8100261
## [2,] 0.7538671 0.5542448 0.3563800 0.2822235
## [3,] 0.5680540 0.3348982 0.1558356 0.2975959
## 
## , , 3, 1, 1
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.1373672 0.24270801 0.12919579 0.23781004
## [2,] 0.2213227 0.16271695 0.10462718 0.08285606
## [3,] 0.1667711 0.09832048 0.04575072 0.08736915
## 
## , , 1, 2, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5218184 0.9219777 0.4907775 0.9033717
## [2,] 0.8407411 0.6181147 0.3974485 0.3147463
## [3,] 0.6335152 0.3734911 0.1737938 0.3318902
## 
## , , 2, 2, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5372777 0.9492921 0.5053172 0.9301349
## [2,] 0.8656488 0.6364269 0.4092232 0.3240709
## [3,] 0.6522837 0.3845561 0.1789426 0.3417227
## 
## , , 3, 2, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3986502 0.7043572 0.3749361 0.6901429
## [2,] 0.6422955 0.4722170 0.3036361 0.2404546
## [3,] 0.4839825 0.2853336 0.1327721 0.2535520
## 
## , , 1, 3, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5088062 0.8989870 0.4785394 0.8808450
## [2,] 0.8197762 0.6027013 0.3875376 0.3068977
## [3,] 0.6177178 0.3641777 0.1694600 0.3236141
## 
## , , 2, 3, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4353273 0.7691605 0.4094315 0.7536384
## [2,] 0.7013888 0.5156626 0.3315716 0.2625773
## [3,] 0.5285105 0.3115852 0.1449876 0.2768796
## 
## , , 3, 3, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3016373 0.5329495 0.2836941 0.5221943
## [2,] 0.4859907 0.3573014 0.2297452 0.1819392
## [3,] 0.3662037 0.2158967 0.1004616 0.1918492
## 
## , , 1, 4, 1
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.1037074 0.1832360 0.09753827 0.17953820
## [2,] 0.1670909 0.1228456 0.07898983 0.06255341
## [3,] 0.1259063 0.0742285 0.03454018 0.06596063
## 
## , , 2, 4, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5308284 0.9378971 0.4992515 0.9189698
## [2,] 0.8552578 0.6287874 0.4043110 0.3201809
## [3,] 0.6444539 0.3799400 0.1767946 0.3376208
## 
## , , 3, 4, 1
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.2742415 0.4845451 0.25792798 0.4747668
## [2,] 0.4418513 0.3248500 0.20887893 0.1654148
## [3,] 0.3329438 0.1962882 0.09133728 0.1744248
## 
## , , 1, 1, 2
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.2255702 0.3985500 0.21215198 0.3905071
## [2,] 0.3634333 0.2671970 0.17180795 0.1360577
## [3,] 0.2738543 0.1614517 0.07512711 0.1434686
## 
## , , 2, 1, 2
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.1795865 0.3173034 0.16890364 0.3109001
## [2,] 0.2893454 0.2127274 0.13678396 0.1083216
## [3,] 0.2180276 0.1285389 0.05981204 0.1142217
## 
## , , 3, 1, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3196655 0.5648027 0.3006499 0.5534047
## [2,] 0.5150373 0.3786565 0.2434766 0.1928133
## [3,] 0.3880909 0.2288003 0.1064659 0.2033156
## 
## , , 1, 2, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4776979 0.8440232 0.4492816 0.8269904
## [2,] 0.7696553 0.5658523 0.3638437 0.2881341
## [3,] 0.5799507 0.3419119 0.1590993 0.3038284
## 
## , , 2, 2, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3575520 0.6317427 0.3362827 0.6189938
## [2,] 0.5760791 0.4235346 0.2723332 0.2156654
## [3,] 0.4340871 0.2559176 0.1190842 0.2274125
## 
## , , 3, 2, 2
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.1248982 0.22067700 0.11746847 0.21622363
## [2,] 0.2012329 0.14794687 0.09513000 0.07533508
## [3,] 0.1516330 0.08939577 0.04159785 0.07943851
## 
## , , 1, 3, 2
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.1637192 0.2892682 0.15398020 0.28343059
## [2,] 0.2637804 0.1939319 0.12469845 0.09875085
## [3,] 0.1987638 0.1171819 0.05452736 0.10412971
## 
## , , 2, 3, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3206201 0.5664893 0.3015477 0.5550573
## [2,] 0.5165753 0.3797873 0.2442036 0.1933891
## [3,] 0.3892498 0.2294836 0.1067838 0.2039228
## 
## , , 3, 3, 2
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.1904149 0.3364356 0.17908788 0.3296462
## [2,] 0.3067919 0.2255541 0.14503151 0.1148529
## [3,] 0.2311738 0.1362893 0.06341848 0.1211089
## 
## , , 1, 4, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5287606 0.9342436 0.4973068 0.9153901
## [2,] 0.8519263 0.6263381 0.4027361 0.3189336
## [3,] 0.6419435 0.3784600 0.1761059 0.3363056
## 
## , , 2, 4, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3137343 0.5543231 0.2950715 0.5431366
## [2,] 0.5054811 0.3716308 0.2389590 0.1892358
## [3,] 0.3808901 0.2245551 0.1044905 0.1995432
## 
## , , 3, 4, 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02926413 0.05170548 0.02752332 0.05066204
## [2,] 0.04714965 0.03466453 0.02228933 0.01765130
## [3,] 0.03552821 0.02094578 0.00974654 0.01861275
## 
## , , 1, 1, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.2431926 0.4296861 0.2287260 0.4210148
## [2,] 0.3918260 0.2880713 0.1852302 0.1466870
## [3,] 0.2952487 0.1740649 0.0809963 0.1546769
## 
## , , 2, 1, 3
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.1510533 0.2668893 0.14206774 0.26150339
## [2,] 0.2433734 0.1789287 0.11505133 0.09111113
## [3,] 0.1833867 0.1081163 0.05030893 0.09607387
## 
## , , 3, 1, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3491824 0.6169549 0.3284110 0.6045044
## [2,] 0.5625943 0.4136205 0.2659585 0.2106171
## [3,] 0.4239260 0.2499271 0.1162967 0.2220892
## 
## , , 1, 2, 3
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.09333132 0.16490295 0.08777941 0.16157513
## [2,] 0.15037315 0.11055467 0.07108678 0.05629484
## [3,] 0.11330918 0.06680182 0.03108438 0.05936117
## 
## , , 2, 2, 3
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.2597436 0.4589294 0.24429249 0.4496680
## [2,] 0.4184926 0.3076767 0.19783644 0.1566701
## [3,] 0.3153425 0.1859113 0.08650869 0.1652037
## 
## , , 3, 2, 3
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.1079013 0.19064609 0.10148273 0.18679876
## [2,] 0.1738480 0.12781346 0.08218420 0.06508308
## [3,] 0.1309980 0.07723031 0.03593699 0.06862809
## 
## , , 1, 3, 3
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.2334045 0.4123920 0.21952021 0.4040698
## [2,] 0.3760557 0.2764770 0.17777500 0.1407831
## [3,] 0.2833655 0.1670591 0.07773635 0.1484514
## 
## , , 2, 3, 3
## 
##            [,1]       [,2]        [,3]       [,4]
## [1,] 0.02130306 0.03763943 0.020035828 0.03687985
## [2,] 0.03432298 0.02523433 0.016225701 0.01284941
## [3,] 0.02586305 0.01524765 0.007095074 0.01354931
## 
## , , 3, 3, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4986671 0.8810728 0.4690034 0.8632923
## [2,] 0.8034404 0.5906912 0.3798151 0.3007821
## [3,] 0.6054084 0.3569206 0.1660832 0.3171654
## 
## , , 1, 4, 3
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.1282062 0.22652186 0.12057974 0.22195054
## [2,] 0.2065628 0.15186539 0.09764961 0.07733041
## [3,] 0.1556492 0.09176351 0.04269962 0.08154252
## 
## , , 2, 4, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3303348 0.5836539 0.3106845 0.5718755
## [2,] 0.5322275 0.3912948 0.2516030 0.1992487
## [3,] 0.4010440 0.2364369 0.1100194 0.2101016
## 
## , , 3, 4, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4563872 0.8063702 0.4292385 0.7900972
## [2,] 0.7353199 0.5406088 0.3476121 0.2752800
## [3,] 0.5540783 0.3266588 0.1520017 0.2902742
## 
## , , 1, 1, 4
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02770889 0.04895760 0.02606060 0.04796961
## [2,] 0.04464389 0.03282228 0.02110476 0.01671323
## [3,] 0.03364006 0.01983262 0.00922856 0.01762358
## 
## , , 2, 1, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5044186 0.8912348 0.4744128 0.8732492
## [2,] 0.8127070 0.5975040 0.3841957 0.3042512
## [3,] 0.6123910 0.3610373 0.1679987 0.3208235
## 
## , , 3, 1, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4481681 0.7918483 0.4215084 0.7758685
## [2,] 0.7220777 0.5308731 0.3413520 0.2703225
## [3,] 0.5440999 0.3207760 0.1492643 0.2850467
## 
## , , 1, 2, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3109499 0.5494035 0.2924527 0.5383162
## [2,] 0.5009949 0.3683325 0.2368382 0.1875563
## [3,] 0.3775097 0.2225621 0.1035632 0.1977723
## 
## , , 2, 2, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5356135 0.9463516 0.5037520 0.9272538
## [2,] 0.8629674 0.6344556 0.4079556 0.3230671
## [3,] 0.6502632 0.3833649 0.1783883 0.3406642
## 
## , , 3, 2, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5584756 0.9867457 0.5252542 0.9668327
## [2,] 0.8998024 0.6615367 0.4253688 0.3368569
## [3,] 0.6780191 0.3997285 0.1860026 0.3552052
## 
## , , 1, 3, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4991467 0.8819200 0.4694544 0.8641224
## [2,] 0.8042130 0.5912592 0.3801803 0.3010713
## [3,] 0.6059906 0.3572638 0.1662429 0.3174704
## 
## , , 2, 3, 4
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.1836568 0.3244951 0.17273181 0.3179466
## [2,] 0.2959034 0.2175488 0.13988415 0.1107767
## [3,] 0.2229691 0.1314522 0.06116767 0.1168106
## 
## , , 3, 3, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3739925 0.6607907 0.3517452 0.6474556
## [2,] 0.6025676 0.4430091 0.2848553 0.2255818
## [3,] 0.4540467 0.2676848 0.1245598 0.2378690
## 
## , , 1, 4, 4
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.2585822 0.4568774 0.24320022 0.4476574
## [2,] 0.4166214 0.3063010 0.19695188 0.1559696
## [3,] 0.3139326 0.1850800 0.08612189 0.1644651
## 
## , , 2, 4, 4
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.2387778 0.4218859 0.22457388 0.4133720
## [2,] 0.3847130 0.2828419 0.18186763 0.1440241
## [3,] 0.2898889 0.1709050 0.07952595 0.1518690
## 
## , , 3, 4, 4
## 
##            [,1]        [,2]        [,3]        [,4]
## [1,] 0.01131287 0.019988204 0.010639911 0.019584832
## [2,] 0.01822702 0.013400545 0.008616565 0.006823606
## [3,] 0.01373442 0.008097177 0.003767798 0.007195282
## 
## , , 1, 1, 5
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.1886460 0.3333102 0.17742421 0.3265839
## [2,] 0.3039419 0.2234587 0.14368421 0.1137860
## [3,] 0.2290263 0.1350232 0.06282934 0.1199838
## 
## , , 2, 1, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.07227525 0.12769993 0.06797589 0.12512288
## [2,] 0.11644814 0.08561293 0.05504921 0.04359441
## [3,] 0.08774600 0.05173096 0.02407157 0.04596896
## 
## , , 3, 1, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3596617 0.6354702 0.3382668 0.6226461
## [2,] 0.5794782 0.4260336 0.2739401 0.2169379
## [3,] 0.4366484 0.2574276 0.1197868 0.2287543
## 
## , , 1, 2, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.09804952 0.17323932 0.09221694 0.16974327
## [2,] 0.15797500 0.11614357 0.07468044 0.05914073
## [3,] 0.11903732 0.07017887 0.03265580 0.06236207
## 
## , , 2, 2, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4537089 0.8016380 0.4267196 0.7854606
## [2,] 0.7310048 0.5374363 0.3455721 0.2736645
## [3,] 0.5508267 0.3247418 0.1511096 0.2885708
## 
## , , 3, 2, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.07702183 0.13608644 0.07244011 0.13334015
## [2,] 0.12409571 0.09123544 0.05866449 0.04645742
## [3,] 0.09350859 0.05512832 0.02565244 0.04898791
## 
## , , 1, 3, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4741891 0.8378236 0.4459815 0.8209159
## [2,] 0.7640019 0.5616959 0.3611711 0.2860176
## [3,] 0.5756907 0.3394005 0.1579307 0.3015967
## 
## , , 2, 3, 5
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.1897935 0.3353377 0.17850344 0.3285704
## [2,] 0.3057907 0.2248180 0.14455821 0.1144781
## [3,] 0.2304194 0.1358446 0.06321152 0.1207137
## 
## , , 3, 3, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3761635 0.6646265 0.3537870 0.6512140
## [2,] 0.6060654 0.4455807 0.2865089 0.2268913
## [3,] 0.4566824 0.2692387 0.1252828 0.2392498
## 
## , , 1, 4, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3794888 0.6705018 0.3569145 0.6569707
## [2,] 0.6114231 0.4495196 0.2890416 0.2288970
## [3,] 0.4607195 0.2716188 0.1263903 0.2413648
## 
## , , 2, 4, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4070252 0.7191548 0.3828129 0.7046419
## [2,] 0.6557892 0.4821377 0.3100150 0.2455063
## [3,] 0.4941503 0.2913280 0.1355615 0.2588787
## 
## , , 3, 4, 5
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.2126074 0.3756465 0.19996023 0.3680658
## [2,] 0.3425479 0.2518420 0.16193466 0.1282388
## [3,] 0.2581167 0.1521736 0.07080978 0.1352239
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.27232486 0.48115867 0.25612534 0.47144865
## [2,] 0.43876321 0.32257968 0.20741909 0.16425875
## [3,] 0.33061684 0.19491631 0.09069893 0.17320576
## 
## ,,2,1,1
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4678988 0.8267095 0.4400653 0.8100261
## [2,] 0.7538671 0.5542448 0.3563800 0.2822235
## [3,] 0.5680540 0.3348982 0.1558356 0.2975959
## 
## ,,3,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.13736721 0.24270801 0.12919579 0.23781004
## [2,] 0.22132272 0.16271695 0.10462718 0.08285606
## [3,] 0.16677109 0.09832048 0.04575072 0.08736915
## 
## ...
## 
## ,,1,4,5
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3794888 0.6705018 0.3569145 0.6569707
## [2,] 0.6114231 0.4495196 0.2890416 0.2288970
## [3,] 0.4607195 0.2716188 0.1263903 0.2413648
## 
## ,,2,4,5
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4070252 0.7191548 0.3828129 0.7046419
## [2,] 0.6557892 0.4821377 0.3100150 0.2455063
## [3,] 0.4941503 0.2913280 0.1355615 0.2588787
## 
## ,,3,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.21260739 0.37564654 0.19996023 0.36806581
## [2,] 0.34254788 0.25184196 0.16193466 0.12823884
## [3,] 0.25811667 0.15217358 0.07080978 0.13522389

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

3.3.1 Row-wise / Column-wise Summation

einsum::einsum('ij->i', arrC)
## [1] 3.070456 2.348923 1.636624
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
##      [1]      [2]      [3] 
## 3.070456 2.348923 1.636624
einsum::einsum('ij->j', arrC)
## [1] 2.159612 2.070362 1.149030 1.676999
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 2.159612 2.070362 1.149030 1.676999

3.3.2 Mode-wise Vectorization

einsum::einsum('ijk->i', arrE)
## [1] 10.162238 11.496400  9.929105
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 10.162238 11.496400  9.929105
einsum::einsum('ijk->j', arrE)
## [1] 6.990658 8.701640 8.450390 7.445055
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 6.990658 8.701640 8.450390 7.445055
einsum::einsum('ijk->k', arrE)
## [1] 7.952744 5.723790 5.088305 6.997891 5.825012
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4]      [5] 
## 7.952744 5.723790 5.088305 6.997891 5.825012

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,] 1.695878 2.660158 3.328664 2.477538
## [2,] 2.435891 3.797392 2.038188 3.224930
## [3,] 2.858890 2.244090 3.083539 1.742586
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]     [,4]
## [1,] 1.695878 2.660158 3.328664 2.477538
## [2,] 2.435891 3.797392 2.038188 3.224930
## [3,] 2.858890 2.244090 3.083539 1.742586
einsum::einsum('ijk->jk', arrE)
##          [,1]     [,2]      [,3]      [,4]     [,5]
## [1,] 1.554440 1.283847 1.3168031 1.7363562 1.099212
## [2,] 2.582044 1.700670 0.8165077 2.4886861 1.113732
## [3,] 2.206581 1.195163 1.3344208 1.8718580 1.842367
## [4,] 1.609679 1.544110 1.6205736 0.9009908 1.769701
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.5544395 1.2838470 1.3168031 1.7363562 1.0992123
## [2,] 2.5820443 1.7006697 0.8165077 2.4886861 1.1137318
## [3,] 2.2065811 1.1951635 1.3344208 1.8718580 1.8423667
## [4,] 1.6096788 1.5441099 1.6205736 0.9009908 1.7697015
einsum::einsum('ijk->jk', arrE)
##          [,1]     [,2]      [,3]      [,4]     [,5]
## [1,] 1.554440 1.283847 1.3168031 1.7363562 1.099212
## [2,] 2.582044 1.700670 0.8165077 2.4886861 1.113732
## [3,] 2.206581 1.195163 1.3344208 1.8718580 1.842367
## [4,] 1.609679 1.544110 1.6205736 0.9009908 1.769701
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.5544395 1.2838470 1.3168031 1.7363562 1.0992123
## [2,] 2.5820443 1.7006697 0.8165077 2.4886861 1.1137318
## [3,] 2.2065811 1.1951635 1.3344208 1.8718580 1.8423667
## [4,] 1.6096788 1.5441099 1.6205736 0.9009908 1.7697015

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

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.622500978 0.5882665 0.93756379
## [2,] 0.377797139 0.1731135 0.85141194
## [3,] 0.004796433 0.4905398 0.06617263
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
##             [,1]        [,2]        [,3]
## [1,] 0.622500978 0.588266494 0.937563786
## [2,] 0.377797139 0.173113545 0.851411941
## [3,] 0.004796433 0.490539761 0.066172630
einsum::einsum('ijk->jki', arrD)
## , , 1
## 
##            [,1]       [,2]      [,3]
## [1,] 0.03777065 0.76783791 0.4417801
## [2,] 0.48919245 0.09351242 0.4477087
## [3,] 0.11285556 0.58912246 0.4933397
## 
## , , 2
## 
##           [,1]      [,2]      [,3]
## [1,] 0.2849856 0.3698813 0.4209580
## [2,] 0.7745016 0.8754518 0.6396971
## [3,] 0.5148250 0.4791874 0.7388468
## 
## , , 3
## 
##           [,1]      [,2]      [,3]
## [1,] 0.5375775 0.5425131 0.9553911
## [2,] 0.5976889 0.3893509 0.3023950
## [3,] 0.6412208 0.9211341 0.8766264
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
##            [,1]       [,2]       [,3]
## [1,] 0.03777065 0.76783791 0.44178010
## [2,] 0.48919245 0.09351242 0.44770871
## [3,] 0.11285556 0.58912246 0.49333971
## 
## ,,2
##           [,1]      [,2]      [,3]
## [1,] 0.2849856 0.3698813 0.4209580
## [2,] 0.7745016 0.8754518 0.6396971
## [3,] 0.5148250 0.4791874 0.7388468
## 
## ,,3
##           [,1]      [,2]      [,3]
## [1,] 0.5375775 0.5425131 0.9553911
## [2,] 0.5976889 0.3893509 0.3023950
## [3,] 0.6412208 0.9211341 0.8766264

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

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.9787284 0.5814121 0.6396691 1.4308235 0.5338758
## [2,] 2.2585300 1.1659609 0.2755229 2.1819250 0.6946033
## [3,] 1.6922218 0.5203585 0.9525019 1.3263088 1.2623952
## [4,] 1.1537381 1.1886598 1.0473967 0.3890557 1.1133941
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.9787284 0.5814121 0.6396691 1.4308235 0.5338758
## [2,] 2.2585300 1.1659609 0.2755229 2.1819250 0.6946033
## [3,] 1.6922218 0.5203585 0.9525019 1.3263088 1.2623952
## [4,] 1.1537381 1.1886598 1.0473967 0.3890557 1.1133941

3.5.3 Matrix Multiplication

Matrix Multiplication is considered a contracted product.

einsum::einsum('ij,jk->ik', arrC, t(arrC))
##          [,1]     [,2]      [,3]
## [1,] 2.551002 1.741802 1.2408575
## [2,] 1.741802 1.575521 1.0968460
## [3,] 1.240858 1.096846 0.7973842
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 2.5510022 1.7418023 1.2408575
## [2,] 1.7418023 1.5755206 1.0968460
## [3,] 1.2408575 1.0968460 0.7973842

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.3187399 0.8274130 0.46979877
## [2,] 0.9950355 0.4472352 0.16328952
## [3,] 0.2819467 0.1849096 0.03535626
## [4,] 0.9552801 0.1159629 0.12893968
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.31873994 0.82741298 0.46979877
## [2,] 0.99503545 0.44723515 0.16328952
## [3,] 0.28194671 0.18490956 0.03535626
## [4,] 0.95528007 0.11596287 0.12893968
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
## 
##            [,1]       [,2]       [,3]        [,4]      [,5]
## [1,] 0.23266876 0.15963464 0.18555133 0.002408805 0.1116500
## [2,] 0.85428399 0.71592946 0.02732866 0.303350175 0.0301616
## [3,] 0.81220994 0.08409356 0.17091571 0.781663510 0.7054506
## [4,] 0.03374295 0.87716580 0.05156817 0.209778451 0.4518157
## 
## , , 2
## 
##           [,1]      [,2]        [,3]      [,4]       [,5]
## [1,] 0.6868585 0.1011838 0.071585313 0.7982625 0.01638863
## [2,] 0.9056516 0.4010901 0.211667013 0.9000497 0.64582978
## [3,] 0.5945596 0.3225113 0.001423795 0.1058224 0.11301242
## [4,] 0.8840397 0.3088072 0.342351491 0.1788757 0.51976397
## 
## , , 3
## 
##            [,1]        [,2]       [,3]         [,4]       [,5]
## [1,] 0.05920108 0.320593681 0.38253243 0.6301522251 0.40583719
## [2,] 0.49859438 0.048941311 0.03652727 0.9785250957 0.01861192
## [3,] 0.28545227 0.113753653 0.78016238 0.4388228799 0.44393227
## [4,] 0.23595538 0.002686796 0.65347706 0.0004015217 0.14181437
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.232668758 0.159634643 0.185551331 0.002408805 0.111649995
## [2,] 0.854283991 0.715929456 0.027328657 0.303350175 0.030161602
## [3,] 0.812209939 0.084093556 0.170915715 0.781663510 0.705450562
## [4,] 0.033742948 0.877165797 0.051568166 0.209778451 0.451815721
## 
## ,,2
##             [,1]        [,2]        [,3]        [,4]        [,5]
## [1,] 0.686858532 0.101183799 0.071585313 0.798262488 0.016388634
## [2,] 0.905651624 0.401090092 0.211667013 0.900049708 0.645829783
## [3,] 0.594559573 0.322511253 0.001423795 0.105822404 0.113012418
## [4,] 0.884039745 0.308807220 0.342351491 0.178875738 0.519763966
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.0592010833 0.3205936811 0.3825324333 0.6301522251 0.4058371861
## [2,] 0.4985943797 0.0489413115 0.0365272725 0.9785250957 0.0186119199
## [3,] 0.2854522695 0.1137536529 0.7801623797 0.4388228799 0.4439322679
## [4,] 0.2359553846 0.0026867959 0.6534770579 0.0004015217 0.1418143680

3.7 Summation + Permutation

Some examples of combining Summation and Permutation are shown below.

einsum::einsum('ijk->ki', arrE)
##          [,1]     [,2]     [,3]
## [1,] 2.491552 3.491737 1.969455
## [2,] 2.472229 2.075016 1.176545
## [3,] 1.236576 1.350468 2.501260
## [4,] 1.941985 2.590405 2.465501
## [5,] 2.019895 1.988773 1.816344
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]
## [1,] 2.491552 3.491737 1.969455
## [2,] 2.472229 2.075016 1.176545
## [3,] 1.236576 1.350468 2.501260
## [4,] 1.941985 2.590405 2.465501
## [5,] 2.019895 1.988773 1.816344

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.07405726 0.05081088 0.05906003 0.0007667102 0.03553762
## [2,] 0.84885577 0.71138036 0.02715501 0.3014226533 0.02996995
## [3,] 0.22868012 0.02367679 0.04812183 0.2200796868 0.19862170
## [4,] 0.03218895 0.83676882 0.04919325 0.2001173195 0.43100781
## 
## , , 2
## 
##            [,1]       [,2]         [,3]        [,4]        [,5]
## [1,] 0.20252868 0.02983529 2.110781e-02 0.235377508 0.004832390
## [2,] 0.14434243 0.06392559 3.373541e-02 0.143449598 0.102932118
## [3,] 0.03917885 0.02125207 9.382182e-05 0.006973229 0.007447019
## [4,] 0.03653319 0.01276155 1.414777e-02 0.007392091 0.021479394
## 
## , , 3
## 
##              [,1]         [,2]        [,3]         [,4]         [,5]
## [1,] 0.0012673866 6.863323e-03 0.008189318 1.349040e-02 0.0086882296
## [2,] 0.0037099945 3.641678e-04 0.000271796 7.281114e-03 0.0001384896
## [3,] 0.0004599042 1.832733e-04 0.001256952 7.070060e-04 0.0007152380
## [4,] 0.0013863857 1.578661e-05 0.003839587 2.359191e-06 0.0008332483
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.0740572613 0.0508108806 0.0590600282 0.0007667102 0.0355376155
## [2,] 0.8488557714 0.7113803573 0.0271550072 0.3014226533 0.0299699519
## [3,] 0.2286801249 0.0236767910 0.0481218280 0.2200796868 0.1986217046
## [4,] 0.0321889513 0.8367688229 0.0491932465 0.2001173195 0.4310078102
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 2.025287e-01 2.983529e-02 2.110781e-02 2.353775e-01 4.832390e-03
## [2,] 1.443424e-01 6.392559e-02 3.373541e-02 1.434496e-01 1.029321e-01
## [3,] 3.917885e-02 2.125207e-02 9.382182e-05 6.973229e-03 7.447019e-03
## [4,] 3.653319e-02 1.276155e-02 1.414777e-02 7.392091e-03 2.147939e-02
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 1.267387e-03 6.863323e-03 8.189318e-03 1.349040e-02 8.688230e-03
## [2,] 3.709994e-03 3.641678e-04 2.717960e-04 7.281114e-03 1.384896e-04
## [3,] 4.599042e-04 1.832733e-04 1.256952e-03 7.070060e-04 7.152380e-04
## [4,] 1.386386e-03 1.578661e-05 3.839587e-03 2.359191e-06 8.332483e-04

4 Create your original function by einsum

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

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

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

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

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

Session information

## R version 4.4.1 (2024-06-14)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.1 LTS
## 
## Matrix products: default
## BLAS:   /media/volume/teran2_disk/biocbuild/bbs-3.20-bioc/R/lib/libRblas.so 
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.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.13.1
##  [3] HDF5Array_1.33.6          rhdf5_2.49.0             
##  [5] DelayedArray_0.31.11      SparseArray_1.5.39       
##  [7] S4Arrays_1.5.8            abind_1.4-8              
##  [9] IRanges_2.39.2            S4Vectors_0.43.2         
## [11] MatrixGenerics_1.17.0     matrixStats_1.4.1        
## [13] BiocGenerics_0.51.1       Matrix_1.7-0             
## [15] DelayedTensor_1.11.1      BiocStyle_2.33.1         
## 
## loaded via a namespace (and not attached):
##  [1] jsonlite_1.8.9      compiler_4.4.1      BiocManager_1.30.25
##  [4] crayon_1.5.3        rsvd_1.0.5          Rcpp_1.0.13        
##  [7] rhdf5filters_1.17.0 parallel_4.4.1      jquerylib_0.1.4    
## [10] BiocParallel_1.39.0 yaml_2.3.10         fastmap_1.2.0      
## [13] lattice_0.22-6      R6_2.5.1            XVector_0.45.0     
## [16] ScaledMatrix_1.13.0 knitr_1.48          bookdown_0.40      
## [19] bslib_0.8.0         rlang_1.1.4         cachem_1.1.0       
## [22] xfun_0.47           sass_0.4.9          cli_3.6.3          
## [25] Rhdf5lib_1.27.0     BiocSingular_1.21.4 zlibbioc_1.51.1    
## [28] digest_0.6.37       grid_4.4.1          irlba_2.3.5.1      
## [31] rTensor_1.4.8       dqrng_0.4.1         lifecycle_1.0.4    
## [34] evaluate_1.0.0      codetools_0.2-20    beachmat_2.21.6    
## [37] rmarkdown_2.28      tools_4.4.1         htmltools_0.5.8.1