Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2025-04-15 15:16:31.230983
Compiled: Tue Apr 15 18:09:59 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.06069284 0.25288415 0.73022772
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
##        [1]        [2]        [3] 
## 0.06069284 0.25288415 0.73022772
einsum::einsum('iii->i', arrD)
## [1] 0.1765367 0.5950856 0.8940914
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.1765367 0.5950856 0.8940914

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.07704605 0.10918153 0.29595504
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
##        [1]        [2]        [3] 
## 0.07704605 0.10918153 0.29595504
einsum::einsum('ij,ij->ij', arrC, arrC)
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.06649487 0.03829802 0.2090746 0.62292755
## [2,] 0.64033113 0.36491696 0.1547516 0.04184106
## [3,] 0.23794107 0.45191493 0.3038045 0.14081255
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.06649487 0.03829802 0.20907465 0.62292755
## [2,] 0.64033113 0.36491696 0.15475155 0.04184106
## [3,] 0.23794107 0.45191493 0.30380447 0.14081255
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.02416239 0.4044326 0.7688279 0.13952609
## [2,] 0.02270761 0.6329686 0.6150065 0.02478527
## [3,] 0.13877490 0.5582126 0.7203108 0.07069093
## 
## , , 2
## 
##            [,1]      [,2]       [,3]      [,4]
## [1,] 0.32564197 0.1726612 0.03170875 0.1373284
## [2,] 0.19084119 0.1154033 0.05974271 0.2418514
## [3,] 0.04953964 0.1133054 0.37894355 0.7443683
## 
## , , 3
## 
##              [,1]         [,2]        [,3]         [,4]
## [1,] 0.8187318719 2.987619e-01 0.581433727 0.0004421427
## [2,] 0.0003065689 6.750267e-01 0.003512518 0.2762386296
## [3,] 0.7063421303 1.774016e-06 0.713009992 0.0503153214
## 
## , , 4
## 
##           [,1]      [,2]      [,3]        [,4]
## [1,] 0.6204730 0.1657464 0.4955939 0.087357927
## [2,] 0.1970712 0.3857979 0.5118365 0.202830289
## [3,] 0.2005848 0.2303923 0.7997802 0.005921693
## 
## , , 5
## 
##            [,1]       [,2]         [,3]      [,4]
## [1,] 0.02239586 0.17689301 0.0005421553 0.4600547
## [2,] 0.86040209 0.11445206 0.9309201924 0.1202748
## [3,] 0.06658372 0.01083404 0.2671739765 0.3070143
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02416239 0.40443263 0.76882785 0.13952609
## [2,] 0.02270761 0.63296865 0.61500646 0.02478527
## [3,] 0.13877490 0.55821258 0.72031076 0.07069093
## 
## ,,2
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.32564197 0.17266116 0.03170875 0.13732838
## [2,] 0.19084119 0.11540329 0.05974271 0.24185143
## [3,] 0.04953964 0.11330543 0.37894355 0.74436832
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]
## [1,] 8.187319e-01 2.987619e-01 5.814337e-01 4.421427e-04
## [2,] 3.065689e-04 6.750267e-01 3.512518e-03 2.762386e-01
## [3,] 7.063421e-01 1.774016e-06 7.130100e-01 5.031532e-02
## 
## ,,4
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.620473042 0.165746368 0.495593926 0.087357927
## [2,] 0.197071237 0.385797867 0.511836482 0.202830289
## [3,] 0.200584809 0.230392344 0.799780170 0.005921693
## 
## ,,5
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.0223958620 0.1768930086 0.0005421553 0.4600547083
## [2,] 0.8604020896 0.1144520582 0.9309201924 0.1202748459
## [3,] 0.0665837178 0.0108340374 0.2671739765 0.3070142605

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.07704605 0.09171698 0.1510039
## [2,] 0.09171698 0.10918153 0.1797577
## [3,] 0.15100386 0.17975768 0.2959550
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.07704605 0.09171698 0.15100386
## [2,] 0.09171698 0.10918153 0.17975768
## [3,] 0.15100386 0.17975768 0.29595504
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.04008335 0.03041992 0.07107561 0.12268421
## [2,] 0.12438620 0.09390029 0.06114873 0.03179591
## [3,] 0.07582364 0.10449566 0.08567754 0.05832981
## 
## , , 2, 1, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.03885794 0.02948994 0.06890272 0.11893357
## [2,] 0.12058353 0.09102962 0.05927932 0.03082386
## [3,] 0.07350559 0.10130107 0.08305825 0.05654658
## 
## , , 3, 1, 1
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.09606154 0.0729027 0.1703359 0.29401821
## [2,] 0.29809711 0.2250363 0.1465457 0.07620032
## [3,] 0.18171475 0.2504285 0.2053301 0.13979001
## 
## , , 1, 2, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1639899 0.1244547 0.2907862 0.5019285
## [2,] 0.5088917 0.3841671 0.2501731 0.1300842
## [3,] 0.3102114 0.4275151 0.3505260 0.2386403
## 
## , , 2, 2, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.2051565 0.1556966 0.3637825 0.6279280
## [2,] 0.6366392 0.4806048 0.3129742 0.1627393
## [3,] 0.3880841 0.5348345 0.4385188 0.2985464
## 
## , , 3, 2, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1926610 0.1462137 0.3416257 0.5896830
## [2,] 0.5978636 0.4513327 0.2939120 0.1528274
## [3,] 0.3644471 0.5022595 0.4118100 0.2803629
## 
## , , 1, 3, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.2261042 0.1715942 0.4009269 0.6920434
## [2,] 0.7016441 0.5296776 0.3449309 0.1793560
## [3,] 0.4277099 0.5894445 0.4832943 0.3290298
## 
## , , 2, 3, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.2022246 0.1534716 0.3585837 0.6189543
## [2,] 0.6275411 0.4737365 0.3085015 0.1604136
## [3,] 0.3825380 0.5271912 0.4322519 0.2942798
## 
## , , 3, 3, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.2188538 0.1660918 0.3880705 0.6698518
## [2,] 0.6791446 0.5126925 0.3338700 0.1736046
## [3,] 0.4139946 0.5705429 0.4677966 0.3184789
## 
## , , 1, 4, 1
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.09632118 0.07309975 0.1707963 0.29481290
## [2,] 0.29890283 0.22564450 0.1469418 0.07640628
## [3,] 0.18220589 0.25110540 0.2058850 0.14016784
## 
## , , 2, 4, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.04059672 0.03080953 0.07198592 0.12425550
## [2,] 0.12597929 0.09510292 0.06193189 0.03220314
## [3,] 0.07679475 0.10583399 0.08677486 0.05907688
## 
## , , 3, 4, 1
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.06856081 0.05203194 0.1215717 0.20984597
## [2,] 0.21275715 0.16061233 0.1045922 0.05438551
## [3,] 0.12969301 0.17873525 0.1465477 0.09977059
## 
## , , 1, 1, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1471514 0.1116756 0.2609281 0.4503902
## [2,] 0.4566385 0.3447206 0.2244852 0.1167271
## [3,] 0.2783588 0.3836176 0.3145338 0.2141366
## 
## , , 2, 1, 2
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.1126497 0.08549175 0.1997500 0.34479013
## [2,] 0.3495734 0.26389617 0.1718516 0.08935882
## [3,] 0.2130938 0.29367326 0.2407871 0.16392936
## 
## , , 3, 1, 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.05739453 0.04355767 0.10177172 0.17566903
## [2,] 0.17810608 0.13445392 0.08755762 0.04552792
## [3,] 0.10857033 0.14962521 0.12267993 0.08352128
## 
## , , 1, 2, 2
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.1071498 0.08131778 0.1899976 0.32795639
## [2,] 0.3325061 0.25101193 0.1634613 0.08499604
## [3,] 0.2026899 0.27933520 0.2290311 0.15592581
## 
## , , 2, 2, 2
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.08759981 0.06648096 0.1553316 0.2681192
## [2,] 0.27183877 0.20521359 0.1336370 0.0694881
## [3,] 0.16570812 0.22836915 0.1872433 0.1274764
## 
## , , 3, 2, 2
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.08679994 0.06587392 0.1539133 0.26567099
## [2,] 0.26935663 0.20333980 0.1324167 0.06885361
## [3,] 0.16419506 0.22628393 0.1855335 0.12631242
## 
## , , 1, 3, 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.04591807 0.03484799 0.08142172 0.14054272
## [2,] 0.14249246 0.10756887 0.07004983 0.03642428
## [3,] 0.08686089 0.11970655 0.09814918 0.06682058
## 
## , , 2, 3, 2
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.06302844 0.04783333 0.11176173 0.19291288
## [2,] 0.19558915 0.14765205 0.09615236 0.04999698
## [3,] 0.11922770 0.16431257 0.13472231 0.09171980
## 
## , , 3, 3, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1587382 0.1204690 0.2814738 0.4858543
## [2,] 0.4925945 0.3718641 0.2421613 0.1259182
## [3,] 0.3002769 0.4138239 0.3393004 0.2309978
## 
## , , 1, 4, 2
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.09555958 0.07252176 0.1694458 0.29248185
## [2,] 0.29653943 0.22386035 0.1457799 0.07580215
## [3,] 0.18076521 0.24911994 0.2042571 0.13905955
## 
## , , 2, 4, 2
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.1268144 0.09624152 0.2248666 0.3881442
## [2,] 0.3935289 0.29707859 0.1934603 0.1005948
## [3,] 0.2398883 0.33059986 0.2710637 0.1845419
## 
## , , 3, 4, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.2224785 0.1688426 0.3944978 0.6809461
## [2,] 0.6903928 0.5211839 0.3393997 0.1764799
## [3,] 0.4208513 0.5799924 0.4755443 0.3237536
## 
## , , 1, 1, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.2333270 0.1770757 0.4137343 0.7141503
## [2,] 0.7240577 0.5465978 0.3559495 0.1850854
## [3,] 0.4413728 0.6082739 0.4987328 0.3395405
## 
## , , 2, 1, 3
## 
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.004515004 0.003426512 0.008005984 0.013819197
## [2,] 0.014010910 0.010576965 0.006887816 0.003581504
## [3,] 0.008540804 0.011770431 0.009650751 0.006570293
## 
## , , 3, 1, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.2167213 0.1644734 0.3842893 0.6633249
## [2,] 0.6725272 0.5076970 0.3306169 0.1719131
## [3,] 0.4099607 0.5649837 0.4632385 0.3153757
## 
## , , 1, 2, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1409473 0.1069672 0.2499271 0.4314013
## [2,] 0.4373861 0.3301868 0.2150206 0.1118057
## [3,] 0.2666228 0.3674438 0.3012727 0.2051083
## 
## , , 2, 2, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.2118627 0.1607861 0.3756740 0.6484541
## [2,] 0.6574501 0.4963151 0.3232049 0.1680590
## [3,] 0.4007700 0.5523175 0.4528533 0.3083054
## 
## , , 3, 2, 3
## 
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.0003434574 0.0002606555 0.0006090170 0.0010512295
## [2,] 0.0010658131 0.0008045921 0.0005239577 0.0002724458
## [3,] 0.0006497009 0.0008953794 0.0007341349 0.0004998037
## 
## , , 1, 3, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1966275 0.1492239 0.3486589 0.6018231
## [2,] 0.6101722 0.4606246 0.2999630 0.1559737
## [3,] 0.3719502 0.5125998 0.4202882 0.2861349
## 
## , , 2, 3, 3
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01528281 0.01159838 0.02709942 0.04677653
## [2,] 0.04742546 0.03580192 0.02331454 0.01212301
## [3,] 0.02890973 0.03984168 0.03266678 0.02223975
## 
## , , 3, 3, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.2177418 0.1652479 0.3860988 0.6664485
## [2,] 0.6756941 0.5100877 0.3321738 0.1727226
## [3,] 0.4118912 0.5676441 0.4654198 0.3168608
## 
## , , 1, 4, 3
## 
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.005422197 0.004114996 0.009614615 0.016595868
## [2,] 0.016826102 0.012702179 0.008271775 0.004301130
## [3,] 0.010256895 0.014135447 0.011589863 0.007890452
## 
## , , 2, 4, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1355303 0.1028562 0.2403216 0.4148212
## [2,] 0.4205760 0.3174967 0.2067568 0.1075087
## [3,] 0.2563757 0.3533219 0.2896939 0.1972254
## 
## , , 3, 4, 3
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.05784212 0.04389735 0.10256538 0.17703898
## [2,] 0.17949503 0.13550245 0.08824043 0.04588297
## [3,] 0.10941701 0.15079206 0.12363664 0.08417261
## 
## , , 1, 1, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.2031213 0.1541522 0.3601738 0.6216991
## [2,] 0.6303239 0.4758373 0.3098696 0.1611250
## [3,] 0.3842343 0.5295291 0.4341687 0.2955848
## 
## , , 2, 1, 4
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.1144737 0.08687599 0.2029842 0.35037281
## [2,] 0.3552335 0.26816905 0.1746341 0.09080567
## [3,] 0.2165441 0.29842827 0.2446858 0.16658362
## 
## , , 3, 1, 4
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.1154897 0.08764703 0.2047857 0.35348240
## [2,] 0.3583862 0.27054907 0.1761840 0.09161158
## [3,] 0.2184659 0.30107685 0.2468574 0.16806206
## 
## , , 1, 2, 4
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.1049823 0.07967282 0.1861541 0.32132224
## [2,] 0.3257799 0.24593426 0.1601546 0.08327667
## [3,] 0.1985897 0.27368460 0.2243981 0.15277162
## 
## , , 2, 2, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1601673 0.1215537 0.2840080 0.4902286
## [2,] 0.4970296 0.3752122 0.2443416 0.1270519
## [3,] 0.3029805 0.4175498 0.3423553 0.2330776
## 
## , , 3, 2, 4
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.1237736 0.09393386 0.2194748 0.37883735
## [2,] 0.3840929 0.28995530 0.1888215 0.09818279
## [3,] 0.2341363 0.32267281 0.2645642 0.18011700
## 
## , , 1, 3, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1815336 0.1377689 0.3218946 0.5556250
## [2,] 0.5633331 0.4252654 0.2769367 0.1440006
## [3,] 0.3433979 0.4732508 0.3880253 0.2641701
## 
## , , 2, 3, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1844844 0.1400083 0.3271269 0.5646566
## [2,] 0.5724900 0.4321780 0.2814383 0.1463413
## [3,] 0.3489798 0.4809434 0.3943326 0.2684641
## 
## , , 3, 3, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.2306107 0.1750143 0.4089178 0.7058365
## [2,] 0.7156285 0.5402345 0.3518057 0.1829307
## [3,] 0.4362345 0.6011926 0.4929268 0.3355877
## 
## , , 1, 4, 4
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.07621584 0.05784147 0.1351456 0.23327593
## [2,] 0.23651216 0.17854520 0.1162703 0.06045782
## [3,] 0.14417364 0.19869160 0.1629102 0.11091029
## 
## , , 2, 4, 4
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.1161343 0.08813625 0.2059288 0.35545545
## [2,] 0.3603867 0.27205921 0.1771674 0.09212293
## [3,] 0.2196854 0.30275739 0.2482353 0.16900015
## 
## , , 3, 4, 4
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01984344 0.01505952 0.03518630 0.06073537
## [2,] 0.06157795 0.04648576 0.03027195 0.01574071
## [3,] 0.03753683 0.05173105 0.04241505 0.02887644
## 
## , , 1, 1, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.03859028 0.02928681 0.06842812 0.11811435
## [2,] 0.11975294 0.09040260 0.05887100 0.03061154
## [3,] 0.07299928 0.10060330 0.08248614 0.05615709
## 
## , , 2, 1, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.2391910 0.1815260 0.4241324 0.7320985
## [2,] 0.7422548 0.5603350 0.3648953 0.1897370
## [3,] 0.4524655 0.6235612 0.5112671 0.3480739
## 
## , , 3, 1, 5
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.06653928 0.05049777 0.1179871 0.20365862
## [2,] 0.20648396 0.15587664 0.1015083 0.05278194
## [3,] 0.12586898 0.17346520 0.1422267 0.09682883
## 
## , , 1, 2, 5
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.1084550 0.08230827 0.1923118 0.33195109
## [2,] 0.3365562 0.25406940 0.1654523 0.08603134
## [3,] 0.2051587 0.28273767 0.2318208 0.15782508
## 
## , , 2, 2, 5
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.08723804 0.0662064 0.1546901 0.26701187
## [2,] 0.27071612 0.2043661 0.1330851 0.06920113
## [3,] 0.16502377 0.2274260 0.1864700 0.12694993
## 
## , , 3, 2, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02684042 0.02036964 0.04759330 0.08215120
## [2,] 0.08329088 0.06287705 0.04094611 0.02129102
## [3,] 0.05077265 0.06997187 0.05737098 0.03905853
## 
## , , 1, 3, 5
## 
##             [,1]        [,2]       [,3]        [,4]
## [1,] 0.006004211 0.004556696 0.01064664 0.018377255
## [2,] 0.018632201 0.014065620 0.00915966 0.004762810
## [3,] 0.011357861 0.015652734 0.01283391 0.008737406
## 
## , , 2, 3, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.2488000 0.1888184 0.4411710 0.7615089
## [2,] 0.7720733 0.5828452 0.3795541 0.1973593
## [3,] 0.4706423 0.6486114 0.5318061 0.3620570
## 
## , , 3, 3, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1332880 0.1011545 0.2363457 0.4079584
## [2,] 0.4136180 0.3122440 0.2033361 0.1057300
## [3,] 0.2521342 0.3474765 0.2849011 0.1939625
## 
## , , 1, 4, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1749036 0.1327373 0.3101383 0.5353324
## [2,] 0.5427590 0.4097338 0.2668224 0.1387414
## [3,] 0.3308563 0.4559667 0.3738538 0.2545221
## 
## , , 2, 4, 5
## 
##            [,1]       [,2]      [,3]      [,4]
## [1,] 0.08942964 0.06786964 0.1585762 0.2737198
## [2,] 0.27751708 0.20950019 0.1364284 0.0709396
## [3,] 0.16916952 0.23313944 0.1911545 0.1301392
## 
## , , 3, 4, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1428806 0.1084345 0.2533553 0.4373187
## [2,] 0.4433856 0.3347159 0.2179700 0.1133393
## [3,] 0.2702800 0.3724840 0.3054052 0.2079218
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.04008335 0.03041992 0.07107561 0.12268421
## [2,] 0.12438620 0.09390029 0.06114873 0.03179591
## [3,] 0.07582364 0.10449566 0.08567754 0.05832981
## 
## ,,2,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.03885794 0.02948994 0.06890272 0.11893357
## [2,] 0.12058353 0.09102962 0.05927932 0.03082386
## [3,] 0.07350559 0.10130107 0.08305825 0.05654658
## 
## ,,3,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.09606154 0.07290270 0.17033588 0.29401821
## [2,] 0.29809711 0.22503625 0.14654566 0.07620032
## [3,] 0.18171475 0.25042853 0.20533007 0.13979001
## 
## ...
## 
## ,,1,4,5
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1749036 0.1327373 0.3101383 0.5353324
## [2,] 0.5427590 0.4097338 0.2668224 0.1387414
## [3,] 0.3308563 0.4559667 0.3738538 0.2545221
## 
## ,,2,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.08942964 0.06786964 0.15857623 0.27371977
## [2,] 0.27751708 0.20950019 0.13642844 0.07093960
## [3,] 0.16916952 0.23313944 0.19115448 0.13013918
## 
## ,,3,4,5
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.1428806 0.1084345 0.2533553 0.4373187
## [2,] 0.4433856 0.3347159 0.2179700 0.1133393
## [3,] 0.2702800 0.3724840 0.3054052 0.2079218

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

