DelayedTensor 1.15.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2025-04-04 14:56:50
Compiled: Wed May 28 16:06:37 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.9914102 0.8797658 0.6894638
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.9914102 0.8797658 0.6894638
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.1744562 0.6180522 0.7393752 0.9601383
## [2,] 0.2025904 0.5605428 0.6339879 0.6645837
## [3,] 0.9571888 0.7284406 0.6445248 0.2920488
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.1744562 0.6180522 0.7393752 0.9601383
## [2,] 0.2025904 0.5605428 0.6339879 0.6645837
## [3,] 0.9571888 0.7284406 0.6445248 0.2920488
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4175247 0.1246709 0.2293108 0.8051342
## [2,] 0.6411451 0.6317525 0.7016734 0.1835734
## [3,] 0.1882543 0.5786584 0.5121425 0.7641402
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9546939 0.9110632 0.3046850 0.5060249
## [2,] 0.9172658 0.3590456 0.1874781 0.1926499
## [3,] 0.6575393 0.9832491 0.4990047 0.3202248
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.86270026 0.2840163 0.1315727 0.8805401
## [2,] 0.71226258 0.4627567 0.6191737 0.8857155
## [3,] 0.01130099 0.8890350 0.3213122 0.8362699
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.002049455 0.5905102 0.1391612 0.9845227
## [2,] 0.705613736 0.6489962 0.8443546 0.3462096
## [3,] 0.477396049 0.4229138 0.5508625 0.7495819
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.80041436 0.1240761 0.5977351 0.005040878
## [2,] 0.56898466 0.8513476 0.5826569 0.167806462
## [3,] 0.06304595 0.8422464 0.4890701 0.920603547
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.4175247 0.1246709 0.2293108 0.8051342
## [2,] 0.6411451 0.6317525 0.7016734 0.1835734
## [3,] 0.1882543 0.5786584 0.5121425 0.7641402
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.9546939 0.9110632 0.3046850 0.5060249
## [2,] 0.9172658 0.3590456 0.1874781 0.1926499
## [3,] 0.6575393 0.9832491 0.4990047 0.3202248
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.86270026 0.28401627 0.13157267 0.88054011
## [2,] 0.71226258 0.46275671 0.61917370 0.88571550
## [3,] 0.01130099 0.88903498 0.32131220 0.83626986
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.002049455 0.590510235 0.139161228 0.984522714
## [2,] 0.705613736 0.648996230 0.844354627 0.346209557
## [3,] 0.477396049 0.422913795 0.550862521 0.749581869
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.800414358 0.124076123 0.597735073 0.005040878
## [2,] 0.568984662 0.851347583 0.582656917 0.167806462
## [3,] 0.063045951 0.842246412 0.489070072 0.920603547
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.5594540 0.3646904 0.3003220
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.5594540 0.3646904 0.3003220
einsum::einsum('iii->i', arrD)
## [1] 0.98949366 0.04131143 0.53483701
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.98949366 0.04131143 0.53483701
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.9828941 0.7739879 0.4753604
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.9828941 0.7739879 0.4753604
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.03043498 0.3819885 0.5466757 0.92186547
## [2,] 0.04104285 0.3142082 0.4019407 0.44167147
## [3,] 0.91621041 0.5306258 0.4154123 0.08529251
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.03043498 0.38198852 0.54667573 0.92186547
## [2,] 0.04104285 0.31420824 0.40194065 0.44167147
## [3,] 0.91621041 0.53062578 0.41541227 0.08529251
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.17432688 0.01554284 0.05258343 0.64824107
## [2,] 0.41106704 0.39911126 0.49234550 0.03369918
## [3,] 0.03543968 0.33484550 0.26228996 0.58391030
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9114404 0.8300362 0.09283297 0.2560612
## [2,] 0.8413765 0.1289137 0.03514805 0.0371140
## [3,] 0.4323580 0.9667788 0.24900568 0.1025439
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7442517455 0.08066524 0.01731137 0.7753509
## [2,] 0.5073179795 0.21414377 0.38337607 0.7844919
## [3,] 0.0001277123 0.79038319 0.10324153 0.6993473
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 4.200266e-06 0.3487023 0.01936585 0.9692850
## [2,] 4.978907e-01 0.4211961 0.71293474 0.1198611
## [3,] 2.279070e-01 0.1788561 0.30344952 0.5618730
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.640663144 0.01539488 0.3572872 2.541045e-05
## [2,] 0.323743546 0.72479271 0.3394891 2.815901e-02
## [3,] 0.003974792 0.70937902 0.2391895 8.475109e-01
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.17432688 0.01554284 0.05258343 0.64824107
## [2,] 0.41106704 0.39911126 0.49234550 0.03369918
## [3,] 0.03543968 0.33484550 0.26228996 0.58391030
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.91144043 0.83003616 0.09283297 0.25606120
## [2,] 0.84137654 0.12891374 0.03514805 0.03711400
## [3,] 0.43235798 0.96677881 0.24900568 0.10254390
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.7442517455 0.0806652438 0.0173113684 0.7753508938
## [2,] 0.5073179795 0.2141437715 0.3833760716 0.7844919416
## [3,] 0.0001277123 0.7903831948 0.1032415269 0.6993472778
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 4.200266e-06 3.487023e-01 1.936585e-02 9.692850e-01
## [2,] 4.978907e-01 4.211961e-01 7.129347e-01 1.198611e-01
## [3,] 2.279070e-01 1.788561e-01 3.034495e-01 5.618730e-01
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 6.406631e-01 1.539488e-02 3.572872e-01 2.541045e-05
## [2,] 3.237435e-01 7.247927e-01 3.394891e-01 2.815901e-02
## [3,] 3.974792e-03 7.093790e-01 2.391895e-01 8.475109e-01
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.9828941 0.8722088 0.6835415
## [2,] 0.8722088 0.7739879 0.6065667
## [3,] 0.6835415 0.6065667 0.4753604
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.9828941 0.8722088 0.6835415
## [2,] 0.8722088 0.7739879 0.6065667
## [3,] 0.6835415 0.6065667 0.4753604
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07283979 0.2580521 0.3087074 0.4008814
## [2,] 0.08458648 0.2340405 0.2647056 0.2774801
## [3,] 0.39964998 0.3041420 0.2691050 0.1219376
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1118518 0.3962611 0.4740468 0.6155879
## [2,] 0.1298898 0.3593893 0.4064782 0.4260946
## [3,] 0.6136969 0.4670362 0.4132339 0.1872457
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03284214 0.1163510 0.1391906 0.18075014
## [2,] 0.03813850 0.1055246 0.1193509 0.12511073
## [3,] 0.18019490 0.1371321 0.1213346 0.05497944
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02174962 0.07705314 0.09217860 0.11970133
## [2,] 0.02525713 0.06988339 0.07903986 0.08285427
## [3,] 0.11933362 0.09081537 0.08035351 0.03641000
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1102132 0.3904560 0.4671022 0.6065698
## [2,] 0.1279870 0.3541243 0.4005235 0.4198524
## [3,] 0.6047065 0.4601942 0.4071802 0.1845026
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1009506 0.3576411 0.4278457 0.5555920
## [2,] 0.1172306 0.3243628 0.3668624 0.3845669
## [3,] 0.5538853 0.4215183 0.3729597 0.1689965
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04000470 0.1417260 0.1695467 0.22017005
## [2,] 0.04645615 0.1285385 0.1453803 0.15239620
## [3,] 0.21949371 0.1670393 0.1477965 0.06696994
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1224113 0.4336708 0.5187999 0.6737034
## [2,] 0.1421523 0.3933180 0.4448524 0.4663207
## [3,] 0.6716339 0.5111274 0.4522459 0.2049229
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08934646 0.3165308 0.3786655 0.4917276
## [2,] 0.10375514 0.2870778 0.3246922 0.3403616
## [3,] 0.49021709 0.3730654 0.3300886 0.1495706
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1404607 0.4976150 0.5952963 0.7730401
## [2,] 0.1631124 0.4513122 0.5104453 0.5350790
## [3,] 0.7706654 0.5864925 0.5189290 0.2351385
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03202552 0.1134579 0.1357296 0.17625581
## [2,] 0.03719019 0.1029007 0.1163833 0.12199986
## [3,] 0.17571437 0.1337223 0.1183176 0.05361238
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1333090 0.4722786 0.5649864 0.7336803
## [2,] 0.1548074 0.4283333 0.4844557 0.5078351
## [3,] 0.7314265 0.5566308 0.4925074 0.2231662
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1665523 0.5900507 0.7058770 0.9166381
## [2,] 0.1934118 0.5351468 0.6052644 0.6344740
## [3,] 0.9138223 0.6954378 0.6153239 0.2788172
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1600227 0.5669181 0.6782036 0.8807020
## [2,] 0.1858292 0.5141667 0.5815354 0.6095999
## [3,] 0.8779966 0.6681737 0.5912006 0.2678864
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1147118 0.4063936 0.4861683 0.6313287
## [2,] 0.1332111 0.3685789 0.4168720 0.4369899
## [3,] 0.6293893 0.4789784 0.4238004 0.1920336
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1589407 0.5630846 0.6736176 0.8747466
## [2,] 0.1845726 0.5106899 0.5776030 0.6054777
## [3,] 0.8720595 0.6636555 0.5872029 0.2660749
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06263775 0.2219089 0.2654694 0.3447334
## [2,] 0.07273918 0.2012604 0.2276306 0.2386158
## [3,] 0.34367443 0.2615434 0.2314138 0.1048588
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1715339 0.6076993 0.7269900 0.9440551
## [2,] 0.1991968 0.5511532 0.6233680 0.6534513
## [3,] 0.9411550 0.7162386 0.6337285 0.2871567
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05315421 0.1883113 0.2252766 0.2925398
## [2,] 0.06172625 0.1707890 0.1931666 0.2024887
## [3,] 0.29164111 0.2219450 0.1963771 0.0889829
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03270673 0.1158713 0.1386167 0.18000493
## [2,] 0.03798126 0.1050895 0.1188589 0.12459491
## [3,] 0.17945197 0.1365667 0.1208343 0.05475277
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08705448 0.3084109 0.3689517 0.4791135
## [2,] 0.10109354 0.2797135 0.3163629 0.3316304
## [3,] 0.47764171 0.3634953 0.3216209 0.1457337
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0882792 0.3127498 0.3741423 0.4858539
## [2,] 0.1025158 0.2836486 0.3208137 0.3362959
## [3,] 0.4843614 0.3686091 0.3261456 0.1477840
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03360899 0.1190677 0.1424406 0.18497058
## [2,] 0.03902902 0.1079885 0.1221377 0.12803201
## [3,] 0.18440237 0.1403340 0.1241677 0.05626319
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05586521 0.1979156 0.2367663 0.30746005
## [2,] 0.06487445 0.1794997 0.2030186 0.21281616
## [3,] 0.30651557 0.2332647 0.2063928 0.09352126
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1505034 0.5331938 0.6378592 0.8283115
## [2,] 0.1747748 0.4835804 0.5469415 0.5733365
## [3,] 0.8257670 0.6284259 0.5560318 0.2519506
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1242587 0.4402155 0.5266293 0.6838705
## [2,] 0.1442975 0.3992537 0.4515659 0.4733581
## [3,] 0.6817698 0.5188410 0.4590709 0.2080154
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.001971528 0.006984601 0.008355671 0.010850511
## [2,] 0.002289471 0.006334688 0.007164690 0.007510453
## [3,] 0.010817180 0.008232099 0.007283768 0.003300440
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04954841 0.1755369 0.2099946 0.27269489
## [2,] 0.05753896 0.1592033 0.1800629 0.18875258
## [3,] 0.27185720 0.2068890 0.1830555 0.08294661
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08073080 0.2860078 0.3421508 0.4443104
## [2,] 0.09375005 0.2593949 0.2933822 0.3075406
## [3,] 0.44294554 0.3370908 0.2982582 0.1351475
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1550977 0.5494700 0.6573304 0.8535965
## [2,] 0.1801099 0.4983422 0.5636374 0.5908381
## [3,] 0.8509743 0.6476092 0.5730051 0.2596416
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02295367 0.08131878 0.09728158 0.12632796
## [2,] 0.02665536 0.07375212 0.08341548 0.08744105
## [3,] 0.12593989 0.09584288 0.08480186 0.03842564
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1080187 0.3826817 0.4578017 0.5944924
## [2,] 0.1254386 0.3470734 0.3925486 0.4114927
## [3,] 0.5926661 0.4510313 0.3990728 0.1808289
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05605492 0.1985877 0.2375703 0.30850413
## [2,] 0.06509475 0.1801092 0.2037080 0.21353884
## [3,] 0.30755644 0.2340569 0.2070937 0.09383884
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1536157 0.5442198 0.6510495 0.8454402
## [2,] 0.1783889 0.4935804 0.5582518 0.5851926
## [3,] 0.8428431 0.6414212 0.5675300 0.2571607
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1545186 0.5474184 0.6548761 0.8504093
## [2,] 0.1794374 0.4964815 0.5615329 0.5886321
## [3,] 0.8477970 0.6451912 0.5708656 0.2586722
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1458925 0.5168584 0.6183172 0.8029347
## [2,] 0.1694202 0.4687651 0.5301850 0.5557713
## [3,] 0.8004681 0.6091730 0.5389967 0.2442316
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0003575402 0.001266670 0.001515316 0.0019677601
## [2,] 0.0004151998 0.001148807 0.001299330 0.0013620343
## [3,] 0.0019617153 0.001492906 0.001320925 0.0005985409
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1230987 0.4361061 0.5217133 0.6774867
## [2,] 0.1429505 0.3955267 0.4473506 0.4689394
## [3,] 0.6754056 0.5139977 0.4547856 0.2060737
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08328472 0.2950557 0.3529748 0.4583662
## [2,] 0.09671584 0.2676009 0.3026633 0.3172696
## [3,] 0.45695815 0.3477547 0.3076936 0.1394229
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1030182 0.3649661 0.4366086 0.5669715
## [2,] 0.1196317 0.3310063 0.3743763 0.3924435
## [3,] 0.5652298 0.4301517 0.3805985 0.1724578
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1132214 0.4011135 0.4798517 0.6231261
## [2,] 0.1314804 0.3637902 0.4114558 0.4313123
## [3,] 0.6212119 0.4727552 0.4182942 0.1895386
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07377995 0.2613828 0.3126920 0.4060557
## [2,] 0.08567826 0.2370613 0.2681222 0.2810616
## [3,] 0.40480835 0.3080676 0.2725784 0.1235115
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02427755 0.08600890 0.10289237 0.13361402
## [2,] 0.02819272 0.07800583 0.08822653 0.09248428
## [3,] 0.13320357 0.10137070 0.08969287 0.04064187
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1473029 0.5218552 0.6242949 0.8106972
## [2,] 0.1710581 0.4732969 0.5353106 0.5611443
## [3,] 0.8082068 0.6150622 0.5442075 0.2465928
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09610141 0.3404618 0.4072941 0.5289042
## [2,] 0.11159944 0.3087820 0.3492402 0.3660942
## [3,] 0.52727944 0.4012707 0.3550446 0.1608787
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1717561 0.6084864 0.7279317 0.9452779
## [2,] 0.1994548 0.5518671 0.6241755 0.6542977
## [3,] 0.9423741 0.7171664 0.6345493 0.2875287
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06039842 0.2139756 0.2559788 0.3324090
## [2,] 0.07013872 0.1940653 0.2194927 0.2300852
## [3,] 0.33138791 0.2521931 0.2231407 0.1011101
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1307692 0.4632807 0.5542223 0.7197022
## [2,] 0.1518581 0.4201727 0.4752258 0.4981599
## [3,] 0.7174914 0.5460259 0.4831241 0.2189145
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1396373 0.4946979 0.5918065 0.7685084
## [2,] 0.1621562 0.4486665 0.5074530 0.5319423
## [3,] 0.7661477 0.5830544 0.5158869 0.2337601
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09926293 0.3516622 0.4206932 0.5463039
## [2,] 0.11527081 0.3189403 0.3607294 0.3781379
## [3,] 0.54462575 0.4144716 0.3667247 0.1661713
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01099876 0.03896569 0.04661461 0.06053283
## [2,] 0.01277250 0.03533995 0.03997037 0.04189931
## [3,] 0.06034688 0.04592523 0.04063468 0.01841249
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02164585 0.07668552 0.09173881 0.11913023
## [2,] 0.02513663 0.06954998 0.07866276 0.08245897
## [3,] 0.11876428 0.09038209 0.07997014 0.03623628
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1485229 0.5261772 0.6294653 0.8174114
## [2,] 0.1724748 0.4772168 0.5397441 0.5657917
## [3,] 0.8149004 0.6201562 0.5487147 0.2486350
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1469351 0.5205522 0.6227361 0.8086730
## [2,] 0.1706310 0.4721152 0.5339740 0.5597432
## [3,] 0.8061888 0.6135265 0.5428487 0.2459771
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1042786 0.3694315 0.4419505 0.5739083
## [2,] 0.1210954 0.3350561 0.3789568 0.3972450
## [3,] 0.5721453 0.4354145 0.3852551 0.1745678
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1016481 0.3601124 0.4308021 0.5594312
## [2,] 0.1180407 0.3266041 0.3693974 0.3872243
## [3,] 0.5577127 0.4244310 0.3755369 0.1701643
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08532133 0.3022708 0.3616063 0.4695749
## [2,] 0.09908088 0.2741447 0.3100645 0.3250280
## [3,] 0.46813240 0.3562585 0.3152178 0.1428323
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0008794126 0.003115526 0.003727100 0.004839940
## [2,] 0.0010212333 0.002825628 0.003195856 0.003350085
## [3,] 0.0048250720 0.003671980 0.003248971 0.001472182
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02927488 0.10371315 0.1240719 0.16111740
## [2,] 0.03399597 0.09406271 0.1063873 0.11152144
## [3,] 0.16062247 0.12223705 0.1081554 0.04900768
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1606050 0.5689810 0.6806715 0.8839067
## [2,] 0.1865054 0.5160377 0.5836515 0.6118181
## [3,] 0.8811914 0.6706050 0.5933519 0.2688612
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.07283979 0.25805206 0.30870743 0.40088145
## [2,] 0.08458648 0.23404047 0.26470561 0.27748011
## [3,] 0.39964998 0.30414197 0.26910505 0.12193759
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.1118518 0.3962611 0.4740468 0.6155879
## [2,] 0.1298898 0.3593893 0.4064782 0.4260946
## [3,] 0.6136969 0.4670362 0.4132339 0.1872457
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.03284214 0.11635098 0.13919056 0.18075014
## [2,] 0.03813850 0.10552459 0.11935094 0.12511073
## [3,] 0.18019490 0.13713207 0.12133456 0.05497944
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.0008794126 0.0031155257 0.0037271003 0.0048399398
## [2,] 0.0010212333 0.0028256279 0.0031958556 0.0033500852
## [3,] 0.0048250720 0.0036719804 0.0032489711 0.0014721824
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.02927488 0.10371315 0.12407194 0.16111740
## [2,] 0.03399597 0.09406271 0.10638727 0.11152144
## [3,] 0.16062247 0.12223705 0.10815543 0.04900768
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.1606050 0.5689810 0.6806715 0.8839067
## [2,] 0.1865054 0.5160377 0.5836515 0.6118181
## [3,] 0.8811914 0.6706050 0.5933519 0.2688612
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.56064
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 2.56064
einsum::einsum('ij->', arrC)
## [1] 7.17593
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 7.17593
einsum::einsum('ijk->', arrE)
## [1] 31.94276
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 31.94276
einsum::einsum('ij->i', arrC)
## [1] 2.492022 2.061705 2.622203
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 2.492022 2.061705 2.622203
einsum::einsum('ij->j', arrC)
## [1] 1.334235 1.907036 2.017888 1.916771
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 1.334235 1.907036 2.017888 1.916771
einsum::einsum('ijk->i', arrE)
## [1] 9.655447 11.210462 11.076852
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 9.655447 11.210462 11.076852
einsum::einsum('ijk->j', arrE)
## [1] 7.980191 8.704338 6.710194 8.548038
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 7.980191 8.704338 6.710194 8.548038
einsum::einsum('ijk->k', arrE)
## [1] 5.777980 6.792924 6.896656 6.462172 6.013028
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 5.777980 6.792924 6.896656 6.462172 6.013028
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 3.037383 2.034337 1.402465 3.181263
## [2,] 3.545272 2.953899 2.935337 1.775955
## [3,] 1.397537 3.716103 2.372392 3.590820
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 3.037383 2.034337 1.402465 3.181263
## [2,] 3.545272 2.953899 2.935337 1.775955
## [3,] 1.397537 3.716103 2.372392 3.590820
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.246924 2.5294990 1.586264 1.185059 1.432445
## [2,] 1.335082 2.2533579 1.635808 1.662420 1.817670
## [3,] 1.443127 0.9911679 1.072059 1.534378 1.669462
## [4,] 1.752848 1.0188996 2.602525 2.080314 1.093451
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.2469241 2.5294990 1.5862638 1.1850592 1.4324450
## [2,] 1.3350818 2.2533579 1.6358080 1.6624203 1.8176701
## [3,] 1.4431267 0.9911679 1.0720586 1.5343784 1.6694621
## [4,] 1.7528478 1.0188996 2.6025255 2.0803141 1.0934509
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.246924 2.5294990 1.586264 1.185059 1.432445
## [2,] 1.335082 2.2533579 1.635808 1.662420 1.817670
## [3,] 1.443127 0.9911679 1.072059 1.534378 1.669462
## [4,] 1.752848 1.0188996 2.602525 2.080314 1.093451
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.2469241 2.5294990 1.5862638 1.1850592 1.4324450
## [2,] 1.3350818 2.2533579 1.6358080 1.6624203 1.8176701
## [3,] 1.4431267 0.9911679 1.0720586 1.5343784 1.6694621
## [4,] 1.7528478 1.0188996 2.6025255 2.0803141 1.0934509
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.224466
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 1.224466
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.5594540 0.2546372 0.24364906
## [2,] 0.3681325 0.3646904 0.04955275
## [3,] 0.8353648 0.2712386 0.30032199
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.55945397 0.25463723 0.24364906
## [2,] 0.36813251 0.36469045 0.04955275
## [3,] 0.83536479 0.27123856 0.30032199
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.9894937 0.3220280 0.6087646
## [2,] 0.2467925 0.4456187 0.9662087
## [3,] 0.8048473 0.7992862 0.7682028
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.5874383 0.67475672 0.06863921
## [2,] 0.1402102 0.04131143 0.36502121
## [3,] 0.9143184 0.01315154 0.25418622
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.6492236 0.5400341 0.1526263
## [2,] 0.8412032 0.1817668 0.5346183
## [3,] 0.6152483 0.9243389 0.5348370
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.9894937 0.3220280 0.6087646
## [2,] 0.2467925 0.4456187 0.9662087
## [3,] 0.8048473 0.7992862 0.7682028
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.58743826 0.67475672 0.06863921
## [2,] 0.14021019 0.04131143 0.36502121
## [3,] 0.91431844 0.01315154 0.25418622
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.6492236 0.5400341 0.1526263
## [2,] 0.8412032 0.1817668 0.5346183
## [3,] 0.6152483 0.9243389 0.5348370
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.232242
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 2.232242
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 5.027369
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 5.027369
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 22.01796
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 22.01796
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.6208336 2.1851750 1.251697 0.7258019 0.9683815
## [2,] 0.7494996 1.9257287 1.085192 0.9487545 1.4495666
## [3,] 0.8072189 0.3769867 0.503929 1.0357501 0.9359658
## [4,] 1.2658505 0.3957191 2.259190 1.6510190 0.8756953
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.6208336 2.1851750 1.2516974 0.7258019 0.9683815
## [2,] 0.7494996 1.9257287 1.0851922 0.9487545 1.4495666
## [3,] 0.8072189 0.3769867 0.5039290 1.0357501 0.9359658
## [4,] 1.2658505 0.3957191 2.2591901 1.6510190 0.8756953
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.880965 1.488635 1.374155
## [2,] 1.488635 1.198863 1.204951
## [3,] 1.374155 1.204951 1.947541
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.880965 1.488635 1.374155
## [2,] 1.488635 1.198863 1.204951
## [3,] 1.374155 1.204951 1.947541
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.03043498 0.04104285 0.91621041
## [2,] 0.38198852 0.31420824 0.53062578
## [3,] 0.54667573 0.40194065 0.41541227
## [4,] 0.92186547 0.44167147 0.08529251
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.03043498 0.04104285 0.91621041
## [2,] 0.38198852 0.31420824 0.53062578
## [3,] 0.54667573 0.40194065 0.41541227
## [4,] 0.92186547 0.44167147 0.08529251
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.17432688 0.91144043 0.74425175 4.200266e-06 6.406631e-01
## [2,] 0.01554284 0.83003616 0.08066524 3.487023e-01 1.539488e-02
## [3,] 0.05258343 0.09283297 0.01731137 1.936585e-02 3.572872e-01
## [4,] 0.64824107 0.25606120 0.77535089 9.692850e-01 2.541045e-05
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.41106704 0.84137654 0.5073180 0.4978907 0.32374355
## [2,] 0.39911126 0.12891374 0.2141438 0.4211961 0.72479271
## [3,] 0.49234550 0.03514805 0.3833761 0.7129347 0.33948908
## [4,] 0.03369918 0.03711400 0.7844919 0.1198611 0.02815901
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.03543968 0.4323580 0.0001277123 0.2279070 0.003974792
## [2,] 0.33484550 0.9667788 0.7903831948 0.1788561 0.709379019
## [3,] 0.26228996 0.2490057 0.1032415269 0.3034495 0.239189535
## [4,] 0.58391030 0.1025439 0.6993472778 0.5618730 0.847510892
DelayedTensor::einsum('ijk,ijk->jki', darrE, darrE)
## <4 x 5 x 3> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.743269e-01 9.114404e-01 7.442517e-01 4.200266e-06 6.406631e-01
## [2,] 1.554284e-02 8.300362e-01 8.066524e-02 3.487023e-01 1.539488e-02
## [3,] 5.258343e-02 9.283297e-02 1.731137e-02 1.936585e-02 3.572872e-01
## [4,] 6.482411e-01 2.560612e-01 7.753509e-01 9.692850e-01 2.541045e-05
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.41106704 0.84137654 0.50731798 0.49789074 0.32374355
## [2,] 0.39911126 0.12891374 0.21414377 0.42119611 0.72479271
## [3,] 0.49234550 0.03514805 0.38337607 0.71293474 0.33948908
## [4,] 0.03369918 0.03711400 0.78449194 0.11986106 0.02815901
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0354396763 0.4323579802 0.0001277123 0.2279069872 0.0039747919
## [2,] 0.3348454958 0.9667788094 0.7903831948 0.1788560783 0.7093790190
## [3,] 0.2622899629 0.2490056832 0.1032415269 0.3034495172 0.2391895352
## [4,] 0.5839103009 0.1025439031 0.6993472778 0.5618729780 0.8475108916
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 1.576641 2.158144 2.043195
## [2,] 2.676467 1.656439 2.460018
## [3,] 2.158829 2.679908 2.057918
## [4,] 1.716244 2.545174 2.200754
## [5,] 1.527266 2.170796 2.314966
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.576641 2.158144 2.043195
## [2,] 2.676467 1.656439 2.460018
## [3,] 2.158829 2.679908 2.057918
## [4,] 1.716244 2.545174 2.200754
## [5,] 1.527266 2.170796 2.314966
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.005260061 0.02750139 0.022456717 1.267369e-07 0.0193310812
## [2,] 0.005886188 0.31434076 0.030548517 1.320561e-01 0.0058301551
## [3,] 0.028499162 0.05031360 0.009382413 1.049590e-02 0.1936424837
## [4,] 0.592457855 0.23402632 0.708629476 8.858749e-01 0.0000232238
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.01484285 0.03038051 0.01831829 0.01797789 0.01168976
## [2,] 0.11032619 0.03563558 0.05919569 0.11643110 0.20035421
## [3,] 0.17410009 0.01242883 0.13556701 0.25210345 0.12004797
## [4,] 0.01309440 0.01442129 0.30482804 0.04657411 0.01094167
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.02238703 0.273117918 8.067511e-05 0.14396746 0.002510852
## [2,] 0.12250232 0.353693402 2.891595e-01 0.06543401 0.259524387
## [3,] 0.07512292 0.071318150 2.956959e-02 0.08691150 0.068506690
## [4,] 0.03433749 0.006030207 4.112589e-02 0.03304156 0.049838809
DelayedTensor::einsum('i,ij,ijk,ijk,ji->jki',
darrA, darrC, darrE, darrE, t(darrC))
## <4 x 5 x 3> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 5.260061e-03 2.750139e-02 2.245672e-02 1.267369e-07 1.933108e-02
## [2,] 5.886188e-03 3.143408e-01 3.054852e-02 1.320561e-01 5.830155e-03
## [3,] 2.849916e-02 5.031360e-02 9.382413e-03 1.049590e-02 1.936425e-01
## [4,] 5.924579e-01 2.340263e-01 7.086295e-01 8.858749e-01 2.322380e-05
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.01484285 0.03038051 0.01831829 0.01797789 0.01168976
## [2,] 0.11032619 0.03563558 0.05919569 0.11643110 0.20035421
## [3,] 0.17410009 0.01242883 0.13556701 0.25210345 0.12004797
## [4,] 0.01309440 0.01442129 0.30482804 0.04657411 0.01094167
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.238703e-02 2.731179e-01 8.067511e-05 1.439675e-01 2.510852e-03
## [2,] 1.225023e-01 3.536934e-01 2.891595e-01 6.543401e-02 2.595244e-01
## [3,] 7.512292e-02 7.131815e-02 2.956959e-02 8.691150e-02 6.850669e-02
## [4,] 3.433749e-02 6.030207e-03 4.112589e-02 3.304156e-02 4.983881e-02
einsum
By using einsum
and other DelayedTensor functions,
it is possible to implement your original tensor calculation functions.
It is intended to be applied to Delayed Arrays,
which can scale to large-scale data
since the calculation is performed internally by block processing.
For example, kronecker
can be easily implmented by eimsum
and other DelayedTensor functions4 https://stackoverflow.com/
questions/56067643/speeding-up-kronecker-products-numpy
(the kronecker
function inside DelayedTensor
has a more efficient implementation though).
darr1 <- DelayedArray(array(1:6, dim=c(2,3)))
darr2 <- DelayedArray(array(20:1, dim=c(4,5)))
mykronecker <- function(darr1, darr2){
stopifnot((length(dim(darr1)) == 2) && (length(dim(darr2)) == 2))
# Outer Product
tmpdarr <- DelayedTensor::einsum('ij,kl->ikjl', darr1, darr2)
# Reshape
DelayedTensor::unfold(tmpdarr, row_idx=c(2,1), col_idx=c(4,3))
}
identical(as.array(DelayedTensor::kronecker(darr1, darr2)),
as.array(mykronecker(darr1, darr2)))
## [1] TRUE
## R version 4.5.0 Patched (2025-04-21 r88169)
## Platform: aarch64-apple-darwin20
## Running under: macOS Ventura 13.7.1
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.5-arm64/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.17.0
## [3] HDF5Array_1.37.0 h5mread_1.1.1
## [5] rhdf5_2.53.0 DelayedArray_0.35.1
## [7] SparseArray_1.9.0 S4Arrays_1.9.1
## [9] abind_1.4-8 IRanges_2.43.0
## [11] S4Vectors_0.47.0 MatrixGenerics_1.21.0
## [13] matrixStats_1.5.0 BiocGenerics_0.55.0
## [15] generics_0.1.4 Matrix_1.7-3
## [17] DelayedTensor_1.15.0 BiocStyle_2.37.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.5 rlang_1.1.6 crayon_1.5.3
## [16] XVector_0.49.0 cachem_1.1.0 yaml_2.3.10
## [19] tools_4.5.0 beachmat_2.25.0 parallel_4.5.0
## [22] BiocParallel_1.43.2 Rhdf5lib_1.31.0 rsvd_1.0.5
## [25] R6_2.6.1 lifecycle_1.0.4 BiocSingular_1.25.0
## [28] irlba_2.3.5.1 ScaledMatrix_1.17.0 rTensor_1.4.8
## [31] bslib_0.9.0 Rcpp_1.0.14 xfun_0.52
## [34] knitr_1.50 rhdf5filters_1.21.0 htmltools_0.5.8.1
## [37] rmarkdown_2.29 compiler_4.5.0