Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2025-04-04 14:56:50.301149
Compiled: Tue Apr 15 19:26:37 2025

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.3780281 0.7056558 0.7072691
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.3780281 0.7056558 0.7072691
einsum::einsum('iii->i', arrD)
## [1] 0.16432989 0.31015423 0.06343364
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
##        [1]        [2]        [3] 
## 0.16432989 0.31015423 0.06343364

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.2975366 0.4166218 0.7581414
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.2975366 0.4166218 0.7581414
einsum::einsum('ij,ij->ij', arrC, arrC)
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.02920138 0.7050368 0.01475394 0.19996780
## [2,] 0.70577542 0.8649089 0.55985429 0.04878672
## [3,] 0.38963795 0.9905800 0.24805247 0.80862521
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02920138 0.70503685 0.01475394 0.19996780
## [2,] 0.70577542 0.86490887 0.55985429 0.04878672
## [3,] 0.38963795 0.99058001 0.24805247 0.80862521
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
## 
##            [,1]         [,2]         [,3]      [,4]
## [1,] 0.41297869 0.0251582222 4.525079e-05 0.8297734
## [2,] 0.01194106 0.0008580819 1.478415e-01 0.1768106
## [3,] 0.02689780 0.2869101175 6.177877e-01 0.4739657
## 
## , , 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02701527 0.10026586 0.03187008 0.28981226
## [2,] 0.02926500 0.23517519 0.01435167 0.00628731
## [3,] 0.09607359 0.07445438 0.60633943 0.04207739
## 
## , , 3
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.1699899 0.82201668 0.4719428 0.22842099
## [2,] 0.2108937 0.03933482 0.1784333 0.04503161
## [3,] 0.1863953 0.11043586 0.6094701 0.50614170
## 
## , , 4
## 
##             [,1]      [,2]       [,3]        [,4]
## [1,] 0.012598249 0.2813230 0.00260042 0.986160351
## [2,] 0.368325630 0.3291700 0.23599211 0.005011427
## [3,] 0.003090231 0.2755601 0.26827510 0.228668244
## 
## , , 5
## 
##            [,1]      [,2]       [,3]      [,4]
## [1,] 0.14598829 0.1687078 0.77866462 0.2040277
## [2,] 0.26770562 0.6102170 0.92108303 0.4340177
## [3,] 0.09308529 0.1236678 0.01469391 0.1472417
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
##              [,1]         [,2]         [,3]         [,4]
## [1,] 4.129787e-01 2.515822e-02 4.525079e-05 8.297734e-01
## [2,] 1.194106e-02 8.580819e-04 1.478415e-01 1.768106e-01
## [3,] 2.689780e-02 2.869101e-01 6.177877e-01 4.739657e-01
## 
## ,,2
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02701527 0.10026586 0.03187008 0.28981226
## [2,] 0.02926500 0.23517519 0.01435167 0.00628731
## [3,] 0.09607359 0.07445438 0.60633943 0.04207739
## 
## ,,3
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.16998992 0.82201668 0.47194276 0.22842099
## [2,] 0.21089373 0.03933482 0.17843329 0.04503161
## [3,] 0.18639529 0.11043586 0.60947011 0.50614170
## 
## ,,4
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.012598249 0.281323015 0.002600420 0.986160351
## [2,] 0.368325630 0.329170033 0.235992105 0.005011427
## [3,] 0.003090231 0.275560141 0.268275101 0.228668244
## 
## ,,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.14598829 0.16870776 0.77866462 0.20402767
## [2,] 0.26770562 0.61021702 0.92108303 0.43401775
## [3,] 0.09308529 0.12366776 0.01469391 0.14724172

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.2975366 0.3520799 0.4749472
## [2,] 0.3520799 0.4166218 0.5620127
## [3,] 0.4749472 0.5620127 0.7581414
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 0.2975366 0.3520799 0.4749472
## [2,] 0.3520799 0.4166218 0.5620127
## [3,] 0.4749472 0.5620127 0.7581414
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.1098160 0.5395972 0.07805808 0.2873716
## [2,] 0.5398798 0.5976528 0.48084081 0.1419432
## [3,] 0.4011386 0.6396002 0.32006309 0.5778797
## 
## , , 2, 1, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01867339 0.09175449 0.01327319 0.04886540
## [2,] 0.09180254 0.10162641 0.08176340 0.02413639
## [3,] 0.06821063 0.10875925 0.05442434 0.09826414
## 
## , , 3, 1, 1
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.02802593 0.1377096 0.01992106 0.07333959
## [2,] 0.13778174 0.1525259 0.12271451 0.03622507
## [3,] 0.10237385 0.1632312 0.08168272 0.14747963
## 
## , , 1, 2, 1
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.02710452 0.1331821 0.01926611 0.07092838
## [2,] 0.13325185 0.1475113 0.11867998 0.03503409
## [3,] 0.09900807 0.1578646 0.07899721 0.14263090
## 
## , , 2, 2, 1
## 
##             [,1]       [,2]        [,3]        [,4]
## [1,] 0.005005714 0.02459633 0.003558102 0.013099189
## [2,] 0.024609208 0.02724266 0.021918048 0.006470162
## [3,] 0.018285001 0.02915474 0.014589357 0.026341349
## 
## , , 3, 2, 1
## 
##            [,1]      [,2]       [,3]      [,4]
## [1,] 0.09153235 0.4497579 0.06506193 0.2395262
## [2,] 0.44999345 0.4981477 0.40078406 0.1183106
## [3,] 0.33435172 0.5331111 0.26677474 0.4816666
## 
## , , 1, 3, 1
## 
##             [,1]        [,2]         [,3]        [,4]
## [1,] 0.001149515 0.005648316 0.0008170848 0.003008106
## [2,] 0.005651274 0.006256022 0.0050332744 0.001485812
## [3,] 0.004198979 0.006695113 0.0033503091 0.006049044
## 
## , , 2, 3, 1
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.06570522 0.3228524 0.0467038 0.17194051
## [2,] 0.32302150 0.3575883 0.2876972 0.08492763
## [3,] 0.24000970 0.3826863 0.1915005 0.34575765
## 
## , , 3, 3, 1
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.1343140 0.6599720 0.09547148 0.3514792
## [2,] 0.6603176 0.7309788 0.58810805 0.1736083
## [3,] 0.4906256 0.7822839 0.39146361 0.7067947
## 
## , , 1, 4, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1556616 0.7648665 0.1106455 0.4073426
## [2,] 0.7652671 0.8471590 0.6815807 0.2012012
## [3,] 0.5686046 0.9066184 0.4536820 0.8191311
## 
## , , 2, 4, 1
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.07185481 0.3530694 0.05107498 0.18803304
## [2,] 0.35325426 0.3910563 0.31462385 0.09287631
## [3,] 0.26247308 0.4185033 0.20942374 0.37811837
## 
## , , 3, 4, 1
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.1176455 0.5780686 0.08362334 0.3078602
## [2,] 0.5783713 0.6402634 0.51512305 0.1520633
## [3,] 0.4297383 0.6852014 0.34288245 0.6190805
## 
## , , 1, 1, 2
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.02808706 0.1380100 0.01996451 0.07349955
## [2,] 0.13808226 0.1528586 0.12298216 0.03630408
## [3,] 0.10259714 0.1635872 0.08186088 0.14780130
## 
## , , 2, 1, 2
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.02923317 0.1436416 0.02077917 0.07649874
## [2,] 0.14371679 0.1590961 0.12800053 0.03778549
## [3,] 0.10678368 0.1702625 0.08520126 0.15383243
## 
## , , 3, 1, 2
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.0529668 0.2602603 0.03764923 0.13860601
## [2,] 0.2603966 0.2882619 0.23192070 0.06846251
## [3,] 0.1934785 0.3084941 0.15437387 0.27872483
## 
## , , 1, 2, 2
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.05411009 0.2658780 0.03846189 0.14159783
## [2,] 0.26601726 0.2944840 0.23692673 0.06994028
## [3,] 0.19765471 0.3151529 0.15770604 0.28474112
## 
## , , 2, 2, 2
## 
##            [,1]      [,2]       [,3]      [,4]
## [1,] 0.08287002 0.4071943 0.05890468 0.2168582
## [2,] 0.40740750 0.4510046 0.36285513 0.1071141
## [3,] 0.30270973 0.4826591 0.24152803 0.4360832
## 
## , , 3, 2, 2
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.0466280 0.2291137 0.03314356 0.12201835
## [2,] 0.2292337 0.2537642 0.20416562 0.06026927
## [3,] 0.1703240 0.2715751 0.13589920 0.24536847
## 
## , , 1, 3, 2
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.03050656 0.1498986 0.02168431 0.07983101
## [2,] 0.14997706 0.1660263 0.13357620 0.03943142
## [3,] 0.11143515 0.1776791 0.08891261 0.16053333
## 
## , , 2, 3, 2
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.02047166 0.1005905 0.01455142 0.05357119
## [2,] 0.10064322 0.1114131 0.08963730 0.02646075
## [3,] 0.07477939 0.1192329 0.05966547 0.10772709
## 
## , , 3, 3, 2
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.1330637 0.6538284 0.09458275 0.3482074
## [2,] 0.6541708 0.7241743 0.58263345 0.1719922
## [3,] 0.4860585 0.7750018 0.38781954 0.7002152
## 
## , , 1, 4, 2
## 
##            [,1]      [,2]       [,3]      [,4]
## [1,] 0.09199412 0.4520269 0.06539016 0.2407345
## [2,] 0.45226360 0.5006608 0.40280595 0.1189075
## [3,] 0.33603847 0.5358005 0.26812058 0.4840966
## 
## , , 2, 4, 2
## 
##            [,1]       [,2]        [,3]       [,4]
## [1,] 0.01354984 0.06657916 0.009631334 0.03545786
## [2,] 0.06661403 0.07374246 0.059329397 0.01751392
## [3,] 0.04949520 0.07891821 0.039491553 0.07130272
## 
## , , 3, 4, 2
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.03505307 0.1722385 0.02491601 0.09172853
## [2,] 0.17232872 0.1907698 0.15348357 0.04530803
## [3,] 0.12804276 0.2041593 0.10216360 0.18445823
## 
## , , 1, 1, 3
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.07045523 0.3461924 0.05008015 0.18437058
## [2,] 0.34637365 0.3834394 0.30849568 0.09106729
## [3,] 0.25736069 0.4103518 0.20534464 0.37075347
## 
## , , 2, 1, 3
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.0784754 0.3856006 0.05578094 0.2053581
## [2,] 0.3858026 0.4270876 0.34361281 0.1014338
## [3,] 0.2866569 0.4570636 0.22871972 0.4129576
## 
## , , 3, 1, 3
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.07377669 0.3625128 0.05244106 0.19306231
## [2,] 0.36270265 0.4015158 0.32303901 0.09536045
## [3,] 0.26949337 0.4296969 0.21502514 0.38823180
## 
## , , 1, 2, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1549323 0.7612832 0.1101271 0.4054342
## [2,] 0.7616818 0.8431901 0.6783875 0.2002586
## [3,] 0.5659407 0.9023709 0.4515565 0.8152934
## 
## , , 2, 2, 3
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.03389146 0.1665308 0.02409032 0.08868876
## [2,] 0.16661796 0.1844479 0.14839732 0.04380658
## [3,] 0.12379958 0.1973937 0.09877803 0.17834552
## 
## , , 3, 2, 3
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.05678802 0.2790365 0.04036538 0.14860557
## [2,] 0.27918258 0.3090582 0.24865234 0.07340166
## [3,] 0.20743674 0.3307500 0.16551099 0.29883309
## 
## , , 1, 3, 3
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.1173941 0.5768336 0.08344469 0.3072025
## [2,] 0.5771357 0.6388955 0.51402255 0.1517384
## [3,] 0.4288203 0.6837376 0.34214992 0.6177579
## 
## , , 2, 3, 3
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.07218378 0.3546858 0.05130881 0.18889392
## [2,] 0.35487157 0.3928467 0.31606430 0.09330153
## [3,] 0.26367476 0.4204194 0.21038255 0.37984952
## 
## , , 3, 3, 3
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.1334068 0.6555142 0.09482661 0.3491051
## [2,] 0.6558575 0.7260414 0.58413565 0.1724356
## [3,] 0.4873117 0.7769999 0.38881945 0.7020206
## 
## , , 1, 4, 3
## 
##            [,1]      [,2]       [,3]      [,4]
## [1,] 0.08167134 0.4013044 0.05805265 0.2137214
## [2,] 0.40151453 0.4444810 0.35760659 0.1055647
## [3,] 0.29833117 0.4756777 0.23803443 0.4297755
## 
## , , 2, 4, 3
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.03626273 0.1781823 0.02577584 0.09489401
## [2,] 0.17827565 0.1973531 0.15878017 0.04687158
## [3,] 0.13246141 0.2112047 0.10568918 0.19082374
## 
## , , 3, 4, 3
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.1215732 0.5973680 0.08641519 0.3181384
## [2,] 0.5976808 0.6616392 0.53232096 0.1571400
## [3,] 0.4440856 0.7080776 0.35432993 0.6397491
## 
## , , 1, 1, 4
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01918036 0.09424558 0.01363356 0.05019207
## [2,] 0.09429493 0.10438552 0.08398323 0.02479168
## [3,] 0.07006251 0.11171201 0.05590194 0.10093196
## 
## , , 2, 1, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1037093 0.5095912 0.0737174 0.2713914
## [2,] 0.5098580 0.5644184 0.4541021 0.1340500
## [3,] 0.3788319 0.6040331 0.3022649 0.5457448
## 
## , , 3, 1, 4
## 
##             [,1]       [,2]        [,3]       [,4]
## [1,] 0.009499421 0.04667683 0.006752265 0.02485853
## [2,] 0.046701274 0.05169882 0.041594218 0.01227853
## [3,] 0.034699729 0.05532740 0.027686448 0.04998838
## 
## , , 1, 2, 4
## 
##            [,1]      [,2]       [,3]      [,4]
## [1,] 0.09063675 0.4453573 0.06442533 0.2371825
## [2,] 0.44559047 0.4932735 0.39686257 0.1171530
## [3,] 0.33108024 0.5278948 0.26416447 0.4769538
## 
## , , 2, 2, 4
## 
##            [,1]      [,2]       [,3]      [,4]
## [1,] 0.09804192 0.4817437 0.06968899 0.2565607
## [2,] 0.48199597 0.5335748 0.42928692 0.1267246
## [3,] 0.35813006 0.5710247 0.28574716 0.5159217
## 
## , , 3, 2, 4
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.0897036 0.4407721 0.06376204 0.2347406
## [2,] 0.4410029 0.4881951 0.39277669 0.1159469
## [3,] 0.3276716 0.5224599 0.26144478 0.4720433
## 
## , , 1, 3, 4
## 
##             [,1]       [,2]        [,3]       [,4]
## [1,] 0.008714118 0.04281813 0.006194065 0.02280351
## [2,] 0.042840547 0.04742495 0.038155684 0.01126348
## [3,] 0.031831153 0.05075356 0.025397649 0.04585592
## 
## , , 2, 3, 4
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.08301382 0.4079009 0.0590069 0.2172345
## [2,] 0.40811448 0.4517872 0.3634848 0.1073000
## [3,] 0.30323503 0.4834967 0.2419472 0.4368400
## 
## , , 3, 3, 4
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.0885099 0.4349067 0.06291355 0.2316169
## [2,] 0.4351344 0.4816986 0.38754995 0.1144039
## [3,] 0.3233112 0.5155075 0.25796570 0.4657618
## 
## , , 1, 4, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1696975 0.8338341 0.1206224 0.4440724
## [2,] 0.8342708 0.9235469 0.7430384 0.2193434
## [3,] 0.6198754 0.9883677 0.4945902 0.8929917
## 
## , , 2, 4, 4
## 
##            [,1]       [,2]        [,3]       [,4]
## [1,] 0.01209713 0.05944107 0.008598738 0.03165634
## [2,] 0.05947219 0.06583637 0.052968565 0.01563621
## [3,] 0.04418871 0.07045722 0.035257578 0.06365820
## 
## , , 3, 4, 4
## 
##            [,1]      [,2]       [,3]      [,4]
## [1,] 0.08171553 0.4015215 0.05808406 0.2138371
## [2,] 0.40173178 0.4447215 0.35780008 0.1056218
## [3,] 0.29849259 0.4759351 0.23816323 0.4300080
## 
## , , 1, 1, 5
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.06529211 0.3208226 0.04641016 0.17085947
## [2,] 0.32099057 0.3553401 0.28588839 0.08439366
## [3,] 0.23850069 0.3802803 0.19029650 0.34358378
## 
## , , 2, 1, 5
## 
##            [,1]      [,2]       [,3]      [,4]
## [1,] 0.08841591 0.4344449 0.06284674 0.2313709
## [2,] 0.43467235 0.4811870 0.38713840 0.1142825
## [3,] 0.32296791 0.5149600 0.25769176 0.4652671
## 
## , , 3, 1, 5
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.05213654 0.2561807 0.03705907 0.13643335
## [2,] 0.25631486 0.2837434 0.22828534 0.06738936
## [3,] 0.19044569 0.3036584 0.15195406 0.27435581
## 
## , , 1, 2, 5
## 
##            [,1]      [,2]       [,3]      [,4]
## [1,] 0.07018903 0.3448843 0.04989093 0.1836740
## [2,] 0.34506491 0.3819906 0.30733006 0.0907232
## [3,] 0.25638828 0.4088013 0.20456876 0.3693526
## 
## , , 2, 2, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1334885 0.6559158 0.0948847 0.3493190
## [2,] 0.6562592 0.7264861 0.5844935 0.1725413
## [3,] 0.4876102 0.7774759 0.3890576 0.7024506
## 
## , , 3, 2, 5
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.06009384 0.2952801 0.04271518 0.15725638
## [2,] 0.29543471 0.3270495 0.26312721 0.07767461
## [3,] 0.21951231 0.3500040 0.17514592 0.31622914
## 
## , , 1, 3, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1507915 0.7409367 0.1071838 0.3945983
## [2,] 0.7413247 0.8206546 0.6602566 0.1949064
## [3,] 0.5508151 0.8782537 0.4394880 0.7935035
## 
## , , 2, 3, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1640027 0.8058520 0.1165745 0.4291701
## [2,] 0.8062740 0.8925541 0.7181033 0.2119826
## [3,] 0.5990734 0.9551997 0.4779926 0.8630243
## 
## , , 3, 3, 5
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.02071430 0.1017828 0.01472389 0.05420617
## [2,] 0.10183614 0.1127337 0.09069976 0.02677438
## [3,] 0.07566574 0.1206461 0.06037268 0.10900396
## 
## , , 1, 4, 5
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.07718737 0.3792717 0.0548654 0.20198754
## [2,] 0.37947031 0.4200778 0.3379730 0.09976894
## [3,] 0.28195199 0.4495617 0.2249657 0.40617966
## 
## , , 2, 4, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1125785 0.5531713 0.0800217 0.2946007
## [2,] 0.5534610 0.6126874 0.4929368 0.1455139
## [3,] 0.4112296 0.6556899 0.3281146 0.5924168
## 
## , , 3, 4, 5
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.0655718 0.3221969 0.04660897 0.17159138
## [2,] 0.3223656 0.3568623 0.28711306 0.08475518
## [3,] 0.2395224 0.3819093 0.19111167 0.34505560
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.10981597 0.53959725 0.07805808 0.28737161
## [2,] 0.53987981 0.59765285 0.48084081 0.14194321
## [3,] 0.40113859 0.63960021 0.32006309 0.57787972
## 
## ,,2,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01867339 0.09175449 0.01327319 0.04886540
## [2,] 0.09180254 0.10162641 0.08176340 0.02413639
## [3,] 0.06821063 0.10875925 0.05442434 0.09826414
## 
## ,,3,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02802593 0.13770963 0.01992106 0.07333959
## [2,] 0.13778174 0.15252590 0.12271451 0.03622507
## [3,] 0.10237385 0.16323121 0.08168272 0.14747963
## 
## ...
## 
## ,,1,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.07718737 0.37927171 0.05486540 0.20198754
## [2,] 0.37947031 0.42007778 0.33797303 0.09976894
## [3,] 0.28195199 0.44956171 0.22496571 0.40617966
## 
## ,,2,4,5
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1125785 0.5531713 0.0800217 0.2946007
## [2,] 0.5534610 0.6126874 0.4929368 0.1455139
## [3,] 0.4112296 0.6556899 0.3281146 0.5924168
## 
## ,,3,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.06557180 0.32219689 0.04660897 0.17159138
## [2,] 0.32236561 0.35686225 0.28711306 0.08475518
## [3,] 0.23952236 0.38190928 0.19111167 0.34505560

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

