Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2025-10-21 16:26:44.753785
Compiled: Fri Oct 31 17:28:49 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.7703020 0.3160546 0.7985787
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.7703020 0.3160546 0.7985787
einsum::einsum('iii->i', arrD)
## [1] 0.9834322 0.1739180 0.3851428
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.9834322 0.1739180 0.3851428

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.4411775 0.2163524 0.3010679
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.4411775 0.2163524 0.3010679
einsum::einsum('ij,ij->ij', arrC, arrC)
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01943749 0.95831842 0.06252638 0.20057325
## [2,] 0.55331604 0.01285182 0.10355823 0.05166612
## [3,] 0.31242915 0.68013894 0.13276524 0.07531696
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01943749 0.95831842 0.06252638 0.20057325
## [2,] 0.55331604 0.01285182 0.10355823 0.05166612
## [3,] 0.31242915 0.68013894 0.13276524 0.07531696
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
## 
##            [,1]      [,2]      [,3]        [,4]
## [1,] 0.26534292 0.0229562 0.9269555 0.002455763
## [2,] 0.09082541 0.2296334 0.7473634 0.027955701
## [3,] 0.15621651 0.3090574 0.4950587 0.304535325
## 
## , , 2
## 
##            [,1]        [,2]      [,3]      [,4]
## [1,] 0.88373525 0.007243278 0.9734344 0.8157754
## [2,] 0.01883810 0.084681264 0.6845825 0.3804211
## [3,] 0.01324702 0.001392369 0.4666188 0.7269545
## 
## , , 3
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.6587241 0.01894491 0.2182431 0.1245817
## [2,] 0.5196504 0.03547752 0.1877519 0.4017673
## [3,] 0.2055574 0.57466227 0.7466105 0.7937329
## 
## , , 4
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.64755576 0.8121234 0.1531949 0.4548541
## [2,] 0.02530239 0.3528302 0.2890596 0.1839922
## [3,] 0.43271463 0.0399224 0.1518375 0.7748118
## 
## , , 5
## 
##           [,1]       [,2]        [,3]       [,4]
## [1,] 0.7428561 0.53675001 0.003090297 0.05117823
## [2,] 0.5469083 0.94896490 0.010100316 0.23744875
## [3,] 0.4422699 0.01031044 0.099795031 0.28369711
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.265342915 0.022956197 0.926955542 0.002455763
## [2,] 0.090825410 0.229633394 0.747363370 0.027955701
## [3,] 0.156216512 0.309057440 0.495058711 0.304535325
## 
## ,,2
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.883735253 0.007243278 0.973434440 0.815775353
## [2,] 0.018838100 0.084681264 0.684582537 0.380421101
## [3,] 0.013247016 0.001392369 0.466618822 0.726954520
## 
## ,,3
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.65872411 0.01894491 0.21824312 0.12458172
## [2,] 0.51965044 0.03547752 0.18775187 0.40176726
## [3,] 0.20555739 0.57466227 0.74661049 0.79373293
## 
## ,,4
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.64755576 0.81212343 0.15319489 0.45485408
## [2,] 0.02530239 0.35283016 0.28905960 0.18399225
## [3,] 0.43271463 0.03992240 0.15183747 0.77481180
## 
## ,,5
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.742856051 0.536750006 0.003090297 0.051178225
## [2,] 0.546908261 0.948964898 0.010100316 0.237448749
## [3,] 0.442269899 0.010310437 0.099795031 0.283697113

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.4411775 0.3089495 0.3644508
## [2,] 0.3089495 0.2163524 0.2552191
## [3,] 0.3644508 0.2552191 0.3010679
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 0.4411775 0.3089495 0.3644508
## [2,] 0.3089495 0.2163524 0.2552191
## [3,] 0.3644508 0.2552191 0.3010679
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.07181643 0.5042648 0.1288058 0.2306961
## [2,] 0.38316901 0.0583964 0.1657662 0.1170865
## [3,] 0.28792509 0.4248177 0.1876921 0.1413677
## 
## , , 2, 1, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.04201687 0.29502485 0.07535904 0.13497091
## [2,] 0.22417662 0.03416536 0.09698308 0.06850253
## [3,] 0.16845327 0.24854355 0.10981101 0.08270848
## 
## , , 3, 1, 1
## 
##            [,1]      [,2]       [,3]      [,4]
## [1,] 0.05510405 0.3869175 0.09883144 0.1770109
## [2,] 0.29400187 0.0448070 0.12719082 0.0898393
## [3,] 0.22092214 0.3259585 0.14401431 0.1084701
## 
## , , 1, 2, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02112370 0.14832177 0.03788625 0.06785572
## [2,] 0.11270329 0.01717641 0.04875760 0.03443919
## [3,] 0.08468875 0.12495361 0.05520675 0.04158114
## 
## , , 2, 2, 1
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.0668094 0.46910757 0.1198255 0.2146120
## [2,] 0.3564545 0.05432502 0.1542090 0.1089232
## [3,] 0.2678510 0.39519946 0.1746062 0.1315116
## 
## , , 3, 2, 1
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.07750677 0.54422003 0.1390117 0.2489752
## [2,] 0.41352925 0.06302342 0.1789006 0.1263638
## [3,] 0.31073872 0.45847792 0.2025638 0.1525689
## 
## , , 1, 3, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1342300 0.9425065 0.2407471 0.4311873
## [2,] 0.7161699 0.1091470 0.3098288 0.2188428
## [3,] 0.5381523 0.7940142 0.3508097 0.2642262
## 
## , , 2, 3, 1
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.1205274 0.84629314 0.2161711 0.3871706
## [2,] 0.6430615 0.09800501 0.2782007 0.1965028
## [3,] 0.4832164 0.71295928 0.3149982 0.2372533
## 
## , , 3, 3, 1
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.09809534 0.68878435 0.1759381 0.3151119
## [2,] 0.52337742 0.07976469 0.2264231 0.1599305
## [3,] 0.39328205 0.58026607 0.2563720 0.1930966
## 
## , , 1, 4, 1
## 
##            [,1]        [,2]       [,3]       [,4]
## [1,] 0.00690897 0.048511888 0.01239153 0.02219370
## [2,] 0.03686208 0.005617921 0.01594724 0.01126409
## [3,] 0.02769931 0.040868818 0.01805658 0.01360002
## 
## , , 2, 4, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02331070 0.16367793 0.04180872 0.07488101
## [2,] 0.12437177 0.01895473 0.05380560 0.03800477
## [3,] 0.09345681 0.13789040 0.06092245 0.04588615
## 
## , , 3, 4, 1
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.07693764 0.54022385 0.1379909 0.2471470
## [2,] 0.41049273 0.06256064 0.1775870 0.1254359
## [3,] 0.30845698 0.45511134 0.2010764 0.1514486
## 
## , , 1, 1, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1310633 0.9202716 0.2350676 0.4210150
## [2,] 0.6992745 0.1065721 0.3025195 0.2136801
## [3,] 0.5254566 0.7752824 0.3425337 0.2579927
## 
## , , 2, 1, 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01913545 0.13436108 0.03432023 0.06146885
## [2,] 0.10209517 0.01555969 0.04416832 0.03119762
## [3,] 0.07671748 0.11319243 0.05001045 0.03766734
## 
## , , 3, 1, 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01604645 0.11267147 0.02877999 0.05154607
## [2,] 0.08561417 0.01304792 0.03703833 0.02616146
## [3,] 0.06433315 0.09492003 0.04193737 0.03158678
## 
## , , 1, 2, 2
## 
##            [,1]        [,2]       [,3]       [,4]
## [1,] 0.01186554 0.083314866 0.02128135 0.03811572
## [2,] 0.06330736 0.009648281 0.02738797 0.01934508
## [3,] 0.04757112 0.070188571 0.03101057 0.02335683
## 
## , , 2, 2, 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.04057081 0.28487123 0.07276547 0.13032573
## [2,] 0.21646132 0.03298952 0.09364530 0.06614493
## [3,] 0.16265576 0.23998964 0.10603173 0.07986197
## 
## , , 3, 2, 2
## 
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.005202322 0.036528525 0.009330585 0.016711434
## [2,] 0.027756446 0.004230187 0.012007968 0.008481646
## [3,] 0.020857055 0.030773440 0.013596259 0.010240557
## 
## , , 1, 3, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1375541 0.9658469 0.2467090 0.4418653
## [2,] 0.7339052 0.1118499 0.3175014 0.2242623
## [3,] 0.5514792 0.8136773 0.3594972 0.2707695
## 
## , , 2, 3, 2
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.1153541 0.80996794 0.2068924 0.3705522
## [2,] 0.6154596 0.09379836 0.2662596 0.1880684
## [3,] 0.4624754 0.68235712 0.3014776 0.2270698
## 
## , , 3, 3, 2
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.0952360 0.66870727 0.1708098 0.3059269
## [2,] 0.5081217 0.07743967 0.2198232 0.1552687
## [3,] 0.3818184 0.56335214 0.2488991 0.1874682
## 
## , , 1, 4, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1259231 0.8841790 0.2258484 0.4045030
## [2,] 0.6718494 0.1023924 0.2906549 0.2052996
## [3,] 0.5048485 0.7448762 0.3290997 0.2478744
## 
## , , 2, 4, 2
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.08599087 0.60379181 0.1542283 0.2762287
## [2,] 0.45879527 0.06992213 0.1984836 0.1401959
## [3,] 0.34475301 0.50866414 0.2247370 0.1692695
## 
## , , 3, 4, 2
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.1188704 0.83465796 0.2131991 0.3818477
## [2,] 0.6342205 0.09665759 0.2743759 0.1938012
## [3,] 0.4765730 0.70315722 0.3106675 0.2339915
## 
## , , 1, 1, 3
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.1131545 0.79452341 0.2029474 0.3634865
## [2,] 0.6037240 0.09200981 0.2611825 0.1844823
## [3,] 0.4536569 0.66934589 0.2957290 0.2227400
## 
## , , 2, 1, 3
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.1005022 0.70568448 0.1802550 0.3228436
## [2,] 0.5362191 0.08172181 0.2319786 0.1638546
## [3,] 0.4029317 0.59450358 0.2626624 0.1978345
## 
## , , 3, 1, 3
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.06321012 0.44383492 0.1133700 0.2030500
## [2,] 0.33725095 0.05139832 0.1459012 0.1030551
## [3,] 0.25342084 0.37390853 0.1651995 0.1244265
## 
## , , 1, 2, 3
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01918962 0.13474145 0.03441739 0.06164287
## [2,] 0.10238420 0.01560374 0.04429336 0.03128594
## [3,] 0.07693467 0.11351288 0.05015203 0.03777398
## 
## , , 2, 2, 3
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.02626012 0.1843875 0.04709863 0.08435545
## [2,] 0.14010811 0.0213530 0.06061344 0.04281338
## [3,] 0.10528158 0.1553372 0.06863076 0.05169196
## 
## , , 3, 2, 3
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.1056882 0.74209800 0.1895562 0.3395024
## [2,] 0.5638882 0.08593868 0.2439488 0.1723095
## [3,] 0.4237231 0.62518012 0.2762158 0.2080428
## 
## , , 1, 3, 3
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.06513139 0.45732527 0.1168159 0.2092217
## [2,] 0.34750168 0.05296057 0.1503359 0.1061875
## [3,] 0.26112356 0.38527347 0.1702207 0.1282085
## 
## , , 2, 3, 3
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.06041047 0.42417694 0.1083487 0.19405670
## [2,] 0.32231370 0.04912182 0.1394391 0.09849066
## [3,] 0.24219653 0.35734767 0.1578826 0.11891551
## 
## , , 3, 3, 3
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.1204667 0.84586677 0.2160621 0.3869756
## [2,] 0.6427375 0.09795563 0.2780605 0.1964038
## [3,] 0.4829730 0.71260007 0.3148395 0.2371338
## 
## , , 1, 4, 3
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.0492093 0.34552707 0.08825896 0.15807517
## [2,] 0.2625511 0.04001377 0.11358460 0.08022876
## [3,] 0.1972890 0.29108912 0.12860840 0.09686648
## 
## , , 2, 4, 3
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.0883705 0.62050058 0.1584962 0.2838728
## [2,] 0.4714915 0.07185709 0.2039762 0.1440755
## [3,] 0.3542934 0.52274043 0.2309561 0.1739537
## 
## , , 3, 4, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1242102 0.8721519 0.2227762 0.3990007
## [2,] 0.6627105 0.1009996 0.2867012 0.2025070
## [3,] 0.4979812 0.7347440 0.3246231 0.2445027
## 
## , , 1, 1, 4
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.1121912 0.78775923 0.2012196 0.3603920
## [2,] 0.5985841 0.09122648 0.2589589 0.1829117
## [3,] 0.4497947 0.66364741 0.2932113 0.2208437
## 
## , , 2, 1, 4
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.02217690 0.1557169 0.03977520 0.07123891
## [2,] 0.11832252 0.0180328 0.05118858 0.03615627
## [3,] 0.08891121 0.1311836 0.05795928 0.04365431
## 
## , , 3, 1, 4
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.09171087 0.64395528 0.1644873 0.2946031
## [2,] 0.48931375 0.07457326 0.2116865 0.1495215
## [3,] 0.36768555 0.54249983 0.2396862 0.1805291
## 
## , , 1, 2, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1256409 0.8821977 0.2253423 0.4035966
## [2,] 0.6703439 0.1021629 0.2900036 0.2048396
## [3,] 0.5037172 0.7432071 0.3283622 0.2473190
## 
## , , 2, 2, 4
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.08281383 0.58148400 0.1485301 0.2660231
## [2,] 0.44184453 0.06733877 0.1911504 0.1350162
## [3,] 0.33201570 0.48987093 0.2164338 0.1630156
## 
## , , 3, 2, 4
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02785662 0.19559747 0.04996202 0.08948388
## [2,] 0.14862606 0.02265117 0.06429847 0.04541625
## [3,] 0.11168223 0.16478100 0.07280321 0.05483460
## 
## , , 1, 3, 4
## 
##            [,1]       [,2]       [,3]      [,4]
## [1,] 0.05456852 0.38315726 0.09787095 0.1752906
## [2,] 0.29114462 0.04437154 0.12595472 0.0889662
## [3,] 0.21877511 0.32279066 0.14261471 0.1074159
## 
## , , 2, 3, 4
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.07495727 0.52631848 0.1344390 0.2407854
## [2,] 0.39992664 0.06095033 0.1730159 0.1222071
## [3,] 0.30051730 0.44339677 0.1959007 0.1475503
## 
## , , 3, 3, 4
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.05432622 0.38145595 0.09743638 0.17451228
## [2,] 0.28985187 0.04417452 0.12539545 0.08857117
## [3,] 0.21780370 0.32135740 0.14198147 0.10693894
## 
## , , 1, 4, 4
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.09402776 0.6602235 0.1686428 0.3020456
## [2,] 0.50167525 0.0764572 0.2170343 0.1532989
## [3,] 0.37697437 0.5562050 0.2457414 0.1850898
## 
## , , 2, 4, 4
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.05980256 0.41990851 0.1072584 0.19210394
## [2,] 0.31907031 0.04862752 0.1380359 0.09749956
## [3,] 0.23975934 0.35375174 0.1562939 0.11771888
## 
## , , 3, 4, 4
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.1227208 0.86169393 0.2201049 0.3942163
## [2,] 0.6547639 0.09978849 0.2832634 0.2000788
## [3,] 0.4920099 0.72593366 0.3207305 0.2415708
## 
## , , 1, 1, 5
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.1201634 0.84373730 0.2155182 0.3860014
## [2,] 0.6411195 0.09770903 0.2773605 0.1959094
## [3,] 0.4817571 0.71080611 0.3140469 0.2365368
## 
## , , 2, 1, 5
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.1031044 0.72395598 0.1849221 0.3312026
## [2,] 0.5501028 0.08383775 0.2379850 0.1680971
## [3,] 0.4133643 0.60989639 0.2694632 0.2029568
## 
## , , 3, 1, 5
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.09271793 0.65102641 0.1662935 0.2978381
## [2,] 0.49468680 0.07539214 0.2140110 0.1511634
## [3,] 0.37172302 0.54845691 0.2423181 0.1825114
## 
## , , 1, 2, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1021424 0.7172011 0.1831967 0.3281123
## [2,] 0.5449701 0.0830555 0.2357645 0.1665286
## [3,] 0.4095074 0.6042057 0.2669490 0.2010631
## 
## , , 2, 2, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1358142 0.9536302 0.2435885 0.4362763
## [2,] 0.7246223 0.1104352 0.3134854 0.2214257
## [3,] 0.5445037 0.8033853 0.3549501 0.2673446
## 
## , , 3, 2, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01415659 0.09940162 0.02539044 0.04547524
## [2,] 0.07553099 0.01151121 0.03267615 0.02308030
## [3,] 0.05675633 0.08374085 0.03699821 0.02786666
## 
## , , 1, 3, 5
## 
##             [,1]        [,2]       [,3]       [,4]
## [1,] 0.007750329 0.054419558 0.01390054 0.02489640
## [2,] 0.041351067 0.006302059 0.01788926 0.01263581
## [3,] 0.031072476 0.045845732 0.02025547 0.01525620
## 
## , , 2, 3, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01401159 0.09838353 0.02513038 0.04500948
## [2,] 0.07475739 0.01139331 0.03234147 0.02284391
## [3,] 0.05617502 0.08288316 0.03661927 0.02758124
## 
## , , 3, 3, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.04404276 0.30924976 0.07899255 0.14147867
## [2,] 0.23498551 0.03581268 0.10165922 0.07180544
## [3,] 0.17657541 0.26052732 0.11510565 0.08669635
## 
## , , 1, 4, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.03154007 0.22146114 0.05656845 0.10131625
## [2,] 0.16827874 0.02564631 0.07280059 0.05142159
## [3,] 0.12644987 0.18656984 0.08242991 0.06208533
## 
## , , 2, 4, 5
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.06793678 0.47702360 0.1218475 0.2182335
## [2,] 0.36246959 0.05524173 0.1568113 0.1107613
## [3,] 0.27237090 0.40186831 0.1775526 0.1337308
## 
## , , 3, 4, 5
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.07425873 0.52141363 0.1331862 0.2385415
## [2,] 0.39619965 0.06038232 0.1714035 0.1210683
## [3,] 0.29771672 0.43926467 0.1940750 0.1461752
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.07181643 0.50426482 0.12880579 0.23069610
## [2,] 0.38316901 0.05839640 0.16576623 0.11708646
## [3,] 0.28792509 0.42481767 0.18769208 0.14136768
## 
## ,,2,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.04201687 0.29502485 0.07535904 0.13497091
## [2,] 0.22417662 0.03416536 0.09698308 0.06850253
## [3,] 0.16845327 0.24854355 0.10981101 0.08270848
## 
## ,,3,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.05510405 0.38691751 0.09883144 0.17701089
## [2,] 0.29400187 0.04480700 0.12719082 0.08983930
## [3,] 0.22092214 0.32595848 0.14401431 0.10847005
## 
## ...
## 
## ,,1,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.03154007 0.22146114 0.05656845 0.10131625
## [2,] 0.16827874 0.02564631 0.07280059 0.05142159
## [3,] 0.12644987 0.18656984 0.08242991 0.06208533
## 
## ,,2,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.06793678 0.47702360 0.12184749 0.21823352
## [2,] 0.36246959 0.05524173 0.15681126 0.11076125
## [3,] 0.27237090 0.40186831 0.17755264 0.13373076
## 
## ,,3,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.07425873 0.52141363 0.13318616 0.23854151
## [2,] 0.39619965 0.06038232 0.17140353 0.12106828
## [3,] 0.29771672 0.43926467 0.19407503 0.14617525

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

