DelayedTensor 1.4.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2022-11-01 15:55:12
Compiled: Tue Nov 8 07:21:35 2022
einsum
einsum
is an easy and intuitive way to write tensor operations.
It was originally introduced by
Numpy
1
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
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.
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.1725263 0.3396187 0.2426805
DelayedTensor::einsum('i->i', darrA)
## <3> array of class DelayedArray and type "double":
## [1] [2] [3]
## 0.1725263 0.3396187 0.2426805
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.6185682 0.1000074 0.1790709 0.09193419
## [2,] 0.5338655 0.6760010 0.1439728 0.92336834
## [3,] 0.8249636 0.3994717 0.4756649 0.79742736
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.61856820 0.10000740 0.17907090 0.09193419
## [2,] 0.53386554 0.67600096 0.14397276 0.92336834
## [3,] 0.82496362 0.39947171 0.47566491 0.79742736
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1291971 0.69386739 0.6308525 0.5804388
## [2,] 0.4127041 0.09008455 0.2636588 0.9610305
## [3,] 0.8679368 0.94878611 0.5774750 0.1599449
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4294800 0.2580615 0.5345020 0.53573463
## [2,] 0.5549031 0.6652978 0.9679304 0.02134952
## [3,] 0.7837988 0.1634234 0.9041998 0.45692466
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6906492 0.4709925 0.1586909 0.4830126
## [2,] 0.9949523 0.5615428 0.4509960 0.4003067
## [3,] 0.2478749 0.1411837 0.1901870 0.6953729
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6012800 0.6083028 0.5389154 0.5686319
## [2,] 0.2186771 0.8663225 0.4162738 0.1048302
## [3,] 0.3464077 0.5808866 0.5908258 0.6944242
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6891095 0.9085809 0.439594058 0.9291073
## [2,] 0.7263975 0.8227726 0.004168645 0.7054056
## [3,] 0.3063796 0.9883913 0.389343424 0.8394185
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.12919710 0.69386739 0.63085247 0.58043881
## [2,] 0.41270413 0.09008455 0.26365880 0.96103053
## [3,] 0.86793681 0.94878611 0.57747503 0.15994491
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.42948003 0.25806146 0.53450201 0.53573463
## [2,] 0.55490313 0.66529779 0.96793042 0.02134952
## [3,] 0.78379882 0.16342338 0.90419977 0.45692466
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.6906492 0.4709925 0.1586909 0.4830126
## [2,] 0.9949523 0.5615428 0.4509960 0.4003067
## [3,] 0.2478749 0.1411837 0.1901870 0.6953729
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.6012800 0.6083028 0.5389154 0.5686319
## [2,] 0.2186771 0.8663225 0.4162738 0.1048302
## [3,] 0.3464077 0.5808866 0.5908258 0.6944242
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.689109471 0.908580918 0.439594058 0.929107303
## [2,] 0.726397480 0.822772607 0.004168645 0.705405565
## [3,] 0.306379566 0.988391269 0.389343424 0.839418509
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.08089694 0.02906196 0.71047511
DelayedTensor::einsum('ii->i', darrB)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.08089694 0.02906196 0.71047511
einsum::einsum('iii->i', arrD)
## [1] 0.9863949 0.1476439 0.7864602
DelayedTensor::einsum('iii->i', darrD)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.9863949 0.1476439 0.7864602
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.02976533 0.11534083 0.05889384
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.02976533 0.11534083 0.05889384
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.3826266 0.01000148 0.03206639 0.008451895
## [2,] 0.2850124 0.45697730 0.02072816 0.852609086
## [3,] 0.6805650 0.15957765 0.22625711 0.635890394
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.382626614 0.010001480 0.032066388 0.008451895
## [2,] 0.285012417 0.456977303 0.020728156 0.852609086
## [3,] 0.680564973 0.159577651 0.226257105 0.635890394
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01669189 0.481451954 0.39797484 0.33690921
## [2,] 0.17032470 0.008115227 0.06951597 0.92357968
## [3,] 0.75331431 0.900195082 0.33347741 0.02558238
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1844531 0.06659572 0.2856924 0.2870115962
## [2,] 0.3079175 0.44262116 0.9368893 0.0004558019
## [3,] 0.6143406 0.02670720 0.8175772 0.2087801474
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.47699635 0.22183398 0.02518279 0.2333012
## [2,] 0.98993015 0.31533029 0.20339739 0.1602455
## [3,] 0.06144198 0.01993284 0.03617108 0.4835435
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.36153764 0.3700323 0.2904298 0.32334226
## [2,] 0.04781966 0.7505147 0.1732839 0.01098938
## [3,] 0.11999828 0.3374293 0.3490751 0.48222495
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.47487186 0.8255193 1.932429e-01 0.8632404
## [2,] 0.52765330 0.6769548 1.737761e-05 0.4975970
## [3,] 0.09386844 0.9769173 1.515883e-01 0.7046234
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.016691890 0.481451954 0.397974842 0.336909212
## [2,] 0.170324698 0.008115227 0.069515965 0.923579677
## [3,] 0.753314309 0.900195082 0.333477415 0.025582375
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.1844530995 0.0665957182 0.2856923938 0.2870115962
## [2,] 0.3079174817 0.4426211555 0.9368892963 0.0004558019
## [3,] 0.6143405897 0.0267072027 0.8175772311 0.2087801474
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.47699635 0.22183398 0.02518279 0.23330117
## [2,] 0.98993015 0.31533029 0.20339739 0.16024548
## [3,] 0.06144198 0.01993284 0.03617108 0.48354350
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.36153764 0.37003231 0.29042982 0.32334226
## [2,] 0.04781966 0.75051472 0.17328391 0.01098938
## [3,] 0.11999828 0.33742926 0.34907511 0.48222495
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 4.748719e-01 8.255193e-01 1.932429e-01 8.632404e-01
## [2,] 5.276533e-01 6.769548e-01 1.737761e-05 4.975970e-01
## [3,] 9.386844e-02 9.769173e-01 1.515883e-01 7.046234e-01
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.02976533 0.05859316 0.04186878
## [2,] 0.05859316 0.11534083 0.08241884
## [3,] 0.04186878 0.08241884 0.05889384
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.02976533 0.05859316 0.04186878
## [2,] 0.05859316 0.11534083 0.08241884
## [3,] 0.04186878 0.08241884 0.05889384
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07991721 0.01292067 0.02313544 0.01187763
## [2,] 0.06897388 0.08733736 0.01860086 0.11929651
## [3,] 0.10658290 0.05161059 0.06145452 0.10302530
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2552856 0.04127347 0.07390330 0.03794162
## [2,] 0.2203285 0.27898839 0.05941815 0.38107793
## [3,] 0.3404659 0.16486363 0.19630887 0.32910156
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5368781 0.0868001 0.1554222 0.07979307
## [2,] 0.4633616 0.5867261 0.1249593 0.80142537
## [3,] 0.7160163 0.3467162 0.4128471 0.69211656
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4292043 0.06939187 0.1242515 0.06379013
## [2,] 0.3704319 0.46905502 0.0998980 0.64069518
## [3,] 0.5724154 0.27718040 0.3300484 0.55330884
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05572344 0.009009122 0.01613152 0.00828185
## [2,] 0.04809304 0.060897245 0.01296972 0.08318122
## [3,] 0.07431648 0.035986231 0.04285006 0.07183589
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5868889 0.09488563 0.1699000 0.08722588
## [2,] 0.5065242 0.64138032 0.1365994 0.87607905
## [3,] 0.7827140 0.37901321 0.4513043 0.75658800
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3902253 0.06308992 0.11296732 0.05799691
## [2,] 0.3367904 0.42645688 0.09082557 0.58250920
## [3,] 0.5204303 0.25200772 0.30007438 0.50305902
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1630910 0.02636783 0.04721362 0.02423926
## [2,] 0.1407584 0.17823361 0.03795969 0.24345419
## [3,] 0.2175089 0.10532423 0.12541324 0.21024874
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3572077 0.05775178 0.10340898 0.0530897
## [2,] 0.3082940 0.39037368 0.08314067 0.5332222
## [3,] 0.4763959 0.23068494 0.27468461 0.4604944
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3590410 0.05804818 0.10393970 0.05336217
## [2,] 0.3098763 0.39237719 0.08356738 0.53595882
## [3,] 0.4788409 0.23186889 0.27609437 0.46285779
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5944629 0.09611016 0.1720926 0.08835156
## [2,] 0.5130611 0.64965756 0.1383622 0.88738516
## [3,] 0.7928152 0.38390451 0.4571285 0.76635204
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09893684 0.01599567 0.02864148 0.01470441
## [2,] 0.08538908 0.10812291 0.02302771 0.14768807
## [3,] 0.13194873 0.06389347 0.07608018 0.12754445
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2656627 0.04295118 0.07690738 0.0394839
## [2,] 0.2292846 0.29032892 0.06183343 0.3965683
## [3,] 0.3543054 0.17156513 0.20428858 0.3424791
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3432454 0.05549442 0.09936700 0.05101457
## [2,] 0.2962437 0.37511505 0.07989093 0.51237998
## [3,] 0.4577749 0.22166810 0.26394795 0.44249494
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4848330 0.07838568 0.1403556 0.07205791
## [2,] 0.4184432 0.52984876 0.1128457 0.72373501
## [3,] 0.6466055 0.31310546 0.3728256 0.62502262
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1596286 0.02580806 0.04621130 0.02372467
## [2,] 0.1377701 0.17444980 0.03715382 0.23828578
## [3,] 0.2128913 0.10308825 0.12275078 0.20578527
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4115321 0.0665347 0.11913548 0.06116361
## [2,] 0.3551796 0.4497420 0.09578476 0.61431492
## [3,] 0.5488465 0.2657677 0.31645881 0.53052666
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10108851 0.01634355 0.02926437 0.0150242
## [2,] 0.08724611 0.11047437 0.02352852 0.1509000
## [3,] 0.13481835 0.06528302 0.07773477 0.1303183
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3306259 0.05345416 0.09571376 0.04913901
## [2,] 0.2853522 0.36132387 0.07695373 0.49354223
## [3,] 0.4409447 0.21351843 0.25424385 0.42622652
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5987310 0.0968002 0.1733282 0.0889859
## [2,] 0.5167447 0.6543219 0.1393556 0.8937563
## [3,] 0.7985074 0.3866608 0.4604105 0.7718542
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5593092 0.09042667 0.1619159 0.08312687
## [2,] 0.4827211 0.61123992 0.1301801 0.83490944
## [3,] 0.7459319 0.36120223 0.4300961 0.72103364
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3313884 0.05357743 0.09593448 0.04925233
## [2,] 0.2860103 0.36215713 0.07713119 0.49468040
## [3,] 0.4419616 0.21401083 0.25483016 0.42720945
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01320613 0.002135110 0.003823078 0.001962751
## [2,] 0.01139777 0.014432295 0.003073749 0.019713469
## [3,] 0.01761258 0.008528529 0.010155217 0.017024690
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2826391 0.04569585 0.08182191 0.0420070
## [2,] 0.2439363 0.30888151 0.06578470 0.4219098
## [3,] 0.3769462 0.18252848 0.21734303 0.3643642
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4272136 0.06907003 0.12367518 0.06349427
## [2,] 0.3687138 0.46687954 0.09943467 0.63772362
## [3,] 0.5697605 0.27589483 0.32851760 0.55074258
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6154459 0.0995026 0.1781670 0.09147013
## [2,] 0.5311708 0.6725887 0.1432460 0.91870748
## [3,] 0.8207995 0.3974553 0.4732639 0.79340221
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1533275 0.02478933 0.04438719 0.02278818
## [2,] 0.1323319 0.16756369 0.03568724 0.22887986
## [3,] 0.2044878 0.09901902 0.11790540 0.19766225
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2913410 0.04710274 0.08434106 0.04330032
## [2,] 0.2514467 0.31839141 0.06781010 0.43489960
## [3,] 0.3885517 0.18814820 0.22403463 0.37558234
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3473525 0.05615843 0.10055597 0.05162498
## [2,] 0.2997883 0.37960346 0.08084686 0.51851082
## [3,] 0.4632524 0.22432046 0.26710619 0.44778958
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08733174 0.01411941 0.02528189 0.01297961
## [2,] 0.07537311 0.09544031 0.02032661 0.13036455
## [3,] 0.11647141 0.05639889 0.06715613 0.11258374
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09816112 0.01587026 0.02841692 0.01458912
## [2,] 0.08471959 0.10727518 0.02284716 0.14653012
## [3,] 0.13091419 0.06339251 0.07548368 0.12654444
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2789718 0.04510294 0.08076026 0.04146195
## [2,] 0.2407712 0.30487373 0.06493114 0.41643543
## [3,] 0.3720553 0.18016015 0.21452297 0.35963655
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1176436 0.01902010 0.03405695 0.01748468
## [2,] 0.1015343 0.12856657 0.02738174 0.17561261
## [3,] 0.1568973 0.07597431 0.09046526 0.15166028
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2987762 0.04830483 0.08649350 0.04440537
## [2,] 0.2578638 0.32651698 0.06954066 0.44599854
## [3,] 0.3984678 0.19294987 0.22975214 0.38516746
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2476170 0.04003364 0.07168329 0.03680187
## [2,] 0.2137100 0.27060774 0.05763327 0.36963056
## [3,] 0.3302385 0.15991122 0.19041187 0.31921554
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4301356 0.06954244 0.1245211 0.06392854
## [2,] 0.3712356 0.47007276 0.1001148 0.64208534
## [3,] 0.5736574 0.27778181 0.3307645 0.55450939
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3719327 0.06013245 0.10767175 0.05527819
## [2,] 0.3210027 0.40646586 0.08656794 0.55520292
## [3,] 0.4960341 0.24019435 0.28600780 0.47947713
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1352667 0.02186932 0.03915870 0.0201039
## [2,] 0.1167441 0.14782591 0.03148354 0.2019195
## [3,] 0.1804006 0.08735530 0.10401701 0.1743791
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2142768 0.03464333 0.06203154 0.03184671
## [2,] 0.1849351 0.23417193 0.04987327 0.31986188
## [3,] 0.2857737 0.13838007 0.16477398 0.27623496
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3762768 0.06083478 0.10892933 0.05592382
## [2,] 0.3247519 0.41121328 0.08757903 0.56168755
## [3,] 0.5018277 0.24299977 0.28934830 0.48507730
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5358796 0.08663866 0.1551332 0.07964466
## [2,] 0.4624997 0.58563486 0.1247268 0.79993479
## [3,] 0.7146846 0.34607134 0.4120792 0.69082928
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3593180 0.05809296 0.10401989 0.05340334
## [2,] 0.3101153 0.39267991 0.08363185 0.53637231
## [3,] 0.4792103 0.23204777 0.27630738 0.46321488
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3333559 0.05389553 0.09650407 0.04954475
## [2,] 0.2877084 0.36430734 0.07758914 0.49761743
## [3,] 0.4445856 0.21528146 0.25634315 0.42974589
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2574938 0.04163046 0.07454253 0.0382698
## [2,] 0.2222343 0.28140152 0.05993209 0.3843741
## [3,] 0.3434108 0.16628963 0.19800686 0.3319482
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3654660 0.05908695 0.10579971 0.05431709
## [2,] 0.3154215 0.39939880 0.08506282 0.54554982
## [3,] 0.4874098 0.23601819 0.28103509 0.47114065
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3517376 0.0568674 0.10182543 0.05227671
## [2,] 0.3035730 0.3843957 0.08186751 0.52505671
## [3,] 0.4691006 0.2271524 0.27047825 0.45344265
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06484465 0.01048380 0.01877204 0.009637482
## [2,] 0.05596525 0.07086534 0.01509270 0.096796915
## [3,] 0.08648113 0.04187671 0.04986406 0.083594494
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4295487 0.06944756 0.12435117 0.06384132
## [2,] 0.3707291 0.46943142 0.09997817 0.64120931
## [3,] 0.5728747 0.27740282 0.33031322 0.55375285
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4262612 0.06891605 0.12339946 0.06335272
## [2,] 0.3678918 0.46583867 0.09921299 0.63630187
## [3,] 0.5684902 0.27527974 0.32778519 0.54951475
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4493264 0.07264512 0.1300767 0.06678076
## [2,] 0.3877986 0.49104540 0.1045815 0.67073243
## [3,] 0.5992515 0.29017525 0.3455218 0.57924922
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1895167 0.03064022 0.05486367 0.02816676
## [2,] 0.1635655 0.20711288 0.04411031 0.28290119
## [3,] 0.2527520 0.12238997 0.14573401 0.24431545
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5620193 0.09086482 0.1627004 0.08352965
## [2,] 0.4850600 0.61420158 0.1308109 0.83895485
## [3,] 0.7495462 0.36295238 0.4321801 0.72452728
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5089410 0.08228335 0.1473346 0.07564093
## [2,] 0.4392499 0.55619508 0.1184568 0.75972217
## [3,] 0.6787575 0.32867438 0.3913641 0.65610139
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6113874 0.09884644 0.1769921 0.09086695
## [2,] 0.5276680 0.66815345 0.1423014 0.91264920
## [3,] 0.8153868 0.39483435 0.4701430 0.78817024
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2719189 0.04396266 0.07871850 0.04041372
## [2,] 0.2346841 0.29716601 0.06328957 0.40590723
## [3,] 0.3626491 0.17560539 0.20909947 0.35054433
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.002578592 0.0004168954 0.0007464831 0.000383241
## [2,] 0.002225496 0.0028180084 0.0006001714 0.003849195
## [3,] 0.003438981 0.0016652560 0.0019828784 0.003324192
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2408355 0.03893722 0.06972008 0.03579397
## [2,] 0.2078570 0.26319653 0.05605485 0.35950739
## [3,] 0.3211942 0.15553169 0.18519700 0.31047310
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5747162 0.09291761 0.1663761 0.08541672
## [2,] 0.4960184 0.62807743 0.1337661 0.85790827
## [3,] 0.7664797 0.37115209 0.4419437 0.74089558
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4363414 0.07054578 0.1263176 0.06485089
## [2,] 0.3765917 0.47685484 0.1015592 0.65134916
## [3,] 0.5819339 0.28178957 0.3355367 0.56250970
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5192376 0.08394806 0.1503154 0.07717126
## [2,] 0.4481366 0.56744772 0.1208534 0.77509247
## [3,] 0.6924897 0.33532395 0.3992819 0.66937529
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.07991721 0.01292067 0.02313544 0.01187763
## [2,] 0.06897388 0.08733736 0.01860086 0.11929651
## [3,] 0.10658290 0.05161059 0.06145452 0.10302530
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.25528565 0.04127347 0.07390330 0.03794162
## [2,] 0.22032851 0.27898839 0.05941815 0.38107793
## [3,] 0.34046589 0.16486363 0.19630887 0.32910156
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.53687811 0.08680010 0.15542223 0.07979307
## [2,] 0.46336156 0.58672612 0.12495926 0.80142537
## [3,] 0.71601629 0.34671621 0.41284708 0.69211656
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.57471623 0.09291761 0.16637608 0.08541672
## [2,] 0.49601837 0.62807743 0.13376614 0.85790827
## [3,] 0.76647972 0.37115209 0.44194374 0.74089558
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.43634145 0.07054578 0.12631761 0.06485089
## [2,] 0.37659172 0.47685484 0.10155919 0.65134916
## [3,] 0.58193393 0.28178957 0.33553667 0.56250970
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.51923759 0.08394806 0.15031543 0.07717126
## [2,] 0.44813662 0.56744772 0.12085340 0.77509247
## [3,] 0.69248973 0.33532395 0.39928193 0.66937529
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] 0.7548255
DelayedTensor::einsum('i->', darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 0.7548255
einsum::einsum('ij->', arrC)
## [1] 5.764316
DelayedTensor::einsum('ij->', darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 5.764316
einsum::einsum('ijk->', arrE)
## [1] 31.96179
DelayedTensor::einsum('ijk->', darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 31.96179
einsum::einsum('ij->i', arrC)
## [1] 0.9895807 2.2772076 2.4975276
DelayedTensor::einsum('ij->i', darrC)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.9895807 2.2772076 2.4975276
einsum::einsum('ij->j', arrC)
## [1] 1.9773974 1.1754801 0.7987086 1.8127299
DelayedTensor::einsum('ij->j', darrC)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 1.9773974 1.1754801 0.7987086 1.8127299
einsum::einsum('ijk->i', arrE)
## [1] 10.87900 10.20960 10.87319
DelayedTensor::einsum('ijk->i', darrE)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 10.87900 10.20960 10.87319
einsum::einsum('ijk->j', arrE)
## [1] 7.999748 8.768496 7.057614 8.135933
DelayedTensor::einsum('ijk->j', darrE)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 7.999748 8.768496 7.057614 8.135933
einsum::einsum('ijk->k', arrE)
## [1] 6.315977 6.275606 5.485762 6.135778 7.748669
DelayedTensor::einsum('ijk->k', darrE)
## <5> array of class HDF5Array and type "double":
## [1] [2] [3] [4] [5]
## 6.315977 6.275606 5.485762 6.135778 7.748669
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 2.539716 2.939805 2.302555 3.096925
## [2,] 2.907634 3.006020 2.103028 2.192923
## [3,] 2.552398 2.822671 2.652031 2.846085
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 2.539716 2.939805 2.302555 3.096925
## [2,] 2.907634 3.006020 2.103028 2.192923
## [3,] 2.552398 2.822671 2.652031 2.846085
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.409838 1.768182 1.9334765 1.166365 1.7218865
## [2,] 1.732738 1.086783 1.1737190 2.055512 2.7197448
## [3,] 1.471986 2.406632 0.7998738 1.546015 0.8331061
## [4,] 1.701414 1.014009 1.5786922 1.367886 2.4739314
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.4098380 1.7681820 1.9334765 1.1663647 1.7218865
## [2,] 1.7327381 1.0867826 1.1737190 2.0555119 2.7197448
## [3,] 1.4719863 2.4066322 0.7998738 1.5460150 0.8331061
## [4,] 1.7014143 1.0140088 1.5786922 1.3678863 2.4739314
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.409838 1.768182 1.9334765 1.166365 1.7218865
## [2,] 1.732738 1.086783 1.1737190 2.055512 2.7197448
## [3,] 1.471986 2.406632 0.7998738 1.546015 0.8331061
## [4,] 1.701414 1.014009 1.5786922 1.367886 2.4739314
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.4098380 1.7681820 1.9334765 1.1663647 1.7218865
## [2,] 1.7327381 1.0867826 1.1737190 2.0555119 2.7197448
## [3,] 1.4719863 2.4066322 0.7998738 1.5460150 0.8331061
## [4,] 1.7014143 1.0140088 1.5786922 1.3678863 2.4739314
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 0.820434
DelayedTensor::einsum('ii->', darrB)
## <1> array of class HDF5Array and type "double":
## [1]
## 0.820434
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.08089694 0.45510287 0.77980742
## [2,] 0.12780030 0.02906196 0.02988412
## [3,] 0.25288320 0.92353303 0.71047511
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3]
## [1,] 0.08089694 0.45510287 0.77980742
## [2,] 0.12780030 0.02906196 0.02988412
## [3,] 0.25288320 0.92353303 0.71047511
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.9863949 0.95631040 0.0636393
## [2,] 0.5317895 0.44834143 0.9204078
## [3,] 0.5834943 0.09146218 0.3111580
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.94630427 0.4464644 0.68339606
## [2,] 0.91957869 0.1476439 0.03377691
## [3,] 0.06364817 0.3394574 0.32086772
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.6872800 0.13512423 0.4777986
## [2,] 0.4266272 0.55185813 0.6546220
## [3,] 0.5981272 0.03982946 0.7864602
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.98639488 0.95631040 0.06363930
## [2,] 0.53178953 0.44834143 0.92040783
## [3,] 0.58349430 0.09146218 0.31115801
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.94630427 0.44646439 0.68339606
## [2,] 0.91957869 0.14764388 0.03377691
## [3,] 0.06364817 0.33945745 0.32086772
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.68727998 0.13512423 0.47779862
## [2,] 0.42662723 0.55185813 0.65462202
## [3,] 0.59812716 0.03982946 0.78646022
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.204
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 0.204
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 3.750763
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 3.750763
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 21.42625
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 21.42625
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.9403309 1.1067112 1.5283685 0.5293556 1.0963936
## [2,] 1.3897623 0.5359241 0.5570971 1.4579763 2.4793913
## [3,] 0.8009682 2.0401589 0.2647513 0.8127888 0.3448486
## [4,] 1.2860713 0.4962475 0.8770901 0.8165566 2.0654608
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9403309 1.1067112 1.5283685 0.5293556 1.0963936
## [2,] 1.3897623 0.5359241 0.5570971 1.4579763 2.4793913
## [3,] 0.8009682 2.0401589 0.2647513 0.8127888 0.3448486
## [4,] 1.2860713 0.4962475 0.8770901 0.8165566 2.0654608
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 0.4331464 0.5085078 0.708735
## [2,] 0.5085078 1.6153270 1.515265
## [3,] 0.7087350 1.5152649 1.702290
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.4331464 0.5085078 0.7087350
## [2,] 0.5085078 1.6153270 1.5152649
## [3,] 0.7087350 1.5152649 1.7022901
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.382626614 0.28501242 0.6805650
## [2,] 0.010001480 0.45697730 0.1595777
## [3,] 0.032066388 0.02072816 0.2262571
## [4,] 0.008451895 0.85260909 0.6358904
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.382626614 0.285012417 0.680564973
## [2,] 0.010001480 0.456977303 0.159577651
## [3,] 0.032066388 0.020728156 0.226257105
## [4,] 0.008451895 0.852609086 0.635890394
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.01669189 0.18445310 0.47699635 0.3615376 0.4748719
## [2,] 0.48145195 0.06659572 0.22183398 0.3700323 0.8255193
## [3,] 0.39797484 0.28569239 0.02518279 0.2904298 0.1932429
## [4,] 0.33690921 0.28701160 0.23330117 0.3233423 0.8632404
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.170324698 0.3079174817 0.9899301 0.04781966 5.276533e-01
## [2,] 0.008115227 0.4426211555 0.3153303 0.75051472 6.769548e-01
## [3,] 0.069515965 0.9368892963 0.2033974 0.17328391 1.737761e-05
## [4,] 0.923579677 0.0004558019 0.1602455 0.01098938 4.975970e-01
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.75331431 0.6143406 0.06144198 0.1199983 0.09386844
## [2,] 0.90019508 0.0267072 0.01993284 0.3374293 0.97691730
## [3,] 0.33347741 0.8175772 0.03617108 0.3490751 0.15158830
## [4,] 0.02558238 0.2087801 0.48354350 0.4822250 0.70462343
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.01669189 0.18445310 0.47699635 0.36153764 0.47487186
## [2,] 0.48145195 0.06659572 0.22183398 0.37003231 0.82551928
## [3,] 0.39797484 0.28569239 0.02518279 0.29042982 0.19324294
## [4,] 0.33690921 0.28701160 0.23330117 0.32334226 0.86324038
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.703247e-01 3.079175e-01 9.899301e-01 4.781966e-02 5.276533e-01
## [2,] 8.115227e-03 4.426212e-01 3.153303e-01 7.505147e-01 6.769548e-01
## [3,] 6.951597e-02 9.368893e-01 2.033974e-01 1.732839e-01 1.737761e-05
## [4,] 9.235797e-01 4.558019e-04 1.602455e-01 1.098938e-02 4.975970e-01
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.75331431 0.61434059 0.06144198 0.11999828 0.09386844
## [2,] 0.90019508 0.02670720 0.01993284 0.33742926 0.97691730
## [3,] 0.33347741 0.81757723 0.03617108 0.34907511 0.15158830
## [4,] 0.02558238 0.20878015 0.48354350 0.48222495 0.70462343
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 2.034356 1.727478 2.554143
## [2,] 1.757778 2.209481 2.308347
## [3,] 1.803345 2.407798 1.274618
## [4,] 2.317130 1.606104 2.212544
## [5,] 2.966392 2.258744 2.523533
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 2.034356 1.727478 2.554143
## [2,] 1.757778 2.209481 2.308347
## [3,] 1.803345 2.407798 1.274618
## [4,] 2.317130 1.606104 2.212544
## [5,] 2.966392 2.258744 2.523533
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.0011018844 0.0121763321 0.0314880364 0.0238662425 0.031347793
## [2,] 0.0008307543 0.0001149121 0.0003827786 0.0006384976 0.001424449
## [3,] 0.0022017146 0.0015805349 0.0001393187 0.0016067437 0.001069077
## [4,] 0.0004912723 0.0004185129 0.0003401938 0.0004714894 0.001258755
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0164866702 0.0298050372 0.09582082 0.004628729 5.107448e-02
## [2,] 0.0012594671 0.0686939259 0.04893864 0.116478397 1.050620e-01
## [3,] 0.0004893693 0.0065953899 0.00143185 0.001219861 1.223326e-07
## [4,] 0.2674335344 0.0001319829 0.04640099 0.003182106 1.440852e-01
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.124417296 0.101464414 0.0101477494 0.01981890 0.01550330
## [2,] 0.034861306 0.001034274 0.0007719268 0.01306742 0.03783248
## [3,] 0.018310643 0.044891691 0.0019860887 0.01916708 0.00832344
## [4,] 0.003947827 0.032218581 0.0746195733 0.07441610 0.10873624
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,] 0.0011018844 0.0121763321 0.0314880364 0.0238662425 0.0313477926
## [2,] 0.0008307543 0.0001149121 0.0003827786 0.0006384976 0.0014244488
## [3,] 0.0022017146 0.0015805349 0.0001393187 0.0016067437 0.0010690771
## [4,] 0.0004912723 0.0004185129 0.0003401938 0.0004714894 0.0012587549
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.648667e-02 2.980504e-02 9.582082e-02 4.628729e-03 5.107448e-02
## [2,] 1.259467e-03 6.869393e-02 4.893864e-02 1.164784e-01 1.050620e-01
## [3,] 4.893693e-04 6.595390e-03 1.431850e-03 1.219861e-03 1.223326e-07
## [4,] 2.674335e-01 1.319829e-04 4.640099e-02 3.182106e-03 1.440852e-01
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.1244172957 0.1014644138 0.0101477494 0.0198189009 0.0155032994
## [2,] 0.0348613058 0.0010342735 0.0007719268 0.0130674170 0.0378324804
## [3,] 0.0183106432 0.0448916907 0.0019860887 0.0191670843 0.0083234402
## [4,] 0.0039478266 0.0322185812 0.0746195733 0.0744160978 0.1087362356
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
(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.1 (2022-06-23)
## Platform: aarch64-apple-darwin20 (64-bit)
## Running under: macOS Ventura 13.0
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/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.6.0 HDF5Array_1.26.0
## [4] rhdf5_2.42.0 DelayedArray_0.24.0 IRanges_2.32.0
## [7] S4Vectors_0.36.0 MatrixGenerics_1.10.0 matrixStats_0.62.0
## [10] BiocGenerics_0.44.0 Matrix_1.4-1 DelayedTensor_1.4.0
## [13] BiocStyle_2.26.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.9 rTensor_1.4.8 bslib_0.3.1
## [4] compiler_4.2.1 BiocManager_1.30.18 jquerylib_0.1.4
## [7] rhdf5filters_1.10.0 tools_4.2.1 digest_0.6.29
## [10] jsonlite_1.8.0 evaluate_0.15 lattice_0.20-45
## [13] rlang_1.0.4 cli_3.3.0 parallel_4.2.1
## [16] yaml_2.3.5 xfun_0.31 fastmap_1.1.0
## [19] stringr_1.4.0 knitr_1.39 sass_0.4.1
## [22] grid_4.2.1 R6_2.5.1 BiocParallel_1.32.1
## [25] rmarkdown_2.14 bookdown_0.27 irlba_2.3.5
## [28] Rhdf5lib_1.20.0 magrittr_2.0.3 BiocSingular_1.14.0
## [31] codetools_0.2-18 htmltools_0.5.2 rsvd_1.0.5
## [34] beachmat_2.14.0 dqrng_0.3.0 ScaledMatrix_1.6.0
## [37] stringi_1.7.8
https://numpy.org/doc/stable/reference/generated/numpy.einsum.html↩︎
https://stackoverflow.com/ questions/56067643/speeding-up-kronecker-products-numpy↩︎