DelayedTensor 1.14.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2025-04-15 15:16:31
Compiled: Tue Apr 15 17:48:08 2025
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.9957064 0.4226570 0.9883374
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.9957064 0.4226570 0.9883374
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.5234759 0.3491565 0.002568306 0.35501590
## [2,] 0.4347122 0.9159651 0.094107510 0.04428322
## [3,] 0.4064947 0.5126858 0.194152105 0.73687002
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.523475883 0.349156498 0.002568306 0.355015899
## [2,] 0.434712242 0.915965135 0.094107510 0.044283223
## [3,] 0.406494674 0.512685802 0.194152105 0.736870021
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3652453 0.4379316 0.8565457 0.2947135
## [2,] 0.1092186 0.4586303 0.6487513 0.9984174
## [3,] 0.2722273 0.3426239 0.7133445 0.9665311
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4905161 0.4184442 0.6150863 0.8376687
## [2,] 0.6648658 0.6666285 0.9921079 0.3609471
## [3,] 0.4540062 0.1612347 0.7949612 0.2287036
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7911584 0.9831530 0.3344617 0.74300440
## [2,] 0.3841753 0.3480583 0.8389231 0.03483287
## [3,] 0.3889750 0.4661306 0.9264274 0.19429056
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4977503 0.1985104 0.9173119 0.1448846
## [2,] 0.1306331 0.7325148 0.2268243 0.4020997
## [3,] 0.6135146 0.5167229 0.3657563 0.2499708
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4009274 0.4613427 0.1217889 0.5465726
## [2,] 0.4977542 0.2628185 0.5563010 0.2299923
## [3,] 0.6747211 0.4384417 0.9456288 0.1931491
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.3652453 0.4379316 0.8565457 0.2947135
## [2,] 0.1092186 0.4586303 0.6487513 0.9984174
## [3,] 0.2722273 0.3426239 0.7133445 0.9665311
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.4905161 0.4184442 0.6150863 0.8376687
## [2,] 0.6648658 0.6666285 0.9921079 0.3609471
## [3,] 0.4540062 0.1612347 0.7949612 0.2287036
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.79115839 0.98315298 0.33446175 0.74300440
## [2,] 0.38417527 0.34805832 0.83892315 0.03483287
## [3,] 0.38897498 0.46613060 0.92642745 0.19429056
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.4977503 0.1985104 0.9173119 0.1448846
## [2,] 0.1306331 0.7325148 0.2268243 0.4020997
## [3,] 0.6135146 0.5167229 0.3657563 0.2499708
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.4009274 0.4613427 0.1217889 0.5465726
## [2,] 0.4977542 0.2628185 0.5563010 0.2299923
## [3,] 0.6747211 0.4384417 0.9456288 0.1931491
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.7316243 0.8009470 0.2941801
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.7316243 0.8009470 0.2941801
einsum::einsum('iii->i', arrD)
## [1] 0.5975829 0.7688202 0.5936452
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.5975829 0.7688202 0.5936452
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.9914312 0.1786389 0.9768109
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.9914312 0.1786389 0.9768109
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.2740270 0.1219103 6.596194e-06 0.126036289
## [2,] 0.1889747 0.8389921 8.856223e-03 0.001961004
## [3,] 0.1652379 0.2628467 3.769504e-02 0.542977428
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 2.740270e-01 1.219103e-01 6.596194e-06 1.260363e-01
## [2,] 1.889747e-01 8.389921e-01 8.856223e-03 1.961004e-03
## [3,] 1.652379e-01 2.628467e-01 3.769504e-02 5.429774e-01
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13340411 0.1917841 0.7336705 0.08685607
## [2,] 0.01192871 0.2103417 0.4208782 0.99683724
## [3,] 0.07410769 0.1173912 0.5088604 0.93418236
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2406060 0.17509551 0.3783312 0.70168892
## [2,] 0.4420465 0.44439355 0.9842781 0.13028283
## [3,] 0.2061216 0.02599664 0.6319633 0.05230531
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6259316 0.9665898 0.1118647 0.552055533
## [2,] 0.1475906 0.1211446 0.7037920 0.001213329
## [3,] 0.1513015 0.2172777 0.8582678 0.037748823
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.24775538 0.03940638 0.84146113 0.02099155
## [2,] 0.01706502 0.53657791 0.05144925 0.16168415
## [3,] 0.37640014 0.26700260 0.13377764 0.06248540
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1607428 0.21283712 0.01483254 0.29874163
## [2,] 0.2477592 0.06907357 0.30947080 0.05289646
## [3,] 0.4552485 0.19223117 0.89421386 0.03730659
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.13340411 0.19178410 0.73367045 0.08685607
## [2,] 0.01192871 0.21034174 0.42087822 0.99683724
## [3,] 0.07410769 0.11739117 0.50886038 0.93418236
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.24060605 0.17509551 0.37833117 0.70168892
## [2,] 0.44204649 0.44439355 0.98427809 0.13028283
## [3,] 0.20612159 0.02599664 0.63196335 0.05230531
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.625931605 0.966589780 0.111864661 0.552055533
## [2,] 0.147590634 0.121144595 0.703792046 0.001213329
## [3,] 0.151301536 0.217277735 0.858267814 0.037748823
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.24775538 0.03940638 0.84146113 0.02099155
## [2,] 0.01706502 0.53657791 0.05144925 0.16168415
## [3,] 0.37640014 0.26700260 0.13377764 0.06248540
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.16074278 0.21283712 0.01483254 0.29874163
## [2,] 0.24775924 0.06907357 0.30947080 0.05289646
## [3,] 0.45524851 0.19223117 0.89421386 0.03730659
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.9914312 0.4208423 0.9840939
## [2,] 0.4208423 0.1786389 0.4177277
## [3,] 0.9840939 0.4177277 0.9768109
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.9914312 0.4208423 0.9840939
## [2,] 0.4208423 0.1786389 0.4177277
## [3,] 0.9840939 0.4177277 0.9768109
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1911971 0.1275278 0.0009380615 0.12966788
## [2,] 0.1587766 0.3345519 0.0343723230 0.01617424
## [3,] 0.1484703 0.1872561 0.0709131383 0.26913829
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05717333 0.03813440 0.0002805069 0.038774355
## [2,] 0.04747868 0.10004047 0.0102782945 0.004836554
## [3,] 0.04439680 0.05599485 0.0212050294 0.080479944
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1425044 0.09504992 0.0006991629 0.09664501
## [2,] 0.1183405 0.24935070 0.0256186316 0.01205510
## [3,] 0.1106589 0.13956706 0.0528534998 0.20059612
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2292466 0.1529067 0.001124742 0.15547268
## [2,] 0.1903742 0.4011301 0.041212653 0.01939302
## [3,] 0.1780169 0.2245213 0.085025344 0.32269868
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2400819 0.1601337 0.001177903 0.16282104
## [2,] 0.1993722 0.4200893 0.043160554 0.02030963
## [3,] 0.1864308 0.2351332 0.089044035 0.33795091
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1793554 0.1196294 0.000879963 0.12163695
## [2,] 0.1489428 0.3138316 0.032243486 0.01517249
## [3,] 0.1392748 0.1756584 0.066521161 0.25246932
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4483810 0.2990685 0.002199871 0.3040873
## [2,] 0.3723509 0.7845660 0.080607378 0.0379306
## [3,] 0.3481812 0.4391388 0.166300141 0.6311628
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3396056 0.2265157 0.001666192 0.2303170
## [2,] 0.2820201 0.5942336 0.061052367 0.0287288
## [3,] 0.2637139 0.3326056 0.125956426 0.4780454
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3734186 0.2490689 0.001832087 0.25324864
## [2,] 0.3100996 0.6533987 0.067131075 0.03158919
## [3,] 0.2899707 0.3657216 0.138497337 0.52564218
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1542754 0.1029011 0.0007569145 0.10462799
## [2,] 0.1281156 0.2699473 0.0277347575 0.01305087
## [3,] 0.1197995 0.1510954 0.0572192543 0.21716557
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5226474 0.3486039 0.002564241 0.35445404
## [2,] 0.4340243 0.9145155 0.093958572 0.04421314
## [3,] 0.4058513 0.5118744 0.193844833 0.73570383
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5059557 0.3374706 0.002482347 0.34313391
## [2,] 0.4201629 0.8853088 0.090957835 0.04280111
## [3,] 0.3928897 0.4955268 0.187654047 0.71220779
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2567734 0.1712669 0.001259795 0.17414102
## [2,] 0.2132334 0.4492956 0.046161249 0.02172163
## [3,] 0.1993922 0.2514806 0.095234734 0.36144661
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3480412 0.2321422 0.001707579 0.2360379
## [2,] 0.2890253 0.6089939 0.062568862 0.0294424
## [3,] 0.2702644 0.3408672 0.129085088 0.4899197
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2376613 0.1585192 0.001166027 0.16117940
## [2,] 0.1973620 0.4158538 0.042725389 0.02010486
## [3,] 0.1845511 0.2327625 0.088146251 0.33454353
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2190454 0.1461025 0.001074693 0.14855433
## [2,] 0.1819028 0.3832803 0.039378738 0.01853006
## [3,] 0.1700953 0.2145304 0.081241814 0.30833896
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3489639 0.2327577 0.001712106 0.23666371
## [2,] 0.2897916 0.6106085 0.062734748 0.02952046
## [3,] 0.2709809 0.3417710 0.129427325 0.49121855
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08440249 0.05629615 0.0004141001 0.057240893
## [2,] 0.07009071 0.14768539 0.0151733990 0.007139994
## [3,] 0.06554106 0.08266276 0.0313040622 0.118809039
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3219828 0.2147614 0.00157973 0.2183654
## [2,] 0.2673855 0.5633976 0.05788424 0.0272380
## [3,] 0.2500293 0.3153460 0.11942030 0.4532387
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5193446 0.3464009 0.002548036 0.35221408
## [2,] 0.4312815 0.9087362 0.093364804 0.04393374
## [3,] 0.4032866 0.5086396 0.192619837 0.73105457
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4161430 0.2775659 0.002041703 0.28222387
## [2,] 0.3455794 0.7281568 0.074811821 0.03520345
## [3,] 0.3231475 0.4075653 0.154343395 0.58578309
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4384994 0.2924775 0.002151389 0.29738572
## [2,] 0.3641449 0.7672754 0.078830919 0.03709467
## [3,] 0.3405079 0.4294609 0.162635149 0.61725298
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1889471 0.1260270 0.0009270226 0.1281420
## [2,] 0.1569081 0.3306150 0.0339678357 0.0159839
## [3,] 0.1467231 0.1850525 0.0700786452 0.2659711
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11972079 0.07985333 0.0005873806 0.08119340
## [2,] 0.09942023 0.20948448 0.0215227218 0.01012773
## [3,] 0.09296678 0.11725306 0.0444032760 0.16852479
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4141523 0.2762381 0.002031937 0.28087381
## [2,] 0.3439262 0.7246735 0.074453946 0.03503504
## [3,] 0.3216017 0.4056157 0.153605067 0.58298090
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2011065 0.1341373 0.0009866795 0.13638833
## [2,] 0.1670057 0.3518911 0.0361537775 0.01701252
## [3,] 0.1561652 0.1969612 0.0745884363 0.28308724
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2036190 0.1358131 0.0009990067 0.13809230
## [2,] 0.1690922 0.3562875 0.0366054669 0.01722507
## [3,] 0.1581163 0.1994220 0.0755203113 0.28662400
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5146569 0.3432743 0.002525037 0.34903494
## [2,] 0.4273886 0.9005339 0.092522079 0.04353718
## [3,] 0.3996464 0.5040486 0.190881220 0.72445596
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1822001 0.1215268 0.0008939202 0.12356624
## [2,] 0.1513052 0.3188093 0.0327549019 0.01541314
## [3,] 0.1414839 0.1784446 0.0675762557 0.25647374
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2440081 0.1627525 0.001197166 0.16548377
## [2,] 0.2026327 0.4269594 0.043866390 0.02064177
## [3,] 0.1894796 0.2389785 0.090500237 0.34347766
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1750827 0.1167795 0.00085900 0.11873924
## [2,] 0.1453946 0.3063553 0.03147536 0.01481104
## [3,] 0.1359569 0.1714738 0.06493645 0.24645484
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4391560 0.2929155 0.002154611 0.29783106
## [2,] 0.3646902 0.7684244 0.078948968 0.03715022
## [3,] 0.3410178 0.4301040 0.162878695 0.61817732
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4849624 0.3234682 0.002379349 0.32889647
## [2,] 0.4027294 0.8485752 0.087183780 0.04102519
## [3,] 0.3765878 0.4749662 0.179867839 0.68265661
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3889449 0.2594248 0.001908262 0.26377837
## [2,] 0.3229931 0.6805661 0.069922294 0.03290263
## [3,] 0.3020273 0.3809278 0.144255867 0.54749767
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01823417 0.01216212 8.946145e-05 0.012366222
## [2,] 0.01514227 0.03190569 3.278034e-03 0.001542512
## [3,] 0.01415937 0.01785832 6.762874e-03 0.025667296
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10170642 0.06783781 0.0004989976 0.068976239
## [2,] 0.08446049 0.17796338 0.0182842010 0.008603812
## [3,] 0.07897808 0.09961001 0.0377219216 0.143166891
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2605603 0.1737928 0.001278375 0.17670928
## [2,] 0.2163782 0.4559219 0.046842043 0.02204199
## [3,] 0.2023329 0.2551895 0.096639272 0.36677729
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06838330 0.04561141 0.0003355059 0.046376844
## [2,] 0.05678783 0.11965541 0.0122935602 0.005784857
## [3,] 0.05310168 0.06697376 0.0253627006 0.096259650
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3211601 0.2142126 0.001575693 0.2178074
## [2,] 0.2667023 0.5619580 0.057736330 0.0271684
## [3,] 0.2493904 0.3145402 0.119115148 0.4520805
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10391541 0.0693112 0.0005098354 0.070474350
## [2,] 0.08629490 0.1818286 0.0186813198 0.008790681
## [3,] 0.08069342 0.1017735 0.0385412127 0.146276366
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3834538 0.2557623 0.001881322 0.26005439
## [2,] 0.3184331 0.6709580 0.068935142 0.03243812
## [3,] 0.2977634 0.3755499 0.142219287 0.53976818
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2704920 0.1804172 0.001327102 0.18344486
## [2,] 0.2246258 0.4733002 0.048627509 0.02288216
## [3,] 0.2100451 0.2649165 0.100322847 0.38075765
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4801907 0.3202854 0.002355937 0.32566031
## [2,] 0.3987667 0.8402257 0.086325939 0.04062153
## [3,] 0.3728824 0.4702928 0.178098037 0.67593964
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11873704 0.07919717 0.0005825541 0.08052622
## [2,] 0.09860329 0.20776313 0.0213458677 0.01004451
## [3,] 0.09220286 0.11628959 0.0440384103 0.16714001
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1914646 0.1277062 0.0009393739 0.12984928
## [2,] 0.1589987 0.3350200 0.0344204100 0.01619687
## [3,] 0.1486780 0.1875180 0.0710123459 0.26951482
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07584360 0.05058740 0.000372108 0.051436338
## [2,] 0.06298311 0.13270925 0.013634729 0.006415957
## [3,] 0.05889482 0.07428028 0.028129651 0.106761122
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2104895 0.1403957 0.001032715 0.14275178
## [2,] 0.1747977 0.3683093 0.037840600 0.01780627
## [3,] 0.1634514 0.2061508 0.078068499 0.29629520
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1308537 0.08727893 0.0006420014 0.08874361
## [2,] 0.1086654 0.22896454 0.0235241299 0.01106951
## [3,] 0.1016118 0.12815648 0.0485323577 0.18419599
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2098758 0.1399864 0.001029704 0.14233560
## [2,] 0.1742881 0.3672355 0.037730280 0.01775436
## [3,] 0.1629749 0.2055498 0.077840900 0.29543139
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2605623 0.1737941 0.001278385 0.17671065
## [2,] 0.2163798 0.4559255 0.046842408 0.02204216
## [3,] 0.2023344 0.2551915 0.096640024 0.36678014
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3532002 0.2355832 0.00173289 0.23953670
## [2,] 0.2933095 0.6180210 0.06349632 0.02987882
## [3,] 0.2742705 0.3459199 0.13099851 0.49718172
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2415018 0.1610808 0.001184869 0.16378401
## [2,] 0.2005513 0.4225739 0.043415816 0.02042974
## [3,] 0.1875334 0.2365239 0.089570663 0.33994963
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1375792 0.09176479 0.0006749983 0.09330475
## [2,] 0.1142504 0.24073260 0.0247331964 0.01163845
## [3,] 0.1068343 0.13474332 0.0510267688 0.19366309
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2295137 0.1530848 0.001126052 0.15565379
## [2,] 0.1905960 0.4015974 0.041260661 0.01941561
## [3,] 0.1782242 0.2247829 0.085124388 0.32307458
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06375357 0.04252340 0.0003127912 0.043237008
## [2,] 0.05294314 0.11155442 0.0114612534 0.005393207
## [3,] 0.04950655 0.06243946 0.0236455782 0.089742616
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2912102 0.1942361 0.001428751 0.1974957
## [2,] 0.2418309 0.5095523 0.052352101 0.0246348
## [3,] 0.2261334 0.2852076 0.108007009 0.4099215
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4950139 0.3301724 0.002428664 0.33571326
## [2,] 0.4110764 0.8661630 0.088990773 0.04187549
## [3,] 0.3843931 0.4848105 0.183595825 0.69680553
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2861176 0.1908394 0.001403766 0.194042
## [2,] 0.2376018 0.5006415 0.051436588 0.024204
## [3,] 0.2221789 0.2802200 0.106118224 0.402753
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12039543 0.08030331 0.0005906906 0.08165093
## [2,] 0.09998047 0.21066493 0.0216440033 0.01018480
## [3,] 0.09349065 0.11791379 0.0446534905 0.16947444
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10110892 0.06743928 0.000496066 0.068571015
## [2,] 0.08396429 0.17691788 0.018176784 0.008553266
## [3,] 0.07851410 0.09902482 0.037500311 0.142325809
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.1911970913 0.1275277600 0.0009380615 0.1296678786
## [2,] 0.1587765912 0.3345519348 0.0343723230 0.0161742380
## [3,] 0.1484702578 0.1872560651 0.0709131383 0.2691382913
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.0571733256 0.0381343989 0.0002805069 0.0387743548
## [2,] 0.0474786812 0.1000404691 0.0102782945 0.0048365536
## [3,] 0.0443967967 0.0559948476 0.0212050294 0.0804799438
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.1425044169 0.0950499244 0.0006991629 0.0966450133
## [2,] 0.1183405323 0.2493506993 0.0256186316 0.0120551015
## [3,] 0.1106589404 0.1395670624 0.0528534998 0.2005961232
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.286117585 0.190839382 0.001403766 0.194041970
## [2,] 0.237601809 0.500641463 0.051436588 0.024203997
## [3,] 0.222178859 0.280220022 0.106118224 0.402752978
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.1203954261 0.0803033085 0.0005906906 0.0816509257
## [2,] 0.0999804716 0.2106649348 0.0216440033 0.0101848007
## [3,] 0.0934906480 0.1179137905 0.0446534905 0.1694744364
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.101108915 0.067439276 0.000496066 0.068571015
## [2,] 0.083964294 0.176917875 0.018176784 0.008553266
## [3,] 0.078514096 0.099024820 0.037500311 0.142325809
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.406701
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 2.406701
einsum::einsum('ij->', arrC)
## [1] 4.569487
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 4.569487
einsum::einsum('ijk->', arrE)
## [1] 29.90887
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 29.90887
einsum::einsum('ij->i', arrC)
## [1] 1.230217 1.489068 1.850203
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 1.230217 1.489068 1.850203
einsum::einsum('ij->j', arrC)
## [1] 1.3646828 1.7778074 0.2908279 1.1361691
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 1.3646828 1.7778074 0.2908279 1.1361691
einsum::einsum('ijk->i', arrE)
## [1] 10.457018 9.544494 9.907361
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 10.457018 9.544494 9.907361
einsum::einsum('ijk->j', arrE)
## [1] 6.735689 6.893186 9.854220 6.425778
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 6.735689 6.893186 9.854220 6.425778
einsum::einsum('ijk->k', arrE)
## [1] 6.464180 6.685170 6.433591 4.996494 5.329438
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 6.464180 6.685170 6.433591 4.996494 5.329438
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 2.545597 2.499382 2.845195 2.566844
## [2,] 1.786647 2.468650 3.262908 2.026289
## [3,] 2.403444 1.925154 3.746118 1.832645
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 2.545597 2.499382 2.845195 2.566844
## [2,] 1.786647 2.468650 3.262908 2.026289
## [3,] 2.403444 1.925154 3.746118 1.832645
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.7466912 1.609388 1.5643086 1.2418981 1.5734027
## [2,] 1.2391858 1.246307 1.7973419 1.4477481 1.1626030
## [3,] 2.2186414 2.402155 2.0998123 1.5098924 1.6237187
## [4,] 2.2596620 1.427319 0.9721278 0.7969551 0.9697141
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.7466912 1.6093880 1.5643086 1.2418981 1.5734027
## [2,] 1.2391858 1.2463074 1.7973419 1.4477481 1.1626030
## [3,] 2.2186414 2.4021554 2.0998123 1.5098924 1.6237187
## [4,] 2.2596620 1.4273194 0.9721278 0.7969551 0.9697141
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.7466912 1.609388 1.5643086 1.2418981 1.5734027
## [2,] 1.2391858 1.246307 1.7973419 1.4477481 1.1626030
## [3,] 2.2186414 2.402155 2.0998123 1.5098924 1.6237187
## [4,] 2.2596620 1.427319 0.9721278 0.7969551 0.9697141
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.7466912 1.6093880 1.5643086 1.2418981 1.5734027
## [2,] 1.2391858 1.2463074 1.7973419 1.4477481 1.1626030
## [3,] 2.2186414 2.4021554 2.0998123 1.5098924 1.6237187
## [4,] 2.2596620 1.4273194 0.9721278 0.7969551 0.9697141
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.826751
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 1.826751
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.7316243 0.8843474 0.05058377
## [2,] 0.4978037 0.8009470 0.13984586
## [3,] 0.2630879 0.5065913 0.29418011
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.73162432 0.88434736 0.05058377
## [2,] 0.49780370 0.80094698 0.13984586
## [3,] 0.26308792 0.50659130 0.29418011
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.5975829 0.2482910 0.3460442
## [2,] 0.9442503 0.1820156 0.8805966
## [3,] 0.5718831 0.7632111 0.7605240
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.2671846 0.6058313 0.8241276
## [2,] 0.8998193 0.7688202 0.9303579
## [3,] 0.1987412 0.3429332 0.4882122
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.4696357 0.1781605 0.5544263
## [2,] 0.1861006 0.6271419 0.1515466
## [3,] 0.5420160 0.6631194 0.5936452
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.5975829 0.2482910 0.3460442
## [2,] 0.9442503 0.1820156 0.8805966
## [3,] 0.5718831 0.7632111 0.7605240
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.2671846 0.6058313 0.8241276
## [2,] 0.8998193 0.7688202 0.9303579
## [3,] 0.1987412 0.3429332 0.4882122
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.4696357 0.1781605 0.5544263
## [2,] 0.1861006 0.6271419 0.1515466
## [3,] 0.5420160 0.6631194 0.5936452
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] 2.146881
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 2.146881
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 2.569521
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 2.569521
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 19.02954
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 19.02954
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.2194405 0.8887741 0.9248238 0.6412205 0.8637505
## [2,] 0.5195170 0.6454857 1.3050121 0.8429869 0.4741419
## [3,] 1.6634091 1.9945726 1.6739245 1.0266880 1.2185172
## [4,] 2.0178757 0.8842771 0.5910177 0.2451611 0.3889447
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.2194405 0.8887741 0.9248238 0.6412205 0.8637505
## [2,] 0.5195170 0.6454857 1.3050121 0.8429869 0.4741419
## [3,] 1.6634091 1.9945726 1.6739245 1.0266880 1.2185172
## [4,] 2.0178757 0.8842771 0.5910177 0.2451611 0.3889447
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 0.5219801 0.5633395 0.6538970
## [2,] 0.5633395 1.0387841 0.6972127
## [3,] 0.6538970 0.6972127 1.0087571
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.5219801 0.5633395 0.6538970
## [2,] 0.5633395 1.0387841 0.6972127
## [3,] 0.6538970 0.6972127 1.0087571
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 2.740270e-01 0.188974734 0.16523792
## [2,] 1.219103e-01 0.838992128 0.26284673
## [3,] 6.596194e-06 0.008856223 0.03769504
## [4,] 1.260363e-01 0.001961004 0.54297743
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 2.740270e-01 1.889747e-01 1.652379e-01
## [2,] 1.219103e-01 8.389921e-01 2.628467e-01
## [3,] 6.596194e-06 8.856223e-03 3.769504e-02
## [4,] 1.260363e-01 1.961004e-03 5.429774e-01
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.13340411 0.2406060 0.6259316 0.24775538 0.16074278
## [2,] 0.19178410 0.1750955 0.9665898 0.03940638 0.21283712
## [3,] 0.73367045 0.3783312 0.1118647 0.84146113 0.01483254
## [4,] 0.08685607 0.7016889 0.5520555 0.02099155 0.29874163
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.01192871 0.4420465 0.147590634 0.01706502 0.24775924
## [2,] 0.21034174 0.4443935 0.121144595 0.53657791 0.06907357
## [3,] 0.42087822 0.9842781 0.703792046 0.05144925 0.30947080
## [4,] 0.99683724 0.1302828 0.001213329 0.16168415 0.05289646
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.07410769 0.20612159 0.15130154 0.3764001 0.45524851
## [2,] 0.11739117 0.02599664 0.21727774 0.2670026 0.19223117
## [3,] 0.50886038 0.63196335 0.85826781 0.1337776 0.89421386
## [4,] 0.93418236 0.05230531 0.03774882 0.0624854 0.03730659
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.13340411 0.24060605 0.62593160 0.24775538 0.16074278
## [2,] 0.19178410 0.17509551 0.96658978 0.03940638 0.21283712
## [3,] 0.73367045 0.37833117 0.11186466 0.84146113 0.01483254
## [4,] 0.08685607 0.70168892 0.55205553 0.02099155 0.29874163
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.011928712 0.442046487 0.147590634 0.017065019 0.247759238
## [2,] 0.210341736 0.444393549 0.121144595 0.536577909 0.069073574
## [3,] 0.420878223 0.984278092 0.703792046 0.051449252 0.309470798
## [4,] 0.996837239 0.130282832 0.001213329 0.161684153 0.052896461
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.07410769 0.20612159 0.15130154 0.37640014 0.45524851
## [2,] 0.11739117 0.02599664 0.21727774 0.26700260 0.19223117
## [3,] 0.50886038 0.63196335 0.85826781 0.13377764 0.89421386
## [4,] 0.93418236 0.05230531 0.03774882 0.06248540 0.03730659
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 1.954436 2.215018 2.294727
## [2,] 2.361715 2.684549 1.638906
## [3,] 2.851778 1.605990 1.975824
## [4,] 1.758457 1.492072 1.745965
## [5,] 1.530632 1.546866 2.251941
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.954436 2.215018 2.294727
## [2,] 2.361715 2.684549 1.638906
## [3,] 2.851778 1.605990 1.975824
## [4,] 1.758457 1.492072 1.745965
## [5,] 1.530632 1.546866 2.251941
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,] 3.639937e-02 6.564946e-02 1.707857e-01 6.760016e-02 4.385874e-02
## [2,] 2.328006e-02 2.125429e-02 1.173313e-01 4.783415e-03 2.583562e-02
## [3,] 4.818654e-06 2.484831e-06 7.347128e-07 5.526609e-06 9.741826e-08
## [4,] 1.090001e-02 8.805854e-02 6.928028e-02 2.634337e-03 3.749062e-02
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0009527640 0.0353069123 1.178828e-02 0.001363009 1.978890e-02
## [2,] 0.0745884175 0.1575845681 4.295859e-02 0.190273685 2.449390e-02
## [3,] 0.0015754081 0.0036842953 2.634395e-03 0.000192582 1.158394e-03
## [4,] 0.0008262106 0.0001079826 1.005646e-06 0.000134009 4.384228e-05
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.01210259 0.033661886 0.02470918 0.061470216 0.07434701
## [2,] 0.03049603 0.006753439 0.05644468 0.069362272 0.04993805
## [3,] 0.01895781 0.023544059 0.03197513 0.004983942 0.03331431
## [4,] 0.50132421 0.028069381 0.02025771 0.033532473 0.02002039
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,] 3.639937e-02 6.564946e-02 1.707857e-01 6.760016e-02 4.385874e-02
## [2,] 2.328006e-02 2.125429e-02 1.173313e-01 4.783415e-03 2.583562e-02
## [3,] 4.818654e-06 2.484831e-06 7.347128e-07 5.526609e-06 9.741826e-08
## [4,] 1.090001e-02 8.805854e-02 6.928028e-02 2.634337e-03 3.749062e-02
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 9.527640e-04 3.530691e-02 1.178828e-02 1.363009e-03 1.978890e-02
## [2,] 7.458842e-02 1.575846e-01 4.295859e-02 1.902737e-01 2.449390e-02
## [3,] 1.575408e-03 3.684295e-03 2.634395e-03 1.925820e-04 1.158394e-03
## [4,] 8.262106e-04 1.079826e-04 1.005646e-06 1.340090e-04 4.384228e-05
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.012102588 0.033661886 0.024709178 0.061470216 0.074347007
## [2,] 0.030496026 0.006753439 0.056444684 0.069362272 0.049938055
## [3,] 0.018957806 0.023544059 0.031975126 0.004983942 0.033314311
## [4,] 0.501324210 0.028069381 0.020257714 0.033532473 0.020020391
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.5.0 RC (2025-04-04 r88126)
## 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.1
##
## 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.16.0
## [3] HDF5Array_1.36.0 h5mread_1.0.0
## [5] rhdf5_2.52.0 DelayedArray_0.34.0
## [7] SparseArray_1.8.0 S4Arrays_1.8.0
## [9] abind_1.4-8 IRanges_2.42.0
## [11] S4Vectors_0.46.0 MatrixGenerics_1.20.0
## [13] matrixStats_1.5.0 BiocGenerics_0.54.0
## [15] generics_0.1.3 Matrix_1.7-3
## [17] DelayedTensor_1.14.0 BiocStyle_2.36.0
##
## loaded via a namespace (and not attached):
## [1] dqrng_0.4.1 sass_0.4.10 lattice_0.22-7
## [4] digest_0.6.37 evaluate_1.0.3 grid_4.5.0
## [7] bookdown_0.43 fastmap_1.2.0 jsonlite_2.0.0
## [10] BiocManager_1.30.25 codetools_0.2-20 jquerylib_0.1.4
## [13] cli_3.6.4 rlang_1.1.6 crayon_1.5.3
## [16] XVector_0.48.0 cachem_1.1.0 yaml_2.3.10
## [19] tools_4.5.0 beachmat_2.24.0 parallel_4.5.0
## [22] BiocParallel_1.42.0 Rhdf5lib_1.30.0 rsvd_1.0.5
## [25] R6_2.6.1 lifecycle_1.0.4 BiocSingular_1.24.0
## [28] irlba_2.3.5.1 ScaledMatrix_1.16.0 rTensor_1.4.8
## [31] bslib_0.9.0 Rcpp_1.0.14 xfun_0.52
## [34] knitr_1.50 rhdf5filters_1.20.0 htmltools_0.5.8.1
## [37] rmarkdown_2.29 compiler_4.5.0