4. Einsum operation by DelayedTensor

Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2026-10-09 08:18:32.44055
Compiled: Fri Oct 9 08:22:13 2026

What is einsum

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

It was originally introduced by Numpy1 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 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)

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.

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.

  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)

No Operation

diag

We can also extract the diagonal elements as follows.

einsum::einsum('ii->i', arrB)
## [1] 0.10699131 0.68303168 0.09241371
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
##        [1]        [2]        [3] 
## 0.10699131 0.68303168 0.09241371
einsum::einsum('iii->i', arrD)
## [1] 0.3518602 0.7762423 0.0125891
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.3518602 0.7762423 0.0125891

Multiplication

By using multiple arrays or DelayedArray objects as input and writing “,” on the right side of ->, multiplication will be performed.

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.3346494 0.1289359 0.6231210
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.3346494 0.1289359 0.6231210
einsum::einsum('ij,ij->ij', arrC, arrC)
##            [,1]       [,2]       [,3]        [,4]
## [1,] 0.76269478 0.83739300 0.03588423 0.117463041
## [2,] 0.42024830 0.21781731 0.67461161 0.009922734
## [3,] 0.00928805 0.04922506 0.19193518 0.036270884
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.762694784 0.837393003 0.035884233 0.117463041
## [2,] 0.420248301 0.217817314 0.674611606 0.009922734
## [3,] 0.009288050 0.049225060 0.191935177 0.036270884
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.08979394 0.02536299 0.08612086 0.05837382
## [2,] 0.22408734 0.79922194 0.36356067 0.19938900
## [3,] 0.68436282 0.71470860 0.84918466 0.16603450
## 
## , , 2
## 
##              [,1]      [,2]       [,3]       [,4]
## [1,] 0.0009415614 0.3370512 0.02135418 0.05038506
## [2,] 0.2667726896 0.7398452 0.13427358 0.36046412
## [3,] 0.0736590421 0.7093404 0.90571919 0.64676871
## 
## , , 3
## 
##           [,1]      [,2]         [,3]       [,4]
## [1,] 0.1428987 0.7742106 8.933372e-05 0.02110889
## [2,] 0.9086905 0.4158393 9.524020e-01 0.46038137
## [3,] 0.2182461 0.2303120 3.699245e-01 0.17510216
## 
## , , 4
## 
##             [,1]       [,2]        [,3]       [,4]
## [1,] 0.359363249 0.30029675 0.503610195 0.19992064
## [2,] 0.337069522 0.08172017 0.005215648 0.03890463
## [3,] 0.002340974 0.51516630 0.756138328 0.07902437
## 
## , , 5
## 
##           [,1]        [,2]      [,3]       [,4]
## [1,] 0.9182791 0.221214662 0.0680594 0.69858968
## [2,] 0.7858354 0.484715022 0.1969196 0.50317526
## [3,] 0.7985829 0.007238004 0.4905853 0.08375457
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.08979394 0.02536299 0.08612086 0.05837382
## [2,] 0.22408734 0.79922194 0.36356067 0.19938900
## [3,] 0.68436282 0.71470860 0.84918466 0.16603450
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.0009415614 0.3370512009 0.0213541835 0.0503850647
## [2,] 0.2667726896 0.7398451550 0.1342735813 0.3604641234
## [3,] 0.0736590421 0.7093404144 0.9057191887 0.6467687075
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]
## [1,] 1.428987e-01 7.742106e-01 8.933372e-05 2.110889e-02
## [2,] 9.086905e-01 4.158393e-01 9.524020e-01 4.603814e-01
## [3,] 2.182461e-01 2.303120e-01 3.699245e-01 1.751022e-01
## 
## ,,4
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.359363249 0.300296753 0.503610195 0.199920640
## [2,] 0.337069522 0.081720171 0.005215648 0.038904631
## [3,] 0.002340974 0.515166305 0.756138328 0.079024368
## 
## ,,5
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.918279092 0.221214662 0.068059404 0.698589679
## [2,] 0.785835412 0.484715022 0.196919633 0.503175261
## [3,] 0.798582875 0.007238004 0.490585349 0.083754571

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.3346494 0.2077218 0.4566477
## [2,] 0.2077218 0.1289359 0.2834478
## [3,] 0.4566477 0.2834478 0.6231210
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 0.3346494 0.2077218 0.4566477
## [2,] 0.2077218 0.1289359 0.2834478
## [3,] 0.4566477 0.2834478 0.6231210
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.26169709 0.27421308 0.05676431 0.10270087
## [2,] 0.19425691 0.13985233 0.24612198 0.02984965
## [3,] 0.02887924 0.06648392 0.13128067 0.05706930
## 
## , , 2, 1, 1
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.41341292 0.4331849 0.08967275 0.16224050
## [2,] 0.30687509 0.2209301 0.38880833 0.04715463
## [3,] 0.04562164 0.1050272 0.20738911 0.09015457
## 
## , , 3, 1, 1
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.72246796 0.7570209 0.1567094 0.28352661
## [2,] 0.53628566 0.3860908 0.6794697 0.08240601
## [3,] 0.07972701 0.1835424 0.3624270 0.15755140
## 
## , , 1, 2, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.13908349 0.14573534 0.03016838 0.05458217
## [2,] 0.10324123 0.07432697 0.13080583 0.01586412
## [3,] 0.01534838 0.03533404 0.06977141 0.03033048
## 
## , , 2, 2, 1
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.78074478 0.8180849 0.1693501 0.30639687
## [2,] 0.57954436 0.4172342 0.7342781 0.08905317
## [3,] 0.08615807 0.1983475 0.3916616 0.17026006
## 
## , , 3, 2, 1
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.73831194 0.7736226 0.1601461 0.2897444
## [2,] 0.54804660 0.3945579 0.6943707 0.0842132
## [3,] 0.08147545 0.1875675 0.3703751 0.1610066
## 
## , , 1, 3, 1
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.25628876 0.26854610 0.0555912 0.10057842
## [2,] 0.19024233 0.13696209 0.2410355 0.02923276
## [3,] 0.02828241 0.06510994 0.1285676 0.05588989
## 
## , , 2, 3, 1
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.52657937 0.5517637 0.1142195 0.2066517
## [2,] 0.39087818 0.2814068 0.4952396 0.0600626
## [3,] 0.05810998 0.1337770 0.2641592 0.1148332
## 
## , , 3, 3, 1
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.8047787 0.8432682 0.1745633 0.31582877
## [2,] 0.5973846 0.4300780 0.7568816 0.09179452
## [3,] 0.0888103 0.2044533 0.4037182 0.17550122
## 
## , , 1, 4, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.21100097 0.22109236 0.04576789 0.08280560
## [2,] 0.15662535 0.11276005 0.19844308 0.02406715
## [3,] 0.02328474 0.05360462 0.10584890 0.04601380
## 
## , , 2, 4, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.38996532 0.40861590 0.08458677 0.15303868
## [2,] 0.28947001 0.20839956 0.36675623 0.04448015
## [3,] 0.04303411 0.09907036 0.19562659 0.08504126
## 
## , , 3, 4, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.35585622 0.37287549 0.07718822 0.13965285
## [2,] 0.26415094 0.19017147 0.33467716 0.04058961
## [3,] 0.03927005 0.09040497 0.17851572 0.07760295
## 
## , , 1, 1, 2
## 
##            [,1]        [,2]        [,3]        [,4]
## [1,] 0.02679784 0.028079475 0.005812677 0.010516590
## [2,] 0.01989195 0.014320907 0.025202941 0.003056610
## [3,] 0.00295724 0.006807967 0.013443168 0.005843908
## 
## , , 2, 1, 2
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.45107221 0.4726453 0.09784137 0.17701958
## [2,] 0.33482946 0.2410554 0.42422630 0.05145012
## [3,] 0.04977749 0.1145945 0.22628094 0.09836707
## 
## , , 3, 1, 2
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.23702187 0.2483577 0.05141204 0.09301728
## [2,] 0.17594058 0.1266658 0.22291533 0.02703515
## [3,] 0.02615624 0.0602152 0.11890232 0.05168828
## 
## , , 1, 2, 2
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.5070179 0.5312667 0.1099765 0.19897502
## [2,] 0.3763578 0.2709531 0.4768424 0.05783139
## [3,] 0.0559513 0.1288075 0.2543462 0.11056738
## 
## , , 2, 2, 2
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.75118309 0.7871094 0.1629380 0.29479563
## [2,] 0.55760082 0.4014363 0.7064759 0.08568131
## [3,] 0.08289583 0.1908374 0.3768319 0.16381342
## 
## , , 3, 2, 2
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.73553398 0.7707118 0.1595435 0.28865426
## [2,] 0.54598453 0.3930733 0.6917581 0.08389634
## [3,] 0.08116889 0.1868618 0.3689815 0.16040076
## 
## , , 1, 3, 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.12761945 0.13372301 0.02768174 0.05008320
## [2,] 0.09473151 0.06820052 0.12002408 0.01455651
## [3,] 0.01408328 0.03242161 0.06402046 0.02783047
## 
## , , 2, 3, 2
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.32001525 0.3353204 0.06941401 0.12558735
## [2,] 0.23754630 0.1710179 0.30096929 0.03650152
## [3,] 0.03531487 0.0812996 0.16053605 0.06978697
## 
## , , 3, 3, 2
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.83113615 0.8708863 0.1802804 0.3261725
## [2,] 0.61694971 0.4441636 0.7816704 0.0948009
## [3,] 0.09171895 0.2111494 0.4169405 0.1812491
## 
## , , 1, 4, 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.19603170 0.20540716 0.04252093 0.07693103
## [2,] 0.14551370 0.10476039 0.18436472 0.02235973
## [3,] 0.02163282 0.04980168 0.09833955 0.04274940
## 
## , , 2, 4, 2
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.52433206 0.5494089 0.1137320 0.20576980
## [2,] 0.38921001 0.2802059 0.4931260 0.05980627
## [3,] 0.05786198 0.1332061 0.2630318 0.11434313
## 
## , , 3, 4, 2
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.70234402 0.7359345 0.1523443 0.27562913
## [2,] 0.52134772 0.3753364 0.6605435 0.08011063
## [3,] 0.07750626 0.1784299 0.3523318 0.15316290
## 
## , , 1, 1, 3
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.33013348 0.34592253 0.07160874 0.12955817
## [2,] 0.24505703 0.17642511 0.31048533 0.03765563
## [3,] 0.03643145 0.08387013 0.16561188 0.07199350
## 
## , , 2, 1, 3
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.83249834 0.8723136 0.1805759 0.32670712
## [2,] 0.61796086 0.4448916 0.7829516 0.09495627
## [3,] 0.09186927 0.2114955 0.4176238 0.18154616
## 
## , , 3, 1, 3
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.40798919 0.4275018 0.0884963 0.16011200
## [2,] 0.30284908 0.2180316 0.3837074 0.04653599
## [3,] 0.04502312 0.1036493 0.2046683 0.08897179
## 
## , , 1, 2, 3
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.76843111 0.8051823 0.1666792 0.30156447
## [2,] 0.57040396 0.4106537 0.7226973 0.08764865
## [3,] 0.08479921 0.1952193 0.3854844 0.16757477
## 
## , , 2, 2, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5631683 0.5901025 0.1221559 0.2210108
## [2,] 0.4180380 0.3009601 0.5296509 0.0642360
## [3,] 0.0621477 0.1430724 0.2825141 0.1228123
## 
## , , 3, 2, 3
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.41911544 0.4391602 0.09090967 0.16447841
## [2,] 0.31110805 0.2239775 0.39417146 0.04780507
## [3,] 0.04625094 0.1064759 0.21024978 0.09139814
## 
## , , 1, 3, 3
## 
##              [,1]        [,2]        [,3]         [,4]
## [1,] 0.0082543543 0.008649129 0.001790439 0.0032393535
## [2,] 0.0061271809 0.004411171 0.007763090 0.0009415067
## [3,] 0.0009108985 0.002097012 0.004140807 0.0018000592
## 
## , , 2, 3, 3
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.85228635 0.8930480 0.1848681 0.33447277
## [2,] 0.63264944 0.4554664 0.8015619 0.09721333
## [3,] 0.09405295 0.2165226 0.4275505 0.18586141
## 
## , , 3, 3, 3
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.53116805 0.5565718 0.1152148 0.20845253
## [2,] 0.39428435 0.2838591 0.4995552 0.06058599
## [3,] 0.05861636 0.1349428 0.2664611 0.11583389
## 
## , , 1, 4, 3
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.12688437 0.13295277 0.02752229 0.04979473
## [2,] 0.09418586 0.06780769 0.11933275 0.01447266
## [3,] 0.01400216 0.03223487 0.06365170 0.02767017
## 
## , , 2, 4, 3
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.59256263 0.6209027 0.1285318 0.23254633
## [2,] 0.43985735 0.3166686 0.5572958 0.06758877
## [3,] 0.06539148 0.1505400 0.2972598 0.12922244
## 
## , , 3, 4, 3
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.36544425 0.3829221 0.07926794 0.14341559
## [2,] 0.27126810 0.1952954 0.34369456 0.04168324
## [3,] 0.04032812 0.0928408 0.18332557 0.07969385
## 
## , , 1, 1, 4
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.52353078 0.5485693 0.1135582 0.20545535
## [2,] 0.38861523 0.2797777 0.4923724 0.05971487
## [3,] 0.05777356 0.1330025 0.2626299 0.11416840
## 
## , , 2, 1, 4
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.50703172 0.5312811 0.1099795 0.19898043
## [2,] 0.37636803 0.2709605 0.4768553 0.05783296
## [3,] 0.05595282 0.1288110 0.2543531 0.11057038
## 
## , , 3, 1, 4
## 
##             [,1]       [,2]        [,3]        [,4]
## [1,] 0.042254567 0.04427544 0.009165372 0.016582457
## [2,] 0.031365430 0.02258107 0.039739754 0.004819633
## [3,] 0.004662948 0.01073474 0.021197056 0.009214618
## 
## , , 1, 2, 4
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.47857577 0.5014643 0.1038071 0.18781312
## [2,] 0.35524527 0.2557535 0.4500930 0.05458722
## [3,] 0.05281261 0.1215818 0.2400781 0.10436488
## 
## , , 2, 2, 4
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.24965486 0.2615949 0.05415224 0.09797500
## [2,] 0.18531800 0.1334169 0.23479646 0.02847609
## [3,] 0.02755034 0.0634246 0.12523967 0.05444321
## 
## , , 3, 2, 4
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.6268290 0.6568079 0.1359645 0.24599390
## [2,] 0.4652932 0.3349808 0.5895228 0.07149726
## [3,] 0.0691729 0.1592454 0.3144496 0.13669505
## 
## , , 1, 3, 4
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.61975872 0.6493995 0.1344309 0.24321921
## [2,] 0.46004492 0.3312024 0.5828733 0.07069081
## [3,] 0.06839267 0.1574492 0.3109027 0.13515320
## 
## , , 2, 3, 4
## 
##             [,1]       [,2]       [,3]       [,4]
## [1,] 0.063070973 0.06608742 0.01368063 0.02475168
## [2,] 0.046817382 0.03370547 0.05931726 0.00719399
## [3,] 0.006960115 0.01602313 0.03163963 0.01375413
## 
## , , 3, 3, 4
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.75940948 0.7957292 0.1647223 0.29802400
## [2,] 0.56370724 0.4058325 0.7142126 0.08661963
## [3,] 0.08380364 0.1929273 0.3809587 0.16560738
## 
## , , 1, 4, 4
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.39048486 0.40916029 0.08469946 0.15324257
## [2,] 0.28985567 0.20867721 0.36724486 0.04453941
## [3,] 0.04309145 0.09920235 0.19588722 0.08515456
## 
## , , 2, 4, 4
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.17225667 0.18049506 0.03736392 0.06760071
## [2,] 0.12786557 0.09205489 0.16200468 0.01964791
## [3,] 0.01900916 0.04376166 0.08641277 0.03756468
## 
## , , 3, 4, 4
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.24550249 0.2572440 0.05325156 0.09634543
## [2,] 0.18223572 0.1311978 0.23089122 0.02800246
## [3,] 0.02709211 0.0623697 0.12315663 0.05353768
## 
## , , 1, 1, 5
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.83687913 0.8769039 0.1815261 0.32842633
## [2,] 0.62121271 0.4472327 0.7870716 0.09545595
## [3,] 0.09235271 0.2126084 0.4198215 0.18250149
## 
## , , 2, 1, 5
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.77417864 0.8112047 0.1679259 0.30382004
## [2,] 0.57467034 0.4137252 0.7281028 0.08830422
## [3,] 0.08543348 0.1966794 0.3883677 0.16882815
## 
## , , 3, 1, 5
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.78043257 0.8177577 0.1692824 0.30627434
## [2,] 0.57931261 0.4170674 0.7339845 0.08901756
## [3,] 0.08612362 0.1982682 0.3915050 0.17019197
## 
## , , 1, 2, 5
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.41075451 0.4303994 0.08909612 0.16119723
## [2,] 0.30490176 0.2195094 0.38630814 0.04685141
## [3,] 0.04532828 0.1043518 0.20605552 0.08957484
## 
## , , 2, 2, 5
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.60802107 0.6371004 0.1318849 0.23861287
## [2,] 0.45133210 0.3249297 0.5718342 0.06935199
## [3,] 0.06709737 0.1544672 0.3050145 0.13259352
## 
## , , 3, 2, 5
## 
##             [,1]       [,2]       [,3]        [,4]
## [1,] 0.074299315 0.07785277 0.01611615 0.029158155
## [2,] 0.055152144 0.03970595 0.06987733 0.008474715
## [3,] 0.008199204 0.01887568 0.03727234 0.016202741
## 
## , , 1, 3, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.22783448 0.23873095 0.04941922 0.08941177
## [2,] 0.16912081 0.12175597 0.21427474 0.02598722
## [3,] 0.02514238 0.05788116 0.11429345 0.04968475
## 
## , , 2, 3, 5
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.3875430 0.40607773 0.08406135 0.15208806
## [2,] 0.2876719 0.20710506 0.36447808 0.04420386
## [3,] 0.0427668 0.09845497 0.19441143 0.08451301
## 
## , , 3, 3, 5
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.61169182 0.6409468 0.1326811 0.24005343
## [2,] 0.45405689 0.3268914 0.5752865 0.06977068
## [3,] 0.06750245 0.1553998 0.3068560 0.13339402
## 
## , , 1, 4, 5
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.72993884 0.7648491 0.1583299 0.28645849
## [2,] 0.54183127 0.3900832 0.6864960 0.08325815
## [3,] 0.08055145 0.1854403 0.3661747 0.15918061
## 
## , , 2, 4, 5
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.61949104 0.6491190 0.1343728 0.24311416
## [2,] 0.45984622 0.3310593 0.5826216 0.07066027
## [3,] 0.06836313 0.1573812 0.3107685 0.13509482
## 
## , , 3, 4, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.25274330 0.26483106 0.05482215 0.09918703
## [2,] 0.18761054 0.13506737 0.23770108 0.02882836
## [3,] 0.02789116 0.06420922 0.12678899 0.05511672
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.26169709 0.27421308 0.05676431 0.10270087
## [2,] 0.19425691 0.13985233 0.24612198 0.02984965
## [3,] 0.02887924 0.06648392 0.13128067 0.05706930
## 
## ,,2,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.41341292 0.43318491 0.08967275 0.16224050
## [2,] 0.30687509 0.22093009 0.38880833 0.04715463
## [3,] 0.04562164 0.10502720 0.20738911 0.09015457
## 
## ,,3,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.72246796 0.75702090 0.15670940 0.28352661
## [2,] 0.53628566 0.38609076 0.67946972 0.08240601
## [3,] 0.07972701 0.18354237 0.36242696 0.15755140
## 
## ...
## 
## ,,1,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.72993884 0.76484908 0.15832989 0.28645849
## [2,] 0.54183127 0.39008323 0.68649596 0.08325815
## [3,] 0.08055145 0.18544034 0.36617473 0.15918061
## 
## ,,2,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.61949104 0.64911897 0.13437283 0.24311416
## [2,] 0.45984622 0.33105934 0.58262155 0.07066027
## [3,] 0.06836313 0.15738117 0.31076846 0.13509482
## 
## ,,3,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.25274330 0.26483106 0.05482215 0.09918703
## [2,] 0.18761054 0.13506737 0.23770108 0.02882836
## [3,] 0.02789116 0.06420922 0.12678899 0.05511672

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

