Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2024-04-30 20:49:30.349857
Compiled: Tue Apr 30 23:28:17 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.9487124 0.8275455 0.4375758
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.9487124 0.8275455 0.4375758
einsum::einsum('iii->i', arrD)
## [1] 0.18341200 0.09685853 0.81080375
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
##        [1]        [2]        [3] 
## 0.18341200 0.09685853 0.81080375

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.2401414 0.1489904 0.6583442
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.2401414 0.1489904 0.6583442
einsum::einsum('ij,ij->ij', arrC, arrC)
##             [,1]       [,2]        [,3]        [,4]
## [1,] 0.001492122 0.03060122 0.002125346 0.004760225
## [2,] 0.240417503 0.30131591 0.100361853 0.015452787
## [3,] 0.451567680 0.05276568 0.071976786 0.260793950
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.001492122 0.030601224 0.002125346 0.004760225
## [2,] 0.240417503 0.301315911 0.100361853 0.015452787
## [3,] 0.451567680 0.052765678 0.071976786 0.260793950
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
## 
##             [,1]         [,2]      [,3]        [,4]
## [1,] 0.680051840 0.0006773152 0.3232864 0.352670929
## [2,] 0.001740081 0.5807113524 0.6042390 0.001571592
## [3,] 0.144277445 0.2883326247 0.3964925 0.152046237
## 
## , , 2
## 
##           [,1]         [,2]       [,3]      [,4]
## [1,] 0.9087675 0.0907265564 0.01012496 0.6971637
## [2,] 0.8598552 0.0001642149 0.72811465 0.5006435
## [3,] 0.7184179 0.4590453054 0.56985025 0.2667539
## 
## , , 3
## 
##             [,1]        [,2]        [,3]      [,4]
## [1,] 0.986861568 0.528288205 0.228254024 0.9709909
## [2,] 0.744859430 0.001485533 0.001662746 0.8537530
## [3,] 0.003160245 0.007110335 0.113980779 0.9017350
## 
## , , 4
## 
##            [,1]      [,2]        [,3]      [,4]
## [1,] 0.30749603 0.1993086 0.584166517 0.8282052
## [2,] 0.01059173 0.1657257 0.332555947 0.3536602
## [3,] 0.19583698 0.2288832 0.001088835 0.4508122
## 
## , , 5
## 
##              [,1]       [,2]      [,3]      [,4]
## [1,] 2.245521e-02 0.01009552 0.2531346 0.1893713
## [2,] 1.081214e-01 0.24006362 0.8986942 0.1639324
## [3,] 6.828231e-05 0.20845085 0.3528121 0.6708887
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.6800518398 0.0006773152 0.3232863769 0.3526709293
## [2,] 0.0017400805 0.5807113524 0.6042390359 0.0015715921
## [3,] 0.1442774447 0.2883326247 0.3964925159 0.1520462369
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.9087674908 0.0907265564 0.0101249632 0.6971637420
## [2,] 0.8598551986 0.0001642149 0.7281146470 0.5006434580
## [3,] 0.7184179158 0.4590453054 0.5698502527 0.2667538879
## 
## ,,3
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.986861568 0.528288205 0.228254024 0.970990903
## [2,] 0.744859430 0.001485533 0.001662746 0.853752984
## [3,] 0.003160245 0.007110335 0.113980779 0.901735010
## 
## ,,4
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.307496025 0.199308569 0.584166517 0.828205158
## [2,] 0.010591726 0.165725658 0.332555947 0.353660201
## [3,] 0.195836981 0.228883189 0.001088835 0.450812208
## 
## ,,5
##              [,1]         [,2]         [,3]         [,4]
## [1,] 2.245521e-02 1.009552e-02 2.531346e-01 1.893713e-01
## [2,] 1.081214e-01 2.400636e-01 8.986942e-01 1.639324e-01
## [3,] 6.828231e-05 2.084509e-01 3.528121e-01 6.708887e-01

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.2401414 0.1891528 0.3976125
## [2,] 0.1891528 0.1489904 0.3131884
## [3,] 0.3976125 0.3131884 0.6583442
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 0.2401414 0.1891528 0.3976125
## [2,] 0.1891528 0.1489904 0.3131884
## [3,] 0.3976125 0.3131884 0.6583442
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
## 
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.03185468 0.1442582 0.0380177 0.0568964
## [2,] 0.40434684 0.4526703 0.2612494 0.1025119
## [3,] 0.55415650 0.1894291 0.2212418 0.4211335
## 
## , , 2, 1, 1
## 
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.001611339 0.007297163 0.001923089 0.002878051
## [2,] 0.020453504 0.022897903 0.013215056 0.005185469
## [3,] 0.028031485 0.009582094 0.011191309 0.021302640
## 
## , , 3, 1, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01467241 0.06644597 0.01751112 0.02620674
## [2,] 0.18624399 0.20850201 0.12033267 0.04721746
## [3,] 0.25524700 0.08725192 0.10190499 0.19397599
## 
## , , 1, 2, 1
## 
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.001005304 0.004552656 0.001199804 0.001795598
## [2,] 0.012760816 0.014285861 0.008244793 0.003235183
## [3,] 0.017488672 0.005978210 0.006982189 0.013290587
## 
## , , 2, 2, 1
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.02943624 0.1333060 0.03513136 0.05257677
## [2,] 0.37364846 0.4183032 0.24141514 0.09472913
## [3,] 0.51208444 0.1750475 0.20444495 0.38916064
## 
## , , 3, 2, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02074193 0.09393259 0.02475493 0.03704765
## [2,] 0.26328731 0.29475279 0.17011054 0.06674985
## [3,] 0.36083472 0.12334531 0.14405990 0.27421780
## 
## , , 1, 3, 1
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.02196321 0.09946335 0.0262125 0.03922902
## [2,] 0.27878971 0.31210788 0.1801267 0.07068009
## [3,] 0.38208072 0.13060791 0.1525422 0.29036379
## 
## , , 2, 3, 1
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.03002663 0.1359796 0.03583597 0.05363128
## [2,] 0.38114255 0.4266929 0.24625708 0.09662907
## [3,] 0.52235507 0.1785583 0.20854540 0.39696585
## 
## , , 3, 3, 1
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.02432314 0.1101506 0.02902902 0.04344414
## [2,] 0.30874543 0.3456436 0.19948114 0.07827461
## [3,] 0.42313497 0.1446416 0.16893270 0.32156313
## 
## , , 1, 4, 1
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.02293966 0.1038853 0.02737787 0.04097308
## [2,] 0.29118424 0.3259837 0.18813481 0.07382241
## [3,] 0.39906740 0.1364145 0.15932395 0.30327289
## 
## , , 2, 4, 1
## 
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.001531342 0.006934886 0.001827615 0.002735166
## [2,] 0.019438062 0.021761105 0.012558977 0.004928030
## [3,] 0.026639823 0.009106378 0.010635702 0.020245042
## 
## , , 3, 4, 1
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.01506226 0.06821144 0.0179764 0.02690305
## [2,] 0.19119251 0.21404194 0.1235299 0.04847203
## [3,] 0.26202894 0.08957021 0.1046126 0.19912995
## 
## , , 1, 1, 2
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.0368238 0.1667615 0.04394821 0.06577186
## [2,] 0.4674223 0.5232840 0.30200263 0.11850312
## [3,] 0.6406013 0.2189788 0.25575411 0.48682755
## 
## , , 2, 1, 2
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.03581912 0.1622117 0.04274915 0.06397737
## [2,] 0.45466937 0.5090069 0.29376293 0.11526994
## [3,] 0.62312344 0.2130043 0.24877623 0.47354518
## 
## , , 3, 1, 2
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.03274091 0.1482716 0.0390754 0.05847932
## [2,] 0.41559625 0.4652642 0.2685177 0.10536394
## [3,] 0.56957380 0.1946993 0.2273970 0.43284991
## 
## , , 1, 2, 2
## 
##            [,1]       [,2]       [,3]      [,4]
## [1,] 0.01163508 0.05269102 0.01388616 0.0207817
## [2,] 0.14768972 0.16534012 0.09542267 0.0374430
## [3,] 0.20240845 0.06918994 0.08080969 0.1538211
## 
## , , 2, 2, 2
## 
##              [,1]        [,2]         [,3]        [,4]
## [1,] 0.0004950037 0.002241691 0.0005907736 0.000884138
## [2,] 0.0062833222 0.007034242 0.0040596690 0.001592978
## [3,] 0.0086112800 0.002943622 0.0034379734 0.006544177
## 
## , , 3, 2, 2
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.02617158 0.1185215 0.03123508 0.04674569
## [2,] 0.33220856 0.3719108 0.21464072 0.08422309
## [3,] 0.45529114 0.1556337 0.18177075 0.34600034
## 
## , , 1, 3, 2
## 
##            [,1]       [,2]        [,3]        [,4]
## [1,] 0.00388686 0.01760217 0.004638863 0.006942414
## [2,] 0.04933780 0.05523416 0.031877266 0.012508353
## [3,] 0.06761735 0.02311386 0.026995598 0.051386079
## 
## , , 2, 3, 2
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.03296113 0.1492689 0.03933822 0.05887266
## [2,] 0.41839157 0.4683936 0.27032376 0.10607262
## [3,] 0.57340478 0.1960088 0.22892652 0.43576128
## 
## , , 3, 3, 2
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.02915967 0.1320535 0.03480128 0.05208278
## [2,] 0.37013778 0.4143730 0.23914687 0.09383909
## [3,] 0.50727306 0.1734028 0.20252405 0.38550421
## 
## , , 1, 4, 2
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.03225296 0.1460618 0.03849304 0.05760778
## [2,] 0.40940245 0.4583302 0.26451587 0.10379365
## [3,] 0.56108521 0.1917976 0.22400805 0.42639898
## 
## , , 2, 4, 2
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.02733169 0.1237752 0.03261963 0.04881778
## [2,] 0.34693436 0.3883965 0.22415509 0.08795645
## [3,] 0.47547282 0.1625324 0.18982810 0.36133749
## 
## , , 3, 4, 2
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.01995067 0.0903493 0.02381059 0.03563437
## [2,] 0.25324357 0.2835087 0.16362125 0.06420351
## [3,] 0.34706978 0.1186400 0.13856438 0.26375709
## 
## , , 1, 1, 3
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.0383734 0.1737791 0.04579762 0.06853965
## [2,] 0.4870922 0.5453046 0.31471138 0.12348992
## [3,] 0.6675588 0.2281938 0.26651665 0.50731403
## 
## , , 2, 1, 3
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.03333799 0.1509755 0.03978799 0.05954577
## [2,] 0.42317519 0.4737489 0.27341447 0.10728539
## [3,] 0.57996073 0.1982499 0.23154392 0.44074350
## 
## , , 3, 1, 3
## 
##             [,1]       [,2]        [,3]        [,4]
## [1,] 0.002171514 0.00983399 0.002591643 0.003878592
## [2,] 0.027564074 0.03085826 0.017809212 0.006988175
## [3,] 0.037776506 0.01291327 0.015081918 0.028708408
## 
## , , 1, 2, 3
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.02807616 0.1271466 0.03350814 0.05014749
## [2,] 0.35638425 0.3989757 0.23026069 0.09035223
## [3,] 0.48842387 0.1669595 0.19499869 0.37117970
## 
## , , 2, 2, 3
## 
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.001488824 0.006742337 0.001776871 0.002659223
## [2,] 0.018898360 0.021156904 0.012210274 0.004791202
## [3,] 0.025900163 0.008853538 0.010340399 0.019682934
## 
## , , 3, 2, 3
## 
##             [,1]       [,2]        [,3]       [,4]
## [1,] 0.003257221 0.01475076 0.003887406 0.00581780
## [2,] 0.041345484 0.04628668 0.026713413 0.01048210
## [3,] 0.056663900 0.01936961 0.022622535 0.04306196
## 
## , , 1, 3, 3
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01845489 0.08357543 0.02202541 0.03296271
## [2,] 0.23425683 0.26225287 0.15135388 0.05938990
## [3,] 0.32104850 0.10974506 0.12817563 0.24398211
## 
## , , 2, 3, 3
## 
##             [,1]        [,2]       [,3]        [,4]
## [1,] 0.001575125 0.007133165 0.00187987 0.002813369
## [2,] 0.019993828 0.022383291 0.01291806 0.005068930
## [3,] 0.027401499 0.009366744 0.01093979 0.020823880
## 
## , , 3, 3, 3
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01304121 0.05905888 0.01556434 0.02329322
## [2,] 0.16553844 0.18532194 0.10695477 0.04196809
## [3,] 0.22687009 0.07755174 0.09057577 0.17241084
## 
## , , 1, 4, 3
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.03806359 0.1723761 0.04542787 0.06798629
## [2,] 0.48315961 0.5409020 0.31217054 0.12249292
## [3,] 0.66216924 0.2263515 0.26436491 0.50321820
## 
## , , 2, 4, 3
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.03569179 0.1616350 0.04259719 0.06374995
## [2,] 0.45305315 0.5071976 0.29271869 0.11486019
## [3,] 0.62090841 0.2122472 0.24789190 0.47186186
## 
## , , 3, 4, 3
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.03668104 0.1661150 0.04377784 0.06551688
## [2,] 0.46561022 0.5212553 0.30083184 0.11804372
## [3,] 0.63811785 0.2181299 0.25476261 0.48494024
## 
## , , 1, 1, 4
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.02142012 0.09700389 0.02556434 0.03825899
## [2,] 0.27189598 0.30439028 0.17567262 0.06893236
## [3,] 0.37263288 0.12737832 0.14877021 0.28318387
## 
## , , 2, 1, 4
## 
##             [,1]       [,2]        [,3]        [,4]
## [1,] 0.003975443 0.01800333 0.004744585 0.007100634
## [2,] 0.050462227 0.05649297 0.032603761 0.012793424
## [3,] 0.069158378 0.02364063 0.027610839 0.052557189
## 
## , , 3, 1, 4
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.01709423 0.07741351 0.0204015 0.03053241
## [2,] 0.21698534 0.24291727 0.1401947 0.05501115
## [3,] 0.29737796 0.10165368 0.1187254 0.22599358
## 
## , , 1, 2, 4
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01724508 0.07809665 0.02058154 0.03080185
## [2,] 0.21890013 0.24506090 0.14143188 0.05549660
## [3,] 0.30000218 0.10255073 0.11977308 0.22798787
## 
## , , 2, 2, 4
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01572523 0.07121382 0.01876764 0.02808721
## [2,] 0.19960799 0.22346315 0.12896718 0.05060557
## [3,] 0.27356233 0.09351271 0.10921722 0.20789480
## 
## , , 3, 2, 4
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01848031 0.08369054 0.02205575 0.03300811
## [2,] 0.23457946 0.26261406 0.15156233 0.05947170
## [3,] 0.32149067 0.10989621 0.12835216 0.24431813
## 
## , , 1, 3, 4
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.02952368 0.1337019 0.03523572 0.05273295
## [2,] 0.37475840 0.4195458 0.24213227 0.09501053
## [3,] 0.51360561 0.1755675 0.20505226 0.39031666
## 
## , , 2, 3, 4
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.02227586 0.1008792 0.02658564 0.03978745
## [2,] 0.28275833 0.3165508 0.18269081 0.07168623
## [3,] 0.38751970 0.1324671 0.15471363 0.29449716
## 
## , , 3, 3, 4
## 
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.001274628 0.005772321 0.001521234 0.002276643
## [2,] 0.016179464 0.018113072 0.010453589 0.004101895
## [3,] 0.022173922 0.007579784 0.008852732 0.016851162
## 
## , , 1, 4, 4
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.03515371 0.1591983 0.04195501 0.06278888
## [2,] 0.44622306 0.4995512 0.28830575 0.11312859
## [3,] 0.61154778 0.2090474 0.24415476 0.46474821
## 
## , , 2, 4, 4
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.02297181 0.1040309 0.02741624 0.04103050
## [2,] 0.29159236 0.3264406 0.18839850 0.07392588
## [3,] 0.39962672 0.1366057 0.15954725 0.30369794
## 
## , , 3, 4, 4
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.02593582 0.1174538 0.03095371 0.04632459
## [2,] 0.32921596 0.3685606 0.21270719 0.08346439
## [3,] 0.45118979 0.1542317 0.18013332 0.34288350
## 
## , , 1, 1, 5
## 
##            [,1]       [,2]        [,3]       [,4]
## [1,] 0.00578843 0.02621368 0.006908335 0.01033885
## [2,] 0.07347534 0.08225638 0.047472586 0.01862782
## [3,] 0.10069780 0.03442186 0.040202659 0.07652570
## 
## , , 2, 1, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01270159 0.05752084 0.01515900 0.02268661
## [2,] 0.16122739 0.18049569 0.10416939 0.04087514
## [3,] 0.22096181 0.07553210 0.08821695 0.16792082
## 
## , , 3, 1, 5
## 
##              [,1]        [,2]         [,3]         [,4]
## [1,] 0.0003191952 0.001445518 0.0003809508 0.0005701221
## [2,] 0.0040516986 0.004535917 0.0026178118 0.0010272059
## [3,] 0.0055528446 0.001898147 0.0022169215 0.0042199067
## 
## , , 1, 2, 5
## 
##             [,1]       [,2]        [,3]        [,4]
## [1,] 0.003881204 0.01757655 0.004632113 0.006932311
## [2,] 0.049266005 0.05515379 0.031830880 0.012490152
## [3,] 0.067518958 0.02308023 0.026956316 0.051311305
## 
## , , 2, 2, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01892628 0.08571021 0.02258801 0.03380469
## [2,] 0.24024049 0.26895165 0.15521994 0.06090691
## [3,] 0.32924910 0.11254830 0.13144964 0.25021419
## 
## , , 3, 2, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01763616 0.07986771 0.02104828 0.03150037
## [2,] 0.22386432 0.25061835 0.14463925 0.05675515
## [3,] 0.30680559 0.10487636 0.12248928 0.23315815
## 
## , , 1, 3, 5
## 
##            [,1]       [,2]      [,3]       [,4]
## [1,] 0.01943471 0.08801266 0.0231948 0.03471279
## [2,] 0.24669413 0.27617655 0.1593896 0.06254307
## [3,] 0.33809379 0.11557171 0.1349808 0.25693575
## 
## , , 2, 3, 5
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.03661914 0.1658347 0.04370396 0.06540632
## [2,] 0.46482449 0.5203757 0.30032418 0.11784451
## [3,] 0.63704100 0.2177618 0.25433269 0.48412188
## 
## , , 3, 3, 5
## 
##            [,1]      [,2]       [,3]       [,4]
## [1,] 0.02294425 0.1039061 0.02738335 0.04098128
## [2,] 0.29124250 0.3260489 0.18817246 0.07383718
## [3,] 0.39914725 0.1364418 0.15935582 0.30333357
## 
## , , 1, 4, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01680967 0.07612485 0.02006189 0.03002416
## [2,] 0.21337332 0.23887358 0.13786100 0.05409542
## [3,] 0.29242770 0.09996152 0.11674904 0.22223161
## 
## , , 2, 4, 5
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01563992 0.07082747 0.01866582 0.02793483
## [2,] 0.19852508 0.22225082 0.12826752 0.05033102
## [3,] 0.27207822 0.09300539 0.10862470 0.20676694
## 
## , , 3, 4, 5
## 
##            [,1]      [,2]      [,3]       [,4]
## [1,] 0.03163934 0.1432830 0.0377607 0.05651178
## [2,] 0.40161346 0.4496103 0.2594834 0.10181895
## [3,] 0.55041042 0.1881486 0.2197462 0.41828663
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.03185468 0.14425817 0.03801770 0.05689640
## [2,] 0.40434684 0.45267034 0.26124943 0.10251193
## [3,] 0.55415650 0.18942913 0.22124183 0.42113348
## 
## ,,2,1,1
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.001611339 0.007297163 0.001923089 0.002878051
## [2,] 0.020453504 0.022897903 0.013215056 0.005185469
## [3,] 0.028031485 0.009582094 0.011191309 0.021302640
## 
## ,,3,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01467241 0.06644597 0.01751112 0.02620674
## [2,] 0.18624399 0.20850201 0.12033267 0.04721746
## [3,] 0.25524700 0.08725192 0.10190499 0.19397599
## 
## ...
## 
## ,,1,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01680967 0.07612485 0.02006189 0.03002416
## [2,] 0.21337332 0.23887358 0.13786100 0.05409542
## [3,] 0.29242770 0.09996152 0.11674904 0.22223161
## 
## ,,2,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01563992 0.07082747 0.01866582 0.02793483
## [2,] 0.19852508 0.22225082 0.12826752 0.05033102
## [3,] 0.27207822 0.09300539 0.10862470 0.20676694
## 
## ,,3,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.03163934 0.14328299 0.03776070 0.05651178
## [2,] 0.40161346 0.44961030 0.25948339 0.10181895
## [3,] 0.55041042 0.18814860 0.21974624 0.41828663

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

