DelayedTensor 1.11.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2024-04-23 17:14:07.775684
Compiled: Wed May 1 17:01:18 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.8078085 0.4380282 0.3983436
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.8078085 0.4380282 0.3983436
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.31060345 0.1034135 0.7276005 0.8417179
## [2,] 0.66833623 0.5029561 0.7642404 0.9714516
## [3,] 0.03397528 0.3170997 0.2047461 0.7766108
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.31060345 0.10341345 0.72760052 0.84171795
## [2,] 0.66833623 0.50295612 0.76424040 0.97145158
## [3,] 0.03397528 0.31709965 0.20474615 0.77661077
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6515454 0.38186126 0.1842415 0.4875236
## [2,] 0.1550512 0.06930932 0.1747797 0.4221523
## [3,] 0.7265625 0.78799588 0.5953040 0.6682801
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8545924 0.72529186 0.5936690 0.8072265
## [2,] 0.3882640 0.24331634 0.2365493 0.1166793
## [3,] 0.8158703 0.02814333 0.6225236 0.9627005
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5711561 0.4153912 0.3117442 0.21966902
## [2,] 0.5763068 0.1413529 0.2943208 0.86701239
## [3,] 0.2158185 0.3302018 0.9217726 0.03711289
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8897719 0.4363019 0.3892889 0.4847332
## [2,] 0.4976779 0.8448077 0.3693882 0.2091084
## [3,] 0.7557405 0.6353960 0.1943900 0.2358331
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7669595 0.5320980 0.8460130 0.38769074
## [2,] 0.1544301 0.4137726 0.1490351 0.04707721
## [3,] 0.6106117 0.9601918 0.2406658 0.49042628
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.65154538 0.38186126 0.18424153 0.48752357
## [2,] 0.15505120 0.06930932 0.17477967 0.42215233
## [3,] 0.72656253 0.78799588 0.59530403 0.66828007
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.85459244 0.72529186 0.59366898 0.80722649
## [2,] 0.38826402 0.24331634 0.23654932 0.11667931
## [3,] 0.81587032 0.02814333 0.62252360 0.96270047
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.57115614 0.41539120 0.31174422 0.21966902
## [2,] 0.57630684 0.14135287 0.29432079 0.86701239
## [3,] 0.21581847 0.33020176 0.92177260 0.03711289
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.8897719 0.4363019 0.3892889 0.4847332
## [2,] 0.4976779 0.8448077 0.3693882 0.2091084
## [3,] 0.7557405 0.6353960 0.1943900 0.2358331
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.76695953 0.53209799 0.84601298 0.38769074
## [2,] 0.15443009 0.41377265 0.14903508 0.04707721
## [3,] 0.61061171 0.96019177 0.24066579 0.49042628
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.85397468 0.09949394 0.91234937
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.85397468 0.09949394 0.91234937
einsum::einsum('iii->i', arrD)
## [1] 0.2340494 0.1983494 0.8957590
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.2340494 0.1983494 0.8957590
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.6525547 0.1918687 0.1586776
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.6525547 0.1918687 0.1586776
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.09647450 0.01069434 0.52940252 0.7084891
## [2,] 0.44667332 0.25296485 0.58406339 0.9437182
## [3,] 0.00115432 0.10055219 0.04192098 0.6031243
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.09647450 0.01069434 0.52940252 0.70848910
## [2,] 0.44667332 0.25296485 0.58406339 0.94371818
## [3,] 0.00115432 0.10055219 0.04192098 0.60312428
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.42451138 0.145818024 0.03394494 0.2376792
## [2,] 0.02404088 0.004803781 0.03054793 0.1782126
## [3,] 0.52789311 0.620937505 0.35438689 0.4465983
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7303282 0.5260482880 0.35244286 0.65161461
## [2,] 0.1507489 0.0592028428 0.05595558 0.01361406
## [3,] 0.6656444 0.0007920468 0.38753563 0.92679220
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.32621934 0.17254985 0.09718446 0.048254479
## [2,] 0.33212958 0.01998063 0.08662473 0.751710478
## [3,] 0.04657761 0.10903320 0.84966472 0.001377366
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7916941 0.1903594 0.15154584 0.23496624
## [2,] 0.2476833 0.7137000 0.13644767 0.04372634
## [3,] 0.5711437 0.4037281 0.03778749 0.05561725
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.58822692 0.2831283 0.71573796 0.150304110
## [2,] 0.02384865 0.1712078 0.02221146 0.002216263
## [3,] 0.37284666 0.9219682 0.05792002 0.240517939
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.424511379 0.145818024 0.033944941 0.237679233
## [2,] 0.024040876 0.004803781 0.030547933 0.178212586
## [3,] 0.527893114 0.620937505 0.354386893 0.446598256
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.7303282321 0.5260482880 0.3524428637 0.6516146110
## [2,] 0.1507489478 0.0592028428 0.0559555813 0.0136140616
## [3,] 0.6656443748 0.0007920468 0.3875356268 0.9267922037
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.326219338 0.172549849 0.097184459 0.048254479
## [2,] 0.332129578 0.019980633 0.086624725 0.751710478
## [3,] 0.046577613 0.109033203 0.849664719 0.001377366
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.79169411 0.19035937 0.15154584 0.23496624
## [2,] 0.24768333 0.71369999 0.13644767 0.04372634
## [3,] 0.57114367 0.40372805 0.03778749 0.05561725
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.588226915 0.283128271 0.715737964 0.150304110
## [2,] 0.023848653 0.171207802 0.022211455 0.002216263
## [3,] 0.372846656 0.921968239 0.057920024 0.240517939
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.6525547 0.3538430 0.3217854
## [2,] 0.3538430 0.1918687 0.1744857
## [3,] 0.3217854 0.1744857 0.1586776
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.6525547 0.3538430 0.3217854
## [2,] 0.3538430 0.1918687 0.1744857
## [3,] 0.3217854 0.1744857 0.1586776
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.20237224 0.06737856 0.4740648 0.5484174
## [2,] 0.43545138 0.32769873 0.4979373 0.6329448
## [3,] 0.02213644 0.20660481 0.1334014 0.5059972
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.048159438 0.01603438 0.11281534 0.1305094
## [2,] 0.103626336 0.07798395 0.11849639 0.1506247
## [3,] 0.005267909 0.04916668 0.03174614 0.1204144
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.22567283 0.07513634 0.5286473 0.6115607
## [2,] 0.48558806 0.36542907 0.5552684 0.7058203
## [3,] 0.02468517 0.23039272 0.1487609 0.5642563
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11860742 0.03948959 0.27784245 0.3214195
## [2,] 0.25521172 0.19205946 0.29183380 0.3709597
## [3,] 0.01297384 0.12108807 0.07818462 0.2965576
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.021527713 0.007167516 0.05042950 0.05833890
## [2,] 0.046321928 0.034859545 0.05296898 0.06733065
## [3,] 0.002354804 0.021977960 0.01419082 0.05382636
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.24475424 0.08148938 0.5733462 0.6632703
## [2,] 0.52664619 0.39632735 0.6022183 0.7654998
## [3,] 0.02677238 0.24987322 0.1613391 0.6119661
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.057226054 0.01905305 0.13405423 0.1550794
## [2,] 0.123135289 0.09266540 0.14080482 0.1789817
## [3,] 0.006259658 0.05842292 0.03772274 0.1430840
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.054287168 0.01807457 0.12716978 0.1471152
## [2,] 0.116811586 0.08790650 0.13357369 0.1697900
## [3,] 0.005938189 0.05542257 0.03578546 0.1357358
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.18490349 0.06156245 0.4331435 0.5010781
## [2,] 0.39786325 0.29941180 0.4549554 0.5783090
## [3,] 0.02022562 0.18877070 0.1218862 0.4623195
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.15142650 0.0504165 0.35472241 0.4103573
## [2,] 0.32582967 0.2452030 0.37258521 0.4736055
## [3,] 0.01656375 0.1545936 0.09981857 0.3786161
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13112197 0.04365623 0.30715825 0.3553332
## [2,] 0.28213969 0.21232409 0.32262586 0.4101005
## [3,] 0.01434275 0.13386435 0.08643406 0.3278480
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.20757009 0.06910915 0.4862409 0.5625033
## [2,] 0.44663578 0.33611555 0.5107266 0.6492017
## [3,] 0.02270501 0.21191138 0.1368278 0.5189935
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.26543936 0.08837636 0.6218019 0.7193258
## [2,] 0.57115509 0.42982249 0.6531141 0.8301952
## [3,] 0.02903502 0.27099096 0.1749745 0.6636857
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12059614 0.04015172 0.28250110 0.3268088
## [2,] 0.25949091 0.19527976 0.29672705 0.3771797
## [3,] 0.01319138 0.12311838 0.07949556 0.3015300
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.25341213 0.08437197 0.5936277 0.6867327
## [2,] 0.54527569 0.41034697 0.6235211 0.7925785
## [3,] 0.02771943 0.25871219 0.1670463 0.6336137
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2252782 0.07500494 0.5277227 0.6104912
## [2,] 0.4847388 0.36478998 0.5542973 0.7045859
## [3,] 0.0246420 0.22998980 0.1485007 0.5632695
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.075574895 0.02516218 0.17703710 0.2048037
## [2,] 0.162617127 0.12237744 0.18595218 0.2363700
## [3,] 0.008266742 0.07715553 0.04981808 0.1889621
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0087414142 0.002910399 0.020477099 0.02368874
## [2,] 0.0188092047 0.014154858 0.021508267 0.02733988
## [3,] 0.0009561775 0.008924239 0.005762238 0.02185641
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.18439563 0.06139336 0.4319539 0.4997018
## [2,] 0.39677049 0.29858945 0.4537058 0.5767207
## [3,] 0.02017007 0.18825223 0.1215514 0.4610497
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07347303 0.02446238 0.17211341 0.1991078
## [2,] 0.15809448 0.11897393 0.18078055 0.2297962
## [3,] 0.00803683 0.07500971 0.04843256 0.1837067
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.19335797 0.06437731 0.4529485 0.5239893
## [2,] 0.41605507 0.31310205 0.4757577 0.6047515
## [3,] 0.02115042 0.19740201 0.1274593 0.4834585
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.25072733 0.08347808 0.5873384 0.6794570
## [2,] 0.53949871 0.40599950 0.6169151 0.7841815
## [3,] 0.02742575 0.25597124 0.1652765 0.6269008
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.036240996 0.01206621 0.08489593 0.09821107
## [2,] 0.077981011 0.05868457 0.08917104 0.11334830
## [3,] 0.003964213 0.03699897 0.02388964 0.09061441
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.29901809 0.09955618 0.7004614 0.8103223
## [2,] 0.64340761 0.48419609 0.7357346 0.9352169
## [3,] 0.03270802 0.30527198 0.1971092 0.7476436
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.17740307 0.05906523 0.4155735 0.4807524
## [2,] 0.38172434 0.28726647 0.4365006 0.5548505
## [3,] 0.01940519 0.18111341 0.1169420 0.4435660
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.17900289 0.05959788 0.4193212 0.4850878
## [2,] 0.38516674 0.28985705 0.4404370 0.5598542
## [3,] 0.01958019 0.18274670 0.1179966 0.4475661
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.067033961 0.02231853 0.1570296 0.1816583
## [2,] 0.144239304 0.10854722 0.1649372 0.2096572
## [3,] 0.007332494 0.06843596 0.0441880 0.1676069
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12902194 0.04295704 0.30223885 0.3496422
## [2,] 0.27762099 0.20892354 0.31745874 0.4035324
## [3,] 0.01411303 0.13172040 0.08504975 0.3225973
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.043904687 0.01461779 0.10284842 0.1189792
## [2,] 0.094471241 0.07109429 0.10802757 0.1373175
## [3,] 0.004802504 0.04482294 0.02894145 0.1097762
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1025618 0.0341473 0.24025497 0.2779367
## [2,] 0.2206858 0.1660770 0.25235353 0.3207750
## [3,] 0.0112187 0.1047069 0.06760754 0.2564382
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09682883 0.03223855 0.22682526 0.2624007
## [2,] 0.20834996 0.15679366 0.23824753 0.3028444
## [3,] 0.01059160 0.09885398 0.06382843 0.2421039
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.091417051 0.03043673 0.21414796 0.2477351
## [2,] 0.196705244 0.14803044 0.22493184 0.2859184
## [3,] 0.009999632 0.09332902 0.06026105 0.2285727
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.28630575 0.09532369 0.6706822 0.7758725
## [2,] 0.61605402 0.46361116 0.7044559 0.8954574
## [3,] 0.03131749 0.29229377 0.1887294 0.7158585
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.068229955 0.02271673 0.15983130 0.1848994
## [2,] 0.146812766 0.11048388 0.16787994 0.2133978
## [3,] 0.007463317 0.06965697 0.04497639 0.1705973
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.26929704 0.08966075 0.6308387 0.7297799
## [2,] 0.57945579 0.43606918 0.6626059 0.8422606
## [3,] 0.02945699 0.27492932 0.1775174 0.6733312
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.011527390 0.003837972 0.02700336 0.03123858
## [2,] 0.024803886 0.018666153 0.02836317 0.03605337
## [3,] 0.001260921 0.011768483 0.00759872 0.02882227
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.27636623 0.09201439 0.6473985 0.7489370
## [2,] 0.59466683 0.44751624 0.6799997 0.8643704
## [3,] 0.03023025 0.28214637 0.1821774 0.6910065
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.15458048 0.05146659 0.3621107 0.4189045
## [2,] 0.33261620 0.25031016 0.3803456 0.4834700
## [3,] 0.01690875 0.15781350 0.1018976 0.3865020
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2347356 0.07815373 0.5498772 0.6361203
## [2,] 0.5050887 0.38010430 0.5775674 0.7341653
## [3,] 0.0256765 0.23964504 0.1547350 0.5869162
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13551688 0.04511949 0.31745351 0.3672432
## [2,] 0.29159638 0.21944072 0.33343956 0.4238462
## [3,] 0.01482348 0.13835119 0.08933114 0.3388368
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.26240017 0.08736448 0.6146825 0.7110898
## [2,] 0.56461557 0.42490118 0.6456361 0.8206897
## [3,] 0.02870258 0.26788821 0.1729711 0.6560867
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.19735618 0.06570849 0.4623144 0.5348242
## [2,] 0.42465815 0.31957629 0.4855953 0.6172564
## [3,] 0.02158776 0.20148384 0.1300949 0.4934554
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1209145 0.04025771 0.2832468 0.3276714
## [2,] 0.2601759 0.19579523 0.2975103 0.3781753
## [3,] 0.0132262 0.12344337 0.0797054 0.3023259
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11473326 0.03819971 0.26876707 0.3109207
## [2,] 0.24687554 0.18578607 0.28230141 0.3588428
## [3,] 0.01255007 0.11713288 0.07563082 0.2868709
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.060378217 0.02010255 0.14143830 0.1636216
## [2,] 0.129917908 0.09776966 0.14856072 0.1888405
## [3,] 0.006604457 0.06164101 0.03980061 0.1509654
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.15055979 0.05012793 0.35269210 0.4080086
## [2,] 0.32396473 0.24379951 0.37045266 0.4708948
## [3,] 0.01646895 0.15370872 0.09924725 0.3764490
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.064949803 0.02162463 0.15214741 0.1760103
## [2,] 0.139754748 0.10517237 0.15980912 0.2031387
## [3,] 0.007104519 0.06630821 0.04281415 0.1623959
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.073250574 0.02438832 0.17159229 0.1985050
## [2,] 0.157615804 0.11861370 0.18023318 0.2291004
## [3,] 0.008012497 0.07478259 0.04828592 0.1831505
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.23822027 0.07931393 0.5580402 0.6455636
## [2,] 0.51258684 0.38574698 0.5861415 0.7450640
## [3,] 0.02605767 0.24320260 0.1570320 0.5956290
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.047966519 0.01597015 0.11236342 0.1299866
## [2,] 0.103211226 0.07767156 0.11802172 0.1500214
## [3,] 0.005246806 0.04896973 0.03161897 0.1199321
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.18965810 0.06314547 0.4442814 0.5139628
## [2,] 0.40809393 0.30711089 0.4666541 0.5931797
## [3,] 0.02074571 0.19362476 0.1250204 0.4742076
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.16527147 0.05502609 0.3871548 0.4478764
## [2,] 0.35562036 0.26762194 0.4066508 0.5169074
## [3,] 0.01807818 0.16872809 0.1089450 0.4132330
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12851921 0.04278966 0.30106119 0.3482799
## [2,] 0.27653925 0.20810948 0.31622177 0.4019601
## [3,] 0.01405804 0.13120716 0.08471835 0.3213403
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.29823887 0.09929675 0.6986360 0.8082106
## [2,] 0.64173095 0.48293432 0.7338173 0.9327798
## [3,] 0.03262279 0.30447647 0.1965956 0.7456953
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.26277455 0.08748912 0.6155595 0.7121043
## [2,] 0.56542113 0.42550740 0.6465573 0.8218606
## [3,] 0.02874353 0.26827042 0.1732179 0.6570228
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.046290810 0.01541223 0.10843800 0.1254455
## [2,] 0.099605543 0.07495810 0.11389863 0.1447804
## [3,] 0.005063509 0.04725897 0.03051436 0.1157422
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.074751625 0.02488808 0.17510856 0.2025727
## [2,] 0.160845669 0.12104433 0.18392652 0.2337952
## [3,] 0.008176689 0.07631504 0.04927539 0.1869036
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1204181 0.04009244 0.28208399 0.3263263
## [2,] 0.2591078 0.19499143 0.29628893 0.3766228
## [3,] 0.0131719 0.12293660 0.07937818 0.3010848
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.014622343 0.004868417 0.034253401 0.03962573
## [2,] 0.031463403 0.023677769 0.035978304 0.04573323
## [3,] 0.001599462 0.014928166 0.009638877 0.03656067
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.15232809 0.05071668 0.3568344 0.4128006
## [2,] 0.32776965 0.24666290 0.3748036 0.4764254
## [3,] 0.01666237 0.15551400 0.1004129 0.3808703
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.20237224 0.06737856 0.47406476 0.54841744
## [2,] 0.43545138 0.32769873 0.49793730 0.63294479
## [3,] 0.02213644 0.20660481 0.13340140 0.50599716
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.048159438 0.016034380 0.112815336 0.130509380
## [2,] 0.103626336 0.077983951 0.118496394 0.150624736
## [3,] 0.005267909 0.049166682 0.031746136 0.120414434
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.22567283 0.07513634 0.52864728 0.61156072
## [2,] 0.48558806 0.36542907 0.55526844 0.70582032
## [3,] 0.02468517 0.23039272 0.14876088 0.56425629
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.12041808 0.04009244 0.28208399 0.32632625
## [2,] 0.25910777 0.19499143 0.29628893 0.37662278
## [3,] 0.01317190 0.12293660 0.07937818 0.30108480
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.014622343 0.004868417 0.034253401 0.039625730
## [2,] 0.031463403 0.023677769 0.035978304 0.045733228
## [3,] 0.001599462 0.014928166 0.009638877 0.036560666
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.15232809 0.05071668 0.35683442 0.41280060
## [2,] 0.32776965 0.24666290 0.37480358 0.47642539
## [3,] 0.01666237 0.15551400 0.10041289 0.38087033
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.64418
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.64418
einsum::einsum('ij->', arrC)
## [1] 6.222752
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 6.222752
einsum::einsum('ijk->', arrE)
## [1] 28.1427
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 28.1427
einsum::einsum('ij->i', arrC)
## [1] 1.983335 2.906984 1.332432
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 1.983335 2.906984 1.332432
einsum::einsum('ij->j', arrC)
## [1] 1.0129150 0.9234692 1.6965871 2.5897803
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 1.0129150 0.9234692 1.6965871 2.5897803
einsum::einsum('ijk->i', arrE)
## [1] 10.936769 6.370392 10.835541
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 10.936769 6.370392 10.835541
einsum::einsum('ijk->j', arrE)
## [1] 8.630359 6.945432 6.123686 6.443225
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 8.630359 6.945432 6.123686 6.443225
einsum::einsum('ijk->k', arrE)
## [1] 5.304607 6.394826 4.901859 5.942438 5.598972
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 5.304607 6.394826 4.901859 5.942438 5.598972
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 3.734025 2.490944 2.324957 2.386843
## [2,] 1.771730 1.712559 1.224073 1.662030
## [3,] 3.124604 2.741929 2.574656 2.394353
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 3.734025 2.490944 2.324957 2.386843
## [2,] 1.771730 1.712559 1.224073 1.662030
## [3,] 3.124604 2.741929 2.574656 2.394353
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.5331591 2.0587268 1.3632815 2.1431904 1.5320013
## [2,] 1.2391665 0.9967515 0.8869458 1.9165056 1.9060624
## [3,] 0.9543252 1.4527419 1.5278376 0.9530672 1.2357139
## [4,] 1.5779560 1.8866063 1.1237943 0.9296747 0.9251942
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.5331591 2.0587268 1.3632815 2.1431904 1.5320013
## [2,] 1.2391665 0.9967515 0.8869458 1.9165056 1.9060624
## [3,] 0.9543252 1.4527419 1.5278376 0.9530672 1.2357139
## [4,] 1.5779560 1.8866063 1.1237943 0.9296747 0.9251942
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.5331591 2.0587268 1.3632815 2.1431904 1.5320013
## [2,] 1.2391665 0.9967515 0.8869458 1.9165056 1.9060624
## [3,] 0.9543252 1.4527419 1.5278376 0.9530672 1.2357139
## [4,] 1.5779560 1.8866063 1.1237943 0.9296747 0.9251942
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.5331591 2.0587268 1.3632815 2.1431904 1.5320013
## [2,] 1.2391665 0.9967515 0.8869458 1.9165056 1.9060624
## [3,] 0.9543252 1.4527419 1.5278376 0.9530672 1.2357139
## [4,] 1.5779560 1.8866063 1.1237943 0.9296747 0.9251942
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.865818
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 1.865818
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.85397468 0.95859734 0.1981837
## [2,] 0.69112378 0.09949394 0.9164978
## [3,] 0.04778335 0.62711482 0.9123494
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.85397468 0.95859734 0.19818373
## [2,] 0.69112378 0.09949394 0.91649784
## [3,] 0.04778335 0.62711482 0.91234937
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.2340494 0.6673744 1.295591e-01
## [2,] 0.4082558 0.4769921 5.345861e-05
## [3,] 0.8088967 0.4566673 3.946848e-03
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.04344477 0.7975048 0.0840122
## [2,] 0.19006257 0.1983494 0.2937842
## [3,] 0.97387423 0.4273696 0.1994173
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.9342410 0.2932085 0.7334949
## [2,] 0.6660487 0.8622597 0.8973472
## [3,] 0.7279250 0.7793644 0.8957590
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 2.340494e-01 6.673744e-01 1.295591e-01
## [2,] 4.082558e-01 4.769921e-01 5.345861e-05
## [3,] 8.088967e-01 4.566673e-01 3.946848e-03
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.04344477 0.79750484 0.08401220
## [2,] 0.19006257 0.19834940 0.29378421
## [3,] 0.97387423 0.42736960 0.19941733
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.9342410 0.2932085 0.7334949
## [2,] 0.6660487 0.8622597 0.8973472
## [3,] 0.7279250 0.7793644 0.8957590
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.003101
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.003101
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 4.319232
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 4.319232
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 17.51993
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 17.51993
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.9764454 1.5467216 0.7049265 1.6105211 0.9849222
## [2,] 0.7715593 0.5860432 0.3015637 1.3077874 1.3763043
## [3,] 0.4188798 0.7959341 1.0334739 0.3257810 0.7958694
## [4,] 0.8624901 1.5920209 0.8013423 0.3343098 0.3930383
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9764454 1.5467216 0.7049265 1.6105211 0.9849222
## [2,] 0.7715593 0.5860432 0.3015637 1.3077874 1.3763043
## [3,] 0.4188798 0.7959341 1.0334739 0.3257810 0.7958694
## [4,] 0.8624901 1.5920209 0.8013423 0.3343098 0.3930383
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.3450605 1.633350 0.8460058
## [2,] 1.6333499 2.227420 1.0931092
## [3,] 0.8460058 1.093109 0.7467518
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.3450605 1.6333499 0.8460058
## [2,] 1.6333499 2.2274197 1.0931092
## [3,] 0.8460058 1.0931092 0.7467518
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.09647450 0.4466733 0.00115432
## [2,] 0.01069434 0.2529649 0.10055219
## [3,] 0.52940252 0.5840634 0.04192098
## [4,] 0.70848910 0.9437182 0.60312428
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.09647450 0.44667332 0.00115432
## [2,] 0.01069434 0.25296485 0.10055219
## [3,] 0.52940252 0.58406339 0.04192098
## [4,] 0.70848910 0.94371818 0.60312428
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.42451138 0.7303282 0.32621934 0.7916941 0.5882269
## [2,] 0.14581802 0.5260483 0.17254985 0.1903594 0.2831283
## [3,] 0.03394494 0.3524429 0.09718446 0.1515458 0.7157380
## [4,] 0.23767923 0.6516146 0.04825448 0.2349662 0.1503041
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.024040876 0.15074895 0.33212958 0.24768333 0.023848653
## [2,] 0.004803781 0.05920284 0.01998063 0.71369999 0.171207802
## [3,] 0.030547933 0.05595558 0.08662473 0.13644767 0.022211455
## [4,] 0.178212586 0.01361406 0.75171048 0.04372634 0.002216263
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.5278931 0.6656443748 0.046577613 0.57114367 0.37284666
## [2,] 0.6209375 0.0007920468 0.109033203 0.40372805 0.92196824
## [3,] 0.3543869 0.3875356268 0.849664719 0.03778749 0.05792002
## [4,] 0.4465983 0.9267922037 0.001377366 0.05561725 0.24051794
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.42451138 0.73032823 0.32621934 0.79169411 0.58822692
## [2,] 0.14581802 0.52604829 0.17254985 0.19035937 0.28312827
## [3,] 0.03394494 0.35244286 0.09718446 0.15154584 0.71573796
## [4,] 0.23767923 0.65161461 0.04825448 0.23496624 0.15030411
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.024040876 0.150748948 0.332129578 0.247683334 0.023848653
## [2,] 0.004803781 0.059202843 0.019980633 0.713699991 0.171207802
## [3,] 0.030547933 0.055955581 0.086624725 0.136447669 0.022211455
## [4,] 0.178212586 0.013614062 0.751710478 0.043726341 0.002216263
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.5278931141 0.6656443748 0.0465776128 0.5711436735 0.3728466556
## [2,] 0.6209375051 0.0007920468 0.1090332030 0.4037280526 0.9219682387
## [3,] 0.3543868926 0.3875356268 0.8496647187 0.0377874884 0.0579200239
## [4,] 0.4465982559 0.9267922037 0.0013773663 0.0556172506 0.2405179389
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 1.705172 0.8212925 2.778143
## [2,] 2.980780 0.9848090 2.429238
## [3,] 1.517961 1.8789929 1.504906
## [4,] 2.200096 1.9209823 1.821360
## [5,] 2.532761 0.7643150 2.301896
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.7051717 0.8212925 2.7781425
## [2,] 2.9807798 0.9848090 2.4292377
## [3,] 1.5179606 1.8789929 1.5049057
## [4,] 2.2000959 1.9209823 1.8213596
## [5,] 2.5327612 0.7643150 2.3018956
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.033083414 0.056916617 0.025423228 0.061699040 0.04584225
## [2,] 0.001259719 0.004544521 0.001490655 0.001644511 0.00244594
## [3,] 0.014516754 0.150724264 0.041561506 0.064809470 0.30608955
## [4,] 0.136029423 0.372934389 0.027617175 0.134476711 0.08602258
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0047037303 0.029494865 0.064982989 0.04846061 0.0046661209
## [2,] 0.0005322866 0.006560016 0.002213969 0.07908206 0.0189708093
## [3,] 0.0078152691 0.014315467 0.022161746 0.03490826 0.0056824957
## [4,] 0.0736686670 0.005627716 0.310738484 0.01807539 0.0009161484
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0002427337 0.0003060739 2.141713e-05 0.0002626210 0.0001714408
## [2,] 0.0248712291 0.0000317249 4.367251e-03 0.0161710524 0.0369288103
## [3,] 0.0059178909 0.0064714401 1.418851e-02 0.0006310116 0.0009672039
## [4,] 0.1072955397 0.2226624677 3.309132e-04 0.0133620829 0.0577846011
DelayedTensor::einsum('i,ij,ijk,ijk,ji->jki',
darrA, darrC, darrE, darrE, t(darrC))
## <4 x 5 x 3> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.033083414 0.056916617 0.025423228 0.061699040 0.045842245
## [2,] 0.001259719 0.004544521 0.001490655 0.001644511 0.002445940
## [3,] 0.014516754 0.150724264 0.041561506 0.064809470 0.306089551
## [4,] 0.136029423 0.372934389 0.027617175 0.134476711 0.086022582
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0047037303 0.0294948653 0.0649829887 0.0484606141 0.0046661209
## [2,] 0.0005322866 0.0065600155 0.0022139690 0.0790820641 0.0189708093
## [3,] 0.0078152691 0.0143154669 0.0221617460 0.0349082619 0.0056824957
## [4,] 0.0736686670 0.0056277157 0.3107384843 0.0180753856 0.0009161484
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.427337e-04 3.060739e-04 2.141713e-05 2.626210e-04 1.714408e-04
## [2,] 2.487123e-02 3.172490e-05 4.367251e-03 1.617105e-02 3.692881e-02
## [3,] 5.917891e-03 6.471440e-03 1.418851e-02 6.310116e-04 9.672039e-04
## [4,] 1.072955e-01 2.226625e-01 3.309132e-04 1.336208e-02 5.778460e-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.4.0 RC (2024-04-16 r86468)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 22.04.4 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.20-bioc/R/lib/libRblas.so
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_GB LC_COLLATE=C
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## time zone: America/New_York
## tzcode source: system (glibc)
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] einsum_0.1.2 DelayedRandomArray_1.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