Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2023-10-26 14:37:16
Compiled: Mon Nov 6 21:51:53 2023

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.8612397 0.3927046 0.2557266
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.8612397 0.3927046 0.2557266
einsum::einsum('iii->i', arrD)
## [1] 0.9915518 0.9435684 0.4905826
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.9915518 0.9435684 0.4905826

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.36602726 0.08978873 0.78602641
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
##        [1]        [2]        [3] 
## 0.36602726 0.08978873 0.78602641
einsum::einsum('ij,ij->ij', arrC, arrC)
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.5254305 0.09125805 0.32178872 0.1171382
## [2,] 0.9297364 0.47210618 0.08531601 0.4389507
## [3,] 0.9669207 0.67374590 0.44012693 0.7139710
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.52543050 0.09125805 0.32178872 0.11713824
## [2,] 0.92973642 0.47210618 0.08531601 0.43895072
## [3,] 0.96692070 0.67374590 0.44012693 0.71397095
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.1136185 0.01403559 0.6236724 0.6252173
## [2,] 0.4627076 0.14516494 0.4084756 0.6372145
## [3,] 0.8003273 0.27417986 0.2648286 0.4227327
## 
## , , 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5416897 0.1237904 0.7957888 0.2809515
## [2,] 0.6259837 0.8575259 0.5400375 0.9959349
## [3,] 0.3661030 0.2599018 0.6953526 0.6932125
## 
## , , 3
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.2407908 0.4566398 0.3970622 0.05906733
## [2,] 0.5673882 0.1020449 0.7717557 0.40196238
## [3,] 0.2822069 0.5377962 0.3555170 0.24284992
## 
## , , 4
## 
##            [,1]      [,2]        [,3]      [,4]
## [1,] 0.05079987 0.2424257 0.001140887 0.8380916
## [2,] 0.29632705 0.3985703 0.096274630 0.2566055
## [3,] 0.19254517 0.0109613 0.363285101 0.8804533
## 
## , , 5
## 
##            [,1]       [,2]        [,3]      [,4]
## [1,] 0.45269277 0.06934906 0.010407160 0.2332482
## [2,] 0.04651049 0.12314085 0.005171854 0.4073387
## [3,] 0.01187266 0.57099709 0.300618470 0.2248323
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.11361846 0.01403559 0.62367240 0.62521729
## [2,] 0.46270764 0.14516494 0.40847555 0.63721448
## [3,] 0.80032735 0.27417986 0.26482863 0.42273270
## 
## ,,2
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5416897 0.1237904 0.7957888 0.2809515
## [2,] 0.6259837 0.8575259 0.5400375 0.9959349
## [3,] 0.3661030 0.2599018 0.6953526 0.6932125
## 
## ,,3
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.24079082 0.45663983 0.39706222 0.05906733
## [2,] 0.56738824 0.10204495 0.77175570 0.40196238
## [3,] 0.28220686 0.53779620 0.35551699 0.24284992
## 
## ,,4
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.050799867 0.242425650 0.001140887 0.838091554
## [2,] 0.296327045 0.398570331 0.096274630 0.256605534
## [3,] 0.192545173 0.010961304 0.363285101 0.880453337
## 
## ,,5
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.452692765 0.069349059 0.010407160 0.233248214
## [2,] 0.046510494 0.123140846 0.005171854 0.407338654
## [3,] 0.011872658 0.570997089 0.300618470 0.224832296

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.3660273 0.18128741 0.5363833
## [2,] 0.1812874 0.08978873 0.2656620
## [3,] 0.5363833 0.26566203 0.7860264
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.36602726 0.18128741 0.53638335
## [2,] 0.18128741 0.08978873 0.26566203
## [3,] 0.53638335 0.26566203 0.78602641
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.2443330 0.1018263 0.19120967 0.1153649
## [2,] 0.3250157 0.2316031 0.09845544 0.2233224
## [3,] 0.3314514 0.2766767 0.22362143 0.2848162
## 
## , , 2, 1, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4930727 0.2054892 0.3858680 0.2328106
## [2,] 0.6558934 0.4673833 0.1986866 0.4506727
## [3,] 0.6688808 0.5583434 0.4512761 0.5747694
## 
## , , 3, 1, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.6484724 0.2702523 0.5074804 0.3061845
## [2,] 0.8626085 0.6146865 0.2613058 0.5927093
## [3,] 0.8796892 0.7343141 0.5935028 0.7559170
## 
## , , 1, 2, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.08587623 0.03578911 0.06720487 0.04054755
## [2,] 0.11423396 0.08140201 0.03460434 0.07849160
## [3,] 0.11649593 0.09724413 0.07859670 0.10010496
## 
## , , 2, 2, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.2761776 0.1150977 0.2161306 0.1304008
## [2,] 0.3673760 0.2617886 0.1112874 0.2524287
## [3,] 0.3746505 0.3127368 0.2527667 0.3219372
## 
## , , 3, 2, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3795556 0.1581807 0.2970320 0.1792120
## [2,] 0.5048911 0.3597805 0.1529442 0.3469171
## [3,] 0.5148885 0.4297994 0.3473816 0.4424437
## 
## , , 1, 3, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5724478 0.2385689 0.4479852 0.2702885
## [2,] 0.7614794 0.5426229 0.2306713 0.5232222
## [3,] 0.7765576 0.6482258 0.5239227 0.6672960
## 
## , , 2, 3, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4632769 0.1930717 0.3625504 0.2187421
## [2,] 0.6162585 0.4391399 0.1866802 0.4234391
## [3,] 0.6284612 0.5246034 0.4240060 0.5400367
## 
## , , 3, 3, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3730269 0.1554598 0.2919227 0.1761294
## [2,] 0.4962064 0.3535919 0.1503134 0.3409497
## [3,] 0.5060319 0.4224064 0.3414062 0.4348332
## 
## , , 1, 4, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5731564 0.2388642 0.4485397 0.2706231
## [2,] 0.7624220 0.5432945 0.2309568 0.5238698
## [3,] 0.7775188 0.6490282 0.5245712 0.6681220
## 
## , , 2, 4, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5786294 0.2411451 0.4528227 0.2732072
## [2,] 0.7697022 0.5484824 0.2331622 0.5288722
## [3,] 0.7849432 0.6552256 0.5295803 0.6745018
## 
## , , 3, 4, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4712925 0.1964122 0.3688233 0.2225268
## [2,] 0.6269210 0.4467379 0.1899102 0.4307654
## [3,] 0.6393348 0.5336801 0.4313421 0.5493804
## 
## , , 1, 1, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5334982 0.2223366 0.4175041 0.2518980
## [2,] 0.7096680 0.5057026 0.2149763 0.4876219
## [3,] 0.7237203 0.6041202 0.4882748 0.6218929
## 
## , , 2, 1, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5735076 0.2390106 0.4488145 0.2707889
## [2,] 0.7628892 0.5436274 0.2310983 0.5241908
## [3,] 0.7779953 0.6494259 0.5248927 0.6685314
## 
## , , 3, 1, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4385906 0.1827836 0.3432314 0.2070861
## [2,] 0.5834203 0.4157397 0.1767327 0.4008755
## [3,] 0.5949727 0.4966492 0.4014122 0.5112601
## 
## , , 1, 2, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.2550358 0.1062867 0.1995855 0.1204184
## [2,] 0.3392528 0.2417483 0.1027682 0.2331049
## [3,] 0.3459704 0.2887963 0.2334170 0.2972924
## 
## , , 2, 2, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.6712453 0.2797430 0.5253020 0.3169370
## [2,] 0.8929015 0.6362730 0.2704823 0.6135239
## [3,] 0.9105820 0.7601017 0.6143454 0.7824632
## 
## , , 3, 2, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3695407 0.1540069 0.2891945 0.1744833
## [2,] 0.4915691 0.3502874 0.1489086 0.3377633
## [3,] 0.5013027 0.4184588 0.3382156 0.4307695
## 
## , , 1, 3, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.6466311 0.2694849 0.5060394 0.3053151
## [2,] 0.8601592 0.6129411 0.2605639 0.5910263
## [3,] 0.8771914 0.7322291 0.5918176 0.7537706
## 
## , , 2, 3, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5326839 0.2219972 0.4168668 0.2515135
## [2,] 0.7085849 0.5049307 0.2146482 0.4868776
## [3,] 0.7226157 0.6031982 0.4875295 0.6209437
## 
## , , 3, 3, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.6044497 0.2519058 0.4730292 0.2853986
## [2,] 0.8040489 0.5729575 0.2435666 0.5524722
## [3,] 0.8199700 0.6844640 0.5532119 0.7046003
## 
## , , 1, 4, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3842141 0.1601221 0.3006776 0.1814116
## [2,] 0.5110879 0.3641963 0.1548214 0.3511749
## [3,] 0.5212080 0.4350746 0.3516451 0.4478741
## 
## , , 2, 4, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.7233910 0.3014748 0.5661101 0.3415583
## [2,] 0.9622666 0.6857019 0.2914948 0.6611856
## [3,] 0.9813206 0.8191502 0.6620708 0.8432488
## 
## , , 3, 4, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.6035189 0.2515178 0.4723007 0.2849591
## [2,] 0.8028106 0.5720751 0.2431915 0.5516214
## [3,] 0.8187072 0.6834099 0.5523599 0.7035152
## 
## , , 1, 1, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3556949 0.1482366 0.2783591 0.1679459
## [2,] 0.4731511 0.3371629 0.1433294 0.3251081
## [3,] 0.4825201 0.4027801 0.3255434 0.4146295
## 
## , , 2, 1, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5460065 0.2275494 0.4272928 0.2578039
## [2,] 0.7263068 0.5175592 0.2200166 0.4990546
## [3,] 0.7406885 0.6182843 0.4997228 0.6364737
## 
## , , 3, 1, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3850715 0.1604794 0.3013486 0.1818164
## [2,] 0.5122285 0.3650090 0.1551669 0.3519587
## [3,] 0.5223712 0.4360455 0.3524299 0.4488736
## 
## , , 1, 2, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4898290 0.2041374 0.3833296 0.2312790
## [2,] 0.6515786 0.4643086 0.1973795 0.4477079
## [3,] 0.6644806 0.5546704 0.4483074 0.5709882
## 
## , , 2, 2, 3
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.2315546 0.09650089 0.18120958 0.1093314
## [2,] 0.3080177 0.21949043 0.09330631 0.2116429
## [3,] 0.3141168 0.26220672 0.21192623 0.2699206
## 
## , , 3, 2, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5315774 0.2215361 0.4160009 0.2509910
## [2,] 0.7071129 0.5038818 0.2142023 0.4858663
## [3,] 0.7211146 0.6019452 0.4865168 0.6196538
## 
## , , 1, 3, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4567588 0.1903553 0.3574495 0.2156645
## [2,] 0.6075880 0.4329613 0.1840537 0.4174814
## [3,] 0.6196190 0.5172224 0.4180404 0.5324386
## 
## , , 2, 3, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.6367919 0.2653845 0.4983395 0.3006694
## [2,] 0.8470711 0.6036146 0.2565991 0.5820333
## [3,] 0.8638441 0.7210875 0.5828126 0.7423013
## 
## , , 3, 3, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4322030 0.1801216 0.3382327 0.2040702
## [2,] 0.5749236 0.4096850 0.1741588 0.3950373
## [3,] 0.5863077 0.4894161 0.3955662 0.5038143
## 
## , , 1, 4, 3
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.1761697 0.07341914 0.13786661 0.08318079
## [2,] 0.2343439 0.16699118 0.07098865 0.16102065
## [3,] 0.2389842 0.19949028 0.16123624 0.20535910
## 
## , , 2, 4, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4595686 0.1915262 0.3596484 0.2169912
## [2,] 0.6113257 0.4356247 0.1851859 0.4200496
## [3,] 0.6234306 0.5204042 0.4206120 0.5357140
## 
## , , 3, 4, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3572125 0.1488691 0.2795467 0.1686624
## [2,] 0.4751699 0.3386015 0.1439409 0.3264952
## [3,] 0.4845788 0.4044986 0.3269324 0.4163986
## 
## , , 1, 1, 4
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.1633763 0.06808742 0.12785470 0.07714018
## [2,] 0.2173258 0.15486423 0.06583344 0.14932729
## [3,] 0.2216291 0.18500325 0.14952722 0.19044587
## 
## , , 2, 1, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3945875 0.1644452 0.3087956 0.1863095
## [2,] 0.5248867 0.3740292 0.1590014 0.3606563
## [3,] 0.5352801 0.4468211 0.3611392 0.4599662
## 
## , , 3, 1, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3180709 0.1325568 0.2489154 0.1501812
## [2,] 0.4231031 0.3014992 0.1281686 0.2907195
## [3,] 0.4314811 0.3601757 0.2911088 0.3707717
## 
## , , 1, 2, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3569003 0.1487390 0.2793024 0.1685150
## [2,] 0.4747546 0.3383056 0.1438151 0.3262099
## [3,] 0.4841553 0.4041451 0.3266467 0.4160347
## 
## , , 2, 2, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4576254 0.1907164 0.3581277 0.2160737
## [2,] 0.6087408 0.4337828 0.1844029 0.4182735
## [3,] 0.6207946 0.5182037 0.4188335 0.5334488
## 
## , , 3, 2, 4
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.07589073 0.03162763 0.05939044 0.03583278
## [2,] 0.10095110 0.07193677 0.03058063 0.06936478
## [3,] 0.10295005 0.08593680 0.06945765 0.08846498
## 
## , , 1, 3, 4
## 
##            [,1]       [,2]        [,3]       [,4]
## [1,] 0.02448381 0.01020368 0.019160498 0.01156034
## [2,] 0.03256877 0.02320819 0.009865899 0.02237841
## [3,] 0.03321367 0.02772487 0.022408375 0.02854051
## 
## , , 2, 3, 4
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.2249125 0.09373278 0.17601162 0.1061953
## [2,] 0.2991823 0.21319439 0.09062983 0.2055719
## [3,] 0.3051064 0.25468537 0.20584717 0.2621780
## 
## , , 3, 3, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4368994 0.1820788 0.3419080 0.2062876
## [2,] 0.5811707 0.4141366 0.1760512 0.3993298
## [3,] 0.5926786 0.4947341 0.3998644 0.5092887
## 
## , , 1, 4, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.6635954 0.2765549 0.5193153 0.3133250
## [2,] 0.8827255 0.6290216 0.2673997 0.6065319
## [3,] 0.9002045 0.7514391 0.6073439 0.7735457
## 
## , , 2, 4, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3671898 0.1530272 0.2873548 0.1733734
## [2,] 0.4884419 0.3480590 0.1479613 0.3356146
## [3,] 0.4981136 0.4157967 0.3360640 0.4280291
## 
## , , 3, 4, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.6801596 0.2834580 0.5322781 0.3211460
## [2,] 0.9047594 0.6447228 0.2740744 0.6216716
## [3,] 0.9226747 0.7701960 0.6225040 0.7928544
## 
## , , 1, 1, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4877075 0.2032532 0.3816693 0.2302773
## [2,] 0.6487565 0.4622976 0.1965247 0.4457688
## [3,] 0.6616026 0.5522680 0.4463656 0.5685152
## 
## , , 2, 1, 5
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.1563267 0.0651495 0.12233786 0.07381164
## [2,] 0.2079483 0.1481820 0.06299277 0.14288392
## [3,] 0.2120659 0.1770205 0.14307523 0.18222827
## 
## , , 3, 1, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.07898264 0.03291619 0.06181009 0.03729266
## [2,] 0.10506399 0.07486759 0.03182653 0.07219080
## [3,] 0.10714438 0.08943799 0.07228746 0.09206918
## 
## , , 1, 2, 5
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.1908877 0.07955287 0.14938455 0.09013006
## [2,] 0.2539219 0.18094231 0.07691934 0.17447298
## [3,] 0.2589499 0.21615653 0.17470658 0.22251565
## 
## , , 2, 2, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.2543658 0.1060075 0.1990611 0.1201020
## [2,] 0.3383615 0.2411132 0.1024982 0.2324925
## [3,] 0.3450615 0.2880376 0.2328038 0.2965114
## 
## , , 3, 2, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5477402 0.2282719 0.4286495 0.2586225
## [2,] 0.7286129 0.5192025 0.2207152 0.5006392
## [3,] 0.7430403 0.6202475 0.5013095 0.6384946
## 
## , , 1, 3, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.07394754 0.03081781 0.05786974 0.03491527
## [2,] 0.09836623 0.07009483 0.02979761 0.06758869
## [3,] 0.10031400 0.08373638 0.06767918 0.08619983
## 
## , , 2, 3, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.05212917 0.02172495 0.04079515 0.02461345
## [2,] 0.06934307 0.04941320 0.02100576 0.04764650
## [3,] 0.07071614 0.05902979 0.04771030 0.06076639
## 
## , , 3, 3, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3974344 0.1656317 0.3110235 0.1876537
## [2,] 0.5286738 0.3767278 0.1601486 0.3632584
## [3,] 0.5391421 0.4500450 0.3637448 0.4632849
## 
## , , 1, 4, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3500796 0.1458965 0.2739647 0.1652945
## [2,] 0.4656816 0.3318402 0.1410667 0.3199757
## [3,] 0.4749026 0.3964215 0.3204042 0.4080839
## 
## , , 2, 4, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4626318 0.1928028 0.3620456 0.2184375
## [2,] 0.6154003 0.4385283 0.1864202 0.4228494
## [3,] 0.6275860 0.5238728 0.4234155 0.5392847
## 
## , , 3, 4, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3437059 0.1432402 0.2689768 0.1622851
## [2,] 0.4572032 0.3257986 0.1384984 0.3141501
## [3,] 0.4662564 0.3892041 0.3145707 0.4006541
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.24433298 0.10182632 0.19120967 0.11536493
## [2,] 0.32501572 0.23160306 0.09845544 0.22332242
## [3,] 0.33145141 0.27667665 0.22362143 0.28481622
## 
## ,,2,1,1
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4930727 0.2054892 0.3858680 0.2328106
## [2,] 0.6558934 0.4673833 0.1986866 0.4506727
## [3,] 0.6688808 0.5583434 0.4512761 0.5747694
## 
## ,,3,1,1
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.6484724 0.2702523 0.5074804 0.3061845
## [2,] 0.8626085 0.6146865 0.2613058 0.5927093
## [3,] 0.8796892 0.7343141 0.5935028 0.7559170
## 
## ...
## 
## ,,1,4,5
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3500796 0.1458965 0.2739647 0.1652945
## [2,] 0.4656816 0.3318402 0.1410667 0.3199757
## [3,] 0.4749026 0.3964215 0.3204042 0.4080839
## 
## ,,2,4,5
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4626318 0.1928028 0.3620456 0.2184375
## [2,] 0.6154003 0.4385283 0.1864202 0.4228494
## [3,] 0.6275860 0.5238728 0.4234155 0.5392847
## 
## ,,3,4,5
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3437059 0.1432402 0.2689768 0.1622851
## [2,] 0.4572032 0.3257986 0.1384984 0.3141501
## [3,] 0.4662564 0.3892041 0.3145707 0.4006541

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

