Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2024-04-30 22:06:37
Compiled: Wed May 1 00:28:36 2024

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.1040453 0.4594117 0.6404594
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.1040453 0.4594117 0.6404594
einsum::einsum('iii->i', arrD)
## [1] 0.3331117 0.2842798 0.5891910
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.3331117 0.2842798 0.5891910

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.6568559 0.5501412 0.7390838
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.6568559 0.5501412 0.7390838
einsum::einsum('ij,ij->ij', arrC, arrC)
##           [,1]         [,2]        [,3]       [,4]
## [1,] 0.2612489 0.0002043149 0.061759468 0.44769565
## [2,] 0.8271734 0.3564508750 0.003466486 0.01355496
## [3,] 0.3203796 0.1043818797 0.188586722 0.79594738
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.2612489374 0.0002043149 0.0617594684 0.4476956467
## [2,] 0.8271734069 0.3564508750 0.0034664863 0.0135549582
## [3,] 0.3203796117 0.1043818797 0.1885867220 0.7959473806
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
## 
##           [,1]      [,2]      [,3]        [,4]
## [1,] 0.3865470 0.4673521 0.4363369 0.004770979
## [2,] 0.3971427 0.3012684 0.2238310 0.003148307
## [3,] 0.4483645 0.1464811 0.7621089 0.003262142
## 
## , , 2
## 
##            [,1]      [,2]       [,3]        [,4]
## [1,] 0.49886821 0.8797056 0.01067818 0.444887934
## [2,] 0.26324859 0.5019237 0.02362048 0.093239449
## [3,] 0.08816064 0.4011822 0.01405814 0.002957532
## 
## , , 3
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.01056983 0.87122872 0.5805828 0.61020242
## [2,] 0.43381025 0.32288343 0.4698300 0.24132916
## [3,] 0.07562326 0.01823169 0.0197701 0.02768285
## 
## , , 4
## 
##           [,1]         [,2]       [,3]        [,4]
## [1,] 0.5679070 0.3523687185 0.02911798 0.054669967
## [2,] 0.7030580 0.0002325371 0.13089024 0.002290473
## [3,] 0.2251823 0.1005707361 0.92461320 0.101344044
## 
## , , 5
## 
##             [,1]        [,2]       [,3]       [,4]
## [1,] 0.002066162 0.058750052 0.83834445 0.96936737
## [2,] 0.126010037 0.318739777 0.07073768 0.77382024
## [3,] 0.694577525 0.003140613 0.66672294 0.05433003
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.386546958 0.467352052 0.436336866 0.004770979
## [2,] 0.397142686 0.301268374 0.223830961 0.003148307
## [3,] 0.448364493 0.146481069 0.762108851 0.003262142
## 
## ,,2
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.498868210 0.879705649 0.010678180 0.444887934
## [2,] 0.263248586 0.501923706 0.023620483 0.093239449
## [3,] 0.088160639 0.401182168 0.014058140 0.002957532
## 
## ,,3
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01056983 0.87122872 0.58058283 0.61020242
## [2,] 0.43381025 0.32288343 0.46983001 0.24132916
## [3,] 0.07562326 0.01823169 0.01977010 0.02768285
## 
## ,,4
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.5679070360 0.3523687185 0.0291179768 0.0546699666
## [2,] 0.7030579662 0.0002325371 0.1308902394 0.0022904725
## [3,] 0.2251823225 0.1005707361 0.9246132045 0.1013440443
## 
## ,,5
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.002066162 0.058750052 0.838344450 0.969367374
## [2,] 0.126010037 0.318739777 0.070737683 0.773820235
## [3,] 0.694577525 0.003140613 0.666722943 0.054330028

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.6568559 0.6011352 0.6967579
## [2,] 0.6011352 0.5501412 0.6376523
## [3,] 0.6967579 0.6376523 0.7390838
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 0.6568559 0.6011352 0.6967579
## [2,] 0.6011352 0.5501412 0.6376523
## [3,] 0.6967579 0.6376523 0.7390838
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.3177813 0.008886917 0.15450869 0.41599927
## [2,] 0.5654568 0.371194021 0.03660546 0.07238527
## [3,] 0.3519116 0.200869356 0.26999560 0.55468102
## 
## , , 2, 1, 1
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.3221073 0.009007895 0.15661201 0.42166225
## [2,] 0.5731543 0.376247070 0.03710377 0.07337065
## [3,] 0.3567021 0.203603782 0.27367104 0.56223187
## 
## , , 3, 1, 1
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.3422495 0.009571183 0.16640539 0.44802995
## [2,] 0.6089952 0.399774832 0.03942397 0.07795872
## [3,] 0.3790077 0.216335685 0.29078444 0.59738978
## 
## , , 1, 2, 1
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.3494213 0.009771744 0.16989236 0.45741828
## [2,] 0.6217565 0.408151991 0.04025009 0.07959232
## [3,] 0.3869497 0.220868933 0.29687774 0.60990790
## 
## , , 2, 2, 1
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.2805460 0.007845611 0.13640445 0.36725541
## [2,] 0.4992005 0.327700131 0.03231629 0.06390368
## [3,] 0.3106771 0.177332905 0.23835942 0.48968742
## 
## , , 3, 2, 1
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.1956221 0.005470673 0.09511358 0.25608385
## [2,] 0.3480880 0.228502309 0.02253385 0.04455945
## [3,] 0.2166323 0.123652616 0.16620585 0.34145457
## 
## , , 1, 3, 1
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.3376278 0.009441934 0.16415826 0.44197977
## [2,] 0.6007714 0.394376289 0.03889159 0.07690597
## [3,] 0.3738896 0.213414297 0.28685770 0.58932265
## 
## , , 2, 3, 1
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.2418173 0.006762544 0.11757415 0.31655670
## [2,] 0.4302871 0.282461930 0.02785511 0.05508193
## [3,] 0.2677889 0.152852532 0.20545449 0.42208727
## 
## , , 3, 3, 1
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.4462064 0.01247839 0.21695031 0.5841171
## [2,] 0.7939749 0.52120473 0.05139883 0.1016383
## [3,] 0.4941297 0.28204672 0.37910897 0.7788444
## 
## , , 1, 4, 1
## 
##            [,1]         [,2]       [,3]        [,4]
## [1,] 0.03530458 0.0009873105 0.01716546 0.046216301
## [2,] 0.06282059 0.0412385691 0.00406676 0.008041792
## [3,] 0.03909635 0.0223159974 0.02999572 0.061623438
## 
## , , 2, 4, 1
## 
##            [,1]         [,2]        [,3]        [,4]
## [1,] 0.02867912 0.0008020262 0.013944095 0.037543088
## [2,] 0.05103132 0.0334995059 0.003303568 0.006532624
## [3,] 0.03175931 0.0181280510 0.024366554 0.050058835
## 
## , , 3, 4, 1
## 
##            [,1]         [,2]        [,3]        [,4]
## [1,] 0.02919300 0.0008163971 0.014193948 0.038215792
## [2,] 0.05194571 0.0340997557 0.003362762 0.006649676
## [3,] 0.03232837 0.0184528726 0.024803158 0.050955798
## 
## , , 1, 1, 2
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.3610108 0.01009585 0.17552731 0.47258981
## [2,] 0.6423788 0.42168947 0.04158509 0.08223222
## [3,] 0.3997839 0.22819466 0.30672450 0.63013716
## 
## , , 2, 1, 2
## 
##           [,1]        [,2]      [,3]       [,4]
## [1,] 0.2622469 0.007333867 0.1275072 0.34330052
## [2,] 0.4666393 0.306325299 0.0302084 0.05973545
## [3,] 0.2904126 0.165766047 0.2228120 0.45774668
## 
## , , 3, 1, 2
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.1517626 0.004244117 0.07378858 0.19866840
## [2,] 0.2700447 0.177270801 0.01748164 0.03456897
## [3,] 0.1680621 0.095929001 0.12894156 0.26489853
## 
## , , 1, 2, 2
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.4793977 0.0134066 0.23308829 0.6275670
## [2,] 0.8530352 0.5599749 0.05522217 0.1091988
## [3,] 0.5308858 0.3030269 0.40730922 0.8367792
## 
## , , 2, 2, 2
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.3621147 0.01012672 0.17606403 0.47403487
## [2,] 0.6443430 0.42297889 0.04171225 0.08248366
## [3,] 0.4010064 0.22889242 0.30766239 0.63206397
## 
## , , 3, 2, 2
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.3237413 0.00905359 0.15740647 0.42380126
## [2,] 0.5760618 0.37815570 0.03729199 0.07374285
## [3,] 0.3585116 0.20463663 0.27505932 0.56508397
## 
## , , 1, 3, 2
## 
##            [,1]        [,2]        [,3]       [,4]
## [1,] 0.05281726 0.001477062 0.025680317 0.06914170
## [2,] 0.09398248 0.061694784 0.006084058 0.01203089
## [3,] 0.05848992 0.033385752 0.044874969 0.09219148
## 
## , , 2, 3, 2
## 
##            [,1]       [,2]        [,3]       [,4]
## [1,] 0.07855461 0.00219682 0.038194090 0.10283379
## [2,] 0.13977924 0.09175806 0.009048761 0.01789343
## [3,] 0.08699150 0.04965431 0.066742112 0.13711551
## 
## , , 3, 3, 2
## 
##            [,1]        [,2]        [,3]       [,4]
## [1,] 0.06060259 0.001694782 0.029465628 0.07933327
## [2,] 0.10783561 0.070788672 0.006980856 0.01380426
## [3,] 0.06711141 0.038306854 0.051489596 0.10578062
## 
## , , 1, 4, 2
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.3409201 0.009534004 0.16575899 0.44628958
## [2,] 0.6066296 0.398221915 0.03927083 0.07765589
## [3,] 0.3775355 0.215495334 0.28965489 0.59506923
## 
## , , 2, 4, 2
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.1560728 0.004364654 0.07588425 0.20431078
## [2,] 0.2777142 0.182305467 0.01797813 0.03555076
## [3,] 0.1728352 0.098653479 0.13260363 0.27242191
## 
## , , 3, 4, 2
## 
##            [,1]         [,2]        [,3]        [,4]
## [1,] 0.02779662 0.0007773466 0.013515014 0.036387829
## [2,] 0.04946101 0.0324686742 0.003201912 0.006331605
## [3,] 0.03078202 0.0175702229 0.023616757 0.048518447
## 
## , , 1, 1, 3
## 
##            [,1]        [,2]        [,3]       [,4]
## [1,] 0.05254860 0.001469548 0.025549693 0.06879001
## [2,] 0.09350443 0.061380971 0.006053111 0.01196969
## [3,] 0.05819241 0.033215934 0.044646711 0.09172254
## 
## , , 2, 1, 3
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.3366489 0.009414558 0.16368228 0.44069826
## [2,] 0.5990295 0.393232808 0.03877883 0.07668298
## [3,] 0.3728055 0.212795509 0.28602596 0.58761393
## 
## , , 3, 1, 3
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.1405578 0.003930771 0.06834071 0.18400056
## [2,] 0.2501071 0.164182759 0.01619095 0.03201672
## [3,] 0.1556539 0.088846488 0.11942170 0.24534086
## 
## , , 1, 2, 3
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.4770824 0.01334185 0.23196255 0.6245361
## [2,] 0.8489153 0.55727035 0.05495546 0.1086714
## [3,] 0.5283218 0.30156341 0.40534204 0.8327378
## 
## , , 2, 2, 3
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.2904358 0.008122185 0.14121299 0.38020193
## [2,] 0.5167984 0.339252239 0.03345551 0.06615642
## [3,] 0.3216291 0.183584258 0.24676209 0.50694992
## 
## , , 3, 2, 3
## 
##            [,1]        [,2]        [,3]       [,4]
## [1,] 0.06901455 0.001930027 0.033555614 0.09034515
## [2,] 0.12280377 0.080614517 0.007949836 0.01572036
## [3,] 0.07642683 0.043624049 0.058636626 0.12046353
## 
## , , 1, 3, 3
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.3894569 0.01089136 0.18935809 0.50982782
## [2,] 0.6929954 0.45491676 0.04486181 0.08871176
## [3,] 0.4312852 0.24617540 0.33089305 0.67978922
## 
## , , 2, 3, 3
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.3503464 0.009797615 0.17034216 0.45862932
## [2,] 0.6234027 0.409232597 0.04035665 0.07980305
## [3,] 0.3879742 0.221453696 0.29766374 0.61152266
## 
## , , 3, 3, 3
## 
##            [,1]        [,2]        [,3]       [,4]
## [1,] 0.07186736 0.002009807 0.034942677 0.09407968
## [2,] 0.12788002 0.083946819 0.008278452 0.01637018
## [3,] 0.07958603 0.045427304 0.061060444 0.12544304
## 
## , , 1, 4, 3
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.3992677 0.01116573 0.19412825 0.52267099
## [2,] 0.7104528 0.46637666 0.04599194 0.09094651
## [3,] 0.4421498 0.25237685 0.33922864 0.69691392
## 
## , , 2, 4, 3
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.2510916 0.007021904 0.12208342 0.32869745
## [2,] 0.4467897 0.293295057 0.02892342 0.05719446
## [3,] 0.2780592 0.158714812 0.21333419 0.43827539
## 
## , , 3, 4, 3
## 
##            [,1]        [,2]        [,3]       [,4]
## [1,] 0.08504185 0.002378239 0.041348256 0.11132607
## [2,] 0.15132257 0.099335679 0.009796031 0.01937111
## [3,] 0.09417548 0.053754890 0.072253848 0.14843886
## 
## , , 1, 1, 4
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.3851819 0.01077181 0.18727957 0.5042316
## [2,] 0.6853886 0.44992328 0.04436938 0.0877380
## [3,] 0.4265511 0.24347321 0.32726095 0.6723274
## 
## , , 2, 1, 4
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.4285711 0.01198521 0.20837583 0.56103119
## [2,] 0.7625948 0.50060526 0.04936741 0.09762132
## [3,] 0.4746003 0.27089945 0.36412552 0.74806226
## 
## , , 3, 1, 4
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.2425462 0.006782927 0.11792854 0.31751086
## [2,] 0.4315841 0.283313318 0.02793907 0.05524796
## [3,] 0.2685960 0.153313255 0.20607376 0.42335952
## 
## , , 1, 2, 4
## 
##           [,1]        [,2]      [,3]       [,4]
## [1,] 0.3034072 0.008484938 0.1475198 0.39718250
## [2,] 0.5398796 0.354403919 0.0349497 0.06911109
## [3,] 0.3359937 0.191783496 0.2577830 0.52959131
## 
## , , 2, 2, 4
## 
##             [,1]         [,2]         [,3]        [,4]
## [1,] 0.007794232 0.0002179697 0.0037896391 0.010203226
## [2,] 0.013868975 0.0091042867 0.0008978232 0.001775396
## [3,] 0.008631346 0.0049267286 0.0066221900 0.013604678
## 
## , , 3, 2, 4
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.1620926 0.004533001 0.07881114 0.21219114
## [2,] 0.2884258 0.189337072 0.01867156 0.03692197
## [3,] 0.1795016 0.102458589 0.13771821 0.28292934
## 
## , , 1, 3, 4
## 
##            [,1]        [,2]       [,3]       [,4]
## [1,] 0.08721835 0.002439106 0.04240649 0.11417527
## [2,] 0.15519541 0.101878007 0.01004674 0.01986688
## [3,] 0.09658574 0.055130655 0.07410306 0.15223790
## 
## , , 2, 3, 4
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.1849187 0.005171347 0.08990946 0.24207228
## [2,] 0.3290424 0.215999862 0.02130092 0.04212139
## [3,] 0.2047793 0.116886993 0.15711194 0.32277197
## 
## , , 3, 3, 4
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.4914817 0.01374454 0.23896364 0.6433858
## [2,] 0.8745373 0.57408988 0.05661412 0.1119513
## [3,] 0.5442676 0.31066520 0.41757607 0.8578715
## 
## , , 1, 4, 4
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.1195093 0.003342138 0.05810670 0.15644650
## [2,] 0.2126536 0.139596409 0.01376636 0.02722222
## [3,] 0.1323448 0.075541736 0.10153832 0.20860109
## 
## , , 2, 4, 4
## 
##            [,1]         [,2]        [,3]        [,4]
## [1,] 0.02446188 0.0006840889 0.011893627 0.032022408
## [2,] 0.04352721 0.0285734307 0.002817781 0.005572007
## [3,] 0.02708913 0.0154623358 0.020783472 0.042697724
## 
## , , 3, 4, 4
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.1627145 0.004550395 0.07911355 0.21300537
## [2,] 0.2895326 0.190063603 0.01874321 0.03706365
## [3,] 0.1801904 0.102851747 0.13824667 0.28401501
## 
## , , 1, 1, 5
## 
##            [,1]         [,2]        [,3]        [,4]
## [1,] 0.02323322 0.0006497289 0.011296241 0.030414006
## [2,] 0.04134095 0.0271382619 0.002676252 0.005292139
## [3,] 0.02572851 0.0146857031 0.019739572 0.040553129
## 
## , , 2, 1, 5
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.1814387 0.005074025 0.08821742 0.23751662
## [2,] 0.3228500 0.211934868 0.02090005 0.04132869
## [3,] 0.2009255 0.114687247 0.15415518 0.31669758
## 
## , , 3, 1, 5
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.4259785 0.0119127 0.20711528 0.55763728
## [2,] 0.7579816 0.4975769 0.04906876 0.09703076
## [3,] 0.4717292 0.2692607 0.36192278 0.74353693
## 
## , , 1, 2, 5
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.1238886 0.003464608 0.06023597 0.16217935
## [2,] 0.2204461 0.144711808 0.01427082 0.02821975
## [3,] 0.1371945 0.078309903 0.10525911 0.21624512
## 
## , , 2, 2, 5
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.2885662 0.0080699 0.14030395 0.37775443
## [2,] 0.5134716 0.3370684 0.03324014 0.06573054
## [3,] 0.3195586 0.1824025 0.24517359 0.50368650
## 
## , , 3, 2, 5
## 
##            [,1]         [,2]        [,3]        [,4]
## [1,] 0.02864405 0.0008010456 0.013927045 0.037497184
## [2,] 0.05096893 0.0334585453 0.003299529 0.006524636
## [3,] 0.03172047 0.0181058855 0.024336761 0.049997627
## 
## , , 1, 3, 5
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.4679921 0.01308764 0.22754276 0.6126362
## [2,] 0.8327402 0.54665219 0.05390834 0.1066008
## [3,] 0.5182552 0.29581746 0.39761870 0.8168709
## 
## , , 2, 3, 5
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.1359417 0.003801679 0.06609631 0.17795773
## [2,] 0.2418932 0.158790771 0.01565922 0.03096524
## [3,] 0.1505421 0.085928647 0.11549973 0.23728353
## 
## , , 3, 3, 5
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.4173496 0.01167139 0.2029198 0.54634143
## [2,] 0.7426274 0.48749767 0.0480748 0.09506525
## [3,] 0.4621736 0.26380636 0.3545914 0.72847538
## 
## , , 1, 4, 5
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.5032357 0.01407324 0.24467859 0.6587728
## [2,] 0.8954524 0.58781957 0.05796808 0.1146287
## [3,] 0.5572841 0.31809494 0.42756265 0.8783880
## 
## , , 2, 4, 5
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.4496217 0.0125739 0.21861090 0.5885881
## [2,] 0.8000522 0.5251942 0.05179225 0.1024163
## [3,] 0.4979119 0.2842056 0.38201076 0.7848058
## 
## , , 3, 4, 5
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.1191372 0.003331731 0.05792576 0.15595934
## [2,] 0.2119914 0.139161726 0.01372349 0.02713745
## [3,] 0.1319327 0.075306510 0.10122214 0.20795154
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.317781343 0.008886917 0.154508688 0.415999267
## [2,] 0.565456775 0.371194021 0.036605461 0.072385274
## [3,] 0.351911586 0.200869356 0.269995600 0.554681025
## 
## ,,2,1,1
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.322107288 0.009007895 0.156612008 0.421662248
## [2,] 0.573154315 0.376247070 0.037103769 0.073370652
## [3,] 0.356702144 0.203603782 0.273671039 0.562231875
## 
## ,,3,1,1
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.342249540 0.009571183 0.166405387 0.448029945
## [2,] 0.608995226 0.399774832 0.039423970 0.077958720
## [3,] 0.379007707 0.216335685 0.290784439 0.597389776
## 
## ...
## 
## ,,1,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.50323573 0.01407324 0.24467859 0.65877276
## [2,] 0.89545235 0.58781957 0.05796808 0.11462868
## [3,] 0.55728408 0.31809494 0.42756265 0.87838797
## 
## ,,2,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.44962175 0.01257390 0.21861090 0.58858810
## [2,] 0.80005220 0.52519415 0.05179225 0.10241631
## [3,] 0.49791187 0.28420558 0.38201076 0.78480583
## 
## ,,3,4,5
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.119137156 0.003331731 0.057925760 0.155959343
## [2,] 0.211991401 0.139161726 0.013723494 0.027137451
## [3,] 0.131932684 0.075306510 0.101222141 0.207951540