3.3.1 Row-wise / Column-wise Summation

einsum::einsum('ij->i', arrC)
## [1] 1.816263 1.406324 2.022468
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
##      [1]      [2]      [3] 
## 1.816263 1.406324 2.022468
einsum::einsum('ij->j', arrC)
## [1] 1.4422242 1.9170086 0.9362270 0.9495952
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
##       [1]       [2]       [3]       [4] 
## 1.4422242 1.9170086 0.9362270 0.9495952

3.3.2 Mode-wise Vectorization

einsum::einsum('ijk->i', arrE)
## [1] 11.011421  9.682199 10.603800
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 11.011421  9.682199 10.603800
einsum::einsum('ijk->j', arrE)
## [1] 8.278063 6.187426 8.583245 8.248685
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 8.278063 6.187426 8.583245 8.248685
einsum::einsum('ijk->k', arrE)
## [1] 5.697866 6.475562 6.712195 6.618879 5.792918
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4]      [5] 
## 5.697866 6.475562 6.712195 6.618879 5.792918

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,] 3.933404 2.008072 2.863570 2.206375
## [2,] 2.058092 2.526699 2.763343 2.334064
## [3,] 2.286567 1.652655 2.956332 3.708247
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]     [,4]
## [1,] 3.933404 2.008072 2.863570 2.206375
## [2,] 2.058092 2.526699 2.763343 2.334064
## [3,] 2.286567 1.652655 2.956332 3.708247
einsum::einsum('ijk->jk', arrE)
##           [,1]      [,2]     [,3]     [,4]      [,5]
## [1,] 1.2117294 1.1924196 1.985870 1.621586 2.2664572
## [2,] 1.1866432 0.4134224 1.084060 1.694980 1.8083209
## [3,] 2.5308912 2.4971179 1.764535 1.318707 0.4719943
## [4,] 0.7686024 2.3726020 1.877729 1.983606 1.2461454
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.2117294 1.1924196 1.9858704 1.6215862 2.2664572
## [2,] 1.1866432 0.4134224 1.0840601 1.6949799 1.8083209
## [3,] 2.5308912 2.4971179 1.7645347 1.3187071 0.4719943
## [4,] 0.7686024 2.3726020 1.8777294 1.9836058 1.2461454
einsum::einsum('ijk->jk', arrE)
##           [,1]      [,2]     [,3]     [,4]      [,5]
## [1,] 1.2117294 1.1924196 1.985870 1.621586 2.2664572
## [2,] 1.1866432 0.4134224 1.084060 1.694980 1.8083209
## [3,] 2.5308912 2.4971179 1.764535 1.318707 0.4719943
## [4,] 0.7686024 2.3726020 1.877729 1.983606 1.2461454
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.2117294 1.1924196 1.9858704 1.6215862 2.2664572
## [2,] 1.1866432 0.4134224 1.0840601 1.6949799 1.8083209
## [3,] 2.5308912 2.4971179 1.7645347 1.3187071 0.4719943
## [4,] 0.7686024 2.3726020 1.8777294 1.9836058 1.2461454

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

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.77030196 0.1367265 0.19206457
## [2,] 0.30522497 0.3160546 0.05176618
## [3,] 0.08523346 0.2429886 0.79857869
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.77030196 0.13672648 0.19206457
## [2,] 0.30522497 0.31605462 0.05176618
## [3,] 0.08523346 0.24298863 0.79857869
einsum::einsum('ijk->jki', arrD)
## , , 1
## 
##           [,1]      [,2]      [,3]
## [1,] 0.9834322 0.1552073 0.7141006
## [2,] 0.1583481 0.1727339 0.8592959
## [3,] 0.7088734 0.9606377 0.6367972
## 
## , , 2
## 
##           [,1]      [,2]      [,3]
## [1,] 0.8417612 0.6055044 0.5987732
## [2,] 0.3157352 0.1739180 0.5995460
## [3,] 0.8248322 0.3263473 0.6993484
## 
## , , 3
## 
##           [,1]      [,2]      [,3]
## [1,] 0.8832871 0.9748028 0.3041330
## [2,] 0.5446688 0.4902163 0.6051293
## [3,] 0.7375910 0.6512484 0.3851428
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
##           [,1]      [,2]      [,3]
## [1,] 0.9834322 0.1552073 0.7141006
## [2,] 0.1583481 0.1727339 0.8592959
## [3,] 0.7088734 0.9606377 0.6367972
## 
## ,,2
##           [,1]      [,2]      [,3]
## [1,] 0.8417612 0.6055044 0.5987732
## [2,] 0.3157352 0.1739180 0.5995460
## [3,] 0.8248322 0.3263473 0.6993484
## 
## ,,3
##           [,1]      [,2]      [,3]
## [1,] 0.8832871 0.9748028 0.3041330
## [2,] 0.5446688 0.4902163 0.6051293
## [3,] 0.7375910 0.6512484 0.3851428

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

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.5123848 0.91582037 1.3839319 1.105573 1.7320342
## [2,] 0.5616470 0.09331691 0.6290847 1.204876 1.4960253
## [3,] 2.1693776 2.12463580 1.1526055 0.594092 0.1129856
## [4,] 0.3349468 1.92315097 1.3200819 1.413658 0.5723241
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]       [,4]       [,5]
## [1,] 0.51238484 0.91582037 1.38393194 1.10557278 1.73203421
## [2,] 0.56164703 0.09331691 0.62908471 1.20487599 1.49602534
## [3,] 2.16937762 2.12463580 1.15260548 0.59409196 0.11298564
## [4,] 0.33494679 1.92315097 1.32008190 1.41365813 0.57232409