3.3.1 Row-wise / Column-wise Summation

einsum::einsum('ij->i', arrC)
## [1] 1.936474 2.605951 3.312530
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
##      [1]      [2]      [3] 
## 1.936474 2.605951 3.312530
einsum::einsum('ij->j', arrC)
## [1] 2.672416 1.810010 1.522774 1.849756
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 2.672416 1.810010 1.522774 1.849756

3.3.2 Mode-wise Vectorization

einsum::einsum('ijk->i', arrE)
## [1]  9.773702 11.803115 11.689605
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
##  9.773702 11.803115 11.689605
einsum::einsum('ijk->j', arrE)
## [1] 8.025345 7.137600 8.178171 9.925306
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 8.025345 7.137600 8.178171 9.925306
einsum::einsum('ijk->k', arrE)
## [1] 7.117617 8.741351 6.978448 5.744086 4.684920
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4]      [5] 
## 7.117617 8.741351 6.978448 5.744086 4.684920

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.461986 1.901771 2.447721 2.962224
## [2,] 2.984691 2.608715 2.634687 3.575021
## [3,] 2.578668 2.627113 3.095764 3.388060
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]     [,4]
## [1,] 2.461986 1.901771 2.447721 2.962224
## [2,] 2.984691 2.608715 2.634687 3.575021
## [3,] 2.578668 2.627113 3.095764 3.388060
einsum::einsum('ijk->jk', arrE)
##          [,1]     [,2]     [,3]      [,4]      [,5]
## [1,] 1.911910 2.132252 1.775188 1.2085473 0.9974491
## [2,] 1.023099 1.787671 1.728542 1.2283879 1.3698999
## [3,] 1.943465 2.460820 2.104877 0.9467899 0.7222179
## [4,] 2.239143 2.360608 1.369841 2.3603606 1.5953535
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.9119097 2.1322515 1.7751876 1.2085473 0.9974491
## [2,] 1.0230989 1.7876712 1.7285419 1.2283879 1.3698999
## [3,] 1.9434653 2.4608205 2.1048775 0.9467899 0.7222179
## [4,] 2.2391430 2.3606078 1.3698408 2.3603606 1.5953535
einsum::einsum('ijk->jk', arrE)
##          [,1]     [,2]     [,3]      [,4]      [,5]
## [1,] 1.911910 2.132252 1.775188 1.2085473 0.9974491
## [2,] 1.023099 1.787671 1.728542 1.2283879 1.3698999
## [3,] 1.943465 2.460820 2.104877 0.9467899 0.7222179
## [4,] 2.239143 2.360608 1.369841 2.3603606 1.5953535
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.9119097 2.1322515 1.7751876 1.2085473 0.9974491
## [2,] 1.0230989 1.7876712 1.7285419 1.2283879 1.3698999
## [3,] 1.9434653 2.4608205 2.1048775 0.9467899 0.7222179
## [4,] 2.2391430 2.3606078 1.3698408 2.3603606 1.5953535

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

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.8612397 0.1728550 0.4459206
## [2,] 0.8473313 0.3927046 0.6744638
## [3,] 0.6237156 0.3278920 0.2557266
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 0.8612397 0.1728550 0.4459206
## [2,] 0.8473313 0.3927046 0.6744638
## [3,] 0.6237156 0.3278920 0.2557266
einsum::einsum('ijk->jki', arrD)
## , , 1
## 
##           [,1]      [,2]       [,3]
## [1,] 0.9915518 0.9258058 0.04578181
## [2,] 0.6971436 0.2975677 0.31980324
## [3,] 0.2712393 0.7315312 0.87483529
## 
## , , 2
## 
##           [,1]      [,2]      [,3]
## [1,] 0.8870124 0.1477663 0.3789780
## [2,] 0.6432395 0.9435684 0.1034875
## [3,] 0.7086422 0.1715822 0.7425985
## 
## , , 3
## 
##           [,1]       [,2]      [,3]
## [1,] 0.7072483 0.35034529 0.5867733
## [2,] 0.9251157 0.01995233 0.4560217
## [3,] 0.2377633 0.53701610 0.4905826
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
##            [,1]       [,2]       [,3]
## [1,] 0.99155180 0.92580577 0.04578181
## [2,] 0.69714358 0.29756774 0.31980324
## [3,] 0.27123932 0.73153122 0.87483529
## 
## ,,2
##           [,1]      [,2]      [,3]
## [1,] 0.8870124 0.1477663 0.3789780
## [2,] 0.6432395 0.9435684 0.1034875
## [3,] 0.7086422 0.1715822 0.7425985
## 
## ,,3
##            [,1]       [,2]       [,3]
## [1,] 0.70724834 0.35034529 0.58677327
## [2,] 0.92511566 0.01995233 0.45602169
## [3,] 0.23776326 0.53701610 0.49058263

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

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.3766534 1.533776 1.0903859 0.5396721 0.5110759
## [2,] 0.4333804 1.241218 1.0964810 0.6519573 0.7634870
## [3,] 1.2969766 2.031179 1.5243349 0.4607006 0.3161975
## [4,] 1.6851645 1.970099 0.7038796 1.9751504 0.8654192
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.3766534 1.5337765 1.0903859 0.5396721 0.5110759
## [2,] 0.4333804 1.2412181 1.0964810 0.6519573 0.7634870
## [3,] 1.2969766 2.0311789 1.5243349 0.4607006 0.3161975
## [4,] 1.6851645 1.9700989 0.7038796 1.9751504 0.8654192

