DelayedTensor 1.4.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2022-11-01 15:19:15
Compiled: Tue Nov 1 17:22:03 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.7774108 0.8585216 0.8100404
DelayedTensor::einsum('i->i', darrA)
## <3> array of class DelayedArray and type "double":
## [1] [2] [3]
## 0.7774108 0.8585216 0.8100404
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.40169214 0.04615804 0.9022896 0.039021726
## [2,] 0.05236875 0.81336877 0.3647608 0.496821722
## [3,] 0.22832466 0.14611688 0.4249503 0.003878607
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.401692139 0.046158041 0.902289640 0.039021726
## [2,] 0.052368748 0.813368772 0.364760770 0.496821722
## [3,] 0.228324659 0.146116880 0.424950343 0.003878607
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8517532 0.4680484 0.9516558 0.1302022
## [2,] 0.8080918 0.2266931 0.5695859 0.1355850
## [3,] 0.6859343 0.8239180 0.1269498 0.7593384
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.75022747 0.9663290 0.1576790 0.3860173
## [2,] 0.07826416 0.9792552 0.3043143 0.8004274
## [3,] 0.85870187 0.3389123 0.6035951 0.5672843
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.73614380 0.6764503 0.05283309 0.13830677
## [2,] 0.09486985 0.3081539 0.65469032 0.65486727
## [3,] 0.07015385 0.8636132 0.32671844 0.07409094
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5879090 0.3224374 0.6908438 0.2263849
## [2,] 0.2718817 0.9638783 0.7331534 0.2827502
## [3,] 0.8504181 0.3893858 0.4212819 0.7587750
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1929721 0.2335735 0.60471089 0.8261483
## [2,] 0.4342109 0.2280298 0.02705248 0.9578504
## [3,] 0.3206595 0.8609435 0.83758296 0.8881323
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.8517532 0.4680484 0.9516558 0.1302022
## [2,] 0.8080918 0.2266931 0.5695859 0.1355850
## [3,] 0.6859343 0.8239180 0.1269498 0.7593384
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.75022747 0.96632897 0.15767899 0.38601730
## [2,] 0.07826416 0.97925518 0.30431431 0.80042738
## [3,] 0.85870187 0.33891226 0.60359514 0.56728430
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.73614380 0.67645025 0.05283309 0.13830677
## [2,] 0.09486985 0.30815390 0.65469032 0.65486727
## [3,] 0.07015385 0.86361317 0.32671844 0.07409094
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.5879090 0.3224374 0.6908438 0.2263849
## [2,] 0.2718817 0.9638783 0.7331534 0.2827502
## [3,] 0.8504181 0.3893858 0.4212819 0.7587750
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.19297205 0.23357348 0.60471089 0.82614827
## [2,] 0.43421090 0.22802982 0.02705248 0.95785037
## [3,] 0.32065953 0.86094352 0.83758296 0.88813234
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.9857093 0.1380189 0.8830550
DelayedTensor::einsum('ii->i', darrB)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.9857093 0.1380189 0.8830550
einsum::einsum('iii->i', arrD)
## [1] 0.3409732 0.3430357 0.6971212
DelayedTensor::einsum('iii->i', darrD)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.3409732 0.3430357 0.6971212
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.6043675 0.7370594 0.6561654
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.6043675 0.7370594 0.6561654
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.161356574 0.002130565 0.8141266 1.522695e-03
## [2,] 0.002742486 0.661568760 0.1330504 2.468318e-01
## [3,] 0.052132150 0.021350143 0.1805828 1.504359e-05
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 1.613566e-01 2.130565e-03 8.141266e-01 1.522695e-03
## [2,] 2.742486e-03 6.615688e-01 1.330504e-01 2.468318e-01
## [3,] 5.213215e-02 2.135014e-02 1.805828e-01 1.504359e-05
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7254835 0.21906927 0.90564872 0.01695262
## [2,] 0.6530124 0.05138978 0.32442808 0.01838329
## [3,] 0.4705058 0.67884095 0.01611625 0.57659485
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.562841252 0.9337917 0.02486266 0.1490094
## [2,] 0.006125278 0.9589407 0.09260720 0.6406840
## [3,] 0.737368904 0.1148615 0.36432709 0.3218115
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.541907690 0.45758494 0.002791335 0.019128761
## [2,] 0.009000288 0.09495883 0.428619414 0.428851146
## [3,] 0.004921563 0.74582771 0.106744941 0.005489468
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.34563700 0.1039659 0.4772652 0.05125011
## [2,] 0.07391965 0.9290614 0.5375139 0.07994765
## [3,] 0.72321087 0.1516213 0.1774785 0.57573949
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03723821 0.05455657 0.3656752636 0.6825210
## [2,] 0.18853911 0.05199760 0.0007318367 0.9174773
## [3,] 0.10282253 0.74122374 0.7015452212 0.7887791
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.72548345 0.21906927 0.90564872 0.01695262
## [2,] 0.65301241 0.05138978 0.32442808 0.01838329
## [3,] 0.47050581 0.67884095 0.01611625 0.57659485
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.562841252 0.933791676 0.024862663 0.149009355
## [2,] 0.006125278 0.958940713 0.092607200 0.640683986
## [3,] 0.737368904 0.114861517 0.364327093 0.321811477
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.541907690 0.457584941 0.002791335 0.019128761
## [2,] 0.009000288 0.094958826 0.428619414 0.428851146
## [3,] 0.004921563 0.745827712 0.106744941 0.005489468
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.34563700 0.10396587 0.47726517 0.05125011
## [2,] 0.07391965 0.92906137 0.53751394 0.07994765
## [3,] 0.72321087 0.15162132 0.17747847 0.57573949
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.0372382128 0.0545565717 0.3656752636 0.6825209566
## [2,] 0.1885391067 0.0519975976 0.0007318367 0.9174773297
## [3,] 0.1028225313 0.7412237378 0.7015452212 0.7887790584
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.6043675 0.6674240 0.6297341
## [2,] 0.6674240 0.7370594 0.6954372
## [3,] 0.6297341 0.6954372 0.6561654
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.6043675 0.6674240 0.6297341
## [2,] 0.6674240 0.7370594 0.6954372
## [3,] 0.6297341 0.6954372 0.6561654
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.34214255 0.03931526 0.7685281 0.033236878
## [2,] 0.04460525 0.69278943 0.3106861 0.423169473
## [3,] 0.19447625 0.12445551 0.3619528 0.003303616
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.32460414 0.03729994 0.7291329 0.03153314
## [2,] 0.04231876 0.65727666 0.2947602 0.40147757
## [3,] 0.18450729 0.11807586 0.3433989 0.00313427
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.27553440 0.03166138 0.6189114 0.026766338
## [2,] 0.03592152 0.55791751 0.2502019 0.340787039
## [3,] 0.15661571 0.10022657 0.2914880 0.002660469
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.18801135 0.02160420 0.4223152 0.018264055
## [2,] 0.02451111 0.38069592 0.1707257 0.232536591
## [3,] 0.10686698 0.06838977 0.1988973 0.001815376
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09106085 0.01046371 0.20454288 0.0088459577
## [2,] 0.01187164 0.18438513 0.08268877 0.1126260791
## [3,] 0.05175964 0.03312370 0.09633333 0.0008792536
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.33096140 0.03803044 0.7434127 0.032150704
## [2,] 0.04314756 0.67014921 0.3005330 0.409340384
## [3,] 0.18812081 0.12038833 0.3501243 0.003195654
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.38227265 0.04392657 0.8586692 0.037135251
## [2,] 0.04983702 0.77404709 0.3471267 0.472803263
## [3,] 0.21728648 0.13905297 0.4044065 0.003691099
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2287982 0.02629097 0.5139314 0.02222622
## [2,] 0.0298285 0.46328337 0.2077626 0.28298264
## [3,] 0.1300505 0.08322611 0.2420457 0.00220920
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.050994742 0.005859755 0.11454550 0.0049538007
## [2,] 0.006648203 0.103257013 0.04630631 0.0630714244
## [3,] 0.028985773 0.018549510 0.05394737 0.0004923884
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.052301208 0.006009879 0.11748011 0.0050807152
## [2,] 0.006818527 0.105902419 0.04749266 0.0646872904
## [3,] 0.029728377 0.019024742 0.05532948 0.0005050032
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.054463420 0.006258337 0.12233692 0.0052907598
## [2,] 0.007100416 0.110280587 0.04945608 0.0673615623
## [3,] 0.030957394 0.019811254 0.05761688 0.0005258808
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3050203 0.03504957 0.6851432 0.029630696
## [2,] 0.0397656 0.61762217 0.2769769 0.377255827
## [3,] 0.1733757 0.11095216 0.3226811 0.002945175
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.30136048 0.03462903 0.6769225 0.029275170
## [2,] 0.03928847 0.61021159 0.2736535 0.372729302
## [3,] 0.17129543 0.10962090 0.3188094 0.002909837
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.031438097 0.00361252 0.07061694 0.0030540025
## [2,] 0.004098596 0.06365762 0.02854769 0.0388833336
## [3,] 0.017869637 0.01143571 0.03325838 0.0003035559
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.34493379 0.0396360 0.7747978 0.033508029
## [2,] 0.04496914 0.6984413 0.3132208 0.426621742
## [3,] 0.19606281 0.1254708 0.3649057 0.003330567
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.38816675 0.04460385 0.8719086 0.03770782
## [2,] 0.05060544 0.78598181 0.3524789 0.48009322
## [3,] 0.22063673 0.14119697 0.4106418 0.00374801
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.39335911 0.0452005 0.8835718 0.038212227
## [2,] 0.05128237 0.7964956 0.3571939 0.486515246
## [3,] 0.22358811 0.1430857 0.4161348 0.003798146
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13613839 0.01564353 0.3057970 0.013224941
## [2,] 0.01774841 0.27566065 0.1236219 0.168378970
## [3,] 0.07738203 0.04952080 0.1440209 0.001314507
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.063338410 0.007278153 0.14227212 0.0061529062
## [2,] 0.008257451 0.128251165 0.05751511 0.0783383465
## [3,] 0.036002001 0.023039562 0.06700574 0.0006115748
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12224067 0.01404655 0.2745796 0.011874869
## [2,] 0.01593656 0.24751976 0.1110019 0.151189960
## [3,] 0.06948246 0.04446546 0.1293185 0.001180316
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.24245942 0.02786077 0.5446176 0.023553324
## [2,] 0.03160952 0.49094544 0.2201678 0.299879176
## [3,] 0.13781565 0.08819544 0.2564980 0.002341108
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.15506011 0.01781780 0.3482994 0.015063061
## [2,] 0.02021524 0.31397442 0.1408040 0.191781779
## [3,] 0.08813727 0.05640364 0.1640382 0.001497209
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.32152538 0.03694616 0.7222173 0.031234057
## [2,] 0.04191738 0.65104263 0.2919645 0.397669708
## [3,] 0.18275731 0.11695595 0.3401419 0.003104543
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.22787364 0.02618473 0.5118547 0.022136412
## [2,] 0.02970797 0.46141133 0.2069231 0.281839163
## [3,] 0.12952499 0.08288981 0.2410677 0.002200273
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.29570318 0.03397896 0.6642149 0.028725601
## [2,] 0.03855093 0.59875638 0.2685164 0.365732229
## [3,] 0.16807978 0.10756303 0.3128246 0.002855212
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.038108473 0.004379006 0.08560008 0.0037019852
## [2,] 0.004968215 0.077164173 0.03460480 0.0471334021
## [3,] 0.021661126 0.013862086 0.04031498 0.0003679628
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.028180251 0.003238164 0.06329909 0.0027375243
## [2,] 0.003673869 0.057060952 0.02558937 0.0348539572
## [3,] 0.016017854 0.010250662 0.02981190 0.0002720992
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.27172475 0.03122362 0.6103541 0.026396256
## [2,] 0.03542485 0.55020351 0.2467425 0.336075178
## [3,] 0.15445027 0.09884080 0.2874578 0.002623684
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12378300 0.01422378 0.2780441 0.012024697
## [2,] 0.01613763 0.25064276 0.1124025 0.153097551
## [3,] 0.07035913 0.04502649 0.1309501 0.001195208
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.34690662 0.03986269 0.7792292 0.033699676
## [2,] 0.04522634 0.70243599 0.3150122 0.429061783
## [3,] 0.19718418 0.12618846 0.3669927 0.003349616
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.021222635 0.002438672 0.04767075 0.0020616382
## [2,] 0.002766803 0.042972782 0.01927144 0.0262486247
## [3,] 0.012063096 0.007719806 0.02245144 0.0002049188
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.26298395 0.03021922 0.5907203 0.025547146
## [2,] 0.03428531 0.53250466 0.2388053 0.325264371
## [3,] 0.14948194 0.09566131 0.2782109 0.002539286
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13124023 0.01508068 0.2947947 0.012749117
## [2,] 0.01710984 0.26574258 0.1191741 0.162320819
## [3,] 0.07459788 0.04773908 0.1388391 0.001267212
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.055556740 0.006383969 0.12479276 0.0053969686
## [2,] 0.007242952 0.112494404 0.05044888 0.0687138054
## [3,] 0.031578845 0.020208953 0.05877351 0.0005364375
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.26305504 0.03022739 0.5908800 0.025554051
## [2,] 0.03429458 0.53264859 0.2388699 0.325352286
## [3,] 0.14952235 0.09568716 0.2782861 0.002539973
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02976175 0.003419893 0.06685149 0.0028911564
## [2,] 0.00388005 0.060263259 0.02702547 0.0368099895
## [3,] 0.01691679 0.010825937 0.03148497 0.0002873696
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.23615842 0.02713673 0.5304642 0.022941224
## [2,] 0.03078806 0.47818682 0.2144461 0.292085963
## [3,] 0.13423412 0.08590343 0.2498321 0.002280268
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10921274 0.01254953 0.24531603 0.010609293
## [2,] 0.01423810 0.22114007 0.09917177 0.135076728
## [3,] 0.06207729 0.03972650 0.11553622 0.001054522
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.34160625 0.03925363 0.7673234 0.033184780
## [2,] 0.04453533 0.69170349 0.3101991 0.422506163
## [3,] 0.19417141 0.12426043 0.3613854 0.003298437
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12952056 0.01488308 0.2909319 0.012582063
## [2,] 0.01688564 0.26226050 0.1176125 0.160193897
## [3,] 0.07362041 0.04711354 0.1370199 0.001250608
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3871823 0.04449073 0.8696974 0.037612194
## [2,] 0.0504771 0.78398851 0.3515850 0.478875676
## [3,] 0.2200772 0.14083889 0.4096004 0.003738505
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.15641322 0.01797329 0.3513388 0.015194507
## [2,] 0.02039165 0.31671427 0.1420327 0.193455336
## [3,] 0.08890639 0.05689584 0.1654696 0.001510274
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.27750653 0.0318880 0.6233412 0.026957918
## [2,] 0.03617863 0.5619108 0.2519927 0.343226211
## [3,] 0.15773668 0.1009439 0.2935743 0.002679511
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.29450197 0.03384093 0.6615167 0.028608912
## [2,] 0.03839433 0.59632410 0.2674256 0.364246545
## [3,] 0.16739701 0.10712609 0.3115538 0.002843614
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.16922564 0.01944555 0.3801183 0.016439148
## [2,] 0.02206201 0.34265757 0.1536671 0.209302015
## [3,] 0.09618905 0.06155640 0.1790239 0.001633987
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09093702 0.01044948 0.20426472 0.0088339283
## [2,] 0.01185549 0.18413438 0.08257632 0.1124729213
## [3,] 0.05168925 0.03307865 0.09620233 0.0008780579
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11357852 0.01305119 0.2551225 0.011033399
## [2,] 0.01480727 0.22998015 0.1031362 0.140476422
## [3,] 0.06455883 0.04131457 0.1201548 0.001096677
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3047940 0.03502357 0.6846348 0.02960871
## [2,] 0.0397361 0.61716389 0.2767714 0.37697590
## [3,] 0.1732470 0.11086983 0.3224417 0.00294299
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07751536 0.008907212 0.17411668 0.0075301024
## [2,] 0.01010570 0.156957441 0.07038863 0.0958727070
## [3,] 0.04406028 0.028196474 0.08200354 0.0007484627
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.17441911 0.02004232 0.3917840 0.016943659
## [2,] 0.02273908 0.35317359 0.1583831 0.215725407
## [3,] 0.09914106 0.06344554 0.1845181 0.001684133
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12880641 0.01480102 0.2893278 0.012512688
## [2,] 0.01679254 0.26081444 0.1169640 0.159310617
## [3,] 0.07321448 0.04685377 0.1362644 0.001243712
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09382463 0.01078129 0.21075093 0.0091144403
## [2,] 0.01223195 0.18998138 0.08519844 0.1160443796
## [3,] 0.05333059 0.03412903 0.09925713 0.0009059397
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09159778 0.01052541 0.20574894 0.008898117
## [2,] 0.01194164 0.18547233 0.08317633 0.113290166
## [3,] 0.05206483 0.03331901 0.09690135 0.000884438
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.34583424 0.03973947 0.7768204 0.033595502
## [2,] 0.04508653 0.70026457 0.3140384 0.427735440
## [3,] 0.19657464 0.12579838 0.3658582 0.003339261
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.24290761 0.02791227 0.5456244 0.023596862
## [2,] 0.03166795 0.49185296 0.2205748 0.300433507
## [3,] 0.13807041 0.08835847 0.2569721 0.002345436
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.010866769 0.001248689 0.024409172 0.0010556345
## [2,] 0.001416705 0.022003642 0.009867683 0.0134402597
## [3,] 0.006176748 0.003952824 0.011495961 0.0001049259
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.33645049 0.03866119 0.7557424 0.032683933
## [2,] 0.04386317 0.68126383 0.3055174 0.416129410
## [3,] 0.19124084 0.12238501 0.3559312 0.003248655
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.33185726 0.03813339 0.7454250 0.032237731
## [2,] 0.04326435 0.67196320 0.3013465 0.410448404
## [3,] 0.18863002 0.12071421 0.3510720 0.003204304
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.38476096 0.0442125 0.8642585 0.037376974
## [2,] 0.05016142 0.7790856 0.3493862 0.475880869
## [3,] 0.21870086 0.1399581 0.4070388 0.003715125
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.35675578 0.04099445 0.8013526 0.034656457
## [2,] 0.04651038 0.72237911 0.3239558 0.441243440
## [3,] 0.20278251 0.12977113 0.3774121 0.003444716
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.342142550 0.039315257 0.768528056 0.033236878
## [2,] 0.044605247 0.692789425 0.310686140 0.423169473
## [3,] 0.194476251 0.124455515 0.361952799 0.003303616
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.32460414 0.03729994 0.72913289 0.03153314
## [2,] 0.04231876 0.65727666 0.29476020 0.40147757
## [3,] 0.18450729 0.11807586 0.34339890 0.00313427
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.275534399 0.031661381 0.618911376 0.026766338
## [2,] 0.035921519 0.557917506 0.250201909 0.340787039
## [3,] 0.156615706 0.100226574 0.291487999 0.002660469
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.331857264 0.038133385 0.745425021 0.032237731
## [2,] 0.043264351 0.671963200 0.301346478 0.410448404
## [3,] 0.188630021 0.120714207 0.351071989 0.003204304
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.384760963 0.044212496 0.864258465 0.037376974
## [2,] 0.050161425 0.779085579 0.349386239 0.475880869
## [3,] 0.218700859 0.139958107 0.407038843 0.003715125
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.356755780 0.040994449 0.801352612 0.034656457
## [2,] 0.046510379 0.722379113 0.323955838 0.441243440
## [3,] 0.202782515 0.129771127 0.377412144 0.003444716
If there is a vanishing subscript on the left or right side of ->, the summation is done for that subscript.
einsum::einsum('i->', arrA)
## [1] 2.445973
DelayedTensor::einsum('i->', darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 2.445973
einsum::einsum('ij->', arrC)
## [1] 3.919752
DelayedTensor::einsum('ij->', darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 3.919752
einsum::einsum('ijk->', arrE)
## [1] 30.89062
DelayedTensor::einsum('ijk->', darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 30.89062
einsum::einsum('ij->i', arrC)
## [1] 1.3891615 1.7273200 0.8032705
DelayedTensor::einsum('ij->i', darrC)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 1.3891615 1.7273200 0.8032705
einsum::einsum('ij->j', arrC)
## [1] 0.6823855 1.0056437 1.6920008 0.5397221
DelayedTensor::einsum('ij->j', darrC)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 0.6823855 1.0056437 1.6920008 0.5397221
einsum::einsum('ijk->i', arrE)
## [1] 9.950626 9.513605 11.426390
DelayedTensor::einsum('ijk->i', darrE)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 9.950626 9.513605 11.426390
einsum::einsum('ijk->j', arrE)
## [1] 7.592191 8.649622 7.062647 7.586161
DelayedTensor::einsum('ijk->j', darrE)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 7.592191 8.649622 7.062647 7.586161
einsum::einsum('ijk->k', arrE)
## [1] 6.537756 6.791007 4.650892 6.499099 6.411867
DelayedTensor::einsum('ijk->k', darrE)
## <5> array of class HDF5Array and type "double":
## [1] [2] [3] [4] [5]
## 6.537756 6.791007 4.650892 6.499099 6.411867
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 3.119005 2.666838 2.457723 1.707059
## [2,] 1.687318 2.706010 2.288796 2.831480
## [3,] 2.785868 3.276773 2.316128 3.047621
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 3.119005 2.666838 2.457723 1.707059
## [2,] 1.687318 2.706010 2.288796 2.831480
## [3,] 2.785868 3.276773 2.316128 3.047621
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.345779 1.687193 0.9011675 1.710209 0.9478425
## [2,] 1.518660 2.284496 1.8482173 1.675702 1.3225468
## [3,] 1.648191 1.065588 1.0342418 1.845279 1.4693463
## [4,] 1.025126 1.753729 0.8672650 1.267910 2.6721310
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.3457793 1.6871935 0.9011675 1.7102087 0.9478425
## [2,] 1.5186596 2.2844964 1.8482173 1.6757015 1.3225468
## [3,] 1.6481915 1.0655884 1.0342418 1.8452792 1.4693463
## [4,] 1.0251256 1.7537290 0.8672650 1.2679100 2.6721310
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.345779 1.687193 0.9011675 1.710209 0.9478425
## [2,] 1.518660 2.284496 1.8482173 1.675702 1.3225468
## [3,] 1.648191 1.065588 1.0342418 1.845279 1.4693463
## [4,] 1.025126 1.753729 0.8672650 1.267910 2.6721310
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.3457793 1.6871935 0.9011675 1.7102087 0.9478425
## [2,] 1.5186596 2.2844964 1.8482173 1.6757015 1.3225468
## [3,] 1.6481915 1.0655884 1.0342418 1.8452792 1.4693463
## [4,] 1.0251256 1.7537290 0.8672650 1.2679100 2.6721310
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 2.006783
DelayedTensor::einsum('ii->', darrB)
## <1> array of class HDF5Array and type "double":
## [1]
## 2.006783
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.9857093 0.6354389 0.5036426
## [2,] 0.5509790 0.1380189 0.6506757
## [3,] 0.9595481 0.8698930 0.8830550
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3]
## [1,] 0.9857093 0.6354389 0.5036426
## [2,] 0.5509790 0.1380189 0.6506757
## [3,] 0.9595481 0.8698930 0.8830550
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.34097322 0.8734482 0.8331534
## [2,] 0.05357973 0.8550165 0.7067313
## [3,] 0.55493081 0.8625327 0.3407459
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.05385402 0.7976627 0.4773441
## [2,] 0.77897257 0.3430357 0.0154227
## [3,] 0.40698596 0.9184410 0.8206278
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.5450415 0.1845741 0.9916426
## [2,] 0.9375830 0.5239527 0.1038288
## [3,] 0.9986598 0.8908599 0.6971212
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.34097322 0.87344818 0.83315337
## [2,] 0.05357973 0.85501646 0.70673126
## [3,] 0.55493081 0.86253266 0.34074588
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.05385402 0.79766275 0.47734414
## [2,] 0.77897257 0.34303568 0.01542270
## [3,] 0.40698596 0.91844099 0.82062777
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.5450415 0.1845741 0.9916426
## [2,] 0.9375830 0.5239527 0.1038288
## [3,] 0.9986598 0.8908599 0.6971212
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.997592
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 1.997592
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 2.27741
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 2.27741
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 21.2692
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 21.2692
The inner product is an operation that eliminates all subscripts, while the outer product is an operation that leaves all subscripts intact. In the middle of the two, the operation that eliminates some subscripts while keeping others by summing them is called contracted product.
einsum::einsum('ijk,ijk->jk', arrE, arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.8490017 1.306335 0.5558295 1.1427675 0.3285999
## [2,] 0.9493000 2.007594 1.2983715 1.1846486 0.8477779
## [3,] 1.2461931 0.481797 0.5381557 1.1922576 1.0679523
## [4,] 0.6119308 1.111505 0.4534694 0.7069373 2.3887773
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.8490017 1.3063354 0.5558295 1.1427675 0.3285999
## [2,] 0.9493000 2.0075939 1.2983715 1.1846486 0.8477779
## [3,] 1.2461931 0.4817970 0.5381557 1.1922576 1.0679523
## [4,] 0.6119308 1.1115048 0.4534694 0.7069373 2.3887773
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 0.9791364 0.4070863 0.4820403
## [2,] 0.4070863 1.0441935 0.2877362
## [3,] 0.4820403 0.2877362 0.2540801
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.9791364 0.4070863 0.4820403
## [2,] 0.4070863 1.0441935 0.2877362
## [3,] 0.4820403 0.2877362 0.2540801
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.161356574 0.002742486 5.213215e-02
## [2,] 0.002130565 0.661568760 2.135014e-02
## [3,] 0.814126595 0.133050420 1.805828e-01
## [4,] 0.001522695 0.246831823 1.504359e-05
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 1.613566e-01 2.742486e-03 5.213215e-02
## [2,] 2.130565e-03 6.615688e-01 2.135014e-02
## [3,] 8.141266e-01 1.330504e-01 1.805828e-01
## [4,] 1.522695e-03 2.468318e-01 1.504359e-05
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.72548345 0.56284125 0.541907690 0.34563700 0.03723821
## [2,] 0.21906927 0.93379168 0.457584941 0.10396587 0.05455657
## [3,] 0.90564872 0.02486266 0.002791335 0.47726517 0.36567526
## [4,] 0.01695262 0.14900935 0.019128761 0.05125011 0.68252096
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.65301241 0.006125278 0.009000288 0.07391965 0.1885391067
## [2,] 0.05138978 0.958940713 0.094958826 0.92906137 0.0519975976
## [3,] 0.32442808 0.092607200 0.428619414 0.53751394 0.0007318367
## [4,] 0.01838329 0.640683986 0.428851146 0.07994765 0.9174773297
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.47050581 0.7373689 0.004921563 0.7232109 0.1028225
## [2,] 0.67884095 0.1148615 0.745827712 0.1516213 0.7412237
## [3,] 0.01611625 0.3643271 0.106744941 0.1774785 0.7015452
## [4,] 0.57659485 0.3218115 0.005489468 0.5757395 0.7887791
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.725483452 0.562841252 0.541907690 0.345636995 0.037238213
## [2,] 0.219069265 0.933791676 0.457584941 0.103965867 0.054556572
## [3,] 0.905648724 0.024862663 0.002791335 0.477265170 0.365675264
## [4,] 0.016952618 0.149009355 0.019128761 0.051250110 0.682520957
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.6530124080 0.0061252784 0.0090002884 0.0739196522 0.1885391067
## [2,] 0.0513897825 0.9589407128 0.0949588256 0.9290613741 0.0519975976
## [3,] 0.3244280787 0.0926071995 0.4286194138 0.5375139400 0.0007318367
## [4,] 0.0183832863 0.6406839862 0.4288511462 0.0799476543 0.9174773297
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.470505808 0.737368904 0.004921563 0.723210871 0.102822531
## [2,] 0.678840952 0.114861517 0.745827712 0.151621321 0.741223738
## [3,] 0.016116255 0.364327093 0.106744941 0.177478468 0.701545221
## [4,] 0.576594855 0.321811477 0.005489468 0.575739495 0.788779058
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 2.401660 1.739956 2.396141
## [2,] 2.260253 2.162261 2.368494
## [3,] 1.603734 1.712581 1.334576
## [4,] 1.827575 2.251664 2.419861
## [5,] 1.857405 1.647144 2.907318
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 2.401660 1.739956 2.396141
## [2,] 2.260253 2.162261 2.368494
## [3,] 1.603734 1.712581 1.334576
## [4,] 1.827575 2.251664 2.419861
## [5,] 1.857405 1.647144 2.907318
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,] 9.100489e-02 0.0706029973 6.797708e-02 4.335682e-02 4.671174e-03
## [2,] 3.628497e-04 0.0015466615 7.579089e-04 1.722012e-04 9.036336e-05
## [3,] 5.731948e-01 0.0157358478 1.766666e-03 3.020663e-01 2.314398e-01
## [4,] 2.006782e-05 0.0001763912 2.264385e-05 6.066781e-05 8.079407e-04
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.001537507 1.442187e-05 2.119103e-05 0.0001740426 4.439122e-04
## [2,] 0.029187911 5.446506e-01 5.393387e-02 0.5276800109 2.953313e-02
## [3,] 0.037058338 1.057821e-02 4.895977e-02 0.0613984249 8.359526e-05
## [4,] 0.003895611 1.357676e-01 9.087804e-02 0.0169417444 1.944230e-01
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.986906e-02 3.113846e-02 2.078334e-04 3.054058e-02 4.342108e-03
## [2,] 1.174020e-02 1.986470e-03 1.289870e-02 2.622211e-03 1.281908e-02
## [3,] 2.357475e-03 5.329353e-02 1.561458e-02 2.596143e-02 1.026216e-01
## [4,] 7.026336e-06 3.921567e-06 6.689419e-08 7.015913e-06 9.611995e-06
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,] 9.100489e-02 7.060300e-02 6.797708e-02 4.335682e-02 4.671174e-03
## [2,] 3.628497e-04 1.546662e-03 7.579089e-04 1.722012e-04 9.036336e-05
## [3,] 5.731948e-01 1.573585e-02 1.766666e-03 3.020663e-01 2.314398e-01
## [4,] 2.006782e-05 1.763912e-04 2.264385e-05 6.066781e-05 8.079407e-04
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.537507e-03 1.442187e-05 2.119103e-05 1.740426e-04 4.439122e-04
## [2,] 2.918791e-02 5.446506e-01 5.393387e-02 5.276800e-01 2.953313e-02
## [3,] 3.705834e-02 1.057821e-02 4.895977e-02 6.139842e-02 8.359526e-05
## [4,] 3.895611e-03 1.357676e-01 9.087804e-02 1.694174e-02 1.944230e-01
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.986906e-02 3.113846e-02 2.078334e-04 3.054058e-02 4.342108e-03
## [2,] 1.174020e-02 1.986470e-03 1.289870e-02 2.622211e-03 1.281908e-02
## [3,] 2.357475e-03 5.329353e-02 1.561458e-02 2.596143e-02 1.026216e-01
## [4,] 7.026336e-06 3.921567e-06 6.689419e-08 7.015913e-06 9.611995e-06
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)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.5 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.16-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.16-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.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