3.3.1 Row-wise / Column-wise Summation

einsum::einsum('ij->i', arrC)
## [1] 0.3286559 1.4803550 1.6806605
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.3286559 1.4803550 1.6806605
einsum::einsum('ij->j', arrC)
## [1] 1.2009397 0.9535624 0.6311858 0.7039835
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
##       [1]       [2]       [3]       [4] 
## 1.2009397 0.9535624 0.6311858 0.7039835

3.3.2 Mode-wise Vectorization

einsum::einsum('ijk->i', arrE)
## [1] 11.250550  9.841087  9.663910
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 11.250550  9.841087  9.663910
einsum::einsum('ijk->j', arrE)
## [1] 7.473961 5.545242 7.959619 9.776724
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 7.473961 5.545242 7.959619 9.776724
einsum::einsum('ijk->k', arrE)
## [1] 5.570264 7.487535 6.477497 5.982084 5.238167
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4]      [5] 
## 5.570264 7.487535 6.477497 5.982084 5.238167

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,] 3.475728 1.600984 2.414398 3.759439
## [2,] 2.263785 1.710458 3.196073 2.670771
## [3,] 1.734448 2.233799 2.349149 3.346514
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]     [,4]
## [1,] 3.475728 1.600984 2.414398 3.759439
## [2,] 2.263785 1.710458 3.196073 2.670771
## [3,] 1.734448 2.233799 2.349149 3.346514
einsum::einsum('ijk->jk', arrE)
##          [,1]     [,2]      [,3]     [,4]      [,5]
## [1,] 1.246206 2.728172 1.9126775 1.099974 0.4869321
## [2,] 1.325036 0.991552 0.8496998 1.331951 1.0470035
## [3,] 1.975587 1.708803 0.8561466 1.373982 2.0450995
## [4,] 1.023435 2.059007 2.8589735 2.176177 1.6591317
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.2462055 2.7281721 1.9126775 1.0999741 0.4869321
## [2,] 1.3250356 0.9915520 0.8496998 1.3319514 1.0470035
## [3,] 1.9755874 1.7088035 0.8561466 1.3739820 2.0450995
## [4,] 1.0234354 2.0590072 2.8589735 2.1761766 1.6591317
einsum::einsum('ijk->jk', arrE)
##          [,1]     [,2]      [,3]     [,4]      [,5]
## [1,] 1.246206 2.728172 1.9126775 1.099974 0.4869321
## [2,] 1.325036 0.991552 0.8496998 1.331951 1.0470035
## [3,] 1.975587 1.708803 0.8561466 1.373982 2.0450995
## [4,] 1.023435 2.059007 2.8589735 2.176177 1.6591317
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.2462055 2.7281721 1.9126775 1.0999741 0.4869321
## [2,] 1.3250356 0.9915520 0.8496998 1.3319514 1.0470035
## [3,] 1.9755874 1.7088035 0.8561466 1.3739820 2.0450995
## [4,] 1.0234354 2.0590072 2.8589735 2.1761766 1.6591317

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

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.9487124 0.4865367 0.3120466
## [2,] 0.9198248 0.8275455 0.3651485
## [3,] 0.5219029 0.2371892 0.4375758
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 0.9487124 0.4865367 0.3120466
## [2,] 0.9198248 0.8275455 0.3651485
## [3,] 0.5219029 0.2371892 0.4375758
einsum::einsum('ijk->jki', arrD)
## , , 1
## 
##            [,1]      [,2]       [,3]
## [1,] 0.18341200 0.1208492 0.97885203
## [2,] 0.05435189 0.9892235 0.22477836
## [3,] 0.98983241 0.4205073 0.03742131
## 
## , , 2
## 
##           [,1]       [,2]      [,3]
## [1,] 0.2899530 0.46197456 0.8362635
## [2,] 0.8684128 0.09685853 0.2140245
## [3,] 0.2181789 0.56400738 0.8445627
## 
## , , 3
## 
##           [,1]      [,2]      [,3]
## [1,] 0.2545215 0.1483334 0.6303677
## [2,] 0.2660790 0.3554440 0.8176630
## [3,] 0.6628449 0.5976142 0.8108037
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
##            [,1]       [,2]       [,3]
## [1,] 0.18341200 0.12084919 0.97885203
## [2,] 0.05435189 0.98922352 0.22477836
## [3,] 0.98983241 0.42050732 0.03742131
## 
## ,,2
##            [,1]       [,2]       [,3]
## [1,] 0.28995305 0.46197456 0.83626353
## [2,] 0.86841280 0.09685853 0.21402452
## [3,] 0.21817889 0.56400738 0.84456271
## 
## ,,3
##           [,1]      [,2]      [,3]
## [1,] 0.2545215 0.1483334 0.6303677
## [2,] 0.2660790 0.3554440 0.8176630
## [3,] 0.6628449 0.5976142 0.8108037

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

