DelayedTensor 1.9.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2024-04-25 16:06:28
Compiled: Tue Apr 30 17:02:19 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.3971603 0.3830693 0.5958055
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.3971603 0.3830693 0.5958055
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.5352345 0.7741032 0.4654785 0.3745194
## [2,] 0.1141337 0.0822383 0.1614156 0.4111726
## [3,] 0.4586567 0.6539890 0.7842927 0.8746693
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.5352345 0.7741032 0.4654785 0.3745194
## [2,] 0.1141337 0.0822383 0.1614156 0.4111726
## [3,] 0.4586567 0.6539890 0.7842927 0.8746693
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8397126 0.3151070 0.2247947 0.1617295
## [2,] 0.1800441 0.5322967 0.6742223 0.7709647
## [3,] 0.3791926 0.5829549 0.1881943 0.1179181
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6949987 0.0117332 0.6406104 0.9362038
## [2,] 0.8487628 0.3585903 0.4949705 0.7936359
## [3,] 0.8711747 0.4317642 0.7798197 0.3539810
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03780901 0.7350797 0.3902281 0.2647270
## [2,] 0.78244581 0.9519219 0.2239445 0.2110721
## [3,] 0.94904269 0.6722299 0.5445318 0.3535789
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9415216 0.6561456 0.6434980 0.6955385
## [2,] 0.1824847 0.7589000 0.2490018 0.1955216
## [3,] 0.4256260 0.6481608 0.4088985 0.1706765
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1642054 0.87548728 0.8996973 0.5441403
## [2,] 0.7548411 0.07256683 0.2604087 0.9058986
## [3,] 0.6118638 0.03351413 0.5774367 0.8657313
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.8397126 0.3151070 0.2247947 0.1617295
## [2,] 0.1800441 0.5322967 0.6742223 0.7709647
## [3,] 0.3791926 0.5829549 0.1881943 0.1179181
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.6949987 0.0117332 0.6406104 0.9362038
## [2,] 0.8487628 0.3585903 0.4949705 0.7936359
## [3,] 0.8711747 0.4317642 0.7798197 0.3539810
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.03780901 0.73507968 0.39022814 0.26472701
## [2,] 0.78244581 0.95192194 0.22394447 0.21107208
## [3,] 0.94904269 0.67222987 0.54453182 0.35357888
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.9415216 0.6561456 0.6434980 0.6955385
## [2,] 0.1824847 0.7589000 0.2490018 0.1955216
## [3,] 0.4256260 0.6481608 0.4088985 0.1706765
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.16420539 0.87548728 0.89969729 0.54414034
## [2,] 0.75484109 0.07256683 0.26040871 0.90589862
## [3,] 0.61186378 0.03351413 0.57743671 0.86573134
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.2096641 0.8309999 0.8326274
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.2096641 0.8309999 0.8326274
einsum::einsum('iii->i', arrD)
## [1] 0.003364534 0.713164139 0.811811986
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.003364534 0.713164139 0.811811986
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.1577363 0.1467421 0.3549842
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.1577363 0.1467421 0.3549842
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.2864759 0.599235773 0.21667020 0.1402648
## [2,] 0.0130265 0.006763139 0.02605501 0.1690629
## [3,] 0.2103660 0.427701670 0.61511503 0.7650464
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.286475936 0.599235773 0.216670199 0.140264779
## [2,] 0.013026497 0.006763139 0.026055007 0.169062919
## [3,] 0.210365985 0.427701670 0.615115033 0.765046439
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.70511731 0.09929245 0.05053265 0.02615643
## [2,] 0.03241589 0.28333982 0.45457565 0.59438659
## [3,] 0.14378704 0.33983640 0.03541708 0.01390467
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4830232 0.0001376681 0.4103817 0.8764775
## [2,] 0.7203984 0.1285869723 0.2449958 0.6298579
## [3,] 0.7589454 0.1864203281 0.6081188 0.1253025
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.001429521 0.5403421 0.15227800 0.07008039
## [2,] 0.612221441 0.9061554 0.05015113 0.04455142
## [3,] 0.900682036 0.4518930 0.29651491 0.12501802
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.88646295 0.4305270 0.41408966 0.48377380
## [2,] 0.03330066 0.5759292 0.06200189 0.03822868
## [3,] 0.18115753 0.4201124 0.16719795 0.02913047
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02696341 0.766477984 0.8094552 0.2960887
## [2,] 0.56978506 0.005265944 0.0678127 0.8206523
## [3,] 0.37437729 0.001123197 0.3334331 0.7494907
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.70511731 0.09929245 0.05053265 0.02615643
## [2,] 0.03241589 0.28333982 0.45457565 0.59438659
## [3,] 0.14378704 0.33983640 0.03541708 0.01390467
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.4830231784 0.0001376681 0.4103817247 0.8764775210
## [2,] 0.7203983673 0.1285869723 0.2449957512 0.6298579113
## [3,] 0.7589453520 0.1864203281 0.6081187699 0.1253025321
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.001429521 0.540342143 0.152278002 0.070080390
## [2,] 0.612221441 0.906155384 0.050151125 0.044551424
## [3,] 0.900682036 0.451893000 0.296514905 0.125018022
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.88646295 0.43052699 0.41408966 0.48377380
## [2,] 0.03330066 0.57592923 0.06200189 0.03822868
## [3,] 0.18115753 0.42011244 0.16719795 0.02913047
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.026963410 0.766477984 0.809455222 0.296088705
## [2,] 0.569785064 0.005265944 0.067812699 0.820652309
## [3,] 0.374377286 0.001123197 0.333433149 0.749490750
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.1577363 0.1521399 0.2366303
## [2,] 0.1521399 0.1467421 0.2282348
## [3,] 0.2366303 0.2282348 0.3549842
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.1577363 0.1521399 0.2366303
## [2,] 0.1521399 0.1467421 0.2282348
## [3,] 0.2366303 0.2282348 0.3549842
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.44944315 0.65002424 0.3908681 0.3144887
## [2,] 0.09583949 0.06905654 0.1355427 0.3452668
## [3,] 0.38513984 0.54916286 0.6585805 0.7344709
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09636582 0.13937274 0.08380666 0.06743002
## [2,] 0.02054910 0.01480652 0.02906194 0.07402922
## [3,] 0.08257845 0.11774689 0.14120730 0.15747908
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.20295696 0.29353422 0.17650600 0.1420150
## [2,] 0.04327865 0.03118416 0.06120762 0.1559136
## [3,] 0.17391924 0.24798782 0.29739800 0.3316682
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.16865615 0.24392537 0.1466755 0.1180137
## [2,] 0.03596433 0.02591387 0.0508632 0.1295634
## [3,] 0.14452596 0.20607655 0.2471362 0.2756145
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.28490356 0.41205262 0.24777267 0.1993555
## [2,] 0.06075299 0.04377518 0.08592102 0.2188658
## [3,] 0.24414148 0.34811624 0.41747645 0.4655836
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.31201755 0.45126725 0.27135294 0.2183279
## [2,] 0.06653479 0.04794122 0.09409803 0.2396951
## [3,] 0.26737617 0.38124611 0.45720726 0.5098928
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12031786 0.17401428 0.10463708 0.08418997
## [2,] 0.02565664 0.01848673 0.03628538 0.09242942
## [3,] 0.10310359 0.14701326 0.17630483 0.19662101
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.36086699 0.5219176 0.3138359 0.2525093
## [2,] 0.07695147 0.0554469 0.1088300 0.2772217
## [3,] 0.30923657 0.4409340 0.5287876 0.5897215
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10072806 0.14568179 0.08760038 0.07048240
## [2,] 0.02147930 0.01547678 0.03037750 0.07738033
## [3,] 0.08631657 0.12307699 0.14759939 0.16460775
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08656319 0.12519531 0.07528159 0.06057083
## [2,] 0.01845878 0.01330036 0.02610567 0.06649873
## [3,] 0.07417831 0.10576931 0.12684325 0.14145982
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.41264689 0.59680625 0.3588675 0.2887412
## [2,] 0.08799304 0.06340283 0.1244458 0.3169996
## [3,] 0.35360814 0.50420247 0.6046620 0.6743392
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06311381 0.091280743 0.05488831 0.04416260
## [2,] 0.01345842 0.009697381 0.01903382 0.04848467
## [3,] 0.05408391 0.077117115 0.09248227 0.10313930
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.37198725 0.53800071 0.3235069 0.2602905
## [2,] 0.07932276 0.05715551 0.1121837 0.2857644
## [3,] 0.31876582 0.45452153 0.5450824 0.6078940
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.45428713 0.65703004 0.3950808 0.3178781
## [2,] 0.09687243 0.06980082 0.1370036 0.3489880
## [3,] 0.38929078 0.55508160 0.6656785 0.7423868
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.46628273 0.67437913 0.4055131 0.3262718
## [2,] 0.09943038 0.07164393 0.1406212 0.3582032
## [3,] 0.39957013 0.56973871 0.6832560 0.7619898
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.006280016 0.0090827115 0.005461554 0.004394313
## [2,] 0.001339154 0.0009649189 0.001893923 0.004824373
## [3,] 0.005381513 0.0076733874 0.009202267 0.010262674
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.19192987 0.27758587 0.16691604 0.1342990
## [2,] 0.04092723 0.02948985 0.05788207 0.1474425
## [3,] 0.16446983 0.23451410 0.28123972 0.3136479
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.23109508 0.33423005 0.20097694 0.1617041
## [2,] 0.04927884 0.03550756 0.06969349 0.1775296
## [3,] 0.19803155 0.28236906 0.33862951 0.3776509
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.34287678 0.49589859 0.2981904 0.2399210
## [2,] 0.07311523 0.05268272 0.1034045 0.2634015
## [3,] 0.29382028 0.41895220 0.5024261 0.5603223
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2649252 0.38315822 0.23039809 0.1853760
## [2,] 0.0564928 0.04070553 0.07989597 0.2035183
## [3,] 0.2270215 0.32370525 0.38820171 0.4329355
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.41738638 0.60366093 0.3629893 0.2920576
## [2,] 0.08900369 0.06413105 0.1258751 0.3206405
## [3,] 0.35766955 0.50999354 0.6116069 0.6820844
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5010885 0.72471835 0.4357827 0.3506265
## [2,] 0.1068524 0.07699181 0.1511179 0.3849414
## [3,] 0.4293962 0.61226702 0.7342578 0.8188687
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.42478128 0.61435608 0.3694204 0.2972320
## [2,] 0.09058058 0.06526727 0.1281052 0.3263213
## [3,] 0.36400643 0.51902917 0.6224428 0.6941690
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.18946282 0.2740178 0.16477052 0.1325727
## [2,] 0.04040115 0.0291108 0.05713806 0.1455473
## [3,] 0.16235575 0.2314997 0.27762469 0.3096163
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.020236686 0.029268077 0.017599281 0.01416021
## [2,] 0.004315282 0.003109349 0.006102966 0.01554603
## [3,] 0.017341357 0.024726680 0.029653332 0.03307038
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.41879197 0.60569381 0.3642117 0.2930411
## [2,] 0.08930342 0.06434702 0.1262990 0.3217203
## [3,] 0.35887403 0.51171099 0.6136665 0.6843814
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5079604 0.73465699 0.4417589 0.3554349
## [2,] 0.1083177 0.07804766 0.1531903 0.3902204
## [3,] 0.4352848 0.62066352 0.7443273 0.8300985
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.39343998 0.56902754 0.3421638 0.2753016
## [2,] 0.08389735 0.06045171 0.1186534 0.3022446
## [3,] 0.33714924 0.48073406 0.5765176 0.6429517
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5095014 0.73688583 0.4430992 0.3565132
## [2,] 0.1086464 0.07828445 0.1536551 0.3914042
## [3,] 0.4366054 0.62254652 0.7465854 0.8326169
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.35980060 0.52037530 0.3129085 0.2517631
## [2,] 0.07672407 0.05528304 0.1085084 0.2764025
## [3,] 0.30832275 0.43963097 0.5272250 0.5879789
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.20886355 0.3020769 0.18164279 0.1461480
## [2,] 0.04453817 0.0320917 0.06298892 0.1604511
## [3,] 0.17898076 0.2552049 0.30605308 0.3413206
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11986280 0.17335613 0.10424133 0.08387155
## [2,] 0.02555961 0.01841681 0.03614814 0.09207983
## [3,] 0.10271364 0.14645723 0.17563801 0.19587736
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.29145220 0.42152383 0.25346784 0.2039377
## [2,] 0.06214942 0.04478137 0.08789595 0.2238966
## [3,] 0.24975318 0.35611785 0.42707233 0.4762853
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.14169102 0.2049260 0.12322472 0.0991454
## [2,] 0.03021427 0.0217707 0.04273108 0.1088485
## [3,] 0.12141882 0.1731286 0.20762346 0.2315486
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11297305 0.16339157 0.09824951 0.07905059
## [2,] 0.02409043 0.01735821 0.03407033 0.08678706
## [3,] 0.09680963 0.13803883 0.16554229 0.18461828
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.18924760 0.27370654 0.16458335 0.1324221
## [2,] 0.04035526 0.02907773 0.05707316 0.1453820
## [3,] 0.16217133 0.23123671 0.27730933 0.3092646
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5039348 0.72883490 0.4382580 0.3526181
## [2,] 0.1074593 0.07742914 0.1519763 0.3871279
## [3,] 0.4318352 0.61574482 0.7384285 0.8235201
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09767209 0.14126198 0.08494269 0.06834405
## [2,] 0.02082765 0.01500723 0.02945588 0.07503270
## [3,] 0.08369782 0.11934298 0.14312140 0.15961375
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.22780973 0.32947849 0.1981198 0.1594052
## [2,] 0.04857827 0.03500276 0.0687027 0.1750058
## [3,] 0.19521625 0.27835477 0.3338154 0.3722821
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.35119172 0.5079244 0.3054216 0.2457392
## [2,] 0.07488831 0.0539603 0.1059122 0.2697891
## [3,] 0.30094557 0.4291120 0.5146102 0.5739104
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.40618944 0.58746693 0.3532516 0.2842228
## [2,] 0.08661605 0.06241065 0.1224983 0.3120389
## [3,] 0.34807459 0.49631229 0.5951997 0.6637866
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.34691801 0.50174336 0.3017049 0.2427488
## [2,] 0.07397698 0.05330365 0.1046233 0.2665060
## [3,] 0.29728331 0.42389007 0.5083478 0.5669264
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.34442230 0.49813386 0.2995345 0.2410025
## [2,] 0.07344479 0.05292018 0.1038706 0.2645888
## [3,] 0.29514468 0.42084064 0.5046908 0.5628480
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13327434 0.19275309 0.11590497 0.0932560
## [2,] 0.02841949 0.02047749 0.04019278 0.1023827
## [3,] 0.11420635 0.16284444 0.19529029 0.2177942
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.21885655 0.31652961 0.19033343 0.1531404
## [2,] 0.04666909 0.03362712 0.06600261 0.1681279
## [3,] 0.18754403 0.26741512 0.32069608 0.3576509
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.37227618 0.53841858 0.3237582 0.2604927
## [2,] 0.07938437 0.05719991 0.1122708 0.2859864
## [3,] 0.31901340 0.45487455 0.5455058 0.6083662
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10464988 0.15135387 0.09101108 0.07322662
## [2,] 0.02231560 0.01607936 0.03156024 0.08039311
## [3,] 0.08967728 0.12786896 0.15334613 0.17101671
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09135195 0.13212124 0.07944624 0.06392167
## [2,] 0.01947994 0.01403615 0.02754986 0.07017751
## [3,] 0.07828193 0.11162057 0.13386035 0.14928551
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08788839 0.12711192 0.07643407 0.06149810
## [2,] 0.01874137 0.01350397 0.02650532 0.06751676
## [3,] 0.07531391 0.10738853 0.12878509 0.14362542
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.40401697 0.58432490 0.3513623 0.2827026
## [2,] 0.08615279 0.06207685 0.1218432 0.3103700
## [3,] 0.34621293 0.49365780 0.5920163 0.6602363
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.32749059 0.47364571 0.28480941 0.2291549
## [2,] 0.06983427 0.05031864 0.09876438 0.2515816
## [3,] 0.28063543 0.40015221 0.47988029 0.5351785
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.46859097 0.67771751 0.4075205 0.3278870
## [2,] 0.09992259 0.07199859 0.1413173 0.3599764
## [3,] 0.40154812 0.57255909 0.6866383 0.7657619
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.038840267 0.056174213 0.03377830 0.02717768
## [2,] 0.008282319 0.005967773 0.01171342 0.02983749
## [3,] 0.033283263 0.047457910 0.05691363 0.06347198
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.017937918 0.025943397 0.015600106 0.01255169
## [2,] 0.003825091 0.002756145 0.005409705 0.01378009
## [3,] 0.015371481 0.021917875 0.026284888 0.02931378
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4815490 0.69645856 0.4187897 0.3369541
## [2,] 0.1026858 0.07398958 0.1452252 0.3699309
## [3,] 0.4126522 0.58839217 0.7056260 0.7869376
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13937972 0.20158322 0.12121465 0.09752811
## [2,] 0.02972141 0.02141557 0.04203404 0.10707293
## [3,] 0.11943821 0.17030445 0.20423665 0.22777152
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.30906403 0.44699561 0.26878435 0.2162612
## [2,] 0.06590498 0.04748742 0.09320731 0.2374262
## [3,] 0.26484522 0.37763728 0.45287939 0.5050662
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.29124266 0.42122078 0.25328561 0.2037911
## [2,] 0.06210474 0.04474918 0.08783276 0.2237356
## [3,] 0.24957362 0.35586182 0.42676529 0.4759429
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4848682 0.70125903 0.4216763 0.3392766
## [2,] 0.1033935 0.07449957 0.1462262 0.3724807
## [3,] 0.4154965 0.59244777 0.7104897 0.7923617
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4633693 0.67016540 0.4029793 0.3242332
## [2,] 0.0988091 0.07119628 0.1397426 0.3559650
## [3,] 0.3970735 0.56617881 0.6789868 0.7572287
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.44944315 0.65002424 0.39086815 0.31448867
## [2,] 0.09583949 0.06905654 0.13554275 0.34526684
## [3,] 0.38513984 0.54916286 0.65858049 0.73447089
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.09636582 0.13937274 0.08380666 0.06743002
## [2,] 0.02054910 0.01480652 0.02906194 0.07402922
## [3,] 0.08257845 0.11774689 0.14120730 0.15747908
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.20295696 0.29353422 0.17650600 0.14201499
## [2,] 0.04327865 0.03118416 0.06120762 0.15591362
## [3,] 0.17391924 0.24798782 0.29739800 0.33166815
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.29124266 0.42122078 0.25328561 0.20379111
## [2,] 0.06210474 0.04474918 0.08783276 0.22373560
## [3,] 0.24957362 0.35586182 0.42676529 0.47594286
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.48486817 0.70125903 0.42167630 0.33927660
## [2,] 0.10339354 0.07449957 0.14622620 0.37248070
## [3,] 0.41549649 0.59244777 0.71048967 0.79236174
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.46336925 0.67016540 0.40297929 0.32423318
## [2,] 0.09880910 0.07119628 0.13974257 0.35596502
## [3,] 0.39707349 0.56617881 0.67898677 0.75722865
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.376035
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.376035
einsum::einsum('ij->', arrC)
## [1] 5.689904
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 5.689904
einsum::einsum('ijk->', arrE)
## [1] 30.84175
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 30.84175
einsum::einsum('ij->i', arrC)
## [1] 2.1493355 0.7689602 2.7716078
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 2.1493355 0.7689602 2.7716078
einsum::einsum('ij->j', arrC)
## [1] 1.108025 1.510331 1.411187 1.660361
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 1.108025 1.510331 1.411187 1.660361
einsum::einsum('ijk->i', arrE)
## [1] 10.67297 10.20249 9.96629
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 10.67297 10.20249 9.96629
einsum::einsum('ijk->j', arrE)
## [1] 8.663726 7.636452 7.200257 7.341318
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 8.663726 7.636452 7.200257 7.341318
einsum::einsum('ijk->k', arrE)
## [1] 4.967132 7.216245 6.116611 5.975974 6.565792
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 4.967132 7.216245 6.116611 5.975974 6.565792
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 2.678247 2.593553 2.798829 2.602339
## [2,] 2.748579 2.674276 1.902548 2.877093
## [3,] 3.236900 2.368624 2.498881 1.861886
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 2.678247 2.593553 2.798829 2.602339
## [2,] 2.748579 2.674276 1.902548 2.877093
## [3,] 3.236900 2.368624 2.498881 1.861886
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.398949 2.4149362 1.769298 1.549632 1.5309103
## [2,] 1.430359 0.8020877 2.359231 2.063206 0.9815682
## [3,] 1.087211 1.9154006 1.158704 1.301398 1.7375427
## [4,] 1.050612 2.0838206 0.829378 1.061737 2.3157703
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.3989494 2.4149362 1.7692975 1.5496323 1.5309103
## [2,] 1.4303587 0.8020877 2.3592315 2.0632064 0.9815682
## [3,] 1.0872112 1.9154006 1.1587044 1.3013982 1.7375427
## [4,] 1.0506122 2.0838206 0.8293780 1.0617366 2.3157703
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.398949 2.4149362 1.769298 1.549632 1.5309103
## [2,] 1.430359 0.8020877 2.359231 2.063206 0.9815682
## [3,] 1.087211 1.9154006 1.158704 1.301398 1.7375427
## [4,] 1.050612 2.0838206 0.829378 1.061737 2.3157703
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.3989494 2.4149362 1.7692975 1.5496323 1.5309103
## [2,] 1.4303587 0.8020877 2.3592315 2.0632064 0.9815682
## [3,] 1.0872112 1.9154006 1.1587044 1.3013982 1.7375427
## [4,] 1.0506122 2.0838206 0.8293780 1.0617366 2.3157703
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.873291
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 1.873291
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.2096641 0.2281567 0.3624145
## [2,] 0.2298234 0.8309999 0.7177937
## [3,] 0.5495751 0.9378619 0.8326274
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.2096641 0.2281567 0.3624145
## [2,] 0.2298234 0.8309999 0.7177937
## [3,] 0.5495751 0.9378619 0.8326274
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.003364534 0.5526274 0.3134847
## [2,] 0.834164755 0.4725573 0.6759908
## [3,] 0.026479335 0.1784190 0.6694899
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.6439817 0.3556481 0.6420536
## [2,] 0.4951052 0.7131641 0.3669971
## [3,] 0.7612595 0.7289020 0.9378706
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.7334745 0.9770117 0.260272
## [2,] 0.4302559 0.8965707 0.066706
## [3,] 0.1065696 0.1095281 0.811812
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.003364534 0.552627384 0.313484718
## [2,] 0.834164755 0.472557325 0.675990788
## [3,] 0.026479335 0.178418982 0.669489914
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.6439817 0.3556481 0.6420536
## [2,] 0.4951052 0.7131641 0.3669971
## [3,] 0.7612595 0.7289020 0.9378706
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.7334745 0.9770117 0.2602720
## [2,] 0.4302559 0.8965707 0.0667060
## [3,] 0.1065696 0.1095281 0.8118120
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.6594627
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 0.6594627
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 3.475783
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 3.475783
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 20.64556
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 20.64556
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.8813202 1.962367 1.5143330 1.1009211 0.9711258
## [2,] 0.7224687 0.315145 1.8983905 1.4265687 0.7728671
## [3,] 0.5405254 1.263496 0.4989440 0.6432895 1.2107011
## [4,] 0.6344477 1.631638 0.2396498 0.5511330 1.8662318
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.8813202 1.9623669 1.5143330 1.1009211 0.9711258
## [2,] 0.7224687 0.3151450 1.8983905 1.4265687 0.7728671
## [3,] 0.5405254 1.2634962 0.4989440 0.6432895 1.2107011
## [4,] 0.6344477 1.6316380 0.2396498 0.5511330 1.8662318
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.2426467 0.3538768 1.4443959
## [2,] 0.3538768 0.2149076 0.5923683
## [3,] 1.4443959 0.5923683 2.0182291
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.2426467 0.3538768 1.4443959
## [2,] 0.3538768 0.2149076 0.5923683
## [3,] 1.4443959 0.5923683 2.0182291
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.2864759 0.013026497 0.2103660
## [2,] 0.5992358 0.006763139 0.4277017
## [3,] 0.2166702 0.026055007 0.6151150
## [4,] 0.1402648 0.169062919 0.7650464
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.286475936 0.013026497 0.210365985
## [2,] 0.599235773 0.006763139 0.427701670
## [3,] 0.216670199 0.026055007 0.615115033
## [4,] 0.140264779 0.169062919 0.765046439
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.70511731 0.4830231784 0.001429521 0.8864630 0.02696341
## [2,] 0.09929245 0.0001376681 0.540342143 0.4305270 0.76647798
## [3,] 0.05053265 0.4103817247 0.152278002 0.4140897 0.80945522
## [4,] 0.02615643 0.8764775210 0.070080390 0.4837738 0.29608871
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.03241589 0.7203984 0.61222144 0.03330066 0.569785064
## [2,] 0.28333982 0.1285870 0.90615538 0.57592923 0.005265944
## [3,] 0.45457565 0.2449958 0.05015113 0.06200189 0.067812699
## [4,] 0.59438659 0.6298579 0.04455142 0.03822868 0.820652309
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.14378704 0.7589454 0.9006820 0.18115753 0.374377286
## [2,] 0.33983640 0.1864203 0.4518930 0.42011244 0.001123197
## [3,] 0.03541708 0.6081188 0.2965149 0.16719795 0.333433149
## [4,] 0.01390467 0.1253025 0.1250180 0.02913047 0.749490750
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.7051173116 0.4830231784 0.0014295214 0.8864629502 0.0269634104
## [2,] 0.0992924483 0.0001376681 0.5403421426 0.4305269908 0.7664779841
## [3,] 0.0505326494 0.4103817247 0.1522780021 0.4140896649 0.8094552221
## [4,] 0.0261564258 0.8764775210 0.0700803899 0.4837737954 0.2960887054
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.032415888 0.720398367 0.612221441 0.033300658 0.569785064
## [2,] 0.283339822 0.128586972 0.906155384 0.575929231 0.005265944
## [3,] 0.454575654 0.244995751 0.050151125 0.062001893 0.067812699
## [4,] 0.594386586 0.629857911 0.044551424 0.038228682 0.820652309
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.143787042 0.758945352 0.900682036 0.181157532 0.374377286
## [2,] 0.339836400 0.186420328 0.451893000 0.420112437 0.001123197
## [3,] 0.035417083 0.608118770 0.296514905 0.167197953 0.333433149
## [4,] 0.013904667 0.125302532 0.125018022 0.029130473 0.749490750
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 1.541344 2.157528 1.268260
## [2,] 2.283546 2.495959 2.436740
## [3,] 1.427844 2.169384 2.519383
## [4,] 2.936704 1.385908 1.653362
## [5,] 2.483530 1.993715 2.088546
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.541344 2.157528 1.268260
## [2,] 2.283546 2.495959 2.436740
## [3,] 1.427844 2.169384 2.519383
## [4,] 2.936704 1.385908 1.653362
## [5,] 2.483530 1.993715 2.088546
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.080226048 0.054956871 0.0001626465 0.10085899 0.003067813
## [2,] 0.023630876 0.000032764 0.1285974778 0.10246227 0.182416154
## [3,] 0.004348477 0.035314501 0.0131039501 0.03563358 0.069655897
## [4,] 0.001457112 0.048826466 0.0039040109 0.02694988 0.016494393
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0001617569 0.0035948248 0.003055016 0.0001661720 2.843257e-03
## [2,] 0.0007340629 0.0003331368 0.002347623 0.0014920892 1.364275e-05
## [3,] 0.0045370622 0.0024452717 0.000500552 0.0006188331 6.768300e-04
## [4,] 0.0384941494 0.0407913724 0.002885276 0.0024757971 5.314776e-02
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.01802187 0.09512410 0.11288898 0.02270578 0.0469234076
## [2,] 0.08659950 0.04750494 0.11515455 0.10705600 0.0002862209
## [3,] 0.01297997 0.22286880 0.10866943 0.06127620 0.1221995618
## [4,] 0.00633801 0.05711526 0.05698558 0.01327822 0.3416320509
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,] 0.0802260485 0.0549568707 0.0001626465 0.1008589897 0.0030678127
## [2,] 0.0236308765 0.0000327640 0.1285974778 0.1024622748 0.1824161540
## [3,] 0.0043484765 0.0353145008 0.0131039501 0.0356335795 0.0696558969
## [4,] 0.0014571119 0.0488264659 0.0039040109 0.0269498808 0.0164943934
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.617569e-04 3.594825e-03 3.055016e-03 1.661720e-04 2.843257e-03
## [2,] 7.340629e-04 3.331368e-04 2.347623e-03 1.492089e-03 1.364275e-05
## [3,] 4.537062e-03 2.445272e-03 5.005520e-04 6.188331e-04 6.768300e-04
## [4,] 3.849415e-02 4.079137e-02 2.885276e-03 2.475797e-03 5.314776e-02
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0180218679 0.0951240991 0.1128889808 0.0227057811 0.0469234076
## [2,] 0.0865994976 0.0475049370 0.1151545474 0.1070560011 0.0002862209
## [3,] 0.0129799691 0.2228688040 0.1086694337 0.0612762009 0.1221995618
## [4,] 0.0063380101 0.0571152627 0.0569855777 0.0132782204 0.3416320509
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.0 Patched (2024-04-24 r86482)
## Platform: aarch64-apple-darwin20
## Running under: macOS Ventura 13.6.6
##
## 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.11.0
## [3] HDF5Array_1.31.6 rhdf5_2.47.7
## [5] DelayedArray_0.29.9 SparseArray_1.3.7
## [7] S4Arrays_1.3.7 abind_1.4-5
## [9] IRanges_2.37.1 S4Vectors_0.41.7
## [11] MatrixGenerics_1.15.1 matrixStats_1.3.0
## [13] BiocGenerics_0.49.1 Matrix_1.7-0
## [15] DelayedTensor_1.9.0 BiocStyle_2.31.0
##
## loaded via a namespace (and not attached):
## [1] jsonlite_1.8.8 compiler_4.4.0 BiocManager_1.30.22
## [4] crayon_1.5.2 rsvd_1.0.5 Rcpp_1.0.12
## [7] rhdf5filters_1.15.5 parallel_4.4.0 jquerylib_0.1.4
## [10] BiocParallel_1.37.1 yaml_2.3.8 fastmap_1.1.1
## [13] lattice_0.22-6 R6_2.5.1 XVector_0.43.1
## [16] ScaledMatrix_1.11.1 knitr_1.46 bookdown_0.39
## [19] bslib_0.7.0 rlang_1.1.3 cachem_1.0.8
## [22] xfun_0.43 sass_0.4.9 cli_3.6.2
## [25] Rhdf5lib_1.25.3 BiocSingular_1.19.0 zlibbioc_1.49.3
## [28] digest_0.6.35 grid_4.4.0 irlba_2.3.5.1
## [31] rTensor_1.4.8 dqrng_0.3.2 lifecycle_1.0.4
## [34] evaluate_0.23 codetools_0.2-20 beachmat_2.19.4
## [37] rmarkdown_2.26 tools_4.4.0 htmltools_0.5.8.1