3.3 Summation

If there is a vanishing subscript on the left or right side of ->, the summation is done for that subscript.

einsum::einsum('i->', arrA)
## [1] 2.411881
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
##      [1] 
## 2.411881
einsum::einsum('ij->', arrC)
## [1] 5.34039
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
##     [1] 
## 5.34039
einsum::einsum('ijk->', arrE)
## [1] 27.90291
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
##      [1] 
## 27.90291

3.3.1 Row-wise / Column-wise Summation

einsum::einsum('ij->i', arrC)
## [1] 1.443034 1.681828 2.215527
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
##      [1]      [2]      [3] 
## 1.443034 1.681828 2.215527
einsum::einsum('ij->j', arrC)
## [1] 1.9866368 0.9344108 0.7416571 1.6776852
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
##       [1]       [2]       [3]       [4] 
## 1.9866368 0.9344108 0.7416571 1.6776852

3.3.2 Mode-wise Vectorization

einsum::einsum('ijk->i', arrE)
## [1] 10.968552  9.121005  7.813350
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 10.968552  9.121005  7.813350
einsum::einsum('ijk->j', arrE)
## [1] 7.774736 7.320650 7.462357 5.345164
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 7.774736 7.320650 7.462357 5.345164
einsum::einsum('ijk->k', arrE)
## [1] 5.725712 5.198409 5.699895 5.086616 6.192274
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4]      [5] 
## 5.725712 5.198409 5.699895 5.086616 6.192274

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.229896 3.390945 2.612105 2.7356067
## [2,] 2.995377 2.405393 1.939992 1.7802432
## [3,] 2.549463 1.524313 2.910261 0.8293138
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]
## [1,] 2.2298958 3.3909447 2.6121046 2.7356067
## [2,] 2.9953773 2.4053929 1.9399919 1.7802432
## [3,] 2.5494626 1.5243127 2.9102608 0.8293138
einsum::einsum('ijk->jk', arrE)
##           [,1]      [,2]     [,3]      [,4]      [,5]
## [1,] 1.9215218 1.5163022 1.036449 2.0666154 1.2338472
## [2,] 1.6152392 2.2797814 1.636649 0.9259846 0.8629957
## [3,] 2.0066542 0.3755919 1.588008 1.4939956 1.9981081
## [4,] 0.1822972 1.0267337 1.438789 0.6000208 2.0973231
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.9215218 1.5163022 1.0364492 2.0666154 1.2338472
## [2,] 1.6152392 2.2797814 1.6366494 0.9259846 0.8629957
## [3,] 2.0066542 0.3755919 1.5880075 1.4939956 1.9981081
## [4,] 0.1822972 1.0267337 1.4387888 0.6000208 2.0973231
einsum::einsum('ijk->jk', arrE)
##           [,1]      [,2]     [,3]      [,4]      [,5]
## [1,] 1.9215218 1.5163022 1.036449 2.0666154 1.2338472
## [2,] 1.6152392 2.2797814 1.636649 0.9259846 0.8629957
## [3,] 2.0066542 0.3755919 1.588008 1.4939956 1.9981081
## [4,] 0.1822972 1.0267337 1.438789 0.6000208 2.0973231
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.9215218 1.5163022 1.0364492 2.0666154 1.2338472
## [2,] 1.6152392 2.2797814 1.6366494 0.9259846 0.8629957
## [3,] 2.0066542 0.3755919 1.5880075 1.4939956 1.9981081
## [4,] 0.1822972 1.0267337 1.4387888 0.6000208 2.0973231

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

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.1040453 0.7043958 0.9775731
## [2,] 0.5829736 0.4594117 0.6859623
## [3,] 0.7320465 0.3073544 0.6404594
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 0.1040453 0.7043958 0.9775731
## [2,] 0.5829736 0.4594117 0.6859623
## [3,] 0.7320465 0.3073544 0.6404594
einsum::einsum('ijk->jki', arrD)
## , , 1
## 
##            [,1]       [,2]      [,3]
## [1,] 0.33311167 0.05436405 0.4281262
## [2,] 0.07329307 0.85987906 0.5602690
## [3,] 0.67654175 0.91912010 0.4116535
## 
## , , 2
## 
##           [,1]      [,2]      [,3]
## [1,] 0.1826237 0.2821208 0.4261233
## [2,] 0.3536521 0.2842798 0.8465415
## [3,] 0.4417099 0.2835674 0.3709127
## 
## , , 3
## 
##           [,1]      [,2]      [,3]
## [1,] 0.9518693 0.5309218 0.4954508
## [2,] 0.7201502 0.2160762 0.6557688
## [3,] 0.9430935 0.8230076 0.5891910
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
##            [,1]       [,2]       [,3]
## [1,] 0.33311167 0.05436405 0.42812621
## [2,] 0.07329307 0.85987906 0.56026904
## [3,] 0.67654175 0.91912010 0.41165346
## 
## ,,2
##           [,1]      [,2]      [,3]
## [1,] 0.1826237 0.2821208 0.4261233
## [2,] 0.3536521 0.2842798 0.8465415
## [3,] 0.4417099 0.2835674 0.3709127
## 
## ,,3
##           [,1]      [,2]      [,3]
## [1,] 0.9518693 0.5309218 0.4954508
## [2,] 0.7201502 0.2160762 0.6557688
## [3,] 0.9430935 0.8230076 0.5891910