3.5.3 Matrix Multiplication

Matrix Multiplication is considered a contracted product.

einsum::einsum('ij,jk->ik', arrC, t(arrC))
##          [,1]     [,2]     [,3]
## [1,] 1.055616 1.298949 1.626266
## [2,] 1.298949 1.926109 2.265729
## [3,] 1.626266 2.265729 2.794764
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]
## [1,] 1.055616 1.298949 1.626266
## [2,] 1.298949 1.926109 2.265729
## [3,] 1.626266 2.265729 2.794764

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.52543050 0.92973642 0.9669207
## [2,] 0.09125805 0.47210618 0.6737459
## [3,] 0.32178872 0.08531601 0.4401269
## [4,] 0.11713824 0.43895072 0.7139710
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.52543050 0.92973642 0.96692070
## [2,] 0.09125805 0.47210618 0.67374590
## [3,] 0.32178872 0.08531601 0.44012693
## [4,] 0.11713824 0.43895072 0.71397095
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
## 
##            [,1]      [,2]       [,3]        [,4]       [,5]
## [1,] 0.11361846 0.5416897 0.24079082 0.050799867 0.45269277
## [2,] 0.01403559 0.1237904 0.45663983 0.242425650 0.06934906
## [3,] 0.62367240 0.7957888 0.39706222 0.001140887 0.01040716
## [4,] 0.62521729 0.2809515 0.05906733 0.838091554 0.23324821
## 
## , , 2
## 
##           [,1]      [,2]      [,3]       [,4]        [,5]
## [1,] 0.4627076 0.6259837 0.5673882 0.29632705 0.046510494
## [2,] 0.1451649 0.8575259 0.1020449 0.39857033 0.123140846
## [3,] 0.4084756 0.5400375 0.7717557 0.09627463 0.005171854
## [4,] 0.6372145 0.9959349 0.4019624 0.25660553 0.407338654
## 
## , , 3
## 
##           [,1]      [,2]      [,3]      [,4]       [,5]
## [1,] 0.8003273 0.3661030 0.2822069 0.1925452 0.01187266
## [2,] 0.2741799 0.2599018 0.5377962 0.0109613 0.57099709
## [3,] 0.2648286 0.6953526 0.3555170 0.3632851 0.30061847
## [4,] 0.4227327 0.6932125 0.2428499 0.8804533 0.22483230
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.113618459 0.541689734 0.240790815 0.050799867 0.452692765
## [2,] 0.014035588 0.123790430 0.456639825 0.242425650 0.069349059
## [3,] 0.623672397 0.795788846 0.397062223 0.001140887 0.010407160
## [4,] 0.625217287 0.280951457 0.059067333 0.838091554 0.233248214
## 
## ,,2
##             [,1]        [,2]        [,3]        [,4]        [,5]
## [1,] 0.462707638 0.625983717 0.567388244 0.296327045 0.046510494
## [2,] 0.145164940 0.857525924 0.102044945 0.398570331 0.123140846
## [3,] 0.408475552 0.540037479 0.771755697 0.096274630 0.005171854
## [4,] 0.637214481 0.995934926 0.401962376 0.256605534 0.407338654
## 
## ,,3
##            [,1]       [,2]       [,3]       [,4]       [,5]
## [1,] 0.80032735 0.36610300 0.28220686 0.19254517 0.01187266
## [2,] 0.27417986 0.25990178 0.53779620 0.01096130 0.57099709
## [3,] 0.26482863 0.69535261 0.35551699 0.36328510 0.30061847
## [4,] 0.42273270 0.69321253 0.24284992 0.88045334 0.22483230