3.3.1 Row-wise / Column-wise Summation

einsum::einsum('ij->i', arrC)
## [1] 1.700069 2.002226 2.086473
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
##      [1]      [2]      [3] 
## 1.700069 2.002226 2.086473
einsum::einsum('ij->j', arrC)
## [1] 1.545865 1.472029 1.401816 1.369058
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 1.545865 1.472029 1.401816 1.369058

3.3.2 Mode-wise Vectorization

einsum::einsum('ijk->i', arrE)
## [1] 9.277716 9.633054 9.514487
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
##      [1]      [2]      [3] 
## 9.277716 9.633054 9.514487
einsum::einsum('ijk->j', arrE)
## [1] 6.686290 7.011266 9.032754 5.694947
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 6.686290 7.011266 9.032754 5.694947
einsum::einsum('ijk->k', arrE)
## [1] 6.163944 5.085131 5.569418 6.324332 5.282434
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4]      [5] 
## 6.163944 5.085131 5.569418 6.324332 5.282434

3.3.3 Mode-wise Summation

These are the same as what the modeSum function does.

einsum::einsum('ijk->ij', arrE)
##          [,1]     [,2]     [,3]     [,4]
## [1,] 2.568285 2.425773 2.544684 1.738975
## [2,] 1.976559 2.916338 2.768183 1.971975
## [3,] 2.141447 1.669156 3.719887 1.983997
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]     [,4]
## [1,] 2.568285 2.425773 2.544684 1.738975
## [2,] 1.976559 2.916338 2.768183 1.971975
## [3,] 2.141447 1.669156 3.719887 1.983997
einsum::einsum('ijk->jk', arrE)
##          [,1]     [,2]     [,3]      [,4]      [,5]
## [1,] 0.678658 1.230079 1.762789 1.6794951 1.3352693
## [2,] 2.178680 1.091844 1.369523 1.5082379 0.8629808
## [3,] 2.509763 1.038077 1.666184 2.3137161 1.5050150
## [4,] 0.796843 1.725130 0.770922 0.8228831 1.5791686
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.6786580 1.2300792 1.7627888 1.6794951 1.3352693
## [2,] 2.1786797 1.0918445 1.3695232 1.5082379 0.8629808
## [3,] 2.5097630 1.0380766 1.6661837 2.3137161 1.5050150
## [4,] 0.7968430 1.7251302 0.7709220 0.8228831 1.5791686
einsum::einsum('ijk->jk', arrE)
##          [,1]     [,2]     [,3]      [,4]      [,5]
## [1,] 0.678658 1.230079 1.762789 1.6794951 1.3352693
## [2,] 2.178680 1.091844 1.369523 1.5082379 0.8629808
## [3,] 2.509763 1.038077 1.666184 2.3137161 1.5050150
## [4,] 0.796843 1.725130 0.770922 0.8228831 1.5791686
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.6786580 1.2300792 1.7627888 1.6794951 1.3352693
## [2,] 2.1786797 1.0918445 1.3695232 1.5082379 0.8629808
## [3,] 2.5097630 1.0380766 1.6661837 2.3137161 1.5050150
## [4,] 0.7968430 1.7251302 0.7709220 0.8228831 1.5791686

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

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.06069284 0.98948161 0.7708404
## [2,] 0.17783959 0.25288415 0.5908817
## [3,] 0.20251859 0.08485453 0.7302277
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.06069284 0.98948161 0.77084041
## [2,] 0.17783959 0.25288415 0.59088169
## [3,] 0.20251859 0.08485453 0.73022772
einsum::einsum('ijk->jki', arrD)
## , , 1
## 
##           [,1]       [,2]      [,3]
## [1,] 0.1765367 0.06597283 0.6711562
## [2,] 0.5326844 0.08002499 0.3185304
## [3,] 0.4872799 0.59672932 0.4961316
## 
## , , 2
## 
##           [,1]      [,2]        [,3]
## [1,] 0.3789242 0.2098766 0.005088257
## [2,] 0.6009557 0.5950856 0.168318268
## [3,] 0.8518405 0.3183856 0.568113783
## 
## , , 3
## 
##           [,1]       [,2]      [,3]
## [1,] 0.9315400 0.05153787 0.2757881
## [2,] 0.4073338 0.43189277 0.2425296
## [3,] 0.8628276 0.53065500 0.8940914
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
##            [,1]       [,2]       [,3]
## [1,] 0.17653666 0.06597283 0.67115619
## [2,] 0.53268435 0.08002499 0.31853043
## [3,] 0.48727988 0.59672932 0.49613161
## 
## ,,2
##             [,1]        [,2]        [,3]
## [1,] 0.378924191 0.209876608 0.005088257
## [2,] 0.600955720 0.595085575 0.168318268
## [3,] 0.851840539 0.318385611 0.568113783
## 
## ,,3
##            [,1]       [,2]       [,3]
## [1,] 0.93154000 0.05153787 0.27578810
## [2,] 0.40733377 0.43189277 0.24252956
## [3,] 0.86282763 0.53065500 0.89409142

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

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.1856449 0.5660228 1.5253806 1.0181291 0.9493817
## [2,] 1.5956139 0.4013699 0.9737904 0.7819366 0.3021791
## [3,] 2.1041451 0.4703950 1.2979562 1.8072106 1.1986363
## [4,] 0.2350023 1.1235481 0.3269961 0.2961099 0.8873438
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.1856449 0.5660228 1.5253806 1.0181291 0.9493817
## [2,] 1.5956139 0.4013699 0.9737904 0.7819366 0.3021791
## [3,] 2.1041451 0.4703950 1.2979562 1.8072106 1.1986363
## [4,] 0.2350023 1.1235481 0.3269961 0.2961099 0.8873438