Row-wise / Column-wise Summation

einsum::einsum('ij->i', arrC)
## [1] 2.320576 2.035935 0.946795
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
##      [1]      [2]      [3] 
## 2.320576 2.035935 0.946795
einsum::einsum('ij->j', arrC)
## [1] 1.6179641 1.6036680 1.4488828 0.6327909
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
##       [1]       [2]       [3]       [4] 
## 1.6179641 1.6036680 1.4488828 0.6327909

Mode-wise Vectorization

einsum::einsum('ijk->i', arrE)
## [1]  8.218024 11.884557 11.730275
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
##  8.218024 11.884557 11.730275
einsum::einsum('ijk->j', arrE)
## [1] 8.184131 8.989476 7.932262 6.726987
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 8.184131 8.989476 7.932262 6.726987
einsum::einsum('ijk->k', arrE)
## [1] 6.412497 6.194844 6.638929 5.356958 7.229628
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4]      [5] 
## 6.412497 6.194844 6.638929 5.356958 7.229628

Mode-wise Summation

These are the same as what the modeSum function does.

einsum::einsum('ijk->ij', arrE)
##          [,1]     [,2]     [,3]     [,4]
## [1,] 2.266099 2.638039 1.419583 1.894304
## [2,] 3.410182 3.381073 2.461280 2.632022
## [3,] 2.507851 2.970364 4.051399 2.200661
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]     [,4]
## [1,] 2.266099 2.638039 1.419583 1.894304
## [2,] 3.410182 3.381073 2.461280 2.632022
## [3,] 2.507851 2.970364 4.051399 2.200661
einsum::einsum('ijk->jk', arrE)
##          [,1]      [,2]     [,3]      [,4]     [,5]
## [1,] 1.600297 0.8185873 1.798440 1.2284296 2.738377
## [2,] 1.898654 2.2829271 2.004657 1.5516117 1.251626
## [3,] 1.817936 1.4642571 1.593577 1.6514366 1.405056
## [4,] 1.095610 1.6290721 1.242255 0.9254802 1.834569
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.6002972 0.8185873 1.7984403 1.2284296 2.7383770
## [2,] 1.8986542 2.2829271 2.0046567 1.5516117 1.2516259
## [3,] 1.8179356 1.4642571 1.5935767 1.6514366 1.4050562
## [4,] 1.0956100 1.6290721 1.2422552 0.9254802 1.8345691
einsum::einsum('ijk->jk', arrE)
##          [,1]      [,2]     [,3]      [,4]     [,5]
## [1,] 1.600297 0.8185873 1.798440 1.2284296 2.738377
## [2,] 1.898654 2.2829271 2.004657 1.5516117 1.251626
## [3,] 1.817936 1.4642571 1.593577 1.6514366 1.405056
## [4,] 1.095610 1.6290721 1.242255 0.9254802 1.834569
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.6002972 0.8185873 1.7984403 1.2284296 2.7383770
## [2,] 1.8986542 2.2829271 2.0046567 1.5516117 1.2516259
## [3,] 1.8179356 1.4642571 1.5935767 1.6514366 1.4050562
## [4,] 1.0956100 1.6290721 1.2422552 0.9254802 1.8345691

