DelayedTensor 1.13.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2024-10-23 23:57:56.972842
Compiled: Tue Oct 29 17:04:42 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.7358893 0.9480045 0.7557545
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.7358893 0.9480045 0.7557545
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.5434744 0.09238693 0.8108559 0.77306275
## [2,] 0.5053249 0.86481960 0.1714924 0.05467289
## [3,] 0.3119569 0.34162729 0.3345491 0.83837347
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.54347440 0.09238693 0.81085592 0.77306275
## [2,] 0.50532486 0.86481960 0.17149237 0.05467289
## [3,] 0.31195694 0.34162729 0.33454910 0.83837347
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1892480 0.4194989 0.2305368 0.7050893
## [2,] 0.1799024 0.1307024 0.1806976 0.9510029
## [3,] 0.7738620 0.6908746 0.5416806 0.7651585
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4732710 0.5379533 0.03577884 0.6127002
## [2,] 0.5886999 0.8602529 0.89041678 0.0871864
## [3,] 0.3828656 0.0284476 0.76320582 0.3331085
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1562328 0.3648658 0.9871752 0.6540371
## [2,] 0.8043943 0.7479002 0.6317191 0.4145194
## [3,] 0.6358459 0.9035905 0.8085191 0.8437670
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8433155 0.3197214 0.3594628 0.16848415
## [2,] 0.2677753 0.8294304 0.9185144 0.84605636
## [3,] 0.7645319 0.8005765 0.9197365 0.08345058
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04863352 0.2438971 0.7483452 0.3311905
## [2,] 0.58476506 0.5094344 0.1785714 0.8212335
## [3,] 0.49885071 0.2415952 0.9857845 0.1301080
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.1892480 0.4194989 0.2305368 0.7050893
## [2,] 0.1799024 0.1307024 0.1806976 0.9510029
## [3,] 0.7738620 0.6908746 0.5416806 0.7651585
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.47327100 0.53795332 0.03577884 0.61270021
## [2,] 0.58869989 0.86025294 0.89041678 0.08718640
## [3,] 0.38286562 0.02844760 0.76320582 0.33310847
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.1562328 0.3648658 0.9871752 0.6540371
## [2,] 0.8043943 0.7479002 0.6317191 0.4145194
## [3,] 0.6358459 0.9035905 0.8085191 0.8437670
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.84331547 0.31972138 0.35946283 0.16848415
## [2,] 0.26777526 0.82943042 0.91851440 0.84605636
## [3,] 0.76453195 0.80057648 0.91973647 0.08345058
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.04863352 0.24389713 0.74834515 0.33119045
## [2,] 0.58476506 0.50943436 0.17857143 0.82123352
## [3,] 0.49885071 0.24159522 0.98578449 0.13010801
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.5855002 0.6205705 0.1666936
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.5855002 0.6205705 0.1666936
einsum::einsum('iii->i', arrD)
## [1] 0.9492409 0.5780297 0.1325988
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.9492409 0.5780297 0.1325988
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.5415331 0.8987125 0.5711648
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.5415331 0.8987125 0.5711648
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.29536442 0.008535344 0.65748733 0.597626014
## [2,] 0.25535321 0.747912939 0.02940963 0.002989125
## [3,] 0.09731713 0.116709205 0.11192310 0.702870070
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.295364422 0.008535344 0.657487329 0.597626014
## [2,] 0.255353210 0.747912939 0.029409632 0.002989125
## [3,] 0.097317133 0.116709205 0.111923097 0.702870070
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03581480 0.17597935 0.05314723 0.4971509
## [2,] 0.03236487 0.01708312 0.03265163 0.9044065
## [3,] 0.59886236 0.47730775 0.29341788 0.5854675
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2239854 0.2893937755 0.001280125 0.375401552
## [2,] 0.3465676 0.7400351165 0.792842033 0.007601469
## [3,] 0.1465861 0.0008092658 0.582483117 0.110961255
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02440869 0.1331270 0.9745150 0.4277645
## [2,] 0.64705014 0.5593547 0.3990690 0.1718263
## [3,] 0.40429995 0.8164758 0.6537032 0.7119427
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.71118098 0.1022218 0.1292135 0.028386908
## [2,] 0.07170359 0.6879548 0.8436687 0.715811367
## [3,] 0.58450910 0.6409227 0.8459152 0.006963999
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.002365219 0.05948581 0.56002047 0.10968712
## [2,] 0.341950179 0.25952336 0.03188776 0.67442450
## [3,] 0.248852031 0.05836825 0.97177106 0.01692809
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.03581480 0.17597935 0.05314723 0.49715095
## [2,] 0.03236487 0.01708312 0.03265163 0.90440649
## [3,] 0.59886236 0.47730775 0.29341788 0.58546754
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.2239854372 0.2893937755 0.0012801255 0.3754015524
## [2,] 0.3465675652 0.7400351165 0.7928420334 0.0076014686
## [3,] 0.1465860832 0.0008092658 0.5824831173 0.1109612552
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.02440869 0.13312705 0.97451496 0.42776453
## [2,] 0.64705014 0.55935475 0.39906898 0.17182631
## [3,] 0.40429995 0.81647578 0.65370316 0.71194272
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.711180977 0.102221759 0.129213525 0.028386908
## [2,] 0.071703589 0.687954823 0.843668706 0.715811367
## [3,] 0.584509095 0.640922699 0.845915174 0.006963999
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.002365219 0.059485808 0.560020469 0.109687117
## [2,] 0.341950179 0.259523365 0.031887756 0.674424502
## [3,] 0.248852031 0.058368251 0.971771065 0.016928093
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.5415331 0.6976264 0.5561517
## [2,] 0.6976264 0.8987125 0.7164586
## [3,] 0.5561517 0.7164586 0.5711648
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.5415331 0.6976264 0.5561517
## [2,] 0.6976264 0.8987125 0.7164586
## [3,] 0.5561517 0.7164586 0.5711648
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10285144 0.01748404 0.15345286 0.14630058
## [2,] 0.09563172 0.16366538 0.03245459 0.01034673
## [3,] 0.05903723 0.06465228 0.06331275 0.15866050
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09777234 0.01662063 0.14587492 0.139075835
## [2,] 0.09090915 0.15558311 0.03085189 0.009835783
## [3,] 0.05612180 0.06145957 0.06018618 0.150825389
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4205742 0.07149473 0.6274906 0.59824387
## [2,] 0.3910517 0.66925101 0.1327114 0.04230927
## [3,] 0.2414116 0.26437237 0.2588948 0.64878535
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2279869 0.03875622 0.34015319 0.32429899
## [2,] 0.2119832 0.36279089 0.07194086 0.02293522
## [3,] 0.1308656 0.14331228 0.14034299 0.35169677
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07103341 0.01207519 0.10598082 0.101041159
## [2,] 0.06604717 0.11303400 0.02241446 0.007145878
## [3,] 0.04077352 0.04465151 0.04372637 0.109577427
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3754727 0.06382778 0.5601998 0.53408944
## [2,] 0.3491161 0.59748192 0.1184797 0.03777211
## [3,] 0.2155231 0.23602163 0.2311315 0.57921096
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12529086 0.02129859 0.18693215 0.17821943
## [2,] 0.11649599 0.19937277 0.03953531 0.01260411
## [3,] 0.07191756 0.07875767 0.07712589 0.19327596
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09820454 0.01669410 0.14651975 0.139690609
## [2,] 0.09131101 0.15627086 0.03098826 0.009879261
## [3,] 0.05636988 0.06173124 0.06045223 0.151492102
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2943895 0.05004421 0.43922493 0.41875310
## [2,] 0.2737247 0.46845601 0.09289409 0.02961524
## [3,] 0.1689810 0.18505288 0.18121876 0.45413065
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3831980 0.06514104 0.5717259 0.54507829
## [2,] 0.3562992 0.60977506 0.1209174 0.03854927
## [3,] 0.2199575 0.24087775 0.2358870 0.59112818
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5168457 0.08786023 0.7711263 0.73518491
## [2,] 0.4805654 0.82244593 0.1630897 0.05199407
## [3,] 0.2966720 0.32488854 0.3181572 0.79729559
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4158441 0.07069064 0.6204333 0.59151554
## [2,] 0.3866536 0.66172407 0.1312188 0.04183342
## [3,] 0.2386965 0.26139903 0.2559831 0.64148859
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2572107 0.04372405 0.38375459 0.36586818
## [2,] 0.2391556 0.40929403 0.08116236 0.02587509
## [3,] 0.1476402 0.16168229 0.15833238 0.39677785
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3199433 0.05438817 0.4773508 0.45510196
## [2,] 0.2974847 0.50911921 0.1009575 0.03218592
## [3,] 0.1836490 0.20111595 0.1969490 0.49355037
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2080777 0.03537178 0.31044886 0.29597915
## [2,] 0.1934715 0.33110969 0.06565853 0.02093237
## [3,] 0.1194376 0.13079734 0.12808735 0.32098438
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2923639 0.04969985 0.43620264 0.41587167
## [2,] 0.2718412 0.46523258 0.09225489 0.02941146
## [3,] 0.1678183 0.18377954 0.17997180 0.45100579
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4675254 0.07947613 0.6975412 0.66502950
## [2,] 0.4347072 0.74396360 0.1475268 0.04703251
## [3,] 0.2683619 0.29388588 0.2877968 0.72121324
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.015460541 0.002628186 0.023066903 0.021991778
## [2,] 0.014375278 0.024602040 0.004878546 0.001555312
## [3,] 0.008874425 0.009718476 0.009517118 0.023849711
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01944488 0.003305497 0.029011485 0.027659289
## [2,] 0.01807994 0.030942243 0.006135798 0.001956133
## [3,] 0.01116146 0.012223029 0.011969779 0.029996031
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4839187 0.08226287 0.7219997 0.68834804
## [2,] 0.4499497 0.77004988 0.1526997 0.04868166
## [3,] 0.2777717 0.30419067 0.2978881 0.74650180
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4147828 0.07051024 0.6188500 0.59000599
## [2,] 0.3856669 0.66003535 0.1308840 0.04172667
## [3,] 0.2380874 0.26073193 0.2553298 0.63985151
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3329869 0.05660549 0.4968116 0.47365571
## [2,] 0.3096126 0.52987515 0.1050734 0.03349809
## [3,] 0.1911361 0.20931511 0.2049783 0.51367160
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04738358 0.008054884 0.07069561 0.067400559
## [2,] 0.04405746 0.075400509 0.01495180 0.004766732
## [3,] 0.02719840 0.029785254 0.02916813 0.073094766
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1810359 0.03077487 0.27010298 0.2575138
## [2,] 0.1683280 0.28807874 0.05712556 0.0182120
## [3,] 0.1039155 0.11379895 0.11144114 0.2792693
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08490853 0.01443387 0.12668230 0.120777768
## [2,] 0.07894832 0.13511320 0.02679273 0.008541699
## [3,] 0.04873791 0.05337339 0.05226755 0.130981445
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4371677 0.07431552 0.6522479 0.62184725
## [2,] 0.4064804 0.69565593 0.1379475 0.04397856
## [3,] 0.2509364 0.27480304 0.2691094 0.67438281
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3455659 0.05874385 0.5155794 0.49154875
## [2,] 0.3213087 0.54989196 0.1090427 0.03476353
## [3,] 0.1983565 0.21722230 0.2127217 0.53307630
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1982952 0.03370883 0.2958536 0.28206416
## [2,] 0.1843758 0.31554309 0.0625717 0.01994827
## [3,] 0.1138224 0.12464811 0.1220655 0.30589380
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4064646 0.0690962 0.6064393 0.57817380
## [2,] 0.3779326 0.6467988 0.1282592 0.04088986
## [3,] 0.2333127 0.2555031 0.2502093 0.62701971
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4910783 0.08347995 0.7326817 0.6985322
## [2,] 0.4566067 0.78144277 0.1549589 0.0494019
## [3,] 0.2818813 0.30869117 0.3022954 0.7575463
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5365045 0.09120209 0.8004569 0.76314841
## [2,] 0.4988442 0.85372850 0.1692930 0.05397172
## [3,] 0.3079562 0.33724600 0.3302586 0.82762153
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3433231 0.05836258 0.5122331 0.4883585
## [2,] 0.3192233 0.54632303 0.1083350 0.0345379
## [3,] 0.1970691 0.21581247 0.2113410 0.5296165
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4394094 0.0746966 0.6555925 0.62503601
## [2,] 0.4085648 0.6992232 0.1386549 0.04420407
## [3,] 0.2522231 0.2762122 0.2704893 0.67784097
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3554524 0.06042448 0.5303299 0.5056117
## [2,] 0.3305012 0.56562411 0.1121624 0.0357581
## [3,] 0.2040314 0.22343692 0.2188075 0.5483274
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2252807 0.03829617 0.33611548 0.32044948
## [2,] 0.2094669 0.35848447 0.07108691 0.02266297
## [3,] 0.1293122 0.14161113 0.13867708 0.34752204
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4585658 0.07795304 0.6841735 0.65228482
## [2,] 0.4263764 0.72970622 0.1446996 0.04613118
## [3,] 0.2632190 0.28825383 0.2822815 0.70739185
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4583204 0.07791133 0.6838073 0.65193577
## [2,] 0.4261483 0.72931574 0.1446222 0.04610649
## [3,] 0.2630781 0.28809958 0.2821304 0.70701331
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.14552900 0.02473893 0.21712715 0.20700708
## [2,] 0.13531349 0.23157729 0.04592141 0.01464005
## [3,] 0.08353435 0.09147934 0.08958397 0.22449567
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4155035 0.07063276 0.6199253 0.59103117
## [2,] 0.3863370 0.66118221 0.1311114 0.04179917
## [3,] 0.2385010 0.26118498 0.2557735 0.64096330
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1737604 0.02953808 0.25924797 0.24716469
## [2,] 0.1615632 0.27650131 0.05482978 0.01748009
## [3,] 0.0997393 0.10922555 0.10696250 0.26804592
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4507742 0.07662853 0.6725486 0.64120176
## [2,] 0.4191318 0.71730768 0.1422410 0.04534736
## [3,] 0.2587466 0.28335607 0.2774852 0.69537246
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4350928 0.0739628 0.6491522 0.61889585
## [2,] 0.4045512 0.6923542 0.1372928 0.04376983
## [3,] 0.2497454 0.2734988 0.2678321 0.67118208
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1953588 0.03320967 0.29147256 0.27788732
## [2,] 0.1816455 0.31087050 0.06164513 0.01965287
## [3,] 0.1121369 0.12280231 0.12025796 0.30136410
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4991891 0.08485872 0.7447828 0.71006927
## [2,] 0.4641482 0.79434926 0.1575182 0.05021783
## [3,] 0.2865369 0.31378959 0.3072882 0.77005810
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4998532 0.08497163 0.7457738 0.71101400
## [2,] 0.4647657 0.79540612 0.1577278 0.05028465
## [3,] 0.2869182 0.31420708 0.3076970 0.77108265
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09156682 0.01556573 0.13661637 0.130248819
## [2,] 0.08513923 0.14570839 0.02889375 0.009211515
## [3,] 0.05255980 0.05755878 0.05636622 0.141252639
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4598100 0.07816455 0.6860298 0.65405466
## [2,] 0.4275333 0.73168612 0.1450922 0.04625634
## [3,] 0.2639332 0.28903594 0.2830474 0.70931121
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04535325 0.007709742 0.06766639 0.064512532
## [2,] 0.04216965 0.072169694 0.01431114 0.004562484
## [3,] 0.02603299 0.028508994 0.02791831 0.069962749
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02643107 0.004493101 0.039434778 0.037596763
## [2,] 0.02457573 0.042059221 0.008340277 0.002658935
## [3,] 0.01517156 0.016614538 0.016270300 0.040773053
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3178048 0.05402465 0.4741602 0.45206009
## [2,] 0.2954963 0.50571629 0.1002827 0.03197079
## [3,] 0.1824215 0.19977170 0.1956326 0.49025151
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2711126 0.04608728 0.40449605 0.38564290
## [2,] 0.2520817 0.43141587 0.08554909 0.02727361
## [3,] 0.1556199 0.17042102 0.16689005 0.41822320
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1325518 0.02253291 0.19776543 0.18854778
## [2,] 0.1232473 0.21092701 0.04182650 0.01333456
## [3,] 0.0760854 0.08332191 0.08159556 0.20447688
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2768645 0.04706508 0.4130779 0.39382472
## [2,] 0.2574298 0.44056882 0.0873641 0.02785225
## [3,] 0.1589216 0.17403668 0.1704308 0.42709625
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13130082 0.02232024 0.19589892 0.18676827
## [2,] 0.12208407 0.20893628 0.04143174 0.01320871
## [3,] 0.07536731 0.08253552 0.08082546 0.20254702
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4067064 0.06913731 0.6068001 0.57851776
## [2,] 0.3781574 0.64718356 0.1283355 0.04091419
## [3,] 0.2334515 0.25565513 0.2503582 0.62739272
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09704900 0.01649767 0.14479570 0.138046922
## [2,] 0.09023658 0.15443207 0.03062364 0.009763016
## [3,] 0.05570660 0.06100487 0.05974091 0.149709550
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5357486 0.0910736 0.7993292 0.76207327
## [2,] 0.4981414 0.8525257 0.1690545 0.05389568
## [3,] 0.3075223 0.3367709 0.3297933 0.82645556
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1799935 0.03059767 0.26854774 0.25603100
## [2,] 0.1673588 0.28642000 0.05679663 0.01810714
## [3,] 0.1033172 0.11314370 0.11079947 0.27766129
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4463194 0.07587124 0.6659021 0.63486505
## [2,] 0.4149897 0.71021885 0.1408353 0.04489921
## [3,] 0.2561895 0.28055578 0.2747429 0.68850040
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07071037 0.01202028 0.10549885 0.10058165
## [2,] 0.06574681 0.11251995 0.02231253 0.00711338
## [3,] 0.04058810 0.04444845 0.04352752 0.10907910
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.10285144 0.01748404 0.15345286 0.14630058
## [2,] 0.09563172 0.16366538 0.03245459 0.01034673
## [3,] 0.05903723 0.06465228 0.06331275 0.15866050
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.097772343 0.016620629 0.145874918 0.139075835
## [2,] 0.090909149 0.155583112 0.030851886 0.009835783
## [3,] 0.056121799 0.061459566 0.060186181 0.150825389
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.42057417 0.07149473 0.62749057 0.59824387
## [2,] 0.39105169 0.66925101 0.13271142 0.04230927
## [3,] 0.24141162 0.26437237 0.25889483 0.64878535
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.17999353 0.03059767 0.26854774 0.25603100
## [2,] 0.16735877 0.28642000 0.05679663 0.01810714
## [3,] 0.10331716 0.11314370 0.11079947 0.27766129
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.44631940 0.07587124 0.66590207 0.63486505
## [2,] 0.41498971 0.71021885 0.14083528 0.04489921
## [3,] 0.25618950 0.28055578 0.27474293 0.68850040
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.07071037 0.01202028 0.10549885 0.10058165
## [2,] 0.06574681 0.11251995 0.02231253 0.00711338
## [3,] 0.04058810 0.04444845 0.04352752 0.10907910
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] 2.439648
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 2.439648
einsum::einsum('ij->', arrC)
## [1] 5.642597
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 5.642597
einsum::einsum('ijk->', arrE)
## [1] 31.74817
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 31.74817
einsum::einsum('ij->i', arrC)
## [1] 2.219780 1.596310 1.826507
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 2.219780 1.596310 1.826507
einsum::einsum('ij->j', arrC)
## [1] 1.360756 1.298834 1.316897 1.666109
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 1.360756 1.298834 1.316897 1.666109
einsum::einsum('ijk->i', arrE)
## [1] 8.429437 11.423175 11.895560
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 8.429437 11.423175 11.895560
einsum::einsum('ijk->j', arrE)
## [1] 7.192194 7.628741 9.180145 7.747092
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 7.192194 7.628741 9.180145 7.747092
einsum::einsum('ijk->k', arrE)
## [1] 5.758254 5.593887 7.952566 7.121056 5.322409
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 5.758254 5.593887 7.952566 7.121056 5.322409
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 1.710701 1.885937 2.361299 2.471501
## [2,] 2.425537 3.077720 2.799919 3.119999
## [3,] 3.055956 2.665084 4.018927 2.155593
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 1.710701 1.885937 2.361299 2.471501
## [2,] 2.425537 3.077720 2.799919 3.119999
## [3,] 3.055956 2.665084 4.018927 2.155593
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.1430124 1.444837 1.596473 1.875623 1.1322493
## [2,] 1.2410760 1.426654 2.016357 1.949728 0.9949267
## [3,] 0.9529151 1.689401 2.427413 2.197714 1.9127011
## [4,] 2.4212507 1.032995 1.912323 1.097991 1.2825320
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.1430124 1.4448365 1.5964729 1.8756227 1.1322493
## [2,] 1.2410760 1.4266539 2.0163565 1.9497283 0.9949267
## [3,] 0.9529151 1.6894014 2.4274134 2.1977137 1.9127011
## [4,] 2.4212507 1.0329951 1.9123235 1.0979911 1.2825320
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.1430124 1.444837 1.596473 1.875623 1.1322493
## [2,] 1.2410760 1.426654 2.016357 1.949728 0.9949267
## [3,] 0.9529151 1.689401 2.427413 2.197714 1.9127011
## [4,] 2.4212507 1.032995 1.912323 1.097991 1.2825320
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.1430124 1.4448365 1.5964729 1.8756227 1.1322493
## [2,] 1.2410760 1.4266539 2.0163565 1.9497283 0.9949267
## [3,] 0.9529151 1.6894014 2.4274134 2.1977137 1.9127011
## [4,] 2.4212507 1.0329951 1.9123235 1.0979911 1.2825320
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.372764
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 1.372764
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.5855002 0.05760985 0.9849034
## [2,] 0.2204578 0.62057051 0.8571587
## [3,] 0.6624133 0.21990712 0.1666936
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.58550017 0.05760985 0.98490337
## [2,] 0.22045779 0.62057051 0.85715867
## [3,] 0.66241330 0.21990712 0.16669358
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.9492409 0.3935521 0.9717702
## [2,] 0.8776574 0.9366490 0.6482676
## [3,] 0.1620806 0.5789647 0.7783804
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.9890423 0.6741274 0.1018531
## [2,] 0.5640216 0.5780297 0.3090479
## [3,] 0.9405024 0.3317194 0.6723549
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.1372259 0.09916504 0.9404910
## [2,] 0.9580944 0.12663580 0.1488957
## [3,] 0.8065342 0.80525894 0.1325988
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.9492409 0.3935521 0.9717702
## [2,] 0.8776574 0.9366490 0.6482676
## [3,] 0.1620806 0.5789647 0.7783804
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.9890423 0.6741274 0.1018531
## [2,] 0.5640216 0.5780297 0.3090479
## [3,] 0.9405024 0.3317194 0.6723549
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.13722592 0.09916504 0.94049100
## [2,] 0.95809439 0.12663580 0.14889568
## [3,] 0.80653420 0.80525894 0.13259878
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] 2.01141
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 2.01141
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 3.623498
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 3.623498
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 21.94885
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 21.94885
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.6670420 0.7171391 1.075759 1.3673937 0.5931674
## [2,] 0.6703702 1.0302382 1.508958 1.4310993 0.3773774
## [3,] 0.3792167 1.3766053 2.027287 1.8187974 1.5636793
## [4,] 1.9870250 0.4939643 1.311534 0.7511623 0.8010397
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.6670420 0.7171391 1.0757588 1.3673937 0.5931674
## [2,] 0.6703702 1.0302382 1.5089576 1.4310993 0.3773774
## [3,] 0.3792167 1.3766053 2.0272871 1.8187974 1.5636793
## [4,] 1.9870250 0.4939643 1.3115336 0.7511623 0.8010397
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.5590131 0.5358503 1.1204889
## [2,] 0.5358503 1.0356649 0.5562945
## [3,] 1.1204889 0.5562945 1.0288195
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.5590131 0.5358503 1.1204889
## [2,] 0.5358503 1.0356649 0.5562945
## [3,] 1.1204889 0.5562945 1.0288195
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.295364422 0.255353210 0.09731713
## [2,] 0.008535344 0.747912939 0.11670921
## [3,] 0.657487329 0.029409632 0.11192310
## [4,] 0.597626014 0.002989125 0.70287007
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.295364422 0.255353210 0.097317133
## [2,] 0.008535344 0.747912939 0.116709205
## [3,] 0.657487329 0.029409632 0.111923097
## [4,] 0.597626014 0.002989125 0.702870070
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.03581480 0.223985437 0.02440869 0.71118098 0.002365219
## [2,] 0.17597935 0.289393775 0.13312705 0.10222176 0.059485808
## [3,] 0.05314723 0.001280125 0.97451496 0.12921352 0.560020469
## [4,] 0.49715095 0.375401552 0.42776453 0.02838691 0.109687117
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.03236487 0.346567565 0.6470501 0.07170359 0.34195018
## [2,] 0.01708312 0.740035117 0.5593547 0.68795482 0.25952336
## [3,] 0.03265163 0.792842033 0.3990690 0.84366871 0.03188776
## [4,] 0.90440649 0.007601469 0.1718263 0.71581137 0.67442450
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.5988624 0.1465860832 0.4043000 0.584509095 0.24885203
## [2,] 0.4773078 0.0008092658 0.8164758 0.640922699 0.05836825
## [3,] 0.2934179 0.5824831173 0.6537032 0.845915174 0.97177106
## [4,] 0.5854675 0.1109612552 0.7119427 0.006963999 0.01692809
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.035814805 0.223985437 0.024408692 0.711180977 0.002365219
## [2,] 0.175979350 0.289393775 0.133127049 0.102221759 0.059485808
## [3,] 0.053147228 0.001280125 0.974514962 0.129213525 0.560020469
## [4,] 0.497150948 0.375401552 0.427764534 0.028386908 0.109687117
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.032364869 0.346567565 0.647050144 0.071703589 0.341950179
## [2,] 0.017083118 0.740035117 0.559354747 0.687954823 0.259523365
## [3,] 0.032651635 0.792842033 0.399068975 0.843668706 0.031887756
## [4,] 0.904406490 0.007601469 0.171826306 0.715811367 0.674424502
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.5988623638 0.1465860832 0.4042999529 0.5845090955 0.2488520309
## [2,] 0.4773077507 0.0008092658 0.8164757810 0.6409226994 0.0583682511
## [3,] 0.2934178841 0.5824831173 0.6537031566 0.8459151736 0.9717710646
## [4,] 0.5854675366 0.1109612552 0.7119427171 0.0069639988 0.0169280934
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 1.544373 1.442305 2.771576
## [2,] 1.659703 2.426556 1.507628
## [3,] 2.162311 2.598533 3.191722
## [4,] 1.690984 2.861776 2.568295
## [5,] 1.372066 2.094004 1.856338
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.544373 1.442305 2.771576
## [2,] 1.659703 2.426556 1.507628
## [3,] 2.162311 2.598533 3.191722
## [4,] 1.690984 2.861776 2.568295
## [5,] 1.372066 2.094004 1.856338
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.007784546 0.0486844727 0.0053053641 0.1545791160 0.0005140935
## [2,] 0.001105338 0.0018177022 0.0008361802 0.0006420619 0.0003736345
## [3,] 0.025714644 0.0006193732 0.4715072821 0.0625184018 0.2709591330
## [4,] 0.218640329 0.1650965749 0.1881251130 0.0124841820 0.0482389248
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0078347577 8.389568e-02 0.1566353010 0.017357717 0.0827779265
## [2,] 0.0121123546 5.247033e-01 0.3965963930 0.487777037 0.1840085041
## [3,] 0.0009103427 2.210480e-02 0.0111262275 0.023521873 0.0008890454
## [4,] 0.0025628198 2.154031e-05 0.0004869048 0.002028397 0.0019111190
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.04404505 1.078109e-02 0.02973540 0.042989393 0.018302534
## [2,] 0.04210022 7.138008e-05 0.07201603 0.056531629 0.005148284
## [3,] 0.02481916 4.927014e-02 0.05529439 0.071552905 0.082198599
## [4,] 0.31099872 5.894231e-02 0.37818198 0.003699257 0.008992156
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.0077845458 0.0486844727 0.0053053641 0.1545791160 0.0005140935
## [2,] 0.0011053384 0.0018177022 0.0008361802 0.0006420619 0.0003736345
## [3,] 0.0257146439 0.0006193732 0.4715072821 0.0625184018 0.2709591330
## [4,] 0.2186403287 0.1650965749 0.1881251130 0.0124841820 0.0482389248
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 7.834758e-03 8.389568e-02 1.566353e-01 1.735772e-02 8.277793e-02
## [2,] 1.211235e-02 5.247033e-01 3.965964e-01 4.877770e-01 1.840085e-01
## [3,] 9.103427e-04 2.210480e-02 1.112623e-02 2.352187e-02 8.890454e-04
## [4,] 2.562820e-03 2.154031e-05 4.869048e-04 2.028397e-03 1.911119e-03
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 4.404505e-02 1.078109e-02 2.973540e-02 4.298939e-02 1.830253e-02
## [2,] 4.210022e-02 7.138008e-05 7.201603e-02 5.653163e-02 5.148284e-03
## [3,] 2.481916e-02 4.927014e-02 5.529439e-02 7.155290e-02 8.219860e-02
## [4,] 3.109987e-01 5.894231e-02 3.781820e-01 3.699257e-03 8.992156e-03
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 Under development (unstable) (2024-10-21 r87258)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.1 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.21-bioc/R/lib/libRblas.so
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_GB LC_COLLATE=C
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## time zone: America/New_York
## tzcode source: system (glibc)
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] einsum_0.1.2 DelayedRandomArray_1.15.0
## [3] HDF5Array_1.35.0 rhdf5_2.51.0
## [5] DelayedArray_0.33.0 SparseArray_1.7.0
## [7] S4Arrays_1.7.0 abind_1.4-8
## [9] IRanges_2.41.0 S4Vectors_0.45.0
## [11] MatrixGenerics_1.19.0 matrixStats_1.4.1
## [13] BiocGenerics_0.53.0 Matrix_1.7-1
## [15] DelayedTensor_1.13.0 BiocStyle_2.35.0
##
## loaded via a namespace (and not attached):
## [1] jsonlite_1.8.9 compiler_4.5.0 BiocManager_1.30.25
## [4] crayon_1.5.3 rsvd_1.0.5 Rcpp_1.0.13
## [7] rhdf5filters_1.19.0 parallel_4.5.0 jquerylib_0.1.4
## [10] BiocParallel_1.41.0 yaml_2.3.10 fastmap_1.2.0
## [13] lattice_0.22-6 R6_2.5.1 XVector_0.47.0
## [16] ScaledMatrix_1.15.0 knitr_1.48 bookdown_0.41
## [19] bslib_0.8.0 rlang_1.1.4 cachem_1.1.0
## [22] xfun_0.48 sass_0.4.9 cli_3.6.3
## [25] Rhdf5lib_1.29.0 BiocSingular_1.23.0 zlibbioc_1.53.0
## [28] digest_0.6.37 grid_4.5.0 irlba_2.3.5.1
## [31] rTensor_1.4.8 dqrng_0.4.1 lifecycle_1.0.4
## [34] evaluate_1.0.1 codetools_0.2-20 beachmat_2.23.0
## [37] rmarkdown_2.28 tools_4.5.0 htmltools_0.5.8.1