Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2024-10-29 16:15:12.703724
Compiled: Tue Oct 29 20:22:51 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.8317144 0.1897086 0.6208871
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.8317144 0.1897086 0.6208871
einsum::einsum('iii->i', arrD)
## [1] 0.03217426 0.85863361 0.45823864
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
##        [1]        [2]        [3] 
## 0.03217426 0.85863361 0.45823864

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.05930889 0.41962052 0.12046284
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
##        [1]        [2]        [3] 
## 0.05930889 0.41962052 0.12046284
einsum::einsum('ij,ij->ij', arrC, arrC)
##              [,1]        [,2]      [,3]        [,4]
## [1,] 0.0001857741 0.071490773 0.9062649 0.002420734
## [2,] 0.8676513144 0.001003556 0.4914540 0.477806712
## [3,] 0.0137050823 0.973500117 0.5107900 0.329696647
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.0001857741 0.0714907734 0.9062649081 0.0024207342
## [2,] 0.8676513144 0.0010035563 0.4914539545 0.4778067117
## [3,] 0.0137050823 0.9735001172 0.5107900264 0.3296966473
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
## 
##            [,1]       [,2]        [,3]        [,4]
## [1,] 0.09582672 0.14010719 0.944389514 0.515002775
## [2,] 0.62088727 0.02003822 0.434547666 0.104880936
## [3,] 0.18600564 0.33365562 0.004320186 0.005793737
## 
## , , 2
## 
##             [,1]         [,2]      [,3]      [,4]
## [1,] 0.241382242 0.1324459546 0.4204418 0.0522559
## [2,] 0.004297148 0.1142354113 0.1863956 0.2731807
## [3,] 0.658137998 0.0004817007 0.2541810 0.2138467
## 
## , , 3
## 
##             [,1]       [,2]      [,3]      [,4]
## [1,] 0.588982206 0.03494633 0.3379393 0.3357473
## [2,] 0.567926861 0.96747678 0.1952565 0.2283728
## [3,] 0.009548042 0.09967645 0.7331515 0.8654997
## 
## , , 4
## 
##             [,1]      [,2]       [,3]        [,4]
## [1,] 0.005174832 0.6413739 0.91462772 0.951688886
## [2,] 0.097538773 0.4256765 0.04318531 0.765241507
## [3,] 0.453733691 0.9021761 0.14533619 0.007124862
## 
## , , 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.72511634 0.02482274 0.17542079 0.41085263
## [2,] 0.43333177 0.35534081 0.62260564 0.17851352
## [3,] 0.03020293 0.16688065 0.02729106 0.07558257
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.095826720 0.140107189 0.944389514 0.515002775
## [2,] 0.620887268 0.020038223 0.434547666 0.104880936
## [3,] 0.186005642 0.333655618 0.004320186 0.005793737
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.2413822419 0.1324459546 0.4204418192 0.0522559011
## [2,] 0.0042971481 0.1142354113 0.1863956338 0.2731807285
## [3,] 0.6581379984 0.0004817007 0.2541809668 0.2138466524
## 
## ,,3
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.588982206 0.034946330 0.337939264 0.335747348
## [2,] 0.567926861 0.967476784 0.195256480 0.228372843
## [3,] 0.009548042 0.099676446 0.733151514 0.865499717
## 
## ,,4
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.005174832 0.641373941 0.914627721 0.951688886
## [2,] 0.097538773 0.425676516 0.043185309 0.765241507
## [3,] 0.453733691 0.902176075 0.145336188 0.007124862
## 
## ,,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.72511634 0.02482274 0.17542079 0.41085263
## [2,] 0.43333177 0.35534081 0.62260564 0.17851352
## [3,] 0.03020293 0.16688065 0.02729106 0.07558257

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.05930889 0.1577569 0.08452524
## [2,] 0.15775686 0.4196205 0.22483033
## [3,] 0.08452524 0.2248303 0.12046284
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.05930889 0.15775686 0.08452524
## [2,] 0.15775686 0.41962052 0.22483033
## [3,] 0.08452524 0.22483033 0.12046284
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
## 
##             [,1]        [,2]      [,3]       [,4]
## [1,] 0.004219256 0.082769115 0.2946937 0.01523059
## [2,] 0.288347325 0.009806503 0.2170125 0.21397815
## [3,] 0.036239662 0.305429736 0.2212404 0.17774630
## 
## , , 2, 1, 1
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.01073987 0.21068391 0.7501256 0.03876858
## [2,] 0.73397115 0.02496188 0.5523925 0.54466880
## [3,] 0.09224593 0.77745343 0.5631545 0.45244276
## 
## , , 3, 1, 1
## 
##             [,1]       [,2]      [,3]       [,4]
## [1,] 0.005878353 0.11531560 0.4105732 0.02121957
## [2,] 0.401731303 0.01366262 0.3023462 0.29811867
## [3,] 0.050489827 0.42553086 0.3082366 0.24763973
## 
## , , 1, 2, 1
## 
##             [,1]       [,2]      [,3]       [,4]
## [1,] 0.005101793 0.10008182 0.3563344 0.01841636
## [2,] 0.348660561 0.01185772 0.2624047 0.25873569
## [3,] 0.043819865 0.36931608 0.2675170 0.21492527
## 
## , , 2, 2, 1
## 
##            [,1]       [,2]      [,3]        [,4]
## [1,] 0.00192940 0.03784902 0.1347588 0.006964712
## [2,] 0.13185670 0.00448436 0.0992364 0.097848850
## [3,] 0.01657183 0.13966822 0.1011698 0.081280594
## 
## , , 3, 2, 1
## 
##             [,1]       [,2]      [,3]       [,4]
## [1,] 0.007873028 0.15444513 0.5498912 0.02841991
## [2,] 0.538049009 0.01829869 0.4049400 0.39927797
## [3,] 0.067622317 0.56992437 0.4128292 0.33167023
## 
## , , 1, 3, 1
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.01324549 0.25983675 0.9251308 0.04781335
## [2,] 0.90520760 0.03078552 0.6812664 0.67174076
## [3,] 0.11376703 0.95883435 0.6945392 0.55799826
## 
## , , 2, 3, 1
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.00898486 0.17625592 0.6275471 0.03243338
## [2,] 0.61403245 0.02088284 0.4621257 0.45566412
## [3,] 0.07717196 0.65040926 0.4711291 0.37850880
## 
## , , 3, 3, 1
## 
##              [,1]        [,2]       [,3]        [,4]
## [1,] 0.0008958676 0.017574227 0.06257183 0.003233887
## [2,] 0.0612243041 0.002082198 0.04607790 0.045433623
## [3,] 0.0076947066 0.064851384 0.04697561 0.037740575
## 
## , , 1, 4, 1
## 
##             [,1]       [,2]      [,3]       [,4]
## [1,] 0.009781318 0.19188003 0.6831756 0.03530842
## [2,] 0.668463039 0.02273399 0.5030906 0.49605623
## [3,] 0.084012829 0.70806445 0.5128921 0.41206151
## 
## , , 2, 4, 1
## 
##             [,1]       [,2]      [,3]       [,4]
## [1,] 0.004414087 0.08659110 0.3083017 0.01593389
## [2,] 0.301662199 0.01025933 0.2270334 0.22385892
## [3,] 0.037913083 0.31953342 0.2314566 0.18595401
## 
## , , 3, 4, 1
## 
##             [,1]        [,2]       [,3]       [,4]
## [1,] 0.001037461 0.020351873 0.07246144 0.00374501
## [2,] 0.070900942 0.002411295 0.05336061 0.05261451
## [3,] 0.008910872 0.075101290 0.05440021 0.04370556
## 
## , , 1, 1, 2
## 
##             [,1]       [,2]      [,3]       [,4]
## [1,] 0.006696459 0.13136439 0.4677139 0.02417276
## [2,] 0.457641366 0.01556408 0.3444245 0.33960868
## [3,] 0.057516637 0.48475318 0.3511348 0.28210444
## 
## , , 2, 1, 2
## 
##              [,1]        [,2]       [,3]        [,4]
## [1,] 0.0008934757 0.017527306 0.06240476 0.003225252
## [2,] 0.0610608403 0.002076639 0.04595487 0.045312319
## [3,] 0.0076741624 0.064678236 0.04685019 0.037639810
## 
## , , 3, 1, 2
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.01105735 0.21691195 0.7723001 0.03991462
## [2,] 0.75566811 0.02569978 0.5687218 0.56076979
## [3,] 0.09497281 0.80043577 0.5798020 0.46581744
## 
## , , 1, 2, 2
## 
##             [,1]       [,2]      [,3]       [,4]
## [1,] 0.004960346 0.09730706 0.3464551 0.01790577
## [2,] 0.338993962 0.01152896 0.2551296 0.25156225
## [3,] 0.042604961 0.35907681 0.2601001 0.20896647
## 
## , , 2, 2, 2
## 
##             [,1]       [,2]      [,3]      [,4]
## [1,] 0.004606732 0.09037023 0.3217570 0.0166293
## [2,] 0.314827738 0.01070708 0.2369419 0.2336289
## [3,] 0.039567736 0.33347891 0.2415581 0.1940697
## 
## , , 3, 2, 2
## 
##              [,1]         [,2]       [,3]        [,4]
## [1,] 0.0002991446 0.0058683179 0.02089374 0.001079847
## [2,] 0.0204437823 0.0006952796 0.01538615 0.015171019
## [3,] 0.0025693866 0.0216549227 0.01568591 0.012602186
## 
## , , 1, 3, 2
## 
##             [,1]       [,2]      [,3]       [,4]
## [1,] 0.008837828 0.17337160 0.6172776 0.03190263
## [2,] 0.603984186 0.02054111 0.4545633 0.44820746
## [3,] 0.075909089 0.63976571 0.4634193 0.37231473
## 
## , , 2, 3, 2
## 
##             [,1]       [,2]      [,3]       [,4]
## [1,] 0.005884512 0.11543642 0.4110034 0.02124181
## [2,] 0.402152231 0.01367693 0.3026630 0.29843104
## [3,] 0.050542730 0.42597673 0.3085596 0.24789920
## 
## , , 3, 3, 2
## 
##             [,1]       [,2]      [,3]       [,4]
## [1,] 0.006871698 0.13480205 0.4799534 0.02480533
## [2,] 0.469617344 0.01597138 0.3534377 0.34849587
## [3,] 0.059021785 0.49743864 0.3603236 0.28948681
## 
## , , 1, 4, 2
## 
##             [,1]        [,2]      [,3]       [,4]
## [1,] 0.003115733 0.061121312 0.2176182 0.01124712
## [2,] 0.212931682 0.007241667 0.1602541 0.15801335
## [3,] 0.026761379 0.225546283 0.1633762 0.13125774
## 
## , , 2, 4, 2
## 
##             [,1]       [,2]      [,3]       [,4]
## [1,] 0.007123897 0.13974942 0.4975682 0.02571571
## [2,] 0.486852768 0.01655754 0.3664093 0.36128602
## [3,] 0.061187943 0.51569513 0.3735478 0.30011126
## 
## , , 3, 4, 2
## 
##             [,1]       [,2]      [,3]       [,4]
## [1,] 0.006302949 0.12364491 0.4402292 0.02275227
## [2,] 0.430748568 0.01464948 0.3241848 0.31965194
## [3,] 0.054136734 0.45626718 0.3305007 0.26552688
## 
## , , 1, 1, 3
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.01046029 0.20519940 0.7305983 0.03775936
## [2,] 0.71486445 0.02431207 0.5380127 0.53049001
## [3,] 0.08984459 0.75721480 0.5484945 0.44066479
## 
## , , 2, 1, 3
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.01027162 0.20149821 0.7174205 0.0370783
## [2,] 0.70197043 0.02387355 0.5283085 0.5209216
## [3,] 0.08822406 0.74355690 0.5386013 0.4327165
## 
## , , 3, 1, 3
## 
##             [,1]       [,2]       [,3]        [,4]
## [1,] 0.001331833 0.02612656 0.09302180 0.004807626
## [2,] 0.091018521 0.00309548 0.06850126 0.067543456
## [3,] 0.011439261 0.09641068 0.06983584 0.056106661
## 
## , , 1, 2, 3
## 
##             [,1]        [,2]      [,3]        [,4]
## [1,] 0.002547964 0.049983399 0.1779624 0.009197596
## [2,] 0.174129921 0.005922044 0.1310516 0.129219159
## [3,] 0.021884751 0.184445809 0.1336048 0.107339126
## 
## , , 2, 2, 3
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.01340642 0.26299366 0.9363708 0.04839426
## [2,] 0.91620549 0.03115955 0.6895435 0.67990213
## [3,] 0.11514925 0.97048378 0.7029776 0.56477770
## 
## , , 3, 2, 3
## 
##             [,1]       [,2]      [,3]       [,4]
## [1,] 0.004303173 0.08441532 0.3005549 0.01553352
## [2,] 0.294082300 0.01000155 0.2213287 0.21823399
## [3,] 0.036960437 0.31150447 0.2256407 0.18128152
## 
## , , 1, 3, 3
## 
##             [,1]       [,2]      [,3]       [,4]
## [1,] 0.007923406 0.15543339 0.5534099 0.02860177
## [2,] 0.541491871 0.01841578 0.4075311 0.40183286
## [3,] 0.068055018 0.57357119 0.4154708 0.33379251
## 
## , , 2, 3, 3
## 
##             [,1]       [,2]      [,3]       [,4]
## [1,] 0.006022757 0.11814837 0.4206591 0.02174084
## [2,] 0.411599978 0.01399825 0.3097734 0.30544207
## [3,] 0.051730128 0.43598418 0.3158086 0.25372309
## 
## , , 3, 3, 3
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.0116705 0.22894010 0.8151254 0.04212796
## [2,] 0.7975712 0.02712487 0.6002585 0.59186545
## [3,] 0.1002392 0.84482133 0.6119530 0.49164784
## 
## , , 1, 4, 3
## 
##             [,1]       [,2]      [,3]       [,4]
## [1,] 0.007897668 0.15492849 0.5516122 0.02850886
## [2,] 0.539732923 0.01835596 0.4062073 0.40052757
## [3,] 0.067833952 0.57170804 0.4141212 0.33270824
## 
## , , 2, 4, 3
## 
##             [,1]       [,2]      [,3]       [,4]
## [1,] 0.006513506 0.12777539 0.4549355 0.02351234
## [2,] 0.445138178 0.01513886 0.3350145 0.33033025
## [3,] 0.055945229 0.47150927 0.3415415 0.27439709
## 
## , , 3, 4, 3
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.0126802 0.24874735 0.8856478 0.04577275
## [2,] 0.8665748 0.02947164 0.6521911 0.64307198
## [3,] 0.1089116 0.91791289 0.6648975 0.53418382
## 
## , , 1, 1, 4
## 
##              [,1]        [,2]       [,3]        [,4]
## [1,] 0.0009804845 0.019234156 0.06848189 0.003539335
## [2,] 0.0670070893 0.002278867 0.05043007 0.049724939
## [3,] 0.0084214905 0.070976755 0.05141257 0.041305264
## 
## , , 2, 1, 4
## 
##            [,1]        [,2]      [,3]       [,4]
## [1,] 0.00425678 0.083505223 0.2973146 0.01536605
## [2,] 0.29091175 0.009893718 0.2189425 0.21588117
## [3,] 0.03656196 0.308146081 0.2232080 0.17932709
## 
## , , 3, 1, 4
## 
##             [,1]       [,2]      [,3]       [,4]
## [1,] 0.009181066 0.18010489 0.6412511 0.03314165
## [2,] 0.627441338 0.02133887 0.4722173 0.46561465
## [3,] 0.078857197 0.66461252 0.4814173 0.38677445
## 
## , , 1, 2, 4
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.01091562 0.21413155 0.7624006 0.03940299
## [2,] 0.74598186 0.02537035 0.5614319 0.55358177
## [3,] 0.09375544 0.79017568 0.5723700 0.45984654
## 
## , , 2, 2, 4
## 
##             [,1]       [,2]      [,3]       [,4]
## [1,] 0.008892675 0.17444754 0.6211084 0.03210062
## [2,] 0.607732498 0.02066858 0.4573843 0.45098902
## [3,] 0.076380179 0.64373608 0.4662953 0.37462531
## 
## , , 3, 2, 4
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.01294608 0.25396312 0.9042182 0.04673252
## [2,] 0.88474531 0.03008961 0.6658664 0.65655600
## [3,] 0.11119531 0.93715981 0.6788391 0.54538466
## 
## , , 1, 3, 4
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.01303511 0.25570969 0.9104367 0.04705391
## [2,] 0.89082992 0.03029654 0.6704457 0.66107130
## [3,] 0.11196003 0.94360489 0.6835077 0.54913541
## 
## , , 2, 3, 4
## 
##             [,1]        [,2]      [,3]       [,4]
## [1,] 0.002832439 0.055563938 0.1978316 0.01022449
## [2,] 0.193571151 0.006583228 0.1456832 0.14364620
## [3,] 0.024328136 0.205038786 0.1485215 0.11932331
## 
## , , 3, 3, 4
## 
##             [,1]       [,2]      [,3]       [,4]
## [1,] 0.005196124 0.10193231 0.3629230 0.01875687
## [2,] 0.355107216 0.01207696 0.2672565 0.26351965
## [3,] 0.044630084 0.37614465 0.2724633 0.21889919
## 
## , , 1, 4, 4
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.01329658 0.26083898 0.9286992 0.04799777
## [2,] 0.90869913 0.03090426 0.6838942 0.67433177
## [3,] 0.11420584 0.96253272 0.6972182 0.56015055
## 
## , , 2, 4, 4
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.01192317 0.23389679 0.8327734 0.04304005
## [2,] 0.81483913 0.02771214 0.6132544 0.60467969
## [3,] 0.10240946 0.86311222 0.6252022 0.50229230
## 
## , , 3, 4, 4
## 
##             [,1]        [,2]       [,3]        [,4]
## [1,] 0.001150485 0.022569047 0.08035554 0.004152999
## [2,] 0.078625033 0.002673986 0.05917383 0.058346439
## [3,] 0.009881641 0.083282975 0.06032668 0.048466928
## 
## , , 1, 1, 5
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.01160637 0.22768208 0.8106463 0.04189647
## [2,] 0.79318859 0.02697582 0.5969600 0.58861316
## [3,] 0.09968841 0.84017905 0.6085903 0.48894624
## 
## , , 2, 1, 5
## 
##             [,1]      [,2]      [,3]       [,4]
## [1,] 0.008972281 0.1760092 0.6266685 0.03238798
## [2,] 0.613172794 0.0208536 0.4614787 0.45502618
## [3,] 0.077063918 0.6494987 0.4704695 0.37797888
## 
## , , 3, 1, 5
## 
##             [,1]        [,2]      [,3]        [,4]
## [1,] 0.002368738 0.046467522 0.1654444 0.008550629
## [2,] 0.161881466 0.005505482 0.1218333 0.120129768
## [3,] 0.020345358 0.171471726 0.1242069 0.099788796
## 
## , , 1, 2, 5
## 
##             [,1]        [,2]      [,3]        [,4]
## [1,] 0.002147422 0.042125968 0.1499866 0.007751727
## [2,] 0.146756555 0.004991094 0.1104501 0.108905802
## [3,] 0.018444450 0.155450777 0.1126020 0.090465327
## 
## , , 2, 2, 5
## 
##             [,1]       [,2]      [,3]       [,4]
## [1,] 0.008124846 0.15938503 0.5674794 0.02932892
## [2,] 0.555258425 0.01888397 0.4178919 0.41204881
## [3,] 0.069785206 0.58815331 0.4260335 0.34227865
## 
## , , 3, 2, 5
## 
##             [,1]       [,2]      [,3]      [,4]
## [1,] 0.005567953 0.10922649 0.3888934 0.0200991
## [2,] 0.380518350 0.01294118 0.2863811 0.2823769
## [3,] 0.047823771 0.40306120 0.2919606 0.2345634
## 
## , , 1, 3, 5
## 
##             [,1]       [,2]      [,3]       [,4]
## [1,] 0.005708646 0.11198646 0.3987201 0.02060697
## [2,] 0.390133411 0.01326818 0.2936175 0.28951206
## [3,] 0.049032197 0.41324588 0.2993379 0.24049043
## 
## , , 2, 3, 5
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.01075472 0.2109753 0.7511629 0.03882219
## [2,] 0.73498612 0.0249964 0.5531564 0.54542200
## [3,] 0.09237349 0.7785285 0.5639333 0.45306842
## 
## , , 3, 3, 5
## 
##            [,1]        [,2]      [,3]        [,4]
## [1,] 0.00225166 0.044170794 0.1572671 0.008128001
## [2,] 0.15388023 0.005233366 0.1158115 0.114192171
## [3,] 0.01933976 0.162996475 0.1180678 0.094856582
## 
## , , 1, 4, 5
## 
##             [,1]       [,2]      [,3]       [,4]
## [1,] 0.008736463 0.17138312 0.6101978 0.03153672
## [2,] 0.597056802 0.02030551 0.4493497 0.44306675
## [3,] 0.075038451 0.63242793 0.4581042 0.36804447
## 
## , , 2, 4, 5
## 
##             [,1]       [,2]      [,3]       [,4]
## [1,] 0.005758749 0.11296933 0.4022195 0.02078783
## [2,] 0.393557475 0.01338463 0.2961945 0.29205300
## [3,] 0.049462536 0.41687280 0.3019651 0.24260113
## 
## , , 3, 4, 5
## 
##            [,1]        [,2]      [,3]       [,4]
## [1,] 0.00374717 0.073508209 0.2617209 0.01352647
## [2,] 0.25608460 0.008709269 0.1927313 0.19003647
## [3,] 0.03218486 0.271255683 0.1964862 0.15785855
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.004219256 0.082769115 0.294693729 0.015230595
## [2,] 0.288347325 0.009806503 0.217012490 0.213978153
## [3,] 0.036239662 0.305429736 0.221240442 0.177746303
## 
## ,,2,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01073987 0.21068391 0.75012555 0.03876858
## [2,] 0.73397115 0.02496188 0.55239253 0.54466880
## [3,] 0.09224593 0.77745343 0.56315453 0.45244276
## 
## ,,3,1,1
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.005878353 0.115315598 0.410573241 0.021219572
## [2,] 0.401731303 0.013662618 0.302346173 0.298118675
## [3,] 0.050489827 0.425530862 0.308236641 0.247639732
## 
## ...
## 
## ,,1,4,5
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.008736463 0.171383116 0.610197772 0.031536725
## [2,] 0.597056802 0.020305510 0.449349697 0.443066749
## [3,] 0.075038451 0.632427928 0.458104165 0.368044474
## 
## ,,2,4,5
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.005758749 0.112969329 0.402219510 0.020787827
## [2,] 0.393557475 0.013384632 0.296194485 0.292053002
## [3,] 0.049462536 0.416872796 0.301965103 0.242601128
## 
## ,,3,4,5
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.003747170 0.073508209 0.261720911 0.013526467
## [2,] 0.256084596 0.008709269 0.192731303 0.190036474
## [3,] 0.032184863 0.271255683 0.196486196 0.157858549

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

