DelayedTensor 1.9.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2024-04-23 17:14:07
Compiled: Mon Apr 29 18:39:00 2024
einsum
einsum
is an easy and intuitive way to write tensor operations.
It was originally introduced by
Numpy
1 https://numpy.org/doc/stable/reference/generated/numpy.einsum.html
package of Python but similar tools have been implemented in other languages
(e.g. R, Julia) inspired by Numpy
.
In this vignette, we will use CRAN einsum package first.
einsum
is named after
Einstein summation2 https://en.wikipedia.org/wiki/Einstein_notation
introduced by Albert Einstein,
which is a notational convention that implies summation over
a set of indexed terms in a formula.
Here, we consider a simple example of einsum
; matrix multiplication.
If we naively implement the matrix multiplication,
the calculation would look like the following in a for loop.
A <- matrix(runif(3*4), nrow=3, ncol=4)
B <- matrix(runif(4*5), nrow=4, ncol=5)
C <- matrix(0, nrow=3, ncol=5)
I <- nrow(A)
J <- ncol(A)
K <- ncol(B)
for(i in 1:I){
for(j in 1:J){
for(k in 1:K){
C[i,k] = C[i,k] + A[i,j] * B[j,k]
}
}
}
Therefore, any programming language can implement this. However, when analyzing tensor data, such operations tend to be more complicated and increase the possibility of causing bugs because the order of tensors is larger or more tensors are handled simultaneously. In addition, several programming languages, especially R, are known to significantly slow down the speed of computation if the code is written in for loop.
Obviously, in the case of the R language, it should be executed using the built-in matrix multiplication function (%*%) prepared by the R, as shown below.
C <- A %*% B
However, more complex operations than matrix multiplication are not always provided by programming languages as standard.
einsum
is a function that solves such a problem.
To put it simply, einsum
is a wrapper for the for loop above.
Like the Einstein summation, it omits many notations such as for,
array size (e.g. I, J, and K), brackets (e.g. {}, (), and []),
and even addition operator (+) and
extracts the array subscripts (e.g. i, j, and k)
to concisely express the tensor operation as follows.
suppressPackageStartupMessages(library("einsum"))
C <- einsum('ij,jk->ik', A, B)
DelayedTensor
CRAN einsum is easy to use because the syntax is almost
the same as that of Numpy
‘s einsum
,
except that it prohibits the implicit modes that do not use’->’.
It is extremely fast because the internal calculation
is actually performed by C++.
When the input tensor is huge, however,
it is not scalable because it assumes that the input is R’s standard array.
Using einsum
of DelayedTensor,
we can augment the CRAN einsum
’s functionality;
in DelayedTensor,
the input DelayedArray objects are divided into
multiple block tensors and the CRAN einsum
is incremently applied in the block processing.
A surprisingly large number of tensor operations can be handled
uniformly in einsum
.
In more detail, einsum
is capable of performing any tensor operation
that can be described by a combination of the following
three operations3 https://ajcr.net/Basic-guide-to-einsum/.
Some typical operations are introduced below. Here we use the arrays and DelayedArray objects below.
suppressPackageStartupMessages(library("DelayedTensor"))
suppressPackageStartupMessages(library("DelayedArray"))
arrA <- array(runif(3), dim=c(3))
arrB <- array(runif(3*3), dim=c(3,3))
arrC <- array(runif(3*4), dim=c(3,4))
arrD <- array(runif(3*3*3), dim=c(3,3,3))
arrE <- array(runif(3*4*5), dim=c(3,4,5))
darrA <- DelayedArray(arrA)
darrB <- DelayedArray(arrB)
darrC <- DelayedArray(arrC)
darrD <- DelayedArray(arrD)
darrE <- DelayedArray(arrE)
If the same subscript is written on both sides of ->,
einsum
will simply output the object without any calculation.
einsum::einsum('i->i', arrA)
## [1] 0.1802109 0.7880089 0.8521719
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.1802109 0.7880089 0.8521719
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.6452428 0.2615415 0.8273664 0.45922480
## [2,] 0.6644433 0.4195248 0.7887032 0.04357353
## [3,] 0.8668410 0.7820416 0.1409807 0.95245966
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.64524284 0.26154153 0.82736636 0.45922480
## [2,] 0.66444331 0.41952480 0.78870320 0.04357353
## [3,] 0.86684101 0.78204157 0.14098068 0.95245966
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1074749 0.7782782 0.2974319 0.4745549
## [2,] 0.3776362 0.9462920 0.9684314 0.9491844
## [3,] 0.2752759 0.7813814 0.2373342 0.7663776
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3051037 0.8392012 0.09357652 0.9943589
## [2,] 0.8564676 0.5193209 0.26583416 0.7694328
## [3,] 0.2811889 0.4336065 0.72447984 0.8120297
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08122170 0.7918745 0.03080069 0.8828181
## [2,] 0.04049925 0.7573545 0.65416821 0.2758596
## [3,] 0.20554903 0.4956781 0.36479273 0.5567419
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3982322 0.1039269 0.8085101 0.4979834
## [2,] 0.6882710 0.9242470 0.9837856 0.2541147
## [3,] 0.1000515 0.8098535 0.9655056 0.8553541
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4949354 0.3966900 0.1759729 0.6060376
## [2,] 0.9701573 0.9195945 0.5431720 0.5117288
## [3,] 0.3536232 0.6950483 0.5504753 0.3967384
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.1074749 0.7782782 0.2974319 0.4745549
## [2,] 0.3776362 0.9462920 0.9684314 0.9491844
## [3,] 0.2752759 0.7813814 0.2373342 0.7663776
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.30510365 0.83920120 0.09357652 0.99435885
## [2,] 0.85646755 0.51932095 0.26583416 0.76943285
## [3,] 0.28118887 0.43360649 0.72447984 0.81202971
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.08122170 0.79187447 0.03080069 0.88281812
## [2,] 0.04049925 0.75735447 0.65416821 0.27585961
## [3,] 0.20554903 0.49567811 0.36479273 0.55674192
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.3982322 0.1039269 0.8085101 0.4979834
## [2,] 0.6882710 0.9242470 0.9837856 0.2541147
## [3,] 0.1000515 0.8098535 0.9655056 0.8553541
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.4949354 0.3966900 0.1759729 0.6060376
## [2,] 0.9701573 0.9195945 0.5431720 0.5117288
## [3,] 0.3536232 0.6950483 0.5504753 0.3967384
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.63078402 0.06513377 0.65452827
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.63078402 0.06513377 0.65452827
einsum::einsum('iii->i', arrD)
## [1] 0.9591049 0.8983378 0.8796796
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.9591049 0.8983378 0.8796796
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.03247596 0.62095807 0.72619688
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.03247596 0.62095807 0.72619688
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.4163383 0.06840397 0.68453509 0.210887415
## [2,] 0.4414849 0.17600106 0.62205274 0.001898653
## [3,] 0.7514133 0.61158902 0.01987555 0.907179396
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.416338323 0.068403970 0.684535093 0.210887415
## [2,] 0.441484909 0.176001060 0.622052743 0.001898653
## [3,] 0.751413331 0.611589015 0.019875553 0.907179396
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01155086 0.6057169 0.08846571 0.2252023
## [2,] 0.14260910 0.8954686 0.93785941 0.9009510
## [3,] 0.07577683 0.6105568 0.05632751 0.5873346
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09308824 0.7042587 0.008756565 0.9887495
## [2,] 0.73353667 0.2696942 0.070667799 0.5920269
## [3,] 0.07906718 0.1880146 0.524871041 0.6593923
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.006596965 0.6270652 0.0009486823 0.77936783
## [2,] 0.001640189 0.5735858 0.4279360512 0.07609852
## [3,] 0.042250405 0.2456968 0.1330737324 0.30996157
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1585889 0.0108008 0.6536886 0.24798742
## [2,] 0.4737170 0.8542325 0.9678341 0.06457427
## [3,] 0.0100103 0.6558627 0.9322011 0.73163060
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2449611 0.1573630 0.03096647 0.3672816
## [2,] 0.9412052 0.8456540 0.29503583 0.2618664
## [3,] 0.1250494 0.4830921 0.30302307 0.1574013
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.01155086 0.60571692 0.08846571 0.22520234
## [2,] 0.14260910 0.89546863 0.93785941 0.90095100
## [3,] 0.07577683 0.61055683 0.05632751 0.58733461
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.093088238 0.704258662 0.008756565 0.988749528
## [2,] 0.733536672 0.269694246 0.070667799 0.592026905
## [3,] 0.079067179 0.188014587 0.524871041 0.659392255
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.0065969653 0.6270651688 0.0009486823 0.7793678331
## [2,] 0.0016401893 0.5735857900 0.4279360512 0.0760985221
## [3,] 0.0422504051 0.2456967902 0.1330737324 0.3099615663
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.15858887 0.01080080 0.65368857 0.24798742
## [2,] 0.47371696 0.85423249 0.96783407 0.06457427
## [3,] 0.01001030 0.65586271 0.93220105 0.73163060
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.24496106 0.15736297 0.03096647 0.36728161
## [2,] 0.94120523 0.84565399 0.29503583 0.26186639
## [3,] 0.12504936 0.48309207 0.30302307 0.15740133
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.03247596 0.1420078 0.1535706
## [2,] 0.14200777 0.6209581 0.6715190
## [3,] 0.15357063 0.6715190 0.7261969
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.03247596 0.14200777 0.15357063
## [2,] 0.14200777 0.62095807 0.67151903
## [3,] 0.15357063 0.67151903 0.72619688
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06934742 0.02810915 0.08892113 0.049355147
## [2,] 0.07141099 0.04508839 0.08476581 0.004683062
## [3,] 0.09316367 0.08404985 0.01515189 0.102365523
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2436671 0.09876755 0.31244348 0.17341991
## [2,] 0.2509178 0.15842775 0.29784288 0.01645494
## [3,] 0.3273505 0.29532720 0.05323941 0.35968324
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1776198 0.07199608 0.22775403 0.12641353
## [2,] 0.1829052 0.11548507 0.21711100 0.01199474
## [3,] 0.2386205 0.21527721 0.03880859 0.26218920
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5021784 0.2035521 0.6439212 0.35740464
## [2,] 0.5171217 0.3265070 0.6138305 0.03391233
## [3,] 0.6746434 0.6086459 0.1097222 0.74127857
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6105882 0.2474947 0.7829302 0.43456077
## [2,] 0.6287574 0.3969930 0.7463436 0.04123329
## [3,] 0.8202847 0.7400397 0.1334089 0.90130499
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5041807 0.2043637 0.6464887 0.35882970
## [2,] 0.5191836 0.3278089 0.6162780 0.03404755
## [3,] 0.6773334 0.6110727 0.1101597 0.74423422
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1919158 0.07779078 0.24608512 0.13658809
## [2,] 0.1976266 0.12478004 0.23458546 0.01296016
## [3,] 0.2578261 0.23260408 0.04193215 0.28329185
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6248734 0.2532850 0.8012476 0.44472772
## [2,] 0.6434678 0.4062810 0.7638050 0.04219798
## [3,] 0.8394761 0.7573536 0.1365301 0.92239186
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1531382 0.06207274 0.19636231 0.10898974
## [2,] 0.1576951 0.09956757 0.18718622 0.01034149
## [3,] 0.2057310 0.18560519 0.03345953 0.22605123
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3062031 0.1241158 0.39263075 0.21792737
## [2,] 0.3153148 0.1990875 0.37428296 0.02067803
## [3,] 0.4113636 0.3711217 0.06690307 0.45199439
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6124544 0.2482511 0.7853232 0.43588901
## [2,] 0.6306792 0.3982064 0.7486248 0.04135932
## [3,] 0.8227920 0.7423016 0.1338167 0.90405984
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4944997 0.2004396 0.6340750 0.35193959
## [2,] 0.5092145 0.3215144 0.6044445 0.03339378
## [3,] 0.6643275 0.5993391 0.1080444 0.72994373
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1968659 0.07979727 0.25243250 0.14011116
## [2,] 0.2027241 0.12799855 0.24063623 0.01329444
## [3,] 0.2644764 0.23860374 0.04301372 0.29059892
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5526296 0.2240018 0.7086124 0.39331114
## [2,] 0.5690741 0.3593094 0.6754987 0.03731932
## [3,] 0.7424212 0.6697932 0.1207454 0.81575079
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1814351 0.07354257 0.2326462 0.12912890
## [2,] 0.1868341 0.11796570 0.2217746 0.01225239
## [3,] 0.2437460 0.21990138 0.0396422 0.26782105
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5414886 0.2194860 0.6943268 0.38538200
## [2,] 0.5576016 0.3520657 0.6618807 0.03656696
## [3,] 0.7274540 0.6562902 0.1183112 0.79930529
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3350881 0.1358240 0.42966868 0.23848506
## [2,] 0.3450593 0.2178680 0.40959009 0.02262865
## [3,] 0.4501687 0.4061306 0.07321422 0.49463225
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2797815 0.1134061 0.35875142 0.19912285
## [2,] 0.2881069 0.1819087 0.34198683 0.01889377
## [3,] 0.3758679 0.3390983 0.06113014 0.41299269
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06037958 0.02447415 0.07742207 0.042972659
## [2,] 0.06217629 0.03925767 0.07380410 0.004077459
## [3,] 0.08111597 0.07318073 0.01319248 0.089127861
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1715276 0.06952667 0.21994224 0.12207764
## [2,] 0.1766317 0.11152402 0.20966425 0.01158333
## [3,] 0.2304359 0.20789336 0.03747748 0.25319631
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4674654 0.1894816 0.5994102 0.33269911
## [2,] 0.4813758 0.3039373 0.5713996 0.03156815
## [3,] 0.6280088 0.5665734 0.1021377 0.69003782
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6416029 0.2600661 0.8226991 0.45663424
## [2,] 0.6606951 0.4171582 0.7842540 0.04332773
## [3,] 0.8619510 0.7776300 0.1401854 0.94708669
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4964710 0.2012386 0.6366029 0.35334264
## [2,] 0.5112445 0.3227962 0.6068542 0.03352691
## [3,] 0.6669759 0.6017285 0.1084752 0.73285374
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5239564 0.2123795 0.6718461 0.3729042
## [2,] 0.5395477 0.3406666 0.6404504 0.0353830
## [3,] 0.7039007 0.6350410 0.1144805 0.7734255
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05240772 0.02124285 0.06720011 0.037299021
## [2,] 0.05396722 0.03407452 0.06405982 0.003539117
## [3,] 0.07040630 0.06351875 0.01145069 0.077360397
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02613185 0.01059224 0.033507718 0.018598260
## [2,] 0.02690946 0.01699044 0.031941889 0.001764695
## [3,] 0.03510641 0.03167210 0.005709612 0.038573903
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1326290 0.05375961 0.17006436 0.094393213
## [2,] 0.1365757 0.08623292 0.16211718 0.008956497
## [3,] 0.1781783 0.16074789 0.02897844 0.195777162
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5109513 0.2071081 0.6551703 0.36364839
## [2,] 0.5261557 0.3322110 0.6245539 0.03450477
## [3,] 0.6864293 0.6192787 0.1116390 0.75422848
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4886775 0.1980796 0.6266096 0.34779595
## [2,] 0.5032191 0.3177290 0.5973279 0.03300061
## [3,] 0.6565059 0.5922827 0.1067724 0.72134958
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3198328 0.1296404 0.41010739 0.22762768
## [2,] 0.3293500 0.2079493 0.39094291 0.02159845
## [3,] 0.4296741 0.3876409 0.06988104 0.47211340
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01987392 0.008055659 0.025483452 0.014144439
## [2,] 0.02046531 0.012921652 0.024292601 0.001342095
## [3,] 0.02669930 0.024087418 0.004342302 0.029336412
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4220974 0.1710922 0.54123677 0.30041027
## [2,] 0.4346577 0.2744398 0.51594457 0.02850442
## [3,] 0.5670598 0.5115867 0.09222508 0.62306883
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2353799 0.09540845 0.30181723 0.16752187
## [2,] 0.2423841 0.15303960 0.28771319 0.01589531
## [3,] 0.3162173 0.28528308 0.05142873 0.34745035
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5696321 0.2308936 0.7304140 0.4054120
## [2,] 0.5865826 0.3703641 0.6962815 0.0384675
## [3,] 0.7652629 0.6904005 0.1244603 0.8408486
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1779964 0.07214874 0.22823696 0.12668157
## [2,] 0.1832931 0.11572995 0.21757135 0.01202018
## [3,] 0.2391264 0.21573368 0.03889088 0.26274515
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3592337 0.1456111 0.46062954 0.25566970
## [2,] 0.3699234 0.2335670 0.43910414 0.02425921
## [3,] 0.4826067 0.4353953 0.07848986 0.53027422
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2569565 0.1041543 0.32948391 0.18287809
## [2,] 0.2646027 0.1670683 0.31408699 0.01735238
## [3,] 0.3452040 0.3114341 0.05614304 0.37930008
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4441019 0.1800114 0.56945227 0.3160711
## [2,] 0.4573171 0.2887468 0.54284154 0.0299904
## [3,] 0.5966215 0.5382565 0.09703292 0.6555504
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06455750 0.02616762 0.08277923 0.045946123
## [2,] 0.06647854 0.04197408 0.07891093 0.004359597
## [3,] 0.08672873 0.07824442 0.01410533 0.095295003
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06705808 0.02718120 0.08598561 0.047725803
## [2,] 0.06905352 0.04359991 0.08196747 0.004528461
## [3,] 0.09008809 0.08127514 0.01465168 0.098986165
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5963638 0.2417290 0.7646909 0.42443714
## [2,] 0.6141097 0.3877445 0.7289566 0.04027271
## [3,] 0.8011752 0.7227996 0.1303010 0.88030797
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5225522 0.2118103 0.6700455 0.37190481
## [2,] 0.5381017 0.3397536 0.6387341 0.03528818
## [3,] 0.7020142 0.6333391 0.1141737 0.77135279
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5216853 0.2114590 0.6689341 0.37128788
## [2,] 0.5372091 0.3391900 0.6376745 0.03522964
## [3,] 0.7008497 0.6322885 0.1139843 0.77007324
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6347806 0.2573008 0.8139511 0.45177873
## [2,] 0.6536697 0.4127225 0.7759148 0.04286701
## [3,] 0.8527857 0.7693612 0.1386948 0.93701608
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6229856 0.2525198 0.7988268 0.44338411
## [2,] 0.6415237 0.4050535 0.7614974 0.04207049
## [3,] 0.8369398 0.7550655 0.1361176 0.91960513
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3213202 0.1302433 0.41201467 0.22868630
## [2,] 0.3308817 0.2089164 0.39276107 0.02169889
## [3,] 0.4316724 0.3894437 0.07020603 0.47430905
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1639657 0.06646154 0.21024594 0.11669577
## [2,] 0.1688448 0.10660741 0.20042107 0.01107267
## [3,] 0.2202770 0.19872825 0.03582526 0.24203399
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5519111 0.2237106 0.7076912 0.3927998
## [2,] 0.5683343 0.3588422 0.6746205 0.0372708
## [3,] 0.7414560 0.6689224 0.1205884 0.8146902
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3193535 0.1294462 0.40949291 0.22728661
## [2,] 0.3288565 0.2076377 0.39035714 0.02156608
## [3,] 0.4290303 0.3870601 0.06977633 0.47140601
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6259871 0.2537364 0.8026755 0.44552030
## [2,] 0.6446145 0.4070051 0.7651662 0.04227318
## [3,] 0.8409722 0.7587034 0.1367734 0.92403571
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2281728 0.09248715 0.29257593 0.16239254
## [2,] 0.2349626 0.14835370 0.27890375 0.01540861
## [3,] 0.3065351 0.27654804 0.04985404 0.33681182
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2559614 0.1037509 0.32820798 0.18216989
## [2,] 0.2635780 0.1664213 0.31287069 0.01728519
## [3,] 0.3438672 0.3102281 0.05592563 0.37783124
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5933617 0.2405121 0.7608415 0.42230058
## [2,] 0.6110184 0.3857927 0.7252871 0.04006998
## [3,] 0.7971422 0.7191611 0.1296451 0.87587663
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4484749 0.1817840 0.57505954 0.31918339
## [2,] 0.4618202 0.2915900 0.54818678 0.03028571
## [3,] 0.6024963 0.5435566 0.09798838 0.66200542
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1135453 0.04602423 0.14559408 0.080811129
## [2,] 0.1169240 0.07382501 0.13879041 0.007667762
## [3,] 0.1525405 0.13761814 0.02480878 0.167607109
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3504778 0.1420620 0.44940225 0.24943806
## [2,] 0.3609070 0.2278741 0.42840150 0.02366792
## [3,] 0.4708438 0.4247831 0.07657676 0.51734942
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3551903 0.1439722 0.45544476 0.25279192
## [2,] 0.3657596 0.2309380 0.43416164 0.02398615
## [3,] 0.4771746 0.4304946 0.07760639 0.52430553
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3910414 0.1585040 0.5014152 0.2783075
## [2,] 0.4026777 0.2542478 0.4779838 0.0264072
## [3,] 0.5253383 0.4739466 0.0854396 0.5772264
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3301894 0.1338383 0.42338721 0.23499856
## [2,] 0.3400148 0.2146829 0.40360216 0.02229783
## [3,] 0.4435875 0.4001932 0.07214388 0.48740106
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2559926 0.1037636 0.32824798 0.18219210
## [2,] 0.2636102 0.1664416 0.31290882 0.01728729
## [3,] 0.3439091 0.3102659 0.05593245 0.37787729
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.069347421 0.028109154 0.088921131 0.049355147
## [2,] 0.071410989 0.045088393 0.084765811 0.004683062
## [3,] 0.093163665 0.084049853 0.015151887 0.102365523
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.24366705 0.09876755 0.31244348 0.17341991
## [2,] 0.25091784 0.15842775 0.29784288 0.01645494
## [3,] 0.32735054 0.29532720 0.05323941 0.35968324
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.17761981 0.07199608 0.22775403 0.12641353
## [2,] 0.18290524 0.11548507 0.21711100 0.01199474
## [3,] 0.23862045 0.21527721 0.03880859 0.26218920
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.3910414 0.1585040 0.5014152 0.2783075
## [2,] 0.4026777 0.2542478 0.4779838 0.0264072
## [3,] 0.5253383 0.4739466 0.0854396 0.5772264
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.33018936 0.13383834 0.42338721 0.23499856
## [2,] 0.34001479 0.21468293 0.40360216 0.02229783
## [3,] 0.44358753 0.40019321 0.07214388 0.48740106
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.25599259 0.10376356 0.32824798 0.18219210
## [2,] 0.26361015 0.16644158 0.31290882 0.01728729
## [3,] 0.34390908 0.31026589 0.05593245 0.37787729
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.820392
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.820392
einsum::einsum('ij->', arrC)
## [1] 6.851943
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 6.851943
einsum::einsum('ijk->', arrE)
## [1] 32.99562
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 32.99562
einsum::einsum('ij->i', arrC)
## [1] 2.193376 1.916245 2.742323
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 2.193376 1.916245 2.742323
einsum::einsum('ij->j', arrC)
## [1] 2.176527 1.463108 1.757050 1.455258
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 2.176527 1.463108 1.757050 1.455258
einsum::einsum('ijk->i', arrE)
## [1] 9.158984 13.175552 10.661086
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 9.158984 13.175552 10.661086
einsum::einsum('ijk->j', arrE)
## [1] 5.535688 10.192347 7.664271 9.603315
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 5.535688 10.192347 7.664271 9.603315
einsum::einsum('ijk->k', arrE)
## [1] 6.959653 6.894601 5.137358 7.389835 6.614174
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 6.959653 6.894601 5.137358 7.389835 6.614174
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 1.386968 2.909971 1.406292 3.455753
## [2,] 2.933031 4.066809 3.415391 2.760320
## [3,] 1.215688 3.215568 2.842588 3.387242
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 1.386968 2.909971 1.406292 3.455753
## [2,] 2.933031 4.066809 3.415391 2.760320
## [3,] 1.215688 3.215568 2.842588 3.387242
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.760387 1.442760 0.327270 1.186555 1.818716
## [2,] 2.505952 1.792129 2.044907 1.838027 2.011333
## [3,] 1.503197 1.083891 1.049762 2.757801 1.269620
## [4,] 2.190117 2.575821 1.715420 1.607452 1.514505
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.760387 1.442760 0.327270 1.186555 1.818716
## [2,] 2.505952 1.792129 2.044907 1.838027 2.011333
## [3,] 1.503197 1.083891 1.049762 2.757801 1.269620
## [4,] 2.190117 2.575821 1.715420 1.607452 1.514505
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.760387 1.442760 0.327270 1.186555 1.818716
## [2,] 2.505952 1.792129 2.044907 1.838027 2.011333
## [3,] 1.503197 1.083891 1.049762 2.757801 1.269620
## [4,] 2.190117 2.575821 1.715420 1.607452 1.514505
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.760387 1.442760 0.327270 1.186555 1.818716
## [2,] 2.505952 1.792129 2.044907 1.838027 2.011333
## [3,] 1.503197 1.083891 1.049762 2.757801 1.269620
## [4,] 2.190117 2.575821 1.715420 1.607452 1.514505
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.350446
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 1.350446
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.6307840 0.44151633 0.938887267
## [2,] 0.8181075 0.06513377 0.005179228
## [3,] 0.3493343 0.29814907 0.654528272
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.630784017 0.441516325 0.938887267
## [2,] 0.818107528 0.065133767 0.005179228
## [3,] 0.349334269 0.298149073 0.654528272
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.9591049 0.4535650 0.3198905
## [2,] 0.3741849 0.0454401 0.9054403
## [3,] 0.8707380 0.4286372 0.6536397
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.9866978 0.6445178 0.3789828
## [2,] 0.7376704 0.8983378 0.6644647
## [3,] 0.7597456 0.2224295 0.1705449
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.2419014 0.4159604 0.7998105
## [2,] 0.1854034 0.3383961 0.2889970
## [3,] 0.6876714 0.7600557 0.8796796
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.9591049 0.4535650 0.3198905
## [2,] 0.3741849 0.0454401 0.9054403
## [3,] 0.8707380 0.4286372 0.6536397
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.9866978 0.6445178 0.3789828
## [2,] 0.7376704 0.8983378 0.6644647
## [3,] 0.7597456 0.2224295 0.1705449
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.2419014 0.4159604 0.7998105
## [2,] 0.1854034 0.3383961 0.2889970
## [3,] 0.6876714 0.7600557 0.8796796
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.379631
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.379631
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 4.911659
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 4.911659
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 23.24819
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 23.24819
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.2299368 0.9056921 0.05048756 0.6423161 1.3112157
## [2,] 2.1117424 1.1619675 1.44634775 1.5208960 1.4861090
## [3,] 1.0826526 0.6042954 0.56195847 2.5537237 0.6290254
## [4,] 1.7134880 2.2401687 1.16542792 1.0441923 0.7865493
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.22993678 0.90569209 0.05048756 0.64231613 1.31121566
## [2,] 2.11174239 1.16196750 1.44634775 1.52089600 1.48610903
## [3,] 1.08265264 0.60429541 0.56195847 2.55372369 0.62902537
## [4,] 1.71348796 2.24016869 1.16542792 1.04419229 0.78654933
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.380165 1.211007 1.317895
## [2,] 1.211007 1.241437 1.056746
## [3,] 1.317895 1.056746 2.290057
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.380165 1.211007 1.317895
## [2,] 1.211007 1.241437 1.056746
## [3,] 1.317895 1.056746 2.290057
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.41633832 0.441484909 0.75141333
## [2,] 0.06840397 0.176001060 0.61158902
## [3,] 0.68453509 0.622052743 0.01987555
## [4,] 0.21088742 0.001898653 0.90717940
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.416338323 0.441484909 0.751413331
## [2,] 0.068403970 0.176001060 0.611589015
## [3,] 0.684535093 0.622052743 0.019875553
## [4,] 0.210887415 0.001898653 0.907179396
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.01155086 0.093088238 0.0065969653 0.1585889 0.24496106
## [2,] 0.60571692 0.704258662 0.6270651688 0.0108008 0.15736297
## [3,] 0.08846571 0.008756565 0.0009486823 0.6536886 0.03096647
## [4,] 0.22520234 0.988749528 0.7793678331 0.2479874 0.36728161
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.1426091 0.7335367 0.001640189 0.47371696 0.9412052
## [2,] 0.8954686 0.2696942 0.573585790 0.85423249 0.8456540
## [3,] 0.9378594 0.0706678 0.427936051 0.96783407 0.2950358
## [4,] 0.9009510 0.5920269 0.076098522 0.06457427 0.2618664
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.07577683 0.07906718 0.04225041 0.0100103 0.1250494
## [2,] 0.61055683 0.18801459 0.24569679 0.6558627 0.4830921
## [3,] 0.05632751 0.52487104 0.13307373 0.9322011 0.3030231
## [4,] 0.58733461 0.65939226 0.30996157 0.7316306 0.1574013
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.0115508578 0.0930882384 0.0065969653 0.1585888675 0.2449610597
## [2,] 0.6057169228 0.7042586624 0.6270651688 0.0108007974 0.1573629702
## [3,] 0.0884657135 0.0087565653 0.0009486823 0.6536885667 0.0309664692
## [4,] 0.2252023439 0.9887495280 0.7793678331 0.2479874201 0.3672816149
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.142609096 0.733536672 0.001640189 0.473716963 0.941205235
## [2,] 0.895468633 0.269694246 0.573585790 0.854232493 0.845653985
## [3,] 0.937859415 0.070667799 0.427936051 0.967834070 0.295035831
## [4,] 0.900951003 0.592026905 0.076098522 0.064574275 0.261866386
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.07577683 0.07906718 0.04225041 0.01001030 0.12504936
## [2,] 0.61055683 0.18801459 0.24569679 0.65586271 0.48309207
## [3,] 0.05632751 0.52487104 0.13307373 0.93220105 0.30302307
## [4,] 0.58733461 0.65939226 0.30996157 0.73163060 0.15740133
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 1.657740 3.241544 2.060369
## [2,] 2.232240 2.411056 2.251305
## [3,] 1.786715 1.727882 1.622762
## [4,] 1.808653 2.850418 2.730765
## [5,] 1.673636 2.944653 1.995885
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.657740 3.241544 2.060369
## [2,] 2.232240 2.411056 2.251305
## [3,] 1.786715 1.727882 1.622762
## [4,] 1.808653 2.850418 2.730765
## [5,] 1.673636 2.944653 1.995885
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.0008666457 0.006984289 0.0004949617 0.0118987150 0.018379107
## [2,] 0.0074667565 0.008681494 0.0077299193 0.0001331429 0.001939835
## [3,] 0.0109131890 0.001080216 0.0001170301 0.0806394544 0.003820044
## [4,] 0.0085586358 0.037576639 0.0296192539 0.0094245644 0.013958245
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.049612856 0.2551930434 0.0005706121 1.648033e-01 0.3274397008
## [2,] 0.124192909 0.0374040046 0.0795508463 1.184739e-01 0.1172840948
## [3,] 0.459722849 0.0346401616 0.2097670266 4.744159e-01 0.1446215828
## [4,] 0.001347963 0.0008857642 0.0001138552 9.661314e-05 0.0003917928
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0485224273 0.050629347 0.027054341 0.006409928 0.080073271
## [2,] 0.3182093696 0.097989245 0.128051995 0.341821836 0.251777418
## [3,] 0.0009540408 0.008889944 0.002253921 0.015789050 0.005132419
## [4,] 0.4540523845 0.509758187 0.239622842 0.565603681 0.121682681
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.0008666457 0.0069842886 0.0004949617 0.0118987150 0.0183791074
## [2,] 0.0074667565 0.0086814942 0.0077299193 0.0001331429 0.0019398351
## [3,] 0.0109131890 0.0010802157 0.0001170301 0.0806394544 0.0038200441
## [4,] 0.0085586358 0.0375766385 0.0296192539 0.0094245644 0.0139582453
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 4.961286e-02 2.551930e-01 5.706121e-04 1.648033e-01 3.274397e-01
## [2,] 1.241929e-01 3.740400e-02 7.955085e-02 1.184739e-01 1.172841e-01
## [3,] 4.597228e-01 3.464016e-02 2.097670e-01 4.744159e-01 1.446216e-01
## [4,] 1.347963e-03 8.857642e-04 1.138552e-04 9.661314e-05 3.917928e-04
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0485224273 0.0506293472 0.0270543412 0.0064099282 0.0800732708
## [2,] 0.3182093696 0.0979892450 0.1280519951 0.3418218360 0.2517774185
## [3,] 0.0009540408 0.0088899440 0.0022539213 0.0157890501 0.0051324191
## [4,] 0.4540523845 0.5097581874 0.2396228420 0.5656036806 0.1216826806
einsum
By using einsum
and other DelayedTensor functions,
it is possible to implement your original tensor calculation functions.
It is intended to be applied to Delayed Arrays,
which can scale to large-scale data
since the calculation is performed internally by block processing.
For example, kronecker
can be easily implmented by eimsum
and other DelayedTensor functions4 https://stackoverflow.com/
questions/56067643/speeding-up-kronecker-products-numpy
(the kronecker
function inside DelayedTensor
has a more efficient implementation though).
darr1 <- DelayedArray(array(1:6, dim=c(2,3)))
darr2 <- DelayedArray(array(20:1, dim=c(4,5)))
mykronecker <- function(darr1, darr2){
stopifnot((length(dim(darr1)) == 2) && (length(dim(darr2)) == 2))
# Outer Product
tmpdarr <- DelayedTensor::einsum('ij,kl->ikjl', darr1, darr2)
# Reshape
DelayedTensor::unfold(tmpdarr, row_idx=c(2,1), col_idx=c(4,3))
}
identical(as.array(DelayedTensor::kronecker(darr1, darr2)),
as.array(mykronecker(darr1, darr2)))
## [1] TRUE
## R version 4.4.0 Patched (2024-04-24 r86482)
## Platform: x86_64-apple-darwin20
## Running under: macOS Monterey 12.7.4
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.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.2 DelayedRandomArray_1.11.0
## [3] HDF5Array_1.31.6 rhdf5_2.47.7
## [5] DelayedArray_0.29.9 SparseArray_1.3.7
## [7] S4Arrays_1.3.7 abind_1.4-5
## [9] IRanges_2.37.1 S4Vectors_0.41.7
## [11] MatrixGenerics_1.15.1 matrixStats_1.3.0
## [13] BiocGenerics_0.49.1 Matrix_1.7-0
## [15] DelayedTensor_1.9.0 BiocStyle_2.31.0
##
## loaded via a namespace (and not attached):
## [1] jsonlite_1.8.8 compiler_4.4.0 BiocManager_1.30.22
## [4] crayon_1.5.2 rsvd_1.0.5 Rcpp_1.0.12
## [7] rhdf5filters_1.15.5 parallel_4.4.0 jquerylib_0.1.4
## [10] BiocParallel_1.37.1 yaml_2.3.8 fastmap_1.1.1
## [13] lattice_0.22-6 R6_2.5.1 XVector_0.43.1
## [16] ScaledMatrix_1.11.1 knitr_1.46 bookdown_0.39
## [19] bslib_0.7.0 rlang_1.1.3 cachem_1.0.8
## [22] xfun_0.43 sass_0.4.9 cli_3.6.2
## [25] Rhdf5lib_1.25.3 BiocSingular_1.19.0 zlibbioc_1.49.3
## [28] digest_0.6.35 grid_4.4.0 irlba_2.3.5.1
## [31] rTensor_1.4.8 dqrng_0.3.2 lifecycle_1.0.4
## [34] evaluate_0.23 codetools_0.2-20 beachmat_2.19.4
## [37] rmarkdown_2.26 tools_4.4.0 htmltools_0.5.8.1