Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2023-10-20 14:48:21.036969
Compiled: Tue Oct 24 19:03:35 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.4200485 0.9008985 0.6553774
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.4200485 0.9008985 0.6553774
einsum::einsum('iii->i', arrD)
## [1] 0.7135298 0.5345016 0.7826825
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.7135298 0.5345016 0.7826825

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.06915333 0.11295868 0.36978951
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
##        [1]        [2]        [3] 
## 0.06915333 0.11295868 0.36978951
einsum::einsum('ij,ij->ij', arrC, arrC)
##           [,1]         [,2]        [,3]        [,4]
## [1,] 0.6873445 0.1574331270 0.173003993 0.003008926
## [2,] 0.5435359 0.1739698433 0.007479815 0.152796252
## [3,] 0.6054440 0.0004179006 0.536359956 0.141541972
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.6873445394 0.1574331270 0.1730039933 0.0030089258
## [2,] 0.5435359201 0.1739698433 0.0074798148 0.1527962520
## [3,] 0.6054439761 0.0004179006 0.5363599564 0.1415419723
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
## 
##           [,1]       [,2]         [,3]         [,4]
## [1,] 0.1453879 0.03315927 0.0871090323 0.0004848728
## [2,] 0.2451393 0.86458864 0.0008075832 0.0260972892
## [3,] 0.9177970 0.63514138 0.6240924296 0.8867651118
## 
## , , 2
## 
##              [,1]       [,2]      [,3]        [,4]
## [1,] 3.114611e-06 0.09304736 0.3994082 0.421865026
## [2,] 4.703581e-03 0.03758110 0.8547559 0.817510874
## [3,] 4.573718e-01 0.93764228 0.4265359 0.005633881
## 
## , , 3
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.4435234 0.80542276 0.3842885 0.01389213
## [2,] 0.2205556 0.05236496 0.7304182 0.30859012
## [3,] 0.1152802 0.00680069 0.2300045 0.42967284
## 
## , , 4
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.9885807 0.02065745 0.14321277 0.2531936
## [2,] 0.7484741 0.25182559 0.47304061 0.9628224
## [3,] 0.2061419 0.51001104 0.04002736 0.2545050
## 
## , , 5
## 
##              [,1]         [,2]      [,3]      [,4]
## [1,] 0.0015156174 0.2443402080 0.2283971 0.3007475
## [2,] 0.0066026690 0.2317554480 0.1850947 0.8363865
## [3,] 0.0003311362 0.0004613333 0.7958239 0.9816080
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.1453879260 0.0331592720 0.0871090323 0.0004848728
## [2,] 0.2451393138 0.8645886394 0.0008075832 0.0260972892
## [3,] 0.9177969702 0.6351413793 0.6240924296 0.8867651118
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]
## [1,] 3.114611e-06 9.304736e-02 3.994082e-01 4.218650e-01
## [2,] 4.703581e-03 3.758110e-02 8.547559e-01 8.175109e-01
## [3,] 4.573718e-01 9.376423e-01 4.265359e-01 5.633881e-03
## 
## ,,3
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.44352338 0.80542276 0.38428848 0.01389213
## [2,] 0.22055563 0.05236496 0.73041817 0.30859012
## [3,] 0.11528021 0.00680069 0.23000447 0.42967284
## 
## ,,4
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.98858073 0.02065745 0.14321277 0.25319362
## [2,] 0.74847412 0.25182559 0.47304061 0.96282242
## [3,] 0.20614189 0.51001104 0.04002736 0.25450503
## 
## ,,5
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.0015156174 0.2443402080 0.2283970582 0.3007474840
## [2,] 0.0066026690 0.2317554480 0.1850947266 0.8363865200
## [3,] 0.0003311362 0.0004613333 0.7958238926 0.9816079810

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.06915333 0.08838252 0.1599130
## [2,] 0.08838252 0.11295868 0.2043794
## [3,] 0.15991303 0.20437939 0.3697895
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.06915333 0.08838252 0.15991303
## [2,] 0.08838252 0.11295868 0.20437939
## [3,] 0.15991303 0.20437939 0.36978951
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.3161196 0.151290700 0.15859600 0.02091558
## [2,] 0.2811113 0.159038092 0.03297688 0.14904607
## [3,] 0.2966888 0.007794722 0.27924946 0.14345206
## 
## , , 2, 1, 1
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.4104816 0.19645114 0.20593708 0.0271589
## [2,] 0.3650233 0.20651113 0.04282052 0.1935365
## [3,] 0.3852507 0.01012146 0.36260572 0.1862727
## 
## , , 3, 1, 1
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.7942561 0.38012057 0.3984753 0.05255077
## [2,] 0.7062971 0.39958603 0.0828550 0.37448089
## [3,] 0.7454359 0.01958438 0.7016192 0.36042585
## 
## , , 1, 2, 1
## 
##           [,1]        [,2]       [,3]        [,4]
## [1,] 0.1509697 0.072252113 0.07574092 0.009988683
## [2,] 0.1342507 0.075952046 0.01574882 0.071180141
## [3,] 0.1416901 0.003722537 0.13336156 0.068508604
## 
## , , 2, 2, 1
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.7708893 0.36893752 0.38675223 0.05100474
## [2,] 0.6855180 0.38783031 0.08041743 0.36346376
## [3,] 0.7235053 0.01900821 0.68097777 0.34982221
## 
## , , 3, 2, 1
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.6607276 0.3162156 0.33148453 0.04371605
## [2,] 0.5875561 0.3324086 0.06892561 0.31152403
## [3,] 0.6201149 0.0162919 0.58366463 0.29983189
## 
## , , 1, 3, 1
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.2446915 0.117106137 0.12276079 0.01618965
## [2,] 0.2175934 0.123102984 0.02552566 0.11536869
## [3,] 0.2296511 0.006033483 0.21615225 0.11103866
## 
## , , 2, 3, 1
## 
##            [,1]         [,2]        [,3]        [,4]
## [1,] 0.02356030 0.0112756531 0.011820115 0.001558832
## [2,] 0.02095115 0.0118530641 0.002457758 0.011108361
## [3,] 0.02211213 0.0005809385 0.020812384 0.010691442
## 
## , , 3, 3, 1
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.6549554 0.31345306 0.32858862 0.04333414
## [2,] 0.5824231 0.32950457 0.06832346 0.30880250
## [3,] 0.6146975 0.01614957 0.57856563 0.29721251
## 
## , , 1, 4, 1
## 
##            [,1]         [,2]        [,3]        [,4]
## [1,] 0.01825581 0.0087369931 0.009158872 0.001207869
## [2,] 0.01623409 0.0091844027 0.001904405 0.008607366
## [3,] 0.01713369 0.0004501429 0.016126574 0.008284314
## 
## , , 2, 4, 1
## 
##           [,1]        [,2]       [,3]        [,4]
## [1,] 0.1339322 0.064098189 0.06719327 0.008861422
## [2,] 0.1191000 0.067380571 0.01397150 0.063147193
## [3,] 0.1256998 0.003302434 0.11831120 0.060777148
## 
## , , 3, 4, 1
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.7807132 0.37363914 0.39168087 0.05165472
## [2,] 0.6942541 0.39277269 0.08144224 0.36809562
## [3,] 0.7327255 0.01925045 0.68965593 0.35428023
## 
## , , 1, 1, 2
## 
##             [,1]         [,2]         [,3]         [,4]
## [1,] 0.001463151 7.002449e-04 0.0007340573 9.680719e-05
## [2,] 0.001301116 7.361035e-04 0.0001526326 6.898557e-04
## [3,] 0.001373216 3.607766e-05 0.0012924986 6.639640e-04
## 
## , , 2, 1, 2
## 
##            [,1]        [,2]        [,3]        [,4]
## [1,] 0.05685931 0.027212121 0.028526100 0.003762011
## [2,] 0.05056249 0.028605617 0.005931435 0.026808387
## [3,] 0.05336436 0.001402009 0.050227609 0.025802213
## 
## , , 3, 1, 2
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.5606888 0.26833834 0.28129547 0.03709714
## [2,] 0.4985960 0.28207959 0.05848979 0.26435713
## [3,] 0.5262252 0.01382519 0.49529375 0.25443526
## 
## , , 1, 2, 2
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.2528944 0.121031966 0.12687618 0.01673238
## [2,] 0.2248879 0.127229850 0.02638138 0.11923627
## [3,] 0.2373499 0.006235747 0.22339847 0.11476109
## 
## , , 2, 2, 2
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.1607208 0.076918857 0.08063300 0.01063385
## [2,] 0.1429219 0.080857768 0.01676603 0.07577765
## [3,] 0.1508418 0.003962974 0.14197535 0.07293356
## 
## , , 3, 2, 2
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.8027972 0.38420822 0.40276030 0.05311587
## [2,] 0.7138923 0.40388300 0.08374599 0.37850789
## [3,] 0.7534520 0.01979498 0.70916413 0.36430171
## 
## , , 1, 3, 2
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.5239571 0.25075901 0.26286729 0.03466684
## [2,] 0.4659321 0.26360004 0.05465802 0.24703861
## [3,] 0.4917512 0.01291948 0.46284615 0.23776674
## 
## , , 2, 3, 2
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.7664932 0.36683361 0.38454673 0.05071388
## [2,] 0.6816088 0.38561866 0.07995884 0.36139106
## [3,] 0.7193795 0.01889981 0.67709441 0.34782731
## 
## , , 3, 3, 2
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.5414584 0.25913488 0.27164761 0.03582478
## [2,] 0.4814952 0.27240483 0.05648371 0.25529021
## [3,] 0.5081768 0.01335102 0.47830618 0.24570865
## 
## , , 1, 4, 2
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.5384855 0.25771211 0.27015613 0.03562809
## [2,] 0.4788515 0.27090920 0.05617359 0.25388855
## [3,] 0.5053866 0.01327771 0.47568005 0.24435959
## 
## , , 2, 4, 2
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.7496077 0.35875241 0.37607532 0.04959667
## [2,] 0.6665932 0.37712364 0.07819738 0.35342976
## [3,] 0.7035318 0.01848346 0.66217830 0.34016482
## 
## , , 3, 4, 2
## 
##            [,1]        [,2]        [,3]        [,4]
## [1,] 0.06222875 0.029781865 0.031219928 0.004117272
## [2,] 0.05533730 0.031306954 0.006491563 0.029340005
## [3,] 0.05840376 0.001534406 0.054970793 0.028238814
## 
## , , 1, 1, 3
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.5521353 0.26424472 0.27700418 0.03653121
## [2,] 0.4909897 0.27777634 0.05759751 0.26032424
## [3,] 0.5181974 0.01361428 0.48773782 0.25055374
## 
## , , 2, 1, 3
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.3893555 0.186340447 0.19533818 0.02576112
## [2,] 0.3462368 0.195882689 0.04061669 0.18357580
## [3,] 0.3654231 0.009600538 0.34394361 0.17668582
## 
## , , 3, 1, 3
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.2814911 0.134717943 0.14122300 0.01862444
## [2,] 0.2503177 0.141616666 0.02936451 0.13271919
## [3,] 0.2641888 0.006940869 0.24865979 0.12773797
## 
## , , 1, 2, 3
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.7440450 0.3560902 0.37328455 0.04922862
## [2,] 0.6616466 0.3743251 0.07761709 0.35080704
## [3,] 0.6983111 0.0183463 0.65726442 0.33764053
## 
## , , 2, 2, 3
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.1897176 0.090796360 0.09518060 0.01255238
## [2,] 0.1687075 0.095445919 0.01979091 0.08944925
## [3,] 0.1780563 0.004677964 0.16759017 0.08609204
## 
## , , 3, 2, 3
## 
##            [,1]        [,2]        [,3]       [,4]
## [1,] 0.06836971 0.032720849 0.034300825 0.00452358
## [2,] 0.06079819 0.034396439 0.007132174 0.03223538
## [3,] 0.06416726 0.001685827 0.060395512 0.03102552
## 
## , , 1, 3, 3
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.5139442 0.24596694 0.25784383 0.03400435
## [2,] 0.4570280 0.25856258 0.05361349 0.24231764
## [3,] 0.4823538 0.01267258 0.45400105 0.23322296
## 
## , , 2, 3, 3
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.7085541 0.33910473 0.35547892 0.04688042
## [2,] 0.6300861 0.35646982 0.07391477 0.33407358
## [3,] 0.6650017 0.01747118 0.62591298 0.32153511
## 
## , , 3, 3, 3
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.3976082 0.19029010 0.19947855 0.02630715
## [2,] 0.3535756 0.20003460 0.04147759 0.18746685
## [3,] 0.3731686 0.00980403 0.35123381 0.18043084
## 
## , , 1, 4, 3
## 
##            [,1]        [,2]       [,3]        [,4]
## [1,] 0.09771734 0.046766241 0.04902442 0.006465322
## [2,] 0.08689575 0.049161077 0.01019365 0.046072391
## [3,] 0.09171099 0.002409466 0.08632023 0.044343199
## 
## , , 2, 4, 3
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.4605516 0.22041394 0.2310570 0.0304717
## [2,] 0.4095483 0.23170104 0.0480437 0.2171438
## [3,] 0.4322430 0.01135605 0.4068358 0.2089939
## 
## , , 3, 4, 3
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.5434457 0.26008602 0.27264467 0.03595627
## [2,] 0.4832625 0.27340467 0.05669103 0.25622724
## [3,] 0.5100420 0.01340002 0.48006177 0.24661050
## 
## , , 1, 1, 4
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.8243152 0.39450647 0.4135558 0.05453958
## [2,] 0.7330274 0.41470861 0.0859907 0.38865335
## [3,] 0.7736474 0.02032556 0.7281724 0.37406639
## 
## , , 2, 1, 4
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.7172584 0.34327048 0.35984582 0.04745633
## [2,] 0.6378264 0.36084889 0.07482278 0.33817753
## [3,] 0.6731710 0.01768581 0.63360204 0.32548503
## 
## , , 3, 1, 4
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.3764180 0.180148723 0.18884748 0.02490513
## [2,] 0.3347320 0.189373895 0.03926707 0.17747594
## [3,] 0.3532809 0.009281531 0.33251504 0.17081490
## 
## , , 1, 2, 4
## 
##           [,1]        [,2]       [,3]        [,4]
## [1,] 0.1191587 0.057027776 0.05978145 0.007883955
## [2,] 0.1059626 0.059948092 0.01243036 0.056181681
## [3,] 0.1118344 0.002938156 0.10526077 0.054073067
## 
## , , 2, 2, 4
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.4160420 0.19911225 0.20872669 0.0275268
## [2,] 0.3699679 0.20930852 0.04340056 0.1961581
## [3,] 0.3904693 0.01025856 0.36751757 0.1887959
## 
## , , 3, 2, 4
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.5920754 0.28335955 0.29704199 0.03917378
## [2,] 0.5265067 0.29787001 0.06176397 0.27915547
## [3,] 0.5556826 0.01459911 0.52301960 0.26867819
## 
## , , 1, 3, 4
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.3137459 0.150154699 0.15740515 0.02075853
## [2,] 0.2790005 0.157843918 0.03272927 0.14792692
## [3,] 0.2944610 0.007736194 0.27715265 0.14237492
## 
## , , 2, 3, 4
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.5702121 0.27289607 0.28607327 0.03772723
## [2,] 0.5070647 0.28687070 0.05948324 0.26884723
## [3,] 0.5351631 0.01406001 0.50370631 0.25875684
## 
## , , 3, 3, 4
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.1658692 0.079382827 0.08321595 0.01097449
## [2,] 0.1475002 0.083447914 0.01730310 0.07820506
## [3,] 0.1556738 0.004089922 0.14652329 0.07526986
## 
## , , 1, 4, 4
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.4171705 0.19965236 0.20929287 0.02760146
## [2,] 0.3709715 0.20987628 0.04351829 0.19669020
## [3,] 0.3915285 0.01028639 0.36851447 0.18930801
## 
## , , 2, 4, 4
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.8135052 0.38933295 0.40813248 0.05382436
## [2,] 0.7234145 0.40927016 0.08486303 0.38355659
## [3,] 0.7635018 0.02005901 0.71862326 0.36916092
## 
## , , 3, 4, 4
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.4182495 0.20016873 0.20983419 0.02767285
## [2,] 0.3719309 0.21041910 0.04363084 0.19719892
## [3,] 0.3925411 0.01031299 0.36946760 0.18979764
## 
## , , 1, 1, 5
## 
##            [,1]         [,2]        [,3]        [,4]
## [1,] 0.03227617 0.0154469538 0.016192833 0.002135505
## [2,] 0.02870179 0.0162379714 0.003366977 0.015217774
## [3,] 0.03029227 0.0007958501 0.028511690 0.014646620
## 
## , , 2, 1, 5
## 
##            [,1]        [,2]       [,3]        [,4]
## [1,] 0.06736697 0.032240950 0.03379775 0.004457235
## [2,] 0.05990649 0.033891965 0.00702757 0.031762605
## [3,] 0.06322615 0.001661102 0.05950972 0.030570489
## 
## , , 3, 1, 5
## 
##            [,1]         [,2]        [,3]         [,4]
## [1,] 0.01508657 0.0072202360 0.007568876 0.0009981805
## [2,] 0.01341583 0.0075899745 0.001573797 0.0071131125
## [3,] 0.01415925 0.0003719973 0.013326973 0.0068461428
## 
## , , 1, 2, 5
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.4098120 0.19613068 0.20560115 0.0271146
## [2,] 0.3644279 0.20617427 0.04275067 0.1932208
## [3,] 0.3846223 0.01010494 0.36201423 0.1859688
## 
## , , 2, 2, 5
## 
##           [,1]        [,2]       [,3]      [,4]
## [1,] 0.3991188 0.191013049 0.20023641 0.0264071
## [2,] 0.3549189 0.200794569 0.04163518 0.1881791
## [3,] 0.3745864 0.009841277 0.35256821 0.1811163
## 
## , , 3, 2, 5
## 
##            [,1]         [,2]        [,3]        [,4]
## [1,] 0.01780716 0.0085222738 0.008933785 0.001178184
## [2,] 0.01583513 0.0089586879 0.001857603 0.008395833
## [3,] 0.01671261 0.0004390802 0.015730249 0.008080720
## 
## , , 1, 3, 5
## 
##           [,1]        [,2]       [,3]       [,4]
## [1,] 0.3962164 0.189624004 0.19878029 0.02621507
## [2,] 0.3523379 0.199334393 0.04133241 0.18681064
## [3,] 0.3718624 0.009769711 0.35000434 0.17979925
## 
## , , 2, 3, 5
## 
##           [,1]        [,2]       [,3]      [,4]
## [1,] 0.3566845 0.170704545 0.17894727 0.0235995
## [2,] 0.3171839 0.179446094 0.03720852 0.1681719
## [3,] 0.3347603 0.008794953 0.31508316 0.1618600
## 
## , , 3, 3, 5
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.7395980 0.35396192 0.37105352 0.0489344
## [2,] 0.6576921 0.37208784 0.07715319 0.3487103
## [3,] 0.6941374 0.01823665 0.65333611 0.3356225
## 
## , , 1, 4, 5
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.4546616 0.21759508 0.22810199 0.0300820
## [2,] 0.4043106 0.22873783 0.04742927 0.2143667
## [3,] 0.4267151 0.01121082 0.40163280 0.2063211
## 
## , , 2, 4, 5
## 
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.7582122 0.36287042 0.38039218 0.05016597
## [2,] 0.6742448 0.38145253 0.07909498 0.35748668
## [3,] 0.7116075 0.01869563 0.66977925 0.34406947
## 
## , , 3, 4, 5
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.8214030 0.39311272 0.41209477 0.0543469
## [2,] 0.7304377 0.41324350 0.08568691 0.3872803
## [3,] 0.7709142 0.02025375 0.72559990 0.3727449
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.316119593 0.151290700 0.158596002 0.020915580
## [2,] 0.281111295 0.159038092 0.032976882 0.149046067
## [3,] 0.296688800 0.007794722 0.279249461 0.143452061
## 
## ,,2,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.41048163 0.19645114 0.20593708 0.02715890
## [2,] 0.36502332 0.20651113 0.04282052 0.19353648
## [3,] 0.38525072 0.01012146 0.36260572 0.18627265
## 
## ,,3,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.79425609 0.38012057 0.39847527 0.05255077
## [2,] 0.70629712 0.39958603 0.08285500 0.37448089
## [3,] 0.74543588 0.01958438 0.70161923 0.36042585
## 
## ...
## 
## ,,1,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.45466157 0.21759508 0.22810199 0.03008200
## [2,] 0.40431060 0.22873783 0.04742927 0.21436671
## [3,] 0.42671507 0.01121082 0.40163280 0.20632109
## 
## ,,2,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.75821218 0.36287042 0.38039218 0.05016597
## [2,] 0.67424485 0.38145253 0.07909498 0.35748668
## [3,] 0.71160746 0.01869563 0.66977925 0.34406947
## 
## ,,3,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.82140300 0.39311272 0.41209477 0.05434690
## [2,] 0.73043768 0.41324350 0.08568691 0.38728029
## [3,] 0.77091416 0.02025375 0.72559990 0.37274486

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

