4. Einsum operation by DelayedTensor

Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2026-07-04 14:25:15.875989
Compiled: Sat Jul 4 14:28:05 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.06984382 0.25198299 0.92261500
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
##        [1]        [2]        [3] 
## 0.06984382 0.25198299 0.92261500
einsum::einsum('iii->i', arrD)
## [1] 0.964915121 0.256330549 0.009418362
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
##         [1]         [2]         [3] 
## 0.964915121 0.256330549 0.009418362

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.072857346 0.753374396 0.004368118
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
##         [1]         [2]         [3] 
## 0.072857346 0.753374396 0.004368118
einsum::einsum('ij,ij->ij', arrC, arrC)
##            [,1]      [,2]         [,3]        [,4]
## [1,] 0.90388512 0.2084503 0.4637654312 0.389362786
## [2,] 0.59740325 0.5007220 0.0003939068 0.707080782
## [3,] 0.03296242 0.1886345 0.0342360111 0.001179628
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.9038851177 0.2084502672 0.4637654312 0.3893627858
## [2,] 0.5974032456 0.5007220167 0.0003939068 0.7070807818
## [3,] 0.0329624199 0.1886345304 0.0342360111 0.0011796282
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
## 
##            [,1]      [,2]      [,3]        [,4]
## [1,] 0.05463284 0.3223101 0.2528648 0.006893862
## [2,] 0.11786432 0.8039566 0.5382102 0.699148817
## [3,] 0.30312181 0.1760347 0.0152417 0.132812222
## 
## , , 2
## 
##             [,1]       [,2]       [,3]        [,4]
## [1,] 0.061388691 0.07032738 0.02613862 0.127316731
## [2,] 0.093248630 0.73528336 0.69631045 0.204144712
## [3,] 0.001727302 0.34047351 0.89101272 0.003748264
## 
## , , 3
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.57673100 0.03335441 0.5212658 0.2782550
## [2,] 0.07598089 0.35967333 0.2101516 0.2586862
## [3,] 0.43577899 0.28784498 0.6950908 0.9674389
## 
## , , 4
## 
##              [,1]      [,2]      [,3]      [,4]
## [1,] 6.236017e-02 0.2306726 0.6810289 0.5343156
## [2,] 9.413462e-05 0.1362343 0.6428977 0.7181582
## [3,] 1.022929e-01 0.2900237 0.4136356 0.1109121
## 
## , , 5
## 
##           [,1]        [,2]       [,3]      [,4]
## [1,] 0.5054046 0.001413284 0.02410386 0.5158113
## [2,] 0.8988897 0.531744449 0.77899138 0.4159215
## [3,] 0.5258854 0.876880050 0.01894992 0.0229425
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.054632845 0.322310122 0.252864771 0.006893862
## [2,] 0.117864322 0.803956591 0.538210228 0.699148817
## [3,] 0.303121813 0.176034668 0.015241703 0.132812222
## 
## ,,2
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.061388691 0.070327378 0.026138622 0.127316731
## [2,] 0.093248630 0.735283362 0.696310454 0.204144712
## [3,] 0.001727302 0.340473514 0.891012719 0.003748264
## 
## ,,3
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.57673100 0.03335441 0.52126578 0.27825504
## [2,] 0.07598089 0.35967333 0.21015157 0.25868623
## [3,] 0.43577899 0.28784498 0.69509082 0.96743895
## 
## ,,4
##              [,1]         [,2]         [,3]         [,4]
## [1,] 6.236017e-02 2.306726e-01 6.810289e-01 5.343156e-01
## [2,] 9.413462e-05 1.362343e-01 6.428977e-01 7.181582e-01
## [3,] 1.022929e-01 2.900237e-01 4.136356e-01 1.109121e-01
## 
## ,,5
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.505404554 0.001413284 0.024103862 0.515811274
## [2,] 0.898889674 0.531744449 0.778991375 0.415921521
## [3,] 0.525885359 0.876880050 0.018949925 0.022942502

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.07285735 0.23428371 0.017839548
## [2,] 0.23428371 0.75337440 0.057365741
## [3,] 0.01783955 0.05736574 0.004368118
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
##             [,1]        [,2]        [,3]
## [1,] 0.072857346 0.234283714 0.017839548
## [2,] 0.234283714 0.753374396 0.057365741
## [3,] 0.017839548 0.057365741 0.004368118
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
## 
##           [,1]      [,2]        [,3]        [,4]
## [1,] 0.2222202 0.1067157 0.159175453 0.145849226
## [2,] 0.1806595 0.1653961 0.004638992 0.196544739
## [3,] 0.0424362 0.1015167 0.043248245 0.008027854
## 
## , , 2, 1, 1
## 
##            [,1]      [,2]        [,3]       [,4]
## [1,] 0.32639823 0.1567445 0.233797772 0.21422414
## [2,] 0.26535359 0.2429347 0.006813777 0.28868598
## [3,] 0.06233052 0.1491083 0.063523258 0.01179136
## 
## , , 3, 1, 1
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.52343796 0.2513679 0.37493655 0.34354673
## [2,] 0.42554195 0.3895892 0.01092711 0.46295962
## [3,] 0.09995813 0.2391218 0.10187091 0.01890955
## 
## , , 1, 2, 1
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.5397512 0.2592019 0.38662164 0.35425354
## [2,] 0.4388042 0.4017310 0.01126766 0.47738799
## [3,] 0.1030734 0.2465742 0.10504577 0.01949887
## 
## , , 2, 2, 1
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.8524579 0.4093714 0.61061221 0.55949154
## [2,] 0.6930269 0.6344752 0.01779562 0.75396436
## [3,] 0.1627893 0.3894278 0.16590439 0.03079561
## 
## , , 3, 2, 1
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.39889236 0.1915580 0.28572503 0.26180403
## [2,] 0.32428950 0.2968913 0.00832714 0.35280410
## [3,] 0.07617433 0.1822257 0.07763198 0.01441026
## 
## , , 1, 3, 1
## 
##            [,1]      [,2]        [,3]       [,4]
## [1,] 0.47808023 0.2295860 0.342446988 0.31377720
## [2,] 0.38866725 0.3558300 0.009980238 0.42284255
## [3,] 0.09129641 0.2184011 0.093043437 0.01727097
## 
## , , 2, 3, 1
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.6974813 0.3349479 0.49960314 0.45777618
## [2,] 0.5670349 0.5191278 0.01456038 0.61689392
## [3,] 0.1331943 0.3186299 0.13574303 0.02519698
## 
## , , 3, 3, 1
## 
##            [,1]       [,2]        [,3]        [,4]
## [1,] 0.11737439 0.05636610 0.084074817 0.077036043
## [2,] 0.09542244 0.08736050 0.002450267 0.103812886
## [3,] 0.02241436 0.05362007 0.022843273 0.004240229
## 
## , , 1, 4, 1
## 
##            [,1]       [,2]        [,3]       [,4]
## [1,] 0.07893833 0.03790815 0.056543214 0.05180940
## [2,] 0.06417488 0.05875295 0.001647889 0.06981775
## [3,] 0.01507443 0.03606134 0.015362889 0.00285170
## 
## , , 2, 4, 1
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.7949530 0.3817561 0.56942168 0.52174949
## [2,] 0.6462769 0.5916749 0.01659516 0.70310361
## [3,] 0.1518079 0.3631578 0.15471285 0.02871821
## 
## , , 3, 4, 1
## 
##            [,1]      [,2]        [,3]       [,4]
## [1,] 0.34647798 0.1663873 0.248180816 0.22740303
## [2,] 0.28167792 0.2578798 0.007232955 0.30644570
## [3,] 0.06616504 0.1582813 0.067431155 0.01251675
## 
## , , 1, 1, 2
## 
##            [,1]      [,2]        [,3]        [,4]
## [1,] 0.23555960 0.1131216 0.168730414 0.154604242
## [2,] 0.19150405 0.1753245 0.004917461 0.208342898
## [3,] 0.04498355 0.1076105 0.045844344 0.008509749
## 
## , , 2, 1, 2
## 
##            [,1]      [,2]        [,3]       [,4]
## [1,] 0.29032060 0.1394192 0.207955503 0.19054539
## [2,] 0.23602338 0.2160825 0.006060633 0.25677678
## [3,] 0.05544096 0.1326270 0.056501868 0.01048803
## 
## , , 3, 1, 2
## 
##             [,1]       [,2]         [,3]        [,4]
## [1,] 0.039513070 0.01897516 0.0283030569 0.025933515
## [2,] 0.032123137 0.02940915 0.0008248612 0.034947706
## [3,] 0.007545599 0.01805073 0.0076899893 0.001427436
## 
## , , 1, 2, 2
## 
##            [,1]      [,2]       [,3]        [,4]
## [1,] 0.25212670 0.1210775 0.18059736 0.165477684
## [2,] 0.20497269 0.1876552 0.00526331 0.222995824
## [3,] 0.04814728 0.1151789 0.04906861 0.009108247
## 
## , , 2, 2, 2
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.8152372 0.3914971 0.58395120 0.53506259
## [2,] 0.6627674 0.6067723 0.01701861 0.72104420
## [3,] 0.1556815 0.3724243 0.15866055 0.02945099
## 
## , , 3, 2, 2
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.5547512 0.2664053 0.3973661 0.36409850
## [2,] 0.4509989 0.4128954 0.0115808 0.49065495
## [3,] 0.1059379 0.2534266 0.1079651 0.02004076
## 
## , , 1, 3, 2
## 
##            [,1]       [,2]        [,3]        [,4]
## [1,] 0.15370853 0.07381465 0.110100815 0.100883135
## [2,] 0.12496119 0.11440360 0.003208766 0.135948952
## [3,] 0.02935289 0.07021856 0.029914581 0.005552824
## 
## , , 2, 3, 2
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.7933377 0.3809804 0.56826465 0.52068933
## [2,] 0.6449637 0.5904727 0.01656144 0.70167495
## [3,] 0.1514994 0.3624199 0.15439849 0.02865986
## 
## , , 3, 3, 2
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.8974258 0.4309662 0.64282260 0.58900526
## [2,] 0.7295847 0.6679444 0.01873435 0.79373671
## [3,] 0.1713766 0.4099704 0.17465601 0.03242011
## 
## , , 1, 4, 2
## 
##            [,1]      [,2]        [,3]       [,4]
## [1,] 0.33923399 0.1629086 0.242991973 0.22264860
## [2,] 0.27578874 0.2524882 0.007081732 0.30003869
## [3,] 0.06478169 0.1549720 0.066021338 0.01225506
## 
## , , 2, 4, 2
## 
##            [,1]      [,2]        [,3]       [,4]
## [1,] 0.42956183 0.2062863 0.307693452 0.28193324
## [2,] 0.34922301 0.3197182 0.008967385 0.37993000
## [3,] 0.08203111 0.1962364 0.083600841 0.01551821
## 
## , , 3, 4, 2
## 
##            [,1]       [,2]        [,3]       [,4]
## [1,] 0.05820653 0.02795222 0.041693108 0.03820255
## [2,] 0.04732045 0.04332249 0.001215099 0.05148131
## [3,] 0.01111539 0.02659045 0.011328089 0.00210275
## 
## , , 1, 1, 3
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.7220101 0.3467272 0.51717299 0.4738751
## [2,] 0.5869761 0.5373843 0.01507243 0.6385886
## [3,] 0.1378784 0.3298354 0.14051679 0.0260831
## 
## , , 2, 1, 3
## 
##            [,1]      [,2]        [,3]       [,4]
## [1,] 0.26206488 0.1258501 0.187716042 0.17200039
## [2,] 0.21305218 0.1950521 0.005470776 0.23178574
## [3,] 0.05004512 0.1197189 0.051002772 0.00946727
## 
## , , 3, 1, 3
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.6276099 0.3013938 0.44955448 0.41191762
## [2,] 0.5102311 0.4671233 0.01310177 0.55509544
## [3,] 0.1198513 0.2867106 0.12214473 0.02267283
## 
## , , 1, 2, 3
## 
##            [,1]       [,2]        [,3]        [,4]
## [1,] 0.17363339 0.08338307 0.124372917 0.113960371
## [2,] 0.14115960 0.12923346 0.003624711 0.153571686
## [3,] 0.03315784 0.07932083 0.033792336 0.006272623
## 
## , , 2, 2, 3
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.5701784 0.2738138 0.40841653 0.37422375
## [2,] 0.4635407 0.4243776 0.01190285 0.50429962
## [3,] 0.1088839 0.2604742 0.11096747 0.02059808
## 
## , , 3, 2, 3
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.51007724 0.2449518 0.36536632 0.33477772
## [2,] 0.41468002 0.3796450 0.01064820 0.45114261
## [3,] 0.09740671 0.2330182 0.09927066 0.01842688
## 
## , , 1, 3, 3
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.6864141 0.3296331 0.49167575 0.45051248
## [2,] 0.5580375 0.5108906 0.01432935 0.60710544
## [3,] 0.1310808 0.3135741 0.13358915 0.02479717
## 
## , , 2, 3, 3
## 
##            [,1]      [,2]        [,3]       [,4]
## [1,] 0.43583583 0.2092992 0.312187495 0.28605104
## [2,] 0.35432362 0.3243879 0.009098359 0.38547910
## [3,] 0.08322923 0.1991026 0.084821880 0.01574486
## 
## , , 3, 3, 3
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.7926426 0.3806466 0.56776676 0.52023312
## [2,] 0.6443986 0.5899553 0.01654693 0.70106017
## [3,] 0.1513667 0.3621024 0.15426321 0.02863475
## 
## , , 1, 4, 3
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.50150831 0.2408367 0.35922843 0.32915370
## [2,] 0.40771370 0.3732672 0.01046931 0.44356374
## [3,] 0.09577035 0.2291037 0.09760298 0.01811733
## 
## , , 2, 4, 3
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.48355210 0.2322137 0.34636647 0.31736854
## [2,] 0.39311575 0.3599026 0.01009447 0.42768220
## [3,] 0.09234135 0.2209008 0.09410837 0.01746865
## 
## , , 3, 4, 3
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.9351223 0.4490689 0.66982441 0.61374646
## [2,] 0.7602310 0.6960014 0.01952129 0.82707768
## [3,] 0.1785753 0.4271913 0.18199245 0.03378192
## 
## , , 1, 1, 4
## 
##            [,1]      [,2]        [,3]        [,4]
## [1,] 0.23741616 0.1140131 0.170060262 0.155822755
## [2,] 0.19301339 0.1767063 0.004956218 0.209984952
## [3,] 0.04533809 0.1084587 0.046205666 0.008576819
## 
## , , 2, 1, 4
## 
##             [,1]        [,2]         [,3]         [,4]
## [1,] 0.009224255 0.004429716 0.0066072978 0.0060541324
## [2,] 0.007499089 0.006865514 0.0001925624 0.0081584793
## [3,] 0.001761506 0.004213910 0.0017952142 0.0003332324
## 
## , , 3, 1, 4
## 
##            [,1]      [,2]        [,3]       [,4]
## [1,] 0.30407401 0.1460239 0.217807019 0.19957214
## [2,] 0.24720456 0.2263190 0.006347744 0.26894111
## [3,] 0.05806738 0.1389099 0.059178542 0.01098488
## 
## , , 1, 2, 4
## 
##            [,1]      [,2]        [,3]       [,4]
## [1,] 0.45661971 0.2192801 0.327074903 0.29969207
## [2,] 0.37122039 0.3398571 0.009532236 0.40386159
## [3,] 0.08719821 0.2085973 0.088866815 0.01649569
## 
## , , 2, 2, 4
## 
##            [,1]      [,2]        [,3]       [,4]
## [1,] 0.35091333 0.1685173 0.251357843 0.23031407
## [2,] 0.28528376 0.2611810 0.007325546 0.31036860
## [3,] 0.06701203 0.1603075 0.068294359 0.01267698
## 
## , , 3, 2, 4
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.51200401 0.2458770 0.36674646 0.33604232
## [2,] 0.41624644 0.3810791 0.01068842 0.45284676
## [3,] 0.09777465 0.2338985 0.09964565 0.01849649
## 
## , , 1, 3, 4
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.7845839 0.3767767 0.5619944 0.51494400
## [2,] 0.6378471 0.5839573 0.0163787 0.69393262
## [3,] 0.1498278 0.3584209 0.1526948 0.02834362
## 
## , , 2, 3, 4
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.7623028 0.3660768 0.54603453 0.5003203
## [2,] 0.6197331 0.5673738 0.01591357 0.6742259
## [3,] 0.1455729 0.3482423 0.14835852 0.0275387
## 
## , , 3, 3, 4
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.6114565 0.2936366 0.43798391 0.40131574
## [2,] 0.4970989 0.4551005 0.01276456 0.54080848
## [3,] 0.1167666 0.2793313 0.11900098 0.02208928
## 
## , , 1, 4, 4
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.6949532 0.3337338 0.4977922 0.45611687
## [2,] 0.5649795 0.5172461 0.0145076 0.61465785
## [3,] 0.1327115 0.3174750 0.1352510 0.02510565
## 
## , , 2, 4, 4
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.8056876 0.3869112 0.57711085 0.52879492
## [2,] 0.6550038 0.5996646 0.01681926 0.71259795
## [3,] 0.1538578 0.3680617 0.15680201 0.02910601
## 
## , , 3, 4, 4
## 
##            [,1]      [,2]        [,3]       [,4]
## [1,] 0.31662559 0.1520515 0.226797664 0.20781008
## [2,] 0.25740867 0.2356610 0.006609767 0.28004247
## [3,] 0.06046429 0.1446439 0.061621316 0.01143831
## 
## , , 1, 1, 5
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.6758903 0.3245793 0.48413754 0.44360537
## [2,] 0.5494819 0.5030578 0.01410965 0.59779750
## [3,] 0.1290711 0.3087665 0.13154100 0.02441699
## 
## , , 2, 1, 5
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.9013839 0.4328669 0.64565777 0.5916031
## [2,] 0.7328026 0.6708903 0.01881698 0.7972375
## [3,] 0.1721324 0.4117786 0.17542633 0.0325631
## 
## , , 3, 1, 5
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.6894490 0.3310905 0.4938496 0.45250435
## [2,] 0.5605048 0.5131495 0.0143927 0.60978966
## [3,] 0.1316604 0.3149605 0.1341798 0.02490681
## 
## , , 1, 2, 5
## 
##            [,1]       [,2]         [,3]        [,4]
## [1,] 0.03574139 0.01716390 0.0256014139 0.023458054
## [2,] 0.02905685 0.02660193 0.0007461248 0.031611804
## [3,] 0.00682534 0.01632771 0.0069559483 0.001291182
## 
## , , 2, 2, 5
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.6932791 0.3329298 0.49659309 0.45501813
## [2,] 0.5636185 0.5160001 0.01447266 0.61317720
## [3,] 0.1323918 0.3167102 0.13492520 0.02504517
## 
## , , 3, 2, 5
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.8902802 0.4275347 0.63770421 0.58431538
## [2,] 0.7237755 0.6626259 0.01858518 0.78741668
## [3,] 0.1700120 0.4067061 0.17326533 0.03216197
## 
## , , 1, 3, 5
## 
##            [,1]       [,2]        [,3]        [,4]
## [1,] 0.14760461 0.07088340 0.105728605 0.096876968
## [2,] 0.11999886 0.10986052 0.003081343 0.130550287
## [3,] 0.02818726 0.06743012 0.028726644 0.005332316
## 
## , , 2, 3, 5
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.8391178 0.4029652 0.60105680 0.5507361
## [2,] 0.6821818 0.6245463 0.01751713 0.7421656
## [3,] 0.1602418 0.3833336 0.16330817 0.0303137
## 
## , , 3, 3, 5
## 
##            [,1]       [,2]        [,3]        [,4]
## [1,] 0.13087610 0.06284995 0.093746039 0.085897587
## [2,] 0.10639900 0.09740967 0.002732124 0.115754600
## [3,] 0.02499271 0.05978804 0.025470960 0.004727988
## 
## , , 1, 4, 5
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.6828134 0.3279039 0.48909655 0.44814921
## [2,] 0.5551102 0.5082106 0.01425418 0.60392072
## [3,] 0.1303932 0.3119292 0.13288838 0.02466709
## 
## , , 2, 4, 5
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.6131438 0.2944469 0.43919247 0.40242311
## [2,] 0.4984705 0.4563563 0.01279978 0.54230076
## [3,] 0.1170888 0.2801021 0.11932935 0.02215023
## 
## , , 3, 4, 5
## 
##            [,1]       [,2]        [,3]        [,4]
## [1,] 0.14400481 0.06915469 0.103150081 0.094514319
## [2,] 0.11707231 0.10718123 0.003006195 0.127366409
## [3,] 0.02749983 0.06578562 0.028026055 0.005202271
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.222220195 0.106715655 0.159175453 0.145849226
## [2,] 0.180659455 0.165396095 0.004638992 0.196544739
## [3,] 0.042436196 0.101516703 0.043248245 0.008027854
## 
## ,,2,1,1
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.326398234 0.156744536 0.233797772 0.214224137
## [2,] 0.265353592 0.242934685 0.006813777 0.288685984
## [3,] 0.062330516 0.149108287 0.063523258 0.011791356
## 
## ,,3,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.52343796 0.25136790 0.37493655 0.34354673
## [2,] 0.42554195 0.38958923 0.01092711 0.46295962
## [3,] 0.09995813 0.23912181 0.10187091 0.01890955
## 
## ...
## 
## ,,1,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.68281340 0.32790395 0.48909655 0.44814921
## [2,] 0.55511020 0.50821065 0.01425418 0.60392072
## [3,] 0.13039320 0.31192919 0.13288838 0.02466709
## 
## ,,2,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.61314376 0.29444686 0.43919247 0.40242311
## [2,] 0.49847053 0.45635629 0.01279978 0.54230076
## [3,] 0.11708877 0.28010205 0.11932935 0.02215023
## 
## ,,3,4,5
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.144004812 0.069154686 0.103150081 0.094514319
## [2,] 0.117072307 0.107181229 0.003006195 0.127366409
## [3,] 0.027499825 0.065785622 0.028026055 0.005202271

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