Trace

If we take the diagonal elements of a matrix and add them together, we get trace.

einsum::einsum('ii->', arrB)
## [1] 0.8824367
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
##       [1] 
## 0.8824367

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.10699131 0.9054340 0.59885252
## [2,] 0.73754414 0.6830317 0.11033178
## [3,] 0.08118953 0.8486083 0.09241371
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.10699131 0.90543402 0.59885252
## [2,] 0.73754414 0.68303168 0.11033178
## [3,] 0.08118953 0.84860835 0.09241371
einsum::einsum('ijk->jki', arrD)
## , , 1
## 
##           [,1]      [,2]       [,3]
## [1,] 0.3518602 0.3198072 0.09540995
## [2,] 0.9668560 0.5795112 0.74593958
## [3,] 0.7861685 0.9303584 0.43602442
## 
## , , 2
## 
##           [,1]      [,2]      [,3]
## [1,] 0.6033804 0.7843529 0.8831996
## [2,] 0.1317651 0.7762423 0.4548359
## [3,] 0.4840233 0.8427797 0.1444793
## 
## , , 3
## 
##            [,1]       [,2]      [,3]
## [1,] 0.23404904 0.27659665 0.5062039
## [2,] 0.75493955 0.24080157 0.1431519
## [3,] 0.04891729 0.09107336 0.0125891
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
##            [,1]       [,2]       [,3]
## [1,] 0.35186022 0.31980723 0.09540995
## [2,] 0.96685602 0.57951120 0.74593958
## [3,] 0.78616850 0.93035838 0.43602442
## 
## ,,2
##           [,1]      [,2]      [,3]
## [1,] 0.6033804 0.7843529 0.8831996
## [2,] 0.1317651 0.7762423 0.4548359
## [3,] 0.4840233 0.8427797 0.1444793
## 
## ,,3
##            [,1]       [,2]       [,3]
## [1,] 0.23404904 0.27659665 0.50620391
## [2,] 0.75493955 0.24080157 0.14315186
## [3,] 0.04891729 0.09107336 0.01258910