3.5.2 Contracted Product

The inner product is an operation that eliminates all subscripts, while the outer product is an operation that leaves all subscripts intact. In the middle of the two, the operation that eliminates some subscripts while keeping others by summing them is called contracted product.

einsum::einsum('ijk,ijk->jk', arrE, arrE)
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.8260694 2.4870406 1.7348812 0.5139247 0.1306449
## [2,] 0.8697213 0.5499361 0.5368841 0.5939174 0.4586100
## [3,] 1.3240179 1.3080899 0.3438975 0.9178113 1.5046408
## [4,] 0.5062888 1.4645611 2.7264789 1.6326776 1.0241923
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.8260694 2.4870406 1.7348812 0.5139247 0.1306449
## [2,] 0.8697213 0.5499361 0.5368841 0.5939174 0.4586100
## [3,] 1.3240179 1.3080899 0.3438975 0.9178113 1.5046408
## [4,] 0.5062888 1.4645611 2.7264789 1.6326776 1.0241923

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.03897892 0.1381459 0.1137432
## [2,] 0.13814593 0.6575481 0.6040582
## [3,] 0.11374318 0.6040582 0.8371041
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.03897892 0.13814593 0.11374318
## [2,] 0.13814593 0.65754805 0.60405820
## [3,] 0.11374318 0.60405820 0.83710409

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.001492122 0.24041750 0.45156768
## [2,] 0.030601224 0.30131591 0.05276568
## [3,] 0.002125346 0.10036185 0.07197679
## [4,] 0.004760225 0.01545279 0.26079395
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
##             [,1]        [,2]        [,3]
## [1,] 0.001492122 0.240417503 0.451567680
## [2,] 0.030601224 0.301315911 0.052765678
## [3,] 0.002125346 0.100361853 0.071976786
## [4,] 0.004760225 0.015452787 0.260793950
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
## 
##              [,1]       [,2]      [,3]      [,4]       [,5]
## [1,] 0.6800518398 0.90876749 0.9868616 0.3074960 0.02245521
## [2,] 0.0006773152 0.09072656 0.5282882 0.1993086 0.01009552
## [3,] 0.3232863769 0.01012496 0.2282540 0.5841665 0.25313462
## [4,] 0.3526709293 0.69716374 0.9709909 0.8282052 0.18937130
## 
## , , 2
## 
##             [,1]         [,2]        [,3]       [,4]      [,5]
## [1,] 0.001740081 0.8598551986 0.744859430 0.01059173 0.1081214
## [2,] 0.580711352 0.0001642149 0.001485533 0.16572566 0.2400636
## [3,] 0.604239036 0.7281146470 0.001662746 0.33255595 0.8986942
## [4,] 0.001571592 0.5006434580 0.853752984 0.35366020 0.1639324
## 
## , , 3
## 
##           [,1]      [,2]        [,3]        [,4]         [,5]
## [1,] 0.1442774 0.7184179 0.003160245 0.195836981 6.828231e-05
## [2,] 0.2883326 0.4590453 0.007110335 0.228883189 2.084509e-01
## [3,] 0.3964925 0.5698503 0.113980779 0.001088835 3.528121e-01
## [4,] 0.1520462 0.2667539 0.901735010 0.450812208 6.708887e-01
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.6800518398 0.9087674908 0.9868615678 0.3074960253 0.0224552095
## [2,] 0.0006773152 0.0907265564 0.5282882045 0.1993085691 0.0100955181
## [3,] 0.3232863769 0.0101249632 0.2282540240 0.5841665170 0.2531346197
## [4,] 0.3526709293 0.6971637420 0.9709909028 0.8282051580 0.1893712986
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.0017400805 0.8598551986 0.7448594296 0.0105917261 0.1081213826
## [2,] 0.5807113524 0.0001642149 0.0014855325 0.1657256576 0.2400636165
## [3,] 0.6042390359 0.7281146470 0.0016627456 0.3325559468 0.8986941515
## [4,] 0.0015715921 0.5006434580 0.8537529839 0.3536602006 0.1639323614
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 1.442774e-01 7.184179e-01 3.160245e-03 1.958370e-01 6.828231e-05
## [2,] 2.883326e-01 4.590453e-01 7.110335e-03 2.288832e-01 2.084509e-01
## [3,] 3.964925e-01 5.698503e-01 1.139808e-01 1.088835e-03 3.528121e-01
## [4,] 1.520462e-01 2.667539e-01 9.017350e-01 4.508122e-01 6.708887e-01