3.3.1 Row-wise / Column-wise Summation

einsum::einsum('ij->i', arrC)
## [1] 1.579192 2.739220 3.016773
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
##      [1]      [2]      [3] 
## 1.579192 2.739220 3.016773
einsum::einsum('ij->j', arrC)
## [1] 1.635198 2.764948 1.367749 1.567291
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 1.635198 2.764948 1.367749 1.567291

3.3.2 Mode-wise Vectorization

einsum::einsum('ijk->i', arrE)
## [1] 9.214263 7.745161 8.735270
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
##      [1]      [2]      [3] 
## 9.214263 7.745161 8.735270
einsum::einsum('ijk->j', arrE)
## [1] 4.843887 6.407946 7.162408 7.280454
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 4.843887 6.407946 7.162408 7.280454
einsum::einsum('ijk->k', arrE)
## [1] 4.836544 3.619611 6.032223 5.000578 6.205739
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4]      [5] 
## 4.836544 3.619611 6.032223 5.000578 6.205739

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.713622 2.323052 1.805644 3.371946
## [2,] 1.863878 2.067469 2.372235 1.441579
## [3,] 1.266387 2.017425 2.984530 2.466929
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]     [,4]
## [1,] 1.713622 2.323052 1.805644 3.371946
## [2,] 1.863878 2.067469 2.372235 1.441579
## [3,] 1.266387 2.017425 2.984530 2.466929
einsum::einsum('ijk->jk', arrE)
##           [,1]      [,2]     [,3]      [,4]     [,5]
## [1,] 0.9159148 0.6453908 1.303265 0.7747301 1.204586
## [2,] 0.7235464 1.0744598 1.437300 1.6290706 1.543569
## [3,] 1.1772232 1.0769984 1.890080 1.0547373 1.963369
## [4,] 2.0198592 0.8227624 1.401577 1.5420402 1.494215
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.9159148 0.6453908 1.3032652 0.7747301 1.2045857
## [2,] 0.7235464 1.0744598 1.4373003 1.6290706 1.5435687
## [3,] 1.1772232 1.0769984 1.8900802 1.0547373 1.9633689
## [4,] 2.0198592 0.8227624 1.4015770 1.5420402 1.4942153
einsum::einsum('ijk->jk', arrE)
##           [,1]      [,2]     [,3]      [,4]     [,5]
## [1,] 0.9159148 0.6453908 1.303265 0.7747301 1.204586
## [2,] 0.7235464 1.0744598 1.437300 1.6290706 1.543569
## [3,] 1.1772232 1.0769984 1.890080 1.0547373 1.963369
## [4,] 2.0198592 0.8227624 1.401577 1.5420402 1.494215
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.9159148 0.6453908 1.3032652 0.7747301 1.2045857
## [2,] 0.7235464 1.0744598 1.4373003 1.6290706 1.5435687
## [3,] 1.1772232 1.0769984 1.8900802 1.0547373 1.9633689
## [4,] 2.0198592 0.8227624 1.4015770 1.5420402 1.4942153

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

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.3780281 0.5924883 0.3340864
## [2,] 0.6587618 0.7056558 0.7423077
## [3,] 0.5698973 0.5796130 0.7072691
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 0.3780281 0.5924883 0.3340864
## [2,] 0.6587618 0.7056558 0.7423077
## [3,] 0.5698973 0.5796130 0.7072691
einsum::einsum('ijk->jki', arrD)
## , , 1
## 
##            [,1]      [,2]       [,3]
## [1,] 0.16432989 0.9839553 0.09591816
## [2,] 0.04142506 0.9049522 0.98766482
## [3,] 0.98114267 0.6965892 0.41655311
## 
## , , 2
## 
##           [,1]      [,2]       [,3]
## [1,] 0.7362322 0.7543466 0.05567971
## [2,] 0.9876986 0.3101542 0.59095474
## [3,] 0.1627663 0.1396324 0.66372065
## 
## , , 3
## 
##           [,1]      [,2]       [,3]
## [1,] 0.3582187 0.9924773 0.65948144
## [2,] 0.7542827 0.4013092 0.41661109
## [3,] 0.9048116 0.5855557 0.06343364
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
##            [,1]       [,2]       [,3]
## [1,] 0.16432989 0.98395534 0.09591816
## [2,] 0.04142506 0.90495219 0.98766482
## [3,] 0.98114267 0.69658920 0.41655311
## 
## ,,2
##            [,1]       [,2]       [,3]
## [1,] 0.73623217 0.75434664 0.05567971
## [2,] 0.98769857 0.31015423 0.59095474
## [3,] 0.16276626 0.13963242 0.66372065
## 
## ,,3
##            [,1]       [,2]       [,3]
## [1,] 0.35821875 0.99247731 0.65948144
## [2,] 0.75428267 0.40130923 0.41661109
## [3,] 0.90481165 0.58555567 0.06343364

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

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.4518175 0.1523539 0.5672789 0.3840141 0.5067792
## [2,] 0.3129264 0.4098954 0.9717873 0.8860532 0.9025925
## [3,] 0.7656744 0.6525612 1.2598462 0.5068676 1.7144416
## [4,] 1.4805497 0.3381770 0.7795943 1.2198400 0.7852871
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.4518175 0.1523539 0.5672789 0.3840141 0.5067792
## [2,] 0.3129264 0.4098954 0.9717873 0.8860532 0.9025925
## [3,] 0.7656744 0.6525612 1.2598462 0.5068676 1.7144416
## [4,] 1.4805497 0.3381770 0.7795943 1.2198400 0.7852871

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.948960 1.114109 1.404982
## [2,] 1.114109 2.179325 2.021293
## [3,] 1.404982 2.021293 2.436896
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]
## [1,] 0.948960 1.114109 1.404982
## [2,] 1.114109 2.179325 2.021293
## [3,] 1.404982 2.021293 2.436896

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.02920138 0.70577542 0.3896380
## [2,] 0.70503685 0.86490887 0.9905800
## [3,] 0.01475394 0.55985429 0.2480525
## [4,] 0.19996780 0.04878672 0.8086252
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.02920138 0.70577542 0.38963795
## [2,] 0.70503685 0.86490887 0.99058001
## [3,] 0.01475394 0.55985429 0.24805247
## [4,] 0.19996780 0.04878672 0.80862521
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
## 
##              [,1]       [,2]      [,3]       [,4]      [,5]
## [1,] 4.129787e-01 0.02701527 0.1699899 0.01259825 0.1459883
## [2,] 2.515822e-02 0.10026586 0.8220167 0.28132302 0.1687078
## [3,] 4.525079e-05 0.03187008 0.4719428 0.00260042 0.7786646
## [4,] 8.297734e-01 0.28981226 0.2284210 0.98616035 0.2040277
## 
## , , 2
## 
##              [,1]       [,2]       [,3]        [,4]      [,5]
## [1,] 0.0119410587 0.02926500 0.21089373 0.368325630 0.2677056
## [2,] 0.0008580819 0.23517519 0.03933482 0.329170033 0.6102170
## [3,] 0.1478414897 0.01435167 0.17843329 0.235992105 0.9210830
## [4,] 0.1768105916 0.00628731 0.04503161 0.005011427 0.4340177
## 
## , , 3
## 
##           [,1]       [,2]      [,3]        [,4]       [,5]
## [1,] 0.0268978 0.09607359 0.1863953 0.003090231 0.09308529
## [2,] 0.2869101 0.07445438 0.1104359 0.275560141 0.12366776
## [3,] 0.6177877 0.60633943 0.6094701 0.268275101 0.01469391
## [4,] 0.4739657 0.04207739 0.5061417 0.228668244 0.14724172
DelayedTensor::einsum('ijk,ijk->jki', darrE, darrE)
## <4 x 5 x 3> HDF5Array object of type "double":
## ,,1
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 4.129787e-01 2.701527e-02 1.699899e-01 1.259825e-02 1.459883e-01
## [2,] 2.515822e-02 1.002659e-01 8.220167e-01 2.813230e-01 1.687078e-01
## [3,] 4.525079e-05 3.187008e-02 4.719428e-01 2.600420e-03 7.786646e-01
## [4,] 8.297734e-01 2.898123e-01 2.284210e-01 9.861604e-01 2.040277e-01
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.0119410587 0.0292649986 0.2108937314 0.3683256301 0.2677056235
## [2,] 0.0008580819 0.2351751945 0.0393348156 0.3291700328 0.6102170215
## [3,] 0.1478414897 0.0143516737 0.1784332893 0.2359921051 0.9210830284
## [4,] 0.1768105916 0.0062873098 0.0450316146 0.0050114269 0.4340177475
## 
## ,,3
##             [,1]        [,2]        [,3]        [,4]        [,5]
## [1,] 0.026897804 0.096073593 0.186395289 0.003090231 0.093085286
## [2,] 0.286910117 0.074454375 0.110435857 0.275560141 0.123667761
## [3,] 0.617787671 0.606339433 0.609470106 0.268275101 0.014693908
## [4,] 0.473965737 0.042077391 0.506141698 0.228668244 0.147241717