3.5.3 Matrix Multiplication

Matrix Multiplication is considered a contracted product.

einsum::einsum('ij,jk->ik', arrC, t(arrC))
##           [,1]      [,2]      [,3]
## [1,] 1.2408555 0.3969509 1.0992837
## [2,] 0.3969509 0.7213922 0.6889087
## [3,] 1.0992837 0.6889087 1.2006503
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 1.2408555 0.3969509 1.0992837
## [2,] 0.3969509 0.7213922 0.6889087
## [3,] 1.0992837 0.6889087 1.2006503

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.01943749 0.55331604 0.31242915
## [2,] 0.95831842 0.01285182 0.68013894
## [3,] 0.06252638 0.10355823 0.13276524
## [4,] 0.20057325 0.05166612 0.07531696
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.01943749 0.55331604 0.31242915
## [2,] 0.95831842 0.01285182 0.68013894
## [3,] 0.06252638 0.10355823 0.13276524
## [4,] 0.20057325 0.05166612 0.07531696
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
## 
##             [,1]        [,2]       [,3]      [,4]        [,5]
## [1,] 0.265342915 0.883735253 0.65872411 0.6475558 0.742856051
## [2,] 0.022956197 0.007243278 0.01894491 0.8121234 0.536750006
## [3,] 0.926955542 0.973434440 0.21824312 0.1531949 0.003090297
## [4,] 0.002455763 0.815775353 0.12458172 0.4548541 0.051178225
## 
## , , 2
## 
##            [,1]       [,2]       [,3]       [,4]       [,5]
## [1,] 0.09082541 0.01883810 0.51965044 0.02530239 0.54690826
## [2,] 0.22963339 0.08468126 0.03547752 0.35283016 0.94896490
## [3,] 0.74736337 0.68458254 0.18775187 0.28905960 0.01010032
## [4,] 0.02795570 0.38042110 0.40176726 0.18399225 0.23744875
## 
## , , 3
## 
##           [,1]        [,2]      [,3]      [,4]       [,5]
## [1,] 0.1562165 0.013247016 0.2055574 0.4327146 0.44226990
## [2,] 0.3090574 0.001392369 0.5746623 0.0399224 0.01031044
## [3,] 0.4950587 0.466618822 0.7466105 0.1518375 0.09979503
## [4,] 0.3045353 0.726954520 0.7937329 0.7748118 0.28369711
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.265342915 0.883735253 0.658724108 0.647555757 0.742856051
## [2,] 0.022956197 0.007243278 0.018944913 0.812123428 0.536750006
## [3,] 0.926955542 0.973434440 0.218243123 0.153194891 0.003090297
## [4,] 0.002455763 0.815775353 0.124581718 0.454854083 0.051178225
## 
## ,,2
##            [,1]       [,2]       [,3]       [,4]       [,5]
## [1,] 0.09082541 0.01883810 0.51965044 0.02530239 0.54690826
## [2,] 0.22963339 0.08468126 0.03547752 0.35283016 0.94896490
## [3,] 0.74736337 0.68458254 0.18775187 0.28905960 0.01010032
## [4,] 0.02795570 0.38042110 0.40176726 0.18399225 0.23744875
## 
## ,,3
##             [,1]        [,2]        [,3]        [,4]        [,5]
## [1,] 0.156216512 0.013247016 0.205557394 0.432714631 0.442269899
## [2,] 0.309057440 0.001392369 0.574662271 0.039922400 0.010310437
## [3,] 0.495058711 0.466618822 0.746610488 0.151837469 0.099795031
## [4,] 0.304535325 0.726954520 0.793732928 0.774811801 0.283697113