Multiplication + Summation

Some examples of combining Multiplication and Summation are shown below.

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

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.9982441 0.3413733 1.2698353 0.6987737 2.5026974
## [2,] 1.5392935 1.7862368 1.4203619 0.8971832 0.7131677
## [3,] 1.2988662 1.0613470 1.3224158 1.2649642 0.7555644
## [4,] 0.4237973 1.0576179 0.6565924 0.3178496 1.2855195
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.9982441 0.3413733 1.2698353 0.6987737 2.5026974
## [2,] 1.5392935 1.7862368 1.4203619 0.8971832 0.7131677
## [3,] 1.2988662 1.0613470 1.3224158 1.2649642 0.7555644
## [4,] 0.4237973 1.0576179 0.6565924 0.3178496 1.2855195

Matrix Multiplication

Matrix Multiplication is considered a contracted product.

einsum::einsum('ij,jk->ik', arrC, t(arrC))
##           [,1]      [,2]      [,3]
## [1,] 1.7534351 1.1829567 0.4354581
## [2,] 1.1829567 1.3226000 0.5448306
## [3,] 0.4354581 0.5448306 0.2867192
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 1.7534351 1.1829567 0.4354581
## [2,] 1.1829567 1.3226000 0.5448306
## [3,] 0.4354581 0.5448306 0.2867192

