DelayedTensor 1.4.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2022-11-01 15:19:15
Compiled: Tue Nov 1 19:54:01 2022
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.5372348 0.1855961 0.7625167
DelayedTensor::einsum('i->i', darrA)
## <3> array of class DelayedArray and type "double":
## [1] [2] [3]
## 0.5372348 0.1855961 0.7625167
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.73298644 0.7589225 0.51766641 0.88946534
## [2,] 0.84278530 0.7890250 0.80050731 0.02650101
## [3,] 0.08752315 0.8077978 0.06952223 0.68089558
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.73298644 0.75892249 0.51766641 0.88946534
## [2,] 0.84278530 0.78902498 0.80050731 0.02650101
## [3,] 0.08752315 0.80779777 0.06952223 0.68089558
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04348392 0.8912891 0.1186771 0.5066935
## [2,] 0.72399212 0.1319790 0.7667382 0.8874482
## [3,] 0.30759398 0.4674566 0.1752664 0.5911315
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6833471 0.3738173 0.4662831 0.8566212
## [2,] 0.3907399 0.7320185 0.5891796 0.1850442
## [3,] 0.6539022 0.4240819 0.8372723 0.9130429
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3060487 0.29238867 0.5268886 0.2677236
## [2,] 0.2043107 0.09200612 0.8787854 0.3660494
## [3,] 0.2540254 0.60010718 0.2970086 0.3898010
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.99126155 0.95065750 0.7724418 0.7298512
## [2,] 0.38261188 0.06755012 0.2333753 0.2512858
## [3,] 0.04660864 0.35218098 0.9507217 0.4555093
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2503302 0.72340667 0.3276600 0.8004479
## [2,] 0.5616896 0.01636223 0.1420294 0.6796604
## [3,] 0.4814021 0.57147654 0.4082078 0.3026026
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.04348392 0.89128907 0.11867713 0.50669351
## [2,] 0.72399212 0.13197905 0.76673823 0.88744817
## [3,] 0.30759398 0.46745662 0.17526636 0.59113154
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.6833471 0.3738173 0.4662831 0.8566212
## [2,] 0.3907399 0.7320185 0.5891796 0.1850442
## [3,] 0.6539022 0.4240819 0.8372723 0.9130429
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.30604873 0.29238867 0.52688861 0.26772360
## [2,] 0.20431071 0.09200612 0.87878543 0.36604944
## [3,] 0.25402536 0.60010718 0.29700856 0.38980103
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.99126155 0.95065750 0.77244178 0.72985124
## [2,] 0.38261188 0.06755012 0.23337530 0.25128577
## [3,] 0.04660864 0.35218098 0.95072169 0.45550932
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.25033022 0.72340667 0.32765997 0.80044786
## [2,] 0.56168957 0.01636223 0.14202940 0.67966041
## [3,] 0.48140209 0.57147654 0.40820784 0.30260256
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.7868833 0.3000984 0.6568541
DelayedTensor::einsum('ii->i', darrB)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.7868833 0.3000984 0.6568541
einsum::einsum('iii->i', arrD)
## [1] 0.800061831 0.486291567 0.005348413
DelayedTensor::einsum('iii->i', darrD)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.800061831 0.486291567 0.005348413
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.28862122 0.03444592 0.58143176
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.28862122 0.03444592 0.58143176
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.537269117 0.5759633 0.267978514 0.7911485852
## [2,] 0.710287055 0.6225604 0.640811955 0.0007023035
## [3,] 0.007660302 0.6525372 0.004833341 0.4636187918
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.5372691174 0.5759633427 0.2679785139 0.7911485852
## [2,] 0.7102870552 0.6225604195 0.6408119550 0.0007023035
## [3,] 0.0076603016 0.6525372418 0.0048333406 0.4636187918
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.001890852 0.79439620 0.01408426 0.2567383
## [2,] 0.524164584 0.01741847 0.58788752 0.7875643
## [3,] 0.094614058 0.21851569 0.03071830 0.3494365
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4669632 0.1397394 0.2174200 0.73379981
## [2,] 0.1526776 0.5358511 0.3471325 0.03424136
## [3,] 0.4275881 0.1798455 0.7010249 0.83364729
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09366582 0.085491133 0.27761161 0.07167593
## [2,] 0.04174286 0.008465127 0.77226384 0.13399219
## [3,] 0.06452888 0.360128624 0.08821409 0.15194485
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.982599460 0.903749673 0.59666630 0.53268284
## [2,] 0.146391850 0.004563018 0.05446403 0.06314454
## [3,] 0.002172366 0.124031442 0.90387173 0.20748874
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06266522 0.5233172070 0.10736106 0.64071677
## [2,] 0.31549517 0.0002677227 0.02017235 0.46193827
## [3,] 0.23174798 0.3265854326 0.16663364 0.09156831
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.001890852 0.794396203 0.014084262 0.256738310
## [2,] 0.524164584 0.017418469 0.587887516 0.787564256
## [3,] 0.094614058 0.218515693 0.030718298 0.349436497
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.46696325 0.13973940 0.21741996 0.73379981
## [2,] 0.15267764 0.53585108 0.34713255 0.03424136
## [3,] 0.42758806 0.17984546 0.70102488 0.83364729
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.093665824 0.085491133 0.277611612 0.071675928
## [2,] 0.041742864 0.008465127 0.772263837 0.133992190
## [3,] 0.064528882 0.360128624 0.088214088 0.151944846
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.982599460 0.903749673 0.596666300 0.532682839
## [2,] 0.146391850 0.004563018 0.054464028 0.063144538
## [3,] 0.002172366 0.124031442 0.903871728 0.207488742
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.0626652185 0.5233172070 0.1073610580 0.6407167725
## [2,] 0.3154951742 0.0002677227 0.0201723501 0.4619382665
## [3,] 0.2317479757 0.3265854326 0.1666336416 0.0915683110
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.2886212 0.09970870 0.4096505
## [2,] 0.0997087 0.03444592 0.1415202
## [3,] 0.4096505 0.14152015 0.5814318
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.28862122 0.09970870 0.40965051
## [2,] 0.09970870 0.03444592 0.14152015
## [3,] 0.40965051 0.14152015 0.58143176
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03187313 0.03300093 0.022510167 0.038677443
## [2,] 0.03664761 0.03430990 0.034809199 0.001152368
## [3,] 0.00380585 0.03512622 0.003023099 0.029608012
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.53067640 0.5494539 0.37478640 0.64396589
## [2,] 0.61016991 0.5712479 0.57956098 0.01918652
## [3,] 0.06336607 0.5848392 0.05033355 0.49296303
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.22546222 0.2334400 0.15923107 0.273594185
## [2,] 0.25923569 0.2426993 0.24623123 0.008151551
## [3,] 0.02692159 0.2484737 0.02138462 0.209439383
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.65330280 0.6764193 0.4613904 0.79277073
## [2,] 0.75116532 0.7032493 0.7134834 0.02362006
## [3,] 0.07800843 0.7199813 0.0619644 0.60687479
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09673885 0.1001619 0.068321120 0.117390788
## [2,] 0.11123000 0.1041348 0.105650192 0.003497578
## [3,] 0.01155122 0.1066124 0.009175478 0.089863950
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.34263936 0.3547633 0.24198659 0.41578646
## [2,] 0.39396557 0.3688350 0.37420244 0.01238807
## [3,] 0.04091328 0.3776104 0.03249863 0.31828915
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08698873 0.09006675 0.061435166 0.105559196
## [2,] 0.10001934 0.09363922 0.095001913 0.003145064
## [3,] 0.01038700 0.09586712 0.008250699 0.080806735
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.56200872 0.5818949 0.39691463 0.68198708
## [2,] 0.64619571 0.6049756 0.61377956 0.02031934
## [3,] 0.06710734 0.6193694 0.05330535 0.52206867
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12846787 0.1330136 0.09072951 0.155893355
## [2,] 0.14771191 0.1382895 0.14030201 0.004644736
## [3,] 0.01533986 0.1415798 0.01218491 0.119338092
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.37139947 0.3845411 0.26229821 0.45068631
## [2,] 0.42703384 0.3997938 0.40561186 0.01342789
## [3,] 0.04434741 0.4093059 0.03522646 0.34500537
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.65048747 0.6735044 0.45940211 0.78935439
## [2,] 0.74792827 0.7002188 0.71040875 0.02351827
## [3,] 0.07767226 0.7168787 0.06169738 0.60425954
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.43329140 0.4486230 0.30600894 0.52579101
## [2,] 0.49819697 0.4664176 0.47320512 0.01566558
## [3,] 0.05173769 0.4775147 0.04109678 0.40249885
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.50088415 0.5186075 0.35374584 0.60781355
## [2,] 0.57591488 0.5391779 0.54702434 0.01810939
## [3,] 0.05980869 0.5520063 0.04750781 0.46528802
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.28640702 0.2965413 0.20227290 0.3475496
## [2,] 0.32930981 0.3083035 0.31279012 0.0103550
## [3,] 0.03419878 0.3156388 0.02716511 0.2660530
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.47930143 0.4962611 0.33850320 0.58162332
## [2,] 0.55109914 0.5159452 0.52345348 0.01732907
## [3,] 0.05723158 0.5282207 0.04546074 0.44523911
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.27400303 0.2836984 0.19351268 0.332497558
## [2,] 0.31504775 0.2949512 0.29924351 0.009906537
## [3,] 0.03271767 0.3019688 0.02598861 0.254530568
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.53655963 0.5555453 0.37894139 0.65110508
## [2,] 0.61693443 0.5775809 0.58598616 0.01939923
## [3,] 0.06406856 0.5913229 0.05089156 0.49842816
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.31084628 0.3218453 0.21953296 0.3772061
## [2,] 0.35740999 0.3346112 0.33948066 0.0112386
## [3,] 0.03711698 0.3425724 0.02948312 0.2887555
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.34177921 0.3538728 0.24137911 0.41474268
## [2,] 0.39297656 0.3679090 0.37326305 0.01235697
## [3,] 0.04081057 0.3766625 0.03241704 0.31749012
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.43186062 0.4471416 0.30499847 0.52405479
## [2,] 0.49655187 0.4648774 0.47164254 0.01561385
## [3,] 0.05156685 0.4759379 0.04096108 0.40116976
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.61370923 0.6354248 0.43342774 0.74472468
## [2,] 0.70564077 0.6606288 0.67024259 0.02218856
## [3,] 0.07328071 0.6763467 0.05820904 0.57009500
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.62789169 0.6501091 0.44344400 0.76193483
## [2,] 0.72194772 0.6758955 0.68573150 0.02270133
## [3,] 0.07497418 0.6919767 0.05955421 0.58326956
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13563490 0.1404342 0.09579118 0.164590419
## [2,] 0.15595255 0.1460045 0.14812925 0.004903859
## [3,] 0.01619565 0.1494783 0.01286469 0.125995791
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.66924804 0.6929288 0.47265163 0.81211999
## [2,] 0.76949911 0.7204136 0.73089750 0.02419656
## [3,] 0.07991239 0.7375540 0.06347678 0.62168686
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.22432957 0.2322673 0.15843115 0.2722197
## [2,] 0.25793337 0.2414801 0.24499424 0.0081106
## [3,] 0.02678635 0.2472255 0.02127719 0.2083872
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.14975698 0.1550560 0.10576479 0.18172729
## [2,] 0.17219006 0.1612063 0.16355221 0.00541444
## [3,] 0.01788192 0.1650417 0.01420414 0.13911426
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1861971 0.1927856 0.13150039 0.225946749
## [2,] 0.2140888 0.2004324 0.20334915 0.006731928
## [3,] 0.0222331 0.2052011 0.01766041 0.172964742
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.21431693 0.2219003 0.15135979 0.260069585
## [2,] 0.24642087 0.2307020 0.23405927 0.007748595
## [3,] 0.02559078 0.2361909 0.02032751 0.199086152
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.067439241 0.06982552 0.047628480 0.081836258
## [2,] 0.077541409 0.07259513 0.073651575 0.002438255
## [3,] 0.008052666 0.07432234 0.006396471 0.062646563
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.43987042 0.4554348 0.31065533 0.53377453
## [2,] 0.50576150 0.4734996 0.48039018 0.01590345
## [3,] 0.05252327 0.4847652 0.04172079 0.40861032
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.38620221 0.3998676 0.27275254 0.46864916
## [2,] 0.44405398 0.4157283 0.42177819 0.01396308
## [3,] 0.04611495 0.4256194 0.03663047 0.35875613
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.64413780 0.6669300 0.45491770 0.7816492
## [2,] 0.74062744 0.6933837 0.70347416 0.0232887
## [3,] 0.07691407 0.7098809 0.06109512 0.5983611
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.21770325 0.2254065 0.1537514 0.264178823
## [2,] 0.25031445 0.2343472 0.2377575 0.007871027
## [3,] 0.02599512 0.2399229 0.0206487 0.202231819
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.19623777 0.2031815 0.13859152 0.238130865
## [2,] 0.22563352 0.2112406 0.21431470 0.007094946
## [3,] 0.02343201 0.2162665 0.01861274 0.182291818
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2683093 0.2778031 0.18949150 0.32558829
## [2,] 0.3085011 0.2888221 0.29302525 0.00970068
## [3,] 0.0320378 0.2956939 0.02544857 0.24924144
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.28571887 0.2958288 0.20178690 0.34671451
## [2,] 0.32851858 0.3075628 0.31203858 0.01033012
## [3,] 0.03411661 0.3148804 0.02709984 0.26541380
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.72658127 0.7522907 0.51314281 0.88169279
## [2,] 0.83542066 0.7821301 0.79351212 0.02626943
## [3,] 0.08675833 0.8007389 0.06891471 0.67494561
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2804493 0.2903728 0.19806532 0.3403200
## [2,] 0.3224597 0.3018903 0.30628361 0.0101396
## [3,] 0.0334874 0.3090730 0.02660003 0.2605187
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.034163503 0.03537235 0.024127729 0.041456772
## [2,] 0.039281079 0.03677538 0.037310559 0.001235176
## [3,] 0.004079335 0.03765036 0.003240337 0.031735619
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.69681905 0.7214754 0.49212345 0.84557689
## [2,] 0.80120016 0.7500925 0.76100828 0.02519338
## [3,] 0.08320454 0.7679390 0.06609183 0.64729849
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.049513320 0.05126530 0.034968427 0.060083488
## [2,] 0.056930245 0.05329873 0.054074363 0.001790146
## [3,] 0.005912199 0.05456683 0.004696235 0.045994576
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.25814388 0.2672781 0.18231226 0.313252773
## [2,] 0.29681295 0.2778796 0.28192345 0.009333151
## [3,] 0.03082399 0.2844910 0.02448441 0.239798472
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.56618935 0.5862234 0.39986716 0.68706019
## [2,] 0.65100257 0.6094759 0.61834529 0.02047049
## [3,] 0.06760654 0.6239767 0.05370188 0.52595219
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.17106093 0.1771138 0.12081055 0.207579235
## [2,] 0.19668527 0.1841389 0.18681863 0.006184681
## [3,] 0.02042574 0.1885200 0.01622477 0.158904207
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.69686610 0.7215241 0.49215668 0.84563399
## [2,] 0.80125426 0.7501432 0.76105966 0.02519508
## [3,] 0.08321016 0.7679909 0.06609629 0.64734220
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.53497106 0.5539005 0.37781948 0.64917738
## [2,] 0.61510790 0.5758709 0.58425126 0.01934179
## [3,] 0.06387888 0.5895722 0.05074089 0.49695249
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.18418906 0.1907064 0.13008220 0.223509982
## [2,] 0.21177995 0.1982708 0.20115610 0.006659327
## [3,] 0.02199332 0.2029881 0.01746995 0.171099371
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.33388215 0.3456963 0.23580188 0.40515975
## [2,] 0.38389656 0.3594082 0.36463854 0.01207146
## [3,] 0.03986761 0.3679594 0.03166802 0.31015428
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.18348866 0.1899812 0.12958755 0.222660052
## [2,] 0.21097463 0.1975168 0.20039117 0.006634004
## [3,] 0.02190969 0.2022162 0.01740352 0.170448740
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.41171084 0.4262788 0.29076782 0.49960340
## [2,] 0.47338371 0.4431871 0.44963661 0.01488534
## [3,] 0.04916084 0.4537316 0.03904991 0.38245195
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.35286121 0.3653469 0.24920569 0.42819048
## [2,] 0.40571861 0.3798383 0.38536590 0.01275764
## [3,] 0.04213383 0.3888755 0.03346815 0.32778456
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.53024728 0.5490096 0.37448333 0.64344516
## [2,] 0.60967650 0.5707859 0.57909233 0.01917101
## [3,] 0.06331483 0.5843663 0.05029285 0.49256440
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.011993295 0.01241767 0.008470179 0.0145536398
## [2,] 0.013789850 0.01291021 0.013098088 0.0004336157
## [3,] 0.001432074 0.01321738 0.001137539 0.0111409727
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.41888455 0.4337064 0.29583421 0.50830857
## [2,] 0.48163202 0.4509093 0.45747115 0.01514471
## [3,] 0.05001743 0.4616375 0.03973032 0.38911585
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.24017032 0.2486685 0.16961856 0.29144219
## [2,] 0.27614701 0.2585319 0.26229420 0.00868332
## [3,] 0.02867783 0.2646830 0.02277965 0.22310223
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10410562 0.1077893 0.073523849 0.126330227
## [2,] 0.11970029 0.1120647 0.113695572 0.003763922
## [3,] 0.01243086 0.1147310 0.009874201 0.096707190
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.29921081 0.3097981 0.21131549 0.36308672
## [2,] 0.34403157 0.3220862 0.32677336 0.01081792
## [3,] 0.03572764 0.3297494 0.02837952 0.27794692
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.58671742 0.6074779 0.41436497 0.71197062
## [2,] 0.67460568 0.6315734 0.64076436 0.02121268
## [3,] 0.07005772 0.6466000 0.05564892 0.54502141
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.49818186 0.5158096 0.35183736 0.60453437
## [2,] 0.57280780 0.5362690 0.54407312 0.01801169
## [3,] 0.05948602 0.5490282 0.04725151 0.46277777
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.22180357 0.2296519 0.15664718 0.269154490
## [2,] 0.25502899 0.2387610 0.24223556 0.008019273
## [3,] 0.02648473 0.2444417 0.02103761 0.206040748
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.031873126 0.033000928 0.022510167 0.038677443
## [2,] 0.036647612 0.034309902 0.034809199 0.001152368
## [3,] 0.003805850 0.035126217 0.003023099 0.029608012
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.53067640 0.54945390 0.37478640 0.64396589
## [2,] 0.61016991 0.57124786 0.57956098 0.01918652
## [3,] 0.06336607 0.58483922 0.05033355 0.49296303
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.225462217 0.233439990 0.159231073 0.273594185
## [2,] 0.259235685 0.242699336 0.246231231 0.008151551
## [3,] 0.026921594 0.248473734 0.021384620 0.209439383
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.58671742 0.60747788 0.41436497 0.71197062
## [2,] 0.67460568 0.63157335 0.64076436 0.02121268
## [3,] 0.07005772 0.64660000 0.05564892 0.54502141
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.49818186 0.51580957 0.35183736 0.60453437
## [2,] 0.57280780 0.53626904 0.54407312 0.01801169
## [3,] 0.05948602 0.54902816 0.04725151 0.46277777
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.221803574 0.229651890 0.156647183 0.269154490
## [2,] 0.255028990 0.238760981 0.242235564 0.008019273
## [3,] 0.026484729 0.244441676 0.021037605 0.206040748
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.485348
DelayedTensor::einsum('i->', darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 1.485348
einsum::einsum('ij->', arrC)
## [1] 7.003598
DelayedTensor::einsum('ij->', darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 7.003598
einsum::einsum('ijk->', arrE)
## [1] 28.64157
DelayedTensor::einsum('ijk->', darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 28.64157
einsum::einsum('ij->i', arrC)
## [1] 2.899041 2.458819 1.645739
DelayedTensor::einsum('ij->i', darrC)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 2.899041 2.458819 1.645739
einsum::einsum('ij->j', arrC)
## [1] 1.663295 2.355745 1.387696 1.596862
DelayedTensor::einsum('ij->j', darrC)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 1.663295 2.355745 1.387696 1.596862
einsum::einsum('ijk->i', arrE)
## [1] 10.879319 8.282856 9.479400
DelayedTensor::einsum('ijk->i', darrE)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 10.879319 8.282856 9.479400
einsum::einsum('ijk->j', arrE)
## [1] 6.281348 6.686778 7.490535 8.182913
DelayedTensor::einsum('ijk->j', darrE)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 6.281348 6.686778 7.490535 8.182913
einsum::einsum('ijk->k', arrE)
## [1] 5.611750 7.105350 4.475143 6.184056 5.265275
DelayedTensor::einsum('ijk->k', darrE)
## <5> array of class HDF5Array and type "double":
## [1] [2] [3] [4] [5]
## 5.611750 7.105350 4.475143 6.184056 5.265275
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 2.274472 3.231559 2.211951 3.161337
## [2,] 2.263344 1.039916 2.610108 2.369488
## [3,] 1.743532 2.415303 2.668477 2.652087
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 2.274472 3.231559 2.211951 3.161337
## [2,] 2.263344 1.039916 2.610108 2.369488
## [3,] 1.743532 2.415303 2.668477 2.652087
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.075070 1.727989 0.7643848 1.420482 1.2934219
## [2,] 1.490725 1.529918 0.9845020 1.370389 1.3112454
## [3,] 1.060682 1.892735 1.7026826 1.956539 0.8778972
## [4,] 1.985273 1.954708 1.0235741 1.436646 1.7827108
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.0750700 1.7279891 0.7643848 1.4204821 1.2934219
## [2,] 1.4907247 1.5299177 0.9845020 1.3703886 1.3112454
## [3,] 1.0606817 1.8927350 1.7026826 1.9565388 0.8778972
## [4,] 1.9852732 1.9547083 1.0235741 1.4366463 1.7827108
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.075070 1.727989 0.7643848 1.420482 1.2934219
## [2,] 1.490725 1.529918 0.9845020 1.370389 1.3112454
## [3,] 1.060682 1.892735 1.7026826 1.956539 0.8778972
## [4,] 1.985273 1.954708 1.0235741 1.436646 1.7827108
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.0750700 1.7279891 0.7643848 1.4204821 1.2934219
## [2,] 1.4907247 1.5299177 0.9845020 1.3703886 1.3112454
## [3,] 1.0606817 1.8927350 1.7026826 1.9565388 0.8778972
## [4,] 1.9852732 1.9547083 1.0235741 1.4366463 1.7827108
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.743836
DelayedTensor::einsum('ii->', darrB)
## <1> array of class HDF5Array and type "double":
## [1]
## 1.743836
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.7868833 0.5214540 0.7154265
## [2,] 0.5073765 0.3000984 0.1500958
## [3,] 0.5877858 0.1064723 0.6568541
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3]
## [1,] 0.7868833 0.5214540 0.7154265
## [2,] 0.5073765 0.3000984 0.1500958
## [3,] 0.5877858 0.1064723 0.6568541
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.8000618 0.9718378 0.1902373
## [2,] 0.8987882 0.1617375 0.8086999
## [3,] 0.9085666 0.2554021 0.3265900
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.8902897 0.1392241 0.2845303
## [2,] 0.1512602 0.4862916 0.6061906
## [3,] 0.2883212 0.4529147 0.9097508
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.4991233 0.3749554 0.950183843
## [2,] 0.3374989 0.9210267 0.585279400
## [3,] 0.7442246 0.3947412 0.005348413
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.8000618 0.9718378 0.1902373
## [2,] 0.8987882 0.1617375 0.8086999
## [3,] 0.9085666 0.2554021 0.3265900
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.8902897 0.1392241 0.2845303
## [2,] 0.1512602 0.4862916 0.6061906
## [3,] 0.2883212 0.4529147 0.9097508
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.499123301 0.374955397 0.950183843
## [2,] 0.337498927 0.921026728 0.585279400
## [3,] 0.744224607 0.394741221 0.005348413
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.9044989
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 0.9044989
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 5.275371
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 5.275371
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 18.06738
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 18.06738
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.6206695 1.0472290 0.1999376 1.1311637 0.6099084
## [2,] 1.0303304 0.8554359 0.4540849 1.0323441 0.8501704
## [3,] 0.6326901 1.2655774 1.1380895 1.5550021 0.2941670
## [4,] 1.3937391 1.6016885 0.3576130 0.8033161 1.1942234
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.6206695 1.0472290 0.1999376 1.1311637 0.6099084
## [2,] 1.0303304 0.8554359 0.4540849 1.0323441 0.8501704
## [3,] 0.6326901 1.2655774 1.1380895 1.5550021 0.2941670
## [4,] 1.3937391 1.6016885 0.3576130 0.8033161 1.1942234
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 2.172360 1.6545265 1.3188315
## [2,] 1.654526 1.9743617 0.7848333
## [3,] 1.318832 0.7848333 1.1286497
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 2.1723596 1.6545265 1.3188315
## [2,] 1.6545265 1.9743617 0.7848333
## [3,] 1.3188315 0.7848333 1.1286497
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.5372691 0.7102870552 0.007660302
## [2,] 0.5759633 0.6225604195 0.652537242
## [3,] 0.2679785 0.6408119550 0.004833341
## [4,] 0.7911486 0.0007023035 0.463618792
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.5372691174 0.7102870552 0.0076603016
## [2,] 0.5759633427 0.6225604195 0.6525372418
## [3,] 0.2679785139 0.6408119550 0.0048333406
## [4,] 0.7911485852 0.0007023035 0.4636187918
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.001890852 0.4669632 0.09366582 0.9825995 0.06266522
## [2,] 0.794396203 0.1397394 0.08549113 0.9037497 0.52331721
## [3,] 0.014084262 0.2174200 0.27761161 0.5966663 0.10736106
## [4,] 0.256738310 0.7337998 0.07167593 0.5326828 0.64071677
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.52416458 0.15267764 0.041742864 0.146391850 0.3154951742
## [2,] 0.01741847 0.53585108 0.008465127 0.004563018 0.0002677227
## [3,] 0.58788752 0.34713255 0.772263837 0.054464028 0.0201723501
## [4,] 0.78756426 0.03424136 0.133992190 0.063144538 0.4619382665
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.09461406 0.4275881 0.06452888 0.002172366 0.23174798
## [2,] 0.21851569 0.1798455 0.36012862 0.124031442 0.32658543
## [3,] 0.03071830 0.7010249 0.08821409 0.903871728 0.16663364
## [4,] 0.34943650 0.8336473 0.15194485 0.207488742 0.09156831
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.001890852 0.466963250 0.093665824 0.982599460 0.062665218
## [2,] 0.794396203 0.139739397 0.085491133 0.903749673 0.523317207
## [3,] 0.014084262 0.217419956 0.277611612 0.596666300 0.107361058
## [4,] 0.256738310 0.733799807 0.071675928 0.532682839 0.640716773
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.5241645838 0.1526776437 0.0417428643 0.1463918502 0.3154951742
## [2,] 0.0174184690 0.5358510818 0.0084651269 0.0045630183 0.0002677227
## [3,] 0.5878875157 0.3471325496 0.7722638373 0.0544640283 0.0201723501
## [4,] 0.7875642557 0.0342413630 0.1339921904 0.0631445384 0.4619382665
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.094614058 0.427588063 0.064528882 0.002172366 0.231747976
## [2,] 0.218515693 0.179845457 0.360128624 0.124031442 0.326585433
## [3,] 0.030718298 0.701024884 0.088214088 0.903871728 0.166633642
## [4,] 0.349436497 0.833647294 0.151944846 0.207488742 0.091568311
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 1.560144 2.5101576 1.541449
## [2,] 2.380069 1.8969821 2.828299
## [3,] 1.393050 1.5411517 1.540942
## [4,] 3.444212 0.9348231 1.805021
## [5,] 2.101845 1.3997416 1.763689
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 1.5601436 2.5101576 1.5414485
## [2,] 2.3800687 1.8969821 2.8282992
## [3,] 1.3930496 1.5411517 1.5409421
## [4,] 3.4442121 0.9348231 1.8050206
## [5,] 2.1018447 1.3997416 1.7636890
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.0005457748 0.13478412 0.02703567 0.28361718 0.01808767
## [2,] 0.2458080684 0.04323922 0.02645331 0.27964504 0.16192876
## [3,] 0.0020276743 0.03130138 0.03996702 0.08590048 0.01545649
## [4,] 0.1091221376 0.31188880 0.03046460 0.22640754 0.27232548
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0690987971 2.012696e-02 5.502817e-03 1.929833e-02 4.159063e-02
## [2,] 0.0020126136 6.191481e-02 9.781014e-04 5.272331e-04 3.093397e-05
## [3,] 0.0699187663 4.128524e-02 9.184705e-02 6.477528e-03 2.399142e-03
## [4,] 0.0001026549 4.463184e-06 1.746519e-05 8.230563e-06 6.021126e-05
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0005526509 0.002497588 0.0003769202 1.268902e-05 0.0013536650
## [2,] 0.1087269760 0.089485805 0.1791894013 6.171439e-02 0.1624992967
## [3,] 0.0001132124 0.002583629 0.0003251133 3.331222e-03 0.0006141288
## [4,] 0.1235317709 0.294708559 0.0537150988 7.335081e-02 0.0323709622
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.0005457748 0.1347841150 0.0270356718 0.2836171769 0.0180876675
## [2,] 0.2458080684 0.0432392187 0.0264533116 0.2796450446 0.1619287597
## [3,] 0.0020276743 0.0313013818 0.0399670168 0.0859004848 0.0154564904
## [4,] 0.1091221376 0.3118888005 0.0304646020 0.2264075435 0.2723254812
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 6.909880e-02 2.012696e-02 5.502817e-03 1.929833e-02 4.159063e-02
## [2,] 2.012614e-03 6.191481e-02 9.781014e-04 5.272331e-04 3.093397e-05
## [3,] 6.991877e-02 4.128524e-02 9.184705e-02 6.477528e-03 2.399142e-03
## [4,] 1.026549e-04 4.463184e-06 1.746519e-05 8.230563e-06 6.021126e-05
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 5.526509e-04 2.497588e-03 3.769202e-04 1.268902e-05 1.353665e-03
## [2,] 1.087270e-01 8.948580e-02 1.791894e-01 6.171439e-02 1.624993e-01
## [3,] 1.132124e-04 2.583629e-03 3.251133e-04 3.331222e-03 6.141288e-04
## [4,] 1.235318e-01 2.947086e-01 5.371510e-02 7.335081e-02 3.237096e-02
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.2.1 (2022-06-23 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows Server x64 (build 20348)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=C
## [2] LC_CTYPE=English_United States.utf8
## [3] LC_MONETARY=English_United States.utf8
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.utf8
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] einsum_0.1.0 DelayedRandomArray_1.6.0 HDF5Array_1.26.0
## [4] rhdf5_2.42.0 DelayedArray_0.24.0 IRanges_2.32.0
## [7] S4Vectors_0.36.0 MatrixGenerics_1.10.0 matrixStats_0.62.0
## [10] BiocGenerics_0.44.0 Matrix_1.5-1 DelayedTensor_1.4.0
## [13] BiocStyle_2.26.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.9 rTensor_1.4.8 bslib_0.4.0
## [4] compiler_4.2.1 BiocManager_1.30.19 jquerylib_0.1.4
## [7] rhdf5filters_1.10.0 tools_4.2.1 digest_0.6.30
## [10] jsonlite_1.8.3 evaluate_0.17 lattice_0.20-45
## [13] rlang_1.0.6 cli_3.4.1 parallel_4.2.1
## [16] yaml_2.3.6 xfun_0.34 fastmap_1.1.0
## [19] stringr_1.4.1 knitr_1.40 sass_0.4.2
## [22] grid_4.2.1 R6_2.5.1 BiocParallel_1.32.0
## [25] rmarkdown_2.17 bookdown_0.29 irlba_2.3.5.1
## [28] BiocSingular_1.14.0 Rhdf5lib_1.20.0 magrittr_2.0.3
## [31] codetools_0.2-18 htmltools_0.5.3 rsvd_1.0.5
## [34] beachmat_2.14.0 dqrng_0.3.0 ScaledMatrix_1.6.0
## [37] stringi_1.7.8 cachem_1.0.6