DelayedTensor 1.6.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2023-04-25 14:41:07.038824
Compiled: Tue Apr 25 21:41:04 2023
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.29254885 0.09853432 0.89953497
DelayedTensor::einsum('i->i', darrA)
## <3> array of class DelayedArray and type "double":
## [1] [2] [3]
## 0.29254885 0.09853432 0.89953497
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.4515236 0.9111966 0.2808572 0.9283137
## [2,] 0.3871821 0.6237711 0.2855248 0.7301415
## [3,] 0.7141665 0.7469071 0.8579895 0.7407552
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.4515236 0.9111966 0.2808572 0.9283137
## [2,] 0.3871821 0.6237711 0.2855248 0.7301415
## [3,] 0.7141665 0.7469071 0.8579895 0.7407552
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.39094926 0.039026751 0.3830638 0.3244813
## [2,] 0.25685179 0.004152644 0.5045538 0.3195759
## [3,] 0.01259238 0.532789665 0.2524713 0.3241553
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.87904776 0.79999407 0.5497679 0.78015074
## [2,] 0.45940415 0.05418917 0.9141287 0.34352446
## [3,] 0.09110684 0.53126510 0.8160858 0.06069821
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6508681 0.2285613 0.08634113 0.2462185
## [2,] 0.4218009 0.4950677 0.79455717 0.3157589
## [3,] 0.2725895 0.7042664 0.52431328 0.9909947
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3009833 0.04436158 0.6431168 0.6833921
## [2,] 0.7275619 0.07254014 0.5686732 0.6392277
## [3,] 0.2282106 0.36440017 0.8170215 0.4875279
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3161511 0.5048095 0.6811165 0.6260877
## [2,] 0.1550355 0.5352202 0.9492058 0.7265478
## [3,] 0.4209274 0.1371570 0.1996784 0.6868419
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.390949255 0.039026751 0.383063759 0.324481274
## [2,] 0.256851787 0.004152644 0.504553813 0.319575927
## [3,] 0.012592385 0.532789665 0.252471268 0.324155293
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.87904776 0.79999407 0.54976791 0.78015074
## [2,] 0.45940415 0.05418917 0.91412866 0.34352446
## [3,] 0.09110684 0.53126510 0.81608577 0.06069821
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.65086809 0.22856134 0.08634113 0.24621847
## [2,] 0.42180085 0.49506770 0.79455717 0.31575885
## [3,] 0.27258946 0.70426643 0.52431328 0.99099471
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.30098334 0.04436158 0.64311681 0.68339211
## [2,] 0.72756187 0.07254014 0.56867316 0.63922766
## [3,] 0.22821061 0.36440017 0.81702151 0.48752789
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.3161511 0.5048095 0.6811165 0.6260877
## [2,] 0.1550355 0.5352202 0.9492058 0.7265478
## [3,] 0.4209274 0.1371570 0.1996784 0.6868419
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.1145025 0.4532036 0.6980150
DelayedTensor::einsum('ii->i', darrB)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.1145025 0.4532036 0.6980150
einsum::einsum('iii->i', arrD)
## [1] 0.2062262 0.8784310 0.8486420
DelayedTensor::einsum('iii->i', darrD)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.2062262 0.8784310 0.8486420
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.085584832 0.009709011 0.809163167
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.085584832 0.009709011 0.809163167
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.2038735 0.8302793 0.07888078 0.8617663
## [2,] 0.1499100 0.3890903 0.08152443 0.5331066
## [3,] 0.5100338 0.5578702 0.73614597 0.5487183
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.20387354 0.83027930 0.07888078 0.86176628
## [2,] 0.14990995 0.38909033 0.08152443 0.53310660
## [3,] 0.51003378 0.55787015 0.73614597 0.54871831
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1528413202 1.523087e-03 0.14673784 0.1052881
## [2,] 0.0659728406 1.724445e-05 0.25457455 0.1021288
## [3,] 0.0001585681 2.838648e-01 0.06374174 0.1050767
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.772724972 0.639990508 0.3022448 0.608635175
## [2,] 0.211052173 0.002936466 0.8356312 0.118009056
## [3,] 0.008300457 0.282242603 0.6659960 0.003684272
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.42362927 0.05224029 0.007454791 0.06062353
## [2,] 0.17791596 0.24509203 0.631321090 0.09970365
## [3,] 0.07430501 0.49599121 0.274904418 0.98207051
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09059097 0.001967950 0.4135992 0.4670248
## [2,] 0.52934627 0.005262072 0.3233892 0.4086120
## [3,] 0.05208008 0.132787482 0.6675242 0.2376834
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09995151 0.25483266 0.46391965 0.3919858
## [2,] 0.02403600 0.28646063 0.90099163 0.5278717
## [3,] 0.17717984 0.01881205 0.03987147 0.4717518
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> array of class HDF5Array and type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 1.528413e-01 1.523087e-03 1.467378e-01 1.052881e-01
## [2,] 6.597284e-02 1.724445e-05 2.545746e-01 1.021288e-01
## [3,] 1.585681e-04 2.838648e-01 6.374174e-02 1.050767e-01
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.772724972 0.639990508 0.302244755 0.608635175
## [2,] 0.211052173 0.002936466 0.835631216 0.118009056
## [3,] 0.008300457 0.282242603 0.665995990 0.003684272
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.423629269 0.052240288 0.007454791 0.060623535
## [2,] 0.177915959 0.245092030 0.631321090 0.099703652
## [3,] 0.074305013 0.495991206 0.274904418 0.982070508
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.090590969 0.001967950 0.413599233 0.467024775
## [2,] 0.529346274 0.005262072 0.323389159 0.408612007
## [3,] 0.052080084 0.132787482 0.667524150 0.237683443
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.09995151 0.25483266 0.46391965 0.39198585
## [2,] 0.02403600 0.28646063 0.90099163 0.52787167
## [3,] 0.17717984 0.01881205 0.03987147 0.47175179
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.08558483 0.028826101 0.26315792
## [2,] 0.02882610 0.009709011 0.08863506
## [3,] 0.26315792 0.088635063 0.80916317
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.085584832 0.028826101 0.263157925
## [2,] 0.028826101 0.009709011 0.088635063
## [3,] 0.263157925 0.088635063 0.809163167
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1765228 0.3562316 0.1098009 0.3629235
## [2,] 0.1513685 0.2438628 0.1116257 0.2854483
## [3,] 0.2792029 0.2920028 0.3354304 0.2895977
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11597464 0.2340425 0.07213868 0.2384390
## [2,] 0.09944841 0.1602167 0.07333756 0.1875381
## [3,] 0.18343494 0.1918444 0.22037614 0.1902643
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.005685759 0.011474138 0.003536662 0.011689683
## [2,] 0.004875545 0.007854765 0.003595438 0.009194223
## [3,] 0.008993059 0.009405341 0.010804134 0.009327875
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01762150 0.03556104 0.01096095 0.03622907
## [2,] 0.01511046 0.02434376 0.01114311 0.02849505
## [3,] 0.02787160 0.02914936 0.03348454 0.02890927
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.001875017 0.003783875 0.001166300 0.003854956
## [2,] 0.001607829 0.002590299 0.001185683 0.003032018
## [3,] 0.002965679 0.003101639 0.003562925 0.003076093
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2405671 0.4854761 0.1496378 0.4945959
## [2,] 0.2062866 0.3323388 0.1521247 0.3890118
## [3,] 0.3805005 0.3979444 0.4571279 0.3946667
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1729623 0.3490464 0.1075862 0.3556033
## [2,] 0.1483154 0.2389441 0.1093742 0.2796907
## [3,] 0.2735713 0.2861130 0.3286647 0.2837565
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2278179 0.4597477 0.1417076 0.4683842
## [2,] 0.1953542 0.3147261 0.1440626 0.3683957
## [3,] 0.3603354 0.3768548 0.4329019 0.3737509
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11399673 0.2300510 0.07090838 0.2343725
## [2,] 0.09775235 0.1574843 0.07208681 0.1843397
## [3,] 0.18030652 0.1885726 0.21661770 0.1870194
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1465109 0.2956662 0.09113291 0.3012204
## [2,] 0.1256333 0.2024020 0.09264746 0.2369172
## [3,] 0.2317337 0.2423574 0.27840152 0.2403612
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1442961 0.2911965 0.08975521 0.2966667
## [2,] 0.1237341 0.1993422 0.09124686 0.2333356
## [3,] 0.2282304 0.2386935 0.27419279 0.2367275
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1463638 0.2953692 0.09104136 0.3009178
## [2,] 0.1255071 0.2021987 0.09255438 0.2366792
## [3,] 0.2315008 0.2421139 0.27812184 0.2401197
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3969108 0.8009854 0.2468869 0.8160321
## [2,] 0.3403515 0.5483245 0.2509900 0.6418292
## [3,] 0.6277865 0.6565670 0.7542137 0.6511592
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2074318 0.4186075 0.1290270 0.4264712
## [2,] 0.1778730 0.2865630 0.1311713 0.3354300
## [3,] 0.3280911 0.3431322 0.3941639 0.3403060
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04113689 0.08301625 0.02558802 0.08457573
## [2,] 0.03527494 0.05682981 0.02601327 0.06652089
## [3,] 0.06506546 0.06804834 0.07816871 0.06748787
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3612162 0.7289519 0.2246841 0.7426454
## [2,] 0.3097434 0.4990131 0.2284182 0.5841089
## [3,] 0.5713290 0.5975212 0.6863865 0.5925998
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02446769 0.04937699 0.01521942 0.05030455
## [2,] 0.02098107 0.03380164 0.01547235 0.03956576
## [3,] 0.03870009 0.04047427 0.04649374 0.04014091
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2398787 0.4840870 0.1492096 0.4931807
## [2,] 0.2056963 0.3313878 0.1516894 0.3878987
## [3,] 0.3794117 0.3968057 0.4558199 0.3935374
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2482332 0.5009467 0.1544063 0.5103571
## [2,] 0.2128603 0.3429293 0.1569724 0.4014084
## [3,] 0.3926258 0.4106255 0.4716951 0.4072435
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4127506 0.8329510 0.2567396 0.8485981
## [2,] 0.3539342 0.5702070 0.2610064 0.6674433
## [3,] 0.6528401 0.6827692 0.7843128 0.6771456
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3684820 0.7436146 0.2292036 0.7575836
## [2,] 0.3159738 0.5090507 0.2330127 0.5958581
## [3,] 0.5828211 0.6095402 0.7001930 0.6045198
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3522565 0.7108707 0.2191110 0.7242246
## [2,] 0.3020604 0.4866354 0.2227524 0.5696204
## [3,] 0.5571575 0.5827001 0.6693611 0.5779007
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1551094 0.3130183 0.09648133 0.3188985
## [2,] 0.1330065 0.2142806 0.09808476 0.2508215
## [3,] 0.2453337 0.2565808 0.29474038 0.2544675
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02740667 0.05530800 0.01704753 0.05634697
## [2,] 0.02350126 0.03786178 0.01733084 0.04431828
## [3,] 0.04334862 0.04533592 0.05207842 0.04496251
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2938823 0.5930688 0.182801 0.6042097
## [2,] 0.2520045 0.4059927 0.185839 0.4752258
## [3,] 0.4648282 0.4861380 0.558438 0.4821339
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1904530 0.3843435 0.1184658 0.3915635
## [2,] 0.1633137 0.2631072 0.1204346 0.3079743
## [3,] 0.3012360 0.3150460 0.3619007 0.3124512
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1230806 0.2483826 0.07655872 0.2530485
## [2,] 0.1055417 0.1700334 0.07783106 0.1990289
## [3,] 0.1946743 0.2035990 0.23387889 0.2019221
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10320084 0.2082643 0.06419311 0.2121766
## [2,] 0.08849485 0.1425699 0.06525994 0.1668821
## [3,] 0.16323085 0.1707141 0.19610323 0.1693080
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2235347 0.4511040 0.1390433 0.4595781
## [2,] 0.1916813 0.3088089 0.1413541 0.3614695
## [3,] 0.3535608 0.3697696 0.4247629 0.3667240
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3179929 0.6417252 0.1977983 0.6537802
## [2,] 0.2726793 0.4393010 0.2010855 0.5142141
## [3,] 0.5029635 0.5260216 0.6042532 0.5216890
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03898506 0.07867375 0.02424953 0.08015165
## [2,] 0.03342974 0.05385710 0.02465254 0.06304124
## [3,] 0.06166194 0.06448880 0.07407978 0.06395764
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3587613 0.7239978 0.2231571 0.7375983
## [2,] 0.3076383 0.4956218 0.2268658 0.5801392
## [3,] 0.5674461 0.5934604 0.6817217 0.5885724
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2367398 0.4777525 0.1472572 0.4867272
## [2,] 0.2030047 0.3270514 0.1497045 0.3828229
## [3,] 0.3744470 0.3916133 0.4498553 0.3883878
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11117344 0.2243534 0.06915224 0.2285680
## [2,] 0.09533138 0.1535840 0.07030149 0.1797743
## [3,] 0.17584098 0.1839023 0.21125286 0.1823876
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1425726 0.2877184 0.08868316 0.2931233
## [2,] 0.1222562 0.1969612 0.09015699 0.2305486
## [3,] 0.2255044 0.2358425 0.27091778 0.2339000
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4474575 0.9029910 0.2783280 0.9199539
## [2,] 0.3836954 0.6181538 0.2829536 0.7235664
## [3,] 0.7077352 0.7401809 0.8502630 0.7340845
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1359011 0.2742550 0.08453335 0.2794069
## [2,] 0.1165353 0.1877447 0.08593821 0.2197604
## [3,] 0.2149522 0.2248066 0.25824054 0.2229550
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3285113 0.6629519 0.2043410 0.6754056
## [2,] 0.2816989 0.4538320 0.2077370 0.5312231
## [3,] 0.5196003 0.5434211 0.6242404 0.5389453
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10304247 0.2079447 0.0640946 0.2118510
## [2,] 0.08835906 0.1423512 0.0651598 0.1666260
## [3,] 0.16298037 0.1704521 0.1958023 0.1690482
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02003030 0.04042212 0.01245927 0.04118146
## [2,] 0.01717601 0.02767147 0.01266633 0.03239023
## [3,] 0.03168155 0.03313398 0.03806177 0.03286107
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03275358 0.06609833 0.02037342 0.06734001
## [2,] 0.02808624 0.04524844 0.02071201 0.05296457
## [3,] 0.05180574 0.05418074 0.06223868 0.05373449
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1645353 0.3320402 0.1023444 0.3382777
## [2,] 0.1410892 0.2273023 0.1040453 0.2660637
## [3,] 0.2602424 0.2721731 0.3126515 0.2699313
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2903824 0.5860059 0.1806240 0.5970141
## [2,] 0.2490033 0.4011577 0.1836258 0.4695663
## [3,] 0.4592925 0.4803485 0.5517875 0.4763921
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2567693 0.5181731 0.1597160 0.5279071
## [2,] 0.2201800 0.3547219 0.1623703 0.4152119
## [3,] 0.4061273 0.4247460 0.4879156 0.4212476
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3689045 0.7444672 0.2294664 0.7584522
## [2,] 0.3163361 0.5096344 0.2332799 0.5965413
## [3,] 0.5834894 0.6102391 0.7009959 0.6052130
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3085676 0.6227046 0.1919356 0.6344022
## [2,] 0.2645972 0.4262802 0.1951254 0.4989729
## [3,] 0.4880557 0.5104304 0.5863433 0.5062263
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2886264 0.5824621 0.1795317 0.5934038
## [2,] 0.2474975 0.3987317 0.1825154 0.4667266
## [3,] 0.4565150 0.4774437 0.5484506 0.4735112
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2201303 0.4442338 0.1369257 0.4525788
## [2,] 0.1887621 0.3041058 0.1392013 0.3559643
## [3,] 0.3481761 0.3641380 0.4182938 0.3611388
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1427497 0.2880758 0.08879332 0.2934874
## [2,] 0.1224080 0.1972059 0.09026898 0.2308350
## [3,] 0.2257845 0.2361355 0.27125431 0.2341906
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07000217 0.14126780 0.04354283 0.1439216
## [2,] 0.06002696 0.09670664 0.04426648 0.1131978
## [3,] 0.11072114 0.11579709 0.13301881 0.1148433
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1900586 0.3835476 0.1182205 0.3907526
## [2,] 0.1629755 0.2625623 0.1201852 0.3073365
## [3,] 0.3006122 0.3143936 0.3611512 0.3118041
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2279334 0.4599807 0.1417794 0.4686216
## [2,] 0.1954532 0.3148856 0.1441357 0.3685824
## [3,] 0.3605180 0.3770458 0.4331213 0.3739403
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2416645 0.4876908 0.1503205 0.4968522
## [2,] 0.2072277 0.3338548 0.1528186 0.3907865
## [3,] 0.3822363 0.3997597 0.4592133 0.3964671
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06192963 0.12497701 0.03852154 0.1273247
## [2,] 0.05310474 0.08555458 0.03916173 0.1001440
## [3,] 0.09795295 0.10244355 0.11767928 0.1015998
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3075401 0.6206310 0.1912965 0.6322897
## [2,] 0.2637161 0.4248607 0.1944757 0.4973114
## [3,] 0.4864306 0.5087307 0.5843908 0.5045406
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4285888 0.8649131 0.2665913 0.8811607
## [2,] 0.3675155 0.5920871 0.2710218 0.6930545
## [3,] 0.6778910 0.7089685 0.8144086 0.7031292
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09015951 0.1819463 0.05608112 0.1853642
## [2,] 0.07731190 0.1245536 0.05701314 0.1457935
## [3,] 0.14260363 0.1491412 0.17132198 0.1479128
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2826934 0.5704890 0.1758413 0.5812058
## [2,] 0.2424099 0.3905354 0.1787636 0.4571326
## [3,] 0.4471309 0.4676293 0.5371767 0.4637778
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3280535 0.6620279 0.2040562 0.6744642
## [2,] 0.2813063 0.4531995 0.2074474 0.5304827
## [3,] 0.5188761 0.5426637 0.6233704 0.5381941
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3101253 0.6258480 0.1929045 0.6376047
## [2,] 0.2659329 0.4284321 0.1961104 0.5014918
## [3,] 0.4905195 0.5130071 0.5893031 0.5087817
DelayedTensor::einsum('ij,klm->ijklm', darrC, darrE)
## <3 x 4 x 3 x 4 x 5> array of class HDF5Array and type "double":
## ,,1,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.1765228 0.3562316 0.1098009 0.3629235
## [2,] 0.1513685 0.2438628 0.1116257 0.2854483
## [3,] 0.2792029 0.2920028 0.3354304 0.2895977
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.11597464 0.23404248 0.07213868 0.23843903
## [2,] 0.09944841 0.16021671 0.07333756 0.18753815
## [3,] 0.18343494 0.19184441 0.22037614 0.19026431
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.005685759 0.011474138 0.003536662 0.011689683
## [2,] 0.004875545 0.007854765 0.003595438 0.009194223
## [3,] 0.008993059 0.009405341 0.010804134 0.009327875
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.2826934 0.5704890 0.1758413 0.5812058
## [2,] 0.2424099 0.3905354 0.1787636 0.4571326
## [3,] 0.4471309 0.4676293 0.5371767 0.4637778
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.3280535 0.6620279 0.2040562 0.6744642
## [2,] 0.2813063 0.4531995 0.2074474 0.5304827
## [3,] 0.5188761 0.5426637 0.6233704 0.5381941
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.3101253 0.6258480 0.1929045 0.6376047
## [2,] 0.2659329 0.4284321 0.1961104 0.5014918
## [3,] 0.4905195 0.5130071 0.5893031 0.5087817
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.290618
DelayedTensor::einsum('i->', darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 1.290618
einsum::einsum('ij->', arrC)
## [1] 7.658329
DelayedTensor::einsum('ij->', darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 7.658329
einsum::einsum('ijk->', arrE)
## [1] 26.87116
DelayedTensor::einsum('ijk->', darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 26.87116
einsum::einsum('ij->i', arrC)
## [1] 2.571891 2.026619 3.059818
DelayedTensor::einsum('ij->i', darrC)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 2.571891 2.026619 3.059818
einsum::einsum('ij->j', arrC)
## [1] 1.552872 2.281875 1.424372 2.399210
DelayedTensor::einsum('ij->j', darrC)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 1.552872 2.281875 1.424372 2.399210
einsum::einsum('ijk->i', arrE)
## [1] 9.158489 9.257577 8.455093
DelayedTensor::einsum('ijk->i', darrE)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 9.158489 9.257577 8.455093
einsum::einsum('ijk->j', arrE)
## [1] 5.584080 5.047801 8.684095 7.555183
DelayedTensor::einsum('ijk->j', darrE)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 5.584080 5.047801 8.684095 7.555183
einsum::einsum('ijk->k', arrE)
## [1] 3.344664 6.279363 5.731337 5.577017 5.938779
DelayedTensor::einsum('ijk->k', darrE)
## <5> array of class HDF5Array and type "double":
## [1] [2] [3] [4] [5]
## 3.344664 6.279363 5.731337 5.577017 5.938779
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 2.538000 1.616753 2.343406 2.660330
## [2,] 2.020654 1.161170 3.731119 2.344635
## [3,] 1.025427 2.269878 2.609570 2.550218
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 2.538000 1.616753 2.343406 2.660330
## [2,] 2.020654 1.161170 3.731119 2.344635
## [3,] 1.025427 2.269878 2.609570 2.550218
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.6603934 1.429559 1.345258 1.2567558 0.8921139
## [2,] 0.5759691 1.385448 1.427895 0.4813019 1.1771867
## [3,] 1.1400888 2.279982 1.405212 2.0288115 1.8300007
## [4,] 0.9682125 1.184373 1.552972 1.8101477 2.0394774
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.6603934 1.4295588 1.3452584 1.2567558 0.8921139
## [2,] 0.5759691 1.3854483 1.4278955 0.4813019 1.1771867
## [3,] 1.1400888 2.2799823 1.4052116 2.0288115 1.8300007
## [4,] 0.9682125 1.1843734 1.5529720 1.8101477 2.0394774
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.6603934 1.429559 1.345258 1.2567558 0.8921139
## [2,] 0.5759691 1.385448 1.427895 0.4813019 1.1771867
## [3,] 1.1400888 2.279982 1.405212 2.0288115 1.8300007
## [4,] 0.9682125 1.184373 1.552972 1.8101477 2.0394774
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.6603934 1.4295588 1.3452584 1.2567558 0.8921139
## [2,] 0.5759691 1.3854483 1.4278955 0.4813019 1.1771867
## [3,] 1.1400888 2.2799823 1.4052116 2.0288115 1.8300007
## [4,] 0.9682125 1.1843734 1.5529720 1.8101477 2.0394774
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.265721
DelayedTensor::einsum('ii->', darrB)
## <1> array of class HDF5Array and type "double":
## [1]
## 1.265721
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.11450248 0.2409943 0.8705800
## [2,] 0.14830063 0.4532036 0.9320405
## [3,] 0.01869054 0.5914659 0.6980150
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3]
## [1,] 0.11450248 0.24099435 0.87058004
## [2,] 0.14830063 0.45320359 0.93204048
## [3,] 0.01869054 0.59146589 0.69801502
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.2062262 0.4770331 0.1597773
## [2,] 0.3174036 0.9916611 0.1426214
## [3,] 0.6954683 0.4099797 0.3240630
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.84430614 0.00234031 0.9246801
## [2,] 0.03175935 0.87843096 0.2925081
## [3,] 0.08830900 0.16867579 0.1086621
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.9460945 0.1663410 0.1462208
## [2,] 0.8979592 0.7172170 0.2342384
## [3,] 0.3567385 0.6944697 0.8486420
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.2062262 0.4770331 0.1597773
## [2,] 0.3174036 0.9916611 0.1426214
## [3,] 0.6954683 0.4099797 0.3240630
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.84430614 0.00234031 0.92468008
## [2,] 0.03175935 0.87843096 0.29250806
## [3,] 0.08830900 0.16867579 0.10866213
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.9460945 0.1663410 0.1462208
## [2,] 0.8979592 0.7172170 0.2342384
## [3,] 0.3567385 0.6944697 0.8486420
Some examples of combining Multiplication and Summation are shown below.
Inner Product first calculate Hadamard Product and collapses it to 0D tensor (norm).
einsum::einsum('i,i->', arrA, arrA)
## [1] 0.904457
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 0.904457
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 5.481199
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 5.481199
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 16.24616
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 16.24616
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.2189727 0.9920776 0.6758502 0.6720173 0.3011673
## [2,] 0.2854052 0.9251696 0.7933235 0.1400175 0.5601053
## [3,] 0.4650541 1.8038720 0.9136803 1.4045125 1.4047827
## [4,] 0.3124935 0.7303285 1.1423977 1.1133202 1.3916093
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.2189727 0.9920776 0.6758502 0.6720173 0.3011673
## [2,] 0.2854052 0.9251696 0.7933235 0.1400175 0.5601053
## [3,] 0.4650541 1.8038720 0.9136803 1.4045125 1.4047827
## [4,] 0.3124935 0.7303285 1.1423977 1.1133202 1.3916093
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.974800 1.501192 1.931668
## [2,] 1.501192 1.153631 1.528245
## [3,] 1.931668 1.528245 2.352768
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 1.974800 1.501192 1.931668
## [2,] 1.501192 1.153631 1.528245
## [3,] 1.931668 1.528245 2.352768
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.20387354 0.14990995 0.5100338
## [2,] 0.83027930 0.38909033 0.5578702
## [3,] 0.07888078 0.08152443 0.7361460
## [4,] 0.86176628 0.53310660 0.5487183
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.20387354 0.14990995 0.51003378
## [2,] 0.83027930 0.38909033 0.55787015
## [3,] 0.07888078 0.08152443 0.73614597
## [4,] 0.86176628 0.53310660 0.54871831
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.152841320 0.7727250 0.423629269 0.09059097 0.09995151
## [2,] 0.001523087 0.6399905 0.052240288 0.00196795 0.25483266
## [3,] 0.146737843 0.3022448 0.007454791 0.41359923 0.46391965
## [4,] 0.105288097 0.6086352 0.060623535 0.46702477 0.39198585
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 6.597284e-02 0.211052173 0.17791596 0.529346274 0.0240360
## [2,] 1.724445e-05 0.002936466 0.24509203 0.005262072 0.2864606
## [3,] 2.545746e-01 0.835631216 0.63132109 0.323389159 0.9009916
## [4,] 1.021288e-01 0.118009056 0.09970365 0.408612007 0.5278717
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0001585681 0.008300457 0.07430501 0.05208008 0.17717984
## [2,] 0.2838648267 0.282242603 0.49599121 0.13278748 0.01881205
## [3,] 0.0637417413 0.665995990 0.27490442 0.66752415 0.03987147
## [4,] 0.1050766539 0.003684272 0.98207051 0.23768344 0.47175179
DelayedTensor::einsum('ijk,ijk->jki', darrE, darrE)
## <4 x 5 x 3> array of class HDF5Array and type "double":
## ,,1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.152841320 0.772724972 0.423629269 0.090590969 0.099951511
## [2,] 0.001523087 0.639990508 0.052240288 0.001967950 0.254832657
## [3,] 0.146737843 0.302244755 0.007454791 0.413599233 0.463919646
## [4,] 0.105288097 0.608635175 0.060623535 0.467024775 0.391985847
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 6.597284e-02 2.110522e-01 1.779160e-01 5.293463e-01 2.403600e-02
## [2,] 1.724445e-05 2.936466e-03 2.450920e-01 5.262072e-03 2.864606e-01
## [3,] 2.545746e-01 8.356312e-01 6.313211e-01 3.233892e-01 9.009916e-01
## [4,] 1.021288e-01 1.180091e-01 9.970365e-02 4.086120e-01 5.278717e-01
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0001585681 0.0083004570 0.0743050131 0.0520800842 0.1771798367
## [2,] 0.2838648267 0.2822426030 0.4959912062 0.1327874817 0.0188120477
## [3,] 0.0637417413 0.6659959897 0.2749044177 0.6675241502 0.0398714667
## [4,] 0.1050766539 0.0036842721 0.9820705083 0.2376834427 0.4717517937
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 1.137521 1.085134 1.122009
## [2,] 3.008960 1.771246 1.499156
## [3,] 1.211989 2.027185 2.492164
## [4,] 1.671854 2.008003 1.897160
## [5,] 2.128165 2.366009 1.444605
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 1.137521 1.085134 1.122009
## [2,] 3.008960 1.771246 1.499156
## [3,] 1.211989 2.027185 2.492164
## [4,] 1.671854 2.008003 1.897160
## [5,] 2.128165 2.366009 1.444605
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.0091159104 0.046087613 0.0252665080 0.0054031145 0.005961405
## [2,] 0.0003699537 0.155451939 0.0126890226 0.0004780096 0.061898154
## [3,] 0.0033861933 0.006974746 0.0001720303 0.0095444155 0.010705634
## [4,] 0.0265440493 0.153442246 0.0152837228 0.1177410267 0.098823057
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 9.745029e-04 0.0031175096 0.002628045 0.0078191191 0.0003550423
## [2,] 6.611308e-07 0.0001125804 0.009396522 0.0002017413 0.0109825423
## [3,] 2.044986e-03 0.0067125867 0.005071373 0.0025977701 0.0072376238
## [4,] 5.364752e-03 0.0061989324 0.005237362 0.0214641004 0.0277287263
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 7.274999e-05 0.003808193 0.03409064 0.02389399 0.081288908
## [2,] 1.424501e-01 0.141636031 0.24890015 0.06663591 0.009440332
## [3,] 4.220908e-02 0.441015250 0.18203869 0.44202718 0.026402449
## [4,] 5.186492e-02 0.001818525 0.48474147 0.11731848 0.232852588
DelayedTensor::einsum('i,ij,ijk,ijk,ji->jki',
darrA, darrC, darrE, darrE, t(darrC))
## <4 x 5 x 3> array of class HDF5Array and type "double":
## ,,1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0091159104 0.0460876128 0.0252665080 0.0054031145 0.0059614050
## [2,] 0.0003699537 0.1554519392 0.0126890226 0.0004780096 0.0618981536
## [3,] 0.0033861933 0.0069747459 0.0001720303 0.0095444155 0.0107056337
## [4,] 0.0265440493 0.1534422461 0.0152837228 0.1177410267 0.0988230574
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 9.745029e-04 3.117510e-03 2.628045e-03 7.819119e-03 3.550423e-04
## [2,] 6.611308e-07 1.125804e-04 9.396522e-03 2.017413e-04 1.098254e-02
## [3,] 2.044986e-03 6.712587e-03 5.071373e-03 2.597770e-03 7.237624e-03
## [4,] 5.364752e-03 6.198932e-03 5.237362e-03 2.146410e-02 2.772873e-02
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 7.274999e-05 3.808193e-03 3.409064e-02 2.389399e-02 8.128891e-02
## [2,] 1.424501e-01 1.416360e-01 2.489001e-01 6.663591e-02 9.440332e-03
## [3,] 4.220908e-02 4.410152e-01 1.820387e-01 4.420272e-01 2.640245e-02
## [4,] 5.186492e-02 1.818525e-03 4.847415e-01 1.173185e-01 2.328526e-01
einsum
By using einsum
and other DelayedTensor functions,
it is possible to implement your original tensor calculation functions.
It is intended to be applied to Delayed Arrays,
which can scale to large-scale data
since the calculation is performed internally by block processing.
For example, kronecker
can be easily implmented by eimsum
and other DelayedTensor functions4 https://stackoverflow.com/
questions/56067643/speeding-up-kronecker-products-numpy
(the kronecker
function inside DelayedTensor
has a more efficient implementation though).
darr1 <- DelayedArray(array(1:6, dim=c(2,3)))
darr2 <- DelayedArray(array(20:1, dim=c(4,5)))
mykronecker <- function(darr1, darr2){
stopifnot((length(dim(darr1)) == 2) && (length(dim(darr2)) == 2))
# Outer Product
tmpdarr <- DelayedTensor::einsum('ij,kl->ikjl', darr1, darr2)
# Reshape
DelayedTensor::unfold(tmpdarr, row_idx=c(2,1), col_idx=c(4,3))
}
identical(as.array(DelayedTensor::kronecker(darr1, darr2)),
as.array(mykronecker(darr1, darr2)))
## [1] TRUE
## R version 4.3.0 RC (2023-04-13 r84269 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## 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.0 DelayedRandomArray_1.8.0 HDF5Array_1.28.0
## [4] rhdf5_2.44.0 DelayedArray_0.26.0 IRanges_2.34.0
## [7] S4Vectors_0.38.0 MatrixGenerics_1.12.0 matrixStats_0.63.0
## [10] BiocGenerics_0.46.0 Matrix_1.5-4 DelayedTensor_1.6.0
## [13] BiocStyle_2.28.0
##
## loaded via a namespace (and not attached):
## [1] jsonlite_1.8.4 compiler_4.3.0 BiocManager_1.30.20
## [4] Rcpp_1.0.10 rsvd_1.0.5 rhdf5filters_1.12.0
## [7] parallel_4.3.0 jquerylib_0.1.4 BiocParallel_1.34.0
## [10] yaml_2.3.7 fastmap_1.1.1 lattice_0.21-8
## [13] R6_2.5.1 ScaledMatrix_1.8.0 knitr_1.42
## [16] bookdown_0.33 bslib_0.4.2 rlang_1.1.0
## [19] cachem_1.0.7 xfun_0.39 sass_0.4.5
## [22] cli_3.6.1 Rhdf5lib_1.22.0 BiocSingular_1.16.0
## [25] digest_0.6.31 grid_4.3.0 irlba_2.3.5.1
## [28] rTensor_1.4.8 dqrng_0.3.0 evaluate_0.20
## [31] codetools_0.2-19 beachmat_2.16.0 rmarkdown_2.21
## [34] tools_4.3.0 htmltools_0.5.5