DelayedTensor 0.99.12
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2021-06-30 14:57:51
Compiled: Tue Oct 19 19:38:05 2021
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.4910969 0.8980406 0.9102752
DelayedTensor::einsum('i->i', darrA)
## <3> array of class DelayedArray and type "double":
## [1] [2] [3]
## 0.4910969 0.8980406 0.9102752
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.9382698 0.54481588 0.4016545 0.6571750
## [2,] 0.4064196 0.09232675 0.1475820 0.4898579
## [3,] 0.8403644 0.63687590 0.1256378 0.4887113
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.93826981 0.54481588 0.40165454 0.65717497
## [2,] 0.40641955 0.09232675 0.14758202 0.48985794
## [3,] 0.84036439 0.63687590 0.12563777 0.48871134
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1681395 0.1135420 0.9801604 0.1345952
## [2,] 0.3688056 0.7327074 0.5470003 0.4270209
## [3,] 0.8742249 0.7578390 0.1324762 0.2206398
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7274383 0.8822933 0.3543279 0.5142934
## [2,] 0.1754487 0.3443761 0.0621252 0.4326941
## [3,] 0.5682127 0.3141524 0.1187052 0.8797238
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8602266 0.14447142 0.5118079 0.6217786
## [2,] 0.8188870 0.01896894 0.1126800 0.5373417
## [3,] 0.5620746 0.27729123 0.1990970 0.7096294
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1160239 0.3641013 0.7294711 0.2581675
## [2,] 0.2318301 0.9006702 0.5852587 0.9431819
## [3,] 0.6665319 0.5415916 0.2129605 0.6146183
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4652403 0.05358199 0.4267684 0.8277989
## [2,] 0.6949061 0.16627525 0.0249883 0.5574332
## [3,] 0.9163339 0.71059969 0.9936650 0.4880336
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.1681395 0.1135420 0.9801604 0.1345952
## [2,] 0.3688056 0.7327074 0.5470003 0.4270209
## [3,] 0.8742249 0.7578390 0.1324762 0.2206398
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.7274383 0.8822933 0.3543279 0.5142934
## [2,] 0.1754487 0.3443761 0.0621252 0.4326941
## [3,] 0.5682127 0.3141524 0.1187052 0.8797238
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.86022664 0.14447142 0.51180788 0.62177863
## [2,] 0.81888698 0.01896894 0.11268003 0.53734169
## [3,] 0.56207458 0.27729123 0.19909698 0.70962935
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.1160239 0.3641013 0.7294711 0.2581675
## [2,] 0.2318301 0.9006702 0.5852587 0.9431819
## [3,] 0.6665319 0.5415916 0.2129605 0.6146183
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.46524027 0.05358199 0.42676842 0.82779889
## [2,] 0.69490614 0.16627525 0.02498830 0.55743324
## [3,] 0.91633391 0.71059969 0.99366495 0.48803355
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.9964516 0.8565626 0.4527415
DelayedTensor::einsum('ii->i', darrB)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.9964516 0.8565626 0.4527415
einsum::einsum('iii->i', arrD)
## [1] 0.2614571 0.3210300 0.1816670
DelayedTensor::einsum('iii->i', darrD)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.2614571 0.3210300 0.1816670
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.2411761 0.8064769 0.8286010
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.2411761 0.8064769 0.8286010
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.8803502 0.296824341 0.16132637 0.4318789
## [2,] 0.1651769 0.008524229 0.02178045 0.2399608
## [3,] 0.7062123 0.405610913 0.01578485 0.2388388
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.880350240 0.296824341 0.161326372 0.431878942
## [2,] 0.165176853 0.008524229 0.021780452 0.239960797
## [3,] 0.706212312 0.405610913 0.015784849 0.238838776
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02827088 0.01289179 0.96071449 0.01811586
## [2,] 0.13601759 0.53686015 0.29920937 0.18234686
## [3,] 0.76426910 0.57431998 0.01754995 0.04868192
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.52916643 0.77844152 0.125548227 0.2644977
## [2,] 0.03078224 0.11859487 0.003859541 0.1872242
## [3,] 0.32286570 0.09869176 0.014090920 0.7739140
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7399899 0.0208719900 0.26194731 0.3866087
## [2,] 0.6705759 0.0003598207 0.01269679 0.2887361
## [3,] 0.3159278 0.0768904246 0.03963961 0.5035738
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01346154 0.1325698 0.53212812 0.06665048
## [2,] 0.05374519 0.8112069 0.34252776 0.88959215
## [3,] 0.44426479 0.2933214 0.04535216 0.37775564
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2164485 0.00287103 0.1821312867 0.6852510
## [2,] 0.4828945 0.02764746 0.0006244149 0.3107318
## [3,] 0.8396678 0.50495192 0.9873700359 0.2381767
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> array of class HDF5Array and type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.02827088 0.01289179 0.96071449 0.01811586
## [2,] 0.13601759 0.53686015 0.29920937 0.18234686
## [3,] 0.76426910 0.57431998 0.01754995 0.04868192
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.529166432 0.778441517 0.125548227 0.264497704
## [2,] 0.030782244 0.118594870 0.003859541 0.187224195
## [3,] 0.322865695 0.098691756 0.014090920 0.773914042
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.7399898758 0.0208719900 0.2619473109 0.3866086623
## [2,] 0.6705758791 0.0003598207 0.0126967897 0.2887360960
## [3,] 0.3159278327 0.0768904246 0.0396396090 0.5035738215
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.01346154 0.13256977 0.53212812 0.06665048
## [2,] 0.05374519 0.81120689 0.34252776 0.88959215
## [3,] 0.44426479 0.29332144 0.04535216 0.37775564
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.2164485100 0.0028710302 0.1821312867 0.6852510084
## [2,] 0.4828945434 0.0276474589 0.0006244149 0.3107318190
## [3,] 0.8396678390 0.5049519184 0.9873700359 0.2381767487
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.2411761 0.4410249 0.4470333
## [2,] 0.4410249 0.8064769 0.8174641
## [3,] 0.4470333 0.8174641 0.8286010
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.2411761 0.4410249 0.4470333
## [2,] 0.4410249 0.8064769 0.8174641
## [3,] 0.4470333 0.8174641 0.8286010
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.15776019 0.09160505 0.06753398 0.11049705
## [2,] 0.06833517 0.01552377 0.02481436 0.08236445
## [3,] 0.14129842 0.10708397 0.02112467 0.08217166
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3460392 0.20093116 0.14813246 0.2423698
## [2,] 0.1498898 0.03405063 0.05442908 0.1806624
## [3,] 0.3099311 0.23488342 0.04633592 0.1802395
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8202588 0.47629158 0.3511364 0.5745187
## [2,] 0.3553021 0.08071434 0.1290199 0.4282460
## [3,] 0.7346674 0.55677274 0.1098357 0.4272436
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10653307 0.06185951 0.04560468 0.07461699
## [2,] 0.04614570 0.01048297 0.01675676 0.05561947
## [3,] 0.09541669 0.07231219 0.01426517 0.05548928
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6874772 0.3991906 0.29429526 0.4815170
## [2,] 0.2977866 0.0676485 0.10813444 0.3589225
## [3,] 0.6157412 0.4666437 0.09205572 0.3580824
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7110575 0.41288273 0.3043895 0.4980328
## [2,] 0.3080006 0.06996882 0.1118434 0.3712335
## [3,] 0.6368609 0.48264941 0.0952132 0.3703645
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9196550 0.53400697 0.3936859 0.6441369
## [2,] 0.3983564 0.09049503 0.1446541 0.4801394
## [3,] 0.8236919 0.62424056 0.1231452 0.4790155
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5132339 0.29801447 0.21970517 0.3594749
## [2,] 0.2223116 0.05050276 0.08072741 0.2679525
## [3,] 0.4596796 0.34837133 0.06872390 0.2673253
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12429846 0.07217516 0.05320968 0.08706007
## [2,] 0.05384093 0.01223110 0.01955111 0.06489454
## [3,] 0.11132832 0.08437093 0.01664402 0.06474264
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12628659 0.07332959 0.05406076 0.08845258
## [2,] 0.05470211 0.01242674 0.01986383 0.06593251
## [3,] 0.11310899 0.08572042 0.01691024 0.06577819
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4006608 0.23264777 0.17151489 0.2806275
## [2,] 0.1735496 0.03942545 0.06302061 0.2091796
## [3,] 0.3588532 0.27195933 0.05364995 0.2086900
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.20701966 0.12020807 0.08862098 0.1449990
## [2,] 0.08967233 0.02037096 0.03256247 0.1080822
## [3,] 0.18541783 0.14052017 0.02772069 0.1078292
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6825334 0.39631992 0.29217888 0.4780542
## [2,] 0.2956451 0.06716201 0.10735681 0.3563414
## [3,] 0.6113132 0.46328790 0.09139372 0.3555073
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.16461821 0.09558723 0.07046977 0.11530049
## [2,] 0.07130578 0.01619861 0.02589307 0.08594493
## [3,] 0.14744084 0.11173904 0.02204298 0.08574377
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5331368 0.30957131 0.22822522 0.3734152
## [2,] 0.2309328 0.05246123 0.08385798 0.2783435
## [3,] 0.4775057 0.36188099 0.07138898 0.2776920
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8278292 0.48068741 0.3543771 0.5798211
## [2,] 0.3585813 0.08145928 0.1302106 0.4321984
## [3,] 0.7414479 0.56191136 0.1108494 0.4311868
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3231177 0.18762155 0.13832021 0.2263153
## [2,] 0.1399612 0.03179512 0.05082371 0.1686953
## [3,] 0.2894014 0.21932481 0.04326664 0.1683005
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2947598 0.17115524 0.12618076 0.2064531
## [2,] 0.1276777 0.02900467 0.04636325 0.1538901
## [3,] 0.2640025 0.20007612 0.03946941 0.1535299
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3324551 0.19304344 0.14231739 0.2328554
## [2,] 0.1440058 0.03271394 0.05229242 0.1735703
## [3,] 0.2977645 0.22566287 0.04451696 0.1731640
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05829020 0.033846796 0.024952869 0.04082713
## [2,] 0.02524890 0.005735818 0.009168563 0.03043252
## [3,] 0.05220781 0.039566044 0.007805272 0.03036129
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11137749 0.06467247 0.04767848 0.07801008
## [2,] 0.04824411 0.01095966 0.01751875 0.05814868
## [3,] 0.09975561 0.07560047 0.01491385 0.05801257
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4825460 0.28019521 0.20656828 0.3379808
## [2,] 0.2090189 0.04748304 0.07590046 0.2519307
## [3,] 0.4321939 0.32754107 0.06461468 0.2513410
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4059838 0.23573862 0.17379356 0.2843557
## [2,] 0.1758553 0.03994924 0.06385787 0.2119586
## [3,] 0.3636207 0.27557245 0.05436272 0.2114625
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8254183 0.47928752 0.3533451 0.5781325
## [2,] 0.3575370 0.08122205 0.1298314 0.4309397
## [3,] 0.7392886 0.56027492 0.1105265 0.4299310
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8071247 0.46866513 0.3455139 0.5653194
## [2,] 0.3496129 0.07942193 0.1269540 0.4213888
## [3,] 0.7229038 0.54785762 0.1080770 0.4204025
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7683369 0.44614263 0.3289097 0.5381520
## [2,] 0.3328117 0.07560517 0.1208530 0.4011383
## [3,] 0.6881635 0.52152938 0.1028831 0.4001994
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5273776 0.30622716 0.2257598 0.3693813
## [2,] 0.2284381 0.05189452 0.0829521 0.2753367
## [3,] 0.4723475 0.35797175 0.0706178 0.2746922
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13555317 0.07871032 0.05802760 0.09494300
## [2,] 0.05871601 0.01333858 0.02132138 0.07077047
## [3,] 0.12140863 0.09201036 0.01815107 0.07060482
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.017797984 0.010334580 0.007618961 0.012465913
## [2,] 0.007709348 0.001751341 0.002799474 0.009292086
## [3,] 0.015940822 0.012080861 0.002383215 0.009270336
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2601740 0.1510727 0.11137528 0.1822289
## [2,] 0.1126966 0.0256014 0.04092320 0.1358333
## [3,] 0.2330257 0.1766001 0.03483825 0.1355154
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4802139 0.27884106 0.20556996 0.3363473
## [2,] 0.2080087 0.04725356 0.07553364 0.2507132
## [3,] 0.4301051 0.32595811 0.06430240 0.2501263
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10572427 0.06138987 0.04525845 0.07405050
## [2,] 0.04579537 0.01040338 0.01662955 0.05519721
## [3,] 0.09469229 0.07176320 0.01415687 0.05506801
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.18680669 0.10847120 0.07996821 0.13084155
## [2,] 0.08091691 0.01838198 0.02938313 0.09752924
## [3,] 0.16731402 0.12680007 0.02501410 0.09730095
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5833961 0.3387549 0.24974021 0.4086174
## [2,] 0.2527030 0.0574068 0.09176334 0.3045832
## [3,] 0.5225206 0.3959958 0.07811888 0.3038703
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5041715 0.29275229 0.21582573 0.3531275
## [2,] 0.2183862 0.04961101 0.07930197 0.2632211
## [3,] 0.4515628 0.34221998 0.06751041 0.2626050
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6658238 0.38661734 0.28502585 0.4663507
## [2,] 0.2884072 0.06551777 0.10472853 0.3476176
## [3,] 0.5963472 0.45194583 0.08915625 0.3468039
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10886170 0.06321165 0.04660152 0.07624799
## [2,] 0.04715437 0.01071211 0.01712304 0.05683522
## [3,] 0.09750234 0.07389281 0.01457698 0.05670219
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.21751917 0.12630471 0.09311561 0.1523529
## [2,] 0.09422028 0.02140412 0.03421395 0.1135638
## [3,] 0.19482175 0.14764699 0.02912661 0.1132980
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6253868 0.36313717 0.26771557 0.4380281
## [2,] 0.2708916 0.06153873 0.09836812 0.3265059
## [3,] 0.5601297 0.42449811 0.08374158 0.3257417
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3416253 0.19836818 0.14624295 0.2392783
## [2,] 0.1479779 0.03361629 0.05373481 0.1783579
## [3,] 0.3059778 0.23188735 0.04574488 0.1779404
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8450717 0.49069945 0.3617583 0.5918979
## [2,] 0.3660500 0.08315596 0.1329227 0.4412005
## [3,] 0.7568912 0.57361517 0.1131582 0.4401678
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5081590 0.29506769 0.21753272 0.3559204
## [2,] 0.2201134 0.05000339 0.07992918 0.2653029
## [3,] 0.4551343 0.34492663 0.06804436 0.2646819
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6844407 0.3974274 0.29299539 0.4793902
## [2,] 0.2964713 0.0673497 0.10765682 0.3573372
## [3,] 0.6130216 0.4645826 0.09164912 0.3565008
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5491306 0.31885824 0.23507182 0.3846174
## [2,] 0.2378606 0.05403504 0.08637366 0.2866936
## [3,] 0.4918306 0.37273717 0.07353060 0.2860226
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1998144 0.11602425 0.08553654 0.1399523
## [2,] 0.0865513 0.01966195 0.03142914 0.1043204
## [3,] 0.1789644 0.13562939 0.02675588 0.1040762
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2422308 0.14065378 0.10369417 0.1696612
## [2,] 0.1049243 0.02383577 0.03810089 0.1264654
## [3,] 0.2169548 0.16442069 0.03243559 0.1261694
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8849591 0.51386049 0.3788333 0.6198356
## [2,] 0.3833276 0.08708092 0.1391967 0.4620252
## [3,] 0.7926165 0.60068984 0.1184993 0.4609437
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5766778 0.33485380 0.24686423 0.4039118
## [2,] 0.2497929 0.05674571 0.09070661 0.3010756
## [3,] 0.5165033 0.39143557 0.07721927 0.3003709
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4365209 0.25347029 0.18686587 0.3057443
## [2,] 0.1890827 0.04295412 0.06866110 0.2279016
## [3,] 0.3909714 0.29630032 0.05845175 0.2273682
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6520095 0.37859590 0.27911221 0.4566749
## [2,] 0.2824234 0.06415843 0.10255565 0.3404053
## [3,] 0.5839744 0.44256897 0.08730646 0.3396085
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8597684 0.49923326 0.3680497 0.6021917
## [2,] 0.3724160 0.08460213 0.1352344 0.4488734
## [3,] 0.7700544 0.58359099 0.1151261 0.4478228
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05027437 0.029192321 0.021521452 0.03521275
## [2,] 0.02177677 0.004947052 0.007907739 0.02624757
## [3,] 0.04502840 0.034125081 0.006731922 0.02618613
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.15601105 0.09058940 0.06678521 0.10927193
## [2,] 0.06757751 0.01535165 0.02453924 0.08145125
## [3,] 0.13973180 0.10589670 0.02089045 0.08126060
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6667342 0.38714599 0.28541559 0.4669883
## [2,] 0.2888016 0.06560736 0.10487174 0.3480929
## [3,] 0.5971627 0.45256382 0.08927816 0.3472781
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4004239 0.23251021 0.17141348 0.2804615
## [2,] 0.1734470 0.03940214 0.06298334 0.2090559
## [3,] 0.3586410 0.27179852 0.05361823 0.2085666
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02344576 0.013614020 0.010036662 0.01642168
## [2,] 0.01015573 0.002307088 0.003687823 0.01224071
## [3,] 0.02099927 0.015914443 0.003139474 0.01221206
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9323258 0.54136444 0.3991100 0.6530117
## [2,] 0.4038449 0.09174186 0.1466471 0.4867547
## [3,] 0.8350406 0.63284126 0.1248418 0.4856153
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7766987 0.45099798 0.3324892 0.5440087
## [2,] 0.3364337 0.07642798 0.1221682 0.4055039
## [3,] 0.6956527 0.52720517 0.1040028 0.4045547
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5230228 0.3036985 0.22389559 0.3663312
## [2,] 0.2265518 0.0514660 0.08226712 0.2730631
## [3,] 0.4684470 0.3550158 0.07003467 0.2724239
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4579071 0.26588843 0.19602089 0.3207234
## [2,] 0.1983464 0.04505855 0.07202498 0.2390671
## [3,] 0.4101260 0.31081681 0.06131545 0.2385075
DelayedTensor::einsum('ij,klm->ijklm', darrC, darrE)
## <3 x 4 x 3 x 4 x 5> array of class HDF5Array and type "double":
## ,,1,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.15776019 0.09160505 0.06753398 0.11049705
## [2,] 0.06833517 0.01552377 0.02481436 0.08236445
## [3,] 0.14129842 0.10708397 0.02112467 0.08217166
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.34603919 0.20093116 0.14813246 0.24236983
## [2,] 0.14988982 0.03405063 0.05442908 0.18066236
## [3,] 0.30993112 0.23488342 0.04633592 0.18023949
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.82025879 0.47629158 0.35113639 0.57451869
## [2,] 0.35530208 0.08071434 0.12901987 0.42824598
## [3,] 0.73466744 0.55677274 0.10983566 0.42724360
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.77669871 0.45099798 0.33248919 0.54400871
## [2,] 0.33643366 0.07642798 0.12216823 0.40550386
## [3,] 0.69565271 0.52720517 0.10400281 0.40455471
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.52302278 0.30369848 0.22389559 0.36633117
## [2,] 0.22655177 0.05146600 0.08226712 0.27306310
## [3,] 0.46844705 0.35501580 0.07003467 0.27242395
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.45790715 0.26588843 0.19602089 0.32072344
## [2,] 0.19834638 0.04505855 0.07202498 0.23906711
## [3,] 0.41012602 0.31081681 0.06131545 0.23850753
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] 2.299413
DelayedTensor::einsum('i->', darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 2.299413
einsum::einsum('ij->', arrC)
## [1] 5.769691
DelayedTensor::einsum('ij->', darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 5.769691
einsum::einsum('ijk->', arrE)
## [1] 28.69523
DelayedTensor::einsum('ijk->', darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 28.69523
einsum::einsum('ij->i', arrC)
## [1] 2.541915 1.136186 2.091589
DelayedTensor::einsum('ij->i', darrC)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 2.541915 1.136186 2.091589
einsum::einsum('ij->j', arrC)
## [1] 2.1850538 1.2740185 0.6748743 1.6357442
DelayedTensor::einsum('ij->j', darrC)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 2.1850538 1.2740185 0.6748743 1.6357442
einsum::einsum('ijk->i', arrE)
## [1] 9.254228 8.682600 10.758401
DelayedTensor::einsum('ijk->i', darrE)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 9.254228 8.682600 10.758401
einsum::einsum('ijk->j', arrE)
## [1] 8.214324 6.322462 5.991492 8.166950
DelayedTensor::einsum('ijk->j', darrE)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 8.214324 6.322462 5.991492 8.166950
einsum::einsum('ijk->k', arrE)
## [1] 5.457151 5.373791 5.374254 6.164407 6.325625
DelayedTensor::einsum('ijk->k', darrE)
## <5> array of class HDF5Array and type "double":
## [1] [2] [3] [4] [5]
## 5.457151 5.373791 5.374254 6.164407 6.325625
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 2.337069 1.557990 3.002536 2.356634
## [2,] 2.289878 2.162998 1.332053 2.897672
## [3,] 3.587378 2.601474 1.656904 2.912645
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 2.337069 1.557990 3.002536 2.356634
## [2,] 2.289878 2.162998 1.332053 2.897672
## [3,] 3.587378 2.601474 1.656904 2.912645
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.4111700 1.4710997 2.2411882 1.014386 2.0764803
## [2,] 1.6040885 1.5408218 0.4407316 1.806363 0.9304569
## [3,] 1.6596370 0.5351582 0.8235849 1.527690 1.4454217
## [4,] 0.7822559 1.8267114 1.8687497 1.815968 1.8732657
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.4111700 1.4710997 2.2411882 1.0143859 2.0764803
## [2,] 1.6040885 1.5408218 0.4407316 1.8063631 0.9304569
## [3,] 1.6596370 0.5351582 0.8235849 1.5276903 1.4454217
## [4,] 0.7822559 1.8267114 1.8687497 1.8159678 1.8732657
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.4111700 1.4710997 2.2411882 1.014386 2.0764803
## [2,] 1.6040885 1.5408218 0.4407316 1.806363 0.9304569
## [3,] 1.6596370 0.5351582 0.8235849 1.527690 1.4454217
## [4,] 0.7822559 1.8267114 1.8687497 1.815968 1.8732657
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.4111700 1.4710997 2.2411882 1.0143859 2.0764803
## [2,] 1.6040885 1.5408218 0.4407316 1.8063631 0.9304569
## [3,] 1.6596370 0.5351582 0.8235849 1.5276903 1.4454217
## [4,] 0.7822559 1.8267114 1.8687497 1.8159678 1.8732657
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 2.305756
DelayedTensor::einsum('ii->', darrB)
## <1> array of class HDF5Array and type "double":
## [1]
## 2.305756
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.99645158 0.908327423 0.5731793
## [2,] 0.56945680 0.856562604 0.0836814
## [3,] 0.02795457 0.007852488 0.4527415
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3]
## [1,] 0.996451584 0.908327423 0.573179349
## [2,] 0.569456796 0.856562604 0.083681405
## [3,] 0.027954573 0.007852488 0.452741504
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.26145713 0.3268271 0.41209194
## [2,] 0.21082736 0.3683330 0.05527853
## [3,] 0.04051827 0.6091267 0.56952218
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.8626710 0.8369488 0.2711800
## [2,] 0.9476171 0.3210300 0.8813933
## [3,] 0.6413906 0.7889832 0.1725727
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.8519562 0.9328916 0.9896377
## [2,] 0.1606409 0.2093500 0.6897671
## [3,] 0.4180421 0.3797210 0.1816670
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.26145713 0.32682709 0.41209194
## [2,] 0.21082736 0.36833299 0.05527853
## [3,] 0.04051827 0.60912671 0.56952218
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.8626710 0.8369488 0.2711800
## [2,] 0.9476171 0.3210300 0.8813933
## [3,] 0.6413906 0.7889832 0.1725727
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.8519562 0.9328916 0.9896377
## [2,] 0.1606409 0.2093500 0.6897671
## [3,] 0.4180421 0.3797210 0.1816670
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.876254
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 1.876254
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 3.572269
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 3.572269
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 18.62609
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 18.62609
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.9285576 0.8828144 1.72649359 0.5114715 1.5390109
## [2,] 1.1240719 0.9957281 0.09812224 1.2370981 0.5354704
## [3,] 1.2774738 0.1434987 0.31428371 0.9200080 1.1701257
## [4,] 0.2491446 1.2256359 1.17891858 1.3339983 1.2341596
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.92855757 0.88281437 1.72649359 0.51147152 1.53901089
## [2,] 1.12407193 0.99572814 0.09812224 1.23709810 0.53547041
## [3,] 1.27747381 0.14349869 0.31428371 0.92000804 1.17012574
## [4,] 0.24914464 1.22563594 1.17891858 1.33399826 1.23415958
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.7703799 0.8128316 1.5071005
## [2,] 0.8128316 0.4354423 0.6582822
## [3,] 1.5071005 0.6582822 1.3664469
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 1.7703799 0.8128316 1.5071005
## [2,] 0.8128316 0.4354423 0.6582822
## [3,] 1.5071005 0.6582822 1.3664469
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.8803502 0.165176853 0.70621231
## [2,] 0.2968243 0.008524229 0.40561091
## [3,] 0.1613264 0.021780452 0.01578485
## [4,] 0.4318789 0.239960797 0.23883878
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.880350240 0.165176853 0.706212312
## [2,] 0.296824341 0.008524229 0.405610913
## [3,] 0.161326372 0.021780452 0.015784849
## [4,] 0.431878942 0.239960797 0.238838776
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.02827088 0.5291664 0.73998988 0.01346154 0.21644851
## [2,] 0.01289179 0.7784415 0.02087199 0.13256977 0.00287103
## [3,] 0.96071449 0.1255482 0.26194731 0.53212812 0.18213129
## [4,] 0.01811586 0.2644977 0.38660866 0.06665048 0.68525101
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.1360176 0.030782244 0.6705758791 0.05374519 0.4828945434
## [2,] 0.5368602 0.118594870 0.0003598207 0.81120689 0.0276474589
## [3,] 0.2992094 0.003859541 0.0126967897 0.34252776 0.0006244149
## [4,] 0.1823469 0.187224195 0.2887360960 0.88959215 0.3107318190
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.76426910 0.32286570 0.31592783 0.44426479 0.8396678
## [2,] 0.57431998 0.09869176 0.07689042 0.29332144 0.5049519
## [3,] 0.01754995 0.01409092 0.03963961 0.04535216 0.9873700
## [4,] 0.04868192 0.77391404 0.50357382 0.37775564 0.2381767
DelayedTensor::einsum('ijk,ijk->jki', darrE, darrE)
## <4 x 5 x 3> array of class HDF5Array and type "double":
## ,,1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.02827088 0.52916643 0.73998988 0.01346154 0.21644851
## [2,] 0.01289179 0.77844152 0.02087199 0.13256977 0.00287103
## [3,] 0.96071449 0.12554823 0.26194731 0.53212812 0.18213129
## [4,] 0.01811586 0.26449770 0.38660866 0.06665048 0.68525101
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.1360175920 0.0307822442 0.6705758791 0.0537451875 0.4828945434
## [2,] 0.5368601512 0.1185948703 0.0003598207 0.8112068933 0.0276474589
## [3,] 0.2992093689 0.0038595407 0.0126967897 0.3425277573 0.0006244149
## [4,] 0.1823468554 0.1872241947 0.2887360960 0.8895921454 0.3107318190
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.76426910 0.32286570 0.31592783 0.44426479 0.83966784
## [2,] 0.57431998 0.09869176 0.07689042 0.29332144 0.50495192
## [3,] 0.01754995 0.01409092 0.03963961 0.04535216 0.98737004
## [4,] 0.04868192 0.77391404 0.50357382 0.37775564 0.23817675
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 1.396437 2.075534 1.985180
## [2,] 2.478353 1.014644 1.880794
## [3,] 2.138285 1.487878 1.748092
## [4,] 1.467764 2.660941 2.035702
## [5,] 1.773390 1.443603 3.108632
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 1.396437 2.075534 1.985180
## [2,] 2.478353 1.014644 1.880794
## [3,] 2.138285 1.487878 1.748092
## [4,] 1.467764 2.660941 2.035702
## [5,] 1.773390 1.443603 3.108632
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.012222555 0.228778362 0.31992519 0.005819926 0.0935787546
## [2,] 0.001879231 0.113473036 0.00304250 0.019324630 0.0004185087
## [3,] 0.076114409 0.009946794 0.02075327 0.042158849 0.0144296932
## [4,] 0.003842273 0.056098482 0.08199753 0.014136194 0.1453379017
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.020176240 4.566100e-03 9.947022e-02 0.007972320 7.163041e-02
## [2,] 0.004109720 9.078560e-04 2.754465e-06 0.006209873 2.116442e-04
## [3,] 0.005852454 7.549157e-05 2.483458e-04 0.006699750 1.221339e-05
## [4,] 0.039294751 4.034579e-02 6.222105e-02 0.191702245 6.696101e-02
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.4913085229 0.2075534238 0.203093436 0.2855945337 0.53977842
## [2,] 0.2120490212 0.0364387291 0.028389295 0.1082994264 0.18643711
## [3,] 0.0002521675 0.0002024662 0.000569564 0.0006516451 0.01418708
## [4,] 0.0105838887 0.1682558903 0.109481489 0.0821274813 0.05178177
DelayedTensor::einsum('i,ij,ijk,ijk,ji->jki',
darrA, darrC, darrE, darrE, t(darrC))
## <4 x 5 x 3> array of class HDF5Array and type "double":
## ,,1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0122225548 0.2287783623 0.3199251913 0.0058199256 0.0935787546
## [2,] 0.0018792305 0.1134730363 0.0030424997 0.0193246296 0.0004185087
## [3,] 0.0761144092 0.0099467941 0.0207532676 0.0421588494 0.0144296932
## [4,] 0.0038422728 0.0560984818 0.0819975324 0.0141361938 0.1453379017
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.017624e-02 4.566100e-03 9.947022e-02 7.972320e-03 7.163041e-02
## [2,] 4.109720e-03 9.078560e-04 2.754465e-06 6.209873e-03 2.116442e-04
## [3,] 5.852454e-03 7.549157e-05 2.483458e-04 6.699750e-03 1.221339e-05
## [4,] 3.929475e-02 4.034579e-02 6.222105e-02 1.917022e-01 6.696101e-02
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.4913085229 0.2075534238 0.2030934358 0.2855945337 0.5397784199
## [2,] 0.2120490212 0.0364387291 0.0283892948 0.1082994264 0.1864371141
## [3,] 0.0002521675 0.0002024662 0.0005695640 0.0006516451 0.0141870824
## [4,] 0.0105838887 0.1682558903 0.1094814890 0.0821274813 0.0517817726
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.1.1 Patched (2021-08-22 r80813)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Mojave 10.14.6
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib
##
## locale:
## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] einsum_0.1.0 DelayedRandomArray_1.1.0 HDF5Array_1.21.0
## [4] rhdf5_2.37.4 DelayedArray_0.19.4 IRanges_2.27.2
## [7] S4Vectors_0.31.5 MatrixGenerics_1.5.4 matrixStats_0.61.0
## [10] BiocGenerics_0.39.2 Matrix_1.3-4 DelayedTensor_0.99.12
## [13] BiocStyle_2.21.4
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.7 rTensor_1.4.8 bslib_0.3.1
## [4] compiler_4.1.1 BiocManager_1.30.16 jquerylib_0.1.4
## [7] rhdf5filters_1.5.0 tools_4.1.1 digest_0.6.28
## [10] jsonlite_1.7.2 evaluate_0.14 lattice_0.20-45
## [13] rlang_0.4.12 parallel_4.1.1 yaml_2.2.1
## [16] xfun_0.27 fastmap_1.1.0 stringr_1.4.0
## [19] knitr_1.36 sass_0.4.0 grid_4.1.1
## [22] R6_2.5.1 BiocParallel_1.27.17 rmarkdown_2.11
## [25] bookdown_0.24 irlba_2.3.3 Rhdf5lib_1.15.2
## [28] magrittr_2.0.1 BiocSingular_1.9.1 htmltools_0.5.2
## [31] rsvd_1.0.5 beachmat_2.9.1 dqrng_0.3.0
## [34] ScaledMatrix_1.1.0 stringi_1.7.5