3.5 Multiplication + Summation

Some examples of combining Multiplication and Summation are shown below.

3.5.1 Inner Product (Squared Frobenius Norm)

Inner Product first calculate Hadamard Product and collapses it to 0D tensor (norm).

einsum::einsum('i,i->', arrA, arrA)
## [1] 1.946081
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
##      [1] 
## 1.946081
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 3.38085
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
##     [1] 
## 3.38085
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 18.25374
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
##      [1] 
## 18.25374

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,] 1.23205414 0.8502774 0.5200033 1.4961473 0.8226537
## [2,] 0.91510149 1.7828115 1.2123438 0.4531720 0.3806304
## [3,] 1.42227668 0.0483568 1.0701829 1.0846214 1.5758051
## [4,] 0.01118143 0.5410849 0.8792144 0.1583045 1.7975176
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]       [,4]       [,5]
## [1,] 1.23205414 0.85027743 0.52000333 1.49614732 0.82265372
## [2,] 0.91510149 1.78281152 1.21234384 0.45317199 0.38063044
## [3,] 1.42227668 0.04835680 1.07018294 1.08462142 1.57580508
## [4,] 0.01118143 0.54108491 0.87921443 0.15830448 1.79751764

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.7709084 0.5659299 0.9987910
## [2,] 0.5659299 1.2006457 0.8371204
## [3,] 0.9987910 0.8371204 1.4092956
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 0.7709084 0.5659299 0.9987910
## [2,] 0.5659299 1.2006457 0.8371204
## [3,] 0.9987910 0.8371204 1.4092956

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.2612489374 0.827173407 0.3203796
## [2,] 0.0002043149 0.356450875 0.1043819
## [3,] 0.0617594684 0.003466486 0.1885867
## [4,] 0.4476956467 0.013554958 0.7959474
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
##              [,1]         [,2]         [,3]
## [1,] 0.2612489374 0.8271734069 0.3203796117
## [2,] 0.0002043149 0.3564508750 0.1043818797
## [3,] 0.0617594684 0.0034664863 0.1885867220
## [4,] 0.4476956467 0.0135549582 0.7959473806
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
## 
##             [,1]       [,2]       [,3]       [,4]        [,5]
## [1,] 0.386546958 0.49886821 0.01056983 0.56790704 0.002066162
## [2,] 0.467352052 0.87970565 0.87122872 0.35236872 0.058750052
## [3,] 0.436336866 0.01067818 0.58058283 0.02911798 0.838344450
## [4,] 0.004770979 0.44488793 0.61020242 0.05466997 0.969367374
## 
## , , 2
## 
##             [,1]       [,2]      [,3]         [,4]       [,5]
## [1,] 0.397142686 0.26324859 0.4338102 0.7030579662 0.12601004
## [2,] 0.301268374 0.50192371 0.3228834 0.0002325371 0.31873978
## [3,] 0.223830961 0.02362048 0.4698300 0.1308902394 0.07073768
## [4,] 0.003148307 0.09323945 0.2413292 0.0022904725 0.77382024
## 
## , , 3
## 
##             [,1]        [,2]       [,3]      [,4]        [,5]
## [1,] 0.448364493 0.088160639 0.07562326 0.2251823 0.694577525
## [2,] 0.146481069 0.401182168 0.01823169 0.1005707 0.003140613
## [3,] 0.762108851 0.014058140 0.01977010 0.9246132 0.666722943
## [4,] 0.003262142 0.002957532 0.02768285 0.1013440 0.054330028
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.386546958 0.498868210 0.010569826 0.567907036 0.002066162
## [2,] 0.467352052 0.879705649 0.871228719 0.352368719 0.058750052
## [3,] 0.436336866 0.010678180 0.580582829 0.029117977 0.838344450
## [4,] 0.004770979 0.444887934 0.610202416 0.054669967 0.969367374
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.3971426863 0.2632485856 0.4338102451 0.7030579662 0.1260100374
## [2,] 0.3012683740 0.5019237059 0.3228834324 0.0002325371 0.3187397775
## [3,] 0.2238309610 0.0236204834 0.4698300101 0.1308902394 0.0707376829
## [4,] 0.0031483073 0.0932394492 0.2413291607 0.0022904725 0.7738202354
## 
## ,,3
##             [,1]        [,2]        [,3]        [,4]        [,5]
## [1,] 0.448364493 0.088160639 0.075623263 0.225182323 0.694577525
## [2,] 0.146481069 0.401182168 0.018231686 0.100570736 0.003140613
## [3,] 0.762108851 0.014058140 0.019770097 0.924613205 0.666722943
## [4,] 0.003262142 0.002957532 0.027682853 0.101344044 0.054330028