3.7 Summation + Permutation

Some examples of combining Summation and Permutation are shown below.

einsum::einsum('ijk->ki', arrE)
##          [,1]      [,2]     [,3]
## [1,] 1.718893 0.9435583 2.174092
## [2,] 1.197875 0.8551098 1.566626
## [3,] 2.483865 1.2921819 2.256176
## [4,] 1.686691 1.7372134 1.576674
## [5,] 2.126939 2.9170976 1.161702
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 1.7188935 0.9435583 2.1740918
## [2,] 1.1978751 0.8551098 1.5666264
## [3,] 2.4838647 1.2921819 2.2561761
## [4,] 1.6866912 1.7372134 1.5766736
## [5,] 2.1269386 2.9170976 1.1617024

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,] 6.578111e-03 0.0004303114 0.002707676 2.006706e-04 0.002325368
## [2,] 9.675245e-03 0.0385598320 0.316127785 1.081900e-01 0.064880937
## [3,] 3.641703e-07 0.0002564847 0.003798111 2.092771e-05 0.006266553
## [4,] 9.050859e-02 0.0316116416 0.024915311 1.075667e-01 0.022254579
## 
## , , 2
## 
##              [,1]         [,2]        [,3]         [,4]       [,5]
## [1,] 0.0054397676 0.0133317150 0.096072963 0.1677913061 0.12195371
## [2,] 0.0004790381 0.1312903627 0.021959298 0.1837645042 0.34066354
## [3,] 0.0534247288 0.0051861915 0.064479532 0.0852792693 0.33284710
## [4,] 0.0055677650 0.0001979874 0.001418045 0.0001578098 0.01366722
## 
## , , 3
## 
##             [,1]       [,2]       [,3]        [,4]        [,5]
## [1,] 0.009125427 0.03259421 0.06323701 0.001048401 0.031580384
## [2,] 0.247463150 0.06421772 0.09525215 0.237673670 0.106664812
## [3,] 0.133431360 0.13095874 0.13163491 0.057942742 0.003173628
## [4,] 0.333710090 0.02962588 0.35636456 0.161000879 0.103670039
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,] 6.578111e-03 4.303114e-04 2.707676e-03 2.006706e-04 2.325368e-03
## [2,] 9.675245e-03 3.855983e-02 3.161278e-01 1.081900e-01 6.488094e-02
## [3,] 3.641703e-07 2.564847e-04 3.798111e-03 2.092771e-05 6.266553e-03
## [4,] 9.050859e-02 3.161164e-02 2.491531e-02 1.075667e-01 2.225458e-02
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.0054397676 0.0133317150 0.0960729631 0.1677913061 0.1219537077
## [2,] 0.0004790381 0.1312903627 0.0219592981 0.1837645042 0.3406635392
## [3,] 0.0534247288 0.0051861915 0.0644795322 0.0852792693 0.3328470994
## [4,] 0.0055677650 0.0001979874 0.0014180454 0.0001578098 0.0136672175
## 
## ,,3
##             [,1]        [,2]        [,3]        [,4]        [,5]
## [1,] 0.009125427 0.032594205 0.063237006 0.001048401 0.031580384
## [2,] 0.247463150 0.064217722 0.095252149 0.237673670 0.106664812
## [3,] 0.133431360 0.130958741 0.131634911 0.057942742 0.003173628
## [4,] 0.333710090 0.029625875 0.356364561 0.161000879 0.103670039

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.5.0 RC (2025-04-03 r88103 ucrt)
## Platform: x86_64-w64-mingw32/x64
## Running under: Windows Server 2022 x64 (build 20348)
## 
## Matrix products: default
##   LAPACK version 3.12.1
## 
## locale:
## [1] LC_COLLATE=C                          
## [2] LC_CTYPE=English_United States.utf8   
## [3] LC_MONETARY=English_United States.utf8
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.utf8    
## 
## time zone: America/New_York
## tzcode source: internal
## 
## attached base packages:
## [1] stats4    stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
##  [1] einsum_0.1.2              DelayedRandomArray_1.17.0
##  [3] HDF5Array_1.37.0          h5mread_1.1.0            
##  [5] rhdf5_2.53.0              DelayedArray_0.35.0      
##  [7] SparseArray_1.9.0         S4Arrays_1.9.0           
##  [9] abind_1.4-8               IRanges_2.43.0           
## [11] S4Vectors_0.47.0          MatrixGenerics_1.21.0    
## [13] matrixStats_1.5.0         BiocGenerics_0.55.0      
## [15] generics_0.1.3            Matrix_1.7-3             
## [17] DelayedTensor_1.15.0      BiocStyle_2.37.0         
## 
## loaded via a namespace (and not attached):
##  [1] dqrng_0.4.1         sass_0.4.10         lattice_0.22-7     
##  [4] digest_0.6.37       evaluate_1.0.3      grid_4.5.0         
##  [7] bookdown_0.43       fastmap_1.2.0       jsonlite_2.0.0     
## [10] BiocManager_1.30.25 codetools_0.2-20    jquerylib_0.1.4    
## [13] cli_3.6.4           rlang_1.1.6         crayon_1.5.3       
## [16] XVector_0.49.0      cachem_1.1.0        yaml_2.3.10        
## [19] tools_4.5.0         beachmat_2.25.0     parallel_4.5.0     
## [22] BiocParallel_1.43.0 Rhdf5lib_1.31.0     rsvd_1.0.5         
## [25] R6_2.6.1            lifecycle_1.0.4     BiocSingular_1.25.0
## [28] irlba_2.3.5.1       ScaledMatrix_1.17.0 rTensor_1.4.8      
## [31] bslib_0.9.0         Rcpp_1.0.14         xfun_0.52          
## [34] knitr_1.50          rhdf5filters_1.21.0 htmltools_0.5.8.1  
## [37] rmarkdown_2.29      compiler_4.5.0