DelayedTensor 1.10.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2024-04-30 20:49:30.349855
Compiled: Wed May 1 18:23:52 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.7791883 0.6868118 0.6513472
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.7791883 0.6868118 0.6513472
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.1715631 0.7604450 0.8077907 0.1365376
## [2,] 0.2141553 0.2391859 0.1728589 0.4107098
## [3,] 0.1365237 0.6984396 0.2917539 0.9111538
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.1715631 0.7604450 0.8077907 0.1365376
## [2,] 0.2141553 0.2391859 0.1728589 0.4107098
## [3,] 0.1365237 0.6984396 0.2917539 0.9111538
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5630944 0.41805066 0.3019211 0.55215112
## [2,] 0.3862543 0.59877640 0.9551635 0.05379869
## [3,] 0.9763139 0.03822411 0.5047466 0.11716654
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4433162 0.1324757 0.2316757 0.8628285
## [2,] 0.3231706 0.7145718 0.2880868 0.1308649
## [3,] 0.3355295 0.7141035 0.2930746 0.8570112
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9874821 0.5044233 0.05413075 0.1195343
## [2,] 0.1374017 0.7471073 0.33472503 0.9728576
## [3,] 0.7301125 0.6076680 0.78068757 0.9616515
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.18244107 0.86190047 0.8102313 0.2871041
## [2,] 0.76197565 0.86165005 0.3943631 0.6588369
## [3,] 0.04716608 0.02484251 0.4595001 0.7521981
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2153452 0.7264889 0.76738801 0.06282703
## [2,] 0.6023786 0.8149582 0.02387128 0.57401981
## [3,] 0.6883800 0.4722455 0.18603120 0.32671731
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.56309443 0.41805066 0.30192111 0.55215112
## [2,] 0.38625430 0.59877640 0.95516349 0.05379869
## [3,] 0.97631392 0.03822411 0.50474659 0.11716654
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.4433162 0.1324757 0.2316757 0.8628285
## [2,] 0.3231706 0.7145718 0.2880868 0.1308649
## [3,] 0.3355295 0.7141035 0.2930746 0.8570112
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.98748206 0.50442332 0.05413075 0.11953434
## [2,] 0.13740172 0.74710729 0.33472503 0.97285762
## [3,] 0.73011251 0.60766801 0.78068757 0.96165154
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.18244107 0.86190047 0.81023130 0.28710414
## [2,] 0.76197565 0.86165005 0.39436308 0.65883686
## [3,] 0.04716608 0.02484251 0.45950015 0.75219813
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.21534523 0.72648893 0.76738801 0.06282703
## [2,] 0.60237863 0.81495822 0.02387128 0.57401981
## [3,] 0.68837999 0.47224550 0.18603120 0.32671731
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.3786758 0.8674260 0.4159971
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.3786758 0.8674260 0.4159971
einsum::einsum('iii->i', arrD)
## [1] 0.5906993 0.5434373 0.2278085
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.5906993 0.5434373 0.2278085
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.6071344 0.4717105 0.4242531
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.6071344 0.4717105 0.4242531
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.02943390 0.57827658 0.65252579 0.01864253
## [2,] 0.04586249 0.05720991 0.02988021 0.16868251
## [3,] 0.01863873 0.48781794 0.08512036 0.83020121
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.02943390 0.57827658 0.65252579 0.01864253
## [2,] 0.04586249 0.05720991 0.02988021 0.16868251
## [3,] 0.01863873 0.48781794 0.08512036 0.83020121
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3170753 0.174766354 0.09115636 0.3048709
## [2,] 0.1491924 0.358533182 0.91233729 0.0028943
## [3,] 0.9531889 0.001461083 0.25476912 0.0137280
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1965292 0.0175498 0.05367364 0.74447309
## [2,] 0.1044393 0.5106129 0.08299402 0.01712562
## [3,] 0.1125800 0.5099438 0.08589274 0.73446811
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.97512082 0.2544429 0.002930138 0.01428846
## [2,] 0.01887923 0.5581693 0.112040846 0.94645196
## [3,] 0.53306427 0.3692604 0.609473075 0.92477369
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.033284742 0.7428724153 0.6564748 0.08242879
## [2,] 0.580606894 0.7424408005 0.1555222 0.43406600
## [3,] 0.002224639 0.0006171505 0.2111404 0.56580203
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04637357 0.5277862 0.5888843589 0.003947235
## [2,] 0.36286001 0.6641569 0.0005698382 0.329498740
## [3,] 0.47386701 0.2230158 0.0346076080 0.106744198
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.317075336 0.174766354 0.091156355 0.304870864
## [2,] 0.149192385 0.358533182 0.912337294 0.002894300
## [3,] 0.953188864 0.001461083 0.254769117 0.013727999
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.19652921 0.01754980 0.05367364 0.74447309
## [2,] 0.10443926 0.51061286 0.08299402 0.01712562
## [3,] 0.11258002 0.50994379 0.08589274 0.73446811
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.975120817 0.254442890 0.002930138 0.014288458
## [2,] 0.018879232 0.558169298 0.112040846 0.946451956
## [3,] 0.533064274 0.369260405 0.609473075 0.924773694
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.0332847425 0.7428724153 0.6564747634 0.0824287864
## [2,] 0.5806068936 0.7424408005 0.1555222388 0.4340660050
## [3,] 0.0022246390 0.0006171505 0.2111403847 0.5658020322
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.0463735694 0.5277861596 0.5888843589 0.0039472353
## [2,] 0.3628600117 0.6641568945 0.0005698382 0.3294987401
## [3,] 0.4738670093 0.2230158129 0.0346076080 0.1067441975
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.6071344 0.5351557 0.5075221
## [2,] 0.5351557 0.4717105 0.4473529
## [3,] 0.5075221 0.4473529 0.4242531
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.6071344 0.5351557 0.5075221
## [2,] 0.5351557 0.4717105 0.4473529
## [3,] 0.5075221 0.4473529 0.4242531
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09660623 0.4282023 0.4548624 0.07688358
## [2,] 0.12058966 0.1346843 0.0973359 0.23126838
## [3,] 0.07687576 0.3932875 0.1642850 0.51306562
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06626699 0.29372515 0.31201263 0.05273825
## [2,] 0.08271841 0.09238659 0.06676751 0.15863841
## [3,] 0.05273288 0.26977532 0.11269121 0.35193707
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1674995 0.7424330 0.7886573 0.1333036
## [2,] 0.2090828 0.2335206 0.1687646 0.4009817
## [3,] 0.1332900 0.6818963 0.2848434 0.8895721
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07172207 0.31790453 0.33769743 0.05707965
## [2,] 0.08952777 0.09999184 0.07226379 0.17169749
## [3,] 0.05707384 0.29198316 0.12196793 0.38090844
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1027279 0.4553365 0.4836860 0.08175551
## [2,] 0.1282311 0.1432189 0.1035038 0.24592332
## [3,] 0.0817472 0.4182092 0.1746954 0.54557738
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.006557847 0.029067333 0.030877081 0.00521903
## [2,] 0.008185896 0.009142669 0.006607379 0.01569902
## [3,] 0.005218499 0.026697234 0.011152035 0.03482804
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05179852 0.22959439 0.24388906 0.04122359
## [2,] 0.06465801 0.07221528 0.05218976 0.12400195
## [3,] 0.04121940 0.21087367 0.08808667 0.27509656
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1638708 0.7263493 0.7715722 0.1304158
## [2,] 0.2045533 0.2284617 0.1651085 0.3922950
## [3,] 0.1304025 0.6671241 0.2786727 0.8703008
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08659589 0.3838320 0.40772959 0.0689169
## [2,] 0.10809416 0.1207283 0.08724996 0.2073044
## [3,] 0.06890989 0.3525350 0.14726180 0.4599018
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09472876 0.4198806 0.44602254 0.07538941
## [2,] 0.11824609 0.1320668 0.09544425 0.22677386
## [3,] 0.07538174 0.3856442 0.16109226 0.50309458
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.009229871 0.04091095 0.043458085 0.007345546
## [2,] 0.011521276 0.01286789 0.009299585 0.022095649
## [3,] 0.007344799 0.03757514 0.015695981 0.049018884
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02010146 0.08909871 0.09464604 0.01599764
## [2,] 0.02509184 0.02802459 0.02025328 0.04812144
## [3,] 0.01599602 0.08183376 0.03418380 0.10675674
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07605670 0.3371175 0.35810666 0.06052934
## [2,] 0.09493850 0.1060350 0.07663116 0.18207427
## [3,] 0.06052318 0.3096296 0.12933923 0.40392919
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05544416 0.24575350 0.26105424 0.04412495
## [2,] 0.06920871 0.07729787 0.05586293 0.13272934
## [3,] 0.04412047 0.22571519 0.09428631 0.29445815
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05756448 0.25515170 0.27103758 0.0458124
## [2,] 0.07185541 0.08025393 0.05799926 0.1378052
## [3,] 0.04580774 0.23434708 0.09789204 0.3057189
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02272794 0.10074046 0.1070126 0.01808791
## [2,] 0.02837037 0.03168632 0.0228996 0.05440905
## [3,] 0.01808607 0.09252626 0.0386503 0.12070571
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12259416 0.5433925 0.5772244 0.09756594
## [2,] 0.15302934 0.1709155 0.1235201 0.29348162
## [3,] 0.09755602 0.4990853 0.2084791 0.65108480
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12251382 0.5430364 0.5768461 0.0975020
## [2,] 0.15292905 0.1708035 0.1234392 0.2932893
## [3,] 0.09749208 0.4987582 0.2083425 0.6506581
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03974701 0.17617665 0.18714550 0.03163246
## [2,] 0.04961459 0.05541357 0.04004722 0.09515148
## [3,] 0.03162924 0.16181151 0.06759231 0.21109222
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04942507 0.21907418 0.23271386 0.03933469
## [2,] 0.06169532 0.06890632 0.04979838 0.11832007
## [3,] 0.03933069 0.20121126 0.08405047 0.26249140
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05028079 0.22286713 0.23674295 0.04001572
## [2,] 0.06276348 0.07009933 0.05066057 0.12036861
## [3,] 0.04001165 0.20469494 0.08550568 0.26703605
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1480295 0.6561336 0.6969849 0.1178086
## [2,] 0.1847793 0.2063764 0.1491476 0.3543721
## [3,] 0.1177966 0.6026337 0.2517336 0.7861695
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02245159 0.09951555 0.10571144 0.01786798
## [2,] 0.02802541 0.03130104 0.02262117 0.05374749
## [3,] 0.01786617 0.09140123 0.03818035 0.11923804
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1470315 0.6517098 0.6922856 0.1170143
## [2,] 0.1835335 0.2049850 0.1481420 0.3519828
## [3,] 0.1170024 0.5985706 0.2500364 0.7808690
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1694155 0.7509258 0.7976788 0.1348285
## [2,] 0.2114745 0.2361918 0.1706951 0.4055685
## [3,] 0.1348147 0.6896966 0.2881018 0.8997480
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02357307 0.10448645 0.11099183 0.01876051
## [2,] 0.02942531 0.03286456 0.02375111 0.05643223
## [3,] 0.01875860 0.09596681 0.04008749 0.12519409
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12526037 0.5552104 0.5897781 0.09968783
## [2,] 0.15635746 0.1746326 0.1262065 0.29986433
## [3,] 0.09967769 0.5099395 0.2130132 0.66524477
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08654043 0.3835862 0.40746846 0.06887277
## [2,] 0.10802493 0.1206510 0.08719408 0.20717158
## [3,] 0.06886576 0.3523092 0.14716749 0.45960722
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1281761 0.5681340 0.6035063 0.1020083
## [2,] 0.1599970 0.1786976 0.1291442 0.3068443
## [3,] 0.1019979 0.5218094 0.2179715 0.6807296
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10425341 0.4620981 0.4908686 0.08296955
## [2,] 0.13013533 0.1453456 0.1050408 0.24957518
## [3,] 0.08296111 0.4244194 0.1772895 0.55367900
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.009286839 0.04116346 0.043726314 0.007390884
## [2,] 0.011592387 0.01294731 0.009356983 0.022232026
## [3,] 0.007390132 0.03780706 0.015792859 0.049321435
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05742647 0.25453997 0.27038776 0.04570256
## [2,] 0.07168314 0.08006152 0.05786021 0.13747484
## [3,] 0.04569791 0.23378523 0.09765735 0.30498598
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1339372 0.5936699 0.6306321 0.1065932
## [2,] 0.1671884 0.1867295 0.1349488 0.3206360
## [3,] 0.1065824 0.5452631 0.2277687 0.7113264
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02050768 0.09089929 0.09655873 0.01632094
## [2,] 0.02559891 0.02859093 0.02066258 0.04909392
## [3,] 0.01631928 0.08348752 0.03487461 0.10891417
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1669065 0.7398047 0.7858653 0.1328317
## [2,] 0.2083426 0.2326939 0.1681671 0.3995621
## [3,] 0.1328182 0.6794823 0.2838350 0.8864229
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1649839 0.7312831 0.7768132 0.1313016
## [2,] 0.2059428 0.2300135 0.1662301 0.3949597
## [3,] 0.1312883 0.6716556 0.2805656 0.8762124
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03130016 0.13873639 0.14737419 0.02491007
## [2,] 0.03907072 0.04363734 0.03153657 0.07493033
## [3,] 0.02490754 0.12742407 0.05322790 0.16623187
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1307269 0.5794406 0.6155168 0.1040384
## [2,] 0.1631811 0.1822539 0.1317143 0.3129508
## [3,] 0.1040278 0.5321940 0.2223094 0.6942770
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.008091959 0.03586721 0.038100319 0.006439945
## [2,] 0.010100866 0.01128146 0.008153078 0.019371569
## [3,] 0.006439290 0.03294266 0.013760889 0.042975551
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1478703 0.6554279 0.6962352 0.1176819
## [2,] 0.1845806 0.2061545 0.1489872 0.3539909
## [3,] 0.1176699 0.6019855 0.2514629 0.7853239
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1478274 0.6552375 0.6960329 0.1176477
## [2,] 0.1845269 0.2060946 0.1489439 0.3538881
## [3,] 0.1176357 0.6018106 0.2513898 0.7850957
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.004262059 0.01889137 0.020067552 0.003391938
## [2,] 0.005320156 0.00594198 0.004294250 0.010203063
## [3,] 0.003391593 0.01735100 0.007247901 0.022635351
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1390058 0.6161363 0.6544973 0.1106271
## [2,] 0.1735153 0.1937959 0.1400557 0.3327699
## [3,] 0.1106158 0.5658977 0.2363882 0.7382453
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06765816 0.2998914 0.31856282 0.0538454
## [2,] 0.08445494 0.0943261 0.06816918 0.1619688
## [3,] 0.05383992 0.2754388 0.11505698 0.3593254
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07883327 0.3494246 0.3711799 0.06273906
## [2,] 0.09840439 0.1099060 0.0794287 0.18872120
## [3,] 0.06273268 0.3209331 0.1340610 0.41867530
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04925648 0.21832690 0.23192005 0.03920052
## [2,] 0.06148487 0.06867127 0.04962851 0.11791647
## [3,] 0.03919653 0.20052491 0.08376376 0.26159602
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11303210 0.5010092 0.5322023 0.08995603
## [2,] 0.14109341 0.1575845 0.1138858 0.27059073
## [3,] 0.08994687 0.4601578 0.1922182 0.60030169
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1290495 0.5720053 0.6076186 0.1027034
## [2,] 0.1610872 0.1799152 0.1300242 0.3089351
## [3,] 0.1026929 0.5253650 0.2194568 0.6853682
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03694530 0.16375820 0.17395387 0.02940273
## [2,] 0.04611732 0.05150755 0.03722435 0.08844439
## [3,] 0.02939974 0.15040565 0.06282782 0.19621262
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10334595 0.4580758 0.4865958 0.08224735
## [2,] 0.12900258 0.1440805 0.1041265 0.24740278
## [3,] 0.08223899 0.4207251 0.1757463 0.54885956
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11810061 0.5234751 0.5560669 0.09398977
## [2,] 0.14742022 0.1646508 0.1189926 0.28272438
## [3,] 0.09398021 0.4807919 0.2008376 0.62722003
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12463870 0.5524549 0.5868510 0.09919308
## [2,] 0.15558146 0.1737659 0.1255801 0.29837609
## [3,] 0.09918299 0.5074087 0.2119560 0.66194313
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1398168 0.6197309 0.6583157 0.1112725
## [2,] 0.1745276 0.1949265 0.1408728 0.3347113
## [3,] 0.1112611 0.5691991 0.2377673 0.7425523
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08101991 0.3591167 0.38147552 0.06447928
## [2,] 0.10113388 0.1129545 0.08163185 0.19395584
## [3,] 0.06447272 0.3298350 0.13777948 0.43028827
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1316555 0.5835564 0.6198889 0.1047773
## [2,] 0.1643402 0.1835484 0.1326499 0.3151737
## [3,] 0.1047667 0.5359742 0.2238885 0.6992085
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.004095432 0.018152799 0.019283001 0.003259329
## [2,] 0.005112162 0.005709675 0.004126365 0.009804170
## [3,] 0.003258997 0.016672652 0.006964541 0.021750411
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03191609 0.14146649 0.15027427 0.02540026
## [2,] 0.03983957 0.04449605 0.03215715 0.07640483
## [3,] 0.02539768 0.12993157 0.05427534 0.16950303
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.010778800 0.04777650 0.05075109 0.008578254
## [2,] 0.013454741 0.01502734 0.01086021 0.025803673
## [3,] 0.008577381 0.04388089 0.01833003 0.057245083
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09848062 0.4365105 0.46368786 0.07837531
## [2,] 0.12292939 0.1372975 0.09922445 0.23575554
## [3,] 0.07836733 0.4009182 0.16747254 0.52302032
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05605264 0.24845054 0.26391920 0.04460921
## [2,] 0.06996824 0.07814618 0.05647600 0.13418599
## [3,] 0.04460467 0.22819232 0.09532106 0.29768971
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.09660623 0.42820234 0.45486244 0.07688358
## [2,] 0.12058966 0.13468427 0.09733590 0.23126838
## [3,] 0.07687576 0.39328748 0.16428502 0.51306562
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.06626699 0.29372515 0.31201263 0.05273825
## [2,] 0.08271841 0.09238659 0.06676751 0.15863841
## [3,] 0.05273288 0.26977532 0.11269121 0.35193707
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.1674995 0.7424330 0.7886573 0.1333036
## [2,] 0.2090828 0.2335206 0.1687646 0.4009817
## [3,] 0.1332900 0.6818963 0.2848434 0.8895721
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.010778800 0.047776498 0.050751087 0.008578254
## [2,] 0.013454741 0.015027341 0.010860213 0.025803673
## [3,] 0.008577381 0.043880887 0.018330032 0.057245083
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.09848062 0.43651049 0.46368786 0.07837531
## [2,] 0.12292939 0.13729746 0.09922445 0.23575554
## [3,] 0.07836733 0.40091819 0.16747254 0.52302032
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.05605264 0.24845054 0.26391920 0.04460921
## [2,] 0.06996824 0.07814618 0.05647600 0.13418599
## [3,] 0.04460467 0.22819232 0.09532106 0.29768971
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.117347
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 2.117347
einsum::einsum('ij->', arrC)
## [1] 4.951117
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 4.951117
einsum::einsum('ijk->', arrE)
## [1] 29.29301
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 29.29301
einsum::einsum('ij->i', arrC)
## [1] 1.876336 1.036910 2.037871
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 1.876336 1.036910 2.037871
einsum::einsum('ij->j', arrC)
## [1] 0.5222422 1.6980706 1.2724036 1.4584012
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 0.5222422 1.6980706 1.2724036 1.4584012
einsum::einsum('ijk->i', arrE)
## [1] 9.08481 10.33483 9.87337
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 9.08481 10.33483 9.87337
einsum::einsum('ijk->j', arrE)
## [1] 7.380362 8.237486 6.385597 7.289568
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 7.380362 8.237486 6.385597 7.289568
einsum::einsum('ijk->k', arrE)
## [1] 5.465661 5.326709 6.937782 6.102209 5.460651
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 5.465661 5.326709 6.937782 6.102209 5.460651
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 2.391679 2.643339 2.165347 1.884445
## [2,] 2.211181 3.737064 1.996210 2.390378
## [3,] 2.777502 1.857084 2.224040 3.014745
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 2.391679 2.643339 2.165347 1.884445
## [2,] 2.211181 3.737064 1.996210 2.390378
## [3,] 2.777502 1.857084 2.224040 3.014745
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.9256626 1.1020163 1.854996 0.9915828 1.5061039
## [2,] 1.0550512 1.5611510 1.859199 1.7483930 2.0136926
## [3,] 1.7618312 0.8128372 1.169543 1.6640945 0.9772905
## [4,] 0.7231164 1.8507046 2.054044 1.6981391 0.9635641
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.9256626 1.1020163 1.8549963 0.9915828 1.5061039
## [2,] 1.0550512 1.5611510 1.8591986 1.7483930 2.0136926
## [3,] 1.7618312 0.8128372 1.1695433 1.6640945 0.9772905
## [4,] 0.7231164 1.8507046 2.0540435 1.6981391 0.9635641
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.9256626 1.1020163 1.854996 0.9915828 1.5061039
## [2,] 1.0550512 1.5611510 1.859199 1.7483930 2.0136926
## [3,] 1.7618312 0.8128372 1.169543 1.6640945 0.9772905
## [4,] 0.7231164 1.8507046 2.054044 1.6981391 0.9635641
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.9256626 1.1020163 1.8549963 0.9915828 1.5061039
## [2,] 1.0550512 1.5611510 1.8591986 1.7483930 2.0136926
## [3,] 1.7618312 0.8128372 1.1695433 1.6640945 0.9772905
## [4,] 0.7231164 1.8507046 2.0540435 1.6981391 0.9635641
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.662099
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 1.662099
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.3786758 0.0139346 0.3120799
## [2,] 0.3599433 0.8674260 0.7119344
## [3,] 0.8923042 0.6124116 0.4159971
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.3786758 0.0139346 0.3120799
## [2,] 0.3599433 0.8674260 0.7119344
## [3,] 0.8923042 0.6124116 0.4159971
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.5906993 0.4349302 0.9575025
## [2,] 0.3251362 0.1852364 0.6136927
## [3,] 0.4476954 0.1291474 0.5350367
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.47087190 0.3761729 0.9874131
## [2,] 0.06883213 0.5434373 0.2558085
## [3,] 0.27328936 0.6982062 0.3778473
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.3066250 0.04420761 0.06795316
## [2,] 0.6360856 0.96731567 0.17380023
## [3,] 0.1171569 0.40489063 0.22780851
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.5906993 0.4349302 0.9575025
## [2,] 0.3251362 0.1852364 0.6136927
## [3,] 0.4476954 0.1291474 0.5350367
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.47087190 0.37617288 0.98741310
## [2,] 0.06883213 0.54343728 0.25580847
## [3,] 0.27328936 0.69820617 0.37784735
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.30662496 0.04420761 0.06795316
## [2,] 0.63608555 0.96731567 0.17380023
## [3,] 0.11715693 0.40489063 0.22780851
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.503098
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.503098
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 3.002292
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 3.002292
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 19.59294
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 19.59294
The inner product is an operation that eliminates all subscripts, while the outer product is an operation that leaves all subscripts intact. In the middle of the two, the operation that eliminates some subscripts while keeping others by summing them is called contracted product.
einsum::einsum('ijk,ijk->jk', arrE, arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.4194566 0.4135485 1.5270643 0.6161163 0.8831006
## [2,] 0.5347606 1.0381065 1.1818726 1.4859304 1.4149589
## [3,] 1.2582628 0.2225604 0.7244441 1.0231374 0.6240618
## [4,] 0.3214932 1.4960668 1.8855141 1.0822968 0.4401902
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.4194566 0.4135485 1.5270643 0.6161163 0.8831006
## [2,] 0.5347606 1.0381065 1.1818726 1.4859304 1.4149589
## [3,] 1.2582628 0.2225604 0.7244441 1.0231374 0.6240618
## [4,] 0.3214932 1.4960668 1.8855141 1.0822968 0.4401902
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.2788788 0.4143401 0.9146303
## [2,] 0.4143401 0.3016351 0.6209462
## [3,] 0.9146303 0.6209462 1.4217782
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.2788788 0.4143401 0.9146303
## [2,] 0.4143401 0.3016351 0.6209462
## [3,] 0.9146303 0.6209462 1.4217782
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.02943390 0.04586249 0.01863873
## [2,] 0.57827658 0.05720991 0.48781794
## [3,] 0.65252579 0.02988021 0.08512036
## [4,] 0.01864253 0.16868251 0.83020121
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.02943390 0.04586249 0.01863873
## [2,] 0.57827658 0.05720991 0.48781794
## [3,] 0.65252579 0.02988021 0.08512036
## [4,] 0.01864253 0.16868251 0.83020121
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.31707534 0.19652921 0.975120817 0.03328474 0.046373569
## [2,] 0.17476635 0.01754980 0.254442890 0.74287242 0.527786160
## [3,] 0.09115636 0.05367364 0.002930138 0.65647476 0.588884359
## [4,] 0.30487086 0.74447309 0.014288458 0.08242879 0.003947235
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.1491924 0.10443926 0.01887923 0.5806069 0.3628600117
## [2,] 0.3585332 0.51061286 0.55816930 0.7424408 0.6641568945
## [3,] 0.9123373 0.08299402 0.11204085 0.1555222 0.0005698382
## [4,] 0.0028943 0.01712562 0.94645196 0.4340660 0.3294987401
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.953188864 0.11258002 0.5330643 0.0022246390 0.47386701
## [2,] 0.001461083 0.50994379 0.3692604 0.0006171505 0.22301581
## [3,] 0.254769117 0.08589274 0.6094731 0.2111403847 0.03460761
## [4,] 0.013727999 0.73446811 0.9247737 0.5658020322 0.10674420
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.317075336 0.196529209 0.975120817 0.033284742 0.046373569
## [2,] 0.174766354 0.017549803 0.254442890 0.742872415 0.527786160
## [3,] 0.091156355 0.053673643 0.002930138 0.656474763 0.588884359
## [4,] 0.304870864 0.744473095 0.014288458 0.082428786 0.003947235
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.1491923849 0.1044392641 0.0188792318 0.5806068936 0.3628600117
## [2,] 0.3585331823 0.5106128618 0.5581692982 0.7424408005 0.6641568945
## [3,] 0.9123372940 0.0829940194 0.1120408463 0.1555222388 0.0005698382
## [4,] 0.0028942996 0.0171256200 0.9464519558 0.4340660050 0.3294987401
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9531888636 0.1125800213 0.5330642736 0.0022246390 0.4738670093
## [2,] 0.0014610826 0.5099437859 0.3692604053 0.0006171505 0.2230158129
## [3,] 0.2547691166 0.0858927359 0.6094730752 0.2111403847 0.0346076080
## [4,] 0.0137279986 0.7344681118 0.9247736936 0.5658020322 0.1067441975
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 1.835217 1.993993 1.636451
## [2,] 1.670296 1.456694 2.199719
## [3,] 1.665570 2.192092 3.080120
## [4,] 2.141677 2.676826 1.283707
## [5,] 1.772049 2.015228 1.673374
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.835217 1.993993 1.636451
## [2,] 1.670296 1.456694 2.199719
## [3,] 1.665570 2.192092 3.080120
## [4,] 2.141677 2.676826 1.283707
## [5,] 1.772049 2.015228 1.673374
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.007271981 0.004507309 0.0223639583 0.0007633706 1.063557e-03
## [2,] 0.078747332 0.007907701 0.1146484910 0.3347281637 2.378132e-01
## [3,] 0.046347579 0.027289852 0.0014898006 0.3337783306 2.994126e-01
## [4,] 0.004428566 0.010814244 0.0002075547 0.0011973636 5.733769e-05
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0046993963 0.003289722 0.0005946751 0.01828848 1.142969e-02
## [2,] 0.0140876442 0.020063226 0.0219318347 0.02917231 2.609635e-02
## [3,] 0.0187230600 0.001703210 0.0022993113 0.00319164 1.169427e-05
## [4,] 0.0003353137 0.001984057 0.1096494310 0.05028791 3.817346e-02
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0115719850 0.001366754 0.006471552 2.700775e-05 0.005752881
## [2,] 0.0004642427 0.162028941 0.117328368 1.960927e-04 0.070860783
## [3,] 0.0141251395 0.004762143 0.033790957 1.170624e-02 0.001918746
## [4,] 0.0074234042 0.397163041 0.500070631 3.059570e-01 0.057721839
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,] 7.271981e-03 4.507309e-03 2.236396e-02 7.633706e-04 1.063557e-03
## [2,] 7.874733e-02 7.907701e-03 1.146485e-01 3.347282e-01 2.378132e-01
## [3,] 4.634758e-02 2.728985e-02 1.489801e-03 3.337783e-01 2.994126e-01
## [4,] 4.428566e-03 1.081424e-02 2.075547e-04 1.197364e-03 5.733769e-05
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 4.699396e-03 3.289722e-03 5.946751e-04 1.828848e-02 1.142969e-02
## [2,] 1.408764e-02 2.006323e-02 2.193183e-02 2.917231e-02 2.609635e-02
## [3,] 1.872306e-02 1.703210e-03 2.299311e-03 3.191640e-03 1.169427e-05
## [4,] 3.353137e-04 1.984057e-03 1.096494e-01 5.028791e-02 3.817346e-02
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.157198e-02 1.366754e-03 6.471552e-03 2.700775e-05 5.752881e-03
## [2,] 4.642427e-04 1.620289e-01 1.173284e-01 1.960927e-04 7.086078e-02
## [3,] 1.412514e-02 4.762143e-03 3.379096e-02 1.170624e-02 1.918746e-03
## [4,] 7.423404e-03 3.971630e-01 5.000706e-01 3.059570e-01 5.772184e-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 beta (2024-04-15 r86425 ucrt)
## Platform: x86_64-w64-mingw32/x64
## Running under: Windows Server 2022 x64 (build 20348)
##
## Matrix products: default
##
##
## locale:
## [1] LC_COLLATE=C
## [2] LC_CTYPE=English_United States.utf8
## [3] LC_MONETARY=English_United States.utf8
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.utf8
##
## 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.12.0
## [3] HDF5Array_1.32.0 rhdf5_2.48.0
## [5] DelayedArray_0.30.0 SparseArray_1.4.0
## [7] S4Arrays_1.4.0 abind_1.4-5
## [9] IRanges_2.38.0 S4Vectors_0.42.0
## [11] MatrixGenerics_1.16.0 matrixStats_1.3.0
## [13] BiocGenerics_0.50.0 Matrix_1.7-0
## [15] DelayedTensor_1.10.0 BiocStyle_2.32.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.16.0 parallel_4.4.0 jquerylib_0.1.4
## [10] BiocParallel_1.38.0 yaml_2.3.8 fastmap_1.1.1
## [13] lattice_0.22-6 R6_2.5.1 XVector_0.44.0
## [16] ScaledMatrix_1.12.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.26.0 BiocSingular_1.20.0 zlibbioc_1.50.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.20.0
## [37] rmarkdown_2.26 tools_4.4.0 htmltools_0.5.8.1