3.5.3 Matrix Multiplication

Matrix Multiplication is considered a contracted product.

einsum::einsum('ij,jk->ik', arrC, t(arrC))
##           [,1]      [,2]     [,3]
## [1,] 0.9367951 0.6658818 0.805539
## [2,] 0.6658818 1.2018407 1.090013
## [3,] 0.8055390 1.0900128 1.134473
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 0.9367951 0.6658818 0.8055390
## [2,] 0.6658818 1.2018407 1.0900128
## [3,] 0.8055390 1.0900128 1.1344730

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.06649487 0.64033113 0.2379411
## [2,] 0.03829802 0.36491696 0.4519149
## [3,] 0.20907465 0.15475155 0.3038045
## [4,] 0.62292755 0.04184106 0.1408125
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.06649487 0.64033113 0.23794107
## [2,] 0.03829802 0.36491696 0.45191493
## [3,] 0.20907465 0.15475155 0.30380447
## [4,] 0.62292755 0.04184106 0.14081255
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
## 
##            [,1]       [,2]         [,3]       [,4]         [,5]
## [1,] 0.02416239 0.32564197 0.8187318719 0.62047304 0.0223958620
## [2,] 0.40443263 0.17266116 0.2987619342 0.16574637 0.1768930086
## [3,] 0.76882785 0.03170875 0.5814337274 0.49559393 0.0005421553
## [4,] 0.13952609 0.13732838 0.0004421427 0.08735793 0.4600547083
## 
## , , 2
## 
##            [,1]       [,2]         [,3]      [,4]      [,5]
## [1,] 0.02270761 0.19084119 0.0003065689 0.1970712 0.8604021
## [2,] 0.63296865 0.11540329 0.6750266777 0.3857979 0.1144521
## [3,] 0.61500646 0.05974271 0.0035125180 0.5118365 0.9309202
## [4,] 0.02478527 0.24185143 0.2762386296 0.2028303 0.1202748
## 
## , , 3
## 
##            [,1]       [,2]         [,3]        [,4]       [,5]
## [1,] 0.13877490 0.04953964 7.063421e-01 0.200584809 0.06658372
## [2,] 0.55821258 0.11330543 1.774016e-06 0.230392344 0.01083404
## [3,] 0.72031076 0.37894355 7.130100e-01 0.799780170 0.26717398
## [4,] 0.07069093 0.74436832 5.031532e-02 0.005921693 0.30701426
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.0241623857 0.3256419663 0.8187318719 0.6204730422 0.0223958620
## [2,] 0.4044326326 0.1726611625 0.2987619342 0.1657463680 0.1768930086
## [3,] 0.7688278523 0.0317087527 0.5814337274 0.4955939257 0.0005421553
## [4,] 0.1395260926 0.1373283779 0.0004421427 0.0873579269 0.4600547083
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.0227076065 0.1908411917 0.0003065689 0.1970712368 0.8604020896
## [2,] 0.6329686477 0.1154032911 0.6750266777 0.3857978669 0.1144520582
## [3,] 0.6150064577 0.0597427068 0.0035125180 0.5118364824 0.9309201924
## [4,] 0.0247852720 0.2418514276 0.2762386296 0.2028302887 0.1202748459
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 1.387749e-01 4.953964e-02 7.063421e-01 2.005848e-01 6.658372e-02
## [2,] 5.582126e-01 1.133054e-01 1.774016e-06 2.303923e-01 1.083404e-02
## [3,] 7.203108e-01 3.789435e-01 7.130100e-01 7.997802e-01 2.671740e-01
## [4,] 7.069093e-02 7.443683e-01 5.031532e-02 5.921693e-03 3.070143e-01

