DelayedTensor 1.18.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2026-04-28 14:31:28.508828
Compiled: Tue Apr 28 17:42:57 2026
einsumeinsum 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)
DelayedTensorCRAN 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.
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/.
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)
If the same subscript is written on both sides of ->,
einsum will simply output the object without any calculation.
einsum::einsum('i->i', arrA)
## [1] 0.8321224 0.1863563 0.4425933
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.8321224 0.1863563 0.4425933
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.67751444 0.5861652 0.2549415 0.8540375
## [2,] 0.21366082 0.3128272 0.5922864 0.4115734
## [3,] 0.03764164 0.3094880 0.1013203 0.7468116
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.67751444 0.58616517 0.25494148 0.85403746
## [2,] 0.21366082 0.31282721 0.59228638 0.41157342
## [3,] 0.03764164 0.30948798 0.10132030 0.74681162
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.66874953 0.1672199 0.9704708 0.7366113
## [2,] 0.03952633 0.2599051 0.6868535 0.9194146
## [3,] 0.26235585 0.7364755 0.6848040 0.3424103
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04044775 0.4766366 0.0931760 0.3384080
## [2,] 0.40900390 0.2224128 0.4297570 0.4892755
## [3,] 0.60510254 0.4706364 0.8000195 0.3510274
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5262733 0.50797262 0.29044465 0.1819803
## [2,] 0.2907483 0.96892508 0.03712517 0.9843407
## [3,] 0.4684330 0.03874361 0.80821249 0.3079731
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7654967 0.51531140 0.1342951 0.3830414
## [2,] 0.8471176 0.79370552 0.2397016 0.1937215
## [3,] 0.2226755 0.02783344 0.4718165 0.7406293
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03676887 0.6774569 0.1531663 0.4779110
## [2,] 0.33284748 0.8986078 0.3113874 0.1513781
## [3,] 0.76416935 0.6344679 0.2281096 0.5487377
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.66874953 0.16721993 0.97047081 0.73661129
## [2,] 0.03952633 0.25990511 0.68685355 0.91941456
## [3,] 0.26235585 0.73647545 0.68480396 0.34241033
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.04044775 0.47663664 0.09317600 0.33840797
## [2,] 0.40900390 0.22241278 0.42975699 0.48927549
## [3,] 0.60510254 0.47063636 0.80001951 0.35102735
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.52627333 0.50797262 0.29044465 0.18198034
## [2,] 0.29074832 0.96892508 0.03712517 0.98434069
## [3,] 0.46843305 0.03874361 0.80821249 0.30797312
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.76549673 0.51531140 0.13429514 0.38304142
## [2,] 0.84711759 0.79370552 0.23970156 0.19372152
## [3,] 0.22267550 0.02783344 0.47181655 0.74062927
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.03676887 0.67745686 0.15316628 0.47791105
## [2,] 0.33284748 0.89860780 0.31138735 0.15137807
## [3,] 0.76416935 0.63446789 0.22810956 0.54873773
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.67886992 0.09253485 0.07705046
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.67886992 0.09253485 0.07705046
einsum::einsum('iii->i', arrD)
## [1] 0.9804450 0.5695084 0.2018241
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.9804450 0.5695084 0.2018241
By using multiple arrays or DelayedArray objects as input and writing “,” on the right side of ->, multiplication will be performed.
Hadamard Product can also be implemented in einsum,
multiplying by the product of each element.
einsum::einsum('i,i->i', arrA, arrA)
## [1] 0.69242771 0.03472868 0.19588885
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.69242771 0.03472868 0.19588885
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.459025816 0.34358961 0.06499516 0.7293800
## [2,] 0.045650944 0.09786086 0.35080316 0.1693927
## [3,] 0.001416893 0.09578281 0.01026580 0.5577276
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.459025816 0.343589607 0.064995156 0.729379985
## [2,] 0.045650944 0.097860865 0.350803160 0.169392676
## [3,] 0.001416893 0.095782812 0.010265804 0.557727590
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.447225935 0.02796250 0.9418136 0.5425962
## [2,] 0.001562331 0.06755067 0.4717678 0.8453231
## [3,] 0.068830594 0.54239609 0.4689565 0.1172448
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.001636021 0.22718248 0.008681768 0.1145200
## [2,] 0.167284192 0.04946744 0.184691073 0.2393905
## [3,] 0.366149088 0.22149858 0.640031215 0.1232202
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.27696362 0.258036185 0.084358095 0.03311684
## [2,] 0.08453458 0.938815802 0.001378279 0.96892660
## [3,] 0.21942952 0.001501067 0.653207425 0.09484744
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.58598524 0.2655458413 0.01803519 0.14672073
## [2,] 0.71760821 0.6299684480 0.05745684 0.03752803
## [3,] 0.04958438 0.0007747007 0.22261085 0.54853171
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.00135195 0.4589478 0.02345991 0.22839897
## [2,] 0.11078745 0.8074960 0.09696208 0.02291532
## [3,] 0.58395479 0.4025495 0.05203397 0.30111309
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.447225935 0.027962504 0.941813601 0.542596198
## [2,] 0.001562331 0.067550667 0.471767794 0.845323141
## [3,] 0.068830594 0.542396094 0.468956458 0.117244833
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.001636021 0.227182482 0.008681768 0.114519954
## [2,] 0.167284192 0.049467443 0.184691073 0.239390504
## [3,] 0.366149088 0.221498583 0.640031215 0.123220202
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.276963616 0.258036185 0.084358095 0.033116845
## [2,] 0.084534584 0.938815802 0.001378279 0.968926600
## [3,] 0.219429521 0.001501067 0.653207425 0.094847445
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.5859852449 0.2655458413 0.0180351855 0.1467207302
## [2,] 0.7176082074 0.6299684480 0.0574568397 0.0375280277
## [3,] 0.0495843784 0.0007747007 0.2226108531 0.5485317104
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.00135195 0.45894780 0.02345991 0.22839897
## [2,] 0.11078745 0.80749597 0.09696208 0.02291532
## [3,] 0.58395479 0.40254951 0.05203397 0.30111309
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.6924277 0.15507128 0.36829183
## [2,] 0.1550713 0.03472868 0.08248007
## [3,] 0.3682918 0.08248007 0.19588885
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.69242771 0.15507128 0.36829183
## [2,] 0.15507128 0.03472868 0.08248007
## [3,] 0.36829183 0.08248007 0.19588885
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.45308746 0.3919977 0.17049199 0.5711372
## [2,] 0.14288557 0.2092031 0.39609124 0.2752395
## [3,] 0.02517283 0.2069699 0.06775791 0.4994299
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.026779662 0.02316896 0.01007690 0.03375697
## [2,] 0.008445229 0.01236491 0.02341091 0.01626799
## [3,] 0.001487836 0.01223293 0.00400482 0.02951873
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.177749879 0.15378386 0.06688539 0.2240617
## [2,] 0.056055166 0.08207205 0.15538980 0.1079787
## [3,] 0.009875505 0.08119598 0.02658197 0.1959304
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.113293915 0.09801850 0.04263130 0.14281208
## [2,] 0.035728346 0.05231094 0.09904209 0.06882328
## [3,] 0.006294432 0.05175256 0.01694277 0.12488178
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.176089466 0.15234732 0.06626059 0.2219687
## [2,] 0.055531538 0.08130539 0.15393826 0.1069700
## [3,] 0.009783255 0.08043751 0.02633366 0.1941002
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.49897275 0.4316963 0.18775814 0.6289776
## [2,] 0.15735595 0.2303896 0.43620438 0.3031137
## [3,] 0.02772214 0.2279303 0.07461992 0.5500084
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.65750799 0.5688562 0.2474133 0.8288184
## [2,] 0.20735159 0.3035897 0.5747966 0.3994200
## [3,] 0.03653011 0.3003491 0.0983284 0.7247589
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.46535320 0.4026096 0.17510746 0.5865987
## [2,] 0.14675369 0.2148665 0.40681400 0.2826907
## [3,] 0.02585429 0.2125729 0.06959221 0.5129502
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.46396457 0.4014082 0.17458493 0.5848482
## [2,] 0.14631577 0.2142253 0.40560006 0.2818471
## [3,] 0.02577714 0.2119386 0.06938454 0.5114195
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.49906479 0.4317759 0.18779277 0.6290936
## [2,] 0.15738497 0.2304321 0.43628484 0.3031696
## [3,] 0.02772726 0.2279723 0.07463368 0.5501099
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.62291664 0.5389288 0.23439691 0.7852145
## [2,] 0.19644287 0.2876179 0.54455673 0.3784066
## [3,] 0.03460827 0.2845478 0.09315536 0.6866295
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.23198794 0.2007090 0.08729459 0.2924312
## [2,] 0.07315967 0.1071153 0.20280497 0.1409270
## [3,] 0.01288889 0.1059719 0.03469312 0.2557160
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02740394 0.02370906 0.010311810 0.03454390
## [2,] 0.00864210 0.01265316 0.023956653 0.01664722
## [3,] 0.00152252 0.01251809 0.004098179 0.03020685
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.27710605 0.2397438 0.1042721 0.3493047
## [2,] 0.08738811 0.1279476 0.2422474 0.1683351
## [3,] 0.01539558 0.1265818 0.0414404 0.3054489
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.40996571 0.3546900 0.15426574 0.5167802
## [2,] 0.12928670 0.1892925 0.35839400 0.2490441
## [3,] 0.02277705 0.1872720 0.06130917 0.4518976
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.32292820 0.2793878 0.12151445 0.4070655
## [2,] 0.10183857 0.1491049 0.28230539 0.1961710
## [3,] 0.01794138 0.1475133 0.04829297 0.3559578
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.150687868 0.13037062 0.05670224 0.18994884
## [2,] 0.047520895 0.06957677 0.13173206 0.09153919
## [3,] 0.008371982 0.06883408 0.02253493 0.16610045
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.31886293 0.2758706 0.11998473 0.4019411
## [2,] 0.10055655 0.1472279 0.27875151 0.1937014
## [3,] 0.01771552 0.1456563 0.04768502 0.3514767
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.063128089 0.05461653 0.023754428 0.07957580
## [2,] 0.019908061 0.02914799 0.055186879 0.03834877
## [3,] 0.003507298 0.02883685 0.009440621 0.06958492
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.29116657 0.2519086 0.10956288 0.3670286
## [2,] 0.09182223 0.1344397 0.25453922 0.1768766
## [3,] 0.01617676 0.1330046 0.04354311 0.3209475
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.54202477 0.4689436 0.20395815 0.6832466
## [2,] 0.17093282 0.2502679 0.47384066 0.3292668
## [3,] 0.03011405 0.2475964 0.08105822 0.5974639
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.22927629 0.1983630 0.08627423 0.2890131
## [2,] 0.07230452 0.1058632 0.20043443 0.1392797
## [3,] 0.01273823 0.1047332 0.03428760 0.2527270
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.33149121 0.2867963 0.12473662 0.4178596
## [2,] 0.10453900 0.1530587 0.28979121 0.2013728
## [3,] 0.01841713 0.1514249 0.04957354 0.3653966
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.23782610 0.2057600 0.08949143 0.2997905
## [2,] 0.07500079 0.1098109 0.20790872 0.1444735
## [3,] 0.01321325 0.1086387 0.03556620 0.2621513
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.35655778 0.3084831 0.13416890 0.4494571
## [2,] 0.11244399 0.1646326 0.31170453 0.2166001
## [3,] 0.01980979 0.1628753 0.05332217 0.3930270
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.19698618 0.17042654 0.07412381 0.2483100
## [2,] 0.06212152 0.09095399 0.17220627 0.1196643
## [3,] 0.01094424 0.08998311 0.02945871 0.2171342
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.31737015 0.2745791 0.11942301 0.4000594
## [2,] 0.10008579 0.1465386 0.27744652 0.1927946
## [3,] 0.01763259 0.1449744 0.04746178 0.3498312
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.34415879 0.2977559 0.12950329 0.4338276
## [2,] 0.10853384 0.1589077 0.30086527 0.2090680
## [3,] 0.01912092 0.1572114 0.05146794 0.3793599
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.65646073 0.5679501 0.24701919 0.8274983
## [2,] 0.20702132 0.3031061 0.57388113 0.3987838
## [3,] 0.03647193 0.2998707 0.09817178 0.7236045
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.026249353 0.02271015 0.009877352 0.03308849
## [2,] 0.008277991 0.01212005 0.022947311 0.01594584
## [3,] 0.001458373 0.01199068 0.003925514 0.02893418
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.19678044 0.17024854 0.07404639 0.2480506
## [2,] 0.06205664 0.09085899 0.17202641 0.1195393
## [3,] 0.01093281 0.08988913 0.02942794 0.2169074
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.025152842 0.02176148 0.009464747 0.03170629
## [2,] 0.007932195 0.01161376 0.021988736 0.01527974
## [3,] 0.001397452 0.01148980 0.003761534 0.02772551
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.54757563 0.4737460 0.20604688 0.6902437
## [2,] 0.17268334 0.2528309 0.47869325 0.3326388
## [3,] 0.03042244 0.2501321 0.08188833 0.6035825
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.123294309 0.10667054 0.04639434 0.15541803
## [2,] 0.038882068 0.05692840 0.10778448 0.07489827
## [3,] 0.006850039 0.05632073 0.01843830 0.13590503
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6669050 0.5769862 0.2509493 0.8406638
## [2,] 0.2103150 0.3079286 0.5830116 0.4051285
## [3,] 0.0370522 0.3046416 0.0997337 0.7351171
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.20865624 0.18052312 0.07851512 0.2630206
## [2,] 0.06580179 0.09634237 0.18240829 0.1267536
## [3,] 0.01159261 0.09531398 0.03120393 0.2299979
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.51863509 0.4487075 0.19515687 0.6537629
## [2,] 0.16355666 0.2394682 0.45339329 0.3150581
## [3,] 0.02881455 0.2369120 0.07756036 0.5716819
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5739344 0.4965508 0.21596541 0.7234702
## [2,] 0.1809958 0.2650014 0.50173621 0.3486511
## [3,] 0.0318869 0.2621727 0.08583021 0.6326373
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.150865867 0.13052462 0.05676922 0.19017322
## [2,] 0.047577029 0.06965896 0.13188767 0.09164732
## [3,] 0.008381871 0.06891539 0.02256155 0.16629665
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.34913092 0.3020576 0.13137425 0.4400952
## [2,] 0.11010185 0.1612034 0.30521193 0.2120885
## [3,] 0.01939717 0.1594827 0.05221151 0.3848405
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.53774695 0.4652425 0.20234846 0.6778542
## [2,] 0.16958377 0.2482927 0.47010097 0.3266681
## [3,] 0.02987638 0.2456423 0.08041848 0.5927485
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.018857561 0.016314996 0.007095900 0.02377080
## [2,] 0.005946917 0.008707059 0.016485370 0.01145551
## [3,] 0.001047697 0.008614117 0.002820093 0.02078634
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09098690 0.07871914 0.03423740 0.11469308
## [2,] 0.02869361 0.04201118 0.07954118 0.05527231
## [3,] 0.00505509 0.04156273 0.01360682 0.10029317
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.16240127 0.14050471 0.06110987 0.20471411
## [2,] 0.05121483 0.07498517 0.14197197 0.09865479
## [3,] 0.00902276 0.07418475 0.02428664 0.17901191
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.31966252 0.2765624 0.1202856 0.4029490
## [2,] 0.10080871 0.1475971 0.2794505 0.1941871
## [3,] 0.01775995 0.1460216 0.0478046 0.3523581
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.25951609 0.2245255 0.09765315 0.3271317
## [2,] 0.08184094 0.1198258 0.22687022 0.1576497
## [3,] 0.01441831 0.1185467 0.03880987 0.2860598
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.131249128 0.11355281 0.04938765 0.16544544
## [2,] 0.041390698 0.06060136 0.11473862 0.07973063
## [3,] 0.007291996 0.05995448 0.01962792 0.14467348
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5017870 0.4341311 0.18881712 0.6325251
## [2,] 0.1582435 0.2316890 0.43866463 0.3048233
## [3,] 0.0278785 0.2292159 0.07504078 0.5531105
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.024911443 0.02155263 0.009373911 0.03140200
## [2,] 0.007856068 0.01150230 0.021777704 0.01513309
## [3,] 0.001384041 0.01137952 0.003725434 0.02745942
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.22550897 0.1951036 0.08485663 0.2842642
## [2,] 0.07111646 0.1041237 0.19714103 0.1369912
## [3,] 0.01252893 0.1030123 0.03372421 0.2485744
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.51773577 0.4479295 0.19481846 0.6526293
## [2,] 0.16327305 0.2390530 0.45260710 0.3145118
## [3,] 0.02876459 0.2365012 0.07742587 0.5706905
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.45898681 0.3971016 0.17271185 0.5785735
## [2,] 0.14474599 0.2119269 0.40124848 0.2788232
## [3,] 0.02550059 0.2096648 0.06864013 0.5059327
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.60881976 0.5267326 0.22909240 0.7674447
## [2,] 0.19199727 0.2811090 0.53223316 0.3698431
## [3,] 0.03382507 0.2781083 0.09104721 0.6710907
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.42986116 0.3719030 0.16175218 0.5418593
## [2,] 0.13556093 0.1984788 0.37578669 0.2611301
## [3,] 0.02388241 0.1963602 0.06428448 0.4738280
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10377237 0.08978074 0.03904844 0.13080974
## [2,] 0.03272563 0.04791458 0.09071830 0.06303917
## [3,] 0.00576543 0.04740312 0.01551885 0.11438636
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.21096943 0.18252442 0.07938555 0.2659365
## [2,] 0.06653128 0.09741044 0.18443049 0.1281588
## [3,] 0.01172113 0.09637064 0.03154986 0.2325477
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.154547521 0.13370988 0.05815459 0.19481411
## [2,] 0.048738075 0.07135888 0.13510619 0.09388383
## [3,] 0.008586418 0.07059717 0.02311213 0.17035487
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.32379164 0.2801348 0.12183935 0.4081539
## [2,] 0.10211086 0.1495036 0.28306021 0.1966955
## [3,] 0.01798936 0.1479077 0.04842209 0.3569095
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.102560830 0.08873255 0.03859255 0.12928255
## [2,] 0.032343563 0.04735518 0.08965917 0.06230319
## [3,] 0.005698119 0.04684969 0.01533767 0.11305090
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.37177773 0.3216509 0.13989601 0.4686426
## [2,] 0.11724375 0.1716601 0.32500988 0.2258459
## [3,] 0.02065539 0.1698277 0.05559827 0.4098037
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.45308746 0.39199768 0.17049199 0.57113715
## [2,] 0.14288557 0.20920305 0.39609124 0.27523953
## [3,] 0.02517283 0.20696994 0.06775791 0.49942992
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.026779662 0.023168960 0.010076902 0.033756970
## [2,] 0.008445229 0.012364913 0.023410910 0.016267988
## [3,] 0.001487836 0.012232925 0.004004820 0.029518726
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.177749879 0.153783864 0.066885388 0.224061727
## [2,] 0.056055166 0.082072050 0.155389800 0.107978695
## [3,] 0.009875505 0.081195984 0.026581975 0.195930399
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.32379164 0.28013481 0.12183935 0.40815394
## [2,] 0.10211086 0.14950358 0.28306021 0.19669548
## [3,] 0.01798936 0.14790773 0.04842209 0.35690952
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.102560830 0.088732554 0.038592549 0.129282545
## [2,] 0.032343563 0.047355181 0.089659171 0.062303191
## [3,] 0.005698119 0.046849695 0.015337672 0.113050903
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.37177773 0.32165094 0.13989601 0.46864258
## [2,] 0.11724375 0.17166009 0.32500988 0.22584586
## [3,] 0.02065539 0.16982773 0.05559827 0.40980371
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.461072
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.461072
einsum::einsum('ij->', arrC)
## [1] 5.098268
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 5.098268
einsum::einsum('ijk->', arrE)
## [1] 27.16223
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 27.16223
einsum::einsum('ij->i', arrC)
## [1] 2.372659 1.530348 1.195262
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 2.372659 1.530348 1.195262
einsum::einsum('ij->j', arrC)
## [1] 0.9288169 1.2084804 0.9485482 2.0124225
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 0.9288169 1.2084804 0.9485482 2.0124225
einsum::einsum('ijk->i', arrE)
## [1] 8.141839 9.505755 9.514633
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 8.141839 9.505755 9.514633
einsum::einsum('ijk->j', arrE)
## [1] 6.279716 7.396310 6.339340 7.146860
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 6.279716 7.396310 6.339340 7.146860
einsum::einsum('ijk->k', arrE)
## [1] 6.474797 4.725903 5.411172 5.335346 5.215008
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 6.474797 4.725903 5.411172 5.335346 5.215008
These are the same as what the modeSum function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 2.037736 2.344597 1.641553 2.117952
## [2,] 1.919244 3.143556 1.704825 2.738130
## [3,] 2.322736 1.908157 2.992962 2.290778
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 2.037736 2.344597 1.641553 2.117952
## [2,] 1.919244 3.143556 1.704825 2.738130
## [3,] 2.322736 1.908157 2.992962 2.290778
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9706317 1.054554 1.285455 1.8352898 1.1337857
## [2,] 1.1636005 1.169686 1.515641 1.3368504 2.2105326
## [3,] 2.3421283 1.322953 1.135782 0.8458133 0.6926632
## [4,] 1.9984362 1.178711 1.474294 1.3173922 1.1780268
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9706317 1.0545542 1.2854547 1.8352898 1.1337857
## [2,] 1.1636005 1.1696858 1.5156413 1.3368504 2.2105326
## [3,] 2.3421283 1.3229525 1.1357823 0.8458133 0.6926632
## [4,] 1.9984362 1.1787108 1.4742942 1.3173922 1.1780268
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9706317 1.054554 1.285455 1.8352898 1.1337857
## [2,] 1.1636005 1.169686 1.515641 1.3368504 2.2105326
## [3,] 2.3421283 1.322953 1.135782 0.8458133 0.6926632
## [4,] 1.9984362 1.178711 1.474294 1.3173922 1.1780268
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9706317 1.0545542 1.2854547 1.8352898 1.1337857
## [2,] 1.1636005 1.1696858 1.5156413 1.3368504 2.2105326
## [3,] 2.3421283 1.3229525 1.1357823 0.8458133 0.6926632
## [4,] 1.9984362 1.1787108 1.4742942 1.3173922 1.1780268
If we take the diagonal elements of a matrix
and add them together, we get trace.
einsum::einsum('ii->', arrB)
## [1] 0.8484552
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 0.8484552
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.6788699 0.78177586 0.73092674
## [2,] 0.9364708 0.09253485 0.32764708
## [3,] 0.6107648 0.92113249 0.07705046
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.67886992 0.78177586 0.73092674
## [2,] 0.93647084 0.09253485 0.32764708
## [3,] 0.61076485 0.92113249 0.07705046
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.9804450 0.07712947 0.2817982
## [2,] 0.4812978 0.62291908 0.3778716
## [3,] 0.8502037 0.82355085 0.2191459
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.9481351 0.6844578 0.7853498
## [2,] 0.2014194 0.5695084 0.3546038
## [3,] 0.9924401 0.8916166 0.5142287
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.3763421 0.7026034 0.3678609
## [2,] 0.4733593 0.1629307 0.4670467
## [3,] 0.5784273 0.2374249 0.2018241
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.98044500 0.07712947 0.28179820
## [2,] 0.48129785 0.62291908 0.37787165
## [3,] 0.85020371 0.82355085 0.21914595
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.9481351 0.6844578 0.7853498
## [2,] 0.2014194 0.5695084 0.3546038
## [3,] 0.9924401 0.8916166 0.5142287
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.3763421 0.7026034 0.3678609
## [2,] 0.4733593 0.1629307 0.4670467
## [3,] 0.5784273 0.2374249 0.2018241
Some examples of combining Multiplication and Summation are shown below.
Inner Product first calculate Hadamard Product and collapses it to 0D tensor (norm).
einsum::einsum('i,i->', arrA, arrA)
## [1] 0.9230452
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 0.9230452
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 2.925891
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 2.925891
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 16.87242
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 16.87242
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.5176189 0.5350693 0.5809277 1.3531778 0.6960942
## [2,] 0.6379093 0.4981485 1.1983531 0.8962890 1.6689933
## [3,] 1.8825379 0.8334041 0.7389438 0.2981029 0.1724560
## [4,] 1.5051642 0.4771307 1.0968909 0.7327805 0.5524274
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.5176189 0.5350693 0.5809277 1.3531778 0.6960942
## [2,] 0.6379093 0.4981485 1.1983531 0.8962890 1.6689933
## [3,] 1.8825379 0.8334041 0.7389438 0.2981029 0.1724560
## [4,] 1.5051642 0.4771307 1.0968909 0.7327805 0.5524274
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.5969906 0.8306242 0.8705497
## [2,] 0.8306242 0.6637076 0.4722373
## [3,] 0.8705497 0.4722373 0.6651931
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.5969906 0.8306242 0.8705497
## [2,] 0.8306242 0.6637076 0.4722373
## [3,] 0.8705497 0.4722373 0.6651931
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.45902582 0.04565094 0.001416893
## [2,] 0.34358961 0.09786086 0.095782812
## [3,] 0.06499516 0.35080316 0.010265804
## [4,] 0.72937998 0.16939268 0.557727590
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.459025816 0.045650944 0.001416893
## [2,] 0.343589607 0.097860865 0.095782812
## [3,] 0.064995156 0.350803160 0.010265804
## [4,] 0.729379985 0.169392676 0.557727590
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.4472259 0.001636021 0.27696362 0.58598524 0.00135195
## [2,] 0.0279625 0.227182482 0.25803618 0.26554584 0.45894780
## [3,] 0.9418136 0.008681768 0.08435809 0.01803519 0.02345991
## [4,] 0.5425962 0.114519954 0.03311684 0.14672073 0.22839897
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.001562331 0.16728419 0.084534584 0.71760821 0.11078745
## [2,] 0.067550667 0.04946744 0.938815802 0.62996845 0.80749597
## [3,] 0.471767794 0.18469107 0.001378279 0.05745684 0.09696208
## [4,] 0.845323141 0.23939050 0.968926600 0.03752803 0.02291532
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.06883059 0.3661491 0.219429521 0.0495843784 0.58395479
## [2,] 0.54239609 0.2214986 0.001501067 0.0007747007 0.40254951
## [3,] 0.46895646 0.6400312 0.653207425 0.2226108531 0.05203397
## [4,] 0.11724483 0.1232202 0.094847445 0.5485317104 0.30111309
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.447225935 0.001636021 0.276963616 0.585985245 0.001351950
## [2,] 0.027962504 0.227182482 0.258036185 0.265545841 0.458947801
## [3,] 0.941813601 0.008681768 0.084358095 0.018035185 0.023459909
## [4,] 0.542596198 0.114519954 0.033116845 0.146720730 0.228398970
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.001562331 0.167284192 0.084534584 0.717608207 0.110787445
## [2,] 0.067550667 0.049467443 0.938815802 0.629968448 0.807495970
## [3,] 0.471767794 0.184691073 0.001378279 0.057456840 0.096962084
## [4,] 0.845323141 0.239390504 0.968926600 0.037528028 0.022915321
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0688305939 0.3661490881 0.2194295212 0.0495843784 0.5839547934
## [2,] 0.5423960939 0.2214985828 0.0015010671 0.0007747007 0.4025495058
## [3,] 0.4689564578 0.6400312150 0.6532074253 0.2226108531 0.0520339714
## [4,] 0.1172448326 0.1232202016 0.0948474447 0.5485317104 0.3011130937
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 2.5430516 1.905700 2.026046
## [2,] 0.9486684 1.550449 2.226786
## [3,] 1.5066709 2.281139 1.623362
## [4,] 1.7981447 2.074246 1.462955
## [5,] 1.3453031 1.694221 2.175485
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 2.5430516 1.9056996 2.0260456
## [2,] 0.9486684 1.5504492 2.2267858
## [3,] 1.5066709 2.2811393 1.6233623
## [4,] 1.7981447 2.0742462 1.4629548
## [5,] 1.3453031 1.6942207 2.1754845
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.170824954 0.0006249037 0.105790593 0.2238262462 0.0005163985
## [2,] 0.007994721 0.0649534282 0.073774768 0.0759218430 0.1312171290
## [3,] 0.050936977 0.0004695441 0.004562417 0.0009754136 0.0012688040
## [4,] 0.329319773 0.0695059889 0.020099720 0.0890497164 0.1386229708
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.329129e-05 0.0014231439 7.191647e-04 0.006104939 0.0009425068
## [2,] 1.231921e-03 0.0009021374 1.712118e-02 0.011488730 0.0147262976
## [3,] 3.084153e-02 0.0120740664 9.010412e-05 0.003756206 0.0063388372
## [4,] 2.668465e-02 0.0075569354 3.058649e-02 0.001184662 0.0007233771
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 4.316418e-05 0.0002296148 1.376059e-04 3.109473e-05 0.0003662024
## [2,] 2.299371e-02 0.0093899525 6.363449e-05 3.284176e-05 0.0170652141
## [3,] 2.130739e-03 0.0029080306 2.967898e-03 1.011449e-03 0.0002364203
## [4,] 2.894148e-02 0.0304164766 2.341276e-02 1.354031e-01 0.0743287161
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.1708249536 0.0006249037 0.1057905930 0.2238262462 0.0005163985
## [2,] 0.0079947207 0.0649534282 0.0737747676 0.0759218430 0.1312171290
## [3,] 0.0509369772 0.0004695441 0.0045624170 0.0009754136 0.0012688040
## [4,] 0.3293197728 0.0695059889 0.0200997202 0.0890497164 0.1386229708
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.329129e-05 1.423144e-03 7.191647e-04 6.104939e-03 9.425068e-04
## [2,] 1.231921e-03 9.021374e-04 1.712118e-02 1.148873e-02 1.472630e-02
## [3,] 3.084153e-02 1.207407e-02 9.010412e-05 3.756206e-03 6.338837e-03
## [4,] 2.668465e-02 7.556935e-03 3.058649e-02 1.184662e-03 7.233771e-04
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 4.316418e-05 2.296148e-04 1.376059e-04 3.109473e-05 3.662024e-04
## [2,] 2.299371e-02 9.389953e-03 6.363449e-05 3.284176e-05 1.706521e-02
## [3,] 2.130739e-03 2.908031e-03 2.967898e-03 1.011449e-03 2.364203e-04
## [4,] 2.894148e-02 3.041648e-02 2.341276e-02 1.354031e-01 7.432872e-02
einsumBy 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
## R version 4.6.0 RC (2026-04-17 r89917)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.4 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.23-bioc/R/lib/libRblas.so
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0 LAPACK version 3.12.0
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_GB LC_COLLATE=C
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## time zone: America/New_York
## tzcode source: system (glibc)
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] einsum_0.1.2 DelayedRandomArray_1.20.0
## [3] HDF5Array_1.40.0 h5mread_1.4.0
## [5] rhdf5_2.56.0 DelayedArray_0.38.0
## [7] SparseArray_1.12.0 S4Arrays_1.12.0
## [9] abind_1.4-8 IRanges_2.46.0
## [11] S4Vectors_0.50.0 MatrixGenerics_1.24.0
## [13] matrixStats_1.5.0 BiocGenerics_0.58.0
## [15] generics_0.1.4 Matrix_1.7-5
## [17] DelayedTensor_1.18.0 BiocStyle_2.40.0
##
## loaded via a namespace (and not attached):
## [1] dqrng_0.4.1 sass_0.4.10 lattice_0.22-9
## [4] digest_0.6.39 evaluate_1.0.5 grid_4.6.0
## [7] bookdown_0.46 fastmap_1.2.0 jsonlite_2.0.0
## [10] BiocManager_1.30.27 codetools_0.2-20 jquerylib_0.1.4
## [13] cli_3.6.6 rlang_1.2.0 XVector_0.52.0
## [16] cachem_1.1.0 yaml_2.3.12 otel_0.2.0
## [19] tools_4.6.0 beachmat_2.28.0 parallel_4.6.0
## [22] BiocParallel_1.46.0 Rhdf5lib_2.0.0 rsvd_1.0.5
## [25] R6_2.6.1 lifecycle_1.0.5 BiocSingular_1.28.0
## [28] irlba_2.3.7 ScaledMatrix_1.20.0 rTensor_1.4.9
## [31] bslib_0.10.0 Rcpp_1.1.1-1.1 xfun_0.57
## [34] knitr_1.51 rhdf5filters_1.24.0 htmltools_0.5.9
## [37] rmarkdown_2.31 compiler_4.6.0