Multiplication + Permutation

Some examples of combining Multiplication and Permutation are shown below.

einsum::einsum('ij,ij->ji', arrC, arrC)
##            [,1]        [,2]       [,3]
## [1,] 0.76269478 0.420248301 0.00928805
## [2,] 0.83739300 0.217817314 0.04922506
## [3,] 0.03588423 0.674611606 0.19193518
## [4,] 0.11746304 0.009922734 0.03627088
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
##             [,1]        [,2]        [,3]
## [1,] 0.762694784 0.420248301 0.009288050
## [2,] 0.837393003 0.217817314 0.049225060
## [3,] 0.035884233 0.674611606 0.191935177
## [4,] 0.117463041 0.009922734 0.036270884
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
## 
##            [,1]         [,2]         [,3]      [,4]      [,5]
## [1,] 0.08979394 0.0009415614 1.428987e-01 0.3593632 0.9182791
## [2,] 0.02536299 0.3370512009 7.742106e-01 0.3002968 0.2212147
## [3,] 0.08612086 0.0213541835 8.933372e-05 0.5036102 0.0680594
## [4,] 0.05837382 0.0503850647 2.110889e-02 0.1999206 0.6985897
## 
## , , 2
## 
##           [,1]      [,2]      [,3]        [,4]      [,5]
## [1,] 0.2240873 0.2667727 0.9086905 0.337069522 0.7858354
## [2,] 0.7992219 0.7398452 0.4158393 0.081720171 0.4847150
## [3,] 0.3635607 0.1342736 0.9524020 0.005215648 0.1969196
## [4,] 0.1993890 0.3604641 0.4603814 0.038904631 0.5031753
## 
## , , 3
## 
##           [,1]       [,2]      [,3]        [,4]        [,5]
## [1,] 0.6843628 0.07365904 0.2182461 0.002340974 0.798582875
## [2,] 0.7147086 0.70934041 0.2303120 0.515166305 0.007238004
## [3,] 0.8491847 0.90571919 0.3699245 0.756138328 0.490585349
## [4,] 0.1660345 0.64676871 0.1751022 0.079024368 0.083754571
DelayedTensor::einsum('ijk,ijk->jki', darrE, darrE)
## <4 x 5 x 3> HDF5Array object of type "double":
## ,,1
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 8.979394e-02 9.415614e-04 1.428987e-01 3.593632e-01 9.182791e-01
## [2,] 2.536299e-02 3.370512e-01 7.742106e-01 3.002968e-01 2.212147e-01
## [3,] 8.612086e-02 2.135418e-02 8.933372e-05 5.036102e-01 6.805940e-02
## [4,] 5.837382e-02 5.038506e-02 2.110889e-02 1.999206e-01 6.985897e-01
## 
## ,,2
##             [,1]        [,2]        [,3]        [,4]        [,5]
## [1,] 0.224087340 0.266772690 0.908690468 0.337069522 0.785835412
## [2,] 0.799221942 0.739845155 0.415839340 0.081720171 0.484715022
## [3,] 0.363560673 0.134273581 0.952401978 0.005215648 0.196919633
## [4,] 0.199389002 0.360464123 0.460381369 0.038904631 0.503175261
## 
## ,,3
##             [,1]        [,2]        [,3]        [,4]        [,5]
## [1,] 0.684362819 0.073659042 0.218246123 0.002340974 0.798582875
## [2,] 0.714708603 0.709340414 0.230311984 0.515166305 0.007238004
## [3,] 0.849184661 0.905719189 0.369924519 0.756138328 0.490585349
## [4,] 0.166034500 0.646768707 0.175102158 0.079024368 0.083754571