3.7 Summation + Permutation

Some examples of combining Summation and Permutation are shown below.

einsum::einsum('ijk->ki', arrE)
##          [,1]     [,2]     [,3]
## [1,] 2.041753 1.887941 2.234250
## [2,] 1.534824 1.512771 2.037535
## [3,] 2.234974 1.423960 1.910484
## [4,] 2.194369 2.230848 1.899116
## [5,] 1.271797 2.577535 1.433102
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]
## [1,] 2.041753 1.887941 2.234250
## [2,] 1.534824 1.512771 2.037535
## [3,] 2.234974 1.423960 1.910484
## [4,] 2.194369 2.230848 1.899116
## [5,] 1.271797 2.577535 1.433102

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.0004459674 0.006010405 1.511141e-02 0.011452129 4.133625e-04
## [2,] 0.0042992994 0.001835465 3.175973e-03 0.001761958 1.880452e-03
## [3,] 0.0446175435 0.001840161 3.374246e-02 0.028760903 3.146301e-05
## [4,] 0.0241250458 0.023745045 7.644959e-05 0.015104802 7.954670e-02
## 
## , , 2
## 
##             [,1]        [,2]         [,3]        [,4]        [,5]
## [1,] 0.004804526 0.040378605 6.486453e-05 0.041696772 0.182045794
## [2,] 0.076322192 0.013915116 8.139347e-02 0.046518795 0.013800418
## [3,] 0.031447728 0.003054882 1.796090e-04 0.026172236 0.047601654
## [4,] 0.000342666 0.003343690 3.819106e-03 0.002804207 0.001662846
## 
## , , 3
## 
##             [,1]        [,2]         [,3]         [,4]        [,5]
## [1,] 0.017963593 0.006412615 9.143183e-02 0.0259645212 0.008618870
## [2,] 0.137236357 0.027856099 4.361412e-07 0.0566418724 0.002663544
## [3,] 0.119049325 0.062629876 1.178427e-01 0.1321836285 0.044157166
## [4,] 0.005415243 0.057021955 3.854379e-03 0.0004536282 0.023518671
DelayedTensor::einsum('i,ij,ijk,ijk,ji->jki',
    darrA, darrC, darrE, darrE, t(darrC))
