DelayedTensor 1.13.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2024-10-23 23:57:56
Compiled: Fri Nov 22 17:15:41 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.3391358 0.3263181 0.1680363
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.3391358 0.3263181 0.1680363
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.3714649 0.43665338 0.9161388 0.699080172
## [2,] 0.2029072 0.37878550 0.9865080 0.005751135
## [3,] 0.4032804 0.06726562 0.2965304 0.657581290
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.371464915 0.436653380 0.916138786 0.699080172
## [2,] 0.202907215 0.378785500 0.986508015 0.005751135
## [3,] 0.403280435 0.067265624 0.296530424 0.657581290
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4454392 0.3040496 0.0577174 0.9557070
## [2,] 0.4201489 0.9108110 0.6410656 0.4865615
## [3,] 0.8788623 0.6143480 0.8707754 0.7187447
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3891567 0.6152425 0.52581483 0.5273138
## [2,] 0.5234898 0.8546967 0.67624655 0.8206233
## [3,] 0.2416374 0.9218602 0.01486036 0.1435201
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01048491 0.9077042 0.7048502 0.5047760
## [2,] 0.54498322 0.1381419 0.2837720 0.7013626
## [3,] 0.18406093 0.8862632 0.5072904 0.3726544
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9033724 0.9070701 0.6014714 0.7665935
## [2,] 0.2966912 0.9187103 0.7700446 0.3048828
## [3,] 0.6261722 0.7327436 0.2475561 0.8413506
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2238056 0.68619930 0.07876232 0.4029109
## [2,] 0.3424446 0.08243583 0.10945526 0.4064701
## [3,] 0.4188648 0.78755276 0.88718293 0.5264846
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.4454392 0.3040496 0.0577174 0.9557070
## [2,] 0.4201489 0.9108110 0.6410656 0.4865615
## [3,] 0.8788623 0.6143480 0.8707754 0.7187447
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.38915668 0.61524252 0.52581483 0.52731377
## [2,] 0.52348975 0.85469672 0.67624655 0.82062333
## [3,] 0.24163735 0.92186025 0.01486036 0.14352010
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.01048491 0.90770423 0.70485021 0.50477596
## [2,] 0.54498322 0.13814186 0.28377203 0.70136261
## [3,] 0.18406093 0.88626321 0.50729044 0.37265440
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.9033724 0.9070701 0.6014714 0.7665935
## [2,] 0.2966912 0.9187103 0.7700446 0.3048828
## [3,] 0.6261722 0.7327436 0.2475561 0.8413506
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.22380563 0.68619930 0.07876232 0.40291090
## [2,] 0.34244464 0.08243583 0.10945526 0.40647013
## [3,] 0.41886480 0.78755276 0.88718293 0.52648459
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.6772661 0.7177520 0.1136266
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.6772661 0.7177520 0.1136266
einsum::einsum('iii->i', arrD)
## [1] 0.8189629 0.0737262 0.1732961
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.8189629 0.0737262 0.1732961
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.11501311 0.10648348 0.02823619
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.11501311 0.10648348 0.02823619
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.13798618 0.190666174 0.83931028 4.887131e-01
## [2,] 0.04117134 0.143478455 0.97319806 3.307556e-05
## [3,] 0.16263511 0.004524664 0.08793029 4.324132e-01
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 1.379862e-01 1.906662e-01 8.393103e-01 4.887131e-01
## [2,] 4.117134e-02 1.434785e-01 9.731981e-01 3.307556e-05
## [3,] 1.626351e-01 4.524664e-03 8.793029e-02 4.324132e-01
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1984161 0.09244615 0.003331298 0.9133759
## [2,] 0.1765251 0.82957676 0.410965044 0.2367421
## [3,] 0.7723990 0.37742344 0.758249794 0.5165940
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.15144292 0.3785234 0.2764812324 0.27805981
## [2,] 0.27404152 0.7305065 0.4573094020 0.67342266
## [3,] 0.05838861 0.8498263 0.0002208304 0.02059802
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0001099334 0.82392697 0.49681381 0.2547988
## [2,] 0.2970067101 0.01908317 0.08052657 0.4919095
## [3,] 0.0338784249 0.78546248 0.25734359 0.1388713
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.81608173 0.8227762 0.36176780 0.58766553
## [2,] 0.08802567 0.8440286 0.59296870 0.09295352
## [3,] 0.39209168 0.5369132 0.06128405 0.70787091
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05008896 0.470869480 0.006203504 0.1623372
## [2,] 0.11726833 0.006795666 0.011980455 0.1652180
## [3,] 0.17544772 0.620239345 0.787093559 0.2771860
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.198416071 0.092446148 0.003331298 0.913375916
## [2,] 0.176525076 0.829576761 0.410965044 0.236742097
## [3,] 0.772398992 0.377423444 0.758249794 0.516593954
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.1514429217 0.3785233583 0.2764812324 0.2780598084
## [2,] 0.2740415213 0.7305064834 0.4573094020 0.6734226560
## [3,] 0.0583886104 0.8498263179 0.0002208304 0.0205980188
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.0001099334 0.8239269652 0.4968138148 0.2547987698
## [2,] 0.2970067101 0.0190831735 0.0805265675 0.4919095066
## [3,] 0.0338784249 0.7854624829 0.2573435895 0.1388713022
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.81608173 0.82277617 0.36176780 0.58766553
## [2,] 0.08802567 0.84402864 0.59296870 0.09295352
## [3,] 0.39209168 0.53691316 0.06128405 0.70787091
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.050088958 0.470869480 0.006203504 0.162337194
## [2,] 0.117268334 0.006795666 0.011980455 0.165217969
## [3,] 0.175447718 0.620239345 0.787093559 0.277186025
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.11501311 0.11066615 0.05698712
## [2,] 0.11066615 0.10648348 0.05483327
## [3,] 0.05698712 0.05483327 0.02823619
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.11501311 0.11066615 0.05698712
## [2,] 0.11066615 0.10648348 0.05483327
## [3,] 0.05698712 0.05483327 0.02823619
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.16546503 0.19450253 0.4080841 0.311397705
## [2,] 0.09038283 0.16872591 0.4394293 0.002561781
## [3,] 0.17963691 0.02996274 0.1320863 0.292912476
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.15607057 0.18345943 0.3849147 0.293717747
## [2,] 0.08525124 0.15914630 0.4144802 0.002416333
## [3,] 0.16943782 0.02826158 0.1245869 0.276282038
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3264665 0.38375821 0.8051599 0.614395228
## [2,] 0.1783275 0.33290031 0.8670047 0.005054456
## [3,] 0.3544280 0.05911722 0.2606094 0.577923423
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11294375 0.13276428 0.27855161 0.21255503
## [2,] 0.06169385 0.11516957 0.29994735 0.00174863
## [3,] 0.12261725 0.02045208 0.09015995 0.19993732
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3383343 0.39770872 0.8344293 0.636729943
## [2,] 0.1848101 0.34500202 0.8985224 0.005238198
## [3,] 0.3673123 0.06126627 0.2700832 0.598932302
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2282087 0.2682571 0.5628280 0.429478494
## [2,] 0.1246556 0.2327061 0.6060592 0.003533198
## [3,] 0.2477545 0.0413245 0.1821729 0.403983739
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02143999 0.025202497 0.05287715 0.0403490881
## [2,] 0.01171128 0.021862513 0.05693868 0.0003319406
## [3,] 0.02327630 0.003882397 0.01711496 0.0379538806
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2381334 0.27992344 0.5873050 0.448156218
## [2,] 0.1300768 0.24282634 0.6324163 0.003686855
## [3,] 0.2585292 0.04312167 0.1900954 0.421552714
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3234625 0.38022702 0.7977511 0.608741815
## [2,] 0.1766866 0.32983709 0.8590269 0.005007947
## [3,] 0.3511667 0.05857325 0.2582114 0.572605609
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3550116 0.41731270 0.8755603 0.6681158
## [2,] 0.1939199 0.36200796 0.9428126 0.0054964
## [3,] 0.3854179 0.06428623 0.2833962 0.6284551
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.18074053 0.21245872 0.4457579 0.340145500
## [2,] 0.09872684 0.18430244 0.4799968 0.002798281
## [3,] 0.19622073 0.03272886 0.1442803 0.319953741
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2669884 0.31384231 0.6584699 0.502460174
## [2,] 0.1458385 0.27225007 0.7090474 0.004133598
## [3,] 0.2898557 0.04834681 0.2131297 0.472633072
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1445581 0.16992658 0.3565215 0.272051719
## [2,] 0.0789627 0.14740691 0.3839062 0.002238093
## [3,] 0.1569393 0.02617687 0.1153968 0.255902152
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1944581 0.22858357 0.4795893 0.36596131
## [2,] 0.1062198 0.19829033 0.5164268 0.00301066
## [3,] 0.2111132 0.03521286 0.1552306 0.34423707
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08975980 0.10551177 0.22137335 0.168923882
## [2,] 0.04902996 0.09152873 0.23837719 0.001389689
## [3,] 0.09744762 0.01625389 0.07165283 0.158896202
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2285410 0.26864773 0.5636475 0.430103847
## [2,] 0.1248371 0.23304495 0.6069417 0.003538343
## [3,] 0.2481153 0.04138467 0.1824381 0.404571970
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3174898 0.37320621 0.7830208 0.597501531
## [2,] 0.1734241 0.32374672 0.8431652 0.004915476
## [3,] 0.3446825 0.05749171 0.2534436 0.562032572
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3424387 0.4025334 0.8445519 0.644454222
## [2,] 0.1870521 0.3491873 0.9094225 0.005301743
## [3,] 0.3717682 0.0620095 0.2733596 0.606198051
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1953218 0.22959882 0.4817194 0.367586720
## [2,] 0.1066916 0.19917103 0.5187205 0.003024032
## [3,] 0.2120508 0.03536926 0.1559201 0.345765992
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2512019 0.29528534 0.6195357 0.472750558
## [2,] 0.1372153 0.25615239 0.6671226 0.003889185
## [3,] 0.2727170 0.04548815 0.2005277 0.444687081
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.005520103 0.0064888274 0.01361415 1.038858e-02
## [2,] 0.003015275 0.0056288898 0.01465987 8.546395e-05
## [3,] 0.005992893 0.0009995915 0.00440655 9.771896e-03
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1958786 0.23025334 0.4830926 0.368634599
## [2,] 0.1069958 0.19973881 0.5201993 0.003032653
## [3,] 0.2126553 0.03547009 0.1563646 0.346751667
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3048328 0.35832795 0.7518049 0.573681502
## [2,] 0.1665104 0.31084022 0.8095515 0.004719516
## [3,] 0.3309413 0.05519974 0.2433398 0.539626550
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05331268 0.062668536 0.13148433 0.1003320556
## [2,] 0.02912126 0.054363332 0.14158373 0.0008254035
## [3,] 0.05787885 0.009653969 0.04255808 0.0943761319
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.003894778 0.0045782736 0.009605637 7.329796e-03
## [2,] 0.002127465 0.0039715338 0.010343453 6.030016e-05
## [3,] 0.004228361 0.0007052743 0.003109096 6.894684e-03
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2024421 0.23796876 0.4992803 0.380986963
## [2,] 0.1105810 0.20643174 0.5376303 0.003134272
## [3,] 0.2197811 0.03665864 0.1616041 0.358370769
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06837218 0.08037083 0.16862535 0.128673345
## [2,] 0.03734729 0.06971961 0.18157758 0.001058559
## [3,] 0.07422817 0.01238097 0.05457966 0.121035022
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3371803 0.39635212 0.8315830 0.63455803
## [2,] 0.1841797 0.34382520 0.8954575 0.00522033
## [3,] 0.3660594 0.06105729 0.2691619 0.59688932
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05131485 0.060320110 0.12655712 0.0965722353
## [2,] 0.02802998 0.052326133 0.13627805 0.0007944725
## [3,] 0.05570991 0.009292198 0.04096326 0.0908395025
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3292157 0.38698983 0.8119401 0.61956904
## [2,] 0.1798292 0.33570365 0.8743058 0.00509702
## [3,] 0.3574126 0.05961505 0.2628040 0.58279011
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2618271 0.30777523 0.6457406 0.492746804
## [2,] 0.1430192 0.26698704 0.6953404 0.004053689
## [3,] 0.2842523 0.04741219 0.2090095 0.463496308
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10541135 0.1239100 0.25997457 0.198379403
## [2,] 0.05757939 0.1074887 0.27994339 0.001632011
## [3,] 0.11443971 0.0190881 0.08414704 0.186603180
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1884406 0.22151008 0.4647484 0.354636688
## [2,] 0.1029329 0.19215426 0.5004461 0.002917496
## [3,] 0.2045803 0.03412321 0.1504270 0.333584701
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1875066 0.22041213 0.4624448 0.352878865
## [2,] 0.1024227 0.19120181 0.4979655 0.002903035
## [3,] 0.2035663 0.03395407 0.1496814 0.331931227
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2605316 0.30625235 0.6425455 0.490308692
## [2,] 0.1423115 0.26566599 0.6918998 0.004033631
## [3,] 0.2828458 0.04717759 0.2079754 0.461202928
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13842804 0.16272080 0.3414032 0.260515303
## [2,] 0.07561427 0.14115608 0.3676266 0.002143186
## [3,] 0.15028423 0.02506683 0.1105034 0.245050561
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3355712 0.39446062 0.8276145 0.631529747
## [2,] 0.1833008 0.34218437 0.8911841 0.005195417
## [3,] 0.3643124 0.06076591 0.2678774 0.594040801
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11021037 0.12955122 0.27181032 0.207410940
## [2,] 0.06020079 0.11238233 0.29268825 0.001706311
## [3,] 0.11964976 0.01995712 0.08797797 0.195098587
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2326010 0.27342023 0.5736607 0.437744603
## [2,] 0.1270549 0.23718497 0.6177239 0.003601201
## [3,] 0.2525230 0.04211987 0.1856791 0.411759155
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3369447 0.39607523 0.8310021 0.634114723
## [2,] 0.1840511 0.34358500 0.8948319 0.005216683
## [3,] 0.3658036 0.06101464 0.2689739 0.596472327
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3412686 0.40115796 0.8416661 0.642252163
## [2,] 0.1864130 0.34799414 0.9063151 0.005283627
## [3,] 0.3704979 0.06179762 0.2724256 0.604126712
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2721885 0.31995496 0.6712948 0.512246512
## [2,] 0.1486790 0.27755265 0.7228574 0.004214107
## [3,] 0.2955012 0.04928845 0.2172808 0.481838472
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2234255 0.26263450 0.5510312 0.420476703
## [2,] 0.1220429 0.22782863 0.5933563 0.003459143
## [3,] 0.2425616 0.04045835 0.1783546 0.395516314
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2860446 0.33624258 0.7054677 0.538322919
## [2,] 0.1562476 0.29168173 0.7596552 0.004428631
## [3,] 0.3105439 0.05179753 0.2283417 0.506366928
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09195842 0.10809623 0.22679579 0.173061593
## [2,] 0.05023093 0.09377068 0.24421612 0.001423729
## [3,] 0.09983455 0.01665202 0.07340793 0.162788290
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2847626 0.33473563 0.7023060 0.535910289
## [2,] 0.1555473 0.29037449 0.7562506 0.004408783
## [3,] 0.3091521 0.05156539 0.2273183 0.504097517
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11325326 0.13312810 0.27931495 0.213137517
## [2,] 0.06186292 0.11548518 0.30076932 0.001753422
## [3,] 0.12295327 0.02050813 0.09040702 0.200485222
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3125322 0.36737860 0.7707940 0.588171555
## [2,] 0.1707161 0.31869143 0.8299992 0.004838721
## [3,] 0.3393003 0.05659398 0.2494861 0.553256444
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08313594 0.09772548 0.20503701 0.156458076
## [2,] 0.04541178 0.08477433 0.22078604 0.001287136
## [3,] 0.09025643 0.01505443 0.06636518 0.147170392
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12720617 0.14952961 0.3137268 0.239396260
## [2,] 0.06948449 0.12971307 0.3378244 0.001969445
## [3,] 0.13810122 0.02303475 0.1015453 0.225185190
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.15559358 0.1828987 0.3837383 0.292820074
## [2,] 0.08499069 0.1586599 0.4132135 0.002408948
## [3,] 0.16891998 0.0281752 0.1242062 0.275437653
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2548990 0.29963124 0.6286538 0.479708325
## [2,] 0.1392348 0.25992234 0.6769411 0.003946425
## [3,] 0.2767308 0.04615762 0.2034790 0.451231821
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03062202 0.035995885 0.07552266 0.0576292561
## [2,] 0.01672683 0.031225498 0.08132361 0.0004740996
## [3,] 0.03324476 0.005545098 0.02444473 0.0542082611
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2925482 0.34388757 0.7215076 0.550562517
## [2,] 0.1598001 0.29831356 0.7769271 0.004529322
## [3,] 0.3176046 0.05297523 0.2335334 0.517879958
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02925744 0.034391835 0.07215722 0.0550611791
## [2,] 0.01598144 0.029834026 0.07769966 0.0004529728
## [3,] 0.03176330 0.005297997 0.02335543 0.0517926306
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04065879 0.047794011 0.10027621 0.076518005
## [2,] 0.02220926 0.041460067 0.10797850 0.000629492
## [3,] 0.04414117 0.007362577 0.03245682 0.071975734
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3295573 0.38739143 0.8127827 0.620211999
## [2,] 0.1800158 0.33605203 0.8752131 0.005102309
## [3,] 0.3577835 0.05967691 0.2630767 0.583394898
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.14966726 0.17593241 0.3691223 0.281667022
## [2,] 0.08175353 0.15261681 0.3974748 0.002317195
## [3,] 0.16248608 0.02710205 0.1194753 0.264946670
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.15098939 0.17748656 0.3723831 0.284155211
## [2,] 0.08247572 0.15396499 0.4009860 0.002337665
## [3,] 0.16392145 0.02734147 0.1205308 0.267287154
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1955706 0.22989128 0.4823330 0.368054939
## [2,] 0.1068275 0.19942473 0.5193813 0.003027884
## [3,] 0.2123209 0.03541431 0.1561187 0.346206417
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.165465030 0.194502527 0.408084118 0.311397705
## [2,] 0.090382825 0.168725906 0.439429330 0.002561781
## [3,] 0.179636910 0.029962745 0.132086272 0.292912476
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.156070566 0.183459426 0.384914679 0.293717747
## [2,] 0.085251238 0.159146301 0.414480231 0.002416333
## [3,] 0.169437821 0.028261576 0.124586924 0.276282038
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.326466520 0.383758206 0.805159866 0.614395228
## [2,] 0.178327507 0.332900306 0.867004731 0.005054456
## [3,] 0.354427982 0.059117223 0.260609419 0.577923423
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.149667263 0.175932407 0.369122304 0.281667022
## [2,] 0.081753529 0.152616807 0.397474833 0.002317195
## [3,] 0.162486083 0.027102053 0.119475340 0.264946670
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.150989393 0.177486557 0.372383054 0.284155211
## [2,] 0.082475723 0.153964992 0.400986044 0.002337665
## [3,] 0.163921452 0.027341467 0.120530761 0.267287154
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.195570554 0.229891276 0.482332955 0.368054939
## [2,] 0.106827522 0.199424729 0.519381270 0.003027884
## [3,] 0.212320935 0.035414314 0.156118699 0.346206417
If there is a vanishing subscript on the left or right side of ->, the summation is done for that subscript.
einsum::einsum('i->', arrA)
## [1] 0.8334902
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 0.8334902
einsum::einsum('ij->', arrC)
## [1] 5.421947
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 5.421947
einsum::einsum('ijk->', arrE)
## [1] 32.17426
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 32.17426
einsum::einsum('ij->i', arrC)
## [1] 2.423337 1.573952 1.424658
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 2.423337 1.573952 1.424658
einsum::einsum('ij->j', arrC)
## [1] 0.9776526 0.8827045 2.1991772 1.3624126
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 0.9776526 0.8827045 2.1991772 1.3624126
einsum::einsum('ijk->i', arrE)
## [1] 10.51844 10.23304 11.42279
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 10.51844 10.23304 11.42279
einsum::einsum('ijk->j', arrE)
## [1] 6.449614 10.267829 6.976865 8.479956
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 6.449614 10.267829 6.976865 8.479956
einsum::einsum('ijk->k', arrE)
## [1] 7.304231 6.254462 5.746344 7.916659 4.952569
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 7.304231 6.254462 5.746344 7.916659 4.952569
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 1.972259 3.420266 1.968616 3.157301
## [2,] 2.127758 2.904796 2.480584 2.719900
## [3,] 2.349598 3.942768 2.527665 2.602754
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 1.972259 3.420266 1.968616 3.157301
## [2,] 2.127758 2.904796 2.480584 2.719900
## [3,] 2.349598 3.942768 2.527665 2.602754
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.744450 1.154284 0.7395291 1.826236 0.9851151
## [2,] 1.829209 2.391799 1.9321093 2.558524 1.5561879
## [3,] 1.569558 1.216922 1.4959127 1.619072 1.0754005
## [4,] 2.161013 1.491457 1.5787930 1.912827 1.3358656
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.7444504 1.1542838 0.7395291 1.8262359 0.9851151
## [2,] 1.8292086 2.3917995 1.9321093 2.5585240 1.5561879
## [3,] 1.5695583 1.2169217 1.4959127 1.6190721 1.0754005
## [4,] 2.1610132 1.4914572 1.5787930 1.9128269 1.3358656
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.744450 1.154284 0.7395291 1.826236 0.9851151
## [2,] 1.829209 2.391799 1.9321093 2.558524 1.5561879
## [3,] 1.569558 1.216922 1.4959127 1.619072 1.0754005
## [4,] 2.161013 1.491457 1.5787930 1.912827 1.3358656
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.7444504 1.1542838 0.7395291 1.8262359 0.9851151
## [2,] 1.8292086 2.3917995 1.9321093 2.5585240 1.5561879
## [3,] 1.5695583 1.2169217 1.4959127 1.6190721 1.0754005
## [4,] 2.1610132 1.4914572 1.5787930 1.9128269 1.3358656
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.508645
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 1.508645
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.6772661 0.6684504 0.5512946
## [2,] 0.9622721 0.7177520 0.5680659
## [3,] 0.8032269 0.6721922 0.1136266
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.6772661 0.6684504 0.5512946
## [2,] 0.9622721 0.7177520 0.5680659
## [3,] 0.8032269 0.6721922 0.1136266
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.81896294 0.6124837 0.08739333
## [2,] 0.08958876 0.6992258 0.86579478
## [3,] 0.97096823 0.7420513 0.54926389
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.01381848 0.7955638 0.1026375
## [2,] 0.70141645 0.0737262 0.4831755
## [3,] 0.39150747 0.4193728 0.4247563
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.2144520 0.6891224 0.5719222
## [2,] 0.3490131 0.6421481 0.7894078
## [3,] 0.8101435 0.9332682 0.1732961
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.81896294 0.61248366 0.08739333
## [2,] 0.08958876 0.69922583 0.86579478
## [3,] 0.97096823 0.74205129 0.54926389
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.01381848 0.79556385 0.10263753
## [2,] 0.70141645 0.07372620 0.48317552
## [3,] 0.39150747 0.41937281 0.42475631
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.2144520 0.6891224 0.5719222
## [2,] 0.3490131 0.6421481 0.7894078
## [3,] 0.8101435 0.9332682 0.1732961
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.2497328
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 0.2497328
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 3.50206
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 3.50206
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 21.86975
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 21.86975
The inner product is an operation that eliminates all subscripts, while the outer product is an operation that leaves all subscripts intact. In the middle of the two, the operation that eliminates some subscripts while keeping others by summing them is called contracted product.
einsum::einsum('ijk,ijk->jk', arrE, arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.147340 0.4838731 0.3309951 1.296199 0.3428050
## [2,] 1.299446 1.9588562 1.6284726 2.203718 1.0979045
## [3,] 1.172546 0.7340115 0.8346840 1.016021 0.8052775
## [4,] 1.666712 0.9720805 0.8855796 1.388490 0.6047412
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.1473401 0.4838731 0.3309951 1.2961991 0.3428050
## [2,] 1.2994464 1.9588562 1.6284726 2.2037180 1.0979045
## [3,] 1.1725461 0.7340115 0.8346840 1.0160205 0.8052775
## [4,] 1.6667120 0.9720805 0.8855796 1.3884900 0.6047412
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.6566757 1.1485696 0.9105414
## [2,] 1.1485696 1.1578809 0.4036192
## [3,] 0.9105414 0.4036192 0.6875032
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.6566757 1.1485696 0.9105414
## [2,] 1.1485696 1.1578809 0.4036192
## [3,] 0.9105414 0.4036192 0.6875032
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.1379862 4.117134e-02 0.162635109
## [2,] 0.1906662 1.434785e-01 0.004524664
## [3,] 0.8393103 9.731981e-01 0.087930292
## [4,] 0.4887131 3.307556e-05 0.432413153
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.379862e-01 4.117134e-02 1.626351e-01
## [2,] 1.906662e-01 1.434785e-01 4.524664e-03
## [3,] 8.393103e-01 9.731981e-01 8.793029e-02
## [4,] 4.887131e-01 3.307556e-05 4.324132e-01
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.198416071 0.1514429 0.0001099334 0.8160817 0.050088958
## [2,] 0.092446148 0.3785234 0.8239269652 0.8227762 0.470869480
## [3,] 0.003331298 0.2764812 0.4968138148 0.3617678 0.006203504
## [4,] 0.913375916 0.2780598 0.2547987698 0.5876655 0.162337194
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.1765251 0.2740415 0.29700671 0.08802567 0.117268334
## [2,] 0.8295768 0.7305065 0.01908317 0.84402864 0.006795666
## [3,] 0.4109650 0.4573094 0.08052657 0.59296870 0.011980455
## [4,] 0.2367421 0.6734227 0.49190951 0.09295352 0.165217969
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.7723990 0.0583886104 0.03387842 0.39209168 0.1754477
## [2,] 0.3774234 0.8498263179 0.78546248 0.53691316 0.6202393
## [3,] 0.7582498 0.0002208304 0.25734359 0.06128405 0.7870936
## [4,] 0.5165940 0.0205980188 0.13887130 0.70787091 0.2771860
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.1984160711 0.1514429217 0.0001099334 0.8160817292 0.0500889584
## [2,] 0.0924461480 0.3785233583 0.8239269652 0.8227761690 0.4708694796
## [3,] 0.0033312980 0.2764812324 0.4968138148 0.3617677992 0.0062035037
## [4,] 0.9133759163 0.2780598084 0.2547987698 0.5876655347 0.1623371941
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.176525076 0.274041521 0.297006710 0.088025672 0.117268334
## [2,] 0.829576761 0.730506483 0.019083173 0.844028637 0.006795666
## [3,] 0.410965044 0.457309402 0.080526567 0.592968702 0.011980455
## [4,] 0.236742097 0.673422656 0.491909507 0.092953519 0.165217969
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.7723989918 0.0583886104 0.0338784249 0.3920916845 0.1754477177
## [2,] 0.3774234441 0.8498263179 0.7854624829 0.5369131621 0.6202393446
## [3,] 0.7582497939 0.0002208304 0.2573435895 0.0612840454 0.7870935592
## [4,] 0.5165939543 0.0205980188 0.1388713022 0.7078709111 0.2771860255
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 1.762913 2.4585870 3.082730
## [2,] 2.057528 2.8750564 1.321878
## [3,] 2.127815 1.6682597 1.950269
## [4,] 3.178507 2.2903289 2.447823
## [5,] 1.391678 0.9408059 2.620085
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.7629132 2.4585870 3.0827304
## [2,] 2.0575278 2.8750564 1.3218781
## [3,] 2.1278153 1.6682597 1.9502690
## [4,] 3.1785073 2.2903289 2.4478226
## [5,] 1.3916782 0.9408059 2.6200851
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.0092850900 0.007086932 5.144452e-06 0.03818941 0.002343966
## [2,] 0.0059777279 0.024475975 5.327654e-02 0.05320213 0.030447235
## [3,] 0.0009482213 0.078697669 1.414132e-01 0.10297365 0.001765766
## [4,] 0.1513830316 0.046085665 4.223038e-02 0.09739975 0.026905786
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.371606e-03 3.681734e-03 3.990271e-03 1.182621e-03 1.575494e-03
## [2,] 3.884046e-02 3.420203e-02 8.934668e-04 3.951709e-02 3.181705e-04
## [3,] 1.305110e-01 1.452287e-01 2.557299e-02 1.883103e-01 3.804658e-03
## [4,] 2.555193e-06 7.268353e-06 5.309254e-06 1.003261e-06 1.783223e-06
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0211085800 1.595679e-03 0.0009258498 0.0107153152 0.0047947398
## [2,] 0.0002869579 6.461295e-04 0.0005971931 0.0004082192 0.0004715727
## [3,] 0.0112035029 3.262874e-06 0.0038023745 0.0009055010 0.0116296833
## [4,] 0.0375362799 1.496675e-03 0.0100905402 0.0514346721 0.0201406388
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,] 9.285090e-03 7.086932e-03 5.144452e-06 3.818941e-02 2.343966e-03
## [2,] 5.977728e-03 2.447598e-02 5.327654e-02 5.320213e-02 3.044724e-02
## [3,] 9.482213e-04 7.869767e-02 1.414132e-01 1.029737e-01 1.765766e-03
## [4,] 1.513830e-01 4.608567e-02 4.223038e-02 9.739975e-02 2.690579e-02
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.371606e-03 3.681734e-03 3.990271e-03 1.182621e-03 1.575494e-03
## [2,] 3.884046e-02 3.420203e-02 8.934668e-04 3.951709e-02 3.181705e-04
## [3,] 1.305110e-01 1.452287e-01 2.557299e-02 1.883103e-01 3.804658e-03
## [4,] 2.555193e-06 7.268353e-06 5.309254e-06 1.003261e-06 1.783223e-06
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.110858e-02 1.595679e-03 9.258498e-04 1.071532e-02 4.794740e-03
## [2,] 2.869579e-04 6.461295e-04 5.971931e-04 4.082192e-04 4.715727e-04
## [3,] 1.120350e-02 3.262874e-06 3.802374e-03 9.055010e-04 1.162968e-02
## [4,] 3.753628e-02 1.496675e-03 1.009054e-02 5.143467e-02 2.014064e-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 Under development (unstable) (2024-11-20 r87352)
## Platform: x86_64-apple-darwin20
## Running under: macOS Monterey 12.7.6
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.5-x86_64/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.15.0
## [3] HDF5Array_1.35.1 rhdf5_2.51.0
## [5] DelayedArray_0.33.2 SparseArray_1.7.2
## [7] S4Arrays_1.7.1 abind_1.4-8
## [9] IRanges_2.41.1 S4Vectors_0.45.2
## [11] MatrixGenerics_1.19.0 matrixStats_1.4.1
## [13] BiocGenerics_0.53.3 generics_0.1.3
## [15] Matrix_1.7-1 DelayedTensor_1.13.0
## [17] BiocStyle_2.35.0
##
## loaded via a namespace (and not attached):
## [1] dqrng_0.4.1 sass_0.4.9 lattice_0.22-6
## [4] digest_0.6.37 evaluate_1.0.1 grid_4.5.0
## [7] bookdown_0.41 fastmap_1.2.0 jsonlite_1.8.9
## [10] BiocManager_1.30.25 codetools_0.2-20 jquerylib_0.1.4
## [13] cli_3.6.3 rlang_1.1.4 crayon_1.5.3
## [16] XVector_0.47.0 cachem_1.1.0 yaml_2.3.10
## [19] tools_4.5.0 beachmat_2.23.1 parallel_4.5.0
## [22] BiocParallel_1.41.0 Rhdf5lib_1.29.0 rsvd_1.0.5
## [25] R6_2.5.1 lifecycle_1.0.4 zlibbioc_1.53.0
## [28] BiocSingular_1.23.0 irlba_2.3.5.1 ScaledMatrix_1.15.0
## [31] rTensor_1.4.8 bslib_0.8.0 Rcpp_1.0.13-1
## [34] xfun_0.49 knitr_1.49 rhdf5filters_1.19.0
## [37] htmltools_0.5.8.1 rmarkdown_2.29 compiler_4.5.0