DelayedTensor 1.0.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2021-10-26 15:33:21
Compiled: Tue Oct 26 17:17:59 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.04351261 0.51726631 0.47846984
DelayedTensor::einsum('i->i', darrA)
## <3> array of class DelayedArray and type "double":
## [1] [2] [3]
## 0.04351261 0.51726631 0.47846984
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.07960809 0.9694293 0.92319702 0.3385786
## [2,] 0.97537646 0.8081915 0.44895901 0.8314638
## [3,] 0.64610725 0.2409574 0.06983184 0.2982381
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.07960809 0.96942934 0.92319702 0.33857864
## [2,] 0.97537646 0.80819148 0.44895901 0.83146381
## [3,] 0.64610725 0.24095737 0.06983184 0.29823806
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3079833 0.1869298 0.9608465 0.9659390
## [2,] 0.6834736 0.5848407 0.4054105 0.9389173
## [3,] 0.3693574 0.6139628 0.3928506 0.6730643
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.81011123 0.2676450 0.4446518 0.3443087
## [2,] 0.09173055 0.9421209 0.9922351 0.6486499
## [3,] 0.32707647 0.3293384 0.3400049 0.2220610
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5659351 0.9564408 0.08139405 0.1302899
## [2,] 0.9522219 0.3631069 0.54872065 0.4275958
## [3,] 0.5365632 0.8662694 0.05222397 0.2332027
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6811784 0.1161214 0.7726239 0.4027592
## [2,] 0.7066175 0.8724491 0.2624791 0.5465645
## [3,] 0.4920926 0.7506163 0.5553058 0.6694527
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6989823 0.8587177 0.002128638 0.7857134
## [2,] 0.3069388 0.3256864 0.642333269 0.1007636
## [3,] 0.7913248 0.5589708 0.542754170 0.4508501
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.3079833 0.1869298 0.9608465 0.9659390
## [2,] 0.6834736 0.5848407 0.4054105 0.9389173
## [3,] 0.3693574 0.6139628 0.3928506 0.6730643
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.81011123 0.26764498 0.44465178 0.34430868
## [2,] 0.09173055 0.94212086 0.99223510 0.64864986
## [3,] 0.32707647 0.32933837 0.34000492 0.22206098
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.56593509 0.95644076 0.08139405 0.13028988
## [2,] 0.95222192 0.36310685 0.54872065 0.42759585
## [3,] 0.53656320 0.86626935 0.05222397 0.23320271
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.6811784 0.1161214 0.7726239 0.4027592
## [2,] 0.7066175 0.8724491 0.2624791 0.5465645
## [3,] 0.4920926 0.7506163 0.5553058 0.6694527
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.698982327 0.858717707 0.002128638 0.785713391
## [2,] 0.306938789 0.325686386 0.642333269 0.100763567
## [3,] 0.791324772 0.558970827 0.542754170 0.450850068
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.3119922 0.2685346 0.9791065
DelayedTensor::einsum('ii->i', darrB)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.3119922 0.2685346 0.9791065
einsum::einsum('iii->i', arrD)
## [1] 0.6685696 0.9010539 0.9507127
DelayedTensor::einsum('iii->i', darrD)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.6685696 0.9010539 0.9507127
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.001893347 0.267564431 0.228933384
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.001893347 0.267564431 0.228933384
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.006337447 0.93979324 0.852292738 0.11463549
## [2,] 0.951359248 0.65317347 0.201564192 0.69133207
## [3,] 0.417454580 0.05806046 0.004876486 0.08894594
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.006337447 0.939793238 0.852292738 0.114635494
## [2,] 0.951359248 0.653173472 0.201564192 0.691332075
## [3,] 0.417454580 0.058060455 0.004876486 0.088945940
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0948537 0.03494274 0.9232260 0.9330382
## [2,] 0.4671362 0.34203864 0.1643577 0.8815656
## [3,] 0.1364249 0.37695032 0.1543316 0.4530156
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.656280211 0.07163384 0.1977152 0.11854847
## [2,] 0.008414494 0.88759172 0.9845305 0.42074664
## [3,] 0.106979020 0.10846376 0.1156033 0.04931108
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3202825 0.9147789 0.006624991 0.01697545
## [2,] 0.9067266 0.1318466 0.301094353 0.18283821
## [3,] 0.2879001 0.7504226 0.002727343 0.05438350
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4640040 0.01348418 0.59694766 0.1622150
## [2,] 0.4993083 0.76116737 0.06889528 0.2987328
## [3,] 0.2421551 0.56342490 0.30836455 0.4481669
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.48857629 0.7373961 4.531098e-06 0.6173455
## [2,] 0.09421142 0.1060716 4.125920e-01 0.0101533
## [3,] 0.62619489 0.3124484 2.945821e-01 0.2032658
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.09485370 0.03494274 0.92322600 0.93303822
## [2,] 0.46713617 0.34203864 0.16435770 0.88156562
## [3,] 0.13642488 0.37695032 0.15433163 0.45301556
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.656280211 0.071633837 0.197715206 0.118548470
## [2,] 0.008414494 0.887591721 0.984530495 0.420746641
## [3,] 0.106979020 0.108463764 0.115603344 0.049311078
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.320282521 0.914778926 0.006624991 0.016975453
## [2,] 0.906726586 0.131846586 0.301094353 0.182838210
## [3,] 0.287900065 0.750422592 0.002727343 0.054383504
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.46400401 0.01348418 0.59694766 0.16221501
## [2,] 0.49930832 0.76116737 0.06889528 0.29873277
## [3,] 0.24215508 0.56342490 0.30836455 0.44816691
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 4.885763e-01 7.373961e-01 4.531098e-06 6.173455e-01
## [2,] 9.421142e-02 1.060716e-01 4.125920e-01 1.015330e-02
## [3,] 6.261949e-01 3.124484e-01 2.945821e-01 2.032658e-01
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.001893347 0.02250761 0.02081947
## [2,] 0.022507606 0.26756443 0.24749632
## [3,] 0.020819470 0.24749632 0.22893338
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.001893347 0.022507606 0.020819470
## [2,] 0.022507606 0.267564431 0.247496325
## [3,] 0.020819470 0.247496325 0.228933384
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02451796 0.29856802 0.28432924 0.10427656
## [2,] 0.30039964 0.24890946 0.13827187 0.25607695
## [3,] 0.19899023 0.07421084 0.02150704 0.09185233
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05441003 0.6625794 0.63098080 0.2314096
## [2,] 0.66664407 0.5523775 0.30685163 0.5682836
## [3,] 0.44159725 0.1646880 0.04772822 0.2038378
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02940383 0.35806588 0.34098964 0.1250565
## [2,] 0.36026250 0.29851149 0.16582633 0.3071073
## [3,] 0.23864448 0.08899938 0.02579291 0.1101564
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01488112 0.18121521 0.17257301 0.06329043
## [2,] 0.18232691 0.15107505 0.08392381 0.15542535
## [3,] 0.12077669 0.04504211 0.01305365 0.05574957
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04655805 0.5669617 0.5399232 0.1980146
## [2,] 0.57043985 0.4726633 0.2625695 0.4862739
## [3,] 0.37786982 0.1409217 0.0408405 0.1744218
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0488764 0.5951936 0.56680863 0.2078747
## [2,] 0.5988449 0.4961995 0.27564413 0.5104879
## [3,] 0.3966858 0.1479389 0.04287415 0.1831071
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07649115 0.9314728 0.88705063 0.3253221
## [2,] 0.93718706 0.7765480 0.43138069 0.7989091
## [3,] 0.62080989 0.2315230 0.06709768 0.2865610
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03227396 0.39301686 0.37427380 0.1372633
## [2,] 0.39542789 0.32764934 0.18201271 0.3370842
## [3,] 0.26193869 0.09768666 0.02831056 0.1209089
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03127409 0.38084094 0.36267854 0.1330108
## [2,] 0.38317727 0.31749854 0.17637384 0.3266411
## [3,] 0.25382365 0.09466026 0.02743348 0.1171630
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07689656 0.9364096 0.8917520 0.3270463
## [2,] 0.94215420 0.7806637 0.4336670 0.8031434
## [3,] 0.62410022 0.2327501 0.0674533 0.2880798
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07474541 0.9102139 0.86680562 0.3178973
## [2,] 0.91579780 0.7588249 0.42153536 0.7806757
## [3,] 0.60664125 0.2262390 0.06556632 0.2800209
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05358136 0.6524883 0.62137096 0.2278852
## [2,] 0.65649108 0.5439648 0.30217828 0.5596286
## [3,] 0.43487173 0.1621798 0.04700132 0.2007334
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0644914 0.7853456 0.74789228 0.2742864
## [2,] 0.7901634 0.6547250 0.36370674 0.6735782
## [3,] 0.5234187 0.1952023 0.05657156 0.2416060
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.007302494 0.08892629 0.084685372 0.03105801
## [2,] 0.089471822 0.07413585 0.041183258 0.07627063
## [3,] 0.059267775 0.02210315 0.006405713 0.02735754
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02603793 0.31707753 0.30195603 0.11074111
## [2,] 0.31902269 0.26434042 0.14684393 0.27195225
## [3,] 0.21132648 0.07881149 0.02284035 0.09754665
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0213067 0.25946290 0.24708905 0.09061887
## [2,] 0.2610546 0.21630840 0.12016163 0.22253712
## [3,] 0.1729274 0.06449103 0.01869014 0.07982192
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07500044 0.9133196 0.86976317 0.3189820
## [2,] 0.91892252 0.7614141 0.42297365 0.7833394
## [3,] 0.60871112 0.2270110 0.06579004 0.2809763
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0262180 0.31927028 0.30404420 0.11150694
## [2,] 0.3212289 0.26616847 0.14785943 0.27383294
## [3,] 0.2127879 0.07935651 0.02299831 0.09822124
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03539788 0.4310585 0.41050120 0.1505496
## [2,] 0.43370288 0.3593638 0.19963042 0.3697119
## [3,] 0.28729274 0.1071421 0.03105085 0.1326121
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07898994 0.9619018 0.9160285 0.3359496
## [2,] 0.96780276 0.8019160 0.4454729 0.8250076
## [3,] 0.64109029 0.2390864 0.0692896 0.2959223
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02706714 0.32961074 0.31389153 0.1151184
## [2,] 0.33163279 0.27478908 0.15264827 0.2827018
## [3,] 0.21967964 0.08192669 0.02374317 0.1014024
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02740976 0.33378294 0.31786475 0.1165756
## [2,] 0.33583059 0.27826735 0.15458049 0.2862802
## [3,] 0.22246034 0.08296372 0.02404371 0.1026860
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05163777 0.6288202 0.59883162 0.2196190
## [2,] 0.63267781 0.5242333 0.29121720 0.5393289
## [3,] 0.41909738 0.1562970 0.04529641 0.1934521
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01767785 0.21527243 0.20500603 0.07518510
## [2,] 0.21659305 0.17946779 0.09969628 0.18463567
## [3,] 0.14347521 0.05350723 0.01550693 0.06622703
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04505301 0.5486341 0.52246958 0.1916135
## [2,] 0.55199976 0.4573839 0.25408166 0.4705545
## [3,] 0.36565476 0.1363662 0.03952029 0.1687834
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07580456 0.9231119 0.87908844 0.3224020
## [2,] 0.92877485 0.7695776 0.42750861 0.7917381
## [3,] 0.61523749 0.2294449 0.06649541 0.2839888
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04271477 0.5201601 0.4953535 0.1816688
## [2,] 0.52335112 0.4336458 0.2408949 0.4461329
## [3,] 0.34667737 0.1292889 0.0374692 0.1600236
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07614042 0.9272017 0.88298326 0.3238304
## [2,] 0.93288981 0.7729873 0.42940270 0.7952459
## [3,] 0.61796331 0.2304615 0.06679002 0.2852470
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02890624 0.35200643 0.33521916 0.1229402
## [2,] 0.35416588 0.29345986 0.16302009 0.3019102
## [3,] 0.23460597 0.08749327 0.02535642 0.1082923
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06896204 0.8397869 0.79973729 0.2933003
## [2,] 0.84493874 0.7001115 0.38891943 0.7202716
## [3,] 0.55970291 0.2087340 0.06049318 0.2583545
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.006479624 0.07890578 0.075142742 0.02755829
## [2,] 0.079389838 0.06578198 0.036542591 0.06767621
## [3,] 0.052589284 0.01961250 0.005683896 0.02427480
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0436826 0.5319459 0.50657727 0.1857851
## [2,] 0.5352092 0.4434714 0.24635308 0.4562414
## [3,] 0.3545324 0.1322183 0.03831817 0.1636494
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.00415745 0.05062745 0.048213013 0.01768192
## [2,] 0.05093803 0.04220697 0.023446422 0.04342234
## [3,] 0.03374229 0.01258375 0.003646896 0.01557518
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01037213 0.12630683 0.120283228 0.04411337
## [2,] 0.12708168 0.10529917 0.058494815 0.10833132
## [3,] 0.08418124 0.03139431 0.009098382 0.03885740
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03404009 0.4145240 0.39475521 0.1447748
## [2,] 0.41706693 0.3455793 0.19197301 0.3555305
## [3,] 0.27627278 0.1030324 0.02985981 0.1275254
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01856482 0.22607355 0.21529205 0.07895746
## [2,] 0.22746043 0.18847244 0.10469846 0.19389961
## [3,] 0.15067396 0.05619191 0.01628497 0.06954992
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05422731 0.6603543 0.62886187 0.2306325
## [2,] 0.66440538 0.5505226 0.30582118 0.5663752
## [3,] 0.44011430 0.1641350 0.04756794 0.2031533
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05625247 0.6850158 0.6523472 0.2392456
## [2,] 0.68921810 0.5710823 0.3172423 0.5875269
## [3,] 0.45655070 0.1702647 0.0493444 0.2107402
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03917455 0.4770490 0.45429838 0.1666120
## [2,] 0.47997550 0.3977050 0.22092939 0.4091572
## [3,] 0.31794457 0.1185733 0.03436373 0.1467607
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.009244202 0.11257149 0.107202929 0.03931622
## [2,] 0.113262079 0.09384832 0.052133748 0.09655074
## [3,] 0.075026877 0.02798031 0.008108971 0.03463182
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0694540 0.8457777 0.80544237 0.2953926
## [2,] 0.8509663 0.7051059 0.39169387 0.7254098
## [3,] 0.5636957 0.2102230 0.06092473 0.2601975
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05975513 0.7276695 0.69296677 0.2541427
## [2,] 0.73213352 0.6066417 0.33699597 0.6241103
## [3,] 0.48497866 0.1808665 0.05241692 0.2238624
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06150711 0.7490043 0.71328406 0.2615939
## [2,] 0.75359915 0.6244280 0.34687645 0.6424088
## [3,] 0.49919789 0.1861694 0.05395375 0.2304258
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02089546 0.25445495 0.2423199 0.08886982
## [2,] 0.25601595 0.21213338 0.1178424 0.21824188
## [3,] 0.16958966 0.06324628 0.0183294 0.07828126
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04420683 0.5383298 0.51265668 0.1880147
## [2,] 0.54163223 0.4487934 0.24930955 0.4617167
## [3,] 0.35878712 0.1338050 0.03877803 0.1656133
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03206289 0.39044663 0.37182614 0.1363657
## [2,] 0.39284189 0.32550659 0.18082239 0.3348797
## [3,] 0.26022567 0.09704781 0.02812542 0.1201181
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04351096 0.5298557 0.50458674 0.1850551
## [2,] 0.53310617 0.4417288 0.24538506 0.4544486
## [3,] 0.35313930 0.1316988 0.03816761 0.1630063
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05329385 0.6489871 0.61803673 0.2266624
## [2,] 0.65296840 0.5410460 0.30055682 0.5566257
## [3,] 0.43253824 0.1613096 0.04674911 0.1996563
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05564464 0.6776140 0.64529840 0.2366605
## [2,] 0.68177091 0.5649116 0.31381441 0.5811785
## [3,] 0.45161755 0.1684249 0.04881122 0.2084631
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02443481 0.29755547 0.2833650 0.10392292
## [2,] 0.29938087 0.24806531 0.1378029 0.25520850
## [3,] 0.19831538 0.07395916 0.0214341 0.09154083
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06299585 0.7671334 0.73054867 0.2679257
## [2,] 0.77183956 0.6395419 0.35527239 0.6579579
## [3,] 0.51128067 0.1906755 0.05525967 0.2360032
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06836087 0.8324661 0.79276563 0.2907435
## [2,] 0.83757304 0.6940083 0.38552905 0.7139927
## [3,] 0.55482374 0.2069144 0.05996584 0.2561023
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02592727 0.31572994 0.30067270 0.11027045
## [2,] 0.31766684 0.26321696 0.14621984 0.27079644
## [3,] 0.21042834 0.07847654 0.02274328 0.09713208
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0444986 0.5418827 0.51604020 0.1892556
## [2,] 0.5452070 0.4517555 0.25095499 0.4647640
## [3,] 0.3611551 0.1346881 0.03903396 0.1667064
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0001694568 0.0020635638 0.0019651519 0.0007207112
## [2,] 0.0020762230 0.0017203468 0.0009556710 0.0017698852
## [3,] 0.0013753282 0.0005129109 0.0001486467 0.0006348408
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05113492 0.6226967 0.59300016 0.2174803
## [2,] 0.62651675 0.5191283 0.28838131 0.5340769
## [3,] 0.41501618 0.1547749 0.04485532 0.1915682
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04320762 0.5261618 0.50106903 0.1837650
## [2,] 0.52938964 0.4386493 0.24367437 0.4512805
## [3,] 0.35067741 0.1307806 0.03790152 0.1618700
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06254914 0.7616936 0.72536826 0.2660258
## [2,] 0.76636635 0.6350069 0.35275311 0.6532923
## [3,] 0.50765512 0.1893234 0.05486781 0.2343296
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.008021595 0.09768316 0.093024625 0.03411639
## [2,] 0.098282412 0.08143626 0.045238711 0.08378126
## [3,] 0.065104071 0.02427972 0.007036506 0.03005153
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03589131 0.4370673 0.41622344 0.1526482
## [2,] 0.43974855 0.3643732 0.20241320 0.3748655
## [3,] 0.29129750 0.1086356 0.03148369 0.1344606
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.02451796 0.29856802 0.28432924 0.10427656
## [2,] 0.30039964 0.24890946 0.13827187 0.25607695
## [3,] 0.19899023 0.07421084 0.02150704 0.09185233
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.05441003 0.66257936 0.63098080 0.23140956
## [2,] 0.66664407 0.55237755 0.30685163 0.56828357
## [3,] 0.44159725 0.16468800 0.04772822 0.20383784
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.02940383 0.35806588 0.34098964 0.12505652
## [2,] 0.36026250 0.29851149 0.16582633 0.30710730
## [3,] 0.23864448 0.08899938 0.02579291 0.11015643
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.06254914 0.76169361 0.72536826 0.26602577
## [2,] 0.76636635 0.63500687 0.35275311 0.65329225
## [3,] 0.50765512 0.18932343 0.05486781 0.23432964
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.008021595 0.097683158 0.093024625 0.034116391
## [2,] 0.098282412 0.081436257 0.045238711 0.083781260
## [3,] 0.065104071 0.024279724 0.007036506 0.030051531
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.03589131 0.43706728 0.41622344 0.15264820
## [2,] 0.43974855 0.36437318 0.20241320 0.37486552
## [3,] 0.29129750 0.10863565 0.03148369 0.13446065
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.039249
DelayedTensor::einsum('i->', darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 1.039249
einsum::einsum('ij->', arrC)
## [1] 6.629938
DelayedTensor::einsum('ij->', darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 6.629938
einsum::einsum('ijk->', arrE)
## [1] 31.4509
DelayedTensor::einsum('ijk->', darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 31.4509
einsum::einsum('ij->i', arrC)
## [1] 2.310813 3.063991 1.255135
DelayedTensor::einsum('ij->i', darrC)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 2.310813 3.063991 1.255135
einsum::einsum('ij->j', arrC)
## [1] 1.701092 2.018578 1.441988 1.468281
DelayedTensor::einsum('ij->j', darrC)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 1.701092 2.018578 1.441988 1.468281
einsum::einsum('ijk->i', arrE)
## [1] 10.340700 11.342856 9.767342
DelayedTensor::einsum('ijk->i', darrE)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 10.340700 11.342856 9.767342
einsum::einsum('ijk->j', arrE)
## [1] 8.321587 8.593216 6.995963 7.540132
DelayedTensor::einsum('ijk->j', darrE)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 8.321587 8.593216 6.995963 7.540132
einsum::einsum('ijk->k', arrE)
## [1] 7.083576 5.759934 5.713964 6.828261 6.065164
DelayedTensor::einsum('ijk->k', darrE)
## <5> array of class HDF5Array and type "double":
## [1] [2] [3] [4] [5]
## 7.083576 5.759934 5.713964 6.828261 6.065164
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 3.064190 2.385855 2.261645 2.629010
## [2,] 2.740982 3.088204 2.851179 2.662491
## [3,] 2.516414 3.119158 1.883140 2.248631
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 3.064190 2.385855 2.261645 2.629010
## [2,] 2.740982 3.088204 2.851179 2.662491
## [3,] 2.516414 3.119158 1.883140 2.248631
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.360814 1.228918 2.0547202 1.879888 1.797246
## [2,] 1.385733 1.539104 2.1858170 1.739187 1.743375
## [3,] 1.759108 1.776892 0.6823387 1.590409 1.187216
## [4,] 2.577921 1.215020 0.7910884 1.618776 1.337327
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.3608143 1.2289183 2.0547202 1.8798885 1.7972459
## [2,] 1.3857333 1.5391042 2.1858170 1.7391868 1.7433749
## [3,] 1.7591077 1.7768918 0.6823387 1.5904088 1.1872161
## [4,] 2.5779206 1.2150195 0.7910884 1.6187765 1.3373270
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.360814 1.228918 2.0547202 1.879888 1.797246
## [2,] 1.385733 1.539104 2.1858170 1.739187 1.743375
## [3,] 1.759108 1.776892 0.6823387 1.590409 1.187216
## [4,] 2.577921 1.215020 0.7910884 1.618776 1.337327
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.3608143 1.2289183 2.0547202 1.8798885 1.7972459
## [2,] 1.3857333 1.5391042 2.1858170 1.7391868 1.7433749
## [3,] 1.7591077 1.7768918 0.6823387 1.5904088 1.1872161
## [4,] 2.5779206 1.2150195 0.7910884 1.6187765 1.3373270
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.559633
DelayedTensor::einsum('ii->', darrB)
## <1> array of class HDF5Array and type "double":
## [1]
## 1.559633
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.3119922 0.7114224 0.5188113
## [2,] 0.4588809 0.2685346 0.5647092
## [3,] 0.4801963 0.9221895 0.9791065
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3]
## [1,] 0.3119922 0.7114224 0.5188113
## [2,] 0.4588809 0.2685346 0.5647092
## [3,] 0.4801963 0.9221895 0.9791065
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.6685696 0.95433102 0.4027641
## [2,] 0.3858977 0.06197466 0.2906929
## [3,] 0.3704241 0.99258304 0.4433748
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.08922602 0.5486918 0.7525677
## [2,] 0.55640363 0.9010539 0.1394297
## [3,] 0.97903532 0.8393459 0.1808600
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.8501638 0.15527714 0.7246377
## [2,] 0.3360796 0.59717435 0.7186217
## [3,] 0.7094205 0.07561564 0.9507127
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.66856958 0.95433102 0.40276410
## [2,] 0.38589770 0.06197466 0.29069292
## [3,] 0.37042409 0.99258304 0.44337481
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.08922602 0.54869182 0.75256768
## [2,] 0.55640363 0.90105392 0.13942973
## [3,] 0.97903532 0.83934594 0.18085996
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.85016383 0.15527714 0.72463770
## [2,] 0.33607963 0.59717435 0.71862167
## [3,] 0.70942048 0.07561564 0.95071269
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.4983912
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 0.4983912
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 4.979825
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 4.979825
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 20.89401
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 20.89401
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.6984147 0.7716737 1.5149092 1.2054674 1.2089826
## [2,] 0.7539317 1.0676893 1.7970481 1.3380764 1.1559161
## [3,] 1.2419153 1.2978490 0.3104467 0.9742075 0.7071786
## [4,] 2.2676194 0.5886062 0.2541972 0.9091147 0.8307646
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.6984147 0.7716737 1.5149092 1.2054674 1.2089826
## [2,] 0.7539317 1.0676893 1.7970481 1.3380764 1.1559161
## [3,] 1.2419153 1.2978490 0.3104467 0.9742075 0.7071786
## [4,] 2.2676194 0.5886062 0.2541972 0.9091147 0.8307646
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.9130589 1.557126 0.4504721
## [2,] 1.5571259 2.497429 1.1042633
## [3,] 0.4504721 1.104263 0.5693375
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 1.9130589 1.5571259 0.4504721
## [2,] 1.5571259 2.4974290 1.1042633
## [3,] 0.4504721 1.1042633 0.5693375
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.006337447 0.9513592 0.417454580
## [2,] 0.939793238 0.6531735 0.058060455
## [3,] 0.852292738 0.2015642 0.004876486
## [4,] 0.114635494 0.6913321 0.088945940
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.006337447 0.951359248 0.417454580
## [2,] 0.939793238 0.653173472 0.058060455
## [3,] 0.852292738 0.201564192 0.004876486
## [4,] 0.114635494 0.691332075 0.088945940
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.09485370 0.65628021 0.320282521 0.46400401 4.885763e-01
## [2,] 0.03494274 0.07163384 0.914778926 0.01348418 7.373961e-01
## [3,] 0.92322600 0.19771521 0.006624991 0.59694766 4.531098e-06
## [4,] 0.93303822 0.11854847 0.016975453 0.16221501 6.173455e-01
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.4671362 0.008414494 0.9067266 0.49930832 0.09421142
## [2,] 0.3420386 0.887591721 0.1318466 0.76116737 0.10607162
## [3,] 0.1643577 0.984530495 0.3010944 0.06889528 0.41259203
## [4,] 0.8815656 0.420746641 0.1828382 0.29873277 0.01015330
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.1364249 0.10697902 0.287900065 0.2421551 0.6261949
## [2,] 0.3769503 0.10846376 0.750422592 0.5634249 0.3124484
## [3,] 0.1543316 0.11560334 0.002727343 0.3083646 0.2945821
## [4,] 0.4530156 0.04931108 0.054383504 0.4481669 0.2032658
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,] 9.485370e-02 6.562802e-01 3.202825e-01 4.640040e-01 4.885763e-01
## [2,] 3.494274e-02 7.163384e-02 9.147789e-01 1.348418e-02 7.373961e-01
## [3,] 9.232260e-01 1.977152e-01 6.624991e-03 5.969477e-01 4.531098e-06
## [4,] 9.330382e-01 1.185485e-01 1.697545e-02 1.622150e-01 6.173455e-01
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.467136170 0.008414494 0.906726586 0.499308318 0.094211420
## [2,] 0.342038644 0.887591721 0.131846586 0.761167367 0.106071622
## [3,] 0.164357700 0.984530495 0.301094353 0.068895284 0.412592028
## [4,] 0.881565622 0.420746641 0.182838210 0.298732774 0.010153296
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.136424877 0.106979020 0.287900065 0.242155082 0.626194894
## [2,] 0.376950322 0.108463764 0.750422592 0.563424900 0.312448385
## [3,] 0.154331628 0.115603344 0.002727343 0.308364552 0.294582089
## [4,] 0.453015557 0.049311078 0.054383504 0.448166911 0.203265784
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 2.421699 2.612642 2.049235
## [2,] 1.866717 2.674736 1.218481
## [3,] 1.734060 2.291645 1.688259
## [4,] 1.972683 2.388110 2.467467
## [5,] 2.345542 1.375722 2.343900
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 2.421699 2.612642 2.049235
## [2,] 1.866717 2.674736 1.218481
## [3,] 1.734060 2.291645 1.688259
## [4,] 1.972683 2.388110 2.467467
## [5,] 2.345542 1.375722 2.343900
Finally, we will show a more complex example, combining Multiplication, Summation, and Permutation.
einsum::einsum('i,ij,ijk,ijk,ji->jki',
arrA, arrC, arrE, arrE, t(arrC))
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.615675e-05 0.0001809751 8.832074e-05 0.0001279532 1.347292e-04
## [2,] 1.428908e-03 0.0029293121 3.740792e-02 0.0005514066 3.015423e-02
## [3,] 3.423828e-02 0.0073323633 2.456910e-04 0.0221380902 1.680379e-07
## [4,] 4.654078e-03 0.0005913304 8.467507e-05 0.0008091430 3.079375e-03
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.22988055 0.004140824 0.44620567 0.245712661 0.046362013
## [2,] 0.11556276 0.299885847 0.04454630 0.257171530 0.035837849
## [3,] 0.01713632 0.102649478 0.03139281 0.007183185 0.043017821
## [4,] 0.31525032 0.150460172 0.06538345 0.106827673 0.003630847
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0272494265 0.0213679278 5.750499e-02 0.0483679167 0.1250758081
## [2,] 0.0104717465 0.0030131425 2.084687e-02 0.0156520432 0.0086798713
## [3,] 0.0003600945 0.0002697317 6.363577e-06 0.0007194921 0.0006873341
## [4,] 0.0192794131 0.0020985783 2.314450e-03 0.0190730646 0.0086505749
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,] 2.615675e-05 1.809751e-04 8.832074e-05 1.279532e-04 1.347292e-04
## [2,] 1.428908e-03 2.929312e-03 3.740792e-02 5.514066e-04 3.015423e-02
## [3,] 3.423828e-02 7.332363e-03 2.456910e-04 2.213809e-02 1.680379e-07
## [4,] 4.654078e-03 5.913304e-04 8.467507e-05 8.091430e-04 3.079375e-03
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.229880551 0.004140824 0.446205670 0.245712661 0.046362013
## [2,] 0.115562759 0.299885847 0.044546298 0.257171530 0.035837849
## [3,] 0.017136322 0.102649478 0.031392809 0.007183185 0.043017821
## [4,] 0.315250325 0.150460172 0.065383454 0.106827673 0.003630847
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.724943e-02 2.136793e-02 5.750499e-02 4.836792e-02 1.250758e-01
## [2,] 1.047175e-02 3.013143e-03 2.084687e-02 1.565204e-02 8.679871e-03
## [3,] 3.600945e-04 2.697317e-04 6.363577e-06 7.194921e-04 6.873341e-04
## [4,] 1.927941e-02 2.098578e-03 2.314450e-03 1.907306e-02 8.650575e-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.1.1 (2021-08-10)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.3 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.14-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.14-bioc/R/lib/libRlapack.so
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_GB LC_COLLATE=C
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] einsum_0.1.0 DelayedRandomArray_1.2.0 HDF5Array_1.22.0
## [4] rhdf5_2.38.0 DelayedArray_0.20.0 IRanges_2.28.0
## [7] S4Vectors_0.32.0 MatrixGenerics_1.6.0 matrixStats_0.61.0
## [10] BiocGenerics_0.40.0 Matrix_1.3-4 DelayedTensor_1.0.0
## [13] BiocStyle_2.22.0
##
## 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.6.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.28.0 rmarkdown_2.11
## [25] bookdown_0.24 irlba_2.3.3 Rhdf5lib_1.16.0
## [28] magrittr_2.0.1 BiocSingular_1.10.0 htmltools_0.5.2
## [31] rsvd_1.0.5 beachmat_2.10.0 dqrng_0.3.0
## [34] ScaledMatrix_1.2.0 stringi_1.7.5