3.7 Summation + Permutation

Some examples of combining Summation and Permutation are shown below.

einsum::einsum('ijk->ki', arrE)
##          [,1]     [,2]     [,3]
## [1,] 2.035981 2.498609 2.583026
## [2,] 2.509953 3.450056 2.781342
## [3,] 2.039622 2.585198 2.353628
## [4,] 1.667006 1.992528 2.084552
## [5,] 1.521140 1.276724 1.887057
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]
## [1,] 2.035981 2.498609 2.583026
## [2,] 2.509953 3.450056 2.781342
## [3,] 2.039622 2.585198 2.353628
## [4,] 1.667006 1.992528 2.084552
## [5,] 1.521140 1.276724 1.887057

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.0361177670 0.172195820 0.076544134 0.0161485888 0.143904890
## [2,] 0.0007749229 0.006834629 0.025211674 0.0133846330 0.003828851
## [3,] 0.1214182745 0.154926383 0.077301176 0.0002221111 0.002026095
## [4,] 0.0443084334 0.019910708 0.004186034 0.0593945890 0.016530034
## 
## , , 2
## 
##            [,1]       [,2]       [,3]        [,4]         [,5]
## [1,] 0.12890728 0.17439491 0.15807060 0.082554749 0.0129575152
## [2,] 0.02053583 0.12131035 0.01443584 0.056383960 0.0174201842
## [3,] 0.01044257 0.01380592 0.01972974 0.002461236 0.0001322171
## [4,] 0.08381318 0.13099589 0.05287034 0.033751471 0.0535774840
## 
## , , 3
## 
##           [,1]      [,2]      [,3]        [,4]       [,5]
## [1,] 0.6860837 0.3138432 0.2419229 0.165060084 0.01017788
## [2,] 0.1637760 0.1552473 0.3212421 0.006547521 0.34107399
## [3,] 0.1033383 0.2713324 0.1387257 0.141756866 0.11730383
## [4,] 0.2675870 0.4387989 0.1537224 0.557321065 0.14231734
DelayedTensor::einsum('i,ij,ijk,ijk,ji->jki',
    darrA, darrC, darrE, darrE, t(darrC))
