DelayedTensor 1.15.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2025-04-04 14:56:50.301149
Compiled: Tue Apr 15 18:32:56 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.94031084 0.58340268 0.04509887
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.94031084 0.58340268 0.04509887
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.30968249 0.4967751 0.24336700 0.9148191
## [2,] 0.93956772 0.9044776 0.05473789 0.5294111
## [3,] 0.07704107 0.2968380 0.87240745 0.4551029
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.30968249 0.49677514 0.24336700 0.91481909
## [2,] 0.93956772 0.90447763 0.05473789 0.52941111
## [3,] 0.07704107 0.29683797 0.87240745 0.45510288
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4503509 0.02082869 0.1243390 0.3656413
## [2,] 0.4878888 0.31654715 0.5154390 0.7159968
## [3,] 0.3243015 0.72584752 0.8917379 0.9600144
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4050680 0.53413212 0.9571152 0.4557703
## [2,] 0.5850829 0.83561948 0.2001500 0.1110608
## [3,] 0.1527181 0.08844608 0.1970782 0.7128690
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.82143523 0.8662439 0.04586575 0.09785489
## [2,] 0.64011036 0.1635315 0.74631338 0.78282280
## [3,] 0.03549833 0.7802926 0.25611073 0.96192302
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2874220 0.9563886 0.4584351 0.2832160
## [2,] 0.6929565 0.9550607 0.9918725 0.8050627
## [3,] 0.1415264 0.3212254 0.8416267 0.7350452
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9632551 0.68948047 0.2856770 0.2567983
## [2,] 0.2132970 0.07784036 0.9628634 0.6083360
## [3,] 0.7698907 0.69239808 0.5546939 0.4350186
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.45035091 0.02082869 0.12433902 0.36564131
## [2,] 0.48788884 0.31654715 0.51543897 0.71599682
## [3,] 0.32430152 0.72584752 0.89173787 0.96001440
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.40506801 0.53413212 0.95711522 0.45577034
## [2,] 0.58508289 0.83561948 0.20015000 0.11106084
## [3,] 0.15271807 0.08844608 0.19707815 0.71286904
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.82143523 0.86624393 0.04586575 0.09785489
## [2,] 0.64011036 0.16353152 0.74631338 0.78282280
## [3,] 0.03549833 0.78029265 0.25611073 0.96192302
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.2874220 0.9563886 0.4584351 0.2832160
## [2,] 0.6929565 0.9550607 0.9918725 0.8050627
## [3,] 0.1415264 0.3212254 0.8416267 0.7350452
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.96325510 0.68948047 0.28567699 0.25679831
## [2,] 0.21329698 0.07784036 0.96286344 0.60833603
## [3,] 0.76989073 0.69239808 0.55469394 0.43501855
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.7223526 0.3996315 0.1528819
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.7223526 0.3996315 0.1528819
einsum::einsum('iii->i', arrD)
## [1] 0.8532357 0.5120755 0.8331471
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.8532357 0.5120755 0.8331471
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.884184478 0.340358686 0.002033908
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.884184478 0.340358686 0.002033908
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.095903242 0.24678554 0.059227497 0.8368940
## [2,] 0.882787494 0.81807979 0.002996237 0.2802761
## [3,] 0.005935327 0.08811278 0.761094754 0.2071186
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.095903242 0.246785543 0.059227497 0.836893967
## [2,] 0.882787494 0.818079787 0.002996237 0.280276124
## [3,] 0.005935327 0.088112781 0.761094754 0.207118634
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2028159 0.0004338344 0.01546019 0.1336936
## [2,] 0.2380355 0.1002020960 0.26567733 0.5126514
## [3,] 0.1051715 0.5268546181 0.79519643 0.9216276
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.16408010 0.28529712 0.91606954 0.20772660
## [2,] 0.34232199 0.69825992 0.04006002 0.01233451
## [3,] 0.02332281 0.00782271 0.03883980 0.50818227
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.674755829 0.75037855 0.002103667 0.009575579
## [2,] 0.409741278 0.02674256 0.556983657 0.612811532
## [3,] 0.001260131 0.60885662 0.065592706 0.925295905
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08261140 0.9146791 0.2101628 0.08021131
## [2,] 0.48018868 0.9121409 0.9838111 0.64812594
## [3,] 0.02002973 0.1031858 0.7083356 0.54029142
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9278604 0.475383316 0.08161134 0.06594537
## [2,] 0.0454956 0.006059121 0.92710600 0.37007272
## [3,] 0.5927317 0.479415102 0.30768537 0.18924114
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.2028159394 0.0004338344 0.0154601912 0.1336935654
## [2,] 0.2380355177 0.1002020960 0.2656773349 0.5126514409
## [3,] 0.1051714751 0.5268546181 0.7951964262 0.9216276407
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.16408010 0.28529712 0.91606954 0.20772660
## [2,] 0.34232199 0.69825992 0.04006002 0.01233451
## [3,] 0.02332281 0.00782271 0.03883980 0.50818227
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.674755829 0.750378555 0.002103667 0.009575579
## [2,] 0.409741278 0.026742559 0.556983657 0.612811532
## [3,] 0.001260131 0.608856620 0.065592706 0.925295905
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.08261140 0.91467912 0.21016276 0.08021131
## [2,] 0.48018868 0.91214091 0.98381106 0.64812594
## [3,] 0.02002973 0.10318578 0.70833558 0.54029142
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.927860379 0.475383316 0.081611343 0.065945371
## [2,] 0.045495601 0.006059121 0.927106001 0.370072720
## [3,] 0.592731741 0.479415102 0.307685366 0.189241139
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.88418448 0.5485799 0.042406952
## [2,] 0.54857986 0.3403587 0.026310799
## [3,] 0.04240695 0.0263108 0.002033908
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.884184478 0.548579864 0.042406952
## [2,] 0.548579864 0.340358686 0.026310799
## [3,] 0.042406952 0.026310799 0.002033908
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13946579 0.2237231 0.10960055 0.4119896
## [2,] 0.42313517 0.4073323 0.02465126 0.2384208
## [3,] 0.03469552 0.1336812 0.39288949 0.2049560
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.15109063 0.2423710 0.11873604 0.4463300
## [2,] 0.45840460 0.4412845 0.02670601 0.2582938
## [3,] 0.03758748 0.1448239 0.42563786 0.2220396
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10043050 0.1611049 0.07892429 0.2966772
## [2,] 0.30470324 0.2933235 0.01775158 0.1716888
## [3,] 0.02498454 0.0962650 0.28292306 0.1475906
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.006450281 0.010347176 0.005069016 0.019054484
## [2,] 0.019569965 0.018839085 0.001140119 0.011026940
## [3,] 0.001604665 0.006182746 0.018171105 0.009479197
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09802911 0.15725275 0.07703713 0.2895834
## [2,] 0.29741748 0.28630981 0.01732712 0.1675836
## [3,] 0.02438713 0.09396321 0.27615809 0.1440615
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.22478226 0.3605830 0.17664733 0.6640192
## [2,] 0.68198289 0.6565128 0.03973136 0.3842717
## [3,] 0.05592007 0.2154591 0.63323478 0.3303353
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.038505616 0.06176853 0.030260014 0.11374771
## [2,] 0.116824926 0.11246186 0.006806056 0.06582646
## [3,] 0.009579211 0.03690854 0.108474284 0.05658705
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.15962242 0.2560573 0.12544084 0.4715334
## [2,] 0.48428982 0.4662030 0.02821404 0.2728791
## [3,] 0.03970997 0.1530019 0.44967280 0.2345778
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.27615560 0.4429932 0.21701957 0.8157788
## [2,] 0.83784811 0.8065570 0.04881185 0.4720959
## [3,] 0.06870044 0.2647017 0.77795876 0.4058325
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1132327 0.1816415 0.08898503 0.3344956
## [2,] 0.3435448 0.3307144 0.02001443 0.1935746
## [3,] 0.0281694 0.1085362 0.31898820 0.1664044
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.22173167 0.3556894 0.17425000 0.6550076
## [2,] 0.67272749 0.6476031 0.03919216 0.3790567
## [3,] 0.05516116 0.2125350 0.62464095 0.3258522
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.29729965 0.4769113 0.23363582 0.8782395
## [2,] 0.90199853 0.8683115 0.05254917 0.5082423
## [3,] 0.07396054 0.2849687 0.83752371 0.4369053
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12544247 0.2012277 0.09858019 0.3705640
## [2,] 0.38058883 0.3663750 0.02217257 0.2144475
## [3,] 0.03120687 0.1202396 0.35338435 0.1843476
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.18118992 0.2906546 0.1423899 0.5352450
## [2,] 0.54972500 0.5291944 0.0320262 0.3097494
## [3,] 0.04507541 0.1736748 0.5104307 0.2662729
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04729411 0.07586654 0.037166539 0.13970941
## [2,] 0.14348897 0.13813008 0.008359466 0.08085064
## [3,] 0.01176556 0.04533252 0.133232384 0.06950243
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.16541136 0.2653436 0.12999013 0.4886343
## [2,] 0.50185329 0.4831106 0.02923727 0.2827755
## [3,] 0.04115011 0.1585507 0.46598084 0.2430851
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.25877672 0.4151150 0.20336221 0.7644407
## [2,] 0.78512109 0.7557991 0.04574005 0.4423862
## [3,] 0.06437702 0.2480436 0.72900066 0.3802928
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.027390203 0.04393782 0.021524858 0.08091217
## [2,] 0.083101084 0.07999750 0.004841352 0.04682434
## [3,] 0.006813981 0.02625416 0.077161022 0.04025207
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.29640182 0.4754711 0.23293026 0.8755873
## [2,] 0.89927456 0.8656893 0.05239047 0.5067074
## [3,] 0.07373718 0.2841081 0.83499444 0.4355859
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06198295 0.09942955 0.04870991 0.18310104
## [2,] 0.18805448 0.18103120 0.01095579 0.10596164
## [3,] 0.01541977 0.05941212 0.17461235 0.09108884
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06103165 0.09790353 0.04796232 0.18029085
## [2,] 0.18516827 0.17825278 0.01078764 0.10433536
## [3,] 0.01518311 0.05850028 0.17193245 0.08969083
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.14114409 0.2264154 0.11091946 0.4169474
## [2,] 0.42822710 0.4122341 0.02494791 0.2412899
## [3,] 0.03511304 0.1352899 0.39761744 0.2074224
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.034393597 0.05517226 0.027028543 0.10160058
## [2,] 0.104349179 0.10045204 0.006079236 0.05879684
## [3,] 0.008556246 0.03296707 0.096890303 0.05054411
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2207631 0.3541356 0.17348880 0.6521462
## [2,] 0.6697887 0.6447741 0.03902095 0.3774008
## [3,] 0.0549202 0.2116066 0.62191226 0.3244288
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.25438410 0.4080686 0.19991023 0.7514646
## [2,] 0.77179402 0.7429698 0.04496363 0.4348769
## [3,] 0.06328425 0.2438332 0.71662621 0.3738375
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.19823097 0.3179909 0.15578174 0.5855852
## [2,] 0.60142703 0.5789655 0.03503829 0.3388815
## [3,] 0.04931479 0.1900091 0.55843705 0.2913161
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.010993210 0.01763469 0.008639121 0.03247455
## [2,] 0.033353081 0.03210744 0.001943104 0.01879321
## [3,] 0.002734829 0.01053725 0.030969004 0.01615539
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.26826058 0.4303285 0.21081519 0.7924565
## [2,] 0.81389484 0.7834983 0.04741637 0.4585992
## [3,] 0.06673636 0.2571341 0.75571766 0.3942301
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05064285 0.08123840 0.039798176 0.14960176
## [2,] 0.15364894 0.14791060 0.008951371 0.08657541
## [3,] 0.01259864 0.04854237 0.142666118 0.07442367
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.24164297 0.3876300 0.18989748 0.7138266
## [2,] 0.73313778 0.7057572 0.04271158 0.4130956
## [3,] 0.06011458 0.2316205 0.68073312 0.3551134
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.014203820 0.02278497 0.011162211 0.04195887
## [2,] 0.043093980 0.04148455 0.002510595 0.02428184
## [3,] 0.003533547 0.01361470 0.040013624 0.02087364
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.23112018 0.3707499 0.18162805 0.6827417
## [2,] 0.70121196 0.6750238 0.04085162 0.3951066
## [3,] 0.05749678 0.2215341 0.65108935 0.3396494
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07931301 0.12722944 0.06232890 0.2342950
## [2,] 0.24063337 0.23164643 0.01401896 0.1355879
## [3,] 0.01973105 0.07602339 0.22343291 0.1165567
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.030303945 0.04861188 0.02381465 0.08951952
## [2,] 0.091941295 0.08850756 0.00535637 0.05180547
## [3,] 0.007538846 0.02904705 0.08536933 0.04453404
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.24242651 0.3888869 0.19051324 0.7161412
## [2,] 0.73551503 0.7080457 0.04285007 0.4144351
## [3,] 0.06030951 0.2323715 0.68294044 0.3562649
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.29789071 0.4778594 0.23410032 0.8799855
## [2,] 0.90379182 0.8700379 0.05265364 0.5092527
## [3,] 0.07410758 0.2855353 0.83918881 0.4377739
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08900956 0.14278410 0.06994903 0.2629391
## [2,] 0.27005242 0.25996676 0.01573287 0.1521644
## [3,] 0.02214330 0.08531776 0.25074908 0.1308066
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.21459649 0.3442436 0.16864274 0.6339298
## [2,] 0.65107954 0.6267636 0.03793098 0.3668589
## [3,] 0.05338611 0.2056958 0.60454039 0.3153665
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04382825 0.07030681 0.034442859 0.12947107
## [2,] 0.13297365 0.12800748 0.007746858 0.07492566
## [3,] 0.01090335 0.04201041 0.123468700 0.06440908
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2961768 0.4751101 0.2327534 0.8749225
## [2,] 0.8985918 0.8650321 0.0523507 0.5063227
## [3,] 0.0736812 0.2838924 0.8343605 0.4352552
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2957656 0.4744504 0.23243025 0.8737077
## [2,] 0.8973442 0.8638310 0.05227801 0.5056197
## [3,] 0.0735789 0.2834983 0.83320205 0.4346509
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09947789 0.15957681 0.07817567 0.2938632
## [2,] 0.30181305 0.29054123 0.01758320 0.1700603
## [3,] 0.02474755 0.09535191 0.28023947 0.1461906
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.14196933 0.2277392 0.11156798 0.4193852
## [2,] 0.43073083 0.4146443 0.02509377 0.2427006
## [3,] 0.03531833 0.1360809 0.39994221 0.2086351
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.30716554 0.4927376 0.24138904 0.9073839
## [2,] 0.93193138 0.8971265 0.05429301 0.5251083
## [3,] 0.07641492 0.2944254 0.86531696 0.4514040
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.26063706 0.4180992 0.20482418 0.7699362
## [2,] 0.79076532 0.7612326 0.04606888 0.4455666
## [3,] 0.06483983 0.2498268 0.73424144 0.3830268
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08770704 0.14069467 0.06892543 0.2590914
## [2,] 0.26610062 0.25616255 0.01550265 0.1499377
## [3,] 0.02181927 0.08406927 0.24707976 0.1288924
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.24931382 0.3999351 0.19592569 0.7364867
## [2,] 0.75641092 0.7281612 0.04406744 0.4262091
## [3,] 0.06202289 0.2389732 0.70234269 0.3663864
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.22763062 0.3651522 0.17888574 0.6724334
## [2,] 0.69062472 0.6648319 0.04023482 0.3891411
## [3,] 0.05662867 0.2181893 0.64125889 0.3345212
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.29830323 0.4785212 0.23442450 0.8812041
## [2,] 0.90504339 0.8712427 0.05272655 0.5099580
## [3,] 0.07421021 0.2859307 0.84035092 0.4383802
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06605434 0.10596064 0.05190945 0.19512815
## [2,] 0.20040695 0.19292235 0.01167543 0.11292179
## [3,] 0.01643263 0.06331464 0.18608187 0.09707207
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.23842168 0.3824626 0.1873660 0.7043107
## [2,] 0.72336448 0.6963489 0.0421422 0.4075887
## [3,] 0.05931321 0.2285328 0.6716584 0.3503795
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.21352003 0.3425168 0.16779679 0.6307499
## [2,] 0.64781359 0.6236197 0.03774071 0.3650186
## [3,] 0.05311832 0.2046640 0.60150790 0.3137845
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.024105795 0.03866915 0.018943774 0.07120984
## [2,] 0.073136285 0.07040486 0.004260817 0.04120955
## [3,] 0.005996905 0.02310597 0.067908506 0.03542537
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.21442356 0.3439662 0.16850684 0.6334190
## [2,] 0.65055488 0.6262586 0.03790041 0.3665632
## [3,] 0.05334309 0.2055300 0.60405324 0.3151124
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08846916 0.14191723 0.06952435 0.2613428
## [2,] 0.26841288 0.25838845 0.01563736 0.1512406
## [3,] 0.02200886 0.08479978 0.24922673 0.1300124
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.29818194 0.4783266 0.23432919 0.8808459
## [2,] 0.90467540 0.8708884 0.05270512 0.5097506
## [3,] 0.07418003 0.2858144 0.84000923 0.4382019
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.17177900 0.2755582 0.13499420 0.5074446
## [2,] 0.52117252 0.5017083 0.03036278 0.2936611
## [3,] 0.04273422 0.1646542 0.48391912 0.2524428
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07952594 0.12757102 0.06249623 0.2349240
## [2,] 0.24127940 0.23226832 0.01405660 0.1359519
## [3,] 0.01978402 0.07622749 0.22403276 0.1168697
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.18839101 0.3022062 0.14804891 0.5565174
## [2,] 0.57157289 0.5502263 0.03329903 0.3220599
## [3,] 0.04686686 0.1805772 0.53071688 0.2768555
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1347176 0.2161064 0.1058692 0.3979633
## [2,] 0.4087294 0.3934645 0.0238120 0.2303037
## [3,] 0.0335143 0.1291300 0.3795134 0.1979782
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.13946579 0.22372314 0.10960055 0.41198961
## [2,] 0.42313517 0.40733232 0.02465126 0.23842077
## [3,] 0.03469552 0.13368125 0.39288949 0.20495600
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.15109063 0.24237105 0.11873604 0.44633002
## [2,] 0.45840460 0.44128454 0.02670601 0.25829377
## [3,] 0.03758748 0.14482393 0.42563786 0.22203962
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.10043050 0.16110493 0.07892429 0.29667722
## [2,] 0.30470324 0.29332347 0.01775158 0.17168883
## [3,] 0.02498454 0.09626500 0.28292306 0.14759056
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.07952594 0.12757102 0.06249623 0.23492399
## [2,] 0.24127940 0.23226832 0.01405660 0.13595188
## [3,] 0.01978402 0.07622749 0.22403276 0.11686965
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.18839101 0.30220622 0.14804891 0.55651741
## [2,] 0.57157289 0.55022633 0.03329903 0.32205985
## [3,] 0.04686686 0.18057723 0.53071688 0.27685548
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.1347176 0.2161064 0.1058692 0.3979633
## [2,] 0.4087294 0.3934645 0.0238120 0.2303037
## [3,] 0.0335143 0.1291300 0.3795134 0.1979782
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.568812
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.568812
einsum::einsum('ij->', arrC)
## [1] 6.094227
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 6.094227
einsum::einsum('ijk->', arrE)
## [1] 31.31143
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 31.31143
einsum::einsum('ij->i', arrC)
## [1] 1.964644 2.428194 1.701389
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 1.964644 2.428194 1.701389
einsum::einsum('ij->j', arrC)
## [1] 1.326291 1.698091 1.170512 1.899333
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 1.326291 1.698091 1.170512 1.899333
einsum::einsum('ijk->i', arrE)
## [1] 9.325318 11.407852 10.578262
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 9.325318 11.407852 10.578262
einsum::einsum('ijk->j', arrE)
## [1] 6.970802 8.023883 8.029318 8.287430
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 6.970802 8.023883 8.029318 8.287430
einsum::einsum('ijk->k', arrE)
## [1] 5.898933 5.235110 6.198003 7.469838 6.509549
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 5.898933 5.235110 6.198003 7.469838 6.509549
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 2.927531 3.067074 1.871432 1.459281
## [2,] 2.619336 2.348599 3.416638 3.023279
## [3,] 1.423935 2.608210 2.741247 3.804870
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 2.927531 3.067074 1.871432 1.459281
## [2,] 2.619336 2.348599 3.416638 3.023279
## [3,] 1.423935 2.608210 2.741247 3.804870
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.262541 1.142869 1.497044 1.121905 1.946443
## [2,] 1.063223 1.458198 1.810068 2.232675 1.459719
## [3,] 1.531516 1.354343 1.048290 2.291934 1.803234
## [4,] 2.041653 1.279700 1.842601 1.823324 1.300153
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.262541 1.142869 1.497044 1.121905 1.946443
## [2,] 1.063223 1.458198 1.810068 2.232675 1.459719
## [3,] 1.531516 1.354343 1.048290 2.291934 1.803234
## [4,] 2.041653 1.279700 1.842601 1.823324 1.300153
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.262541 1.142869 1.497044 1.121905 1.946443
## [2,] 1.063223 1.458198 1.810068 2.232675 1.459719
## [3,] 1.531516 1.354343 1.048290 2.291934 1.803234
## [4,] 2.041653 1.279700 1.842601 1.823324 1.300153
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.262541 1.142869 1.497044 1.121905 1.946443
## [2,] 1.063223 1.458198 1.810068 2.232675 1.459719
## [3,] 1.531516 1.354343 1.048290 2.291934 1.803234
## [4,] 2.041653 1.279700 1.842601 1.823324 1.300153
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.274866
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 1.274866
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.7223526 0.1857136 0.7928229
## [2,] 0.9632527 0.3996315 0.9751711
## [3,] 0.6197473 0.7106438 0.1528819
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.7223526 0.1857136 0.7928229
## [2,] 0.9632527 0.3996315 0.9751711
## [3,] 0.6197473 0.7106438 0.1528819
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.8532357 0.675384105 0.32165365
## [2,] 0.7227196 0.270522630 0.07917831
## [3,] 0.6626648 0.003493181 0.11037999
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.5884413 0.7848346 0.01507211
## [2,] 0.5311732 0.5120755 0.33400691
## [3,] 0.5302039 0.7533366 0.01781390
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.6768246 0.01043203 0.7483165
## [2,] 0.7780528 0.74672484 0.6048942
## [3,] 0.8076174 0.72784306 0.8331471
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.853235698 0.675384105 0.321653652
## [2,] 0.722719640 0.270522630 0.079178313
## [3,] 0.662664800 0.003493181 0.110379988
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.58844127 0.78483458 0.01507211
## [2,] 0.53117316 0.51207551 0.33400691
## [3,] 0.53020387 0.75333663 0.01781390
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.67682457 0.01043203 0.74831646
## [2,] 0.77805277 0.74672484 0.60489416
## [3,] 0.80761742 0.72784306 0.83314706
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.226577
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.226577
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 4.285211
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 4.285211
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 21.85862
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 21.85862
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.5460229 0.5297249 1.085757 0.5828298 1.5660877
## [2,] 0.6274905 0.9913797 1.385978 1.9300058 0.9608575
## [3,] 1.0763340 0.9949694 0.624680 1.9023094 1.3164027
## [4,] 1.5679726 0.7282434 1.547683 1.2686287 0.6252592
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.5460229 0.5297249 1.0857572 0.5828298 1.5660877
## [2,] 0.6274905 0.9913797 1.3859777 1.9300058 0.9608575
## [3,] 1.0763340 0.9949694 0.6246800 1.9023094 1.3164027
## [4,] 1.5679726 0.7282434 1.5476830 1.2686287 0.6252592
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.238810 1.2379265 0.7999720
## [2,] 1.237926 1.9841396 0.6295589
## [3,] 0.799972 0.6295589 1.0622615
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.2388103 1.2379265 0.7999720
## [2,] 1.2379265 1.9841396 0.6295589
## [3,] 0.7999720 0.6295589 1.0622615
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.09590324 0.882787494 0.005935327
## [2,] 0.24678554 0.818079787 0.088112781
## [3,] 0.05922750 0.002996237 0.761094754
## [4,] 0.83689397 0.280276124 0.207118634
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.095903242 0.882787494 0.005935327
## [2,] 0.246785543 0.818079787 0.088112781
## [3,] 0.059227497 0.002996237 0.761094754
## [4,] 0.836893967 0.280276124 0.207118634
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.2028159394 0.1640801 0.674755829 0.08261140 0.92786038
## [2,] 0.0004338344 0.2852971 0.750378555 0.91467912 0.47538332
## [3,] 0.0154601912 0.9160695 0.002103667 0.21016276 0.08161134
## [4,] 0.1336935654 0.2077266 0.009575579 0.08021131 0.06594537
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.2380355 0.34232199 0.40974128 0.4801887 0.045495601
## [2,] 0.1002021 0.69825992 0.02674256 0.9121409 0.006059121
## [3,] 0.2656773 0.04006002 0.55698366 0.9838111 0.927106001
## [4,] 0.5126514 0.01233451 0.61281153 0.6481259 0.370072720
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.1051715 0.02332281 0.001260131 0.02002973 0.5927317
## [2,] 0.5268546 0.00782271 0.608856620 0.10318578 0.4794151
## [3,] 0.7951964 0.03883980 0.065592706 0.70833558 0.3076854
## [4,] 0.9216276 0.50818227 0.925295905 0.54029142 0.1892411
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.2028159394 0.1640800966 0.6747558292 0.0826113987 0.9278603786
## [2,] 0.0004338344 0.2852971198 0.7503785549 0.9146791225 0.4753833163
## [3,] 0.0154601912 0.9160695406 0.0021036673 0.2101627552 0.0816113430
## [4,] 0.1336935654 0.2077266010 0.0095755793 0.0802113090 0.0659453707
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.238035518 0.342321989 0.409741278 0.480188684 0.045495601
## [2,] 0.100202096 0.698259919 0.026742559 0.912140912 0.006059121
## [3,] 0.265677335 0.040060024 0.556983657 0.983811064 0.927106001
## [4,] 0.512651441 0.012334510 0.612811532 0.648125944 0.370072720
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.105171475 0.023322810 0.001260131 0.020029727 0.592731741
## [2,] 0.526854618 0.007822710 0.608856620 0.103185784 0.479415102
## [3,] 0.795196426 0.038839797 0.065592706 0.708335585 0.307685366
## [4,] 0.921627641 0.508182275 0.925295905 0.540291418 0.189241139
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 0.9611599 2.035872 2.901901
## [2,] 2.3520857 1.731913 1.151111
## [3,] 1.8313998 2.332778 2.033825
## [4,] 1.9854617 3.444952 2.039424
## [5,] 2.1952109 1.862337 2.452001
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.9611599 2.0358718 2.9019013
## [2,] 2.3520857 1.7319132 1.1511114
## [3,] 1.8313998 2.3327781 2.0338247
## [4,] 1.9854617 3.4449524 2.0394238
## [5,] 2.1952109 1.8623368 2.4520013
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.0182897099 0.01479656 0.060848711 0.007449802 0.08367339
## [2,] 0.0001006735 0.06620466 0.174129187 0.212255975 0.11031513
## [3,] 0.0008610129 0.05101798 0.000117158 0.011704438 0.00454512
## [4,] 0.1052088773 0.16346847 0.007535411 0.063121525 0.05189508
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.1225931925 1.763029e-01 0.2110251941 0.247307059 0.023431171
## [2,] 0.0478234483 3.332585e-01 0.0127634195 0.435337438 0.002891836
## [3,] 0.0004644073 7.002543e-05 0.0009736145 0.001719714 0.001620593
## [4,] 0.0838256067 2.016863e-03 0.1002031680 0.105977563 0.060512012
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.815193e-05 6.242969e-06 3.373075e-07 5.361488e-06 0.0001586604
## [2,] 2.093608e-03 3.108578e-05 2.419466e-03 4.100383e-04 0.0019050932
## [3,] 2.729473e-02 1.333157e-03 2.251437e-03 2.431327e-02 0.0105611495
## [4,] 8.608754e-03 4.746837e-03 8.643018e-03 5.046762e-03 0.0017676666
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.0182897099 0.0147965558 0.0608487105 0.0074498016 0.0836733899
## [2,] 0.0001006735 0.0662046579 0.1741291870 0.2122559753 0.1103151334
## [3,] 0.0008610129 0.0510179807 0.0001171580 0.0117044383 0.0045451199
## [4,] 0.1052088773 0.1634684690 0.0075354109 0.0631215252 0.0518950810
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.225932e-01 1.763029e-01 2.110252e-01 2.473071e-01 2.343117e-02
## [2,] 4.782345e-02 3.332585e-01 1.276342e-02 4.353374e-01 2.891836e-03
## [3,] 4.644073e-04 7.002543e-05 9.736145e-04 1.719714e-03 1.620593e-03
## [4,] 8.382561e-02 2.016863e-03 1.002032e-01 1.059776e-01 6.051201e-02
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.815193e-05 6.242969e-06 3.373075e-07 5.361488e-06 1.586604e-04
## [2,] 2.093608e-03 3.108578e-05 2.419466e-03 4.100383e-04 1.905093e-03
## [3,] 2.729473e-02 1.333157e-03 2.251437e-03 2.431327e-02 1.056115e-02
## [4,] 8.608754e-03 4.746837e-03 8.643018e-03 5.046762e-03 1.767667e-03
einsum
By using einsum
and other DelayedTensor functions,
it is possible to implement your original tensor calculation functions.
It is intended to be applied to Delayed Arrays,
which can scale to large-scale data
since the calculation is performed internally by block processing.
For example, kronecker
can be easily implmented by eimsum
and other DelayedTensor functions4 https://stackoverflow.com/
questions/56067643/speeding-up-kronecker-products-numpy
(the kronecker
function inside DelayedTensor
has a more efficient implementation though).
darr1 <- DelayedArray(array(1:6, dim=c(2,3)))
darr2 <- DelayedArray(array(20:1, dim=c(4,5)))
mykronecker <- function(darr1, darr2){
stopifnot((length(dim(darr1)) == 2) && (length(dim(darr2)) == 2))
# Outer Product
tmpdarr <- DelayedTensor::einsum('ij,kl->ikjl', darr1, darr2)
# Reshape
DelayedTensor::unfold(tmpdarr, row_idx=c(2,1), col_idx=c(4,3))
}
identical(as.array(DelayedTensor::kronecker(darr1, darr2)),
as.array(mykronecker(darr1, darr2)))
## [1] TRUE
## R version 4.5.0 beta (2025-04-02 r88102)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.2 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.22-bioc/R/lib/libRblas.so
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0 LAPACK version 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.17.0
## [3] HDF5Array_1.37.0 h5mread_1.1.0
## [5] rhdf5_2.53.0 DelayedArray_0.35.0
## [7] SparseArray_1.9.0 S4Arrays_1.9.0
## [9] abind_1.4-8 IRanges_2.43.0
## [11] S4Vectors_0.47.0 MatrixGenerics_1.21.0
## [13] matrixStats_1.5.0 BiocGenerics_0.55.0
## [15] generics_0.1.3 Matrix_1.7-3
## [17] DelayedTensor_1.15.0 BiocStyle_2.37.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.49.0 cachem_1.1.0 yaml_2.3.10
## [19] tools_4.5.0 beachmat_2.25.0 parallel_4.5.0
## [22] BiocParallel_1.43.0 Rhdf5lib_1.31.0 rsvd_1.0.5
## [25] R6_2.6.1 lifecycle_1.0.4 BiocSingular_1.25.0
## [28] irlba_2.3.5.1 ScaledMatrix_1.17.0 rTensor_1.4.8
## [31] bslib_0.9.0 Rcpp_1.0.14 xfun_0.52
## [34] knitr_1.50 rhdf5filters_1.21.0 htmltools_0.5.8.1
## [37] rmarkdown_2.29 compiler_4.5.0