3.7 Summation + Permutation

Some examples of combining Summation and Permutation are shown below.

einsum::einsum('ijk->ki', arrE)
##          [,1]     [,2]     [,3]
## [1,] 2.013122 1.620730 1.936412
## [2,] 2.190088 2.500956 2.796491
## [3,] 3.183392 1.866359 1.427747
## [4,] 2.675329 1.681380 1.625375
## [5,] 1.188620 2.171661 1.877885
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]
## [1,] 2.013122 1.620730 1.936412
## [2,] 2.190088 2.500956 2.796491
## [3,] 3.183392 1.866359 1.427747
## [4,] 2.675329 1.681380 1.625375
## [5,] 1.188620 2.171661 1.877885

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,] 4.972559e-04 6.644934e-04 0.0007215960 0.0002248420 1.641931e-05
## [2,] 1.015694e-05 1.360526e-03 0.0079221524 0.0029888096 1.513913e-04
## [3,] 3.367057e-04 1.054524e-05 0.0002377287 0.0006084148 2.636420e-04
## [4,] 8.226795e-04 1.626282e-03 0.0022650414 0.0019319635 4.417486e-04
## 
## , , 2
## 
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 1.614785e-04 7.979407e-02 6.912253e-02 0.0009829061 0.0100336020
## [2,] 6.754008e-02 1.909914e-05 1.727760e-04 0.0192748503 0.0279207839
## [3,] 2.340759e-02 2.820640e-02 6.441302e-05 0.0128828689 0.0348144697
## [4,] 9.374019e-06 2.986170e-03 5.092350e-03 0.0021094644 0.0009778015
## 
## , , 3
## 
##            [,1]       [,2]         [,3]         [,4]         [,5]
## [1,] 0.05286251 0.26322462 0.0011578974 7.175366e-02 2.501829e-05
## [2,] 0.01234445 0.01965321 0.0003044164 9.799230e-03 8.924455e-03
## [3,] 0.02315549 0.03327972 0.0066565712 6.358888e-05 2.060451e-02
## [4,] 0.03217360 0.05644621 0.1908107999 9.539370e-02 1.419628e-01
DelayedTensor::einsum('i,ij,ijk,ijk,ji->jki',
    darrA, darrC, darrE, darrE, t(darrC))