Summation + Permutation

Some examples of combining Summation and Permutation are shown below.

einsum::einsum('ijk->ki', arrE)
##           [,1]     [,2]     [,3]
## [1,] 0.9939843 2.416861 3.001652
## [2,] 0.9818429 2.343463 2.869538
## [3,] 1.4126525 3.252534 1.973743
## [4,] 2.3042424 1.135906 1.916809
## [5,] 2.5253022 2.735793 1.968533
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 0.9939843 2.4168607 3.0016521
## [2,] 0.9818429 2.3434632 2.8695376
## [3,] 1.4126525 3.2525336 1.9737428
## [4,] 2.3042424 1.1359063 1.9168094
## [5,] 2.5253022 2.7357931 1.9685329

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.039618026 0.0004154268 6.304842e-02 0.15855484 0.40515437
## [2,] 0.012286404 0.1632752140 3.750451e-01 0.14547053 0.10716138
## [3,] 0.001787751 0.0004432836 1.854446e-06 0.01045426 0.00141282
## [4,] 0.003966564 0.0034237190 1.434372e-03 0.01358482 0.04746992
## 
## , , 2
## 
##              [,1]        [,2]        [,3]        [,4]        [,5]
## [1,] 0.0338150651 0.040256339 0.137122549 0.050864220 0.118583565
## [2,] 0.0625096027 0.057865562 0.032524072 0.006391586 0.037911051
## [3,] 0.0880679016 0.032526050 0.230707141 0.001263424 0.047701251
## [4,] 0.0007104271 0.001284341 0.001640348 0.000138618 0.001792824
## 
## , , 3
## 
##             [,1]         [,2]        [,3]         [,4]         [,5]
## [1,] 0.005017613 0.0005400535 0.001600137 1.716356e-05 0.0058550515
## [2,] 0.027771634 0.0275630412 0.008949298 2.001796e-02 0.0002812492
## [3,] 0.128659807 0.1372253426 0.056047194 1.145624e-01 0.0743284932
## [4,] 0.004753819 0.0185179676 0.005013440 2.262587e-03 0.0023980202
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,] 3.961803e-02 4.154268e-04 6.304842e-02 1.585548e-01 4.051544e-01
## [2,] 1.228640e-02 1.632752e-01 3.750451e-01 1.454705e-01 1.071614e-01
## [3,] 1.787751e-03 4.432836e-04 1.854446e-06 1.045426e-02 1.412820e-03
## [4,] 3.966564e-03 3.423719e-03 1.434372e-03 1.358482e-02 4.746992e-02
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.0338150651 0.0402563388 0.1371225494 0.0508642203 0.1185835650
## [2,] 0.0625096027 0.0578655618 0.0325240719 0.0063915855 0.0379110505
## [3,] 0.0880679016 0.0325260497 0.2307071413 0.0012634238 0.0477012507
## [4,] 0.0007104271 0.0012843410 0.0016403481 0.0001386180 0.0017928236
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 5.017613e-03 5.400535e-04 1.600137e-03 1.716356e-05 5.855052e-03
## [2,] 2.777163e-02 2.756304e-02 8.949298e-03 2.001796e-02 2.812492e-04
## [3,] 1.286598e-01 1.372253e-01 5.604719e-02 1.145624e-01 7.432849e-02
## [4,] 4.753819e-03 1.851797e-02 5.013440e-03 2.262587e-03 2.398020e-03

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 (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.6.1 (2026-06-24)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 26.04.1 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.32.so;  LAPACK version 3.12.0
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [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: Etc/UTC
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats4    stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
##  [1] einsum_0.2.0              DelayedRandomArray_1.20.0
##  [3] HDF5Array_1.40.0          h5mread_1.4.1            
##  [5] rhdf5_2.56.1              DelayedArray_0.38.2      
##  [7] SparseArray_1.12.3        S4Arrays_1.12.1          
##  [9] abind_1.4-8               IRanges_2.46.0           
## [11] S4Vectors_0.50.3          MatrixGenerics_1.24.0    
## [13] matrixStats_1.5.0         BiocGenerics_0.58.1      
## [15] generics_0.1.4            Matrix_1.7-6             
## [17] DelayedTensor_1.18.0      BiocStyle_2.40.0         
## 
## loaded via a namespace (and not attached):
##  [1] dqrng_0.4.1         sass_0.4.10         lattice_0.23-1     
##  [4] digest_0.6.39       evaluate_1.0.5      grid_4.6.1         
##  [7] fastmap_1.2.0       jsonlite_2.0.0      BiocManager_1.30.27
## [10] codetools_0.2-20    jquerylib_0.1.4     cli_3.6.6          
## [13] rlang_1.3.0         XVector_0.52.0      cachem_1.1.0       
## [16] yaml_2.3.12         otel_0.2.0          tools_4.6.1        
## [19] beachmat_2.28.0     parallel_4.6.1      BiocParallel_1.46.0
## [22] Rhdf5lib_2.0.0      rsvd_1.0.5          buildtools_1.0.0   
## [25] R6_2.6.1            lifecycle_1.0.5     BiocSingular_1.28.1
## [28] irlba_2.4.1         ScaledMatrix_1.20.0 rTensor_1.5.0      
## [31] bslib_0.12.0        Rcpp_1.1.2          xfun_0.61          
## [34] sys_3.4.3           knitr_1.52          rhdf5filters_1.24.1
## [37] htmltools_0.5.9     rmarkdown_2.32      maketools_1.3.2    
## [40] compiler_4.6.1