DelayedTensor 1.14.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2025-04-15 15:16:31
Compiled: Tue Apr 15 17:20:55 2025
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.5291650 0.6517463 0.5105248
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.5291650 0.6517463 0.5105248
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.5510719 0.06027873 0.2346374 0.8162218
## [2,] 0.8189063 0.76347951 0.9003373 0.3073543
## [3,] 0.5951697 0.90207801 0.7656247 0.4911158
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.55107193 0.06027873 0.23463744 0.81622183
## [2,] 0.81890630 0.76347951 0.90033730 0.30735431
## [3,] 0.59516975 0.90207801 0.76562473 0.49111577
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4204118 0.0395915 0.3622171 0.8993911
## [2,] 0.6869660 0.7541770 0.7518913 0.7166676
## [3,] 0.5135680 0.6753195 0.1685609 0.5436723
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2535022 0.6425239 0.1979545 0.1199807
## [2,] 0.9965175 0.8814795 0.7027424 0.6767459
## [3,] 0.5711284 0.2966626 0.3354523 0.8490769
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7594838 0.07840451 0.09586951 0.9398888
## [2,] 0.3372053 0.19482604 0.49234997 0.9310860
## [3,] 0.4798003 0.99250938 0.11458648 0.6492069
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8342206 0.1571810 0.3074656 0.6765367
## [2,] 0.9337893 0.4284235 0.5406947 0.7668308
## [3,] 0.2578577 0.6511681 0.1878746 0.8932032
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4852816 0.4140453 0.13027265 0.3603317
## [2,] 0.7946581 0.3402238 0.04888277 0.8957135
## [3,] 0.8996885 0.6620054 0.17414496 0.9542818
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.4204118 0.0395915 0.3622171 0.8993911
## [2,] 0.6869660 0.7541770 0.7518913 0.7166676
## [3,] 0.5135680 0.6753195 0.1685609 0.5436723
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.2535022 0.6425239 0.1979545 0.1199807
## [2,] 0.9965175 0.8814795 0.7027424 0.6767459
## [3,] 0.5711284 0.2966626 0.3354523 0.8490769
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.75948379 0.07840451 0.09586951 0.93988875
## [2,] 0.33720533 0.19482604 0.49234997 0.93108602
## [3,] 0.47980025 0.99250938 0.11458648 0.64920685
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.8342206 0.1571810 0.3074656 0.6765367
## [2,] 0.9337893 0.4284235 0.5406947 0.7668308
## [3,] 0.2578577 0.6511681 0.1878746 0.8932032
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.48528155 0.41404527 0.13027265 0.36033166
## [2,] 0.79465805 0.34022377 0.04888277 0.89571349
## [3,] 0.89968847 0.66200536 0.17414496 0.95428180
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.5221201 0.2558024 0.1523851
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.5221201 0.2558024 0.1523851
einsum::einsum('iii->i', arrD)
## [1] 0.5148198 0.4073956 0.2147752
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.5148198 0.4073956 0.2147752
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.2800156 0.4247732 0.2606355
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.2800156 0.4247732 0.2606355
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.3036803 0.003633526 0.05505473 0.66621807
## [2,] 0.6706075 0.582900969 0.81060726 0.09446667
## [3,] 0.3542270 0.813744732 0.58618123 0.24119470
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.303680267 0.003633526 0.055054728 0.666218070
## [2,] 0.670607528 0.582900969 0.810607256 0.094466672
## [3,] 0.354227026 0.813744732 0.586181227 0.241194702
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1767461 0.001567487 0.13120124 0.8089044
## [2,] 0.4719222 0.568782882 0.56534056 0.5136124
## [3,] 0.2637521 0.456056367 0.02841277 0.2955796
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06426335 0.41283702 0.03918597 0.01439538
## [2,] 0.99304720 0.77700619 0.49384687 0.45798504
## [3,] 0.32618768 0.08800871 0.11252824 0.72093162
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5768156 0.006147268 0.009190964 0.8833909
## [2,] 0.1137074 0.037957185 0.242408493 0.8669212
## [3,] 0.2302083 0.985074877 0.013130062 0.4214695
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.69592403 0.02470586 0.09453507 0.4577020
## [2,] 0.87196252 0.18354673 0.29235076 0.5880294
## [3,] 0.06649059 0.42401990 0.03529686 0.7978119
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2354982 0.1714335 0.016970962 0.1298389
## [2,] 0.6314814 0.1157522 0.002389526 0.8023026
## [3,] 0.8094393 0.4382511 0.030326467 0.9106537
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.176746060 0.001567487 0.131201238 0.808904373
## [2,] 0.471922217 0.568782882 0.565340563 0.513612384
## [3,] 0.263752133 0.456056367 0.028412771 0.295579554
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.06426335 0.41283702 0.03918597 0.01439538
## [2,] 0.99304720 0.77700619 0.49384687 0.45798504
## [3,] 0.32618768 0.08800871 0.11252824 0.72093162
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.576815623 0.006147268 0.009190964 0.883390867
## [2,] 0.113707438 0.037957185 0.242408493 0.866921178
## [3,] 0.230208282 0.985074877 0.013130062 0.421469538
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.69592403 0.02470586 0.09453507 0.45770197
## [2,] 0.87196252 0.18354673 0.29235076 0.58802944
## [3,] 0.06649059 0.42401990 0.03529686 0.79781194
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.235498183 0.171433485 0.016970962 0.129838903
## [2,] 0.631481418 0.115752216 0.002389526 0.802302650
## [3,] 0.809439336 0.438251091 0.030326467 0.910653745
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.2800156 0.3448813 0.2701518
## [2,] 0.3448813 0.4247732 0.3327326
## [3,] 0.2701518 0.3327326 0.2606355
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.2800156 0.3448813 0.2701518
## [2,] 0.3448813 0.4247732 0.3327326
## [3,] 0.2701518 0.3327326 0.2606355
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2316771 0.02534189 0.09864434 0.3431493
## [2,] 0.3442779 0.32097578 0.37851240 0.1292154
## [3,] 0.2502164 0.37924422 0.32187765 0.2064709
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3785676 0.04140944 0.1611879 0.5607166
## [2,] 0.5625607 0.52448443 0.6185011 0.2111419
## [3,] 0.4088614 0.61969688 0.5259581 0.3373798
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2830129 0.03095723 0.1205023 0.4191854
## [2,] 0.4205641 0.39209868 0.4623845 0.1578474
## [3,] 0.3056602 0.46327844 0.3932004 0.2522214
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02181776 0.002386525 0.009289647 0.03231544
## [2,] 0.03242173 0.030227297 0.035645701 0.01216862
## [3,] 0.02356366 0.035714619 0.030312229 0.01944401
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4156057 0.04546083 0.1769581 0.6155757
## [2,] 0.6176003 0.57579866 0.6790136 0.2317995
## [3,] 0.4488633 0.68032645 0.5774165 0.3703882
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3721496 0.0407074 0.1584552 0.5512105
## [2,] 0.5530234 0.5155926 0.6080153 0.2075623
## [3,] 0.4019297 0.6091908 0.5170413 0.3316600
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1996077 0.02183399 0.0849897 0.2956495
## [2,] 0.2966219 0.27654535 0.3261176 0.1113290
## [3,] 0.2155807 0.32674809 0.2773224 0.1778905
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4143462 0.04532306 0.1764219 0.6137101
## [2,] 0.6157285 0.57405362 0.6769558 0.2310970
## [3,] 0.4475030 0.67826463 0.5756666 0.3692657
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09288917 0.01016064 0.03955069 0.13758307
## [2,] 0.13803557 0.12869278 0.15176165 0.05180791
## [3,] 0.10032234 0.15205506 0.12905438 0.08278291
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4956292 0.05421416 0.2110308 0.7341027
## [2,] 0.7365170 0.68666669 0.8097554 0.2764317
## [3,] 0.5352904 0.81132094 0.6885961 0.4417052
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3949354 0.04319981 0.1681570 0.5849597
## [2,] 0.5868836 0.54716100 0.6452425 0.2202709
## [3,] 0.4265388 0.64649004 0.5486984 0.3519667
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2996025 0.03277188 0.1275659 0.4437572
## [2,] 0.4452167 0.41508265 0.4894884 0.1671000
## [3,] 0.3235773 0.49043481 0.4162489 0.2670060
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1396979 0.01528079 0.0594811 0.20691400
## [2,] 0.2075945 0.19354371 0.2282375 0.07791498
## [3,] 0.1508768 0.22867872 0.1940875 0.12449891
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5491528 0.06006881 0.2338203 0.8133794
## [2,] 0.8160545 0.76082072 0.8972019 0.3062840
## [3,] 0.5930971 0.89893655 0.7629585 0.4894055
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3147328 0.0344269 0.1340081 0.4661675
## [2,] 0.4677007 0.4360449 0.5142082 0.1755388
## [3,] 0.3399184 0.5152024 0.4372701 0.2804902
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3540769 0.03873053 0.1507602 0.5244421
## [2,] 0.5261669 0.49055387 0.5784883 0.1974825
## [3,] 0.3824108 0.57960672 0.4919322 0.3155536
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4857586 0.05313447 0.2068281 0.7194828
## [2,] 0.7218492 0.67299158 0.7936289 0.2709265
## [3,] 0.5246300 0.79516331 0.6748825 0.4329085
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1634824 0.01788245 0.06960816 0.24214250
## [2,] 0.2429389 0.22649583 0.26709642 0.09118053
## [3,] 0.1765646 0.26761282 0.22713223 0.14569569
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1090871 0.01193244 0.04644753 0.16157475
## [2,] 0.1621062 0.15113417 0.17822578 0.06084216
## [3,] 0.1178165 0.17857036 0.15155883 0.09721856
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3872616 0.04236042 0.1648897 0.5735937
## [2,] 0.5754802 0.53652942 0.6327052 0.2159909
## [3,] 0.4182510 0.63392846 0.5380370 0.3451279
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1848583 0.02022064 0.07870967 0.2738035
## [2,] 0.2747040 0.25611095 0.30202021 0.1031027
## [3,] 0.1996511 0.30260413 0.25683057 0.1647459
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06611801 0.007232286 0.02815197 0.09793089
## [2,] 0.09825297 0.091602828 0.10802313 0.03687659
## [3,] 0.07140890 0.108231977 0.09186021 0.05892443
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3729357 0.04079339 0.1587899 0.5523748
## [2,] 0.5541915 0.51668165 0.6092996 0.2080008
## [3,] 0.4027787 0.61047761 0.5181334 0.3323606
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4679025 0.05118128 0.1992252 0.6930351
## [2,] 0.6953144 0.64825284 0.7644556 0.2609675
## [3,] 0.5053449 0.76593362 0.6500743 0.4169951
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4185302 0.04578072 0.1782033 0.6199072
## [2,] 0.6219461 0.57985031 0.6837916 0.2334306
## [3,] 0.4520218 0.68511362 0.5814796 0.3729945
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1858244 0.02032631 0.0791210 0.2752344
## [2,] 0.2761396 0.25744937 0.3035985 0.1036415
## [3,] 0.2006944 0.30418552 0.2581727 0.1656069
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2644044 0.02892175 0.1125791 0.3916234
## [2,] 0.3929114 0.36631766 0.4319821 0.1474687
## [3,] 0.2855626 0.43281726 0.3673469 0.2356375
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04320653 0.004726125 0.01839663 0.06399548
## [2,] 0.06420595 0.059860241 0.07059051 0.02409797
## [3,] 0.04666400 0.070726988 0.06002844 0.03850569
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1073632 0.01174387 0.04571348 0.15902126
## [2,] 0.1595443 0.14874569 0.17540915 0.05988062
## [3,] 0.1159546 0.17574828 0.14916363 0.09568214
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5469441 0.05982721 0.2328799 0.8101078
## [2,] 0.8127722 0.75776058 0.8935932 0.3050520
## [3,] 0.5907116 0.89532089 0.7598897 0.4874370
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05283100 0.005778893 0.02249458 0.07825079
## [2,] 0.07850815 0.073194409 0.08631490 0.02946591
## [3,] 0.05705863 0.086481779 0.07340007 0.04708303
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2713202 0.02967823 0.1155237 0.4018668
## [2,] 0.4031885 0.37589912 0.4432810 0.1513259
## [3,] 0.2930318 0.44413808 0.3769553 0.2418008
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06314539 0.006907128 0.02688628 0.09352799
## [2,] 0.09383559 0.087484433 0.10316649 0.03521865
## [3,] 0.06819841 0.103365946 0.08773025 0.05627523
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5179463 0.0566553 0.2205331 0.7671577
## [2,] 0.7696808 0.7175858 0.8462169 0.2888789
## [3,] 0.5593933 0.8478530 0.7196021 0.4615942
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5130954 0.05612469 0.2184676 0.7599727
## [2,] 0.7624722 0.71086510 0.8382915 0.2861733
## [3,] 0.5541542 0.83991222 0.7128625 0.4572710
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3577597 0.03913337 0.1523282 0.5298968
## [2,] 0.5316396 0.49565613 0.5845051 0.1995365
## [3,] 0.3863883 0.58563522 0.4970488 0.3188357
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4597156 0.05028576 0.1957394 0.6809091
## [2,] 0.6831485 0.63691035 0.7510799 0.2564013
## [3,] 0.4965029 0.75253207 0.6386999 0.4096989
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5145851 0.05628764 0.2191019 0.7621792
## [2,] 0.7646860 0.71292903 0.8407254 0.2870042
## [3,] 0.5557632 0.84235082 0.7149322 0.4585987
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1420981 0.01554334 0.06050307 0.21046908
## [2,] 0.2111613 0.19686907 0.23215890 0.07925367
## [3,] 0.1534691 0.23260776 0.19742223 0.12663798
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08661803 0.009474671 0.03688055 0.12829456
## [2,] 0.12871650 0.120004466 0.14151591 0.04831025
## [3,] 0.09354937 0.141789515 0.12034165 0.07719406
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2360922 0.02582483 0.1005242 0.3496886
## [2,] 0.3508387 0.32709259 0.3857257 0.1316778
## [3,] 0.2549847 0.38647145 0.3280117 0.2104056
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3588405 0.03925159 0.1527884 0.5314976
## [2,] 0.5332457 0.49715351 0.5862709 0.2001393
## [3,] 0.3875556 0.58740443 0.4985504 0.3197989
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1694356 0.01853363 0.07214293 0.25096010
## [2,] 0.2517855 0.23474365 0.27682271 0.09450086
## [3,] 0.1829942 0.27735792 0.23540323 0.15100118
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2979617 0.03259239 0.1268672 0.4413268
## [2,] 0.4427783 0.41280933 0.4868076 0.1661848
## [3,] 0.3218051 0.48774880 0.4139692 0.2655437
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1035324 0.01132484 0.04408241 0.15334733
## [2,] 0.1538517 0.14343839 0.16915049 0.05774406
## [3,] 0.1118173 0.16947753 0.14384142 0.09226817
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3728204 0.04078078 0.1587408 0.5522041
## [2,] 0.5540202 0.51652195 0.6091113 0.2079365
## [3,] 0.4026542 0.61028892 0.5179733 0.3322579
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4225789 0.04622359 0.1799272 0.6259040
## [2,] 0.6279626 0.58545959 0.6904064 0.2356887
## [3,] 0.4563945 0.69174118 0.5871046 0.3766027
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4922192 0.05384116 0.2095789 0.7290519
## [2,] 0.7314497 0.68194234 0.8041841 0.2745298
## [3,] 0.5316075 0.80573895 0.6838584 0.4386662
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2674250 0.02925216 0.1138652 0.3960974
## [2,] 0.3974001 0.37050252 0.4369171 0.1491534
## [3,] 0.2888249 0.43776181 0.3715436 0.2383294
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4379137 0.04790098 0.1864565 0.6486172
## [2,] 0.6507505 0.60670514 0.7154603 0.2442416
## [3,] 0.4729564 0.71684355 0.6084099 0.3902691
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4957931 0.05423208 0.2111006 0.7343454
## [2,] 0.7367606 0.68689371 0.8100231 0.2765231
## [3,] 0.5354674 0.81158918 0.6888237 0.4418512
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2281687 0.02495812 0.09715052 0.3379528
## [2,] 0.3390643 0.31611508 0.37278040 0.1272586
## [3,] 0.2464272 0.37350113 0.31700330 0.2033442
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1874878 0.02050826 0.07982923 0.2776981
## [2,] 0.2786114 0.25975388 0.30631615 0.1045692
## [3,] 0.2024909 0.30690838 0.26048374 0.1670893
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3648126 0.03990484 0.1553312 0.5403432
## [2,] 0.5421204 0.50542753 0.5960281 0.2034702
## [3,] 0.3940056 0.59718047 0.5068477 0.3251213
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07178960 0.00785267 0.03056684 0.10633138
## [2,] 0.10668109 0.09946050 0.11728932 0.04003986
## [3,] 0.07753434 0.11751609 0.09973996 0.06397895
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02693792 0.002946592 0.01146973 0.03989919
## [2,] 0.04003041 0.037320997 0.04401099 0.01502433
## [3,] 0.02909355 0.044096076 0.03742586 0.02400710
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0959664 0.01049724 0.04086093 0.14214092
## [2,] 0.1426084 0.13295611 0.15678920 0.05352420
## [3,] 0.1036458 0.15709234 0.13332969 0.08552534
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1985687 0.02172034 0.0845473 0.2941106
## [2,] 0.2950779 0.27510584 0.3244200 0.1107495
## [3,] 0.2144585 0.32504726 0.2758788 0.1769646
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4936026 0.05399247 0.2101679 0.7311009
## [2,] 0.7335054 0.68385890 0.8064443 0.2753014
## [3,] 0.5331016 0.80800344 0.6857804 0.4398990
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5258779 0.0575229 0.2239102 0.7789056
## [2,] 0.7814674 0.7285746 0.8591755 0.2933026
## [3,] 0.5679597 0.8608366 0.7306217 0.4686628
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.23167713 0.02534189 0.09864434 0.34314927
## [2,] 0.34427785 0.32097578 0.37851240 0.12921537
## [3,] 0.25021637 0.37924422 0.32187765 0.20647085
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.37856765 0.04140944 0.16118793 0.56071660
## [2,] 0.56256074 0.52448443 0.61850107 0.21114195
## [3,] 0.40886135 0.61969688 0.52595812 0.33737981
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.28301293 0.03095723 0.12050229 0.41918544
## [2,] 0.42056410 0.39209868 0.46238446 0.15784735
## [3,] 0.30566016 0.46327844 0.39320039 0.25222137
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.19856866 0.02172034 0.08454730 0.29411056
## [2,] 0.29507786 0.27510584 0.32442003 0.11074949
## [3,] 0.21445850 0.32504726 0.27587883 0.17696456
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.49360256 0.05399247 0.21016792 0.73110090
## [2,] 0.73350542 0.68385890 0.80644426 0.27530140
## [3,] 0.53310157 0.80800344 0.68578040 0.43989902
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.5258779 0.0575229 0.2239102 0.7789056
## [2,] 0.7814674 0.7285746 0.8591755 0.2933026
## [3,] 0.5679597 0.8608366 0.7306217 0.4686628
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.691436
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.691436
einsum::einsum('ij->', arrC)
## [1] 7.206276
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 7.206276
einsum::einsum('ijk->', arrE)
## [1] 31.91619
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 31.91619
einsum::einsum('ij->i', arrC)
## [1] 1.662210 2.790077 2.753988
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 1.662210 2.790077 2.753988
einsum::einsum('ij->j', arrC)
## [1] 1.965148 1.725836 1.900599 1.614692
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 1.965148 1.725836 1.900599 1.614692
einsum::einsum('ijk->i', arrE)
## [1] 8.174554 12.871871 10.869768
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 8.174554 12.871871 10.869768
einsum::einsum('ijk->j', arrE)
## [1] 9.224079 7.208541 4.610960 10.872614
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 9.224079 7.208541 4.610960 10.872614
einsum::einsum('ijk->k', arrE)
## [1] 6.532434 6.523767 6.065217 6.635246 6.159530
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 6.532434 6.523767 6.065217 6.635246 6.159530
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 2.752900 1.331746 1.0937793 2.996129
## [2,] 3.749136 2.599130 2.5365612 3.987044
## [3,] 2.722043 3.277665 0.9806192 3.889441
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 2.7528999 1.3317462 1.0937793 2.9961290
## [2,] 3.7491362 2.5991299 2.5365612 3.9870438
## [3,] 2.7220429 3.2776649 0.9806192 3.8894410
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.620946 1.821148 1.576489 2.025868 2.1796281
## [2,] 1.469088 1.820666 1.265740 1.236773 1.4162744
## [3,] 1.282669 1.236149 0.702806 1.036035 0.3533004
## [4,] 2.159731 1.645804 2.520182 2.336571 2.2103269
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.6209458 1.8211481 1.5764894 2.0258676 2.1796281
## [2,] 1.4690879 1.8206661 1.2657399 1.2367726 1.4162744
## [3,] 1.2826693 1.2361491 0.7028060 1.0360348 0.3533004
## [4,] 2.1597310 1.6458036 2.5201816 2.3365707 2.2103269
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.620946 1.821148 1.576489 2.025868 2.1796281
## [2,] 1.469088 1.820666 1.265740 1.236773 1.4162744
## [3,] 1.282669 1.236149 0.702806 1.036035 0.3533004
## [4,] 2.159731 1.645804 2.520182 2.336571 2.2103269
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.6209458 1.8211481 1.5764894 2.0258676 2.1796281
## [2,] 1.4690879 1.8206661 1.2657399 1.2367726 1.4162744
## [3,] 1.2826693 1.2361491 0.7028060 1.0360348 0.3533004
## [4,] 2.1597310 1.6458036 2.5201816 2.3365707 2.2103269
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 0.9303077
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 0.9303077
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.5221201 0.9625834 0.6854062
## [2,] 0.7740344 0.2558024 0.7428081
## [3,] 0.9381218 0.3226553 0.1523851
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.5221201 0.9625834 0.6854062
## [2,] 0.7740344 0.2558024 0.7428081
## [3,] 0.9381218 0.3226553 0.1523851
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.5148198 0.1037203 0.2490509
## [2,] 0.6372394 0.1235482 0.9906757
## [3,] 0.4034584 0.5555055 0.4767506
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.4682090 0.6931005 0.2380944
## [2,] 0.2683164 0.4073956 0.8447501
## [3,] 0.9960132 0.7948983 0.6584886
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.3761126 0.6737600 0.7794815
## [2,] 0.4684923 0.3661319 0.7666195
## [3,] 0.3236282 0.3792525 0.2147752
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.5148198 0.1037203 0.2490509
## [2,] 0.6372394 0.1235482 0.9906757
## [3,] 0.4034584 0.5555055 0.4767506
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.4682090 0.6931005 0.2380944
## [2,] 0.2683164 0.4073956 0.8447501
## [3,] 0.9960132 0.7948983 0.6584886
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.3761126 0.6737600 0.7794815
## [2,] 0.4684923 0.3661319 0.7666195
## [3,] 0.3236282 0.3792525 0.2147752
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.9654243
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 0.9654243
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 5.182517
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 5.182517
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 21.99524
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 21.99524
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.9124204 1.3834982 0.9207313 1.6343771 1.67641894
## [2,] 1.0264067 1.2778519 1.0291793 0.6322725 0.72543679
## [3,] 0.7249546 0.6455611 0.2647295 0.4221827 0.04968696
## [4,] 1.6180963 1.1933120 2.1717816 1.8435433 1.84279530
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.91242041 1.38349823 0.92073134 1.63437714 1.67641894
## [2,] 1.02640674 1.27785192 1.02917933 0.63227249 0.72543679
## [3,] 0.72495457 0.64556107 0.26472952 0.42218269 0.04968696
## [4,] 1.61809631 1.19331204 2.17178158 1.84354335 1.84279530
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.0285866 0.959420 0.9628611
## [2,] 0.9594200 2.158582 2.0163734
## [3,] 0.9628611 2.016373 1.9953477
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.0285866 0.9594200 0.9628611
## [2,] 0.9594200 2.1585824 2.0163734
## [3,] 0.9628611 2.0163734 1.9953477
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.303680267 0.67060753 0.3542270
## [2,] 0.003633526 0.58290097 0.8137447
## [3,] 0.055054728 0.81060726 0.5861812
## [4,] 0.666218070 0.09446667 0.2411947
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.303680267 0.670607528 0.354227026
## [2,] 0.003633526 0.582900969 0.813744732
## [3,] 0.055054728 0.810607256 0.586181227
## [4,] 0.666218070 0.094466672 0.241194702
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.176746060 0.06426335 0.576815623 0.69592403 0.23549818
## [2,] 0.001567487 0.41283702 0.006147268 0.02470586 0.17143349
## [3,] 0.131201238 0.03918597 0.009190964 0.09453507 0.01697096
## [4,] 0.808904373 0.01439538 0.883390867 0.45770197 0.12983890
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.4719222 0.9930472 0.11370744 0.8719625 0.631481418
## [2,] 0.5687829 0.7770062 0.03795719 0.1835467 0.115752216
## [3,] 0.5653406 0.4938469 0.24240849 0.2923508 0.002389526
## [4,] 0.5136124 0.4579850 0.86692118 0.5880294 0.802302650
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.26375213 0.32618768 0.23020828 0.06649059 0.80943934
## [2,] 0.45605637 0.08800871 0.98507488 0.42401990 0.43825109
## [3,] 0.02841277 0.11252824 0.01313006 0.03529686 0.03032647
## [4,] 0.29557955 0.72093162 0.42146954 0.79781194 0.91065374
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.176746060 0.064263346 0.576815623 0.695924031 0.235498183
## [2,] 0.001567487 0.412837019 0.006147268 0.024705864 0.171433485
## [3,] 0.131201238 0.039185966 0.009190964 0.094535068 0.016970962
## [4,] 0.808904373 0.014395375 0.883390867 0.457701972 0.129838903
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.471922217 0.993047200 0.113707438 0.871962516 0.631481418
## [2,] 0.568782882 0.777006192 0.037957185 0.183546725 0.115752216
## [3,] 0.565340563 0.493846870 0.242408493 0.292350764 0.002389526
## [4,] 0.513612384 0.457985045 0.866921178 0.588029441 0.802302650
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.26375213 0.32618768 0.23020828 0.06649059 0.80943934
## [2,] 0.45605637 0.08800871 0.98507488 0.42401990 0.43825109
## [3,] 0.02841277 0.11252824 0.01313006 0.03529686 0.03032647
## [4,] 0.29557955 0.72093162 0.42146954 0.79781194 0.91065374
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 1.721611 2.909702 1.901121
## [2,] 1.213961 3.257485 2.052320
## [3,] 1.873647 1.955467 2.236103
## [4,] 1.975404 2.669738 1.990104
## [5,] 1.389931 2.079478 2.690121
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.721611 2.909702 1.901121
## [2,] 1.213961 3.257485 2.052320
## [3,] 1.873647 1.955467 2.236103
## [4,] 1.975404 2.669738 1.990104
## [5,] 1.389931 2.079478 2.690121
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,] 2.840256e-02 0.010326925 9.269252e-02 1.118329e-01 0.0378438447
## [2,] 3.013861e-06 0.000793776 1.181956e-05 4.750282e-05 0.0003296211
## [3,] 3.822290e-03 0.001141606 2.677607e-04 2.754093e-03 0.0004944157
## [4,] 2.851706e-01 0.005074935 3.114300e-01 1.613579e-01 0.0457733148
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.20626113 0.43402711 0.04969765 0.38110512 0.275999022
## [2,] 0.21608262 0.29518739 0.01442007 0.06973005 0.043974675
## [3,] 0.29867521 0.26090436 0.12806689 0.15445190 0.001262411
## [4,] 0.03162224 0.02819736 0.05337486 0.03620397 0.049396410
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.047697375 0.05898832 0.041631249 0.01202427 0.146380358
## [2,] 0.189462612 0.03656206 0.409236385 0.17615348 0.182065644
## [3,] 0.008502807 0.03367520 0.003929303 0.01056294 0.009075499
## [4,] 0.036396444 0.08877254 0.051898017 0.09823926 0.112134138
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,] 2.840256e-02 1.032692e-02 9.269252e-02 1.118329e-01 3.784384e-02
## [2,] 3.013861e-06 7.937760e-04 1.181956e-05 4.750282e-05 3.296211e-04
## [3,] 3.822290e-03 1.141606e-03 2.677607e-04 2.754093e-03 4.944157e-04
## [4,] 2.851706e-01 5.074935e-03 3.114300e-01 1.613579e-01 4.577331e-02
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.206261129 0.434027112 0.049697649 0.381105120 0.275999022
## [2,] 0.216082621 0.295187390 0.014420068 0.069730047 0.043974675
## [3,] 0.298675210 0.260904359 0.128066890 0.154451903 0.001262411
## [4,] 0.031622241 0.028197360 0.053374863 0.036203973 0.049396410
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.047697375 0.058988324 0.041631249 0.012024269 0.146380358
## [2,] 0.189462612 0.036562057 0.409236385 0.176153483 0.182065644
## [3,] 0.008502807 0.033675204 0.003929303 0.010562939 0.009075499
## [4,] 0.036396444 0.088772540 0.051898017 0.098239264 0.112134138
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.5.0 RC (2025-04-04 r88126)
## Platform: aarch64-apple-darwin20
## Running under: macOS Ventura 13.7.1
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.1
##
## locale:
## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## 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.16.0
## [3] HDF5Array_1.36.0 h5mread_1.0.0
## [5] rhdf5_2.52.0 DelayedArray_0.34.0
## [7] SparseArray_1.8.0 S4Arrays_1.8.0
## [9] abind_1.4-8 IRanges_2.42.0
## [11] S4Vectors_0.46.0 MatrixGenerics_1.20.0
## [13] matrixStats_1.5.0 BiocGenerics_0.54.0
## [15] generics_0.1.3 Matrix_1.7-3
## [17] DelayedTensor_1.14.0 BiocStyle_2.36.0
##
## loaded via a namespace (and not attached):
## [1] dqrng_0.4.1 sass_0.4.10 lattice_0.22-7
## [4] digest_0.6.37 evaluate_1.0.3 grid_4.5.0
## [7] bookdown_0.43 fastmap_1.2.0 jsonlite_2.0.0
## [10] BiocManager_1.30.25 codetools_0.2-20 jquerylib_0.1.4
## [13] cli_3.6.4 rlang_1.1.6 crayon_1.5.3
## [16] XVector_0.48.0 cachem_1.1.0 yaml_2.3.10
## [19] tools_4.5.0 beachmat_2.24.0 parallel_4.5.0
## [22] BiocParallel_1.42.0 Rhdf5lib_1.30.0 rsvd_1.0.5
## [25] R6_2.6.1 lifecycle_1.0.4 BiocSingular_1.24.0
## [28] irlba_2.3.5.1 ScaledMatrix_1.16.0 rTensor_1.4.8
## [31] bslib_0.9.0 Rcpp_1.0.14 xfun_0.52
## [34] knitr_1.50 rhdf5filters_1.20.0 htmltools_0.5.8.1
## [37] rmarkdown_2.29 compiler_4.5.0