## <4 x 5 x 3> HDF5Array object of type "double":
## ,,1
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 4.459674e-04 6.010405e-03 1.511141e-02 1.145213e-02 4.133625e-04
## [2,] 4.299299e-03 1.835465e-03 3.175973e-03 1.761958e-03 1.880452e-03
## [3,] 4.461754e-02 1.840161e-03 3.374246e-02 2.876090e-02 3.146301e-05
## [4,] 2.412505e-02 2.374505e-02 7.644959e-05 1.510480e-02 7.954670e-02
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 4.804526e-03 4.037861e-02 6.486453e-05 4.169677e-02 1.820458e-01
## [2,] 7.632219e-02 1.391512e-02 8.139347e-02 4.651879e-02 1.380042e-02
## [3,] 3.144773e-02 3.054882e-03 1.796090e-04 2.617224e-02 4.760165e-02
## [4,] 3.426660e-04 3.343690e-03 3.819106e-03 2.804207e-03 1.662846e-03
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 1.796359e-02 6.412615e-03 9.143183e-02 2.596452e-02 8.618870e-03
## [2,] 1.372364e-01 2.785610e-02 4.361412e-07 5.664187e-02 2.663544e-03
## [3,] 1.190493e-01 6.262988e-02 1.178427e-01 1.321836e-01 4.415717e-02
## [4,] 5.415243e-03 5.702196e-02 3.854379e-03 4.536282e-04 2.351867e-02

