DelayedTensor 0.99.12
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2021-06-30 14:57:51
Compiled: Tue Oct 19 17:11:30 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.1983883 0.1375838 0.2693067
DelayedTensor::einsum('i->i', darrA)
## <3> array of class DelayedArray and type "double":
## [1] [2] [3]
## 0.1983883 0.1375838 0.2693067
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.2090852 0.8095193 0.50884476 0.5415699
## [2,] 0.1173939 0.8334801 0.79536863 0.7384519
## [3,] 0.4198542 0.4737470 0.09392804 0.5210869
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.20908521 0.80951926 0.50884476 0.54156989
## [2,] 0.11739395 0.83348007 0.79536863 0.73845192
## [3,] 0.41985423 0.47374705 0.09392804 0.52108693
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.28743692 0.5417449 0.6120253 0.2312607
## [2,] 0.05893053 0.2604354 0.3065686 0.3881261
## [3,] 0.33948113 0.5422268 0.3587400 0.4909633
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.001215349 0.46635990 0.076032548 0.1553539
## [2,] 0.519496897 0.07185673 0.006889913 0.4679501
## [3,] 0.233308035 0.45045430 0.541833027 0.6394749
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04640877 0.6701350 0.1221606 0.3519079
## [2,] 0.93755497 0.1528542 0.8095231 0.4081395
## [3,] 0.12053328 0.8223690 0.2434286 0.2861378
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.78355212 0.004254333 0.4065206 0.06723387
## [2,] 0.09518638 0.121652851 0.5081056 0.34250206
## [3,] 0.10413643 0.241727095 0.1152725 0.85464101
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7893551 0.71701673 0.2205570 0.5986210
## [2,] 0.7515596 0.02228179 0.2278475 0.9387999
## [3,] 0.4266968 0.45508745 0.9444303 0.6705511
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.28743692 0.54174489 0.61202533 0.23126074
## [2,] 0.05893053 0.26043541 0.30656861 0.38812615
## [3,] 0.33948113 0.54222680 0.35873998 0.49096327
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.001215349 0.466359896 0.076032548 0.155353921
## [2,] 0.519496897 0.071856734 0.006889913 0.467950083
## [3,] 0.233308035 0.450454302 0.541833027 0.639474926
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.04640877 0.67013498 0.12216058 0.35190793
## [2,] 0.93755497 0.15285420 0.80952314 0.40813947
## [3,] 0.12053328 0.82236904 0.24342860 0.28613782
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.783552120 0.004254333 0.406520645 0.067233870
## [2,] 0.095186377 0.121652851 0.508105618 0.342502061
## [3,] 0.104136431 0.241727095 0.115272457 0.854641006
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.78935514 0.71701673 0.22055701 0.59862097
## [2,] 0.75155965 0.02228179 0.22784753 0.93879991
## [3,] 0.42669676 0.45508745 0.94443029 0.67055105
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.8803983 0.1745346 0.4127019
DelayedTensor::einsum('ii->i', darrB)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.8803983 0.1745346 0.4127019
einsum::einsum('iii->i', arrD)
## [1] 0.6186154 0.5811988 0.9434322
DelayedTensor::einsum('iii->i', darrD)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.6186154 0.5811988 0.9434322
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.03935791 0.01892929 0.07252611
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.03935791 0.01892929 0.07252611
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.04371663 0.6553214 0.258922990 0.2932979
## [2,] 0.01378134 0.6946890 0.632611259 0.5453112
## [3,] 0.17627757 0.2244363 0.008822477 0.2715316
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.043716625 0.655321431 0.258922990 0.293297945
## [2,] 0.013781338 0.694689028 0.632611259 0.545311234
## [3,] 0.176277574 0.224436267 0.008822477 0.271531589
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.082619983 0.2934875 0.37457500 0.05348153
## [2,] 0.003472807 0.0678266 0.09398431 0.15064191
## [3,] 0.115247437 0.2940099 0.12869437 0.24104493
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 1.477073e-06 0.21749155 0.0057809483 0.02413484
## [2,] 2.698770e-01 0.00516339 0.0000474709 0.21897728
## [3,] 5.443264e-02 0.20290908 0.2935830294 0.40892818
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.002153774 0.44908089 0.01492321 0.12383919
## [2,] 0.879009313 0.02336441 0.65532772 0.16657782
## [3,] 0.014528273 0.67629083 0.05925749 0.08187485
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.613953925 1.809935e-05 0.16525904 0.004520393
## [2,] 0.009060446 1.479942e-02 0.25817132 0.117307662
## [3,] 0.010844396 5.843199e-02 0.01328774 0.730411249
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6230815 0.5141129864 0.04864540 0.3583471
## [2,] 0.5648419 0.0004964784 0.05191449 0.8813453
## [3,] 0.1820701 0.2071045902 0.89194858 0.4496387
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.082619983 0.293487524 0.374575000 0.053481529
## [2,] 0.003472807 0.067826603 0.093984313 0.150641905
## [3,] 0.115247437 0.294009898 0.128694373 0.241044930
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 1.477073e-06 2.174916e-01 5.780948e-03 2.413484e-02
## [2,] 2.698770e-01 5.163390e-03 4.747090e-05 2.189773e-01
## [3,] 5.443264e-02 2.029091e-01 2.935830e-01 4.089282e-01
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.002153774 0.449080891 0.014923208 0.123839189
## [2,] 0.879009313 0.023364406 0.655327716 0.166577823
## [3,] 0.014528273 0.676290831 0.059257486 0.081874853
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 6.139539e-01 1.809935e-05 1.652590e-01 4.520393e-03
## [2,] 9.060446e-03 1.479942e-02 2.581713e-01 1.173077e-01
## [3,] 1.084440e-02 5.843199e-02 1.328774e-02 7.304112e-01
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.6230815351 0.5141129864 0.0486453951 0.3583470638
## [2,] 0.5648419072 0.0004964784 0.0519144947 0.8813452715
## [3,] 0.1820701289 0.2071045902 0.8919485797 0.4496387159
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.03935791 0.02729500 0.05342730
## [2,] 0.02729500 0.01892929 0.03705223
## [3,] 0.05342730 0.03705223 0.07252611
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.03935791 0.02729500 0.05342730
## [2,] 0.02729500 0.01892929 0.03705223
## [3,] 0.05342730 0.03705223 0.07252611
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06009881 0.2326857 0.14626077 0.1556672
## [2,] 0.03374335 0.2395729 0.22861831 0.2122583
## [3,] 0.12068161 0.1361724 0.02699839 0.1497796
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.012321502 0.04770540 0.029986492 0.03191500
## [2,] 0.006918088 0.04911742 0.046871496 0.04351736
## [3,] 0.024742233 0.02791817 0.005535229 0.03070793
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07098048 0.2748165 0.1727432 0.1838528
## [2,] 0.03985303 0.2829508 0.2700126 0.2506905
## [3,] 0.14253259 0.1608282 0.0318868 0.1768992
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11327084 0.4385529 0.27566405 0.2933927
## [2,] 0.06359757 0.4515336 0.43088689 0.4000526
## [3,] 0.22745388 0.2566500 0.05088504 0.2822962
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05445319 0.2108275 0.13252119 0.1410440
## [2,] 0.03057354 0.2170677 0.20714216 0.1923190
## [3,] 0.10934491 0.1233805 0.02446219 0.1357095
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11337160 0.4389430 0.2759093 0.2936537
## [2,] 0.06365414 0.4519352 0.4312702 0.4004084
## [3,] 0.22765621 0.2568783 0.0509303 0.2825473
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12796544 0.4954463 0.31142588 0.3314545
## [2,] 0.07184807 0.5101109 0.48678575 0.4519513
## [3,] 0.25696142 0.2899452 0.05748634 0.3189184
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06409896 0.2481732 0.15599583 0.1660283
## [2,] 0.03598930 0.2555188 0.24383506 0.2263862
## [3,] 0.12871413 0.1452360 0.02879539 0.1597489
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07500722 0.2904069 0.18254296 0.1942828
## [2,] 0.04211390 0.2990026 0.28533053 0.2649122
## [3,] 0.15061850 0.1699520 0.03369574 0.1869347
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04835320 0.1872100 0.11767581 0.1252439
## [2,] 0.02714861 0.1927512 0.18393754 0.1707749
## [3,] 0.09709580 0.1095591 0.02172187 0.1205069
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08115144 0.3141956 0.19749596 0.2101974
## [2,] 0.04556366 0.3234954 0.30870336 0.2866125
## [3,] 0.16295640 0.1838736 0.03645593 0.2022475
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10265316 0.3974442 0.24982409 0.2658909
## [2,] 0.05763612 0.4092081 0.39049678 0.3625528
## [3,] 0.20613300 0.2325924 0.04611522 0.2558345
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0002541114 0.0009838482 0.0006184238 0.0006581963
## [2,] 0.0001426746 0.0010129689 0.0009666502 0.0008974766
## [3,] 0.0005102693 0.0005757679 0.0001141553 0.0006333023
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10861912 0.4205427 0.26434327 0.2813439
## [2,] 0.06098579 0.4329903 0.41319154 0.3836235
## [3,] 0.21811297 0.2461101 0.04879532 0.2707030
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04878126 0.1888673 0.11871757 0.1263526
## [2,] 0.02738895 0.1944576 0.18556589 0.1722868
## [3,] 0.09795537 0.1105290 0.02191417 0.1215738
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09750896 0.3775273 0.23730479 0.2525665
## [2,] 0.05474783 0.3887017 0.37092803 0.3443844
## [3,] 0.19580317 0.2209366 0.04380427 0.2430140
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.015024180 0.05816941 0.036563923 0.03891544
## [2,] 0.008435546 0.05989116 0.057152592 0.05306274
## [3,] 0.030169354 0.03404192 0.006749362 0.03744361
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09418333 0.3646514 0.22921131 0.2439525
## [2,] 0.05288061 0.3754447 0.35827722 0.3326388
## [3,] 0.18912514 0.2134014 0.04231029 0.2347258
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.015897281 0.06154981 0.038688764 0.04117694
## [2,] 0.008925761 0.06337161 0.060473903 0.05614638
## [3,] 0.031922587 0.03602020 0.007141588 0.03961957
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.001440579 0.005577517 0.003505896 0.003731369
## [2,] 0.000808834 0.005742605 0.005480020 0.005087869
## [3,] 0.002892759 0.003264076 0.000647156 0.003590243
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11328927 0.4386243 0.27570890 0.2934405
## [2,] 0.06360792 0.4516070 0.43095699 0.4001176
## [3,] 0.22749089 0.2566918 0.05089331 0.2823421
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03248221 0.12576199 0.07905103 0.08413501
## [2,] 0.01823761 0.12948440 0.12356364 0.11472140
## [3,] 0.06522600 0.07359846 0.01459209 0.08095290
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09784144 0.3788146 0.23811395 0.2534277
## [2,] 0.05493451 0.3900271 0.37219282 0.3455586
## [3,] 0.19647082 0.2216900 0.04395363 0.2438427
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13370475 0.5176673 0.32539347 0.3463204
## [2,] 0.07507048 0.5329896 0.50861830 0.4722215
## [3,] 0.26848625 0.3029494 0.06006463 0.3332220
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.009703387 0.03756879 0.023614858 0.02513359
## [2,] 0.005448108 0.03868078 0.036912077 0.03427064
## [3,] 0.019484917 0.02198602 0.004359084 0.02418300
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1960289 0.7589688 0.4770699 0.5077515
## [2,] 0.1100633 0.7814334 0.7457018 0.6923393
## [3,] 0.3936364 0.4441639 0.0880627 0.4885476
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02520173 0.09757401 0.06133273 0.06527720
## [2,] 0.01414988 0.10046209 0.09586839 0.08900803
## [3,] 0.05060641 0.05710229 0.01132146 0.06280832
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.14011531 0.5424872 0.34099467 0.3629249
## [2,] 0.07866979 0.5585442 0.53300434 0.4948625
## [3,] 0.28135901 0.3174745 0.06294446 0.3491986
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03195955 0.12373842 0.07777906 0.08278123
## [2,] 0.01794416 0.12740093 0.12157543 0.11287548
## [3,] 0.06417648 0.07241422 0.01435730 0.07965032
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.17194520 0.6657236 0.41845818 0.4453703
## [2,] 0.09654115 0.6854282 0.65408653 0.6072800
## [3,] 0.34527512 0.3895949 0.07724351 0.4285258
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02554197 0.09889134 0.06216077 0.06615849
## [2,] 0.01434091 0.10181841 0.09716270 0.09020972
## [3,] 0.05128964 0.05787322 0.01147430 0.06365628
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.16925932 0.6553246 0.41192161 0.4384134
## [2,] 0.09503312 0.6747214 0.64386931 0.5977939
## [3,] 0.33988171 0.3835092 0.07603692 0.4218319
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05089732 0.1970601 0.12386737 0.1318336
## [2,] 0.02857704 0.2028929 0.19361548 0.1797603
## [3,] 0.10220453 0.1153236 0.02286477 0.1268475
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07357874 0.2848762 0.17906651 0.1905827
## [2,] 0.04131186 0.2933082 0.27989653 0.2598671
## [3,] 0.14775003 0.1667153 0.03305402 0.1833746
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08533593 0.3303968 0.20767963 0.2210360
## [2,] 0.04791310 0.3401761 0.32462133 0.3013914
## [3,] 0.17135908 0.1933549 0.03833574 0.2126761
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05982719 0.2316341 0.14559973 0.1549636
## [2,] 0.03359085 0.2384902 0.22758505 0.2112990
## [3,] 0.12013617 0.1355569 0.02687636 0.1491027
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.16382916 0.6343005 0.39870639 0.4243482
## [2,] 0.09198427 0.6530751 0.62321278 0.5786156
## [3,] 0.32897767 0.3712055 0.07359751 0.4082988
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01990206 0.07705521 0.04843509 0.05155008
## [2,] 0.01117430 0.07933595 0.07570826 0.07029056
## [3,] 0.03996440 0.04509427 0.00894067 0.04960038
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02177339 0.08430045 0.052989277 0.05639716
## [2,] 0.01222499 0.08679564 0.082826851 0.07689975
## [3,] 0.04372212 0.04933433 0.009781331 0.05426413
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0008895181 0.003443964 0.0021647949 0.002304019
## [2,] 0.0004994329 0.003545902 0.0033837628 0.003141620
## [3,] 0.0017861996 0.002015478 0.0003996011 0.002216877
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02543581 0.09848033 0.06190242 0.06588352
## [2,] 0.01428131 0.10139523 0.09675886 0.08983478
## [3,] 0.05107646 0.05763268 0.01142661 0.06339171
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05054156 0.1956827 0.12300157 0.1309121
## [2,] 0.02837730 0.2014747 0.19226215 0.1785038
## [3,] 0.10149014 0.1145175 0.02270495 0.1259608
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08499745 0.3290863 0.20685590 0.2201593
## [2,] 0.04772306 0.3388269 0.32333377 0.3001960
## [3,] 0.17067941 0.1925880 0.03818369 0.2118326
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10623737 0.4113213 0.25854688 0.2751747
## [2,] 0.05964852 0.4234959 0.40413127 0.3752116
## [3,] 0.21333029 0.2407135 0.04772536 0.2647672
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02410177 0.09331527 0.05865579 0.06242809
## [2,] 0.01353229 0.09607730 0.09168410 0.08512317
## [3,] 0.04839763 0.05460999 0.01082732 0.06006697
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.014057608 0.05442711 0.034211602 0.03641184
## [2,] 0.007892849 0.05603809 0.053475711 0.04964898
## [3,] 0.028228425 0.03185185 0.006315146 0.03503469
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07161212 0.2772620 0.17428038 0.1854888
## [2,] 0.04020767 0.2854686 0.27241540 0.2529213
## [3,] 0.14380094 0.1622593 0.03217055 0.1784733
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1786928 0.6918484 0.43487960 0.4628478
## [2,] 0.1003297 0.7123262 0.67975465 0.6311113
## [3,] 0.3588246 0.4048837 0.08027475 0.4453423
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.16504249 0.6389982 0.40165923 0.4274910
## [2,] 0.09266551 0.6579118 0.62782832 0.5829008
## [3,] 0.33141409 0.3739547 0.07414258 0.4113226
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.15714001 0.6084020 0.38242719 0.4070221
## [2,] 0.08822855 0.6264100 0.59776697 0.5549907
## [3,] 0.31554550 0.3560492 0.07059252 0.3916279
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08921598 0.3454192 0.21712241 0.2310861
## [2,] 0.05009162 0.3556432 0.33938122 0.3150950
## [3,] 0.17915044 0.2021463 0.04007879 0.2223461
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.14991759 0.5804388 0.36485020 0.3883147
## [2,] 0.08417342 0.5976192 0.57029261 0.5294824
## [3,] 0.30104251 0.3396846 0.06734798 0.3736280
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.004658794 0.01803754 0.011337974 0.01206715
## [2,] 0.002615748 0.01857143 0.017722240 0.01645403
## [3,] 0.009355106 0.01055593 0.002092885 0.01161075
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09515206 0.3684021 0.23156887 0.2464617
## [2,] 0.05342451 0.3793063 0.36196228 0.3360602
## [3,] 0.19107039 0.2155963 0.04274547 0.2371401
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04611521 0.1785451 0.11222928 0.1194470
## [2,] 0.02589206 0.1838299 0.17542413 0.1628707
## [3,] 0.09260179 0.1044882 0.02071649 0.1149294
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04763955 0.1844470 0.11593902 0.1233954
## [2,] 0.02674792 0.1899064 0.18122277 0.1682544
## [3,] 0.09566275 0.1079421 0.02140127 0.1187284
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1974664 0.7645345 0.48056841 0.5114750
## [2,] 0.1108704 0.7871638 0.75117023 0.6974164
## [3,] 0.3965231 0.4474211 0.08870849 0.4921303
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12516279 0.4845952 0.30460514 0.3241951
## [2,] 0.07027448 0.4989386 0.47612434 0.4420528
## [3,] 0.25133355 0.2835949 0.05622729 0.3119336
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1962892 0.7599766 0.47770342 0.5084258
## [2,] 0.1102094 0.7824710 0.74669200 0.6932586
## [3,] 0.3941591 0.4447537 0.08817963 0.4891964
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.14020231 0.5428240 0.34120639 0.3631503
## [2,] 0.07871863 0.5588909 0.53333527 0.4951697
## [3,] 0.28153370 0.3176716 0.06298355 0.3494154
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.06009881 0.23268572 0.14626077 0.15566718
## [2,] 0.03374335 0.23957294 0.22861831 0.21225834
## [3,] 0.12068161 0.13617239 0.02699839 0.14977962
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.012321502 0.047705400 0.029986492 0.031915001
## [2,] 0.006918088 0.049117423 0.046871496 0.043517363
## [3,] 0.024742233 0.027918165 0.005535229 0.030707929
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.07098048 0.27481651 0.17274319 0.18385276
## [2,] 0.03985303 0.28295076 0.27001264 0.25069049
## [3,] 0.14253259 0.16082818 0.03188680 0.17689918
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.12516279 0.48459520 0.30460514 0.32419509
## [2,] 0.07027448 0.49893865 0.47612434 0.44205280
## [3,] 0.25133355 0.28359492 0.05622729 0.31193356
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.19628918 0.75997661 0.47770342 0.50842576
## [2,] 0.11020943 0.78247102 0.74669200 0.69325859
## [3,] 0.39415911 0.44475369 0.08817963 0.48919636
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.14020231 0.54282399 0.34120639 0.36315026
## [2,] 0.07871863 0.55889094 0.53333527 0.49516971
## [3,] 0.28153370 0.31767158 0.06298355 0.34941539
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] 0.6052788
DelayedTensor::einsum('i->', darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 0.6052788
einsum::einsum('ij->', arrC)
## [1] 6.06233
DelayedTensor::einsum('ij->', darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 6.06233
einsum::einsum('ijk->', arrE)
## [1] 23.42691
DelayedTensor::einsum('ijk->', darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 23.42691
einsum::einsum('ij->i', arrC)
## [1] 2.069019 2.484695 1.508616
DelayedTensor::einsum('ij->i', darrC)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 2.069019 2.484695 1.508616
einsum::einsum('ij->j', arrC)
## [1] 0.7463334 2.1167464 1.3981414 1.8011087
DelayedTensor::einsum('ij->j', darrC)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 0.7463334 2.1167464 1.3981414 1.8011087
einsum::einsum('ijk->i', arrE)
## [1] 7.149153 7.396262 8.881493
DelayedTensor::einsum('ijk->i', darrE)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 7.149153 7.396262 8.881493
einsum::einsum('ijk->j', arrE)
## [1] 5.494852 5.540456 5.499935 6.891663
DelayedTensor::einsum('ijk->j', darrE)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 5.494852 5.540456 5.499935 6.891663
einsum::einsum('ijk->k', arrE)
## [1] 4.417940 3.630226 4.971153 3.644785 6.762804
DelayedTensor::einsum('ijk->k', darrE)
## <5> array of class HDF5Array and type "double":
## [1] [2] [3] [4] [5]
## 4.417940 3.630226 4.971153 3.644785 6.762804
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 1.907968 2.399511 1.437296 1.404377
## [2,] 2.362728 0.629081 1.858935 2.545518
## [3,] 1.224156 2.511865 2.203704 2.941768
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 1.907968 2.399511 1.437296 1.404377
## [2,] 2.362728 0.629081 1.858935 2.545518
## [3,] 1.224156 2.511865 2.203704 2.941768
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.6858486 0.7540203 1.104497 0.9828749 1.967612
## [2,] 1.3444071 0.9886709 1.645358 0.3676343 1.194386
## [3,] 1.2773339 0.6247555 1.175112 1.0298987 1.392835
## [4,] 1.1103502 1.2627789 1.046185 1.2643769 2.207972
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.6858486 0.7540203 1.1044970 0.9828749 1.9676116
## [2,] 1.3444071 0.9886709 1.6453582 0.3676343 1.1943860
## [3,] 1.2773339 0.6247555 1.1751123 1.0298987 1.3928348
## [4,] 1.1103502 1.2627789 1.0461852 1.2643769 2.2079719
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.6858486 0.7540203 1.104497 0.9828749 1.967612
## [2,] 1.3444071 0.9886709 1.645358 0.3676343 1.194386
## [3,] 1.2773339 0.6247555 1.175112 1.0298987 1.392835
## [4,] 1.1103502 1.2627789 1.046185 1.2643769 2.207972
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.6858486 0.7540203 1.1044970 0.9828749 1.9676116
## [2,] 1.3444071 0.9886709 1.6453582 0.3676343 1.1943860
## [3,] 1.2773339 0.6247555 1.1751123 1.0298987 1.3928348
## [4,] 1.1103502 1.2627789 1.0461852 1.2643769 2.2079719
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.467635
DelayedTensor::einsum('ii->', darrB)
## <1> array of class HDF5Array and type "double":
## [1]
## 1.467635
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.8803983 0.1000342 0.9818683
## [2,] 0.2741444 0.1745346 0.6258982
## [3,] 0.2366780 0.8424701 0.4127019
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3]
## [1,] 0.8803983 0.1000342 0.9818683
## [2,] 0.2741444 0.1745346 0.6258982
## [3,] 0.2366780 0.8424701 0.4127019
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.6186154 0.2794173 0.8450817
## [2,] 0.5800748 0.9138731 0.6251284
## [3,] 0.4841781 0.7601985 0.5831567
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.6211771 0.1984467 0.09474103
## [2,] 0.1255472 0.5811988 0.38155974
## [3,] 0.4442713 0.6768137 0.54281407
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.78457746 0.3164061 0.1291121
## [2,] 0.78161693 0.1726539 0.8144352
## [3,] 0.07164174 0.6433342 0.9434322
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.6186154 0.2794173 0.8450817
## [2,] 0.5800748 0.9138731 0.6251284
## [3,] 0.4841781 0.7601985 0.5831567
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.62117712 0.19844671 0.09474103
## [2,] 0.12554720 0.58119883 0.38155974
## [3,] 0.44427133 0.67681374 0.54281407
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.78457746 0.31640608 0.12911215
## [2,] 0.78161693 0.17265394 0.81443515
## [3,] 0.07164174 0.64333424 0.94343218
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.1308133
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 0.1308133
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 3.81872
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 3.81872
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 13.51625
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 13.51625
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.2013402 0.3243111 0.8956914 0.6338588 1.3699936
## [2,] 0.6553240 0.4255640 1.1487361 0.0732495 0.7217141
## [3,] 0.5972537 0.2994114 0.7295084 0.4367181 0.9925085
## [4,] 0.4451684 0.6520403 0.3722919 0.8522393 1.6893311
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.2013402 0.3243111 0.8956914 0.6338588 1.3699936
## [2,] 0.6553240 0.4255640 1.1487361 0.0732495 0.7217141
## [3,] 0.5972537 0.2994114 0.7295084 0.4367181 0.9925085
## [4,] 0.4451684 0.6520403 0.3722919 0.8522393 1.6893311
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.2512590 1.5039060 0.8012925
## [2,] 1.5039060 1.8863929 0.9036521
## [3,] 0.8012925 0.9036521 0.6810679
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 1.2512590 1.5039060 0.8012925
## [2,] 1.5039060 1.8863929 0.9036521
## [3,] 0.8012925 0.9036521 0.6810679
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.04371663 0.01378134 0.176277574
## [2,] 0.65532143 0.69468903 0.224436267
## [3,] 0.25892299 0.63261126 0.008822477
## [4,] 0.29329794 0.54531123 0.271531589
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.043716625 0.013781338 0.176277574
## [2,] 0.655321431 0.694689028 0.224436267
## [3,] 0.258922990 0.632611259 0.008822477
## [4,] 0.293297945 0.545311234 0.271531589
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.08261998 1.477073e-06 0.002153774 6.139539e-01 0.6230815
## [2,] 0.29348752 2.174916e-01 0.449080891 1.809935e-05 0.5141130
## [3,] 0.37457500 5.780948e-03 0.014923208 1.652590e-01 0.0486454
## [4,] 0.05348153 2.413484e-02 0.123839189 4.520393e-03 0.3583471
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.003472807 0.2698770262 0.87900931 0.009060446 0.5648419072
## [2,] 0.067826603 0.0051633902 0.02336441 0.014799416 0.0004964784
## [3,] 0.093984313 0.0000474709 0.65532772 0.258171319 0.0519144947
## [4,] 0.150641905 0.2189772805 0.16657782 0.117307662 0.8813452715
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.1152474 0.05443264 0.01452827 0.01084440 0.1820701
## [2,] 0.2940099 0.20290908 0.67629083 0.05843199 0.2071046
## [3,] 0.1286944 0.29358303 0.05925749 0.01328774 0.8919486
## [4,] 0.2410449 0.40892818 0.08187485 0.73041125 0.4496387
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,] 8.261998e-02 1.477073e-06 2.153774e-03 6.139539e-01 6.230815e-01
## [2,] 2.934875e-01 2.174916e-01 4.490809e-01 1.809935e-05 5.141130e-01
## [3,] 3.745750e-01 5.780948e-03 1.492321e-02 1.652590e-01 4.864540e-02
## [4,] 5.348153e-02 2.413484e-02 1.238392e-01 4.520393e-03 3.583471e-01
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0034728074 0.2698770262 0.8790093129 0.0090604464 0.5648419072
## [2,] 0.0678266034 0.0051633902 0.0233644056 0.0147994162 0.0004964784
## [3,] 0.0939843126 0.0000474709 0.6553277162 0.2581713188 0.0519144947
## [4,] 0.1506419051 0.2189772805 0.1665778234 0.1173076617 0.8813452715
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.11524744 0.05443264 0.01452827 0.01084440 0.18207013
## [2,] 0.29400990 0.20290908 0.67629083 0.05843199 0.20710459
## [3,] 0.12869437 0.29358303 0.05925749 0.01328774 0.89194858
## [4,] 0.24104493 0.40892818 0.08187485 0.73041125 0.44963872
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 1.6724679 1.014061 1.731411
## [2,] 0.6989617 1.066194 1.865070
## [3,] 1.1906123 2.308072 1.472469
## [4,] 1.2615610 1.067447 1.315777
## [5,] 2.3255498 1.940489 2.496766
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 1.6724679 1.0140607 1.7314112
## [2,] 0.6989617 1.0661936 1.8650703
## [3,] 1.1906123 2.3080718 1.4724687
## [4,] 1.2615610 1.0674469 1.3157770
## [5,] 2.3255498 1.9404889 2.4967656
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.000716552 1.281045e-08 1.867939e-05 5.324740e-03 0.005403903
## [2,] 0.038155752 2.827566e-02 5.838415e-02 2.353062e-06 0.066838847
## [3,] 0.019240901 2.969516e-04 7.665647e-04 8.488908e-03 0.002498782
## [4,] 0.003111923 1.404331e-03 7.205815e-03 2.630276e-04 0.020851095
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 6.584750e-06 5.117107e-04 0.001666679 1.717941e-05 1.070990e-03
## [2,] 6.482726e-03 4.935061e-04 0.002233121 1.414498e-03 4.745237e-05
## [3,] 8.180116e-03 4.131726e-06 0.057037781 2.247047e-02 4.518484e-03
## [4,] 1.130205e-02 1.642898e-02 0.012497663 8.801121e-03 6.612378e-02
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0054711112 0.0025840664 0.0006896969 5.148132e-04 0.008643367
## [2,] 0.0177706041 0.0122642704 0.0408765035 3.531758e-03 0.012517856
## [3,] 0.0003057717 0.0006975393 0.0001407930 3.157103e-05 0.002119227
## [4,] 0.0176264790 0.0299029894 0.0059871219 5.341153e-02 0.032879959
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,] 7.165520e-04 1.281045e-08 1.867939e-05 5.324740e-03 5.403903e-03
## [2,] 3.815575e-02 2.827566e-02 5.838415e-02 2.353062e-06 6.683885e-02
## [3,] 1.924090e-02 2.969516e-04 7.665647e-04 8.488908e-03 2.498782e-03
## [4,] 3.111923e-03 1.404331e-03 7.205815e-03 2.630276e-04 2.085110e-02
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 6.584750e-06 5.117107e-04 1.666679e-03 1.717941e-05 1.070990e-03
## [2,] 6.482726e-03 4.935061e-04 2.233121e-03 1.414498e-03 4.745237e-05
## [3,] 8.180116e-03 4.131726e-06 5.703778e-02 2.247047e-02 4.518484e-03
## [4,] 1.130205e-02 1.642898e-02 1.249766e-02 8.801121e-03 6.612378e-02
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 5.471111e-03 2.584066e-03 6.896969e-04 5.148132e-04 8.643367e-03
## [2,] 1.777060e-02 1.226427e-02 4.087650e-02 3.531758e-03 1.251786e-02
## [3,] 3.057717e-04 6.975393e-04 1.407930e-04 3.157103e-05 2.119227e-03
## [4,] 1.762648e-02 2.990299e-02 5.987122e-03 5.341153e-02 3.287996e-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.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.1.0 HDF5Array_1.21.0
## [4] rhdf5_2.37.4 DelayedArray_0.19.4 IRanges_2.27.2
## [7] S4Vectors_0.31.5 MatrixGenerics_1.5.4 matrixStats_0.61.0
## [10] BiocGenerics_0.39.2 Matrix_1.3-4 DelayedTensor_0.99.12
## [13] BiocStyle_2.21.4
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.7 rTensor_1.4.8 bslib_0.3.1
## [4] compiler_4.1.1 BiocManager_1.30.16 jquerylib_0.1.4
## [7] rhdf5filters_1.5.0 tools_4.1.1 digest_0.6.28
## [10] jsonlite_1.7.2 evaluate_0.14 lattice_0.20-45
## [13] rlang_0.4.12 parallel_4.1.1 yaml_2.2.1
## [16] xfun_0.27 fastmap_1.1.0 stringr_1.4.0
## [19] knitr_1.36 sass_0.4.0 grid_4.1.1
## [22] R6_2.5.1 BiocParallel_1.27.17 rmarkdown_2.11
## [25] bookdown_0.24 irlba_2.3.3 Rhdf5lib_1.15.2
## [28] magrittr_2.0.1 BiocSingular_1.9.1 htmltools_0.5.2
## [31] rsvd_1.0.5 beachmat_2.9.1 dqrng_0.3.0
## [34] ScaledMatrix_1.1.0 stringi_1.7.5