DelayedTensor 1.11.1
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2024-09-25 17:44:00.726748
Compiled: Mon Oct 21 17:20:30 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.6760664 0.8503470 0.1233181
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.6760664 0.8503470 0.1233181
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.8847660 0.1806226 0.4549895 0.7778355
## [2,] 0.9135826 0.6782409 0.4491555 0.3127091
## [3,] 0.5838432 0.6059615 0.3390056 0.5806778
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.8847660 0.1806226 0.4549895 0.7778355
## [2,] 0.9135826 0.6782409 0.4491555 0.3127091
## [3,] 0.5838432 0.6059615 0.3390056 0.5806778
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2959545 0.45205619 0.6632093 0.8390993
## [2,] 0.5778360 0.82622571 0.5766948 0.2188496
## [3,] 0.3065674 0.01701943 0.7941738 0.9596600
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.38222092 0.4389169 0.7347150 0.7525734
## [2,] 0.92741322 0.8664232 0.7012545 0.7535780
## [3,] 0.08941075 0.3475719 0.4443425 0.4745139
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2397078 0.6814294 0.2628959 0.3539665
## [2,] 0.3358372 0.5897795 0.7936006 0.2545530
## [3,] 0.6216277 0.2481434 0.6523394 0.7820640
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5240946 0.9828687 0.4050104 0.285252729
## [2,] 0.4120617 0.7022650 0.8657151 0.918001303
## [3,] 0.3444703 0.4722231 0.5391032 0.007453252
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4478981 0.9343083 0.3013094 0.1339110
## [2,] 0.7691689 0.7325246 0.9300810 0.9815599
## [3,] 0.8014100 0.6097880 0.2319480 0.9952790
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.29595455 0.45205619 0.66320930 0.83909933
## [2,] 0.57783597 0.82622571 0.57669476 0.21884962
## [3,] 0.30656735 0.01701943 0.79417383 0.95965997
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.38222092 0.43891693 0.73471497 0.75257344
## [2,] 0.92741322 0.86642318 0.70125451 0.75357803
## [3,] 0.08941075 0.34757192 0.44434249 0.47451387
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.2397078 0.6814294 0.2628959 0.3539665
## [2,] 0.3358372 0.5897795 0.7936006 0.2545530
## [3,] 0.6216277 0.2481434 0.6523394 0.7820640
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.524094628 0.982868701 0.405010366 0.285252729
## [2,] 0.412061705 0.702264998 0.865715147 0.918001303
## [3,] 0.344470310 0.472223108 0.539103175 0.007453252
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.4478981 0.9343083 0.3013094 0.1339110
## [2,] 0.7691689 0.7325246 0.9300810 0.9815599
## [3,] 0.8014100 0.6097880 0.2319480 0.9952790
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.1909438 0.4437225 0.1456790
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.1909438 0.4437225 0.1456790
einsum::einsum('iii->i', arrD)
## [1] 0.8132550 0.4578509 0.9635212
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.8132550 0.4578509 0.9635212
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.45706572 0.72309008 0.01520734
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.45706572 0.72309008 0.01520734
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.7828110 0.03262454 0.2070155 0.60502811
## [2,] 0.8346332 0.46001067 0.2017407 0.09778697
## [3,] 0.3408729 0.36718929 0.1149248 0.33718668
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.78281096 0.03262454 0.20701546 0.60502811
## [2,] 0.83463323 0.46001067 0.20174068 0.09778697
## [3,] 0.34087288 0.36718929 0.11492482 0.33718668
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08758909 0.2043547974 0.4398466 0.70408768
## [2,] 0.33389441 0.6826489183 0.3325768 0.04789516
## [3,] 0.09398354 0.0002896611 0.6307121 0.92094726
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.146092829 0.1926481 0.5398061 0.5663668
## [2,] 0.860095274 0.7506891 0.4917579 0.5678798
## [3,] 0.007994283 0.1208062 0.1974402 0.2251634
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05745981 0.46434600 0.06911423 0.12529229
## [2,] 0.11278665 0.34783986 0.62980193 0.06479721
## [3,] 0.38642094 0.06157514 0.42554666 0.61162417
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2746752 0.9660309 0.1640334 8.136912e-02
## [2,] 0.1697948 0.4931761 0.7494627 8.427264e-01
## [3,] 0.1186598 0.2229947 0.2906322 5.555096e-05
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2006127 0.8729320 0.09078734 0.01793215
## [2,] 0.5916207 0.5365922 0.86505070 0.96345979
## [3,] 0.6422579 0.3718414 0.05379986 0.99058019
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.0875890948 0.2043547974 0.4398465701 0.7040876792
## [2,] 0.3338944118 0.6826489183 0.3325768470 0.0478951552
## [3,] 0.0939835412 0.0002896611 0.6307120766 0.9209472632
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.146092829 0.192648072 0.539806085 0.566366776
## [2,] 0.860095274 0.750689122 0.491757894 0.567879848
## [3,] 0.007994283 0.120806241 0.197440246 0.225163410
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.05745981 0.46434600 0.06911423 0.12529229
## [2,] 0.11278665 0.34783986 0.62980193 0.06479721
## [3,] 0.38642094 0.06157514 0.42554666 0.61162417
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 2.746752e-01 9.660309e-01 1.640334e-01 8.136912e-02
## [2,] 1.697948e-01 4.931761e-01 7.494627e-01 8.427264e-01
## [3,] 1.186598e-01 2.229947e-01 2.906322e-01 5.555096e-05
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.20061275 0.87293200 0.09078734 0.01793215
## [2,] 0.59162073 0.53659223 0.86505070 0.96345979
## [3,] 0.64225793 0.37184144 0.05379986 0.99058019
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.45706572 0.5748910 0.08337119
## [2,] 0.57489102 0.7230901 0.10486314
## [3,] 0.08337119 0.1048631 0.01520734
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.45706572 0.57489102 0.08337119
## [2,] 0.57489102 0.72309008 0.10486314
## [3,] 0.08337119 0.10486314 0.01520734
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2618505 0.05345609 0.1346562 0.23020396
## [2,] 0.2703789 0.20072847 0.1329296 0.09254767
## [3,] 0.1727910 0.17933705 0.1003303 0.17185423
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5112497 0.1043703 0.2629093 0.4494613
## [2,] 0.5279009 0.3919120 0.2595382 0.1806946
## [3,] 0.3373656 0.3501463 0.1958897 0.3355365
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2712404 0.05537301 0.1394849 0.23845898
## [2,] 0.2800746 0.20792650 0.1376964 0.09586639
## [3,] 0.1789873 0.18576800 0.1039281 0.17801685
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3999640 0.08165158 0.2056808 0.3516254
## [2,] 0.4129907 0.30660298 0.2030435 0.1413621
## [3,] 0.2639299 0.27392863 0.1532496 0.2624990
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7310165 0.1492351 0.3759240 0.6426677
## [2,] 0.7548255 0.5603800 0.3711038 0.2583683
## [3,] 0.4823863 0.5006609 0.2800952 0.4797709
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.015058215 0.003074095 0.007743663 0.013238319
## [2,] 0.015548657 0.011543274 0.007644372 0.005322131
## [3,] 0.009936679 0.010313120 0.005769683 0.009882806
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5867851 0.1197906 0.3017533 0.5158678
## [2,] 0.6058965 0.4498156 0.2978841 0.2073916
## [3,] 0.3872102 0.4018793 0.2248317 0.3851109
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5102399 0.1041641 0.2623901 0.4485737
## [2,] 0.5268583 0.3911380 0.2590256 0.1803377
## [3,] 0.3366993 0.3494548 0.1955028 0.3348738
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7026580 0.1434458 0.3613408 0.6177366
## [2,] 0.7255434 0.5386411 0.3567076 0.2483454
## [3,] 0.4636730 0.4812387 0.2692294 0.4611591
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7424066 0.1515603 0.3817814 0.6526813
## [2,] 0.7665866 0.5691115 0.3768861 0.2623940
## [3,] 0.4899024 0.5084619 0.2844594 0.4872463
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1936307 0.0395292 0.09957428 0.17022901
## [2,] 0.1999372 0.1484328 0.09829751 0.06843626
## [3,] 0.1277739 0.1326144 0.07419125 0.12708111
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8490746 0.1733363 0.4366352 0.7464576
## [2,] 0.8767287 0.6508806 0.4310366 0.3000944
## [3,] 0.5602909 0.5815170 0.3253301 0.5572532
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3381761 0.06903775 0.1739065 0.2973050
## [2,] 0.3491904 0.25923784 0.1716766 0.1195239
## [3,] 0.2231571 0.23161115 0.1295750 0.2219472
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8205437 0.1675118 0.4219633 0.7213749
## [2,] 0.8472686 0.6290095 0.4165528 0.2900105
## [3,] 0.5414639 0.5619767 0.3143983 0.5385282
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07910760 0.01614961 0.04068096 0.06954686
## [2,] 0.08168411 0.06064203 0.04015933 0.02795955
## [3,] 0.05220186 0.05417947 0.03031075 0.05191884
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3883388 0.07927834 0.1997026 0.3414052
## [2,] 0.4009869 0.29769140 0.1971420 0.1372533
## [3,] 0.2562587 0.26596674 0.1487953 0.2548693
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7665818 0.1564956 0.3942135 0.6739347
## [2,] 0.7915492 0.5876436 0.3891587 0.2709384
## [3,] 0.5058553 0.5250191 0.2937223 0.5031127
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3075198 0.06277936 0.1581416 0.2703538
## [2,] 0.3175357 0.23573748 0.1561138 0.1086889
## [3,] 0.2029275 0.21061519 0.1178288 0.2018273
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6500509 0.1327062 0.3342876 0.5714874
## [2,] 0.6712228 0.4983137 0.3300013 0.2297520
## [3,] 0.4289583 0.4452090 0.2490725 0.4266327
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6204462 0.1266624 0.3190635 0.5454607
## [2,] 0.6406539 0.4756195 0.3149723 0.2192887
## [3,] 0.4094227 0.4249332 0.2377292 0.4072029
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3931391 0.08025831 0.2021712 0.3456254
## [2,] 0.4059436 0.30137123 0.1995789 0.1389499
## [3,] 0.2594263 0.26925442 0.1506346 0.2580198
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6658514 0.1359318 0.3424130 0.5853784
## [2,] 0.6875380 0.5104261 0.3380225 0.2353365
## [3,] 0.4393849 0.4560305 0.2551266 0.4370027
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6667403 0.1361133 0.3428701 0.5861598
## [2,] 0.6884558 0.5111074 0.3384737 0.2356507
## [3,] 0.4399714 0.4566392 0.2554672 0.4375860
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4198338 0.08570795 0.2158988 0.3690937
## [2,] 0.4335076 0.32183469 0.2131305 0.1483848
## [3,] 0.2770417 0.28753712 0.1608629 0.2755397
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2120853 0.04329665 0.10906451 0.18645320
## [2,] 0.2189928 0.16257959 0.10766606 0.07495879
## [3,] 0.1399517 0.14525366 0.08126228 0.13919296
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2971374 0.06065981 0.1528024 0.2612261
## [2,] 0.3068151 0.22777853 0.1508431 0.1050194
## [3,] 0.1960763 0.20350442 0.1138507 0.1950132
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5499950 0.1122800 0.2828341 0.4835241
## [2,] 0.5679082 0.4216133 0.2792075 0.1943886
## [3,] 0.3629331 0.3766824 0.2107353 0.3609654
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6029056 0.1230816 0.3100432 0.5300400
## [2,] 0.6225420 0.4621732 0.3060678 0.2130892
## [3,] 0.3978479 0.4129199 0.2310084 0.3956909
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5218169 0.1065275 0.2683435 0.4587515
## [2,] 0.5388123 0.4000126 0.2649027 0.1844294
## [3,] 0.3443388 0.3573837 0.1999386 0.3424718
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2195488 0.04482031 0.11290264 0.19301474
## [2,] 0.2266995 0.16830098 0.11145497 0.07759669
## [3,] 0.1448768 0.15036533 0.08412201 0.14409135
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2326013 0.04748494 0.11961486 0.20448974
## [2,] 0.2401771 0.17830671 0.11808112 0.08220992
## [3,] 0.1534900 0.15930476 0.08912318 0.15265778
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7021509 0.1433422 0.3610800 0.6172908
## [2,] 0.7250197 0.5382524 0.3564501 0.2481661
## [3,] 0.4633383 0.4808914 0.2690351 0.4608262
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5771677 0.1178273 0.2968076 0.5074127
## [2,] 0.5959659 0.4424432 0.2930018 0.2039924
## [3,] 0.3808639 0.3952925 0.2211467 0.3787990
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3131776 0.06393437 0.1610511 0.2753277
## [2,] 0.3233777 0.24007455 0.1589860 0.1106885
## [3,] 0.2066609 0.21449006 0.1199966 0.2055405
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2252198 0.04597803 0.11581893 0.19800034
## [2,] 0.2325552 0.17264822 0.11433387 0.07960102
## [3,] 0.1486190 0.15424929 0.08629489 0.14781325
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6919437 0.1412585 0.3558309 0.6083172
## [2,] 0.7144801 0.5304278 0.3512684 0.2445585
## [3,] 0.4566028 0.4739007 0.2651241 0.4541272
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4637011 0.09466336 0.2384576 0.4076594
## [2,] 0.4788037 0.35546239 0.2354000 0.1638891
## [3,] 0.3059891 0.31758115 0.1776710 0.3043301
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3645782 0.07442767 0.1874838 0.3205162
## [2,] 0.3764524 0.27947709 0.1850798 0.1288554
## [3,] 0.2405794 0.24969351 0.1396912 0.2392751
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3047756 0.06221914 0.1567304 0.2679412
## [2,] 0.3147021 0.23363384 0.1547207 0.1077190
## [3,] 0.2011166 0.20873573 0.1167774 0.2000263
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8696089 0.1775283 0.4471950 0.7645102
## [2,] 0.8979318 0.6666217 0.4414609 0.3073520
## [3,] 0.5738412 0.5955806 0.3331980 0.5707300
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6213402 0.1268450 0.3195232 0.5462467
## [2,] 0.6415771 0.4763048 0.3154262 0.2196046
## [3,] 0.4100126 0.4255455 0.2380718 0.4077897
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4178070 0.08529419 0.2148566 0.3673119
## [2,] 0.4314148 0.32028101 0.2121016 0.1476685
## [3,] 0.2757042 0.28614900 0.1600863 0.2742095
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3583394 0.07315404 0.1842755 0.3150315
## [2,] 0.3700104 0.27469458 0.1819126 0.1266504
## [3,] 0.2364625 0.24542067 0.1373008 0.2351805
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7659554 0.1563678 0.3938913 0.6733840
## [2,] 0.7909023 0.5871634 0.3888407 0.2707170
## [3,] 0.5054419 0.5245900 0.2934823 0.5027015
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4769802 0.09737424 0.2452863 0.4193336
## [2,] 0.4925153 0.36564180 0.2421412 0.1685825
## [3,] 0.3147517 0.32667575 0.1827590 0.3130452
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2523819 0.0515231 0.12978700 0.22187971
## [2,] 0.2606019 0.1934701 0.12812284 0.08920112
## [3,] 0.1665429 0.1728522 0.09670228 0.16563992
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8122164 0.1658118 0.4176810 0.7140540
## [2,] 0.8386700 0.6226260 0.4123253 0.2870673
## [3,] 0.5359688 0.5562734 0.3112076 0.5330630
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.006594384 0.001346226 0.003391151 0.005797404
## [2,] 0.006809161 0.005055100 0.003347669 0.002330700
## [3,] 0.004351530 0.004516383 0.002526694 0.004327938
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3962851 0.08090055 0.2037890 0.3483911
## [2,] 0.4091920 0.30378282 0.2011759 0.1400618
## [3,] 0.2615023 0.27140901 0.1518400 0.2600845
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6805345 0.1389293 0.3499638 0.5982869
## [2,] 0.7026993 0.5216817 0.3454764 0.2405261
## [3,] 0.4490740 0.4660867 0.2607526 0.4466393
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7090603 0.1447528 0.3646331 0.6233651
## [2,] 0.7321542 0.5435490 0.3599577 0.2506082
## [3,] 0.4678978 0.4856236 0.2716825 0.4653610
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8266443 0.1687572 0.4251005 0.7267382
## [2,] 0.8535678 0.6336861 0.4196497 0.2921667
## [3,] 0.5454895 0.5661548 0.3167358 0.5425321
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6481129 0.1323105 0.3332910 0.5697836
## [2,] 0.6692217 0.4968281 0.3290174 0.2290671
## [3,] 0.4276795 0.4438817 0.2483300 0.4253607
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5395197 0.1101415 0.2774472 0.4743148
## [2,] 0.5570917 0.4135832 0.2738897 0.1906863
## [3,] 0.3560206 0.3695080 0.2067216 0.3540904
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2665883 0.0544233 0.1370926 0.23436914
## [2,] 0.2752710 0.2043603 0.1353348 0.09422218
## [3,] 0.1759174 0.1825819 0.1021456 0.17496366
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8229041 0.1679937 0.4231771 0.7234501
## [2,] 0.8497059 0.6308190 0.4177510 0.2908448
## [3,] 0.5430215 0.5635933 0.3153027 0.5400774
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2052197 0.04189505 0.10553389 0.18041737
## [2,] 0.2119036 0.15731659 0.10418071 0.07253223
## [3,] 0.1354212 0.14055153 0.07863167 0.13468703
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11847988 0.02418735 0.06092809 0.10416071
## [2,] 0.12233874 0.09082389 0.06014685 0.04187518
## [3,] 0.07818301 0.08114489 0.04539657 0.07775912
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8684509 0.1772919 0.4465995 0.7634921
## [2,] 0.8967361 0.6657340 0.4408730 0.3069427
## [3,] 0.5730771 0.5947875 0.3327543 0.5699700
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8805890 0.1797699 0.4528415 0.7741633
## [2,] 0.9092696 0.6750389 0.4470350 0.3112328
## [3,] 0.5810868 0.6031007 0.3374052 0.5779364
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.26185054 0.05345609 0.13465622 0.23020396
## [2,] 0.27037894 0.20072847 0.13292962 0.09254767
## [3,] 0.17279105 0.17933705 0.10033026 0.17185423
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.5112497 0.1043703 0.2629093 0.4494613
## [2,] 0.5279009 0.3919120 0.2595382 0.1806946
## [3,] 0.3373656 0.3501463 0.1958897 0.3355365
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.27124038 0.05537301 0.13948493 0.23845898
## [2,] 0.28007461 0.20792650 0.13769642 0.09586639
## [3,] 0.17898726 0.18576800 0.10392806 0.17801685
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.11847988 0.02418735 0.06092809 0.10416071
## [2,] 0.12233874 0.09082389 0.06014685 0.04187518
## [3,] 0.07818301 0.08114489 0.04539657 0.07775912
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.8684509 0.1772919 0.4465995 0.7634921
## [2,] 0.8967361 0.6657340 0.4408730 0.3069427
## [3,] 0.5730771 0.5947875 0.3327543 0.5699700
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.8805890 0.1797699 0.4528415 0.7741633
## [2,] 0.9092696 0.6750389 0.4470350 0.3112328
## [3,] 0.5810868 0.6031007 0.3374052 0.5779364
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.649731
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.649731
einsum::einsum('ij->', arrC)
## [1] 6.76139
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 6.76139
einsum::einsum('ijk->', arrE)
## [1] 33.58393
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 33.58393
einsum::einsum('ij->i', arrC)
## [1] 2.298214 2.353688 2.109488
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 2.298214 2.353688 2.109488
einsum::einsum('ij->j', arrC)
## [1] 2.382192 1.464825 1.243151 1.671222
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 2.382192 1.464825 1.243151 1.671222
einsum::einsum('ijk->i', arrE)
## [1] 10.111398 13.733423 9.739109
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 10.111398 13.733423 9.739109
einsum::einsum('ijk->j', arrE)
## [1] 7.075679 8.901543 8.896393 8.710315
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 7.075679 8.901543 8.896393 8.710315
einsum::einsum('ijk->k', arrE)
## [1] 6.527346 6.912934 5.815944 6.458519 7.869186
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 6.527346 6.912934 5.815944 6.458519 7.869186
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 1.889876 3.489579 2.367140 2.364803
## [2,] 3.022317 3.717218 3.867346 3.126542
## [3,] 2.163486 1.694746 2.661907 3.218970
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 1.889876 3.489579 2.367140 2.364803
## [2,] 3.022317 3.717218 3.867346 3.126542
## [3,] 2.163486 1.694746 2.661907 3.218970
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.180358 1.399045 1.197173 1.280627 2.018477
## [2,] 1.295301 1.652912 1.519352 2.157357 2.276621
## [3,] 2.034078 1.880312 1.708836 1.809829 1.463338
## [4,] 2.017609 1.980665 1.390584 1.210707 2.110750
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.180358 1.399045 1.197173 1.280627 2.018477
## [2,] 1.295301 1.652912 1.519352 2.157357 2.276621
## [3,] 2.034078 1.880312 1.708836 1.809829 1.463338
## [4,] 2.017609 1.980665 1.390584 1.210707 2.110750
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.180358 1.399045 1.197173 1.280627 2.018477
## [2,] 1.295301 1.652912 1.519352 2.157357 2.276621
## [3,] 2.034078 1.880312 1.708836 1.809829 1.463338
## [4,] 2.017609 1.980665 1.390584 1.210707 2.110750
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.180358 1.399045 1.197173 1.280627 2.018477
## [2,] 1.295301 1.652912 1.519352 2.157357 2.276621
## [3,] 2.034078 1.880312 1.708836 1.809829 1.463338
## [4,] 2.017609 1.980665 1.390584 1.210707 2.110750
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 0.7803453
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 0.7803453
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.1909438 0.4341251 0.7081473
## [2,] 0.5166160 0.4437225 0.7078562
## [3,] 0.9033564 0.4308212 0.1456790
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.1909438 0.4341251 0.7081473
## [2,] 0.5166160 0.4437225 0.7078562
## [3,] 0.9033564 0.4308212 0.1456790
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.8132550 0.3200369 0.2757874
## [2,] 0.5746659 0.7184173 0.9841581
## [3,] 0.5903074 0.2195803 0.6148697
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.02381962 0.5024171 0.5596160
## [2,] 0.50810927 0.4578509 0.0418544
## [3,] 0.84812526 0.1773667 0.6736256
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.29778203 0.2860081 0.000100988
## [2,] 0.87173341 0.8286706 0.183804017
## [3,] 0.09578592 0.2508351 0.963521205
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.8132550 0.3200369 0.2757874
## [2,] 0.5746659 0.7184173 0.9841581
## [3,] 0.5903074 0.2195803 0.6148697
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.02381962 0.50241707 0.55961596
## [2,] 0.50810927 0.45785094 0.04185440
## [3,] 0.84812526 0.17736666 0.67362557
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.297782029 0.286008085 0.000100988
## [2,] 0.871733411 0.828670569 0.183804017
## [3,] 0.095785923 0.250835104 0.963521205
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.195363
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.195363
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 4.381824
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 4.381824
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 23.07325
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 23.07325
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.5154670 1.014182 0.5566674 0.5631298 1.434491
## [2,] 0.8872934 1.064143 0.8737610 1.6822017 1.781366
## [3,] 1.4031355 1.229004 1.1244628 1.2041283 1.009638
## [4,] 1.6729301 1.359410 0.8017137 0.9241511 1.971972
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.5154670 1.0141824 0.5566674 0.5631298 1.4344914
## [2,] 0.8872934 1.0641434 0.8737610 1.6822017 1.7813657
## [3,] 1.4031355 1.2290042 1.1244628 1.2041283 1.0096379
## [4,] 1.6729301 1.3594100 0.8017137 0.9241511 1.9719721
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.627479 1.378410 1.231931
## [2,] 1.378410 1.594172 1.278226
## [3,] 1.231931 1.278226 1.160174
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.627479 1.378410 1.231931
## [2,] 1.378410 1.594172 1.278226
## [3,] 1.231931 1.278226 1.160174
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.78281096 0.83463323 0.3408729
## [2,] 0.03262454 0.46001067 0.3671893
## [3,] 0.20701546 0.20174068 0.1149248
## [4,] 0.60502811 0.09778697 0.3371867
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.78281096 0.83463323 0.34087288
## [2,] 0.03262454 0.46001067 0.36718929
## [3,] 0.20701546 0.20174068 0.11492482
## [4,] 0.60502811 0.09778697 0.33718668
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.08758909 0.1460928 0.05745981 0.27467518 0.20061275
## [2,] 0.20435480 0.1926481 0.46434600 0.96603088 0.87293200
## [3,] 0.43984657 0.5398061 0.06911423 0.16403340 0.09078734
## [4,] 0.70408768 0.5663668 0.12529229 0.08136912 0.01793215
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.33389441 0.8600953 0.11278665 0.1697948 0.5916207
## [2,] 0.68264892 0.7506891 0.34783986 0.4931761 0.5365922
## [3,] 0.33257685 0.4917579 0.62980193 0.7494627 0.8650507
## [4,] 0.04789516 0.5678798 0.06479721 0.8427264 0.9634598
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0939835412 0.007994283 0.38642094 1.186598e-01 0.64225793
## [2,] 0.0002896611 0.120806241 0.06157514 2.229947e-01 0.37184144
## [3,] 0.6307120766 0.197440246 0.42554666 2.906322e-01 0.05379986
## [4,] 0.9209472632 0.225163410 0.61162417 5.555096e-05 0.99058019
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.08758909 0.14609283 0.05745981 0.27467518 0.20061275
## [2,] 0.20435480 0.19264807 0.46434600 0.96603088 0.87293200
## [3,] 0.43984657 0.53980609 0.06911423 0.16403340 0.09078734
## [4,] 0.70408768 0.56636678 0.12529229 0.08136912 0.01793215
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.33389441 0.86009527 0.11278665 0.16979485 0.59162073
## [2,] 0.68264892 0.75068912 0.34783986 0.49317613 0.53659223
## [3,] 0.33257685 0.49175789 0.62980193 0.74946272 0.86505070
## [4,] 0.04789516 0.56787985 0.06479721 0.84272639 0.96345979
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 9.398354e-02 7.994283e-03 3.864209e-01 1.186598e-01 6.422579e-01
## [2,] 2.896611e-04 1.208062e-01 6.157514e-02 2.229947e-01 3.718414e-01
## [3,] 6.307121e-01 1.974402e-01 4.255467e-01 2.906322e-01 5.379986e-02
## [4,] 9.209473e-01 2.251634e-01 6.116242e-01 5.555096e-05 9.905802e-01
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 2.250319 2.199606 2.077421
## [2,] 2.308426 3.248669 1.355839
## [3,] 1.537999 1.973770 2.304174
## [4,] 2.197226 2.898043 1.363250
## [5,] 1.817427 3.413334 2.638425
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 2.250319 2.199606 2.077421
## [2,] 2.308426 3.248669 1.355839
## [3,] 1.537999 1.973770 2.304174
## [4,] 2.197226 2.898043 1.363250
## [5,] 1.817427 3.413334 2.638425
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.046354965 0.077317023 0.030409577 0.14536694 0.106170716
## [2,] 0.004507322 0.004249114 0.010241779 0.02130712 0.019253697
## [3,] 0.061559250 0.075549203 0.009672965 0.02295749 0.012706251
## [4,] 0.287999425 0.231666184 0.051249452 0.03328316 0.007334951
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.236974176 0.61043360 0.080047829 0.12050814 0.41988973
## [2,] 0.267030894 0.29364609 0.136064069 0.19291507 0.20989809
## [3,] 0.057053421 0.08436086 0.108042261 0.12857002 0.14839909
## [4,] 0.003982619 0.04722083 0.005388073 0.07007511 0.08011443
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 3.950672e-03 0.0003360459 0.016243506 4.987957e-03 0.026997813
## [2,] 1.311616e-05 0.0054702358 0.002788188 1.009744e-02 0.016837378
## [3,] 8.938644e-03 0.0027981835 0.006030977 4.118929e-03 0.000762468
## [4,] 3.829410e-02 0.0093625660 0.025432070 2.309876e-06 0.041189519
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.046354965 0.077317023 0.030409577 0.145366937 0.106170716
## [2,] 0.004507322 0.004249114 0.010241779 0.021307118 0.019253697
## [3,] 0.061559250 0.075549203 0.009672965 0.022957489 0.012706251
## [4,] 0.287999425 0.231666184 0.051249452 0.033283155 0.007334951
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.236974176 0.610433603 0.080047829 0.120508139 0.419889731
## [2,] 0.267030894 0.293646093 0.136064069 0.192915068 0.209898087
## [3,] 0.057053421 0.084360864 0.108042261 0.128570019 0.148399090
## [4,] 0.003982619 0.047220832 0.005388073 0.070075107 0.080114434
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 3.950672e-03 3.360459e-04 1.624351e-02 4.987957e-03 2.699781e-02
## [2,] 1.311616e-05 5.470236e-03 2.788188e-03 1.009744e-02 1.683738e-02
## [3,] 8.938644e-03 2.798183e-03 6.030977e-03 4.118929e-03 7.624680e-04
## [4,] 3.829410e-02 9.362566e-03 2.543207e-02 2.309876e-06 4.118952e-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.1 (2024-06-14)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.1 LTS
##
## Matrix products: default
## BLAS: /media/volume/teran2_disk/biocbuild/bbs-3.20-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.13.1
## [3] HDF5Array_1.33.8 rhdf5_2.49.0
## [5] DelayedArray_0.31.14 SparseArray_1.5.45
## [7] S4Arrays_1.5.11 abind_1.4-8
## [9] IRanges_2.39.2 S4Vectors_0.43.2
## [11] MatrixGenerics_1.17.0 matrixStats_1.4.1
## [13] BiocGenerics_0.51.3 Matrix_1.7-1
## [15] DelayedTensor_1.11.1 BiocStyle_2.33.1
##
## loaded via a namespace (and not attached):
## [1] jsonlite_1.8.9 compiler_4.4.1 BiocManager_1.30.25
## [4] crayon_1.5.3 rsvd_1.0.5 Rcpp_1.0.13
## [7] rhdf5filters_1.17.0 parallel_4.4.1 jquerylib_0.1.4
## [10] BiocParallel_1.39.0 yaml_2.3.10 fastmap_1.2.0
## [13] lattice_0.22-6 R6_2.5.1 XVector_0.45.0
## [16] ScaledMatrix_1.13.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.27.0 BiocSingular_1.21.4 zlibbioc_1.51.2
## [28] digest_0.6.37 grid_4.4.1 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.21.8
## [37] rmarkdown_2.28 tools_4.4.1 htmltools_0.5.8.1