3.3.1 Row-wise / Column-wise Summation

einsum::einsum('ij->i', arrC)
## [1] 1.282188 2.355430 2.392618
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
##      [1]      [2]      [3] 
## 1.282188 2.355430 2.392618
einsum::einsum('ij->j', arrC)
## [1] 1.062177 1.285718 2.367713 1.314629
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 1.062177 1.285718 2.367713 1.314629

3.3.2 Mode-wise Vectorization

einsum::einsum('ijk->i', arrE)
## [1] 11.094306 10.440783  8.262045
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 11.094306 10.440783  8.262045
einsum::einsum('ijk->j', arrE)
## [1] 7.257151 6.868914 8.078977 7.592092
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 7.257151 6.868914 8.078977 7.592092
einsum::einsum('ijk->k', arrE)
## [1] 5.436636 4.889995 6.972124 6.941110 5.557270
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4]      [5] 
## 5.436636 4.889995 6.972124 6.941110 5.557270

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.491791 1.883590 3.576732 3.142193
## [2,] 2.577717 2.711691 2.529681 2.621693
## [3,] 2.187642 2.273633 1.972564 1.828206
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]     [,4]
## [1,] 2.491791 1.883590 3.576732 3.142193
## [2,] 2.577717 2.711691 2.529681 2.621693
## [3,] 2.187642 2.273633 1.972564 1.828206
einsum::einsum('ijk->jk', arrE)
##          [,1]      [,2]     [,3]     [,4]     [,5]
## [1,] 1.528807 1.3681164 1.618775 1.057846 1.683607
## [2,] 1.093495 0.7238662 1.486259 2.403127 1.162167
## [3,] 1.696727 1.5843141 1.879446 1.545402 1.373086
## [4,] 1.117607 1.2136980 1.987643 1.934735 1.338409
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.5288066 1.3681164 1.6187751 1.0578459 1.6836069
## [2,] 1.0934947 0.7238662 1.4862591 2.4031266 1.1621673
## [3,] 1.6967275 1.5843141 1.8794465 1.5454023 1.3730863
## [4,] 1.1176068 1.2136980 1.9876432 1.9347352 1.3384093
einsum::einsum('ijk->jk', arrE)
##          [,1]      [,2]     [,3]     [,4]     [,5]
## [1,] 1.528807 1.3681164 1.618775 1.057846 1.683607
## [2,] 1.093495 0.7238662 1.486259 2.403127 1.162167
## [3,] 1.696727 1.5843141 1.879446 1.545402 1.373086
## [4,] 1.117607 1.2136980 1.987643 1.934735 1.338409
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.5288066 1.3681164 1.6187751 1.0578459 1.6836069
## [2,] 1.0934947 0.7238662 1.4862591 2.4031266 1.1621673
## [3,] 1.6967275 1.5843141 1.8794465 1.5454023 1.3730863
## [4,] 1.1176068 1.2136980 1.9876432 1.9347352 1.3384093

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

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.8317144 0.5811093 0.59122036
## [2,] 0.2216178 0.1897086 0.05901558
## [3,] 0.5430072 0.3383280 0.62088708
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.83171441 0.58110932 0.59122036
## [2,] 0.22161776 0.18970856 0.05901558
## [3,] 0.54300721 0.33832803 0.62088708
einsum::einsum('ijk->jki', arrD)
## , , 1
## 
##            [,1]       [,2]      [,3]
## [1,] 0.03217426 0.85625299 0.7979777
## [2,] 0.32022400 0.04171301 0.7606669
## [3,] 0.18668808 0.80524256 0.8311667
## 
## , , 2
## 
##            [,1]       [,2]      [,3]
## [1,] 0.46898117 0.04718005 0.5032766
## [2,] 0.08370335 0.85863361 0.8867758
## [3,] 0.10218570 0.42598548 0.7057526
## 
## , , 3
## 
##           [,1]       [,2]       [,3]
## [1,] 0.5680926 0.07968782 0.99099700
## [2,] 0.6366627 0.18172177 0.09190605
## [3,] 0.8488421 0.41079658 0.45823864
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
##            [,1]       [,2]       [,3]
## [1,] 0.03217426 0.85625299 0.79797772
## [2,] 0.32022400 0.04171301 0.76066686
## [3,] 0.18668808 0.80524256 0.83116668
## 
## ,,2
##            [,1]       [,2]       [,3]
## [1,] 0.46898117 0.04718005 0.50327658
## [2,] 0.08370335 0.85863361 0.88677585
## [3,] 0.10218570 0.42598548 0.70575256
## 
## ,,3
##            [,1]       [,2]       [,3]
## [1,] 0.56809256 0.07968782 0.99099700
## [2,] 0.63666268 0.18172177 0.09190605
## [3,] 0.84884210 0.41079658 0.45823864

