DelayedTensor 1.2.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2022-04-26 14:34:11
Compiled: Tue Apr 26 18:21:18 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.2828383 0.2131744 0.7042117
DelayedTensor::einsum('i->i', darrA)
## <3> array of class DelayedArray and type "double":
## [1] [2] [3]
## 0.2828383 0.2131744 0.7042117
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.5275046 0.01085191 0.21332747 0.4965816
## [2,] 0.3491240 0.29238731 0.69132090 0.1315088
## [3,] 0.6236387 0.52978712 0.01897606 0.6313350
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.52750461 0.01085191 0.21332747 0.49658156
## [2,] 0.34912400 0.29238731 0.69132090 0.13150878
## [3,] 0.62363867 0.52978712 0.01897606 0.63133500
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3463339 0.4722079 0.8638290 0.6009584
## [2,] 0.1332262 0.6369022 0.6225681 0.3965724
## [3,] 0.7639394 0.4599283 0.8727685 0.4267361
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5377924 0.4554640 0.7698408 0.6756507
## [2,] 0.6544945 0.9661937 0.6185649 0.8267853
## [3,] 0.6026178 0.2134778 0.5372644 0.5254122
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8352814 0.5012907 0.97576455 0.178944401
## [2,] 0.8426286 0.2568417 0.02746262 0.003590035
## [3,] 0.6750857 0.2190483 0.81905207 0.288210966
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8928000 0.9630793 0.8176109 0.5137843
## [2,] 0.8123430 0.3046765 0.6090077 0.2569556
## [3,] 0.5036312 0.4411903 0.9504870 0.6800592
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8660039 0.4440928 0.9824922 0.6282580
## [2,] 0.3624032 0.3040413 0.1529963 0.8966557
## [3,] 0.3127924 0.5818454 0.5949311 0.1003522
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.3463339 0.4722079 0.8638290 0.6009584
## [2,] 0.1332262 0.6369022 0.6225681 0.3965724
## [3,] 0.7639394 0.4599283 0.8727685 0.4267361
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.5377924 0.4554640 0.7698408 0.6756507
## [2,] 0.6544945 0.9661937 0.6185649 0.8267853
## [3,] 0.6026178 0.2134778 0.5372644 0.5254122
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.835281420 0.501290697 0.975764548 0.178944401
## [2,] 0.842628643 0.256841681 0.027462621 0.003590035
## [3,] 0.675085706 0.219048304 0.819052072 0.288210966
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.8928000 0.9630793 0.8176109 0.5137843
## [2,] 0.8123430 0.3046765 0.6090077 0.2569556
## [3,] 0.5036312 0.4411903 0.9504870 0.6800592
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.8660039 0.4440928 0.9824922 0.6282580
## [2,] 0.3624032 0.3040413 0.1529963 0.8966557
## [3,] 0.3127924 0.5818454 0.5949311 0.1003522
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.2223454 0.7559259 0.4497429
DelayedTensor::einsum('ii->i', darrB)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.2223454 0.7559259 0.4497429
einsum::einsum('iii->i', arrD)
## [1] 0.8249277 0.5208168 0.8218851
DelayedTensor::einsum('iii->i', darrD)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.8249277 0.5208168 0.8218851
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.07999750 0.04544331 0.49591415
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.07999750 0.04544331 0.49591415
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.2782611 0.000117764 0.045508610 0.24659324
## [2,] 0.1218876 0.085490338 0.477924583 0.01729456
## [3,] 0.3889252 0.280674393 0.000360091 0.39858388
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.278261115 0.000117764 0.045508610 0.246593242
## [2,] 0.121887569 0.085490338 0.477924583 0.017294559
## [3,] 0.388925189 0.280674393 0.000360091 0.398583881
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11994714 0.2229803 0.7462006 0.3611510
## [2,] 0.01774922 0.4056445 0.3875911 0.1572696
## [3,] 0.58360343 0.2115341 0.7617249 0.1821037
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2892207 0.20744746 0.5926549 0.4565039
## [2,] 0.4283630 0.93353023 0.3826225 0.6835739
## [3,] 0.3631482 0.04557278 0.2886530 0.2760580
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6976951 0.25129236 0.9521164535 3.202110e-02
## [2,] 0.7100230 0.06596765 0.0007541956 1.288835e-05
## [3,] 0.4557407 0.04798216 0.6708462967 8.306556e-02
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7970919 0.92752181 0.6684875 0.26397430
## [2,] 0.6599012 0.09282777 0.3708904 0.06602617
## [3,] 0.2536444 0.19464892 0.9034256 0.46248050
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.74996268 0.19721841 0.96529092 0.39470809
## [2,] 0.13133607 0.09244109 0.02340787 0.80399145
## [3,] 0.09783906 0.33854410 0.35394301 0.01007057
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.11994714 0.22298033 0.74620059 0.36115100
## [2,] 0.01774922 0.40564445 0.38759107 0.15726963
## [3,] 0.58360343 0.21153407 0.76172493 0.18210369
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.28922069 0.20744746 0.59265490 0.45650393
## [2,] 0.42836303 0.93353023 0.38262252 0.68357391
## [3,] 0.36314824 0.04557278 0.28865301 0.27605796
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 6.976951e-01 2.512924e-01 9.521165e-01 3.202110e-02
## [2,] 7.100230e-01 6.596765e-02 7.541956e-04 1.288835e-05
## [3,] 4.557407e-01 4.798216e-02 6.708463e-01 8.306556e-02
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.79709187 0.92752181 0.66848752 0.26397430
## [2,] 0.65990115 0.09282777 0.37089042 0.06602617
## [3,] 0.25364440 0.19464892 0.90342563 0.46248050
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.74996268 0.19721841 0.96529092 0.39470809
## [2,] 0.13133607 0.09244109 0.02340787 0.80399145
## [3,] 0.09783906 0.33854410 0.35394301 0.01007057
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.07999750 0.06029387 0.1991780
## [2,] 0.06029387 0.04544331 0.1501199
## [3,] 0.19917804 0.15011989 0.4959142
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.07999750 0.06029387 0.19917804
## [2,] 0.06029387 0.04544331 0.15011989
## [3,] 0.19917804 0.15011989 0.49591415
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1826927 0.003758384 0.073882527 0.17198301
## [2,] 0.1209135 0.101263625 0.239427834 0.04554594
## [3,] 0.2159872 0.183483218 0.006572054 0.21865269
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07027743 0.001445759 0.028420805 0.06615767
## [2,] 0.04651246 0.038953646 0.092102047 0.01752041
## [3,] 0.08308500 0.070581517 0.002528109 0.08411035
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4029816 0.008290203 0.16296926 0.3793582
## [2,] 0.2667096 0.223366188 0.52812728 0.1004647
## [3,] 0.4764222 0.404725262 0.01449656 0.4823017
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2490919 0.005124359 0.100734925 0.23448975
## [2,] 0.1648591 0.138067606 0.326447212 0.06209949
## [3,] 0.2944871 0.250169681 0.008960648 0.29812139
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3359689 0.006911607 0.1358687 0.31627390
## [2,] 0.2223579 0.186222128 0.4403038 0.08375823
## [3,] 0.3971969 0.337422599 0.0120859 0.40209867
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2426143 0.004991102 0.09811535 0.22839192
## [2,] 0.1605720 0.134477205 0.31795806 0.06048461
## [3,] 0.2868291 0.243664104 0.00872763 0.29036885
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4556738 0.009374196 0.18427846 0.4289616
## [2,] 0.3015834 0.252572643 0.59718306 0.1136011
## [3,] 0.5387172 0.457645492 0.01639208 0.5453655
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3284076 0.006756054 0.13281088 0.30915585
## [2,] 0.2173535 0.182031017 0.43039435 0.08187317
## [3,] 0.3882576 0.329828572 0.01181389 0.39304904
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4603894 0.009471207 0.18618551 0.4334008
## [2,] 0.3047044 0.255186444 0.60336313 0.1147767
## [3,] 0.5442922 0.462381532 0.01656171 0.5510093
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3170083 0.006521547 0.12820094 0.2984249
## [2,] 0.2098090 0.175712608 0.41545510 0.0790313
## [3,] 0.3747809 0.318380020 0.01140383 0.3794061
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2091937 0.004303568 0.084599778 0.19693052
## [2,] 0.1384529 0.115952724 0.274158757 0.05215275
## [3,] 0.2473179 0.210098927 0.007525383 0.25037001
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2251053 0.004630902 0.091034532 0.21190927
## [2,] 0.1489838 0.124772217 0.295011578 0.05611954
## [3,] 0.2661291 0.226079285 0.008097772 0.26941343
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2836880 0.005836076 0.11472590 0.26705780
## [2,] 0.1877562 0.157243679 0.37178714 0.07072442
## [3,] 0.3353882 0.284915499 0.01020518 0.33952718
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3452489 0.007102516 0.13962165 0.32500989
## [2,] 0.2284997 0.191365880 0.45246571 0.08607177
## [3,] 0.4081681 0.346742748 0.01241973 0.41320527
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3178837 0.006539555 0.12855494 0.29924890
## [2,] 0.2103883 0.176197802 0.41660229 0.07924953
## [3,] 0.3758158 0.319259160 0.01143531 0.38045372
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2402594 0.004942655 0.097162986 0.22617503
## [2,] 0.1590134 0.133171895 0.314871788 0.05989752
## [3,] 0.2840450 0.241298966 0.008642914 0.28755037
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5096716 0.01048505 0.20611566 0.4797940
## [2,] 0.3373214 0.28250277 0.66794988 0.1270629
## [3,] 0.6025557 0.51187697 0.01833455 0.6099919
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11261054 0.002316642 0.045540685 0.10600915
## [2,] 0.07453023 0.062418207 0.147581683 0.02807421
## [3,] 0.13313303 0.113097803 0.004050969 0.13477602
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4060946 0.008354245 0.16422820 0.3822888
## [2,] 0.2687699 0.225091688 0.53220705 0.1012408
## [3,] 0.4801025 0.407851756 0.01460855 0.4860275
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3262958 0.006712611 0.13195688 0.30716791
## [2,] 0.2159558 0.180860522 0.42762683 0.08134671
## [3,] 0.3857610 0.327707711 0.01173793 0.39052166
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2834094 0.005830345 0.11461325 0.26679558
## [2,] 0.1875719 0.157089284 0.37142209 0.07065498
## [3,] 0.3350588 0.284635746 0.01019516 0.33919380
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3564089 0.007332102 0.14413487 0.3355157
## [2,] 0.2358859 0.197551702 0.46709148 0.0888540
## [3,] 0.4213619 0.357951062 0.01282119 0.4265620
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4361331 0.008972201 0.17637602 0.4105663
## [2,] 0.2886506 0.241741525 0.57157395 0.1087295
## [3,] 0.5156153 0.438020198 0.01568913 0.5219785
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2771574 0.005701727 0.112084853 0.26091000
## [2,] 0.1834340 0.153623854 0.363228423 0.06909631
## [3,] 0.3276674 0.278356609 0.009970256 0.33171110
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4406148 0.0090644 0.17818847 0.4147853
## [2,] 0.2916168 0.2442257 0.57744750 0.1098468
## [3,] 0.5209138 0.4425213 0.01585035 0.5273424
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4444905 0.009144131 0.17975584 0.4184338
## [2,] 0.2941819 0.246373920 0.58252679 0.1108131
## [3,] 0.5254958 0.446413802 0.01598978 0.5319810
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3561108 0.00732597 0.14401433 0.3352351
## [2,] 0.2356886 0.19738649 0.46670086 0.0887797
## [3,] 0.4210096 0.35765171 0.01281047 0.4262052
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2644332 0.005439962 0.106939077 0.24893171
## [2,] 0.1750126 0.146571037 0.346552735 0.06592413
## [3,] 0.3126243 0.265577355 0.009512525 0.31648236
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1354852 0.002787223 0.054791387 0.12754284
## [2,] 0.0896696 0.075097248 0.177560021 0.03377694
## [3,] 0.1601764 0.136071415 0.004873844 0.16215314
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11554899 0.002377093 0.046729021 0.10877535
## [2,] 0.07647502 0.064046944 0.151432670 0.02880677
## [3,] 0.13660699 0.116048970 0.004156675 0.13829286
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5147203 0.01058891 0.20815738 0.4845467
## [2,] 0.3406628 0.28530117 0.67456642 0.1283216
## [3,] 0.6085245 0.51694749 0.01851617 0.6160343
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01448666 0.0002980219 0.0058585316 0.013637431
## [2,] 0.00958786 0.0080297219 0.0189854840 0.003611576
## [3,] 0.01712675 0.0145493430 0.0005211325 0.017338114
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4320537 0.008888281 0.17472631 0.4067262
## [2,] 0.2859507 0.239480430 0.56622781 0.1077125
## [3,] 0.5107925 0.433923239 0.01554239 0.5170962
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09439400 0.001941889 0.038173757 0.08886049
## [2,] 0.06247379 0.052321072 0.123708004 0.02353276
## [3,] 0.11159665 0.094802439 0.003395661 0.11297386
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.001893760 3.895874e-05 7.658531e-04 0.0017827453
## [2,] 0.001253367 1.049681e-03 2.481866e-03 0.0004721211
## [3,] 0.002238885 1.901954e-03 6.812474e-05 0.0022665149
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1520326 0.00312764 0.06148332 0.14312025
## [2,] 0.1006214 0.08426923 0.19924626 0.03790227
## [3,] 0.1797395 0.15269046 0.00546911 0.18195767
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4709561 0.009688587 0.19045877 0.4433480
## [2,] 0.3116979 0.261043393 0.61721131 0.1174110
## [3,] 0.5567846 0.472993950 0.01694183 0.5636559
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4285147 0.008815474 0.17329508 0.4033946
## [2,] 0.2836084 0.237518783 0.56158969 0.1068302
## [3,] 0.5066085 0.430368860 0.01541507 0.5128606
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2656678 0.005465361 0.107438373 0.25009397
## [2,] 0.1758297 0.147255374 0.348170781 0.06623193
## [3,] 0.3140839 0.266817329 0.009556938 0.31796001
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5080288 0.01045125 0.20545128 0.4782474
## [2,] 0.3362341 0.28159217 0.66579687 0.1266534
## [3,] 0.6006135 0.51022703 0.01827546 0.6080257
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1607183 0.003306322 0.064995868 0.15129673
## [2,] 0.1063699 0.089083542 0.210629233 0.04006763
## [3,] 0.1900080 0.161413687 0.005781561 0.19235294
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2327299 0.004787759 0.094118020 0.2190870
## [2,] 0.1540301 0.128998456 0.305004102 0.0580204
## [3,] 0.2751434 0.233736960 0.008372056 0.2785389
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4312935 0.008872641 0.17441886 0.4060105
## [2,] 0.2854476 0.239059038 0.56523147 0.1075230
## [3,] 0.5098937 0.433159703 0.01551504 0.5161864
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3212544 0.006608898 0.12991808 0.30242201
## [2,] 0.2126192 0.178066132 0.42101977 0.08008986
## [3,] 0.3798008 0.322644454 0.01155657 0.38448790
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5013863 0.0103146 0.2027650 0.4719943
## [2,] 0.3318378 0.2779103 0.6570916 0.1249974
## [3,] 0.5927605 0.5035558 0.0180365 0.6000757
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2710236 0.005575542 0.109604305 0.25513581
## [2,] 0.1793744 0.150224007 0.355189821 0.06756714
## [3,] 0.3204158 0.272196303 0.009749604 0.32437001
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13554526 0.002788459 0.054815685 0.12759940
## [2,] 0.08970936 0.075130551 0.177638765 0.03379191
## [3,] 0.16024744 0.136131759 0.004876006 0.16222505
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3587344 0.007379942 0.14507531 0.33770485
## [2,] 0.2374250 0.198840675 0.47013913 0.08943375
## [3,] 0.4241112 0.360286599 0.01290485 0.42934517
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4568210 0.009397797 0.18474241 0.4300415
## [2,] 0.3023427 0.253208536 0.59868656 0.1138871
## [3,] 0.5400735 0.458797690 0.01643335 0.5467385
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1911694 0.003932767 0.077310555 0.1799627
## [2,] 0.1265236 0.105962091 0.250536894 0.0476592
## [3,] 0.2260086 0.191996539 0.006876986 0.2287978
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1649994 0.003394395 0.066727202 0.15532691
## [2,] 0.1092033 0.091456514 0.216239891 0.04113494
## [3,] 0.1950694 0.165713361 0.005935568 0.19747676
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2342610 0.004819256 0.094737194 0.2205283
## [2,] 0.1550435 0.129847098 0.307010632 0.0584021
## [3,] 0.2769534 0.235274645 0.008427134 0.2803713
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1603832 0.003299429 0.064860354 0.15098128
## [2,] 0.1061481 0.088897806 0.210190078 0.03998409
## [3,] 0.1896119 0.161077144 0.005769507 0.19195189
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3069261 0.006314135 0.12412361 0.28893371
## [2,] 0.2031362 0.170124218 0.40224190 0.07651778
## [3,] 0.3628613 0.308254214 0.01104114 0.36733938
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5182692 0.01066192 0.20959258 0.4878875
## [2,] 0.3430116 0.28726825 0.67921739 0.1292063
## [3,] 0.6127201 0.52051171 0.01864384 0.6202817
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08070626 0.001660302 0.032638315 0.07597514
## [2,] 0.05341468 0.044734177 0.105769542 0.02012036
## [3,] 0.09541441 0.081055471 0.002903268 0.09659192
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3138289 0.00645614 0.12691515 0.29543181
## [2,] 0.2077047 0.17395030 0.41128830 0.07823866
## [3,] 0.3710220 0.31518683 0.01128945 0.37560082
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3314090 0.0068178 0.13402469 0.31198133
## [2,] 0.2193399 0.1836947 0.43432787 0.08262144
## [3,] 0.3918060 0.3328430 0.01192186 0.39664125
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4729900 0.009730428 0.1912813 0.4452627
## [2,] 0.3130440 0.262170746 0.6198768 0.1179181
## [3,] 0.5591892 0.475036642 0.0170150 0.5660901
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05293625 0.001089013 0.02140788 0.04983306
## [2,] 0.03503536 0.029341712 0.06937558 0.01319720
## [3,] 0.06258352 0.053165307 0.00190429 0.06335586
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.182692708 0.003758384 0.073882527 0.171983007
## [2,] 0.120913463 0.101263625 0.239427834 0.045545942
## [3,] 0.215987187 0.183483218 0.006572054 0.218652686
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.070277427 0.001445759 0.028420805 0.066157667
## [2,] 0.046512459 0.038953646 0.092102047 0.017520413
## [3,] 0.083085001 0.070581517 0.002528109 0.084110354
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.402981563 0.008290203 0.162969264 0.379358222
## [2,] 0.266709585 0.223366188 0.528127281 0.100464738
## [3,] 0.476422159 0.404725262 0.014496564 0.482301689
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.33140898 0.00681780 0.13402469 0.31198133
## [2,] 0.21933994 0.18369466 0.43432787 0.08262144
## [3,] 0.39180597 0.33284299 0.01192186 0.39664125
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.472990017 0.009730428 0.191281294 0.445262683
## [2,] 0.313044027 0.262170746 0.619876824 0.117918095
## [3,] 0.559189167 0.475036642 0.017014997 0.566090126
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.052936252 0.001089013 0.021407883 0.049833056
## [2,] 0.035035364 0.029341712 0.069375578 0.013197196
## [3,] 0.062583517 0.053165307 0.001904290 0.063355861
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.200224
DelayedTensor::einsum('i->', darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 1.200224
einsum::einsum('ij->', arrC)
## [1] 4.516343
DelayedTensor::einsum('ij->', darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 4.516343
einsum::einsum('ijk->', arrE)
## [1] 33.57522
DelayedTensor::einsum('ijk->', darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 33.57522
einsum::einsum('ij->i', arrC)
## [1] 1.248266 1.464341 1.803737
DelayedTensor::einsum('ij->i', darrC)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 1.248266 1.464341 1.803737
einsum::einsum('ij->j', arrC)
## [1] 1.5002673 0.8330263 0.9236244 1.2594253
DelayedTensor::einsum('ij->j', darrC)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 1.5002673 0.8330263 0.9236244 1.2594253
einsum::einsum('ijk->i', arrE)
## [1] 13.321480 9.684909 10.568831
DelayedTensor::einsum('ijk->i', darrE)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 13.321480 9.684909 10.568831
einsum::einsum('ijk->j', arrE)
## [1] 9.141374 7.220280 10.214640 6.998925
DelayedTensor::einsum('ijk->j', darrE)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 9.141374 7.220280 10.214640 6.998925
einsum::einsum('ijk->k', arrE)
## [1] 6.595970 7.383559 5.623201 7.745625 6.226864
DelayedTensor::einsum('ijk->k', darrE)
## <5> array of class HDF5Array and type "double":
## [1] [2] [3] [4] [5]
## 6.595970 7.383559 5.623201 7.745625 6.226864
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 3.478212 2.836135 4.409537 2.597596
## [2,] 2.805095 2.468655 2.030600 2.380559
## [3,] 2.858067 1.915490 3.774503 2.020771
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 3.478212 2.836135 4.409537 2.597596
## [2,] 2.805095 2.468655 2.030600 2.380559
## [3,] 2.858067 1.915490 3.774503 2.020771
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.243499 1.794905 2.3529958 2.208774 1.541199
## [2,] 1.569038 1.635136 0.9771807 1.708946 1.329979
## [3,] 2.359166 1.925670 1.8222792 2.377106 1.730420
## [4,] 1.424267 2.027848 0.4707454 1.450799 1.625266
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.2434995 1.7949047 2.3529958 2.2087742 1.5411994
## [2,] 1.5690385 1.6351355 0.9771807 1.7089462 1.3299795
## [3,] 2.3591657 1.9256701 1.8222792 2.3771056 1.7304196
## [4,] 1.4242668 2.0278482 0.4707454 1.4507991 1.6252659
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.243499 1.794905 2.3529958 2.208774 1.541199
## [2,] 1.569038 1.635136 0.9771807 1.708946 1.329979
## [3,] 2.359166 1.925670 1.8222792 2.377106 1.730420
## [4,] 1.424267 2.027848 0.4707454 1.450799 1.625266
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.2434995 1.7949047 2.3529958 2.2087742 1.5411994
## [2,] 1.5690385 1.6351355 0.9771807 1.7089462 1.3299795
## [3,] 2.3591657 1.9256701 1.8222792 2.3771056 1.7304196
## [4,] 1.4242668 2.0278482 0.4707454 1.4507991 1.6252659
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.428014
DelayedTensor::einsum('ii->', darrB)
## <1> array of class HDF5Array and type "double":
## [1]
## 1.428014
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.2223454 0.2112130 0.71732980
## [2,] 0.4487998 0.7559259 0.09483399
## [3,] 0.6543766 0.3209936 0.44974286
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3]
## [1,] 0.22234537 0.21121295 0.71732980
## [2,] 0.44879979 0.75592589 0.09483399
## [3,] 0.65437664 0.32099365 0.44974286
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.8249277 0.6244408 0.2213509
## [2,] 0.5721727 0.3375885 0.4607428
## [3,] 0.4557934 0.1831242 0.1289510
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.65522620 0.3049551 0.6188671
## [2,] 0.21495209 0.5208168 0.9345842
## [3,] 0.08234009 0.2182057 0.1685677
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.02194149 0.4123791 0.03469557
## [2,] 0.57272283 0.6690331 0.41360785
## [3,] 0.32337268 0.7855111 0.82188508
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.8249277 0.6244408 0.2213509
## [2,] 0.5721727 0.3375885 0.4607428
## [3,] 0.4557934 0.1831242 0.1289510
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.65522620 0.30495508 0.61886713
## [2,] 0.21495209 0.52081684 0.93458415
## [3,] 0.08234009 0.21820575 0.16856775
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.02194149 0.41237913 0.03469557
## [2,] 0.57272283 0.66903306 0.41360785
## [3,] 0.32337268 0.78551110 0.82188508
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.621355
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 0.621355
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 2.341621
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 2.341621
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 22.89204
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 22.89204
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.7212998 1.080732 1.8634588 1.710637 0.9791378
## [2,] 0.8401588 1.186550 0.3652422 1.214998 0.6282036
## [3,] 1.8955166 1.263930 1.6237169 1.942804 1.3426418
## [4,] 0.7005243 1.416136 0.1150995 0.792481 1.2087701
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.7212998 1.0807320 1.8634588 1.7106374 0.9791378
## [2,] 0.8401588 1.1865505 0.3652422 1.2149985 0.6282036
## [3,] 1.8955166 1.2639304 1.6237169 1.9428036 1.3426418
## [4,] 0.7005243 1.4161358 0.1150995 0.7924810 1.2087701
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 0.5704807 0.4001201 0.6522789
## [2,] 0.4001201 0.7025970 0.4687749
## [3,] 0.6522789 0.4687749 1.0685436
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.5704807 0.4001201 0.6522789
## [2,] 0.4001201 0.7025970 0.4687749
## [3,] 0.6522789 0.4687749 1.0685436
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.278261115 0.12188757 0.388925189
## [2,] 0.000117764 0.08549034 0.280674393
## [3,] 0.045508610 0.47792458 0.000360091
## [4,] 0.246593242 0.01729456 0.398583881
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.278261115 0.121887569 0.388925189
## [2,] 0.000117764 0.085490338 0.280674393
## [3,] 0.045508610 0.477924583 0.000360091
## [4,] 0.246593242 0.017294559 0.398583881
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.1199471 0.2892207 0.6976951 0.7970919 0.7499627
## [2,] 0.2229803 0.2074475 0.2512924 0.9275218 0.1972184
## [3,] 0.7462006 0.5926549 0.9521165 0.6684875 0.9652909
## [4,] 0.3611510 0.4565039 0.0320211 0.2639743 0.3947081
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.01774922 0.4283630 7.100230e-01 0.65990115 0.13133607
## [2,] 0.40564445 0.9335302 6.596765e-02 0.09282777 0.09244109
## [3,] 0.38759107 0.3826225 7.541956e-04 0.37089042 0.02340787
## [4,] 0.15726963 0.6835739 1.288835e-05 0.06602617 0.80399145
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.5836034 0.36314824 0.45574071 0.2536444 0.09783906
## [2,] 0.2115341 0.04557278 0.04798216 0.1946489 0.33854410
## [3,] 0.7617249 0.28865301 0.67084630 0.9034256 0.35394301
## [4,] 0.1821037 0.27605796 0.08306556 0.4624805 0.01007057
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.1199471 0.2892207 0.6976951 0.7970919 0.7499627
## [2,] 0.2229803 0.2074475 0.2512924 0.9275218 0.1972184
## [3,] 0.7462006 0.5926549 0.9521165 0.6684875 0.9652909
## [4,] 0.3611510 0.4565039 0.0320211 0.2639743 0.3947081
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.774922e-02 4.283630e-01 7.100230e-01 6.599012e-01 1.313361e-01
## [2,] 4.056445e-01 9.335302e-01 6.596765e-02 9.282777e-02 9.244109e-02
## [3,] 3.875911e-01 3.826225e-01 7.541956e-04 3.708904e-01 2.340787e-02
## [4,] 1.572696e-01 6.835739e-01 1.288835e-05 6.602617e-02 8.039914e-01
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.58360343 0.36314824 0.45574071 0.25364440 0.09783906
## [2,] 0.21153407 0.04557278 0.04798216 0.19464892 0.33854410
## [3,] 0.76172493 0.28865301 0.67084630 0.90342563 0.35394301
## [4,] 0.18210369 0.27605796 0.08306556 0.46248050 0.01007057
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 2.283329 1.789269 2.523372
## [2,] 2.438748 3.066038 1.878772
## [3,] 2.491281 1.130523 2.001397
## [4,] 3.187275 1.982983 2.575368
## [5,] 2.920847 1.716096 1.589921
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 2.283329 1.789269 2.523372
## [2,] 2.438748 3.066038 1.878772
## [3,] 2.491281 1.130523 2.001397
## [4,] 3.187275 1.982983 2.575368
## [5,] 2.920847 1.716096 1.589921
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.440188e-03 2.276251e-02 5.491062e-02 6.273344e-02 5.902424e-02
## [2,] 7.427065e-06 6.909694e-06 8.370087e-06 3.089405e-05 6.568983e-06
## [3,] 9.604779e-03 7.628404e-03 1.225524e-02 8.604489e-03 1.242482e-02
## [4,] 2.518884e-02 3.183933e-02 2.233344e-03 1.841115e-02 2.752931e-02
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0004611833 0.011130287 1.844874e-02 0.0171464128 0.003412545
## [2,] 0.0073926058 0.017012980 1.202217e-03 0.0016917256 0.001684679
## [3,] 0.0394882696 0.038982068 7.683840e-05 0.0377867867 0.002384823
## [4,] 0.0005798148 0.002520171 4.751622e-08 0.0002434224 0.002964121
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.1598406202 9.946110e-02 0.1248208534 0.069469568 2.679672e-02
## [2,] 0.0418105962 9.007652e-03 0.0094838752 0.038473176 6.691466e-02
## [3,] 0.0001931585 7.319672e-05 0.0001701134 0.000229091 8.975298e-05
## [4,] 0.0511142195 7.748600e-02 0.0233154599 0.129812468 2.826681e-03
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.440188e-03 2.276251e-02 5.491062e-02 6.273344e-02 5.902424e-02
## [2,] 7.427065e-06 6.909694e-06 8.370087e-06 3.089405e-05 6.568983e-06
## [3,] 9.604779e-03 7.628404e-03 1.225524e-02 8.604489e-03 1.242482e-02
## [4,] 2.518884e-02 3.183933e-02 2.233344e-03 1.841115e-02 2.752931e-02
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 4.611833e-04 1.113029e-02 1.844874e-02 1.714641e-02 3.412545e-03
## [2,] 7.392606e-03 1.701298e-02 1.202217e-03 1.691726e-03 1.684679e-03
## [3,] 3.948827e-02 3.898207e-02 7.683840e-05 3.778679e-02 2.384823e-03
## [4,] 5.798148e-04 2.520171e-03 4.751622e-08 2.434224e-04 2.964121e-03
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.598406e-01 9.946110e-02 1.248209e-01 6.946957e-02 2.679672e-02
## [2,] 4.181060e-02 9.007652e-03 9.483875e-03 3.847318e-02 6.691466e-02
## [3,] 1.931585e-04 7.319672e-05 1.701134e-04 2.290910e-04 8.975298e-05
## [4,] 5.111422e-02 7.748600e-02 2.331546e-02 1.298125e-01 2.826681e-03
einsum
By using einsum
and other DelayedTensor functions,
it is possible to implement your original tensor calculation functions.
It is intended to be applied to Delayed Arrays,
which can scale to large-scale data
since the calculation is performed internally by block processing.
For example, kronecker
can be easily implmented by eimsum
and other DelayedTensor functions4 https://stackoverflow.com/
questions/56067643/speeding-up-kronecker-products-numpy
(the kronecker
function inside DelayedTensor
has a more efficient implementation though).
darr1 <- DelayedArray(array(1:6, dim=c(2,3)))
darr2 <- DelayedArray(array(20:1, dim=c(4,5)))
mykronecker <- function(darr1, darr2){
stopifnot((length(dim(darr1)) == 2) && (length(dim(darr2)) == 2))
# Outer Product
tmpdarr <- DelayedTensor::einsum('ij,kl->ikjl', darr1, darr2)
# Reshape
DelayedTensor::unfold(tmpdarr, row_idx=c(2,1), col_idx=c(4,3))
}
identical(as.array(DelayedTensor::kronecker(darr1, darr2)),
as.array(mykronecker(darr1, darr2)))
## [1] TRUE
## R version 4.2.0 RC (2022-04-19 r82224 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.4.0 HDF5Array_1.24.0
## [4] rhdf5_2.40.0 DelayedArray_0.22.0 IRanges_2.30.0
## [7] S4Vectors_0.34.0 MatrixGenerics_1.8.0 matrixStats_0.62.0
## [10] BiocGenerics_0.42.0 Matrix_1.4-1 DelayedTensor_1.2.0
## [13] BiocStyle_2.24.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.8.3 rTensor_1.4.8 bslib_0.3.1
## [4] compiler_4.2.0 BiocManager_1.30.17 jquerylib_0.1.4
## [7] rhdf5filters_1.8.0 tools_4.2.0 digest_0.6.29
## [10] jsonlite_1.8.0 evaluate_0.15 lattice_0.20-45
## [13] rlang_1.0.2 cli_3.3.0 parallel_4.2.0
## [16] yaml_2.3.5 xfun_0.30 fastmap_1.1.0
## [19] stringr_1.4.0 knitr_1.38 sass_0.4.1
## [22] grid_4.2.0 R6_2.5.1 BiocParallel_1.30.0
## [25] rmarkdown_2.14 bookdown_0.26 irlba_2.3.5
## [28] Rhdf5lib_1.18.0 magrittr_2.0.3 BiocSingular_1.12.0
## [31] htmltools_0.5.2 rsvd_1.0.5 beachmat_2.12.0
## [34] dqrng_0.3.0 ScaledMatrix_1.4.0 stringi_1.7.6