DelayedTensor 1.6.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2023-04-25 14:41:07
Compiled: Thu May 11 18:45:40 2023
einsum
einsum
is an easy and intuitive way to write tensor operations.
It was originally introduced by
Numpy
1 https://numpy.org/doc/stable/reference/generated/numpy.einsum.html
package of Python but similar tools have been implemented in other languages
(e.g. R, Julia) inspired by Numpy
.
In this vignette, we will use CRAN einsum package first.
einsum
is named after
Einstein summation2 https://en.wikipedia.org/wiki/Einstein_notation
introduced by Albert Einstein,
which is a notational convention that implies summation over
a set of indexed terms in a formula.
Here, we consider a simple example of einsum
; matrix multiplication.
If we naively implement the matrix multiplication,
the calculation would look like the following in a for loop.
A <- matrix(runif(3*4), nrow=3, ncol=4)
B <- matrix(runif(4*5), nrow=4, ncol=5)
C <- matrix(0, nrow=3, ncol=5)
I <- nrow(A)
J <- ncol(A)
K <- ncol(B)
for(i in 1:I){
for(j in 1:J){
for(k in 1:K){
C[i,k] = C[i,k] + A[i,j] * B[j,k]
}
}
}
Therefore, any programming language can implement this. However, when analyzing tensor data, such operations tend to be more complicated and increase the possibility of causing bugs because the order of tensors is larger or more tensors are handled simultaneously. In addition, several programming languages, especially R, are known to significantly slow down the speed of computation if the code is written in for loop.
Obviously, in the case of the R language, it should be executed using the built-in matrix multiplication function (%*%) prepared by the R, as shown below.
C <- A %*% B
However, more complex operations than matrix multiplication are not always provided by programming languages as standard.
einsum
is a function that solves such a problem.
To put it simply, einsum
is a wrapper for the for loop above.
Like the Einstein summation, it omits many notations such as for,
array size (e.g. I, J, and K), brackets (e.g. {}, (), and []),
and even addition operator (+) and
extracts the array subscripts (e.g. i, j, and k)
to concisely express the tensor operation as follows.
suppressPackageStartupMessages(library("einsum"))
C <- einsum('ij,jk->ik', A, B)
DelayedTensor
CRAN einsum is easy to use because the syntax is almost
the same as that of Numpy
‘s einsum
,
except that it prohibits the implicit modes that do not use’->’.
It is extremely fast because the internal calculation
is actually performed by C++.
When the input tensor is huge, however,
it is not scalable because it assumes that the input is R’s standard array.
Using einsum
of DelayedTensor,
we can augment the CRAN einsum
’s functionality;
in DelayedTensor,
the input DelayedArray objects are divided into
multiple block tensors and the CRAN einsum
is incremently applied in the block processing.
A surprisingly large number of tensor operations can be handled
uniformly in einsum
.
In more detail, einsum
is capable of performing any tensor operation
that can be described by a combination of the following
three operations3 https://ajcr.net/Basic-guide-to-einsum/.
Some typical operations are introduced below. Here we use the arrays and DelayedArray objects below.
suppressPackageStartupMessages(library("DelayedTensor"))
suppressPackageStartupMessages(library("DelayedArray"))
arrA <- array(runif(3), dim=c(3))
arrB <- array(runif(3*3), dim=c(3,3))
arrC <- array(runif(3*4), dim=c(3,4))
arrD <- array(runif(3*3*3), dim=c(3,3,3))
arrE <- array(runif(3*4*5), dim=c(3,4,5))
darrA <- DelayedArray(arrA)
darrB <- DelayedArray(arrB)
darrC <- DelayedArray(arrC)
darrD <- DelayedArray(arrD)
darrE <- DelayedArray(arrE)
If the same subscript is written on both sides of ->,
einsum
will simply output the object without any calculation.
einsum::einsum('i->i', arrA)
## [1] 0.4543612 0.4058860 0.2159047
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.4543612 0.4058860 0.2159047
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.899750995 0.78605391 0.9722882 0.7418529
## [2,] 0.006368879 0.09876999 0.1613922 0.4686270
## [3,] 0.469515239 0.42061468 0.8572502 0.5512184
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.899750995 0.786053911 0.972288155 0.741852924
## [2,] 0.006368879 0.098769991 0.161392185 0.468627033
## [3,] 0.469515239 0.420614683 0.857250203 0.551218437
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9795267 0.3099437 0.5176146 0.4028913
## [2,] 0.4746113 0.5736812 0.3755520 0.1150094
## [3,] 0.8460280 0.8986951 0.3605275 0.6482772
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8604418 0.5366278 0.44669307 0.7730176
## [2,] 0.3860563 0.2460052 0.02422102 0.9507041
## [3,] 0.3281744 0.1199686 0.01610152 0.9132435
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.95264419 0.02863245 0.2539420 0.8152436
## [2,] 0.07349337 0.85566926 0.8926316 0.5984821
## [3,] 0.46450046 0.37041668 0.9664314 0.0459808
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3484367 0.35507824 0.5796923 0.57131628
## [2,] 0.2252635 0.08582429 0.8595050 0.64045436
## [3,] 0.1110529 0.99383118 0.4722248 0.04156417
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7239564 0.3063332 0.7278426 0.7167943
## [2,] 0.9397750 0.3852347 0.7393594 0.6471459
## [3,] 0.1802447 0.8781889 0.5822812 0.2181939
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.9795267 0.3099437 0.5176146 0.4028913
## [2,] 0.4746113 0.5736812 0.3755520 0.1150094
## [3,] 0.8460280 0.8986951 0.3605275 0.6482772
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.86044182 0.53662783 0.44669307 0.77301759
## [2,] 0.38605633 0.24600523 0.02422102 0.95070407
## [3,] 0.32817444 0.11996863 0.01610152 0.91324352
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.95264419 0.02863245 0.25394205 0.81524365
## [2,] 0.07349337 0.85566926 0.89263157 0.59848208
## [3,] 0.46450046 0.37041668 0.96643140 0.04598080
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.34843674 0.35507824 0.57969233 0.57131628
## [2,] 0.22526352 0.08582429 0.85950503 0.64045436
## [3,] 0.11105294 0.99383118 0.47222484 0.04156417
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.7239564 0.3063332 0.7278426 0.7167943
## [2,] 0.9397750 0.3852347 0.7393594 0.6471459
## [3,] 0.1802447 0.8781889 0.5822812 0.2181939
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.7066586 0.2759630 0.7355858
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.7066586 0.2759630 0.7355858
einsum::einsum('iii->i', arrD)
## [1] 0.9762580 0.2319773 0.9796330
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.9762580 0.2319773 0.9796330
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.20644406 0.16474347 0.04661482
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.20644406 0.16474347 0.04661482
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 8.095519e-01 0.617880751 0.94534426 0.5503458
## [2,] 4.056262e-05 0.009755511 0.02604744 0.2196113
## [3,] 2.204446e-01 0.176916712 0.73487791 0.3038418
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 8.095519e-01 6.178808e-01 9.453443e-01 5.503458e-01
## [2,] 4.056262e-05 9.755511e-03 2.604744e-02 2.196113e-01
## [3,] 2.204446e-01 1.769167e-01 7.348779e-01 3.038418e-01
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9594726 0.0960651 0.2679249 0.16232143
## [2,] 0.2252559 0.3291101 0.1410393 0.01322716
## [3,] 0.7157634 0.8076528 0.1299801 0.42026338
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7403601 0.28796943 0.1995347005 0.5975562
## [2,] 0.1490395 0.06051857 0.0005866579 0.9038382
## [3,] 0.1076985 0.01439247 0.0002592590 0.8340137
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.907530960 0.0008198171 0.06448656 0.664622202
## [2,] 0.005401276 0.7321698841 0.79679112 0.358180795
## [3,] 0.215760677 0.1372085200 0.93398966 0.002114234
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12140816 0.126080556 0.3360432 0.32640230
## [2,] 0.05074365 0.007365809 0.7387489 0.41018178
## [3,] 0.01233275 0.987700407 0.2229963 0.00172758
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.52411281 0.09384004 0.5297548 0.51379400
## [2,] 0.88317708 0.14840580 0.5466524 0.41879787
## [3,] 0.03248817 0.77121579 0.3390514 0.04760857
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.95947256 0.09606510 0.26792492 0.16232143
## [2,] 0.22525590 0.32911012 0.14103931 0.01322716
## [3,] 0.71576345 0.80765280 0.12998007 0.42026338
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.7403601184 0.2879694257 0.1995347005 0.5975561897
## [2,] 0.1490394921 0.0605185716 0.0005866579 0.9038382368
## [3,] 0.1076984617 0.0143924729 0.0002592590 0.8340137355
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.9075309604 0.0008198171 0.0644865625 0.6646222016
## [2,] 0.0054012760 0.7321698841 0.7967911166 0.3581807951
## [3,] 0.2157606774 0.1372085200 0.9339896586 0.0021142340
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.121408163 0.126080556 0.336043193 0.326402295
## [2,] 0.050743652 0.007365809 0.738748902 0.410181784
## [3,] 0.012332754 0.987700407 0.222996302 0.001727580
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.52411281 0.09384004 0.52975480 0.51379400
## [2,] 0.88317708 0.14840580 0.54665235 0.41879787
## [3,] 0.03248817 0.77121579 0.33905137 0.04760857
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.20644406 0.18441885 0.09809869
## [2,] 0.18441885 0.16474347 0.08763268
## [3,] 0.09809869 0.08763268 0.04661482
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.20644406 0.18441885 0.09809869
## [2,] 0.18441885 0.16474347 0.08763268
## [3,] 0.09809869 0.08763268 0.04661482
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.881330124 0.76996079 0.9523822 0.7266647
## [2,] 0.006238487 0.09674784 0.1580880 0.4590327
## [3,] 0.459902713 0.41200331 0.8396995 0.5399332
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.427031998 0.37307008 0.46145895 0.3520918
## [2,] 0.003022742 0.04687735 0.07659856 0.2224157
## [3,] 0.222837242 0.19962849 0.40686064 0.2616145
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.76121457 0.66502365 0.8225830 0.6276284
## [2,] 0.00538825 0.08356218 0.1365423 0.3964716
## [3,] 0.39722306 0.35585182 0.7252577 0.4663463
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.278872159 0.24363246 0.30135459 0.2299326
## [2,] 0.001973994 0.03061314 0.05002249 0.1452480
## [3,] 0.145523294 0.13036687 0.26569931 0.1708467
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.516170235 0.45094435 0.55778344 0.4255871
## [2,] 0.003653706 0.05666249 0.09258766 0.2688425
## [3,] 0.269352068 0.24129874 0.49178833 0.3162237
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.80860177 0.7064228 0.8737906 0.6666996
## [2,] 0.00572368 0.0887641 0.1450424 0.4211528
## [3,] 0.42195102 0.3780043 0.7704065 0.4953773
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.465724287 0.40687301 0.50327058 0.3839939
## [2,] 0.003296625 0.05112479 0.08353896 0.2425682
## [3,] 0.243027961 0.21771632 0.44372526 0.2853187
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.337903291 0.29520412 0.36514477 0.2786044
## [2,] 0.002391845 0.03709327 0.06061116 0.1759938
## [3,] 0.176327390 0.15796269 0.32194203 0.2070112
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.324384963 0.2833940 0.35053660 0.2674584
## [2,] 0.002296156 0.0356093 0.05818632 0.1689529
## [3,] 0.169273148 0.1516432 0.30906226 0.1987294
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.362501883 0.31669431 0.39172648 0.2988861
## [2,] 0.002565966 0.03979357 0.06502351 0.1888058
## [3,] 0.189163623 0.16946201 0.34537868 0.2220811
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1034798180 0.09040359 0.11182227 0.08532006
## [2,] 0.0007324809 0.01135948 0.01856162 0.05389651
## [3,] 0.0539986637 0.04837464 0.09859183 0.06339530
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.583288089 0.50958086 0.6303123 0.4809264
## [2,] 0.004128799 0.06403034 0.1046269 0.3038002
## [3,] 0.304376041 0.27267492 0.5557358 0.3573424
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.77418338 0.67635365 0.8365974 0.6383213
## [2,] 0.00548005 0.08498583 0.1388686 0.4032263
## [3,] 0.40399054 0.36191446 0.7376139 0.4742914
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.347354570 0.30346109 0.37535800 0.2863970
## [2,] 0.002458746 0.03813078 0.06230648 0.1809164
## [3,] 0.181259331 0.16238096 0.33094687 0.2128014
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.295275277 0.25796280 0.31908012 0.2434572
## [2,] 0.002090103 0.03241379 0.05296479 0.1537914
## [3,] 0.154082900 0.13803499 0.28132760 0.1808958
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.482831422 0.42181840 0.52175688 0.3980989
## [2,] 0.003417718 0.05300273 0.08660754 0.2514783
## [3,] 0.251954943 0.22571354 0.46002431 0.2957992
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.221343448 0.19337337 0.23918797 0.1824997
## [2,] 0.001566778 0.02429793 0.03970332 0.1152847
## [3,] 0.115503203 0.10347341 0.21088803 0.1356026
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1079418969 0.09430181 0.1166441 0.08899908
## [2,] 0.0007640657 0.01184930 0.0193620 0.05622054
## [3,] 0.0563271013 0.05046057 0.1028431 0.06612892
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.401912536 0.35112484 0.43431438 0.3313806
## [2,] 0.002844934 0.04411987 0.07209277 0.2093324
## [3,] 0.209729204 0.18788566 0.38292773 0.2462255
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0217928882 0.01903903 0.023549812 0.01796844
## [2,] 0.0001542608 0.00239231 0.003909084 0.01135063
## [3,] 0.0113721387 0.01018772 0.020763476 0.01335107
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0144873593 0.012656663 0.01565532 0.011944960
## [2,] 0.0001025486 0.001590347 0.00259866 0.007545608
## [3,] 0.0075599094 0.006772536 0.01380303 0.008875455
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.695523343 0.60763350 0.7515958 0.5734654
## [2,] 0.004923255 0.07635094 0.1247590 0.3622569
## [3,] 0.362943537 0.32514255 0.6626695 0.4261015
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.855396937 0.74730466 0.9243583 0.7052826
## [2,] 0.006054919 0.09390103 0.1534362 0.4455256
## [3,] 0.446370050 0.39988009 0.8149913 0.5240456
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.821691770 0.71785864 0.8879359 0.6774924
## [2,] 0.005816338 0.09020105 0.1473904 0.4279706
## [3,] 0.428781751 0.38412364 0.7828782 0.5033967
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.857142562 0.74882969 0.9262447 0.7067219
## [2,] 0.006067276 0.09409266 0.1537493 0.4464348
## [3,] 0.447280966 0.40069614 0.8166544 0.5251150
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0661257360 0.05776975 0.07145674 0.05452127
## [2,] 0.0004680704 0.00725894 0.01186126 0.03444098
## [3,] 0.0345062588 0.03091239 0.06300221 0.04051090
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.417934751 0.36512240 0.45162830 0.3445910
## [2,] 0.002958347 0.04587871 0.07496674 0.2176775
## [3,] 0.218090044 0.19537571 0.39819311 0.2560412
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0257620738 0.022506648 0.027838990 0.02124107
## [2,] 0.0001823566 0.002828027 0.004621053 0.01341794
## [3,] 0.0134433708 0.012043228 0.024545172 0.01578273
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.769889269 0.67260217 0.8319571 0.6347807
## [2,] 0.005449654 0.08451445 0.1380983 0.4009897
## [3,] 0.401749757 0.35990706 0.7335226 0.4716607
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.333282780 0.29116748 0.36015175 0.2747947
## [2,] 0.002359139 0.03658605 0.05978236 0.1735873
## [3,] 0.173916278 0.15580270 0.31753978 0.2041805
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.228484608 0.19961214 0.24690484 0.1883876
## [2,] 0.001617326 0.02508185 0.04098426 0.1190041
## [3,] 0.119229660 0.10681175 0.21769187 0.1399775
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.803146142 0.70165654 0.8678951 0.6622013
## [2,] 0.005685062 0.08816521 0.1440638 0.4183113
## [3,] 0.419104124 0.37545394 0.7652086 0.4920350
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.869547618 0.75966719 0.9396498 0.7169500
## [2,] 0.006155085 0.09545442 0.1559745 0.4528959
## [3,] 0.453754271 0.40649524 0.8284735 0.5327148
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.733516281 0.64082546 0.7926517 0.6047909
## [2,] 0.005192188 0.08052161 0.1315740 0.3820452
## [3,] 0.382769315 0.34290345 0.6988678 0.4493773
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.53848484 0.47043918 0.58189703 0.4439857
## [2,] 0.00381166 0.05911207 0.09659033 0.2804649
## [3,] 0.28099645 0.25173035 0.51304888 0.3298944
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0413712711 0.036143388 0.044706588 0.03411099
## [2,] 0.0002928462 0.004541523 0.007420942 0.02154785
## [3,] 0.0215886866 0.019340200 0.039417051 0.02534547
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.313506305 0.27389006 0.33878092 0.2584888
## [2,] 0.002219151 0.03441509 0.05623497 0.1632869
## [3,] 0.163596360 0.14655761 0.29869747 0.1920648
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.202681072 0.17706927 0.21902105 0.1671124
## [2,] 0.001434676 0.02224928 0.03635577 0.1055646
## [3,] 0.105764653 0.09474914 0.19310719 0.1241694
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0999199890 0.08729359 0.10797545 0.08238494
## [2,] 0.0007072827 0.01096870 0.01792308 0.05204241
## [3,] 0.0521410454 0.04671050 0.09520015 0.06121443
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.31948200 0.27911064 0.34523837 0.2634158
## [2,] 0.00226145 0.03507107 0.05730685 0.1663993
## [3,] 0.16671464 0.14935112 0.30439089 0.1957257
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0772204927 0.067462521 0.08344594 0.06366900
## [2,] 0.0005466045 0.008476865 0.01385137 0.04021958
## [3,] 0.0402958132 0.036098958 0.07357289 0.04730793
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.894200590 0.7812049 0.9662903 0.7372766
## [2,] 0.006329591 0.0981607 0.1603966 0.4657362
## [3,] 0.466618882 0.4180200 0.8519620 0.5478181
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.52157875 0.45566942 0.56362798 0.4300464
## [2,] 0.00369199 0.05725621 0.09355781 0.2716595
## [3,] 0.27217438 0.24382710 0.49694136 0.3195371
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.773340509 0.6756173 0.8356866 0.6376263
## [2,] 0.005474084 0.0848933 0.1387174 0.4027873
## [3,] 0.403550711 0.3615204 0.7368109 0.4737750
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.424884772 0.37119418 0.4591386 0.3503214
## [2,] 0.003007543 0.04664164 0.0762134 0.2212973
## [3,] 0.221716760 0.19862470 0.4048148 0.2602990
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.514042394 0.4490854 0.55548405 0.4238327
## [2,] 0.003638644 0.0564289 0.09220598 0.2677343
## [3,] 0.268241701 0.2403040 0.48976100 0.3149201
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.576249446 0.50343165 0.6227062 0.4751229
## [2,] 0.004078976 0.06325767 0.1033643 0.3001342
## [3,] 0.300703081 0.26938451 0.5490296 0.3530303
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0373974023 0.032671678 0.040412349 0.03083450
## [2,] 0.0002647172 0.004105293 0.006708132 0.01947809
## [3,] 0.0195150107 0.017482500 0.035630892 0.02291094
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.65138045 0.56906873 0.7038942 0.5370691
## [2,] 0.00461079 0.07150516 0.1168409 0.3392655
## [3,] 0.33990854 0.30450667 0.6206117 0.3990581
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.845563503 0.73871382 0.9137321 0.6971748
## [2,] 0.005985313 0.09282157 0.1516723 0.4404040
## [3,] 0.441238690 0.39528317 0.8056223 0.5180213
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.162175382 0.14168208 0.17524982 0.13371509
## [2,] 0.001147957 0.01780277 0.02909009 0.08446756
## [3,] 0.084627651 0.07581358 0.15451484 0.09935422
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.275623619 0.24079442 0.29784416 0.2272542
## [2,] 0.001950999 0.03025653 0.04943979 0.1435560
## [3,] 0.143828114 0.12884825 0.26260421 0.1688565
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.346615329 0.30281526 0.37455916 0.2857875
## [2,] 0.002453513 0.03804963 0.06217387 0.1805314
## [3,] 0.180873575 0.16203538 0.33024255 0.2123485
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.790151360 0.69030384 0.8538527 0.6514870
## [2,] 0.005593079 0.08673871 0.1417328 0.4115431
## [3,] 0.412323083 0.36937916 0.7528276 0.4840739
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.654877072 0.5721235 0.7076727 0.5399521
## [2,] 0.004635541 0.0718890 0.1174681 0.3410867
## [3,] 0.341733176 0.3061413 0.6239432 0.4012002
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.665239373 0.58117636 0.7188704 0.5484959
## [2,] 0.004708891 0.07302652 0.1193268 0.3464838
## [3,] 0.347140514 0.31098543 0.6338160 0.4075485
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.523908071 0.45770440 0.56614509 0.4319670
## [2,] 0.003708478 0.05751191 0.09397563 0.2728727
## [3,] 0.273389887 0.24491601 0.49916066 0.3209641
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.644936341 0.56343892 0.6969306 0.5317559
## [2,] 0.004565176 0.07079776 0.1156850 0.3359092
## [3,] 0.336545824 0.30149419 0.6144720 0.3951102
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.582270202 0.5086916 0.6292123 0.4800871
## [2,] 0.004121594 0.0639186 0.1044443 0.3032701
## [3,] 0.303844880 0.2721991 0.5547660 0.3567188
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.19632016 0.17151215 0.21214732 0.1618678
## [2,] 0.00138965 0.02155101 0.03521479 0.1022516
## [3,] 0.10244535 0.09177555 0.18704675 0.1202725
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.881330124 0.769960795 0.952382209 0.726664747
## [2,] 0.006238487 0.096747844 0.158087955 0.459032692
## [3,] 0.459902713 0.412003313 0.839699464 0.539933177
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.427031998 0.373070076 0.461458954 0.352091788
## [2,] 0.003022742 0.046877355 0.076598556 0.222415690
## [3,] 0.222837242 0.199628486 0.406860642 0.261614504
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.76121457 0.66502365 0.82258304 0.62762838
## [2,] 0.00538825 0.08356218 0.13654231 0.39647161
## [3,] 0.39722306 0.35585182 0.72525771 0.46634625
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.644936341 0.563438925 0.696930560 0.531755911
## [2,] 0.004565176 0.070797762 0.115684991 0.335909163
## [3,] 0.336545824 0.301494187 0.614472017 0.395110207
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.582270202 0.508691596 0.629212330 0.480087106
## [2,] 0.004121594 0.063918599 0.104444297 0.303270081
## [3,] 0.303844880 0.272199084 0.554765987 0.356718773
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.19632016 0.17151215 0.21214732 0.16186777
## [2,] 0.00138965 0.02155101 0.03521479 0.10225155
## [3,] 0.10244535 0.09177555 0.18704675 0.12027249
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.076152
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.076152
einsum::einsum('ij->', arrC)
## [1] 6.433703
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 6.433703
einsum::einsum('ijk->', arrE)
## [1] 30.75128
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 30.75128
einsum::einsum('ij->i', arrC)
## [1] 3.3999460 0.7351581 2.2985986
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 3.3999460 0.7351581 2.2985986
einsum::einsum('ij->j', arrC)
## [1] 1.375635 1.305439 1.990931 1.761698
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 1.375635 1.305439 1.990931 1.761698
einsum::einsum('ijk->i', arrE)
## [1] 11.206669 10.088679 9.455927
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 11.206669 10.088679 9.455927
einsum::einsum('ijk->j', arrE)
## [1] 7.894206 6.944131 7.814620 8.098319
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 7.894206 6.944131 7.814620 8.098319
einsum::einsum('ijk->k', arrE)
## [1] 6.502358 5.601255 6.318068 5.284244 7.045350
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 6.502358 5.601255 6.318068 5.284244 7.045350
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 3.865006 1.536615 2.525785 3.279263
## [2,] 2.099200 2.146415 2.891269 2.951796
## [3,] 1.930001 3.261100 2.397566 1.867260
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 3.865006 1.536615 2.525785 3.279263
## [2,] 2.099200 2.146415 2.891269 2.951796
## [3,] 1.930001 3.261100 2.397566 1.867260
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.300166 1.5746726 1.490638 0.6847532 1.843976
## [2,] 1.782320 0.9026017 1.254718 1.4347337 1.569757
## [3,] 1.253694 0.4870156 2.113005 1.9114222 2.049483
## [4,] 1.166178 2.6369652 1.459707 1.2533348 1.582134
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.3001661 1.5746726 1.4906380 0.6847532 1.8439761
## [2,] 1.7823200 0.9026017 1.2547184 1.4347337 1.5697569
## [3,] 1.2536941 0.4870156 2.1130050 1.9114222 2.0494832
## [4,] 1.1661780 2.6369652 1.4597065 1.2533348 1.5821341
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.300166 1.5746726 1.490638 0.6847532 1.843976
## [2,] 1.782320 0.9026017 1.254718 1.4347337 1.569757
## [3,] 1.253694 0.4870156 2.113005 1.9114222 2.049483
## [4,] 1.166178 2.6369652 1.459707 1.2533348 1.582134
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.3001661 1.5746726 1.4906380 0.6847532 1.8439761
## [2,] 1.7823200 0.9026017 1.2547184 1.4347337 1.5697569
## [3,] 1.2536941 0.4870156 2.1130050 1.9114222 2.0494832
## [4,] 1.1661780 2.6369652 1.4597065 1.2533348 1.5821341
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.718207
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 1.718207
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.7066586 0.7330767 0.1163244
## [2,] 0.8322106 0.2759630 0.6773136
## [3,] 0.3082276 0.9887630 0.7355858
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.7066586 0.7330767 0.1163244
## [2,] 0.8322106 0.2759630 0.6773136
## [3,] 0.3082276 0.9887630 0.7355858
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.9762580 0.2753420 0.2570331
## [2,] 0.1320450 0.1205168 0.1028338
## [3,] 0.1930574 0.4948868 0.4879776
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.1607369 0.3248184 0.9616232
## [2,] 0.8423298 0.2319773 0.3888750
## [3,] 0.7653344 0.1875502 0.5091069
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.6705117 0.8461892 0.4839569
## [2,] 0.6253606 0.1185195 0.5953057
## [3,] 0.3643880 0.5760419 0.9796330
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.9762580 0.2753420 0.2570331
## [2,] 0.1320450 0.1205168 0.1028338
## [3,] 0.1930574 0.4948868 0.4879776
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.1607369 0.3248184 0.9616232
## [2,] 0.8423298 0.2319773 0.3888750
## [3,] 0.7653344 0.1875502 0.5091069
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.6705117 0.8461892 0.4839569
## [2,] 0.6253606 0.1185195 0.5953057
## [3,] 0.3643880 0.5760419 0.9796330
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.4178024
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 0.4178024
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 4.614658
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 4.614658
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 21.17355
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 21.17355
The inner product is an operation that eliminates all subscripts, while the outer product is an operation that leaves all subscripts intact. In the middle of the two, the operation that eliminates some subscripts while keeping others by summing them is called contracted product.
einsum::einsum('ijk,ijk->jk', arrE, arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.9004919 0.9970981 1.1286929 0.1844846 1.4397780
## [2,] 1.2328280 0.3628805 0.8701982 1.1211468 1.0134616
## [3,] 0.5389443 0.2003806 1.7952673 1.2977884 1.4154585
## [4,] 0.5958120 2.3354082 1.0249172 0.7383117 0.9802004
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.9004919 0.9970981 1.1286929 0.1844846 1.4397780
## [2,] 1.2328280 0.3628805 0.8701982 1.1211468 1.0134616
## [3,] 0.5389443 0.2003806 1.7952673 1.2977884 1.4154585
## [4,] 0.5958120 2.3354082 1.0249172 0.7383117 0.9802004
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 2.923123 0.5879410 1.9954898
## [2,] 0.587941 0.2554548 0.4412037
## [3,] 1.995490 0.4412037 1.4360809
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 2.9231226 0.5879410 1.9954898
## [2,] 0.5879410 0.2554548 0.4412037
## [3,] 1.9954898 0.4412037 1.4360809
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.8095519 4.056262e-05 0.2204446
## [2,] 0.6178808 9.755511e-03 0.1769167
## [3,] 0.9453443 2.604744e-02 0.7348779
## [4,] 0.5503458 2.196113e-01 0.3038418
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 8.095519e-01 4.056262e-05 2.204446e-01
## [2,] 6.178808e-01 9.755511e-03 1.769167e-01
## [3,] 9.453443e-01 2.604744e-02 7.348779e-01
## [4,] 5.503458e-01 2.196113e-01 3.038418e-01
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9594726 0.7403601 0.9075309604 0.1214082 0.52411281
## [2,] 0.0960651 0.2879694 0.0008198171 0.1260806 0.09384004
## [3,] 0.2679249 0.1995347 0.0644865625 0.3360432 0.52975480
## [4,] 0.1623214 0.5975562 0.6646222016 0.3264023 0.51379400
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.22525590 0.1490394921 0.005401276 0.050743652 0.8831771
## [2,] 0.32911012 0.0605185716 0.732169884 0.007365809 0.1484058
## [3,] 0.14103931 0.0005866579 0.796791117 0.738748902 0.5466524
## [4,] 0.01322716 0.9038382368 0.358180795 0.410181784 0.4187979
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.7157634 0.107698462 0.215760677 0.01233275 0.03248817
## [2,] 0.8076528 0.014392473 0.137208520 0.98770041 0.77121579
## [3,] 0.1299801 0.000259259 0.933989659 0.22299630 0.33905137
## [4,] 0.4202634 0.834013735 0.002114234 0.00172758 0.04760857
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.9594725583 0.7403601184 0.9075309604 0.1214081632 0.5241128075
## [2,] 0.0960651015 0.2879694257 0.0008198171 0.1260805560 0.0938400410
## [3,] 0.2679249154 0.1995347005 0.0644865625 0.3360431926 0.5297547993
## [4,] 0.1623214305 0.5975561897 0.6646222016 0.3264022954 0.5137939986
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.2252558952 0.1490394921 0.0054012760 0.0507436515 0.8831770753
## [2,] 0.3291101242 0.0605185716 0.7321698841 0.0073658092 0.1484057953
## [3,] 0.1410393093 0.0005866579 0.7967911166 0.7387489020 0.5466523509
## [4,] 0.0132271610 0.9038382368 0.3581807951 0.4101817843 0.4187978655
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.715763447 0.107698462 0.215760677 0.012332754 0.032488165
## [2,] 0.807652796 0.014392473 0.137208520 0.987700407 0.771215790
## [3,] 0.129980067 0.000259259 0.933989659 0.222996302 0.339051372
## [4,] 0.420263375 0.834013735 0.002114234 0.001727580 0.047608569
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 2.209976 1.538854 2.753528
## [2,] 2.616780 1.606987 1.377488
## [3,] 2.050462 2.420276 1.847329
## [4,] 1.854524 1.811047 1.618673
## [5,] 2.474926 2.711515 1.858909
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 2.209976 1.538854 2.753528
## [2,] 2.616780 1.606987 1.377488
## [3,] 2.050462 2.420276 1.847329
## [4,] 1.854524 1.811047 1.618673
## [5,] 2.474926 2.711515 1.858909
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.35292175 0.27232586 0.3338161274 0.04465743 0.19278384
## [2,] 0.02696941 0.08084483 0.0002301563 0.03539598 0.02634475
## [3,] 0.11508117 0.08570568 0.0276987653 0.14433986 0.22754436
## [4,] 0.04058940 0.14942235 0.1661925928 0.08161877 0.12847714
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 3.708568e-06 2.453757e-06 8.892553e-08 8.354334e-07 1.454045e-05
## [2,] 1.303153e-03 2.396309e-04 2.899119e-03 2.916585e-05 5.876314e-04
## [3,] 1.491109e-03 6.202318e-06 8.423908e-03 7.810268e-03 5.779368e-03
## [4,] 1.179032e-03 8.056557e-02 3.192722e-02 3.656244e-02 3.733045e-02
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.03406677 5.125909e-03 0.0102691307 0.0005869775 0.001546274
## [2,] 0.03085003 5.497513e-04 0.0052409732 0.0377273319 0.029458238
## [3,] 0.02062310 4.113495e-05 0.1481901243 0.0353813871 0.053795098
## [4,] 0.02756963 5.471201e-02 0.0001386956 0.0001133307 0.003123163
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.3529217489 0.2723258581 0.3338161274 0.0446574328 0.1927838447
## [2,] 0.0269694137 0.0808448277 0.0002301563 0.0353959827 0.0263447480
## [3,] 0.1150811743 0.0857056822 0.0276987653 0.1443398617 0.2275443638
## [4,] 0.0405894045 0.1494223519 0.1661925928 0.0816187657 0.1284771357
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 3.708568e-06 2.453757e-06 8.892553e-08 8.354334e-07 1.454045e-05
## [2,] 1.303153e-03 2.396309e-04 2.899119e-03 2.916585e-05 5.876314e-04
## [3,] 1.491109e-03 6.202318e-06 8.423908e-03 7.810268e-03 5.779368e-03
## [4,] 1.179032e-03 8.056557e-02 3.192722e-02 3.656244e-02 3.733045e-02
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 3.406677e-02 5.125909e-03 1.026913e-02 5.869775e-04 1.546274e-03
## [2,] 3.085003e-02 5.497513e-04 5.240973e-03 3.772733e-02 2.945824e-02
## [3,] 2.062310e-02 4.113495e-05 1.481901e-01 3.538139e-02 5.379510e-02
## [4,] 2.756963e-02 5.471201e-02 1.386956e-04 1.133307e-04 3.123163e-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.3.0 RC (2023-04-13 r84257)
## Platform: x86_64-apple-darwin20 (64-bit)
## Running under: macOS Monterey 12.6.4
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0
##
## 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.0 DelayedRandomArray_1.8.0 HDF5Array_1.28.1
## [4] rhdf5_2.44.0 DelayedArray_0.26.2 S4Arrays_1.0.1
## [7] IRanges_2.34.0 S4Vectors_0.38.1 MatrixGenerics_1.12.0
## [10] matrixStats_0.63.0 BiocGenerics_0.46.0 Matrix_1.5-4
## [13] DelayedTensor_1.6.0 BiocStyle_2.28.0
##
## loaded via a namespace (and not attached):
## [1] jsonlite_1.8.4 compiler_4.3.0 BiocManager_1.30.20
## [4] crayon_1.5.2 rsvd_1.0.5 Rcpp_1.0.10
## [7] rhdf5filters_1.12.1 parallel_4.3.0 jquerylib_0.1.4
## [10] BiocParallel_1.34.1 yaml_2.3.7 fastmap_1.1.1
## [13] lattice_0.21-8 R6_2.5.1 ScaledMatrix_1.8.1
## [16] knitr_1.42 bookdown_0.34 bslib_0.4.2
## [19] rlang_1.1.1 cachem_1.0.8 xfun_0.39
## [22] sass_0.4.6 cli_3.6.1 Rhdf5lib_1.22.0
## [25] BiocSingular_1.16.0 digest_0.6.31 grid_4.3.0
## [28] irlba_2.3.5.1 rTensor_1.4.8 dqrng_0.3.0
## [31] evaluate_0.21 codetools_0.2-19 beachmat_2.16.0
## [34] rmarkdown_2.21 tools_4.3.0 htmltools_0.5.5