3.5 Multiplication + Summation

Some examples of combining Multiplication and Summation are shown below.

3.5.1 Inner Product (Squared Frobenius Norm)

Inner Product first calculate Hadamard Product and collapses it to 0D tensor (norm).

einsum::einsum('i,i->', arrA, arrA)
## [1] 0.5993922
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
##       [1] 
## 0.5993922
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 4.64597
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
##     [1] 
## 4.64597
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 19.5001
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
##     [1] 
## 19.5001

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.9027196 0.9038174 1.166457 0.5564473 1.1886510
## [2,] 0.4938010 0.2471631 1.102100 1.9692265 0.5470442
## [3,] 1.3832574 0.8610184 1.266347 1.1031492 0.8253175
## [4,] 0.6256774 0.5392833 1.429620 1.7240553 0.6649487
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.9027196 0.9038174 1.1664571 0.5564473 1.1886510
## [2,] 0.4938010 0.2471631 1.1020996 1.9692265 0.5470442
## [3,] 1.3832574 0.8610184 1.2663473 1.1031492 0.8253175
## [4,] 0.6256774 0.5392833 1.4296199 1.7240553 0.6649487

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.9803622 0.7225492 0.9740332
## [2,] 0.7225492 1.8379155 1.0382341
## [3,] 0.9740332 1.0382341 1.8276919
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 0.9803622 0.7225492 0.9740332
## [2,] 0.7225492 1.8379155 1.0382341
## [3,] 0.9740332 1.0382341 1.8276919

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.0001857741 0.867651314 0.01370508
## [2,] 0.0714907734 0.001003556 0.97350012
## [3,] 0.9062649081 0.491453955 0.51079003
## [4,] 0.0024207342 0.477806712 0.32969665
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
##              [,1]         [,2]         [,3]
## [1,] 0.0001857741 0.8676513144 0.0137050823
## [2,] 0.0714907734 0.0010035563 0.9735001172
## [3,] 0.9062649081 0.4914539545 0.5107900264
## [4,] 0.0024207342 0.4778067117 0.3296966473
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
## 
##            [,1]      [,2]       [,3]        [,4]       [,5]
## [1,] 0.09582672 0.2413822 0.58898221 0.005174832 0.72511634
## [2,] 0.14010719 0.1324460 0.03494633 0.641373941 0.02482274
## [3,] 0.94438951 0.4204418 0.33793926 0.914627721 0.17542079
## [4,] 0.51500278 0.0522559 0.33574735 0.951688886 0.41085263
## 
## , , 2
## 
##            [,1]        [,2]      [,3]       [,4]      [,5]
## [1,] 0.62088727 0.004297148 0.5679269 0.09753877 0.4333318
## [2,] 0.02003822 0.114235411 0.9674768 0.42567652 0.3553408
## [3,] 0.43454767 0.186395634 0.1952565 0.04318531 0.6226056
## [4,] 0.10488094 0.273180728 0.2283728 0.76524151 0.1785135
## 
## , , 3
## 
##             [,1]         [,2]        [,3]        [,4]       [,5]
## [1,] 0.186005642 0.6581379984 0.009548042 0.453733691 0.03020293
## [2,] 0.333655618 0.0004817007 0.099676446 0.902176075 0.16688065
## [3,] 0.004320186 0.2541809668 0.733151514 0.145336188 0.02729106
## [4,] 0.005793737 0.2138466524 0.865499717 0.007124862 0.07558257
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.095826720 0.241382242 0.588982206 0.005174832 0.725116341
## [2,] 0.140107189 0.132445955 0.034946330 0.641373941 0.024822744
## [3,] 0.944389514 0.420441819 0.337939264 0.914627721 0.175420790
## [4,] 0.515002775 0.052255901 0.335747348 0.951688886 0.410852630
## 
## ,,2
##             [,1]        [,2]        [,3]        [,4]        [,5]
## [1,] 0.620887268 0.004297148 0.567926861 0.097538773 0.433331765
## [2,] 0.020038223 0.114235411 0.967476784 0.425676516 0.355340807
## [3,] 0.434547666 0.186395634 0.195256480 0.043185309 0.622605639
## [4,] 0.104880936 0.273180728 0.228372843 0.765241507 0.178513515
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.1860056424 0.6581379984 0.0095480419 0.4537336908 0.0302029268
## [2,] 0.3336556182 0.0004817007 0.0996764460 0.9021760745 0.1668806491
## [3,] 0.0043201864 0.2541809668 0.7331515143 0.1453361884 0.0272910607
## [4,] 0.0057937371 0.2138466524 0.8654997174 0.0071248620 0.0755825748