3.3.1 Row-wise / Column-wise Summation

einsum::einsum('ij->i', arrC)
## [1] 1.696632 1.631723 1.907132
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
##      [1]      [2]      [3] 
## 1.696632 1.631723 1.907132
einsum::einsum('ij->j', arrC)
## [1] 2.3444141 0.8343180 1.2347894 0.8219659
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
##       [1]       [2]       [3]       [4] 
## 2.3444141 0.8343180 1.2347894 0.8219659

3.3.2 Mode-wise Vectorization

einsum::einsum('ijk->i', arrE)
## [1]  8.349232 10.758084 11.211757
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
##  8.349232 10.758084 11.211757
einsum::einsum('ijk->j', arrE)
## [1] 6.508040 6.941751 8.343821 8.525461
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 6.508040 6.941751 8.343821 8.525461
einsum::einsum('ijk->k', arrE)
## [1] 5.982121 6.052203 5.966903 6.928332 5.389514
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4]      [5] 
## 5.982121 6.052203 5.966903 6.928332 5.389514

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.082243 2.022622 2.403384 1.840984
## [2,] 1.979732 2.335757 2.925599 3.516995
## [3,] 2.446065 2.583372 3.014838 3.167482
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]     [,4]
## [1,] 2.082243 2.022622 2.403384 1.840984
## [2,] 1.979732 2.335757 2.925599 3.516995
## [3,] 2.446065 2.583372 3.014838 3.167482
einsum::einsum('ijk->jk', arrE)
##          [,1]      [,2]     [,3]     [,4]      [,5]
## [1,] 1.834430 0.7466401 1.475138 2.313446 0.1383849
## [2,] 1.908887 1.4672144 1.208754 1.359700 0.9971963
## [3,] 1.113556 2.2096148 1.954143 1.266283 1.8002250
## [4,] 1.125248 1.6287336 1.328868 1.988904 2.4537075
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.8344303 0.7466401 1.4751384 2.3134465 0.1383849
## [2,] 1.9088870 1.4672144 1.2087537 1.3596998 0.9971963
## [3,] 1.1135556 2.2096148 1.9541429 1.2662825 1.8002250
## [4,] 1.1252484 1.6287336 1.3288681 1.9889035 2.4537075
einsum::einsum('ijk->jk', arrE)
##          [,1]      [,2]     [,3]     [,4]      [,5]
## [1,] 1.834430 0.7466401 1.475138 2.313446 0.1383849
## [2,] 1.908887 1.4672144 1.208754 1.359700 0.9971963
## [3,] 1.113556 2.2096148 1.954143 1.266283 1.8002250
## [4,] 1.125248 1.6287336 1.328868 1.988904 2.4537075
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.8344303 0.7466401 1.4751384 2.3134465 0.1383849
## [2,] 1.9088870 1.4672144 1.2087537 1.3596998 0.9971963
## [3,] 1.1135556 2.2096148 1.9541429 1.2662825 1.8002250
## [4,] 1.1252484 1.6287336 1.3288681 1.9889035 2.4537075

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

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.4200485 0.8837633 0.6857667
## [2,] 0.5573708 0.9008985 0.1107852
## [3,] 0.1543078 0.3227742 0.6553774
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 0.4200485 0.8837633 0.6857667
## [2,] 0.5573708 0.9008985 0.1107852
## [3,] 0.1543078 0.3227742 0.6553774
einsum::einsum('ijk->jki', arrD)
## , , 1
## 
##           [,1]       [,2]       [,3]
## [1,] 0.7135298 0.10558662 0.14789722
## [2,] 0.7324247 0.91817988 0.02967713
## [3,] 0.5964449 0.03823761 0.41915364
## 
## , , 2
## 
##           [,1]      [,2]      [,3]
## [1,] 0.8250967 0.8156074 0.4303639
## [2,] 0.7977488 0.5345016 0.3899781
## [3,] 0.5893655 0.5896236 0.1305676
## 
## , , 3
## 
##            [,1]      [,2]      [,3]
## [1,] 0.19081569 0.1247063 0.4195805
## [2,] 0.03621812 0.6413666 0.6800148
## [3,] 0.26878162 0.6911105 0.7826825
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
##            [,1]       [,2]       [,3]
## [1,] 0.71352982 0.10558662 0.14789722
## [2,] 0.73242472 0.91817988 0.02967713
## [3,] 0.59644491 0.03823761 0.41915364
## 
## ,,2
##           [,1]      [,2]      [,3]
## [1,] 0.8250967 0.8156074 0.4303639
## [2,] 0.7977488 0.5345016 0.3899781
## [3,] 0.5893655 0.5896236 0.1305676
## 
## ,,3
##            [,1]       [,2]       [,3]
## [1,] 0.19081569 0.12470633 0.41958055
## [2,] 0.03621812 0.64136657 0.68001477
## [3,] 0.26878162 0.69111047 0.78268250

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

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.3083242 0.4620785 0.7793592 1.9431967 0.008449423
## [2,] 1.5328893 1.0682707 0.8645884 0.7824941 0.476556989
## [3,] 0.7120090 1.6807000 1.3447111 0.6562807 1.209315677
## [4,] 0.9133473 1.2450098 0.7521551 1.4705211 2.118741985
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
##             [,1]        [,2]        [,3]        [,4]        [,5]
## [1,] 1.308324210 0.462078453 0.779359221 1.943196735 0.008449423
## [2,] 1.532889291 1.068270738 0.864588404 0.782494081 0.476556989
## [3,] 0.712009045 1.680700039 1.344711128 0.656280744 1.209315677
## [4,] 0.913347274 1.245009781 0.752155081 1.470521058 2.118741985

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.0207906 0.8341350 0.9784626
## [2,] 0.8341350 0.8777818 0.7925828
## [3,] 0.9784626 0.7925828 1.2837638
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 1.0207906 0.8341350 0.9784626
## [2,] 0.8341350 0.8777818 0.7925828
## [3,] 0.9784626 0.7925828 1.2837638

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.687344539 0.543535920 0.6054439761
## [2,] 0.157433127 0.173969843 0.0004179006
## [3,] 0.173003993 0.007479815 0.5363599564
## [4,] 0.003008926 0.152796252 0.1415419723
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
##              [,1]         [,2]         [,3]
## [1,] 0.6873445394 0.5435359201 0.6054439761
## [2,] 0.1574331270 0.1739698433 0.0004179006
## [3,] 0.1730039933 0.0074798148 0.5363599564
## [4,] 0.0030089258 0.1527962520 0.1415419723
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
## 
##              [,1]         [,2]       [,3]       [,4]        [,5]
## [1,] 0.1453879260 3.114611e-06 0.44352338 0.98858073 0.001515617
## [2,] 0.0331592720 9.304736e-02 0.80542276 0.02065745 0.244340208
## [3,] 0.0871090323 3.994082e-01 0.38428848 0.14321277 0.228397058
## [4,] 0.0004848728 4.218650e-01 0.01389213 0.25319362 0.300747484
## 
## , , 2
## 
##              [,1]        [,2]       [,3]      [,4]        [,5]
## [1,] 0.2451393138 0.004703581 0.22055563 0.7484741 0.006602669
## [2,] 0.8645886394 0.037581103 0.05236496 0.2518256 0.231755448
## [3,] 0.0008075832 0.854755915 0.73041817 0.4730406 0.185094727
## [4,] 0.0260972892 0.817510874 0.30859012 0.9628224 0.836386520
## 
## , , 3
## 
##           [,1]        [,2]       [,3]       [,4]         [,5]
## [1,] 0.9177970 0.457371757 0.11528021 0.20614189 0.0003311362
## [2,] 0.6351414 0.937642275 0.00680069 0.51001104 0.0004613333
## [3,] 0.6240924 0.426535944 0.23000447 0.04002736 0.7958238926
## [4,] 0.8867651 0.005633881 0.42967284 0.25450503 0.9816079810
DelayedTensor::einsum('ijk,ijk->jki', darrE, darrE)
## <4 x 5 x 3> HDF5Array object of type "double":
## ,,1
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 1.453879e-01 3.114611e-06 4.435234e-01 9.885807e-01 1.515617e-03
## [2,] 3.315927e-02 9.304736e-02 8.054228e-01 2.065745e-02 2.443402e-01
## [3,] 8.710903e-02 3.994082e-01 3.842885e-01 1.432128e-01 2.283971e-01
## [4,] 4.848728e-04 4.218650e-01 1.389213e-02 2.531936e-01 3.007475e-01
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.2451393138 0.0047035814 0.2205556273 0.7484741168 0.0066026690
## [2,] 0.8645886394 0.0375811031 0.0523649571 0.2518255895 0.2317554480
## [3,] 0.0008075832 0.8547559146 0.7304181733 0.4730406126 0.1850947266
## [4,] 0.0260972892 0.8175108745 0.3085901167 0.9628224151 0.8363865200
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.9177969702 0.4573717572 0.1152802108 0.2061418898 0.0003311362
## [2,] 0.6351413793 0.9376422751 0.0068006902 0.5100110398 0.0004613333
## [3,] 0.6240924296 0.4265359438 0.2300044710 0.0400273646 0.7958238926
## [4,] 0.8867651118 0.0056338808 0.4296728359 0.2545050263 0.9816079810