Row-wise / Column-wise Summation

einsum::einsum('ij->i', arrC)
## [1] 2.7122849 2.3412638 0.8352518
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 2.7122849 2.3412638 0.8352518
einsum::einsum('ij->j', arrC)
## [1] 1.9052029 1.5985015 0.8858801 1.4992161
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
##       [1]       [2]       [3]       [4] 
## 1.9052029 1.5985015 0.8858801 1.4992161

Mode-wise Vectorization

einsum::einsum('ijk->i', arrE)
## [1]  8.518526 12.334151  9.887470
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
##  8.518526 12.334151  9.887470
einsum::einsum('ijk->j', arrE)
## [1] 6.380971 8.000119 8.759851 7.599205
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 6.380971 8.000119 8.759851 7.599205
einsum::einsum('ijk->k', arrE)
## [1] 5.655097 5.110798 7.047908 6.148821 6.777521
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4]      [5] 
## 5.655097 5.110798 7.047908 6.148821 6.777521

Mode-wise Summation

These are the same as what the modeSum function does.

einsum::einsum('ijk->ij', arrE)
##          [,1]     [,2]     [,3]     [,4]
## [1,] 2.201570 1.533426 2.367017 2.416512
## [2,] 1.882127 3.452158 3.710917 3.288949
## [3,] 2.297274 3.014535 2.681917 1.893744
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]     [,4]
## [1,] 2.201570 1.533426 2.367017 2.416512
## [2,] 1.882127 3.452158 3.710917 3.288949
## [3,] 2.297274 3.014535 2.681917 1.893744
einsum::einsum('ijk->jk', arrE)
##          [,1]      [,2]     [,3]      [,4]     [,5]
## [1,] 1.127615 0.5946946 1.695210 0.5792551 2.384196
## [2,] 1.883925 1.7061808 1.318871 1.3879217 1.703221
## [3,] 1.359942 1.9400613 2.014131 2.2701989 1.175518
## [4,] 1.283615 0.8698615 2.019696 1.9114457 1.514588
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.1276154 0.5946946 1.6952100 0.5792551 2.3841956
## [2,] 1.8839248 1.7061808 1.3188715 1.3879217 1.7032205
## [3,] 1.3599420 1.9400613 2.0141314 2.2701989 1.1755178
## [4,] 1.2836146 0.8698615 2.0196957 1.9114457 1.5145876
einsum::einsum('ijk->jk', arrE)
##          [,1]      [,2]     [,3]      [,4]     [,5]
## [1,] 1.127615 0.5946946 1.695210 0.5792551 2.384196
## [2,] 1.883925 1.7061808 1.318871 1.3879217 1.703221
## [3,] 1.359942 1.9400613 2.014131 2.2701989 1.175518
## [4,] 1.283615 0.8698615 2.019696 1.9114457 1.514588
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.1276154 0.5946946 1.6952100 0.5792551 2.3841956
## [2,] 1.8839248 1.7061808 1.3188715 1.3879217 1.7032205
## [3,] 1.3599420 1.9400613 2.0141314 2.2701989 1.1755178
## [4,] 1.2836146 0.8698615 2.0196957 1.9114457 1.5145876