## <4 x 5 x 3> HDF5Array object of type "double":
## ,,1
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 4.972559e-04 6.644934e-04 7.215960e-04 2.248420e-04 1.641931e-05
## [2,] 1.015694e-05 1.360526e-03 7.922152e-03 2.988810e-03 1.513913e-04
## [3,] 3.367057e-04 1.054524e-05 2.377287e-04 6.084148e-04 2.636420e-04
## [4,] 8.226795e-04 1.626282e-03 2.265041e-03 1.931964e-03 4.417486e-04
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 1.614785e-04 7.979407e-02 6.912253e-02 9.829061e-04 1.003360e-02
## [2,] 6.754008e-02 1.909914e-05 1.727760e-04 1.927485e-02 2.792078e-02
## [3,] 2.340759e-02 2.820640e-02 6.441302e-05 1.288287e-02 3.481447e-02
## [4,] 9.374019e-06 2.986170e-03 5.092350e-03 2.109464e-03 9.778015e-04
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 5.286251e-02 2.632246e-01 1.157897e-03 7.175366e-02 2.501829e-05
## [2,] 1.234445e-02 1.965321e-02 3.044164e-04 9.799230e-03 8.924455e-03
## [3,] 2.315549e-02 3.327972e-02 6.656571e-03 6.358888e-05 2.060451e-02
## [4,] 3.217360e-02 5.644621e-02 1.908108e-01 9.539370e-02 1.419628e-01

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 beta (2024-04-15 r86425)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 22.04.4 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.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.3.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.46          bookdown_0.39      
## [19] bslib_0.7.0         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-20    beachmat_2.20.0    
## [37] rmarkdown_2.26      tools_4.4.0         htmltools_0.5.8.1