3.7 Summation + Permutation

Some examples of combining Summation and Permutation are shown below.

einsum::einsum('ijk->ki', arrE)
##          [,1]     [,2]     [,3]
## [1,] 1.678968 1.812275 2.206623
## [2,] 2.915010 1.872430 1.688121
## [3,] 1.769385 1.976377 2.966432
## [4,] 2.771717 1.719648 2.127514
## [5,] 1.876340 2.301468 1.615110
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]
## [1,] 1.678968 1.812275 2.206623
## [2,] 2.915010 1.872430 1.688121
## [3,] 1.769385 1.976377 2.966432
## [4,] 2.771717 1.719648 2.127514
## [5,] 1.876340 2.301468 1.615110

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.0034257390 0.011409562 0.008504530 0.008360340 0.0095907251
## [2,] 0.0146122292 0.004610539 0.012058941 0.516938133 0.3416556357
## [3,] 0.0384971782 0.040427483 0.009063805 0.006362302 0.0001283424
## [4,] 0.0003271645 0.108680164 0.016597169 0.060597095 0.0068181245
## 
## , , 2
## 
##              [,1]         [,2]         [,3]        [,4]         [,5]
## [1,] 0.0233755306 0.0048483193 0.1337412608 0.006512019 0.1407565442
## [2,] 0.0013727157 0.0005062125 0.0002120796 0.002109168 0.0056727770
## [3,] 0.0359995666 0.0329754917 0.0090437749 0.013923643 0.0004865197
## [4,] 0.0006718264 0.0091422117 0.0096551987 0.004421669 0.0057063257
## 
## , , 3
## 
##            [,1]         [,2]       [,3]       [,4]        [,5]
## [1,] 0.02678001 0.0022709202 0.03523846 0.07417975 0.075817801
## [2,] 0.11533712 0.0005196182 0.21445817 0.01489864 0.003847751
## [3,] 0.03606396 0.0339921708 0.05438896 0.01106103 0.007269852
## [4,] 0.01258527 0.0300422361 0.03280193 0.03201999 0.011724111
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.0034257390 0.0114095616 0.0085045304 0.0083603401 0.0095907251
## [2,] 0.0146122292 0.0046105389 0.0120589406 0.5169381329 0.3416556357
## [3,] 0.0384971782 0.0404274827 0.0090638051 0.0063623019 0.0001283424
## [4,] 0.0003271645 0.1086801637 0.0165971691 0.0605970946 0.0068181245
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.0233755306 0.0048483193 0.1337412608 0.0065120188 0.1407565442
## [2,] 0.0013727157 0.0005062125 0.0002120796 0.0021091684 0.0056727770
## [3,] 0.0359995666 0.0329754917 0.0090437749 0.0139236426 0.0004865197
## [4,] 0.0006718264 0.0091422117 0.0096551987 0.0044216686 0.0057063257
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.0267800102 0.0022709202 0.0352384587 0.0741797526 0.0758178007
## [2,] 0.1153371202 0.0005196182 0.2144581651 0.0148986373 0.0038477512
## [3,] 0.0360639551 0.0339921708 0.0543889573 0.0110610308 0.0072698519
## [4,] 0.0125852744 0.0300422361 0.0328019311 0.0320199936 0.0117241112

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 Under development (unstable) (2025-10-20 r88955)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.3 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.23-bioc/R/lib/libRblas.so 
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0  LAPACK version 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.19.0
##  [3] HDF5Array_1.39.0          h5mread_1.3.0            
##  [5] rhdf5_2.55.4              DelayedArray_0.37.0      
##  [7] SparseArray_1.11.1        S4Arrays_1.11.0          
##  [9] abind_1.4-8               IRanges_2.45.0           
## [11] S4Vectors_0.49.0          MatrixGenerics_1.23.0    
## [13] matrixStats_1.5.0         BiocGenerics_0.57.0      
## [15] generics_0.1.4            Matrix_1.7-4             
## [17] DelayedTensor_1.17.0      BiocStyle_2.39.0         
## 
## loaded via a namespace (and not attached):
##  [1] jsonlite_2.0.0      compiler_4.6.0      BiocManager_1.30.26
##  [4] rsvd_1.0.5          Rcpp_1.1.0          rhdf5filters_1.23.0
##  [7] parallel_4.6.0      jquerylib_0.1.4     BiocParallel_1.45.0
## [10] yaml_2.3.10         fastmap_1.2.0       lattice_0.22-7     
## [13] R6_2.6.1            XVector_0.51.0      ScaledMatrix_1.19.0
## [16] knitr_1.50          bookdown_0.45       bslib_0.9.0        
## [19] rlang_1.1.6         cachem_1.1.0        xfun_0.54          
## [22] sass_0.4.10         cli_3.6.5           Rhdf5lib_1.33.0    
## [25] BiocSingular_1.27.0 digest_0.6.37       grid_4.6.0         
## [28] irlba_2.3.5.1       rTensor_1.4.9       dqrng_0.4.1        
## [31] lifecycle_1.0.4     evaluate_1.0.5      codetools_0.2-20   
## [34] beachmat_2.27.0     rmarkdown_2.30      tools_4.6.0        
## [37] htmltools_0.5.8.1