4 Create your original function by einsum

By using einsum and other DelayedTensor functions, it is possible to implement your original tensor calculation functions. It is intended to be applied to Delayed Arrays, which can scale to large-scale data since the calculation is performed internally by block processing.

For example, kronecker can be easily implmented by eimsum and other DelayedTensor functions4 https://stackoverflow.com/ questions/56067643/speeding-up-kronecker-products-numpy (the kronecker function inside DelayedTensor has a more efficient implementation though).

darr1 <- DelayedArray(array(1:6, dim=c(2,3)))
darr2 <- DelayedArray(array(20:1, dim=c(4,5)))

mykronecker <- function(darr1, darr2){
    stopifnot((length(dim(darr1)) == 2) && (length(dim(darr2)) == 2))
    # Outer Product
    tmpdarr <- DelayedTensor::einsum('ij,kl->ikjl', darr1, darr2)
    # Reshape
    DelayedTensor::unfold(tmpdarr, row_idx=c(2,1), col_idx=c(4,3))
}

identical(as.array(DelayedTensor::kronecker(darr1, darr2)),
    as.array(mykronecker(darr1, darr2)))
## [1] TRUE

Session information

## R version 4.5.0 RC (2025-04-04 r88126)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.2 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.21-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.16.0
##  [3] HDF5Array_1.36.0          h5mread_1.0.0            
##  [5] rhdf5_2.52.0              DelayedArray_0.34.0      
##  [7] SparseArray_1.8.0         S4Arrays_1.8.0           
##  [9] abind_1.4-8               IRanges_2.42.0           
## [11] S4Vectors_0.46.0          MatrixGenerics_1.20.0    
## [13] matrixStats_1.5.0         BiocGenerics_0.54.0      
## [15] generics_0.1.3            Matrix_1.7-3             
## [17] DelayedTensor_1.14.0      BiocStyle_2.36.0         
## 
## loaded via a namespace (and not attached):
##  [1] dqrng_0.4.1         sass_0.4.10         lattice_0.22-7     
##  [4] digest_0.6.37       evaluate_1.0.3      grid_4.5.0         
##  [7] bookdown_0.43       fastmap_1.2.0       jsonlite_2.0.0     
## [10] BiocManager_1.30.25 codetools_0.2-20    jquerylib_0.1.4    
## [13] cli_3.6.4           rlang_1.1.6         crayon_1.5.3       
## [16] XVector_0.48.0      cachem_1.1.0        yaml_2.3.10        
## [19] tools_4.5.0         beachmat_2.24.0     parallel_4.5.0     
## [22] BiocParallel_1.42.0 Rhdf5lib_1.30.0     rsvd_1.0.5         
## [25] R6_2.6.1            lifecycle_1.0.4     BiocSingular_1.24.0
## [28] irlba_2.3.5.1       ScaledMatrix_1.16.0 rTensor_1.4.8      
## [31] bslib_0.9.0         Rcpp_1.0.14         xfun_0.52          
## [34] knitr_1.50          rhdf5filters_1.20.0 htmltools_0.5.8.1  
## [37] rmarkdown_2.29      compiler_4.5.0