DelayedTensor 1.11.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2024-04-23 17:14:07
Compiled: Wed May 1 18:37:11 2024
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.7202674 0.3471031 0.7344270
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.7202674 0.3471031 0.7344270
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.0180784 0.10745249 0.9436410 0.5395815
## [2,] 0.6559085 0.98158485 0.2021125 0.9963272
## [3,] 0.1833793 0.03814931 0.9492518 0.9109456
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.01807840 0.10745249 0.94364099 0.53958150
## [2,] 0.65590848 0.98158485 0.20211247 0.99632718
## [3,] 0.18337930 0.03814931 0.94925181 0.91094558
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3935522 0.1515865 0.7522723 0.40008049
## [2,] 0.4229032 0.1999155 0.6195391 0.41701482
## [3,] 0.5227980 0.4667494 0.4275142 0.04324489
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5976598 0.24262814 0.92838360 0.09627356
## [2,] 0.6317382 0.64196429 0.03509633 0.13465391
## [3,] 0.2880195 0.05370634 0.33813158 0.02812419
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3469110 0.01532315 0.6504129 0.80523246
## [2,] 0.8520207 0.01590180 0.7477543 0.01631722
## [3,] 0.2410713 0.94929378 0.3562941 0.96393873
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1768755 0.5604456 0.525897939 0.1751305
## [2,] 0.9900119 0.5742820 0.000792199 0.8404791
## [3,] 0.8672624 0.2195078 0.265298781 0.6123359
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.21578957 0.5607626 0.1521433 0.7199196
## [2,] 0.85999973 0.4720765 0.1877576 0.7178241
## [3,] 0.06494126 0.3308589 0.8863111 0.8919539
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.39355220 0.15158646 0.75227231 0.40008049
## [2,] 0.42290317 0.19991545 0.61953909 0.41701482
## [3,] 0.52279795 0.46674935 0.42751422 0.04324489
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.59765978 0.24262814 0.92838360 0.09627356
## [2,] 0.63173823 0.64196429 0.03509633 0.13465391
## [3,] 0.28801946 0.05370634 0.33813158 0.02812419
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.34691099 0.01532315 0.65041292 0.80523246
## [2,] 0.85202072 0.01590180 0.74775432 0.01631722
## [3,] 0.24107128 0.94929378 0.35629412 0.96393873
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.176875469 0.560445590 0.525897939 0.175130511
## [2,] 0.990011919 0.574281991 0.000792199 0.840479130
## [3,] 0.867262368 0.219507758 0.265298781 0.612335897
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.21578957 0.56076263 0.15214333 0.71991959
## [2,] 0.85999973 0.47207651 0.18775760 0.71782406
## [3,] 0.06494126 0.33085887 0.88631113 0.89195390
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.5350511 0.3880381 0.3394419
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.5350511 0.3880381 0.3394419
einsum::einsum('iii->i', arrD)
## [1] 0.6713642 0.1566763 0.8402393
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.6713642 0.1566763 0.8402393
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.5187851 0.1204806 0.5393830
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.5187851 0.1204806 0.5393830
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.0003268287 0.01154604 0.89045833 0.2911482
## [2,] 0.4302159287 0.96350882 0.04084945 0.9926679
## [3,] 0.0336279661 0.00145537 0.90107900 0.8298219
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.0003268287 0.0115460384 0.8904583258 0.2911481926
## [2,] 0.4302159287 0.9635088168 0.0408494514 0.9926678592
## [3,] 0.0336279661 0.0014553699 0.9010790016 0.8298218516
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1548833 0.02297845 0.5659136 0.16006439
## [2,] 0.1788471 0.03996619 0.3838287 0.17390136
## [3,] 0.2733177 0.21785496 0.1827684 0.00187012
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.35719722 0.058868416 0.861896100 0.0092685976
## [2,] 0.39909319 0.412118154 0.001231752 0.0181316752
## [3,] 0.08295521 0.002884371 0.114332963 0.0007909701
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12034724 0.0002347988 0.4230370 0.6483993194
## [2,] 0.72593931 0.0002528673 0.5591365 0.0002662518
## [3,] 0.05811536 0.9011586744 0.1269455 0.9291778789
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03128493 0.31409926 2.765686e-01 0.0306707
## [2,] 0.98012360 0.32979981 6.275793e-07 0.7064052
## [3,] 0.75214401 0.04818366 7.038344e-02 0.3749553
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.046565139 0.3144547 0.02314759 0.5182842
## [2,] 0.739599535 0.2228562 0.03525292 0.5152714
## [3,] 0.004217367 0.1094676 0.78554742 0.7955818
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.15488334 0.02297845 0.56591363 0.16006439
## [2,] 0.17884709 0.03996619 0.38382869 0.17390136
## [3,] 0.27331770 0.21785496 0.18276841 0.00187012
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.3571972176 0.0588684155 0.8618960996 0.0092685976
## [2,] 0.3990931882 0.4121181536 0.0012317522 0.0181316752
## [3,] 0.0829552121 0.0028843713 0.1143329632 0.0007909701
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.1203472371 0.0002347988 0.4230369622 0.6483993194
## [2,] 0.7259393133 0.0002528673 0.5591365217 0.0002662518
## [3,] 0.0581153611 0.9011586744 0.1269454968 0.9291778789
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 3.128493e-02 3.140993e-01 2.765686e-01 3.067070e-02
## [2,] 9.801236e-01 3.297998e-01 6.275793e-07 7.064052e-01
## [3,] 7.521440e-01 4.818366e-02 7.038344e-02 3.749553e-01
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.046565139 0.314454730 0.023147594 0.518284212
## [2,] 0.739599535 0.222856230 0.035252917 0.515271384
## [3,] 0.004217367 0.109467592 0.785547417 0.795581757
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.5187851 0.2500071 0.5289838
## [2,] 0.2500071 0.1204806 0.2549219
## [3,] 0.5289838 0.2549219 0.5393830
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.5187851 0.2500071 0.5289838
## [2,] 0.2500071 0.1204806 0.2549219
## [3,] 0.5289838 0.2549219 0.5393830
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.007114795 0.04228817 0.37137199 0.2123535
## [2,] 0.258134226 0.38630488 0.07954181 0.3921068
## [3,] 0.072169326 0.01501375 0.37358014 0.3585046
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.007645414 0.04544200 0.39906877 0.2281907
## [2,] 0.277385773 0.41511534 0.08547401 0.4213499
## [3,] 0.077551685 0.01613346 0.40144160 0.3852418
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.009451352 0.05617594 0.4933336 0.2820921
## [2,] 0.342907610 0.51317055 0.1056640 0.5208778
## [3,] 0.095870321 0.01994438 0.4962669 0.4762405
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.002740441 0.016288343 0.14304319 0.08179325
## [2,] 0.099426841 0.148794968 0.03063751 0.15102971
## [3,] 0.027797817 0.005782919 0.14389372 0.13808701
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.003614152 0.021481414 0.18864842 0.1078707
## [2,] 0.131126239 0.196233979 0.04040541 0.1991812
## [3,] 0.036660355 0.007626637 0.18977010 0.1821121
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.008438083 0.05015338 0.44044382 0.2518493
## [2,] 0.306144857 0.45815409 0.09433587 0.4650351
## [3,] 0.085592168 0.01780617 0.44306267 0.4251833
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01359988 0.08083354 0.7098750 0.4059122
## [2,] 0.49342178 0.73841910 0.1520436 0.7495094
## [3,] 0.13795117 0.02869867 0.7140959 0.6852791
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01120028 0.06657102 0.5846225 0.3342918
## [2,] 0.40636094 0.60813019 0.1252166 0.6172636
## [3,] 0.11361064 0.02363499 0.5880986 0.5643664
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.007728774 0.04593747 0.40341994 0.2306788
## [2,] 0.280410200 0.41964148 0.08640596 0.4259440
## [3,] 0.078397256 0.01630937 0.40581865 0.3894422
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.007232816 0.04298965 0.37753235 0.2158760
## [2,] 0.262416181 0.39271294 0.08086126 0.3986111
## [3,] 0.073366478 0.01526279 0.37977713 0.3644515
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.007538962 0.04480928 0.3935123 0.2250135
## [2,] 0.273523558 0.40933543 0.0842839 0.4154832
## [3,] 0.076471885 0.01590883 0.3958521 0.3798778
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0007817985 0.004646771 0.040807647 0.02333414
## [2,] 0.0283646874 0.042448525 0.008740331 0.04308606
## [3,] 0.0079302168 0.001649763 0.041050287 0.03939374
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01080473 0.06422003 0.5639763 0.3224862
## [2,] 0.39201012 0.58665379 0.1207945 0.5954647
## [3,] 0.10959843 0.02280031 0.5673296 0.5444355
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01142082 0.06788185 0.5961341 0.3408743
## [2,] 0.41436246 0.62010467 0.1276822 0.6294180
## [3,] 0.11584771 0.02410038 0.5996787 0.5754791
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.005206932 0.03094841 0.27178697 0.1554100
## [2,] 0.188914408 0.28271554 0.05821233 0.2869616
## [3,] 0.052816807 0.01098774 0.27340300 0.2623701
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.004386329 0.026070999 0.22895386 0.1309177
## [2,] 0.159141855 0.238160109 0.04903817 0.2417370
## [3,] 0.044492978 0.009256096 0.23031520 0.2210210
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01160569 0.06898066 0.6057838 0.3463921
## [2,] 0.42106982 0.63014242 0.1297490 0.6396065
## [3,] 0.11772296 0.02449050 0.6093858 0.5847945
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0009709249 0.00577088 0.05067951 0.02897895
## [2,] 0.0352264455 0.05271733 0.01085472 0.05350909
## [3,] 0.0098486313 0.00204886 0.05098084 0.04892356
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01678369 0.09975713 0.8760608 0.5009386
## [2,] 0.60893467 0.91128727 0.1876379 0.9249738
## [3,] 0.17024633 0.03541719 0.8812698 0.8457069
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0006344856 0.003771188 0.033118333 0.01893733
## [2,] 0.0230199782 0.034450023 0.007093405 0.03496742
## [3,] 0.0064359397 0.001338901 0.033315252 0.03197084
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.006112879 0.03633308 0.31907482 0.1824495
## [2,] 0.221783367 0.33190483 0.06834061 0.3368897
## [3,] 0.062006330 0.01289949 0.32097201 0.3080195
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.001740472 0.01034483 0.09084767 0.05194743
## [2,] 0.063146641 0.09450066 0.01945809 0.09591996
## [3,] 0.017654577 0.00367277 0.09138785 0.08769997
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.002434328 0.014468898 0.12706495 0.07265676
## [2,] 0.088320640 0.132174237 0.02721523 0.13415935
## [3,] 0.024692739 0.005136954 0.12782047 0.12266238
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0005084405 0.003022014 0.02653914 0.01517529
## [2,] 0.0184468947 0.027606279 0.00568425 0.02802090
## [3,] 0.0051573942 0.001072918 0.02669694 0.02561961
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.006271597 0.03727645 0.32735943 0.1871868
## [2,] 0.227541861 0.34052257 0.07011504 0.3456369
## [3,] 0.063616294 0.01323442 0.32930589 0.3160170
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01540317 0.09155175 0.8040017 0.4597346
## [2,] 0.55884761 0.83633063 0.1722040 0.8488914
## [3,] 0.15624296 0.03250400 0.8087822 0.7761445
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.004358184 0.025903710 0.22748474 0.1300776
## [2,] 0.158120695 0.236631914 0.04872351 0.2401859
## [3,] 0.044207481 0.009196703 0.22883735 0.2196028
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.000277018 0.0016465104 0.014459550 0.008268087
## [2,] 0.010050582 0.0150409693 0.003096999 0.015266868
## [3,] 0.002809948 0.0005845675 0.014545525 0.013958553
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0002874792 0.0017086881 0.015005591 0.008580317
## [2,] 0.0104301257 0.0156089664 0.003213952 0.015843396
## [3,] 0.0029160610 0.0006066427 0.015094813 0.014485675
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01716172 0.1020040 0.8957925 0.5122214
## [2,] 0.62264983 0.9318124 0.1918641 0.9458072
## [3,] 0.17408082 0.0362149 0.9011188 0.8647550
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01175843 0.06988849 0.6137563 0.3509508
## [2,] 0.42661134 0.63843546 0.1314566 0.6480241
## [3,] 0.11927226 0.02481280 0.6174056 0.5924908
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0135182 0.08034807 0.7056116 0.4034744
## [2,] 0.4904584 0.73398431 0.1511305 0.7450080
## [3,] 0.1371227 0.02852631 0.7098071 0.6811635
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.006441229 0.03828469 0.33621373 0.1922497
## [2,] 0.233696330 0.34973291 0.07201148 0.3549855
## [3,] 0.065336964 0.01359238 0.33821283 0.3245646
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01455732 0.08652424 0.7598504 0.4344885
## [2,] 0.52815880 0.79040399 0.1627475 0.8022750
## [3,] 0.14766296 0.03071906 0.7643684 0.7335230
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0002949893 0.0017533263 0.015397601 0.008804472
## [2,] 0.0107026050 0.0160167391 0.003297914 0.016257293
## [3,] 0.0029922409 0.0006224908 0.015489154 0.014864102
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01742647 0.1035776 0.9096121 0.5201235
## [2,] 0.63225558 0.9461877 0.1948240 0.9603984
## [3,] 0.17676641 0.0367736 0.9150206 0.8780957
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.003197626 0.019005710 0.16690694 0.09543873
## [2,] 0.116014119 0.173618280 0.03574874 0.17622584
## [3,] 0.032435299 0.006747677 0.16789936 0.16112393
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01789783 0.10637925 0.9342158 0.5341921
## [2,] 0.64935721 0.97178070 0.2000938 0.9863758
## [3,] 0.18154769 0.03776827 0.9397706 0.9018470
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01567872 0.09318950 0.8183843 0.4679587
## [2,] 0.56884474 0.85129160 0.1752845 0.8640771
## [3,] 0.15903796 0.03308546 0.8232504 0.7900288
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01013196 0.06022128 0.5288594 0.3024061
## [2,] 0.36760101 0.55012490 0.1132730 0.5583872
## [3,] 0.10277412 0.02138061 0.5320040 0.5105354
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0103821 0.06170803 0.5419160 0.3098719
## [2,] 0.3766764 0.56370650 0.1160696 0.5721728
## [3,] 0.1053114 0.02190846 0.5451382 0.5231396
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.00396835 0.02358666 0.20713652 0.1184423
## [2,] 0.14397700 0.21546549 0.04436526 0.2187015
## [3,] 0.04025318 0.00837407 0.20836814 0.1999596
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.009507395 0.05650904 0.4962589 0.2837648
## [2,] 0.344940916 0.51621345 0.1062905 0.5239664
## [3,] 0.096438794 0.02006264 0.4992096 0.4790644
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 1.432169e-05 8.512376e-05 0.0007475515 0.0004274559
## [2,] 5.196101e-04 7.776106e-04 0.0001601133 0.0007892894
## [3,] 1.452729e-04 3.022185e-05 0.0007519964 0.0007216502
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.004796178 0.02850702 0.25034681 0.1431503
## [2,] 0.174011719 0.26041326 0.05362019 0.2643244
## [3,] 0.048650304 0.01012097 0.25183535 0.2416728
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.00316608 0.018818210 0.16526033 0.09449718
## [2,] 0.11486959 0.171905456 0.03539606 0.17448729
## [3,] 0.03211531 0.006681108 0.16624295 0.15953437
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01519452 0.09031158 0.7931106 0.4535070
## [2,] 0.55127739 0.82500158 0.1698713 0.8373922
## [3,] 0.15412647 0.03206370 0.7978263 0.7656307
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01107006 0.06579702 0.5778253 0.3304051
## [2,] 0.40163630 0.60105964 0.1237607 0.6100869
## [3,] 0.11228973 0.02336019 0.5812610 0.5578047
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.003901131 0.023187128 0.20362789 0.1164361
## [2,] 0.141538209 0.211815774 0.04361376 0.2149970
## [3,] 0.039571340 0.008232223 0.20483864 0.1965726
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01554742 0.09240912 0.8115310 0.4640399
## [2,] 0.56408111 0.84416271 0.1738167 0.8568411
## [3,] 0.15770614 0.03280840 0.8163563 0.7834130
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.001174034 0.006978100 0.06128124 0.03504110
## [2,] 0.042595523 0.063745357 0.01312544 0.06470274
## [3,] 0.011908883 0.002477464 0.06164561 0.05915795
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01013769 0.06025534 0.5291586 0.3025771
## [2,] 0.36780896 0.55043610 0.1133371 0.5587031
## [3,] 0.10283226 0.02139271 0.5323049 0.5108242
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.00853439 0.05072580 0.44547075 0.2547237
## [2,] 0.30963898 0.46338315 0.09541255 0.4703427
## [3,] 0.08656906 0.01800939 0.44811948 0.4300360
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.00598140 0.03555161 0.3122120 0.1785253
## [2,] 0.21701314 0.32476605 0.0668707 0.3296437
## [3,] 0.06067267 0.01262204 0.3140684 0.3013944
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.002750509 0.016348181 0.14356869 0.08209373
## [2,] 0.099792102 0.149341592 0.03075007 0.15158454
## [3,] 0.027899937 0.005804163 0.14442234 0.13859430
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.003394358 0.020175023 0.17717577 0.1013105
## [2,] 0.123151803 0.184300018 0.03794815 0.1870680
## [3,] 0.034430857 0.007162823 0.17822924 0.1710370
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01602309 0.09523634 0.8363595 0.4782371
## [2,] 0.58133898 0.86998958 0.1791345 0.8830559
## [3,] 0.16253111 0.03381216 0.8413324 0.8073812
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0130150 0.07735715 0.6793456 0.3884553
## [2,] 0.4722014 0.70666216 0.1455047 0.7172755
## [3,] 0.1320183 0.02746444 0.6833850 0.6558076
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01297711 0.07713199 0.6773682 0.3873246
## [2,] 0.47082689 0.70460522 0.1450812 0.7151876
## [3,] 0.13163407 0.02738449 0.6813958 0.6538987
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0161251 0.09584267 0.8416843 0.4812818
## [2,] 0.5850401 0.87552843 0.1802750 0.8886779
## [3,] 0.1635659 0.03402743 0.8466889 0.8125215
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.007114795 0.042288166 0.371371993 0.212353487
## [2,] 0.258134226 0.386304881 0.079541809 0.392106759
## [3,] 0.072169326 0.015013745 0.373580142 0.358504641
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.007645414 0.045442000 0.399068767 0.228190725
## [2,] 0.277385773 0.415115344 0.085474005 0.421349924
## [3,] 0.077551685 0.016133465 0.401441599 0.385241773
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.009451352 0.056175944 0.493333582 0.282092104
## [2,] 0.342907610 0.513170552 0.105663987 0.520877815
## [3,] 0.095870321 0.019944382 0.496266906 0.476240487
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.01301500 0.07735715 0.67934564 0.38845529
## [2,] 0.47220136 0.70666216 0.14550473 0.71727546
## [3,] 0.13201835 0.02746444 0.68338497 0.65580757
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.01297711 0.07713199 0.67736821 0.38732458
## [2,] 0.47082689 0.70460522 0.14508120 0.71518763
## [3,] 0.13163407 0.02738449 0.68139579 0.65389866
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.01612510 0.09584267 0.84168426 0.48128182
## [2,] 0.58504012 0.87552843 0.18027501 0.88867792
## [3,] 0.16356588 0.03402743 0.84668885 0.81252146
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.801797
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.801797
einsum::einsum('ij->', arrC)
## [1] 6.526412
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 6.526412
einsum::einsum('ijk->', arrE)
## [1] 26.66268
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 26.66268
einsum::einsum('ij->i', arrC)
## [1] 1.608753 2.835933 2.081726
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 1.608753 2.835933 2.081726
einsum::einsum('ij->j', arrC)
## [1] 0.8573662 1.1271867 2.0950053 2.4468543
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 0.8573662 1.1271867 2.0950053 2.4468543
einsum::einsum('ijk->i', arrE)
## [1] 8.467281 9.378043 8.817356
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 8.467281 9.378043 8.817356
einsum::einsum('ijk->j', arrE)
## [1] 7.471554 5.455002 6.873599 6.862523
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 7.471554 5.455002 6.873599 6.862523
einsum::einsum('ijk->k', arrE)
## [1] 4.817170 4.016379 5.960471 5.808320 6.060338
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 4.817170 4.016379 5.960471 5.808320 6.060338
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 1.730788 1.530746 3.00911 2.196637
## [2,] 3.756674 1.904140 1.59094 2.126289
## [3,] 1.984092 2.020116 2.27355 2.539598
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 1.730788 1.530746 3.009110 2.196637
## [2,] 3.756674 1.904140 1.590940 2.126289
## [3,] 1.984092 2.020116 2.273550 2.539598
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.3392533 1.5174175 1.4400030 2.0341498 1.140731
## [2,] 0.8182513 0.9382988 0.9805187 1.3542353 1.363698
## [3,] 1.7993256 1.3016115 1.7544614 0.7919889 1.226212
## [4,] 0.8603402 0.2590517 1.7854884 1.6279455 2.329698
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.3392533 1.5174175 1.4400030 2.0341498 1.1407306
## [2,] 0.8182513 0.9382988 0.9805187 1.3542353 1.3636980
## [3,] 1.7993256 1.3016115 1.7544614 0.7919889 1.2262121
## [4,] 0.8603402 0.2590517 1.7854884 1.6279455 2.3296975
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.3392533 1.5174175 1.4400030 2.0341498 1.140731
## [2,] 0.8182513 0.9382988 0.9805187 1.3542353 1.363698
## [3,] 1.7993256 1.3016115 1.7544614 0.7919889 1.226212
## [4,] 0.8603402 0.2590517 1.7854884 1.6279455 2.329698
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.3392533 1.5174175 1.4400030 2.0341498 1.1407306
## [2,] 0.8182513 0.9382988 0.9805187 1.3542353 1.3636980
## [3,] 1.7993256 1.3016115 1.7544614 0.7919889 1.2262121
## [4,] 0.8603402 0.2590517 1.7854884 1.6279455 2.3296975
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.262531
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 1.262531
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.5350511 0.05193154 0.6306118
## [2,] 0.5796954 0.38803814 0.8467529
## [3,] 0.3764128 0.74349017 0.3394419
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.53505114 0.05193154 0.63061185
## [2,] 0.57969541 0.38803814 0.84675285
## [3,] 0.37641277 0.74349017 0.33944188
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.67136422 0.8610879 0.1706606
## [2,] 0.01183406 0.8266996 0.8029917
## [3,] 0.92340076 0.6853224 0.8545848
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.5907522 0.1000545 0.69688168
## [2,] 0.4816729 0.1566763 0.04330248
## [3,] 0.3938733 0.6742726 0.49154861
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.4192477 0.2453218 0.2972490
## [2,] 0.7578599 0.2240365 0.1944710
## [3,] 0.2147963 0.3555942 0.8402393
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.67136422 0.86108789 0.17066058
## [2,] 0.01183406 0.82669956 0.80299174
## [3,] 0.92340076 0.68532244 0.85458484
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.59075218 0.10005445 0.69688168
## [2,] 0.48167285 0.15667625 0.04330248
## [3,] 0.39387331 0.67427261 0.49154861
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.4192477 0.2453218 0.2972490
## [2,] 0.7578599 0.2240365 0.1944710
## [3,] 0.2147963 0.3555942 0.8402393
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] 1.178649
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.178649
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 5.386706
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 5.386706
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 17.19284
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 17.19284
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.6070481 0.83924562 0.9044019 1.7635525 0.7903820
## [2,] 0.2807996 0.47387094 0.9016463 0.6920827 0.6467786
## [3,] 1.1325107 0.97746081 1.1091190 0.3469527 0.8439479
## [4,] 0.3358359 0.02819124 1.5778435 1.1120311 1.8291374
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.60704813 0.83924562 0.90440191 1.76355255 0.79038204
## [2,] 0.28079960 0.47387094 0.90164634 0.69208272 0.64677855
## [3,] 1.13251072 0.97746081 1.10911898 0.34695271 0.84394793
## [4,] 0.33583588 0.02819124 1.57784345 1.11203111 1.82913735
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.1934794 0.8456528 1.394697
## [2,] 0.8456528 2.4272421 1.257182
## [3,] 1.3946967 1.2571823 1.765984
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.1934794 0.8456528 1.3946967
## [2,] 0.8456528 2.4272421 1.2571823
## [3,] 1.3946967 1.2571823 1.7659842
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.0003268287 0.43021593 0.03362797
## [2,] 0.0115460384 0.96350882 0.00145537
## [3,] 0.8904583258 0.04084945 0.90107900
## [4,] 0.2911481926 0.99266786 0.82982185
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.0003268287 0.4302159287 0.0336279661
## [2,] 0.0115460384 0.9635088168 0.0014553699
## [3,] 0.8904583258 0.0408494514 0.9010790016
## [4,] 0.2911481926 0.9926678592 0.8298218516
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.15488334 0.357197218 0.1203472371 0.03128493 0.04656514
## [2,] 0.02297845 0.058868416 0.0002347988 0.31409926 0.31445473
## [3,] 0.56591363 0.861896100 0.4230369622 0.27656864 0.02314759
## [4,] 0.16006439 0.009268598 0.6483993194 0.03067070 0.51828421
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.17884709 0.399093188 0.7259393133 9.801236e-01 0.73959954
## [2,] 0.03996619 0.412118154 0.0002528673 3.297998e-01 0.22285623
## [3,] 0.38382869 0.001231752 0.5591365217 6.275793e-07 0.03525292
## [4,] 0.17390136 0.018131675 0.0002662518 7.064052e-01 0.51527138
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.27331770 0.0829552121 0.05811536 0.75214401 0.004217367
## [2,] 0.21785496 0.0028843713 0.90115867 0.04818366 0.109467592
## [3,] 0.18276841 0.1143329632 0.12694550 0.07038344 0.785547417
## [4,] 0.00187012 0.0007909701 0.92917788 0.37495525 0.795581757
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.1548833370 0.3571972176 0.1203472371 0.0312849315 0.0465651393
## [2,] 0.0229784534 0.0588684155 0.0002347988 0.3140992595 0.3144547302
## [3,] 0.5659136276 0.8618960996 0.4230369622 0.2765686427 0.0231475941
## [4,] 0.1600643945 0.0092685976 0.6483993194 0.0306706959 0.5182842121
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.788471e-01 3.990932e-01 7.259393e-01 9.801236e-01 7.395995e-01
## [2,] 3.996619e-02 4.121182e-01 2.528673e-04 3.297998e-01 2.228562e-01
## [3,] 3.838287e-01 1.231752e-03 5.591365e-01 6.275793e-07 3.525292e-02
## [4,] 1.739014e-01 1.813168e-02 2.662518e-04 7.064052e-01 5.152714e-01
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.2733177017 0.0829552121 0.0581153611 0.7521440143 0.0042173673
## [2,] 0.2178549591 0.0028843713 0.9011586744 0.0481836556 0.1094675924
## [3,] 0.1827684072 0.1143329632 0.1269454968 0.0703834434 0.7855474173
## [4,] 0.0018701202 0.0007909701 0.9291778789 0.3749552508 0.7955817572
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 1.697491 1.659373 1.4603064
## [2,] 1.864945 1.443453 0.7079816
## [3,] 1.817880 1.631994 2.5105979
## [4,] 1.438350 2.405565 1.9644048
## [5,] 1.648615 2.237658 2.1740652
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.6974915 1.6593725 1.4603064
## [2,] 1.8649451 1.4434528 0.7079816
## [3,] 1.8178795 1.6319941 2.5105979
## [4,] 1.4383495 2.4055652 1.9644048
## [5,] 1.6486151 2.2376579 2.1740652
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,] 3.646016e-05 8.408567e-05 2.833022e-05 7.364599e-06 1.096162e-05
## [2,] 1.910942e-04 4.895636e-04 1.952642e-06 2.612123e-03 2.615079e-03
## [3,] 3.629589e-01 5.527927e-01 2.713224e-01 1.773823e-01 1.484613e-02
## [4,] 3.356623e-02 1.943667e-03 1.359723e-01 6.431784e-03 1.086866e-01
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.026707108 5.959630e-02 1.084040e-01 1.463612e-01 0.1104438706
## [2,] 0.013366168 1.378275e-01 8.456814e-05 1.102972e-01 0.0745313478
## [3,] 0.005442296 1.746498e-05 7.927981e-03 8.898429e-09 0.0004998501
## [4,] 0.059919123 6.247416e-03 9.173920e-05 2.433976e-01 0.1775409286
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0067502054 2.048769e-03 0.0014352917 1.857592e-02 0.0001041575
## [2,] 0.0002328571 3.082998e-06 0.0009632151 5.150172e-05 0.0001170059
## [3,] 0.1209518793 7.566289e-02 0.0840095761 4.657813e-02 0.5198570031
## [4,] 0.0011397327 4.820516e-04 0.5662814733 2.285141e-01 0.4848621775
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,] 3.646016e-05 8.408567e-05 2.833022e-05 7.364599e-06 1.096162e-05
## [2,] 1.910942e-04 4.895636e-04 1.952642e-06 2.612123e-03 2.615079e-03
## [3,] 3.629589e-01 5.527927e-01 2.713224e-01 1.773823e-01 1.484613e-02
## [4,] 3.356623e-02 1.943667e-03 1.359723e-01 6.431784e-03 1.086866e-01
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.670711e-02 5.959630e-02 1.084040e-01 1.463612e-01 1.104439e-01
## [2,] 1.336617e-02 1.378275e-01 8.456814e-05 1.102972e-01 7.453135e-02
## [3,] 5.442296e-03 1.746498e-05 7.927981e-03 8.898429e-09 4.998501e-04
## [4,] 5.991912e-02 6.247416e-03 9.173920e-05 2.433976e-01 1.775409e-01
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 6.750205e-03 2.048769e-03 1.435292e-03 1.857592e-02 1.041575e-04
## [2,] 2.328571e-04 3.082998e-06 9.632151e-04 5.150172e-05 1.170059e-04
## [3,] 1.209519e-01 7.566289e-02 8.400958e-02 4.657813e-02 5.198570e-01
## [4,] 1.139733e-03 4.820516e-04 5.662815e-01 2.285141e-01 4.848622e-01
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.4.0 Patched (2024-04-24 r86482)
## Platform: x86_64-apple-darwin20
## Running under: macOS Monterey 12.7.4
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0
##
## locale:
## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## time zone: America/New_York
## tzcode source: internal
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] einsum_0.1.2 DelayedRandomArray_1.13.0
## [3] HDF5Array_1.33.0 rhdf5_2.49.0
## [5] DelayedArray_0.31.0 SparseArray_1.5.0
## [7] S4Arrays_1.5.0 abind_1.4-5
## [9] IRanges_2.39.0 S4Vectors_0.43.0
## [11] MatrixGenerics_1.17.0 matrixStats_1.3.0
## [13] BiocGenerics_0.51.0 Matrix_1.7-0
## [15] DelayedTensor_1.11.0 BiocStyle_2.33.0
##
## loaded via a namespace (and not attached):
## [1] jsonlite_1.8.8 compiler_4.4.0 BiocManager_1.30.22
## [4] crayon_1.5.2 rsvd_1.0.5 Rcpp_1.0.12
## [7] rhdf5filters_1.17.0 parallel_4.4.0 jquerylib_0.1.4
## [10] BiocParallel_1.39.0 yaml_2.3.8 fastmap_1.1.1
## [13] lattice_0.22-6 R6_2.5.1 XVector_0.45.0
## [16] ScaledMatrix_1.13.0 knitr_1.46 bookdown_0.39
## [19] bslib_0.7.0 rlang_1.1.3 cachem_1.0.8
## [22] xfun_0.43 sass_0.4.9 cli_3.6.2
## [25] Rhdf5lib_1.27.0 BiocSingular_1.21.0 zlibbioc_1.51.0
## [28] digest_0.6.35 grid_4.4.0 irlba_2.3.5.1
## [31] rTensor_1.4.8 dqrng_0.3.2 lifecycle_1.0.4
## [34] evaluate_0.23 codetools_0.2-20 beachmat_2.21.0
## [37] rmarkdown_2.26 tools_4.4.0 htmltools_0.5.8.1