3.7 Summation + Permutation

Some examples of combining Summation and Permutation are shown below.

einsum::einsum('ijk->ki', arrE)
##          [,1]     [,2]     [,3]
## [1,] 2.373302 1.912576 1.150758
## [2,] 1.732248 1.357942 1.799804
## [3,] 2.115154 2.656975 2.199995
## [4,] 2.804702 2.047342 2.089066
## [5,] 2.068901 2.465946 1.022423
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]
## [1,] 2.373302 1.912576 1.150758
## [2,] 1.732248 1.357942 1.799804
## [3,] 2.115154 2.656975 2.199995
## [4,] 2.804702 2.047342 2.089066
## [5,] 2.068901 2.465946 1.022423

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,] 4.335425e-06 0.0000109207 2.664693e-05 2.341215e-07 3.280596e-05
## [2,] 2.439329e-03 0.0023059431 6.084312e-04 1.116661e-02 4.321750e-04
## [3,] 2.084329e-01 0.0927942266 7.458538e-02 2.018642e-01 3.871650e-02
## [4,] 3.036103e-04 0.0000308065 1.979336e-04 5.610505e-04 2.422106e-04
## 
## , , 2
## 
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 3.489686e-01 2.415205e-03 0.3192022898 0.0548214952 0.2435533536
## [2,] 1.302655e-05 7.426272e-05 0.0006289421 0.0002767259 0.0002310017
## [3,] 1.383403e-01 5.933992e-02 0.0621608078 0.0137482439 0.1982093982
## [4,] 3.246214e-02 8.455332e-02 0.0706846420 0.2368531274 0.0552524712
## 
## , , 3
## 
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.0008847780 0.0031305826 4.541743e-05 0.0021582871 0.0001436671
## [2,] 0.1127355783 0.0001627571 3.367868e-02 0.3048273009 0.0563856427
## [3,] 0.0007658989 0.0450621577 1.299759e-01 0.0257657460 0.0048382619
## [4,] 0.0006629791 0.0244705389 9.903940e-02 0.0008153002 0.0086489375
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,] 4.335425e-06 1.092070e-05 2.664693e-05 2.341215e-07 3.280596e-05
## [2,] 2.439329e-03 2.305943e-03 6.084312e-04 1.116661e-02 4.321750e-04
## [3,] 2.084329e-01 9.279423e-02 7.458538e-02 2.018642e-01 3.871650e-02
## [4,] 3.036103e-04 3.080650e-05 1.979336e-04 5.610505e-04 2.422106e-04
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 3.489686e-01 2.415205e-03 3.192023e-01 5.482150e-02 2.435534e-01
## [2,] 1.302655e-05 7.426272e-05 6.289421e-04 2.767259e-04 2.310017e-04
## [3,] 1.383403e-01 5.933992e-02 6.216081e-02 1.374824e-02 1.982094e-01
## [4,] 3.246214e-02 8.455332e-02 7.068464e-02 2.368531e-01 5.525247e-02
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 8.847780e-04 3.130583e-03 4.541743e-05 2.158287e-03 1.436671e-04
## [2,] 1.127356e-01 1.627571e-04 3.367868e-02 3.048273e-01 5.638564e-02
## [3,] 7.658989e-04 4.506216e-02 1.299759e-01 2.576575e-02 4.838262e-03
## [4,] 6.629791e-04 2.447054e-02 9.903940e-02 8.153002e-04 8.648938e-03

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:   /home/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.14.0
##  [3] HDF5Array_1.34.0          rhdf5_2.50.0             
##  [5] DelayedArray_0.32.0       SparseArray_1.6.0        
##  [7] S4Arrays_1.6.0            abind_1.4-8              
##  [9] IRanges_2.40.0            S4Vectors_0.44.0         
## [11] MatrixGenerics_1.18.0     matrixStats_1.4.1        
## [13] BiocGenerics_0.52.0       Matrix_1.7-1             
## [15] DelayedTensor_1.12.0      BiocStyle_2.34.0         
## 
## 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.18.0 parallel_4.4.1      jquerylib_0.1.4    
## [10] BiocParallel_1.40.0 yaml_2.3.10         fastmap_1.2.0      
## [13] lattice_0.22-6      R6_2.5.1            XVector_0.46.0     
## [16] ScaledMatrix_1.14.0 knitr_1.48          bookdown_0.41      
## [19] bslib_0.8.0         rlang_1.1.4         cachem_1.1.0       
## [22] xfun_0.48           sass_0.4.9          cli_3.6.3          
## [25] Rhdf5lib_1.28.0     BiocSingular_1.22.0 zlibbioc_1.52.0    
## [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.1      codetools_0.2-20    beachmat_2.22.0    
## [37] rmarkdown_2.28      tools_4.4.1         htmltools_0.5.8.1