## <4 x 5 x 3> HDF5Array object of type "double":
## ,,1
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.0361177670 0.1721958196 0.0765441343 0.0161485888 0.1439048902
## [2,] 0.0007749229 0.0068346294 0.0252116740 0.0133846330 0.0038288510
## [3,] 0.1214182745 0.1549263829 0.0773011763 0.0002221111 0.0020260948
## [4,] 0.0443084334 0.0199107081 0.0041860343 0.0593945890 0.0165300339
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.1289072778 0.1743949103 0.1580706001 0.0825547486 0.0129575152
## [2,] 0.0205358342 0.1213103535 0.0144358415 0.0563839604 0.0174201842
## [3,] 0.0104425727 0.0138059196 0.0197297364 0.0024612362 0.0001322171
## [4,] 0.0838131818 0.1309958852 0.0528703391 0.0337514714 0.0535774840
## 
## ,,3
##             [,1]        [,2]        [,3]        [,4]        [,5]
## [1,] 0.686083671 0.313843195 0.241922909 0.165060084 0.010177882
## [2,] 0.163775999 0.155247266 0.321242083 0.006547521 0.341073987
## [3,] 0.103338333 0.271332368 0.138725683 0.141756866 0.117303826
## [4,] 0.267586968 0.438798887 0.153722370 0.557321065 0.142317337

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.3.2 (2023-10-31)
## Platform: aarch64-apple-darwin20 (64-bit)
## Running under: macOS Ventura 13.6.1
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib 
## LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0
## 
## locale:
## [1] C/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.10.0
##  [3] HDF5Array_1.30.0          rhdf5_2.46.0             
##  [5] DelayedArray_0.28.0       SparseArray_1.2.1        
##  [7] S4Arrays_1.2.0            abind_1.4-5              
##  [9] IRanges_2.36.0            S4Vectors_0.40.1         
## [11] MatrixGenerics_1.14.0     matrixStats_1.0.0        
## [13] BiocGenerics_0.48.1       Matrix_1.6-1.1           
## [15] DelayedTensor_1.8.0       BiocStyle_2.30.0         
## 
## loaded via a namespace (and not attached):
##  [1] jsonlite_1.8.7      compiler_4.3.2      BiocManager_1.30.22
##  [4] crayon_1.5.2        rsvd_1.0.5          Rcpp_1.0.11        
##  [7] rhdf5filters_1.14.1 parallel_4.3.2      jquerylib_0.1.4    
## [10] BiocParallel_1.36.0 yaml_2.3.7          fastmap_1.1.1      
## [13] lattice_0.22-5      R6_2.5.1            XVector_0.42.0     
## [16] ScaledMatrix_1.10.0 knitr_1.45          bookdown_0.36      
## [19] bslib_0.5.1         rlang_1.1.1         cachem_1.0.8       
## [22] xfun_0.41           sass_0.4.7          cli_3.6.1          
## [25] Rhdf5lib_1.24.0     BiocSingular_1.18.0 zlibbioc_1.48.0    
## [28] digest_0.6.33       grid_4.3.2          irlba_2.3.5.1      
## [31] rTensor_1.4.8       dqrng_0.3.1         evaluate_0.23      
## [34] codetools_0.2-19    beachmat_2.18.0     rmarkdown_2.25     
## [37] tools_4.3.2         htmltools_0.5.7