Trace

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

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

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.06984382 0.8211048 0.2298736
## [2,] 0.92146121 0.2519830 0.1943777
## [3,] 0.99259774 0.5096894 0.9226150
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.06984382 0.82110478 0.22987355
## [2,] 0.92146121 0.25198299 0.19437770
## [3,] 0.99259774 0.50968937 0.92261500
einsum::einsum('ijk->jki', arrD)
## , , 1
## 
##           [,1]      [,2]      [,3]
## [1,] 0.9649151 0.4829056 0.3728192
## [2,] 0.7993514 0.5304553 0.5119564
## [3,] 0.4734446 0.5470484 0.7237708
## 
## , , 2
## 
##           [,1]      [,2]      [,3]
## [1,] 0.9034563 0.1071731 0.2193893
## [2,] 0.2092330 0.2563305 0.4857721
## [3,] 0.3888711 0.5335921 0.5281922
## 
## , , 3
## 
##           [,1]      [,2]        [,3]
## [1,] 0.7064812 0.3601514 0.864542627
## [2,] 0.6258965 0.1458184 0.777503308
## [3,] 0.6622747 0.5330679 0.009418362
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
##           [,1]      [,2]      [,3]
## [1,] 0.9649151 0.4829056 0.3728192
## [2,] 0.7993514 0.5304553 0.5119564
## [3,] 0.4734446 0.5470484 0.7237708
## 
## ,,2
##           [,1]      [,2]      [,3]
## [1,] 0.9034563 0.1071731 0.2193893
## [2,] 0.2092330 0.2563305 0.4857721
## [3,] 0.3888711 0.5335921 0.5281922
## 
## ,,3
##             [,1]        [,2]        [,3]
## [1,] 0.706481180 0.360151366 0.864542627
## [2,] 0.625896529 0.145818438 0.777503308
## [3,] 0.662274655 0.533067869 0.009418362

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

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.4756190 0.1563646 1.0884909 0.1647472 1.9301796
## [2,] 1.3023014 1.1460843 0.6808727 0.6569307 1.4100378
## [3,] 0.8063167 1.6134618 1.4265082 1.7375622 0.8220452
## [4,] 0.8388549 0.3352097 1.5043802 1.3633858 0.9546753
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.4756190 0.1563646 1.0884909 0.1647472 1.9301796
## [2,] 1.3023014 1.1460843 0.6808727 0.6569307 1.4100378
## [3,] 0.8063167 1.6134618 1.4265082 1.7375622 0.8220452
## [4,] 0.8388549 0.3352097 1.5043802 1.3633858 0.9546753