3.7 Summation + Permutation

Some examples of combining Summation and Permutation are shown below.

einsum::einsum('ijk->ki', arrE)
##           [,1]     [,2]     [,3]
## [1,] 0.8805568 1.614913 3.486652
## [2,] 1.5883003 2.091134 2.372768
## [3,] 2.3012039 2.108621 1.557078
## [4,] 2.0196191 3.035981 1.872732
## [5,] 1.5595523 1.907435 1.922527
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 0.8805568 1.6149126 3.4866521
## [2,] 1.5883003 2.0911343 2.3727683
## [3,] 2.3012039 2.1086213 1.5570779
## [4,] 2.0196191 3.0359809 1.8727324
## [5,] 1.5595523 1.9074347 1.9225268

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,] 2.627903e-02 5.629695e-07 8.016736e-02 0.1786870933 0.0002739496
## [2,] 1.372801e-03 3.852181e-03 3.334468e-02 0.0008552231 0.0101157391
## [3,] 3.963016e-03 1.817103e-02 1.748316e-02 0.0065154501 0.0103909006
## [4,] 3.836594e-07 3.338040e-04 1.099226e-05 0.0002003414 0.0002379688
## 
## , , 2
## 
##              [,1]         [,2]        [,3]        [,4]         [,5]
## [1,] 4.478175e-02 0.0008592444 0.040290829 0.136730326 0.0012061674
## [2,] 5.055258e-02 0.0021973706 0.003061784 0.014724266 0.0135507626
## [3,] 2.030196e-06 0.0021487841 0.001836210 0.001189184 0.0004653125
## [4,] 1.340195e-03 0.0419822830 0.015847273 0.049444582 0.0429516190
## 
## , , 3
## 
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.3379075341 0.1683916679 4.244299e-02 0.0758957590 1.219152e-04
## [2,] 0.0001614064 0.0002382799 1.728237e-06 0.0001296074 1.172371e-07
## [3,] 0.2035553654 0.1391199056 7.501877e-02 0.0130554136 2.595677e-01
## [4,] 0.0763257594 0.0004849201 3.698285e-02 0.0219057890 8.448909e-02
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,] 2.627903e-02 5.629695e-07 8.016736e-02 1.786871e-01 2.739496e-04
## [2,] 1.372801e-03 3.852181e-03 3.334468e-02 8.552231e-04 1.011574e-02
## [3,] 3.963016e-03 1.817103e-02 1.748316e-02 6.515450e-03 1.039090e-02
## [4,] 3.836594e-07 3.338040e-04 1.099226e-05 2.003414e-04 2.379688e-04
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 4.478175e-02 8.592444e-04 4.029083e-02 1.367303e-01 1.206167e-03
## [2,] 5.055258e-02 2.197371e-03 3.061784e-03 1.472427e-02 1.355076e-02
## [3,] 2.030196e-06 2.148784e-03 1.836210e-03 1.189184e-03 4.653125e-04
## [4,] 1.340195e-03 4.198228e-02 1.584727e-02 4.944458e-02 4.295162e-02
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 3.379075e-01 1.683917e-01 4.244299e-02 7.589576e-02 1.219152e-04
## [2,] 1.614064e-04 2.382799e-04 1.728237e-06 1.296074e-04 1.172371e-07
## [3,] 2.035554e-01 1.391199e-01 7.501877e-02 1.305541e-02 2.595677e-01
## [4,] 7.632576e-02 4.849201e-04 3.698285e-02 2.190579e-02 8.448909e-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 Under development (unstable) (2023-10-22 r85388)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 22.04.3 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.19-bioc/R/lib/libRblas.so 
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## time zone: 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.11.0
##  [3] HDF5Array_1.31.0          rhdf5_2.47.0             
##  [5] DelayedArray_0.29.0       SparseArray_1.3.0        
##  [7] S4Arrays_1.3.0            abind_1.4-5              
##  [9] IRanges_2.37.0            S4Vectors_0.41.0         
## [11] MatrixGenerics_1.15.0     matrixStats_1.0.0        
## [13] BiocGenerics_0.49.0       Matrix_1.6-1.1           
## [15] DelayedTensor_1.9.0       BiocStyle_2.31.0         
## 
## loaded via a namespace (and not attached):
##  [1] jsonlite_1.8.7      compiler_4.4.0      BiocManager_1.30.22
##  [4] crayon_1.5.2        rsvd_1.0.5          Rcpp_1.0.11        
##  [7] rhdf5filters_1.15.0 parallel_4.4.0      jquerylib_0.1.4    
## [10] BiocParallel_1.37.0 yaml_2.3.7          fastmap_1.1.1      
## [13] lattice_0.22-5      R6_2.5.1            XVector_0.43.0     
## [16] ScaledMatrix_1.11.0 knitr_1.44          bookdown_0.36      
## [19] bslib_0.5.1         rlang_1.1.1         cachem_1.0.8       
## [22] xfun_0.40           sass_0.4.7          cli_3.6.1          
## [25] Rhdf5lib_1.25.0     BiocSingular_1.19.0 zlibbioc_1.49.0    
## [28] digest_0.6.33       grid_4.4.0          irlba_2.3.5.1      
## [31] rTensor_1.4.8       dqrng_0.3.1         evaluate_0.22      
## [34] codetools_0.2-19    beachmat_2.19.0     rmarkdown_2.25     
## [37] tools_4.4.0         htmltools_0.5.6.1