DelayedTensor 1.12.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2024-10-29 16:15:12
Compiled: Fri Nov 8 16:50:22 2024
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.7595245 0.9131908 0.4381403
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.7595245 0.9131908 0.4381403
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.36716605 0.02141181 0.76098986 0.4923379
## [2,] 0.23284744 0.98299734 0.96602953 0.2572259
## [3,] 0.02409156 0.83104364 0.08987805 0.2001718
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.36716605 0.02141181 0.76098986 0.49233788
## [2,] 0.23284744 0.98299734 0.96602953 0.25722593
## [3,] 0.02409156 0.83104364 0.08987805 0.20017178
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.70434447 0.4730858 0.9320839 0.4080340
## [2,] 0.34175987 0.4692977 0.4819798 0.4418730
## [3,] 0.08967696 0.7141584 0.8729345 0.3739515
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2746617 0.1977177 0.3015558 0.3446547
## [2,] 0.9653231 0.8384621 0.8699850 0.1736321
## [3,] 0.5229048 0.1744929 0.7606380 0.9783620
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8884191 0.42574389 0.9268852 0.0114618
## [2,] 0.4734582 0.95494436 0.2706325 0.6771680
## [3,] 0.2858745 0.05888216 0.9296019 0.5420614
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1710093 0.8362463 0.1531915 0.26598692
## [2,] 0.9354493 0.9513668 0.6210315 0.63601475
## [3,] 0.1529261 0.2124236 0.1097437 0.05725197
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7275467 0.2266579 0.1580540 0.6728964
## [2,] 0.2404158 0.8099618 0.1616461 0.9503038
## [3,] 0.5317472 0.1943695 0.3930701 0.9030736
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.70434447 0.47308585 0.93208387 0.40803398
## [2,] 0.34175987 0.46929769 0.48197979 0.44187301
## [3,] 0.08967696 0.71415841 0.87293453 0.37395149
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.2746617 0.1977177 0.3015558 0.3446547
## [2,] 0.9653231 0.8384621 0.8699850 0.1736321
## [3,] 0.5229048 0.1744929 0.7606380 0.9783620
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.88841912 0.42574389 0.92688522 0.01146180
## [2,] 0.47345820 0.95494436 0.27063246 0.67716801
## [3,] 0.28587448 0.05888216 0.92960188 0.54206139
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.17100932 0.83624628 0.15319155 0.26598692
## [2,] 0.93544934 0.95136684 0.62103152 0.63601475
## [3,] 0.15292609 0.21242357 0.10974369 0.05725197
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.7275467 0.2266579 0.1580540 0.6728964
## [2,] 0.2404158 0.8099618 0.1616461 0.9503038
## [3,] 0.5317472 0.1943695 0.3930701 0.9030736
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.7670114 0.5698553 0.6343976
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.7670114 0.5698553 0.6343976
einsum::einsum('iii->i', arrD)
## [1] 0.3269270 0.4945862 0.1157621
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.3269270 0.4945862 0.1157621
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.5768775 0.8339174 0.1919669
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.5768775 0.8339174 0.1919669
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.1348109057 0.0004584658 0.579105563 0.24239659
## [2,] 0.0542179316 0.9662837739 0.933213045 0.06616518
## [3,] 0.0005804033 0.6906335276 0.008078064 0.04006874
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.1348109057 0.0004584658 0.5791055634 0.2423965894
## [2,] 0.0542179316 0.9662837739 0.9332130446 0.0661651797
## [3,] 0.0005804033 0.6906335276 0.0080780639 0.0400687421
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.496101129 0.2238102 0.8687803 0.1664917
## [2,] 0.116799810 0.2202403 0.2323045 0.1952518
## [3,] 0.008041958 0.5100222 0.7620147 0.1398397
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07543906 0.03909227 0.09093589 0.11878683
## [2,] 0.93184876 0.70301875 0.75687394 0.03014809
## [3,] 0.27342939 0.03044779 0.57857014 0.95719226
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.78928854 0.181257859 0.85911621 0.0001313729
## [2,] 0.22416266 0.911918724 0.07324193 0.4585565189
## [3,] 0.08172422 0.003467108 0.86415966 0.2938305456
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02924419 0.69930783 0.02346765 0.070749040
## [2,] 0.87506547 0.90509886 0.38568014 0.404514767
## [3,] 0.02338639 0.04512377 0.01204368 0.003277788
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.52932413 0.05137382 0.02498107 0.4527895
## [2,] 0.05779976 0.65603804 0.02612946 0.9030773
## [3,] 0.28275504 0.03777952 0.15450407 0.8155418
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.496101129 0.223810218 0.868780333 0.166491728
## [2,] 0.116799810 0.220240320 0.232304521 0.195251756
## [3,] 0.008041958 0.510022233 0.762014697 0.139839715
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.07543906 0.03909227 0.09093589 0.11878683
## [2,] 0.93184876 0.70301875 0.75687394 0.03014809
## [3,] 0.27342939 0.03044779 0.57857014 0.95719226
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.7892885363 0.1812578595 0.8591162119 0.0001313729
## [2,] 0.2241626638 0.9119187242 0.0732419283 0.4585565189
## [3,] 0.0817242205 0.0034671084 0.8641596623 0.2938305456
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.029244186 0.699307834 0.023467650 0.070749040
## [2,] 0.875065468 0.905098858 0.385680144 0.404514767
## [3,] 0.023386390 0.045123772 0.012043677 0.003277788
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.52932413 0.05137382 0.02498107 0.45278950
## [2,] 0.05779976 0.65603804 0.02612946 0.90307735
## [3,] 0.28275504 0.03777952 0.15450407 0.81554184
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.5768775 0.6935908 0.3327783
## [2,] 0.6935908 0.8339174 0.4001057
## [3,] 0.3327783 0.4001057 0.1919669
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.5768775 0.6935908 0.3327783
## [2,] 0.6935908 0.8339174 0.4001057
## [3,] 0.3327783 0.4001057 0.1919669
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.25861137 0.01508129 0.53599900 0.3467755
## [2,] 0.16400481 0.69236874 0.68041755 0.1811757
## [3,] 0.01696876 0.58534099 0.06330511 0.1409899
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.125482621 0.007317699 0.26007580 0.16826133
## [2,] 0.079577912 0.335949046 0.33015013 0.08790950
## [3,] 0.008233529 0.284017367 0.03071671 0.06841068
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.032926336 0.001920146 0.068243259 0.04415137
## [2,] 0.020881052 0.088152216 0.086630594 0.02306724
## [3,] 0.002160458 0.074525469 0.008059991 0.01795080
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.17370106 0.01012963 0.36001353 0.23291808
## [2,] 0.11015683 0.46504213 0.45701490 0.12168995
## [3,] 0.01139738 0.39315498 0.04252003 0.09469844
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.17231018 0.01004852 0.35713078 0.23105303
## [2,] 0.10927477 0.46131838 0.45335542 0.12071553
## [3,] 0.01130611 0.39000686 0.04217956 0.09394015
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.26221472 0.01529143 0.54346731 0.3516072
## [2,] 0.16628996 0.70201582 0.68989811 0.1837001
## [3,] 0.01720519 0.59349680 0.06418717 0.1429544
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.34222955 0.01995761 0.70930637 0.4589002
## [2,] 0.21703334 0.91623596 0.90042053 0.2397561
## [3,] 0.02245536 0.77460237 0.08377388 0.1865769
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.17696661 0.01032006 0.3667817 0.23729691
## [2,] 0.11222776 0.47378486 0.4656067 0.12397770
## [3,] 0.01161165 0.40054624 0.0433194 0.09647875
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.32051192 0.01869111 0.66429433 0.4297787
## [2,] 0.20326057 0.85809232 0.84328053 0.2245414
## [3,] 0.02103036 0.72544669 0.07845765 0.1747369
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.149816223 0.008736748 0.3105097 0.20089058
## [2,] 0.095009669 0.401096317 0.3941729 0.10495692
## [3,] 0.009830176 0.339094042 0.0366733 0.08167689
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.16224077 0.009461303 0.33626088 0.21755082
## [2,] 0.10288900 0.434359993 0.42686237 0.11366120
## [3,] 0.01064541 0.367215752 0.03971468 0.08845051
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.137302289 0.00800698 0.28457329 0.18411048
## [2,] 0.087073648 0.36759332 0.36124818 0.09619002
## [3,] 0.009009075 0.31077000 0.03361003 0.07485454
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10084646 0.005881006 0.20901478 0.13522637
## [2,] 0.06395428 0.269991735 0.26533133 0.07065012
## [3,] 0.00661703 0.228255870 0.02468606 0.05497952
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.35443388 0.02066932 0.73460112 0.4752651
## [2,] 0.22477302 0.94891008 0.93253065 0.2483061
## [3,] 0.02325614 0.80222565 0.08676136 0.1932305
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.19199287 0.01119634 0.39792522 0.2574458
## [2,] 0.12175704 0.51401399 0.50514144 0.1345047
## [3,] 0.01259759 0.43455667 0.04699766 0.1046708
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.072595211 0.004233494 0.15046113 0.09734389
## [2,] 0.046038051 0.194355933 0.19100110 0.05085811
## [3,] 0.004763327 0.164312002 0.01777048 0.03957750
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.30785483 0.0179530 0.63806118 0.4128067
## [2,] 0.19523376 0.8242060 0.80997918 0.2156742
## [3,] 0.02019986 0.6967986 0.07535934 0.1678365
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.064067884 0.003736211 0.13278736 0.08590949
## [2,] 0.040630236 0.171526100 0.16856534 0.04488411
## [3,] 0.004203807 0.145011251 0.01568309 0.03492856
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.110721041 0.006456856 0.22948089 0.14846733
## [2,] 0.070216491 0.296428525 0.29131178 0.07756796
## [3,] 0.007264949 0.250606008 0.02710325 0.06036296
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3194290 0.01862796 0.66204978 0.4283266
## [2,] 0.2025738 0.85519296 0.84043122 0.2237827
## [3,] 0.0209593 0.72299552 0.07819256 0.1741465
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.27928044 0.01628664 0.57883779 0.3744909
## [2,] 0.17711261 0.74770511 0.73479875 0.1956558
## [3,] 0.01832496 0.63212335 0.06836466 0.1522583
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.126545486 0.007379681 0.26227869 0.16968654
## [2,] 0.080251954 0.338794606 0.33294657 0.08865411
## [3,] 0.008303269 0.286423055 0.03097689 0.06899014
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.063751797 0.003717777 0.13213224 0.08548564
## [2,] 0.040429781 0.170679853 0.16773370 0.04466267
## [3,] 0.004183067 0.144295818 0.01560571 0.03475624
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.35922132 0.02094851 0.74452358 0.4816847
## [2,] 0.22780910 0.96172727 0.94512661 0.2516601
## [3,] 0.02357027 0.81306154 0.08793327 0.1958405
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3261973 0.01902267 0.67607794 0.4374024
## [2,] 0.2068661 0.87331364 0.85823910 0.2285244
## [3,] 0.0214034 0.73831506 0.07984938 0.1778364
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.17383777 0.0101376 0.3602969 0.23310141
## [2,] 0.11024353 0.4654081 0.4573746 0.12178573
## [3,] 0.01140635 0.3934644 0.0425535 0.09477297
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.104963404 0.006121091 0.21754758 0.14074684
## [2,] 0.066565143 0.281013858 0.27616319 0.07353433
## [3,] 0.006887163 0.237574171 0.02569384 0.05722400
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.15631870 0.009115949 0.32398678 0.20960984
## [2,] 0.09913338 0.418505112 0.41128117 0.10951237
## [3,] 0.01025684 0.353811751 0.03826503 0.08522191
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3506231 0.02044709 0.72670297 0.4701553
## [2,] 0.2223564 0.93870776 0.92250444 0.2456365
## [3,] 0.0230061 0.79360043 0.08582854 0.1911529
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.021619529 0.001260774 0.044808724 0.02898992
## [2,] 0.013710560 0.057881003 0.056881902 0.01514602
## [3,] 0.001418563 0.048933642 0.005292213 0.01178655
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.34032078 0.01984629 0.70535025 0.4563407
## [2,] 0.21582285 0.91112571 0.89539849 0.2384189
## [3,] 0.02233011 0.77028207 0.08330664 0.1855363
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.099367050 0.005794732 0.20594856 0.13324261
## [2,] 0.063016076 0.266030989 0.26143895 0.06961369
## [3,] 0.006519959 0.224907384 0.02432392 0.05417298
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.34131825 0.01990446 0.7074176 0.4576782
## [2,] 0.21645542 0.91379618 0.8980229 0.2391177
## [3,] 0.02239556 0.77253973 0.0835508 0.1860801
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0042083849 0.000245418 0.008722316 0.005643080
## [2,] 0.0026688515 0.011266922 0.011072440 0.002948273
## [3,] 0.0002761327 0.009525258 0.001030165 0.002294330
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.24863310 0.0144994 0.51531799 0.3333955
## [2,] 0.15767684 0.6656544 0.65416430 0.1741852
## [3,] 0.01631403 0.5627562 0.06086254 0.1355499
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.19902654 0.01160652 0.41250322 0.2668774
## [2,] 0.12621761 0.53284490 0.52364730 0.1394322
## [3,] 0.01305911 0.45047667 0.04871942 0.1085054
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.062788815 0.00366162 0.13013636 0.08419436
## [2,] 0.039819082 0.16810170 0.16520005 0.04398803
## [3,] 0.004119881 0.14211620 0.01536998 0.03423124
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.34346524 0.02002967 0.71186746 0.4605571
## [2,] 0.21781699 0.91954421 0.90367168 0.2406218
## [3,] 0.02253644 0.77739922 0.08407636 0.1872506
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.056149269 0.003274425 0.1163752 0.07529131
## [2,] 0.035608450 0.150325944 0.1477311 0.03933656
## [3,] 0.003684228 0.127088258 0.0137447 0.03061149
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.30704124 0.01790555 0.63637493 0.4117157
## [2,] 0.19471781 0.82202787 0.80783859 0.2151042
## [3,] 0.02014648 0.69495715 0.07516018 0.1673929
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.34930960 0.02037049 0.7239805 0.4683939
## [2,] 0.22152334 0.93519107 0.9190485 0.2447162
## [3,] 0.02291991 0.79062736 0.0855070 0.1904368
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.077994721 0.004548374 0.16165218 0.10458417
## [2,] 0.049462284 0.208811801 0.20520744 0.05464085
## [3,] 0.005117615 0.176533254 0.01909222 0.04252120
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.056246734 0.003280109 0.11657721 0.07542200
## [2,] 0.035670260 0.150586883 0.14798756 0.03940484
## [3,] 0.003690624 0.127308860 0.01376856 0.03066462
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.22802169 0.01329741 0.4725987 0.3057573
## [2,] 0.14460560 0.61047233 0.5999348 0.1597454
## [3,] 0.01496162 0.51610429 0.0558171 0.1243130
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.040294155 0.002349811 0.083513832 0.05403097
## [2,] 0.025553537 0.107877752 0.106015641 0.02822892
## [3,] 0.002643897 0.091201792 0.009863549 0.02196759
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09766136 0.005695263 0.20241335 0.13095543
## [2,] 0.06193437 0.261464432 0.25695121 0.06841873
## [3,] 0.00640804 0.221046735 0.02390639 0.05324307
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.23352302 0.01361823 0.48400078 0.3131342
## [2,] 0.14809441 0.62520081 0.61440903 0.1635995
## [3,] 0.01532259 0.52855601 0.05716377 0.1273122
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.021020980 0.001225869 0.043568170 0.02818731
## [2,] 0.013330975 0.056278536 0.055307095 0.01472669
## [3,] 0.001379289 0.047578887 0.005145696 0.01146023
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.26713043 0.01557809 0.55365562 0.3581988
## [2,] 0.16940738 0.71517643 0.70283155 0.1871439
## [3,] 0.01752773 0.60462302 0.06539047 0.1456343
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.088272524 0.005147739 0.1829540 0.11836581
## [2,] 0.055980208 0.236328107 0.2322488 0.06184118
## [3,] 0.005791992 0.199796033 0.0216081 0.04812446
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.19523950 0.01138567 0.4046542 0.2617993
## [2,] 0.12381597 0.52270604 0.5136835 0.1367792
## [3,] 0.01281062 0.44190509 0.0477924 0.1064408
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.083221098 0.004853158 0.17248439 0.11159229
## [2,] 0.052776721 0.222804148 0.21895826 0.05830230
## [3,] 0.005460544 0.188362635 0.02037157 0.04537052
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.29739045 0.01734275 0.61637268 0.3987749
## [2,] 0.18859752 0.79619025 0.78244697 0.2083432
## [3,] 0.01951324 0.67311356 0.07279778 0.1621315
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.071365895 0.004161805 0.14791325 0.09569549
## [2,] 0.045258450 0.191064740 0.18776671 0.04999689
## [3,] 0.004682666 0.161529569 0.01746956 0.03890730
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.058032064 0.003384223 0.12027749 0.07781597
## [2,] 0.036802470 0.155366665 0.15268483 0.04065559
## [3,] 0.003807768 0.131349774 0.01420559 0.03163795
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.059350962 0.003461136 0.12301105 0.07958450
## [2,] 0.037638882 0.158897693 0.15615491 0.04157957
## [3,] 0.003894307 0.134334968 0.01452844 0.03235699
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.144321978 0.008416343 0.29912233 0.19352328
## [2,] 0.091525357 0.386386820 0.37971728 0.10110781
## [3,] 0.009469671 0.326658369 0.03532837 0.07868153
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.24706469 0.01440793 0.51206730 0.3312924
## [2,] 0.15668219 0.66145532 0.65003774 0.1730864
## [3,] 0.01621112 0.55920623 0.06047861 0.1346949
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3489193 0.02034773 0.72317157 0.4678706
## [2,] 0.2212758 0.93414613 0.91802155 0.2444428
## [3,] 0.0228943 0.78974394 0.08541145 0.1902240
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.33157795 0.01933644 0.68722981 0.4446173
## [2,] 0.21027837 0.88771890 0.87239572 0.2322939
## [3,] 0.02175645 0.75049353 0.08116649 0.1807698
DelayedTensor::einsum('ij,klm->ijklm', darrC, darrE)
## <3 x 4 x 3 x 4 x 5> HDF5Array object of type "double":
## ,,1,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.25861137 0.01508129 0.53599900 0.34677546
## [2,] 0.16400481 0.69236874 0.68041755 0.18117566
## [3,] 0.01696876 0.58534099 0.06330511 0.14098989
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.125482621 0.007317699 0.260075796 0.168261331
## [2,] 0.079577912 0.335949046 0.330150127 0.087909501
## [3,] 0.008233529 0.284017367 0.030716711 0.068410682
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.032926336 0.001920146 0.068243259 0.044151366
## [2,] 0.020881052 0.088152216 0.086630594 0.023067240
## [3,] 0.002160458 0.074525469 0.008059991 0.017950797
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.24706469 0.01440793 0.51206730 0.33129236
## [2,] 0.15668219 0.66145532 0.65003774 0.17308639
## [3,] 0.01621112 0.55920623 0.06047861 0.13469486
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.34891930 0.02034773 0.72317157 0.46787057
## [2,] 0.22127581 0.93414613 0.91802155 0.24444278
## [3,] 0.02289430 0.78974394 0.08541145 0.19022401
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.33157795 0.01933644 0.68722981 0.44461732
## [2,] 0.21027837 0.88771890 0.87239572 0.23229394
## [3,] 0.02175645 0.75049353 0.08116649 0.18076984
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.110856
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 2.110856
einsum::einsum('ij->', arrC)
## [1] 5.226191
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 5.226191
einsum::einsum('ijk->', arrE)
## [1] 30.22309
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 30.22309
einsum::einsum('ij->i', arrC)
## [1] 1.641906 2.439100 1.145185
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 1.641906 2.439100 1.145185
einsum::einsum('ij->j', arrC)
## [1] 0.6241051 1.8354528 1.8168974 0.9497356
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 0.6241051 1.8354528 1.8168974 0.9497356
einsum::einsum('ijk->i', arrE)
## [1] 9.100237 12.264706 8.858145
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 9.100237 12.264706 8.858145
einsum::einsum('ijk->j', arrE)
## [1] 7.305517 7.537811 7.943033 7.436726
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 7.305517 7.537811 7.943033 7.436726
einsum::einsum('ijk->k', arrE)
## [1] 6.303180 6.402390 6.445133 5.102642 5.969743
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 6.303180 6.402390 6.445133 5.102642 5.969743
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 2.765981 2.159452 2.471770 1.703034
## [2,] 2.956406 4.024033 2.405275 2.878992
## [3,] 1.583129 1.354327 3.065988 2.854700
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 2.765981 2.159452 2.471770 1.703034
## [2,] 2.956406 4.024033 2.405275 2.878992
## [3,] 1.583129 1.354327 3.065988 2.854700
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.135781 1.762890 1.647752 1.2593848 1.4997096
## [2,] 1.656542 1.210673 1.439570 2.0000367 1.2309892
## [3,] 2.286998 1.932179 2.127120 0.8839667 0.7127702
## [4,] 1.223858 1.496649 1.230691 0.9592536 2.5262737
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.1357813 1.7628896 1.6477518 1.2593848 1.4997096
## [2,] 1.6565419 1.2106727 1.4395704 2.0000367 1.2309892
## [3,] 2.2869982 1.9321788 2.1271196 0.8839667 0.7127702
## [4,] 1.2238585 1.4966487 1.2306912 0.9592536 2.5262737
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.135781 1.762890 1.647752 1.2593848 1.4997096
## [2,] 1.656542 1.210673 1.439570 2.0000367 1.2309892
## [3,] 2.286998 1.932179 2.127120 0.8839667 0.7127702
## [4,] 1.223858 1.496649 1.230691 0.9592536 2.5262737
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.1357813 1.7628896 1.6477518 1.2593848 1.4997096
## [2,] 1.6565419 1.2106727 1.4395704 2.0000367 1.2309892
## [3,] 2.2869982 1.9321788 2.1271196 0.8839667 0.7127702
## [4,] 1.2238585 1.4966487 1.2306912 0.9592536 2.5262737
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.971264
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 1.971264
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.76701142 0.5364627 0.1052038
## [2,] 0.43659262 0.5698553 0.3701847
## [3,] 0.06580175 0.3043300 0.6343976
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.76701142 0.53646274 0.10520377
## [2,] 0.43659262 0.56985527 0.37018466
## [3,] 0.06580175 0.30432999 0.63439757
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.3269270 0.1325547 0.2742584
## [2,] 0.6199303 0.5628470 0.2273385
## [3,] 0.3228906 0.9886836 0.0597285
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.4939754 0.8039704 0.2574337
## [2,] 0.5760463 0.4945862 0.3040233
## [3,] 0.3520776 0.0975893 0.4433360
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.14160305 0.34706519 0.6288018
## [2,] 0.08039177 0.43915438 0.6145238
## [3,] 0.61608012 0.02320466 0.1157621
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.3269270 0.1325547 0.2742584
## [2,] 0.6199303 0.5628470 0.2273385
## [3,] 0.3228906 0.9886836 0.0597285
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.4939754 0.8039704 0.2574337
## [2,] 0.5760463 0.4945862 0.3040233
## [3,] 0.3520776 0.0975893 0.4433360
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.14160305 0.34706519 0.62880179
## [2,] 0.08039177 0.43915438 0.61452376
## [3,] 0.61608012 0.02320466 0.11576208
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.602762
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.602762
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 3.716012
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 3.716012
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 20.73539
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 20.73539
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.6209429 1.2807172 1.0951754 0.9276960 0.8698789
## [2,] 0.9540728 0.7725588 1.0966437 1.6495305 0.7451914
## [3,] 1.8630996 1.4263800 1.7965178 0.4211915 0.2056146
## [4,] 0.5015832 1.1061272 0.7525184 0.4785416 2.1714087
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.6209429 1.2807172 1.0951754 0.9276960 0.8698789
## [2,] 0.9540728 0.7725588 1.0966437 1.6495305 0.7451914
## [3,] 1.8630996 1.4263800 1.7965178 0.4211915 0.2056146
## [4,] 0.5015832 1.1061272 0.7525184 0.4785416 2.1714087
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 0.9567715 0.9683222 0.1935882
## [2,] 0.9683222 2.0198799 0.9608376
## [3,] 0.1935882 0.9608376 0.7393607
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.9567715 0.9683222 0.1935882
## [2,] 0.9683222 2.0198799 0.9608376
## [3,] 0.1935882 0.9608376 0.7393607
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.1348109057 0.05421793 0.0005804033
## [2,] 0.0004584658 0.96628377 0.6906335276
## [3,] 0.5791055634 0.93321304 0.0080780639
## [4,] 0.2423965894 0.06616518 0.0400687421
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.1348109057 0.0542179316 0.0005804033
## [2,] 0.0004584658 0.9662837739 0.6906335276
## [3,] 0.5791055634 0.9332130446 0.0080780639
## [4,] 0.2423965894 0.0661651797 0.0400687421
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.4961011 0.07543906 0.7892885363 0.02924419 0.52932413
## [2,] 0.2238102 0.03909227 0.1812578595 0.69930783 0.05137382
## [3,] 0.8687803 0.09093589 0.8591162119 0.02346765 0.02498107
## [4,] 0.1664917 0.11878683 0.0001313729 0.07074904 0.45278950
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.1167998 0.93184876 0.22416266 0.8750655 0.05779976
## [2,] 0.2202403 0.70301875 0.91191872 0.9050989 0.65603804
## [3,] 0.2323045 0.75687394 0.07324193 0.3856801 0.02612946
## [4,] 0.1952518 0.03014809 0.45855652 0.4045148 0.90307735
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.008041958 0.27342939 0.081724221 0.023386390 0.28275504
## [2,] 0.510022233 0.03044779 0.003467108 0.045123772 0.03777952
## [3,] 0.762014697 0.57857014 0.864159662 0.012043677 0.15450407
## [4,] 0.139839715 0.95719226 0.293830546 0.003277788 0.81554184
DelayedTensor::einsum('ijk,ijk->jki', darrE, darrE)
## <4 x 5 x 3> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.4961011292 0.0754390570 0.7892885363 0.0292441862 0.5293241333
## [2,] 0.2238102184 0.0390922725 0.1812578595 0.6993078342 0.0513738198
## [3,] 0.8687803330 0.0909358851 0.8591162119 0.0234676499 0.0249810679
## [4,] 0.1664917279 0.1187868285 0.0001313729 0.0707490397 0.4527894988
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.11679981 0.93184876 0.22416266 0.87506547 0.05779976
## [2,] 0.22024032 0.70301875 0.91191872 0.90509886 0.65603804
## [3,] 0.23230452 0.75687394 0.07324193 0.38568014 0.02612946
## [4,] 0.19525176 0.03014809 0.45855652 0.40451477 0.90307735
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.008041958 0.273429386 0.081724221 0.023386390 0.282755036
## [2,] 0.510022233 0.030447787 0.003467108 0.045123772 0.037779518
## [3,] 0.762014697 0.578570138 0.864159662 0.012043677 0.154504069
## [4,] 0.139839715 0.957192257 0.293830546 0.003277788 0.815541843
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 2.517548 1.734910 2.0507214
## [2,] 1.118590 2.847402 2.4363977
## [3,] 2.252510 2.376203 1.8164199
## [4,] 1.426434 3.143862 0.5323453
## [5,] 1.785155 2.162327 2.0222603
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 2.5175482 1.7349104 2.0507214
## [2,] 1.1185898 2.8474023 2.4363977
## [3,] 2.2525100 2.3762030 1.8164199
## [4,] 1.4264341 3.1438624 0.5323453
## [5,] 1.7851549 2.1623275 2.0222603
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.0507968785 7.724370e-03 8.081698e-02 0.0029943761 5.419865e-02
## [2,] 0.0000779343 1.361255e-05 6.311689e-05 0.0002435102 1.788919e-05
## [3,] 0.3821285631 3.999768e-02 3.778778e-01 0.0103221252 1.098779e-02
## [4,] 0.0306521505 2.186939e-02 2.418656e-05 0.0130253331 8.336133e-02
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.005782912 0.046137058 0.01109859 0.04332564 0.002861742
## [2,] 0.194340377 0.620344759 0.80467840 0.79866054 0.578888914
## [3,] 0.197970275 0.645009150 0.06241689 0.32867722 0.022267570
## [4,] 0.011797391 0.001821591 0.02770664 0.02444136 0.054565227
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.045054e-06 6.953256e-05 2.078231e-05 5.947114e-06 7.190406e-05
## [2,] 1.543298e-01 9.213329e-03 1.049127e-03 1.365420e-02 1.143187e-02
## [3,] 2.697018e-03 2.047748e-03 3.058542e-03 4.262649e-05 5.468401e-04
## [4,] 2.454988e-03 1.680421e-02 5.158409e-03 5.754396e-05 1.431743e-02
DelayedTensor::einsum('i,ij,ijk,ijk,ji->jki',
darrA, darrC, darrE, darrE, t(darrC))
## <4 x 5 x 3> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 5.079688e-02 7.724370e-03 8.081698e-02 2.994376e-03 5.419865e-02
## [2,] 7.793430e-05 1.361255e-05 6.311689e-05 2.435102e-04 1.788919e-05
## [3,] 3.821286e-01 3.999768e-02 3.778778e-01 1.032213e-02 1.098779e-02
## [4,] 3.065215e-02 2.186939e-02 2.418656e-05 1.302533e-02 8.336133e-02
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.005782912 0.046137058 0.011098588 0.043325643 0.002861742
## [2,] 0.194340377 0.620344759 0.804678401 0.798660541 0.578888914
## [3,] 0.197970275 0.645009150 0.062416886 0.328677221 0.022267570
## [4,] 0.011797391 0.001821591 0.027706642 0.024441362 0.054565227
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.045054e-06 6.953256e-05 2.078231e-05 5.947114e-06 7.190406e-05
## [2,] 1.543298e-01 9.213329e-03 1.049127e-03 1.365420e-02 1.143187e-02
## [3,] 2.697018e-03 2.047748e-03 3.058542e-03 4.262649e-05 5.468401e-04
## [4,] 2.454988e-03 1.680421e-02 5.158409e-03 5.754396e-05 1.431743e-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.4.1 (2024-06-14)
## Platform: aarch64-apple-darwin20
## Running under: macOS Ventura 13.6.7
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0
##
## locale:
## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## time zone: America/New_York
## tzcode source: internal
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] einsum_0.1.2 DelayedRandomArray_1.14.0
## [3] HDF5Array_1.34.0 rhdf5_2.50.0
## [5] DelayedArray_0.32.0 SparseArray_1.6.0
## [7] S4Arrays_1.6.0 abind_1.4-8
## [9] IRanges_2.40.0 S4Vectors_0.44.0
## [11] MatrixGenerics_1.18.0 matrixStats_1.4.1
## [13] BiocGenerics_0.52.0 Matrix_1.7-1
## [15] DelayedTensor_1.12.0 BiocStyle_2.34.0
##
## loaded via a namespace (and not attached):
## [1] jsonlite_1.8.9 compiler_4.4.1 BiocManager_1.30.25
## [4] crayon_1.5.3 rsvd_1.0.5 Rcpp_1.0.13-1
## [7] rhdf5filters_1.18.0 parallel_4.4.1 jquerylib_0.1.4
## [10] BiocParallel_1.40.0 yaml_2.3.10 fastmap_1.2.0
## [13] lattice_0.22-6 R6_2.5.1 XVector_0.46.0
## [16] ScaledMatrix_1.14.0 knitr_1.49 bookdown_0.41
## [19] bslib_0.8.0 rlang_1.1.4 cachem_1.1.0
## [22] xfun_0.49 sass_0.4.9 cli_3.6.3
## [25] Rhdf5lib_1.28.0 BiocSingular_1.22.0 zlibbioc_1.52.0
## [28] digest_0.6.37 grid_4.4.1 irlba_2.3.5.1
## [31] rTensor_1.4.8 dqrng_0.4.1 lifecycle_1.0.4
## [34] evaluate_1.0.1 codetools_0.2-20 beachmat_2.22.0
## [37] rmarkdown_2.29 tools_4.4.1 htmltools_0.5.8.1