DelayedTensor 1.2.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2022-04-26 14:34:11
Compiled: Tue Apr 26 18:04:40 2022
einsum
einsum
is an easy and intuitive way to write tensor operations.
It was originally introduced by
Numpy
1 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)
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.
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.3231822 0.4873469 0.6061458
DelayedTensor::einsum('i->i', darrA)
## <3> array of class DelayedArray and type "double":
## [1] [2] [3]
## 0.3231822 0.4873469 0.6061458
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.7034414 0.364503481 0.96963295 0.2572033
## [2,] 0.8164451 0.004957495 0.52043812 0.3864276
## [3,] 0.7424503 0.974110611 0.08629203 0.5076156
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.703441376 0.364503481 0.969632949 0.257203308
## [2,] 0.816445116 0.004957495 0.520438124 0.386427560
## [3,] 0.742450315 0.974110611 0.086292035 0.507615647
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5888523 0.6181445 0.1294098 0.4680650
## [2,] 0.8215881 0.5295972 0.2837513 0.8406331
## [3,] 0.8865250 0.5637084 0.6540118 0.2329880
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0695212 0.9641356 0.5006269 0.1661798
## [2,] 0.6169973 0.3226343 0.6642853 0.1512281
## [3,] 0.2455644 0.6111574 0.9712918 0.0302383
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4384077 0.2287355 0.74343210 0.01730393
## [2,] 0.7825925 0.2678854 0.08687853 0.34996199
## [3,] 0.2725689 0.3420578 0.03172751 0.47571994
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2005707 0.9098105 0.2893934 0.8799482
## [2,] 0.1304307 0.8339718 0.7629959 0.2216741
## [3,] 0.4118523 0.6832606 0.7806175 0.8789509
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5328035 0.2933913 0.5523203 0.1107492
## [2,] 0.6460624 0.2199957 0.2965880 0.3119017
## [3,] 0.7606205 0.8951532 0.3026993 0.4139564
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.5888523 0.6181445 0.1294098 0.4680650
## [2,] 0.8215881 0.5295972 0.2837513 0.8406331
## [3,] 0.8865250 0.5637084 0.6540118 0.2329880
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.0695212 0.9641356 0.5006269 0.1661798
## [2,] 0.6169973 0.3226343 0.6642853 0.1512281
## [3,] 0.2455644 0.6111574 0.9712918 0.0302383
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.43840773 0.22873550 0.74343210 0.01730393
## [2,] 0.78259250 0.26788544 0.08687853 0.34996199
## [3,] 0.27256892 0.34205784 0.03172751 0.47571994
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.2005707 0.9098105 0.2893934 0.8799482
## [2,] 0.1304307 0.8339718 0.7629959 0.2216741
## [3,] 0.4118523 0.6832606 0.7806175 0.8789509
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.5328035 0.2933913 0.5523203 0.1107492
## [2,] 0.6460624 0.2199957 0.2965880 0.3119017
## [3,] 0.7606205 0.8951532 0.3026993 0.4139564
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.4600796 0.9216865 0.1629452
DelayedTensor::einsum('ii->i', darrB)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.4600796 0.9216865 0.1629452
einsum::einsum('iii->i', arrD)
## [1] 0.69289183 0.61627465 0.08104312
DelayedTensor::einsum('iii->i', darrD)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.69289183 0.61627465 0.08104312
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.1044467 0.2375070 0.3674127
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.1044467 0.2375070 0.3674127
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.4948298 1.328628e-01 0.940188056 0.06615354
## [2,] 0.6665826 2.457676e-05 0.270855841 0.14932626
## [3,] 0.5512325 9.488915e-01 0.007446315 0.25767365
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 4.948298e-01 1.328628e-01 9.401881e-01 6.615354e-02
## [2,] 6.665826e-01 2.457676e-05 2.708558e-01 1.493263e-01
## [3,] 5.512325e-01 9.488915e-01 7.446315e-03 2.576736e-01
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3467470 0.3821027 0.01674689 0.21908486
## [2,] 0.6750070 0.2804732 0.08051480 0.70666400
## [3,] 0.7859266 0.3177672 0.42773142 0.05428342
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.004833198 0.9295574 0.2506273 0.0276157237
## [2,] 0.380685633 0.1040929 0.4412750 0.0228699478
## [3,] 0.060301855 0.3735134 0.9434077 0.0009143547
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.19220134 0.05231993 0.552691285 0.0002994261
## [2,] 0.61245102 0.07176261 0.007547880 0.1224733928
## [3,] 0.07429381 0.11700356 0.001006635 0.2263094606
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04022860 0.8277551 0.08374852 0.77430876
## [2,] 0.01701218 0.6955089 0.58216275 0.04913942
## [3,] 0.16962231 0.4668451 0.60936363 0.77255476
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2838796 0.08607844 0.30505770 0.01226539
## [2,] 0.4173966 0.04839811 0.08796443 0.09728269
## [3,] 0.5785436 0.80129933 0.09162685 0.17135990
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> array of class HDF5Array and type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.34674705 0.38210265 0.01674689 0.21908486
## [2,] 0.67500696 0.28047322 0.08051480 0.70666400
## [3,] 0.78592657 0.31776718 0.42773142 0.05428342
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.0048331976 0.9295573910 0.2506273340 0.0276157237
## [2,] 0.3806856331 0.1040929132 0.4412749726 0.0228699478
## [3,] 0.0603018554 0.3735134251 0.9434077337 0.0009143547
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.1922013417 0.0523199306 0.5526912855 0.0002994261
## [2,] 0.6124510219 0.0717626095 0.0075478796 0.1224733928
## [3,] 0.0742938142 0.1170035625 0.0010066348 0.2263094606
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.04022860 0.82775506 0.08374852 0.77430876
## [2,] 0.01701218 0.69550892 0.58216275 0.04913942
## [3,] 0.16962231 0.46684508 0.60936363 0.77255476
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.28387958 0.08607844 0.30505770 0.01226539
## [2,] 0.41739661 0.04839811 0.08796443 0.09728269
## [3,] 0.57854361 0.80129933 0.09162685 0.17135990
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.1044467 0.1575018 0.1958955
## [2,] 0.1575018 0.2375070 0.2954033
## [3,] 0.1958955 0.2954033 0.3674127
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.1044467 0.1575018 0.1958955
## [2,] 0.1575018 0.2375070 0.2954033
## [3,] 0.1958955 0.2954033 0.3674127
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4142231 0.214638718 0.57097060 0.1514548
## [2,] 0.4807656 0.002919233 0.30646119 0.2275488
## [3,] 0.4371936 0.573607286 0.05081326 0.2989106
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5779390 0.299471712 0.79663887 0.2113152
## [2,] 0.6707816 0.004073019 0.42758575 0.3174843
## [3,] 0.6099883 0.800317659 0.07089651 0.4170510
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6236184 0.323141447 0.85960385 0.2280172
## [2,] 0.7237990 0.004394943 0.46138141 0.3425777
## [3,] 0.6582008 0.863573405 0.07650005 0.4500140
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4348284 0.225315831 0.59937330 0.1589888
## [2,] 0.5046811 0.003064449 0.32170598 0.2388681
## [3,] 0.4589416 0.602141141 0.05334095 0.3137798
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3725406 0.193040032 0.51351492 0.1362142
## [2,] 0.4323871 0.002625476 0.27562259 0.2046510
## [3,] 0.3931996 0.515886276 0.04570002 0.2688318
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3965358 0.205473681 0.54659026 0.1449877
## [2,] 0.4602370 0.002794582 0.29337535 0.2178325
## [3,] 0.4185255 0.549114353 0.04864355 0.2861472
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09103218 0.0471703101 0.12547997 0.03328462
## [2,] 0.10565597 0.0006415483 0.06734978 0.05000750
## [3,] 0.09608032 0.1260594260 0.01116703 0.06569042
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1996024 0.103428338 0.27513461 0.07298177
## [2,] 0.2316674 0.001406696 0.14767500 0.10964932
## [3,] 0.2106712 0.276405154 0.02448548 0.14403660
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4600590 0.23838957 0.63415138 0.1682140
## [2,] 0.5339647 0.00324226 0.34037267 0.2527282
## [3,] 0.4855713 0.63707982 0.05643601 0.3319866
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3292563 0.17061133 0.45385126 0.1203879
## [2,] 0.3821494 0.00232043 0.24359888 0.1808732
## [3,] 0.3475150 0.45594709 0.04039028 0.2375971
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5913361 0.306413688 0.81510554 0.2162136
## [2,] 0.6863308 0.004167435 0.43749751 0.3248438
## [3,] 0.6241283 0.818869615 0.07253994 0.4267185
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1638934 0.084924943 0.22591286 0.05992529
## [2,] 0.1902219 0.001155037 0.12125585 0.09003299
## [3,] 0.1729820 0.226956099 0.02010501 0.11826836
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04890409 0.025340720 0.067410049 0.01788108
## [2,] 0.05676025 0.000344651 0.036181484 0.02686491
## [3,] 0.05161604 0.067721341 0.005999126 0.03529005
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4340214 0.224897653 0.59826088 0.1586937
## [2,] 0.5037444 0.003058761 0.32110890 0.2384248
## [3,] 0.4580898 0.601023589 0.05324195 0.3131975
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1727401 0.089509064 0.23810730 0.06315997
## [2,] 0.2004898 0.001217384 0.12780106 0.09489284
## [3,] 0.1823193 0.239206850 0.02119025 0.12465231
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6782128 0.351430770 0.93485761 0.2479789
## [2,] 0.7871638 0.004779697 0.50177291 0.3725686
## [3,] 0.7158228 0.939174686 0.08319722 0.4894103
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2269543 0.117601338 0.31283688 0.08298262
## [2,] 0.2634132 0.001599458 0.16791121 0.12467480
## [3,] 0.2395400 0.314281528 0.02784077 0.16377424
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4299134 0.22276902 0.59259840 0.1571917
## [2,] 0.4989765 0.00302981 0.31806964 0.2361681
## [3,] 0.4537540 0.59533495 0.05273802 0.3102331
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3521617 0.182480263 0.48542438 0.1287629
## [2,] 0.4087344 0.002481856 0.26054535 0.1934560
## [3,] 0.3716906 0.487666015 0.04320012 0.2541261
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4672858 0.242134308 0.64411292 0.1708564
## [2,] 0.5423525 0.003293191 0.34571940 0.2566982
## [3,] 0.4931988 0.647087369 0.05732253 0.3372016
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6832468 0.354039237 0.94179652 0.2498195
## [2,] 0.7930064 0.004815174 0.50549727 0.3753339
## [3,] 0.7211359 0.946145635 0.08381474 0.4930429
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1168977 0.0605731131 0.16113340 0.04274199
## [2,] 0.1356767 0.0008238355 0.08648630 0.06421645
## [3,] 0.1233802 0.1618774998 0.01433999 0.08435546
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1063801 0.0551231805 0.14663578 0.03889638
## [2,] 0.1234695 0.0007497127 0.07870489 0.05843872
## [3,] 0.1122794 0.1473129278 0.01304978 0.07676577
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02127087 0.0110219651 0.029320051 0.00777739
## [2,] 0.02468791 0.0001499062 0.015737163 0.01168491
## [3,] 0.02245043 0.0294554476 0.002609324 0.01534943
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3083941 0.159801145 0.4250946 0.1127599
## [2,] 0.3579359 0.002173404 0.2281641 0.1694128
## [3,] 0.3254960 0.427057626 0.0378311 0.2225426
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5505079 0.285257691 0.7588275 0.2012854
## [2,] 0.6389438 0.003879699 0.4072910 0.3024153
## [3,] 0.5810360 0.762331659 0.0675315 0.3972562
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1917363 0.099352319 0.26429180 0.07010563
## [2,] 0.2225376 0.001351259 0.14185526 0.10532814
## [3,] 0.2023689 0.265512274 0.02352053 0.13836025
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1609020 0.083374887 0.22178948 0.05883153
## [2,] 0.1867500 0.001133955 0.11904268 0.08838970
## [3,] 0.1698247 0.222813681 0.01973805 0.11610972
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1884417 0.097645176 0.25975055 0.06890102
## [2,] 0.2187138 0.001328041 0.13941780 0.10351832
## [3,] 0.1988916 0.260950051 0.02311638 0.13598284
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2406176 0.12468127 0.33167055 0.08797841
## [2,] 0.2792714 0.00169575 0.17801994 0.13218057
## [3,] 0.2539609 0.33320217 0.02951687 0.17363391
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5229609 0.270983588 0.72085626 0.1912132
## [2,] 0.6069715 0.003685561 0.38691041 0.2872827
## [3,] 0.5519614 0.724185096 0.06415227 0.3773778
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06111396 0.0316675279 0.084240289 0.02234545
## [2,] 0.07093155 0.0004306999 0.045214901 0.03357226
## [3,] 0.06450299 0.0846293013 0.007496925 0.04410090
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02231844 0.011564787 0.030764037 0.00816042
## [2,] 0.02590377 0.000157289 0.016512205 0.01226038
## [3,] 0.02355610 0.030906102 0.002737831 0.01610538
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01217230 6.307344e-03 0.016778464 0.004450629
## [2,] 0.01412771 8.578417e-05 0.009005627 0.006686717
## [3,] 0.01284731 1.685595e-02 0.001493192 0.008783748
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2461777 0.127562363 0.33933467 0.09001138
## [2,] 0.2857248 0.001734935 0.18213356 0.13523496
## [3,] 0.2598294 0.340901686 0.03019893 0.17764618
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3346411 0.173401574 0.46127373 0.1223567
## [2,] 0.3883992 0.002358379 0.24758279 0.1838313
## [3,] 0.3531984 0.463403841 0.04105084 0.2414829
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1410897 0.0731087123 0.19447994 0.05158744
## [2,] 0.1637550 0.0009943282 0.10438463 0.07750604
## [3,] 0.1489138 0.1953780307 0.01730765 0.10181282
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09175037 0.0475424564 0.12646994 0.03354722
## [2,] 0.10648954 0.0006466097 0.06788113 0.05040203
## [3,] 0.09683834 0.1270539615 0.01125513 0.06620868
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2897139 0.150121592 0.39934555 0.1059298
## [2,] 0.3362548 0.002041756 0.21434363 0.1591511
## [3,] 0.3057799 0.401189681 0.03553957 0.2090627
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6399983 0.331629077 0.8821822 0.2340063
## [2,] 0.7428103 0.004510381 0.4735000 0.3515758
## [3,] 0.6754891 0.886256016 0.0785094 0.4618340
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5866503 0.303985615 0.80864651 0.2145003
## [2,] 0.6808922 0.004134411 0.43403071 0.3222697
## [3,] 0.6191826 0.812380753 0.07196512 0.4233371
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4806338 0.249050876 0.66251201 0.1757369
## [2,] 0.5578448 0.003387261 0.35559488 0.2640307
## [3,] 0.5072871 0.665571424 0.05895995 0.3468338
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2035713 0.105484887 0.28060534 0.07443293
## [2,] 0.2362738 0.001434666 0.15061134 0.11182957
## [3,] 0.2148602 0.281901143 0.02497234 0.14690060
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5367229 0.278114664 0.73982597 0.1962451
## [2,] 0.6229443 0.003782549 0.39709216 0.2948426
## [3,] 0.5664866 0.743242409 0.06584047 0.3873087
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5491186 0.284537783 0.75691241 0.2007774
## [2,] 0.6373313 0.003869907 0.40626309 0.3016521
## [3,] 0.5795697 0.760407755 0.06736107 0.3962536
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6189919 0.320744167 0.85322673 0.2263256
## [2,] 0.7184294 0.004362339 0.45795857 0.3400362
## [3,] 0.6533178 0.857166837 0.07593252 0.4466755
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1559348 0.080800992 0.21494254 0.05701532
## [2,] 0.1809848 0.001098948 0.11536767 0.08566099
## [3,] 0.1645820 0.215935122 0.01912871 0.11252526
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6182905 0.320380678 0.85225979 0.2260691
## [2,] 0.7176152 0.004357395 0.45743958 0.3396509
## [3,] 0.6525774 0.856195439 0.07584647 0.4461693
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3747960 0.194208734 0.5166238 0.1370388
## [2,] 0.4350048 0.002641371 0.2772913 0.2058900
## [3,] 0.3955801 0.519009553 0.0459767 0.2704594
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4544670 0.235491991 0.62644338 0.1661694
## [2,] 0.5274745 0.003202851 0.33623550 0.2496563
## [3,] 0.4796692 0.629336230 0.05575004 0.3279514
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5350520 0.277248836 0.73752274 0.1956341
## [2,] 0.6210049 0.003770773 0.39585593 0.2939247
## [3,] 0.5647230 0.740928543 0.06563549 0.3861029
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2063836 0.106942142 0.28448185 0.07546121
## [2,] 0.2395379 0.001454486 0.15269201 0.11337448
## [3,] 0.2178284 0.285795556 0.02531733 0.14893000
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1547541 0.080189202 0.21331509 0.05658362
## [2,] 0.1796144 0.001090628 0.11449415 0.08501240
## [3,] 0.1633359 0.214300154 0.01898388 0.11167326
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6296878 0.326286473 0.86797008 0.2302364
## [2,] 0.7308435 0.004437718 0.46587187 0.3459119
## [3,] 0.6646068 0.871978271 0.07724459 0.4543938
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3885249 0.201322668 0.53554795 0.1420586
## [2,] 0.4509392 0.002738125 0.28744853 0.2134318
## [3,] 0.4100704 0.538021053 0.04766084 0.2803664
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2086323 0.108107351 0.28758148 0.07628341
## [2,] 0.2421478 0.001470333 0.15435569 0.11460977
## [3,] 0.2202018 0.288909497 0.02559318 0.15055270
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2129312 0.11033494 0.29350718 0.07785525
## [2,] 0.2471373 0.00150063 0.15753624 0.11697134
## [3,] 0.2247392 0.29486257 0.02612054 0.15365488
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07790559 0.0403684813 0.107386106 0.02848507
## [2,] 0.09042067 0.0005490388 0.057638124 0.04279656
## [3,] 0.08222580 0.1078820040 0.009556777 0.05621804
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2194046 0.113689268 0.30243020 0.08022216
## [2,] 0.2546506 0.001546251 0.16232555 0.12052743
## [3,] 0.2315715 0.303826790 0.02691464 0.15832620
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2911941 0.150888547 0.40138576 0.1064710
## [2,] 0.3379727 0.002052187 0.21543869 0.1599642
## [3,] 0.3073421 0.403239317 0.03572114 0.2101307
DelayedTensor::einsum('ij,klm->ijklm', darrC, darrE)
## <3 x 4 x 3 x 4 x 5> array of class HDF5Array and type "double":
## ,,1,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.414223081 0.214638718 0.570970604 0.151454763
## [2,] 0.480765594 0.002919233 0.306461193 0.227548762
## [3,] 0.437193585 0.573607286 0.050813264 0.298910648
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.577939044 0.299471712 0.796638866 0.211315170
## [2,] 0.670781569 0.004073019 0.427585755 0.317484274
## [3,] 0.609988323 0.800317659 0.070896507 0.417050961
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.623618363 0.323141447 0.859603846 0.228017161
## [2,] 0.723799003 0.004394943 0.461381406 0.342577691
## [3,] 0.658200762 0.863573405 0.076500046 0.450013959
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.0779055936 0.0403684813 0.1073861064 0.0284850693
## [2,] 0.0904206712 0.0005490388 0.0576381236 0.0427965562
## [3,] 0.0822258036 0.1078820040 0.0095567768 0.0562180441
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.219404586 0.113689268 0.302430200 0.080222158
## [2,] 0.254650649 0.001546251 0.162325554 0.120527427
## [3,] 0.231571542 0.303826790 0.026914635 0.158326201
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.291194056 0.150888547 0.401385760 0.106470954
## [2,] 0.337972677 0.002052187 0.215438690 0.159964160
## [3,] 0.307342056 0.403239317 0.035721140 0.210130744
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.416675
DelayedTensor::einsum('i->', darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 1.416675
einsum::einsum('ij->', arrC)
## [1] 6.333518
DelayedTensor::einsum('ij->', darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 6.333518
einsum::einsum('ijk->', arrE)
## [1] 28.28812
DelayedTensor::einsum('ijk->', darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 28.28812
einsum::einsum('ij->i', arrC)
## [1] 2.294781 1.728268 2.310469
DelayedTensor::einsum('ij->i', darrC)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 2.294781 1.728268 2.310469
einsum::einsum('ij->j', arrC)
## [1] 2.262337 1.343572 1.576363 1.151247
DelayedTensor::einsum('ij->j', darrC)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 2.262337 1.343572 1.576363 1.151247
einsum::einsum('ijk->i', arrE)
## [1] 8.701801 9.141654 10.444670
DelayedTensor::einsum('ijk->i', darrE)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 8.701801 9.141654 10.444670
einsum::einsum('ijk->j', arrE)
## [1] 7.404958 8.283639 7.050029 5.549499
DelayedTensor::einsum('ijk->j', darrE)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 7.404958 8.283639 7.050029 5.549499
einsum::einsum('ijk->k', arrE)
## [1] 6.617275 5.313860 4.037272 6.983477 5.336242
DelayedTensor::einsum('ijk->k', darrE)
## <5> array of class HDF5Array and type "double":
## [1] [2] [3] [4] [5]
## 6.617275 5.313860 4.037272 6.983477 5.336242
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 1.830155 3.014217 2.215182 1.642246
## [2,] 2.997671 2.174084 2.094499 1.875399
## [3,] 2.577131 3.095338 2.740348 2.031854
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 1.830155 3.014217 2.215182 1.642246
## [2,] 2.997671 2.174084 2.094499 1.875399
## [3,] 2.577131 3.095338 2.740348 2.031854
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.296965 0.9320828 1.4935692 0.7428537 1.9394864
## [2,] 1.711450 1.8979273 0.8386788 2.4270429 1.4085402
## [3,] 1.067173 2.1362040 0.8620381 1.8330067 1.1516075
## [4,] 1.541686 0.3476462 0.8429859 1.9805732 0.8366074
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.2969654 0.9320828 1.4935692 0.7428537 1.9394864
## [2,] 1.7114502 1.8979273 0.8386788 2.4270429 1.4085402
## [3,] 1.0671729 2.1362040 0.8620381 1.8330067 1.1516075
## [4,] 1.5416861 0.3476462 0.8429859 1.9805732 0.8366074
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.296965 0.9320828 1.4935692 0.7428537 1.9394864
## [2,] 1.711450 1.8979273 0.8386788 2.4270429 1.4085402
## [3,] 1.067173 2.1362040 0.8620381 1.8330067 1.1516075
## [4,] 1.541686 0.3476462 0.8429859 1.9805732 0.8366074
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.2969654 0.9320828 1.4935692 0.7428537 1.9394864
## [2,] 1.7114502 1.8979273 0.8386788 2.4270429 1.4085402
## [3,] 1.0671729 2.1362040 0.8620381 1.8330067 1.1516075
## [4,] 1.5416861 0.3476462 0.8429859 1.9805732 0.8366074
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.544711
DelayedTensor::einsum('ii->', darrB)
## <1> array of class HDF5Array and type "double":
## [1]
## 1.544711
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.46007962 0.4832017 0.29061919
## [2,] 0.09792336 0.9216865 0.05712551
## [3,] 0.83577864 0.4535419 0.16294515
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3]
## [1,] 0.46007962 0.48320170 0.29061919
## [2,] 0.09792336 0.92168646 0.05712551
## [3,] 0.83577864 0.45354186 0.16294515
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.6928918 0.4250179 0.3019427
## [2,] 0.7904963 0.1856916 0.6566489
## [3,] 0.5544083 0.2618467 0.6354053
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.1058670 0.3221292 0.29122412
## [2,] 0.2650600 0.6162747 0.74523009
## [3,] 0.9103055 0.6328853 0.02117863
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.3987932 0.5639051 0.02896918
## [2,] 0.7947616 0.1856343 0.49924181
## [3,] 0.4619243 0.1158381 0.08104312
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.6928918 0.4250179 0.3019427
## [2,] 0.7904963 0.1856916 0.6566489
## [3,] 0.5544083 0.2618467 0.6354053
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.10586699 0.32212922 0.29122412
## [2,] 0.26506002 0.61627465 0.74523009
## [3,] 0.91030547 0.63288525 0.02117863
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.39879323 0.56390508 0.02896918
## [2,] 0.79476163 0.18563428 0.49924181
## [3,] 0.46192427 0.11583809 0.08104312
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.7093664
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 0.7093664
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 4.486067
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 4.486067
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 17.93251
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 17.93251
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.8076806 0.44582069 0.8789462 0.2268631 1.2798198
## [2,] 0.9803431 1.40716373 0.2410861 1.9901091 0.9357759
## [3,] 0.5249931 1.63531004 0.5612458 1.2752749 0.4846490
## [4,] 0.9800323 0.05140003 0.3490823 1.5960029 0.2809080
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.80768057 0.44582069 0.87894618 0.22686308 1.27981981
## [2,] 0.98034306 1.40716373 0.24108610 1.99010906 0.93577588
## [3,] 0.52499311 1.63531004 0.56124580 1.27527490 0.48464897
## [4,] 0.98003227 0.05140003 0.34908228 1.59600294 0.28090798
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.634034 1.1801527 1.0915690
## [2,] 1.180153 1.0867893 0.8520654
## [3,] 1.091569 0.8520654 1.7652439
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 1.6340342 1.1801527 1.0915690
## [2,] 1.1801527 1.0867893 0.8520654
## [3,] 1.0915690 0.8520654 1.7652439
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.49482977 6.665826e-01 0.551232470
## [2,] 0.13286279 2.457676e-05 0.948891482
## [3,] 0.94018806 2.708558e-01 0.007446315
## [4,] 0.06615354 1.493263e-01 0.257673645
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 4.948298e-01 6.665826e-01 5.512325e-01
## [2,] 1.328628e-01 2.457676e-05 9.488915e-01
## [3,] 9.401881e-01 2.708558e-01 7.446315e-03
## [4,] 6.615354e-02 1.493263e-01 2.576736e-01
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.34674705 0.004833198 0.1922013417 0.04022860 0.28387958
## [2,] 0.38210265 0.929557391 0.0523199306 0.82775506 0.08607844
## [3,] 0.01674689 0.250627334 0.5526912855 0.08374852 0.30505770
## [4,] 0.21908486 0.027615724 0.0002994261 0.77430876 0.01226539
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.6750070 0.38068563 0.61245102 0.01701218 0.41739661
## [2,] 0.2804732 0.10409291 0.07176261 0.69550892 0.04839811
## [3,] 0.0805148 0.44127497 0.00754788 0.58216275 0.08796443
## [4,] 0.7066640 0.02286995 0.12247339 0.04913942 0.09728269
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.78592657 0.0603018554 0.074293814 0.1696223 0.57854361
## [2,] 0.31776718 0.3735134251 0.117003563 0.4668451 0.80129933
## [3,] 0.42773142 0.9434077337 0.001006635 0.6093636 0.09162685
## [4,] 0.05428342 0.0009143547 0.226309461 0.7725548 0.17135990
DelayedTensor::einsum('ijk,ijk->jki', darrE, darrE)
## <4 x 5 x 3> array of class HDF5Array and type "double":
## ,,1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.3467470458 0.0048331976 0.1922013417 0.0402285990 0.2838795806
## [2,] 0.3821026538 0.9295573910 0.0523199306 0.8277550599 0.0860784415
## [3,] 0.0167468875 0.2506273340 0.5526912855 0.0837485171 0.3050577003
## [4,] 0.2190848557 0.0276157237 0.0002994261 0.7743087592 0.0122653928
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.67500696 0.38068563 0.61245102 0.01701218 0.41739661
## [2,] 0.28047322 0.10409291 0.07176261 0.69550892 0.04839811
## [3,] 0.08051480 0.44127497 0.00754788 0.58216275 0.08796443
## [4,] 0.70666400 0.02286995 0.12247339 0.04913942 0.09728269
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.7859265677 0.0603018554 0.0742938142 0.1696223051 0.5785436123
## [2,] 0.3177671822 0.3735134251 0.1170035625 0.4668450804 0.8012993261
## [3,] 0.4277314180 0.9434077337 0.0010066348 0.6093636253 0.0916268452
## [4,] 0.0542834159 0.0009143547 0.2263094606 0.7725547594 0.1713598973
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 1.804472 2.475570 2.337233
## [2,] 1.700464 1.755145 1.858252
## [3,] 1.427879 1.487318 1.122074
## [4,] 2.279723 1.949073 2.754681
## [5,] 1.489264 1.474548 2.372429
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 1.804472 2.475570 2.337233
## [2,] 1.700464 1.755145 1.858252
## [3,] 1.427879 1.487318 1.122074
## [4,] 2.279723 1.949073 2.754681
## [5,] 1.489264 1.474548 2.372429
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.055451845 0.0007729258 3.073687e-02 0.006433364 0.0453980698
## [2,] 0.016407062 0.0399141590 2.246560e-03 0.035542880 0.0036961124
## [3,] 0.005088576 0.0761536245 1.679364e-01 0.025447157 0.0926924019
## [4,] 0.004683957 0.0005904144 6.401625e-06 0.016554447 0.0002622298
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.192807e-01 1.236684e-01 1.989590e-01 5.526524e-03 1.355942e-01
## [2,] 3.359342e-06 1.246763e-06 8.595300e-07 8.330394e-06 5.796839e-07
## [3,] 1.062801e-02 5.824863e-02 9.963258e-04 7.684592e-02 1.161137e-02
## [4,] 5.142655e-02 1.664330e-03 8.912840e-03 3.576057e-03 7.079620e-03
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.262599464 0.020148492 2.482359e-02 0.056675430 0.1933071725
## [2,] 0.182769054 0.214832429 6.729654e-02 0.268513675 0.4608805704
## [3,] 0.001930588 0.004258120 4.543499e-06 0.002750395 0.0004135626
## [4,] 0.008478407 0.000142811 3.534677e-02 0.120663619 0.0267643234
DelayedTensor::einsum('i,ij,ijk,ijk,ji->jki',
darrA, darrC, darrE, darrE, t(darrC))
## <4 x 5 x 3> array of class HDF5Array and type "double":
## ,,1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 5.545185e-02 7.729258e-04 3.073687e-02 6.433364e-03 4.539807e-02
## [2,] 1.640706e-02 3.991416e-02 2.246560e-03 3.554288e-02 3.696112e-03
## [3,] 5.088576e-03 7.615362e-02 1.679364e-01 2.544716e-02 9.269240e-02
## [4,] 4.683957e-03 5.904144e-04 6.401625e-06 1.655445e-02 2.622298e-04
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.192807e-01 1.236684e-01 1.989590e-01 5.526524e-03 1.355942e-01
## [2,] 3.359342e-06 1.246763e-06 8.595300e-07 8.330394e-06 5.796839e-07
## [3,] 1.062801e-02 5.824863e-02 9.963258e-04 7.684592e-02 1.161137e-02
## [4,] 5.142655e-02 1.664330e-03 8.912840e-03 3.576057e-03 7.079620e-03
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.625995e-01 2.014849e-02 2.482359e-02 5.667543e-02 1.933072e-01
## [2,] 1.827691e-01 2.148324e-01 6.729654e-02 2.685137e-01 4.608806e-01
## [3,] 1.930588e-03 4.258120e-03 4.543499e-06 2.750395e-03 4.135626e-04
## [4,] 8.478407e-03 1.428110e-04 3.534677e-02 1.206636e-01 2.676432e-02
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
## R version 4.2.0 RC (2022-04-19 r82224)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Mojave 10.14.6
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib
##
## locale:
## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] einsum_0.1.0 DelayedRandomArray_1.4.0 HDF5Array_1.24.0
## [4] rhdf5_2.40.0 DelayedArray_0.22.0 IRanges_2.30.0
## [7] S4Vectors_0.34.0 MatrixGenerics_1.8.0 matrixStats_0.62.0
## [10] BiocGenerics_0.42.0 Matrix_1.4-1 DelayedTensor_1.2.0
## [13] BiocStyle_2.24.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.8.3 rTensor_1.4.8 bslib_0.3.1
## [4] compiler_4.2.0 BiocManager_1.30.17 jquerylib_0.1.4
## [7] rhdf5filters_1.8.0 tools_4.2.0 digest_0.6.29
## [10] jsonlite_1.8.0 evaluate_0.15 lattice_0.20-45
## [13] rlang_1.0.2 cli_3.3.0 parallel_4.2.0
## [16] yaml_2.3.5 xfun_0.30 fastmap_1.1.0
## [19] stringr_1.4.0 knitr_1.38 sass_0.4.1
## [22] grid_4.2.0 R6_2.5.1 BiocParallel_1.30.0
## [25] rmarkdown_2.14 bookdown_0.26 irlba_2.3.5
## [28] Rhdf5lib_1.18.0 magrittr_2.0.3 BiocSingular_1.12.0
## [31] htmltools_0.5.2 rsvd_1.0.5 beachmat_2.12.0
## [34] dqrng_0.3.0 ScaledMatrix_1.4.0 stringi_1.7.6