Matrix Multiplication

Matrix Multiplication is considered a contracted product.

einsum::einsum('ij,jk->ik', arrC, t(arrC))
##           [,1]      [,2]      [,3]
## [1,] 1.9654636 1.5961249 0.5183423
## [2,] 1.5961249 1.8056000 0.4802135
## [3,] 0.5183423 0.4802135 0.2570126
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 1.9654636 1.5961249 0.5183423
## [2,] 1.5961249 1.8056000 0.4802135
## [3,] 0.5183423 0.4802135 0.2570126

Multiplication + Permutation

Some examples of combining Multiplication and Permutation are shown below.

einsum::einsum('ij,ij->ji', arrC, arrC)
##           [,1]         [,2]        [,3]
## [1,] 0.9038851 0.5974032456 0.032962420
## [2,] 0.2084503 0.5007220167 0.188634530
## [3,] 0.4637654 0.0003939068 0.034236011
## [4,] 0.3893628 0.7070807818 0.001179628
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
##              [,1]         [,2]         [,3]
## [1,] 0.9038851177 0.5974032456 0.0329624199
## [2,] 0.2084502672 0.5007220167 0.1886345304
## [3,] 0.4637654312 0.0003939068 0.0342360111
## [4,] 0.3893627858 0.7070807818 0.0011796282
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
## 
##             [,1]       [,2]       [,3]       [,4]        [,5]
## [1,] 0.054632845 0.06138869 0.57673100 0.06236017 0.505404554
## [2,] 0.322310122 0.07032738 0.03335441 0.23067263 0.001413284
## [3,] 0.252864771 0.02613862 0.52126578 0.68102894 0.024103862
## [4,] 0.006893862 0.12731673 0.27825504 0.53431558 0.515811274
## 
## , , 2
## 
##           [,1]       [,2]       [,3]         [,4]      [,5]
## [1,] 0.1178643 0.09324863 0.07598089 9.413462e-05 0.8988897
## [2,] 0.8039566 0.73528336 0.35967333 1.362343e-01 0.5317444
## [3,] 0.5382102 0.69631045 0.21015157 6.428977e-01 0.7789914
## [4,] 0.6991488 0.20414471 0.25868623 7.181582e-01 0.4159215
## 
## , , 3
## 
##           [,1]        [,2]      [,3]      [,4]       [,5]
## [1,] 0.3031218 0.001727302 0.4357790 0.1022929 0.52588536
## [2,] 0.1760347 0.340473514 0.2878450 0.2900237 0.87688005
## [3,] 0.0152417 0.891012719 0.6950908 0.4136356 0.01894992
## [4,] 0.1328122 0.003748264 0.9674389 0.1109121 0.02294250
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.054632845 0.061388691 0.576731001 0.062360174 0.505404554
## [2,] 0.322310122 0.070327378 0.033354411 0.230672631 0.001413284
## [3,] 0.252864771 0.026138622 0.521265776 0.681028943 0.024103862
## [4,] 0.006893862 0.127316731 0.278255039 0.534315575 0.515811274
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 1.178643e-01 9.324863e-02 7.598089e-02 9.413462e-05 8.988897e-01
## [2,] 8.039566e-01 7.352834e-01 3.596733e-01 1.362343e-01 5.317444e-01
## [3,] 5.382102e-01 6.963105e-01 2.101516e-01 6.428977e-01 7.789914e-01
## [4,] 6.991488e-01 2.041447e-01 2.586862e-01 7.181582e-01 4.159215e-01
## 
## ,,3
##             [,1]        [,2]        [,3]        [,4]        [,5]
## [1,] 0.303121813 0.001727302 0.435778992 0.102292871 0.525885359
## [2,] 0.176034668 0.340473514 0.287844976 0.290023705 0.876880050
## [3,] 0.015241703 0.891012719 0.695090816 0.413635633 0.018949925
## [4,] 0.132812222 0.003748264 0.967438945 0.110912062 0.022942502