3.7 Summation + Permutation

Some examples of combining Summation and Permutation are shown below.

einsum::einsum('ijk->ki', arrE)
##          [,1]     [,2]      [,3]
## [1,] 2.034991 1.708289 1.9824323
## [2,] 2.414567 1.680584 1.1032582
## [3,] 2.579320 2.403565 0.7170094
## [4,] 1.751659 1.263381 2.0715766
## [5,] 2.188015 2.065185 1.9390734
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 2.0349908 1.7082893 1.9824323
## [2,] 2.4145668 1.6805842 1.1032582
## [3,] 2.5793204 2.4035652 0.7170094
## [4,] 1.7516586 1.2633811 2.0715766
## [5,] 2.1880152 2.0651855 1.9390734

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.081844944 0.1056271172 0.0022379863 1.202450e-01 4.374757e-04
## [2,] 0.000077389 0.0001456708 0.0001442671 5.834887e-05 9.728443e-06
## [3,] 0.021840397 0.0005344854 0.0290604812 1.457471e-03 4.196248e-02
## [4,] 0.001731113 0.1614241705 0.2214072610 1.983658e-02 3.517275e-01
## 
## , , 2
## 
##              [,1]         [,2]        [,3]         [,4]         [,5]
## [1,] 2.436577e-01 1.615101e-01 0.266154286 4.313450e-01 0.0773105566
## [2,] 7.965083e-02 1.327011e-01 0.085365530 6.147930e-05 0.0842700101
## [3,] 5.755019e-04 6.073169e-05 0.001208001 3.365378e-04 0.0001818768
## [4,] 3.165282e-05 9.374216e-04 0.002426303 2.302822e-05 0.0077799241
## 
## , , 3
## 
##             [,1]        [,2]        [,3]        [,4]         [,5]
## [1,] 0.123493164 0.024282111 0.020828938 0.062022033 0.1913076913
## [2,] 0.013144784 0.036000917 0.001636058 0.009024924 0.0002818294
## [3,] 0.123559161 0.002279218 0.003205286 0.149905662 0.1080944375
## [4,] 0.002232205 0.002023768 0.018942707 0.069347279 0.0371767240
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,] 8.184494e-02 1.056271e-01 2.237986e-03 1.202450e-01 4.374757e-04
## [2,] 7.738900e-05 1.456708e-04 1.442671e-04 5.834887e-05 9.728443e-06
## [3,] 2.184040e-02 5.344854e-04 2.906048e-02 1.457471e-03 4.196248e-02
## [4,] 1.731113e-03 1.614242e-01 2.214073e-01 1.983658e-02 3.517275e-01
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 2.436577e-01 1.615101e-01 2.661543e-01 4.313450e-01 7.731056e-02
## [2,] 7.965083e-02 1.327011e-01 8.536553e-02 6.147930e-05 8.427001e-02
## [3,] 5.755019e-04 6.073169e-05 1.208001e-03 3.365378e-04 1.818768e-04
## [4,] 3.165282e-05 9.374216e-04 2.426303e-03 2.302822e-05 7.779924e-03
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.1234931638 0.0242821106 0.0208289375 0.0620220331 0.1913076913
## [2,] 0.0131447838 0.0360009174 0.0016360583 0.0090249245 0.0002818294
## [3,] 0.1235591610 0.0022792176 0.0032052857 0.1499056621 0.1080944375
## [4,] 0.0022322048 0.0020237675 0.0189427069 0.0693472786 0.0371767240

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.4.0 alpha (2024-03-27 r86216)
## Platform: aarch64-apple-darwin20
## Running under: macOS Ventura 13.6.5
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib 
## LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## time zone: America/New_York
## tzcode source: internal
## 
## attached base packages:
## [1] stats4    stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
##  [1] einsum_0.1.2              DelayedRandomArray_1.12.0
##  [3] HDF5Array_1.32.0          rhdf5_2.48.0             
##  [5] DelayedArray_0.30.0       SparseArray_1.4.0        
##  [7] S4Arrays_1.4.0            abind_1.4-5              
##  [9] IRanges_2.38.0            S4Vectors_0.42.0         
## [11] MatrixGenerics_1.16.0     matrixStats_1.2.0        
## [13] BiocGenerics_0.50.0       Matrix_1.7-0             
## [15] DelayedTensor_1.10.0      BiocStyle_2.32.0         
## 
## loaded via a namespace (and not attached):
##  [1] jsonlite_1.8.8      compiler_4.4.0      BiocManager_1.30.22
##  [4] crayon_1.5.2        rsvd_1.0.5          Rcpp_1.0.12        
##  [7] rhdf5filters_1.16.0 parallel_4.4.0      jquerylib_0.1.4    
## [10] BiocParallel_1.38.0 yaml_2.3.8          fastmap_1.1.1      
## [13] lattice_0.22-6      R6_2.5.1            XVector_0.44.0     
## [16] ScaledMatrix_1.12.0 knitr_1.45          bookdown_0.38      
## [19] bslib_0.6.2         rlang_1.1.3         cachem_1.0.8       
## [22] xfun_0.43           sass_0.4.9          cli_3.6.2          
## [25] Rhdf5lib_1.26.0     BiocSingular_1.20.0 zlibbioc_1.50.0    
## [28] digest_0.6.35       grid_4.4.0          irlba_2.3.5.1      
## [31] rTensor_1.4.8       dqrng_0.3.2         lifecycle_1.0.4    
## [34] evaluate_0.23       codetools_0.2-19    beachmat_2.20.0    
## [37] rmarkdown_2.26      tools_4.4.0         htmltools_0.5.8