Summation + Permutation

Some examples of combining Summation and Permutation are shown below.

einsum::einsum('ijk->ki', arrE)
##          [,1]     [,2]     [,3]
## [1,] 1.387346 2.809729 1.458021
## [2,] 1.031450 2.449129 1.630220
## [3,] 2.191546 1.842409 3.013953
## [4,] 2.286218 2.028053 1.834551
## [5,] 1.621966 3.204831 1.950725
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]
## [1,] 1.387346 2.809729 1.458021
## [2,] 1.031450 2.449129 1.630220
## [3,] 2.191546 1.842409 3.013953
## [4,] 2.286218 2.028053 1.834551
## [5,] 1.621966 3.204831 1.950725

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.0133291889 0.014977464 0.140709430 0.01521448 1.233074e-01
## [2,] 0.0181348127 0.003956977 0.001876689 0.01297882 7.951859e-05
## [3,] 0.0316536193 0.003272033 0.065252065 0.08525122 3.017322e-03
## [4,] 0.0007245256 0.013380631 0.029243823 0.05615507 5.421032e-02
## 
## , , 2
## 
##             [,1]         [,2]         [,3]         [,4]        [,5]
## [1,] 0.061116063 0.0483521140 3.939829e-02 4.881153e-05 0.466100316
## [2,] 0.349409506 0.3195632684 1.563185e-01 5.920912e-02 0.231102733
## [3,] 0.000184014 0.0002380684 7.185079e-05 2.198066e-04 0.000266337
## [4,] 0.429085747 0.1252889003 1.587624e-01 4.407523e-01 0.255261816
## 
## , , 3
## 
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 6.603641e-04 3.763003e-06 0.0009493636 2.228495e-04 1.145664e-03
## [2,] 2.194657e-03 4.244746e-03 0.0035886163 3.615779e-03 1.093223e-02
## [3,] 3.448767e-05 2.016110e-03 0.0015727942 9.359406e-04 4.287833e-05
## [4,] 1.035453e-05 2.922285e-07 0.0000754251 8.647113e-06 1.788682e-06
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,] 1.332919e-02 1.497746e-02 1.407094e-01 1.521448e-02 1.233074e-01
## [2,] 1.813481e-02 3.956977e-03 1.876689e-03 1.297882e-02 7.951859e-05
## [3,] 3.165362e-02 3.272033e-03 6.525206e-02 8.525122e-02 3.017322e-03
## [4,] 7.245256e-04 1.338063e-02 2.924382e-02 5.615507e-02 5.421032e-02
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 6.111606e-02 4.835211e-02 3.939829e-02 4.881153e-05 4.661003e-01
## [2,] 3.494095e-01 3.195633e-01 1.563185e-01 5.920912e-02 2.311027e-01
## [3,] 1.840140e-04 2.380684e-04 7.185079e-05 2.198066e-04 2.663370e-04
## [4,] 4.290857e-01 1.252889e-01 1.587624e-01 4.407523e-01 2.552618e-01
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 6.603641e-04 3.763003e-06 9.493636e-04 2.228495e-04 1.145664e-03
## [2,] 2.194657e-03 4.244746e-03 3.588616e-03 3.615779e-03 1.093223e-02
## [3,] 3.448767e-05 2.016110e-03 1.572794e-03 9.359406e-04 4.287833e-05
## [4,] 1.035453e-05 2.922285e-07 7.542510e-05 8.647113e-06 1.788682e-06

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 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.1.2              DelayedRandomArray_1.21.0
##  [3] HDF5Array_1.41.0          h5mread_1.5.0            
##  [5] rhdf5_2.57.1              DelayedArray_0.39.3      
##  [7] SparseArray_1.13.2        S4Arrays_1.13.0          
##  [9] abind_1.4-8               IRanges_2.47.2           
## [11] S4Vectors_0.51.5          MatrixGenerics_1.25.0    
## [13] matrixStats_1.5.0         BiocGenerics_0.59.8      
## [15] generics_0.1.4            Matrix_1.7-5             
## [17] DelayedTensor_1.19.0      BiocStyle_2.41.0         
## 
## loaded via a namespace (and not attached):
##  [1] dqrng_0.4.1         sass_0.4.10         lattice_0.22-9     
##  [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.2.0         XVector_0.53.0      cachem_1.1.0       
## [16] yaml_2.3.12         otel_0.2.0          tools_4.6.1        
## [19] beachmat_2.29.0     parallel_4.6.1      BiocParallel_1.47.0
## [22] Rhdf5lib_2.1.0      rsvd_1.0.5          buildtools_1.0.0   
## [25] R6_2.6.1            lifecycle_1.0.5     BiocSingular_1.29.0
## [28] irlba_2.3.7         ScaledMatrix_1.21.0 rTensor_1.5.0      
## [31] bslib_0.11.0        Rcpp_1.1.1-1.1      xfun_0.59          
## [34] sys_3.4.3           knitr_1.51          rhdf5filters_1.25.0
## [37] htmltools_0.5.9     rmarkdown_2.31      maketools_1.3.2    
## [40] compiler_4.6.1