DelayedTensor 1.13.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2024-10-23 23:57:56
Compiled: Tue Nov 26 16:04:35 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.9167061 0.8632098 0.1566262
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.9167061 0.8632098 0.1566262
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.6312817 0.6423088 0.6090662 0.4607712
## [2,] 0.8522331 0.8940300 0.2234062 0.5907031
## [3,] 0.7594057 0.8002512 0.2113825 0.0587795
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.6312817 0.6423088 0.6090662 0.4607712
## [2,] 0.8522331 0.8940300 0.2234062 0.5907031
## [3,] 0.7594057 0.8002512 0.2113825 0.0587795
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.77098544 0.7836149 0.1798794 0.3116886
## [2,] 0.02025643 0.3130834 0.1633947 0.1627920
## [3,] 0.53493197 0.6934909 0.5387595 0.8117845
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3484838 0.5970235 0.4270169 0.4211090
## [2,] 0.3452606 0.6406762 0.1609200 0.2055338
## [3,] 0.7819580 0.7819250 0.2932192 0.7916236
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1060492 0.2179726 0.9098632 0.4909895
## [2,] 0.7393907 0.8727129 0.9800169 0.2131993
## [3,] 0.9421523 0.0655498 0.4646191 0.1207160
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3043122 0.8200102 0.7547190 0.004307311
## [2,] 0.9929960 0.4431276 0.8435935 0.601773907
## [3,] 0.5730438 0.9879850 0.2608555 0.360961790
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0195338 0.70200728 0.3505923 0.9570638
## [2,] 0.2909377 0.07706136 0.5049999 0.1344599
## [3,] 0.3145689 0.25832025 0.0895727 0.4276671
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.77098544 0.78361486 0.17987937 0.31168861
## [2,] 0.02025643 0.31308338 0.16339467 0.16279199
## [3,] 0.53493197 0.69349091 0.53875952 0.81178450
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.3484838 0.5970235 0.4270169 0.4211090
## [2,] 0.3452606 0.6406762 0.1609200 0.2055338
## [3,] 0.7819580 0.7819250 0.2932192 0.7916236
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.1060492 0.2179726 0.9098632 0.4909895
## [2,] 0.7393907 0.8727129 0.9800169 0.2131993
## [3,] 0.9421523 0.0655498 0.4646191 0.1207160
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.304312173 0.820010161 0.754718979 0.004307311
## [2,] 0.992996048 0.443127628 0.843593532 0.601773907
## [3,] 0.573043847 0.987984993 0.260855534 0.360961790
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.01953380 0.70200728 0.35059229 0.95706375
## [2,] 0.29093771 0.07706136 0.50499988 0.13445989
## [3,] 0.31456894 0.25832025 0.08957270 0.42766713
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.1031317 0.6963489 0.2131121
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.1031317 0.6963489 0.2131121
einsum::einsum('iii->i', arrD)
## [1] 0.8596804 0.4076907 0.1168591
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.8596804 0.4076907 0.1168591
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.84035003 0.74513116 0.02453177
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.84035003 0.74513116 0.02453177
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.3985166 0.4125606 0.37096164 0.212310132
## [2,] 0.7263013 0.7992897 0.04991031 0.348930196
## [3,] 0.5766970 0.6404019 0.04468254 0.003455029
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.398516579 0.412560567 0.370961644 0.212310132
## [2,] 0.726301319 0.799289704 0.049910311 0.348930196
## [3,] 0.576697018 0.640401923 0.044682543 0.003455029
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5944185521 0.6140523 0.03235659 0.09714979
## [2,] 0.0004103228 0.0980212 0.02669782 0.02650123
## [3,] 0.2861522099 0.4809296 0.29026182 0.65899407
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1214410 0.3564371 0.18234347 0.17733275
## [2,] 0.1192049 0.4104660 0.02589523 0.04224415
## [3,] 0.6114583 0.6114067 0.08597748 0.62666790
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01124643 0.047512075 0.8278510 0.24107069
## [2,] 0.54669856 0.761627890 0.9604331 0.04545395
## [3,] 0.88765097 0.004296776 0.2158709 0.01457234
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0926059 0.6724167 0.56960074 1.855293e-05
## [2,] 0.9860412 0.1963621 0.71165005 3.621318e-01
## [3,] 0.3283793 0.9761143 0.06804561 1.302934e-01
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0003815692 0.492814218 0.122914951 0.91597102
## [2,] 0.0846447491 0.005938454 0.255024880 0.01807946
## [3,] 0.0989536154 0.066729354 0.008023269 0.18289917
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.5944185521 0.6140522534 0.0323565879 0.0971497892
## [2,] 0.0004103228 0.0980212012 0.0266978194 0.0265012320
## [3,] 0.2861522099 0.4809296476 0.2902618182 0.6589940677
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.12144097 0.35643708 0.18234347 0.17733275
## [2,] 0.11920487 0.41046599 0.02589523 0.04224415
## [3,] 0.61145833 0.61140675 0.08597748 0.62666790
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.011246432 0.047512075 0.827850983 0.241070690
## [2,] 0.546698565 0.761627890 0.960433121 0.045453954
## [3,] 0.887650969 0.004296776 0.215870941 0.014572344
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 9.260590e-02 6.724167e-01 5.696007e-01 1.855293e-05
## [2,] 9.860412e-01 1.963621e-01 7.116500e-01 3.621318e-01
## [3,] 3.283793e-01 9.761143e-01 6.804561e-02 1.302934e-01
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.0003815692 0.4928142175 0.1229149511 0.9159710236
## [2,] 0.0846447491 0.0059384539 0.2550248800 0.0180794618
## [3,] 0.0989536154 0.0667293535 0.0080232688 0.1828991711
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.8403500 0.7913097 0.14358019
## [2,] 0.7913097 0.7451312 0.13520127
## [3,] 0.1435802 0.1352013 0.02453177
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.84035003 0.79130967 0.14358019
## [2,] 0.79130967 0.74513116 0.13520127
## [3,] 0.14358019 0.13520127 0.02453177
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4867090 0.4952107 0.4695812 0.35524792
## [2,] 0.6570593 0.6892841 0.1722429 0.45542352
## [3,] 0.5854907 0.6169820 0.1629728 0.04531814
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01278751 0.01301088 0.012337504 0.009333578
## [2,] 0.01726320 0.01810985 0.004525410 0.011965534
## [3,] 0.01538284 0.01621023 0.004281853 0.001190662
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3376928 0.3435915 0.3258090 0.24648126
## [2,] 0.4558867 0.4782452 0.1195071 0.31598599
## [3,] 0.4062304 0.4280799 0.1130752 0.03144303
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4946817 0.5033227 0.4772733 0.36106719
## [2,] 0.6678226 0.7005752 0.1750644 0.46288376
## [3,] 0.5950816 0.6270887 0.1656424 0.04606049
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1976438 0.2010962 0.19068851 0.14425981
## [2,] 0.2668200 0.2799059 0.06994475 0.18493933
## [3,] 0.2377573 0.2505453 0.06618033 0.01840288
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4377881 0.4454353 0.4223819 0.31954067
## [2,] 0.5910159 0.6200017 0.1549301 0.40964726
## [3,] 0.5266410 0.5549669 0.1465918 0.04076305
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1135546 0.1155381 0.10955845 0.08288324
## [2,] 0.1532992 0.1608176 0.04018616 0.10625531
## [3,] 0.1366014 0.1439487 0.03802334 0.01057322
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1031481 0.1049498 0.09951817 0.075287566
## [2,] 0.1392504 0.1460797 0.03650338 0.096517746
## [3,] 0.1240828 0.1307568 0.03453877 0.009604257
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3401090 0.3460500 0.3281402 0.24824489
## [2,] 0.4591487 0.4816672 0.1203622 0.31824694
## [3,] 0.4091370 0.4311429 0.1138843 0.03166801
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1967633 0.2002003 0.18983900 0.1436171
## [2,] 0.2656314 0.2786590 0.06963315 0.1841154
## [3,] 0.2366981 0.2494292 0.06588550 0.0183209
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1027676 0.1045627 0.09915110 0.075009866
## [2,] 0.1387367 0.1455409 0.03636873 0.096161739
## [3,] 0.1236252 0.1302745 0.03441137 0.009568831
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5124647 0.5214163 0.4944305 0.37404695
## [2,] 0.6918296 0.7257597 0.1813577 0.47952365
## [3,] 0.6164738 0.6496315 0.1715970 0.04771629
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2199915 0.2238342 0.21224972 0.1605713
## [2,] 0.2969895 0.3115550 0.07785343 0.2058505
## [3,] 0.2646406 0.2788746 0.07366337 0.0204837
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2179567 0.2217639 0.21028656 0.15908615
## [2,] 0.2942425 0.3086733 0.07713334 0.20394651
## [3,] 0.2621929 0.2762952 0.07298203 0.02029424
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4936358 0.5022585 0.4762642 0.3603038
## [2,] 0.6664105 0.6990939 0.1746942 0.4619051
## [3,] 0.5938234 0.6257628 0.1652922 0.0459631
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3768900 0.3834734 0.3636268 0.27509126
## [2,] 0.5088032 0.5337570 0.1333787 0.35266367
## [3,] 0.4533831 0.4777688 0.1262003 0.03509274
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4044472 0.4115119 0.3902142 0.29520516
## [2,] 0.5460055 0.5727838 0.1431310 0.37844944
## [3,] 0.4865332 0.5127019 0.1354277 0.03765863
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4936150 0.5022373 0.4762441 0.36028856
## [2,] 0.6663824 0.6990645 0.1746869 0.46188557
## [3,] 0.5937983 0.6257364 0.1652852 0.04596116
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2695680 0.2742767 0.26008159 0.19675713
## [2,] 0.3639180 0.3817660 0.09539822 0.25224025
## [3,] 0.3242791 0.3417208 0.09026389 0.02509984
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1015858 0.1033603 0.09801091 0.074147291
## [2,] 0.1371413 0.1438673 0.03595051 0.095055927
## [3,] 0.1222035 0.1287764 0.03401566 0.009458795
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1851039 0.1883372 0.17858989 0.13510696
## [2,] 0.2498911 0.2621467 0.06550697 0.17320548
## [3,] 0.2226723 0.2346490 0.06198139 0.01723528
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2658384 0.2704820 0.25648323 0.19403489
## [2,] 0.3588830 0.3764841 0.09407833 0.24875038
## [3,] 0.3197925 0.3369929 0.08901504 0.02475257
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1297497 0.1320162 0.12518370 0.09470407
## [2,] 0.1751627 0.1837534 0.04591752 0.12140947
## [3,] 0.1560836 0.1644787 0.04344624 0.01208117
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4997375 0.5084668 0.4821512 0.36475738
## [2,] 0.6746478 0.7077353 0.1768536 0.46761453
## [3,] 0.6011635 0.6334977 0.1673353 0.04653124
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06694692 0.06811633 0.06459098 0.048864418
## [2,] 0.09037864 0.09481117 0.02369204 0.062643592
## [3,] 0.08053436 0.08486599 0.02241694 0.006233518
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4667638 0.4749171 0.4503379 0.34068995
## [2,] 0.6301332 0.6610375 0.1651844 0.43676039
## [3,] 0.5614975 0.5916982 0.1562942 0.04346101
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5947635 0.6051527 0.5738331 0.43411668
## [2,] 0.8029334 0.8423125 0.2104826 0.55653232
## [3,] 0.7154758 0.7539585 0.1991545 0.05537924
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1376021 0.1400057 0.13275977 0.10043553
## [2,] 0.1857635 0.1948741 0.04869643 0.12875713
## [3,] 0.1655297 0.1744329 0.04607559 0.01281232
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5509277 0.5605512 0.5315400 0.40212102
## [2,] 0.7437549 0.7802316 0.1949694 0.51551428
## [3,] 0.6627432 0.6983896 0.1844762 0.05129763
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04138039 0.04210321 0.03992417 0.030203460
## [2,] 0.05586371 0.05860349 0.01464423 0.038720470
## [3,] 0.04977889 0.05245630 0.01385608 0.003852984
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5743800 0.5844131 0.5541669 0.4192388
## [2,] 0.7754155 0.8134450 0.2032690 0.5374590
## [3,] 0.6909553 0.7281191 0.1923291 0.0534813
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6186667 0.6294735 0.5968952 0.4515636
## [2,] 0.8352029 0.8761645 0.2189418 0.5788991
## [3,] 0.7442304 0.7842597 0.2071584 0.0576049
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2933056 0.2984289 0.28298381 0.21408313
## [2,] 0.3959638 0.4153835 0.10379878 0.27445198
## [3,] 0.3528344 0.3718120 0.09821233 0.02731008
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3099527 0.3153669 0.2990451 0.22623384
## [2,] 0.4184375 0.4389594 0.1096901 0.29002904
## [3,] 0.3728602 0.3929149 0.1037866 0.02886012
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1345888 0.1369398 0.12985251 0.09823612
## [2,] 0.1816955 0.1906066 0.04763004 0.12593751
## [3,] 0.1619048 0.1706130 0.04506660 0.01253175
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07620578 0.07753692 0.07352402 0.055622445
## [2,] 0.10287815 0.10792370 0.02696869 0.071307300
## [3,] 0.09167239 0.09660309 0.02551724 0.007095624
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1921067 0.1954624 0.18534626 0.14021830
## [2,] 0.2593449 0.2720642 0.06798521 0.17975816
## [3,] 0.2310964 0.2435262 0.06432625 0.01788732
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6268602 0.6378101 0.6048003 0.45754402
## [2,] 0.8462641 0.8877683 0.2218414 0.58656588
## [3,] 0.7540869 0.7946462 0.2099019 0.05836781
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3617521 0.3680711 0.3490216 0.26404212
## [2,] 0.4883670 0.5123184 0.1280215 0.33849880
## [3,] 0.4351728 0.4585790 0.1211314 0.03368323
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5176574 0.5266997 0.4994405 0.37783710
## [2,] 0.6988398 0.7331137 0.1831953 0.48438257
## [3,] 0.6227204 0.6562141 0.1733358 0.04819979
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2797384 0.2846248 0.26989406 0.20418046
## [2,] 0.3776480 0.3961694 0.09899744 0.26175688
## [3,] 0.3365136 0.3546134 0.09366941 0.02604682
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6236968 0.6345914 0.6017483 0.45523507
## [2,] 0.8419935 0.8832883 0.2207219 0.58360583
## [3,] 0.7502814 0.7906361 0.2088427 0.05807326
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4764403 0.4847626 0.4596738 0.3477528
## [2,] 0.6431965 0.6747414 0.1686089 0.4458149
## [3,] 0.5731379 0.6039647 0.1595344 0.0443620
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5325452 0.5418475 0.5138043 0.3887036
## [2,] 0.7189384 0.7541980 0.1884640 0.4983133
## [3,] 0.6406297 0.6750867 0.1783209 0.0495860
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1646733 0.1675498 0.15887829 0.12019473
## [2,] 0.2223097 0.2332127 0.05827673 0.15408818
## [3,] 0.1980952 0.2087499 0.05514028 0.01533296
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.002719127 0.002766624 0.0026234379 0.0019846852
## [2,] 0.003670834 0.003850866 0.0009622799 0.0025443424
## [3,] 0.003270997 0.003446931 0.0009104901 0.0002531816
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3798889 0.3865247 0.3665202 0.27728011
## [2,] 0.5128517 0.5380039 0.1344400 0.35546974
## [3,] 0.4569905 0.4815703 0.1272044 0.03537197
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2278686 0.2318489 0.21984963 0.16632081
## [2,] 0.3076236 0.3227107 0.08064109 0.21322126
## [3,] 0.2741164 0.2888601 0.07630099 0.02121715
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01233133 0.01254673 0.011897375 0.009000612
## [2,] 0.01664735 0.01746380 0.004363970 0.011538675
## [3,] 0.01483408 0.01563194 0.004129102 0.001148187
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1836636 0.1868718 0.17720033 0.13405573
## [2,] 0.2479468 0.2601070 0.06499728 0.17185782
## [3,] 0.2209398 0.2328232 0.06149913 0.01710117
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1985816 0.2020504 0.19159331 0.1449443
## [2,] 0.2680861 0.2812341 0.07027664 0.1858169
## [3,] 0.2388854 0.2517342 0.06649435 0.0184902
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4431643 0.4509054 0.4275689 0.32346476
## [2,] 0.5982739 0.6276156 0.1568327 0.41467790
## [3,] 0.5331083 0.5617821 0.1483920 0.04126364
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04864743 0.04949719 0.04693547 0.035507660
## [2,] 0.06567425 0.06889517 0.01721598 0.045520390
## [3,] 0.05852084 0.06166845 0.01628942 0.004529628
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1630728 0.1659214 0.15733414 0.11902654
## [2,] 0.2201491 0.2309461 0.05771034 0.15259058
## [3,] 0.1961699 0.2067211 0.05460437 0.01518393
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2213225 0.2251885 0.21353391 0.16154284
## [2,] 0.2987864 0.3134400 0.07832448 0.20709596
## [3,] 0.2662418 0.2805619 0.07410906 0.02060764
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3187972 0.3243659 0.3075784 0.23268942
## [2,] 0.4303776 0.4514851 0.1128201 0.29830501
## [3,] 0.3834998 0.4041267 0.1067481 0.02968364
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05654561 0.05753333 0.05455571 0.041272524
## [2,] 0.07633682 0.08008069 0.02001109 0.052910876
## [3,] 0.06802202 0.07168066 0.01893410 0.005265038
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6041768 0.6147304 0.5829152 0.44098745
## [2,] 0.8156414 0.8556437 0.2138139 0.56534056
## [3,] 0.7267997 0.7658914 0.2023065 0.05625573
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08488207 0.08636477 0.08189497 0.061955249
## [2,] 0.11459117 0.12021118 0.03003917 0.079425878
## [3,] 0.10210961 0.10760168 0.02842246 0.007903485
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2699784 0.2746943 0.26047759 0.19705671
## [2,] 0.3644721 0.3823473 0.09554347 0.25262431
## [3,] 0.3247729 0.3422411 0.09040133 0.02513806
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.48670900 0.49521072 0.46958118 0.35524792
## [2,] 0.65705934 0.68928414 0.17224290 0.45542352
## [3,] 0.58549074 0.61698200 0.16297280 0.04531814
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.012787510 0.013010880 0.012337504 0.009333578
## [2,] 0.017263197 0.018109852 0.004525410 0.011965534
## [3,] 0.015382845 0.016210228 0.004281853 0.001190662
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.33769276 0.34359150 0.32580898 0.24648126
## [2,] 0.45588675 0.47824525 0.11950710 0.31598599
## [3,] 0.40623039 0.42807993 0.11307523 0.03144303
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.60417683 0.61473045 0.58291519 0.44098745
## [2,] 0.81564144 0.85564374 0.21381394 0.56534056
## [3,] 0.72679967 0.76589138 0.20230649 0.05625573
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.084882067 0.086364767 0.081894975 0.061955249
## [2,] 0.114591173 0.120211179 0.030039167 0.079425878
## [3,] 0.102109606 0.107601682 0.028422462 0.007903485
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.26997843 0.27469435 0.26047759 0.19705671
## [2,] 0.36447210 0.38234726 0.09554347 0.25262431
## [3,] 0.32477285 0.34224112 0.09040133 0.02513806
If there is a vanishing subscript on the left or right side of ->, the summation is done for that subscript.
einsum::einsum('i->', arrA)
## [1] 1.936542
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.936542
einsum::einsum('ij->', arrC)
## [1] 6.733619
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 6.733619
einsum::einsum('ijk->', arrE)
## [1] 28.27711
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 28.27711
einsum::einsum('ij->i', arrC)
## [1] 2.343428 2.560372 1.829819
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 2.343428 2.560372 1.829819
einsum::einsum('ij->j', arrC)
## [1] 2.242921 2.336590 1.043855 1.110254
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 2.242921 2.336590 1.043855 1.110254
einsum::einsum('ijk->i', arrE)
## [1] 9.477222 8.706187 10.093705
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 9.477222 8.706187 10.093705
einsum::einsum('ijk->j', arrE)
## [1] 7.084861 8.254561 6.922022 6.015670
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 7.084861 8.254561 6.922022 6.015670
einsum::einsum('ijk->k', arrE)
## [1] 5.284662 5.794750 6.123232 6.947686 4.126785
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 5.284662 5.794750 6.123232 6.947686 4.126785
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 1.549364 3.120628 2.622071 2.185158
## [2,] 2.388841 2.346662 2.652925 1.317759
## [3,] 3.146655 2.787271 1.647026 2.512753
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 1.549364 3.120628 2.622071 2.185158
## [2,] 2.388841 2.346662 2.652925 1.317759
## [3,] 3.146655 2.787271 1.647026 2.512753
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.3261738 1.4757024 1.7875922 1.870352 0.6250404
## [2,] 1.7901892 2.0196247 1.1562354 2.251123 1.0373889
## [3,] 0.8820336 0.8811561 2.3544992 1.859168 0.9451649
## [4,] 1.2862651 1.4182664 0.8249048 0.967043 1.5191908
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.3261738 1.4757024 1.7875922 1.8703521 0.6250404
## [2,] 1.7901892 2.0196247 1.1562354 2.2511228 1.0373889
## [3,] 0.8820336 0.8811561 2.3544992 1.8591680 0.9451649
## [4,] 1.2862651 1.4182664 0.8249048 0.9670430 1.5191908
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.3261738 1.4757024 1.7875922 1.870352 0.6250404
## [2,] 1.7901892 2.0196247 1.1562354 2.251123 1.0373889
## [3,] 0.8820336 0.8811561 2.3544992 1.859168 0.9451649
## [4,] 1.2862651 1.4182664 0.8249048 0.967043 1.5191908
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.3261738 1.4757024 1.7875922 1.8703521 0.6250404
## [2,] 1.7901892 2.0196247 1.1562354 2.2511228 1.0373889
## [3,] 0.8820336 0.8811561 2.3544992 1.8591680 0.9451649
## [4,] 1.2862651 1.4182664 0.8249048 0.9670430 1.5191908
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.012593
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 1.012593
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.10313167 0.2847531 0.3800403
## [2,] 0.88459185 0.6963489 0.6942805
## [3,] 0.04399886 0.8446211 0.2131121
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.10313167 0.28475310 0.38004025
## [2,] 0.88459185 0.69634892 0.69428054
## [3,] 0.04399886 0.84462106 0.21311206
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.8596804 0.3888013 0.7192713
## [2,] 0.6526655 0.1981879 0.9992846
## [3,] 0.1346271 0.7982726 0.7708364
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.81177714 0.4308224 0.2558253
## [2,] 0.95006430 0.4076907 0.7414118
## [3,] 0.06030578 0.2045338 0.3280642
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.8700244 0.7570645 0.01740769
## [2,] 0.3270129 0.7614262 0.91130773
## [3,] 0.6651432 0.3770154 0.11685910
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.8596804 0.3888013 0.7192713
## [2,] 0.6526655 0.1981879 0.9992846
## [3,] 0.1346271 0.7982726 0.7708364
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.81177714 0.43082245 0.25582527
## [2,] 0.95006430 0.40769065 0.74141178
## [3,] 0.06030578 0.20453377 0.32806424
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.87002436 0.75706452 0.01740769
## [2,] 0.32701292 0.76142622 0.91130773
## [3,] 0.66514316 0.37701541 0.11685910
Some examples of combining Multiplication and Summation are shown below.
Inner Product first calculate Hadamard Product and collapses it to 0D tensor (norm).
einsum::einsum('i,i->', arrA, arrA)
## [1] 1.610013
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.610013
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 4.584017
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 4.584017
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 18.48714
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 18.48714
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.8809811 0.8521042 1.4455960 1.4070263 0.1839799
## [2,] 1.1930031 1.3783098 0.8134367 1.8448931 0.5654820
## [3,] 0.3493162 0.2942162 2.0041550 1.3492964 0.3859631
## [4,] 0.7826451 0.8462448 0.3010970 0.4924438 1.1169497
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.8809811 0.8521042 1.4455960 1.4070263 0.1839799
## [2,] 1.1930031 1.3783098 0.8134367 1.8448931 0.5654820
## [3,] 0.3493162 0.2942162 2.0041550 1.3492964 0.3859631
## [4,] 0.7826451 0.8462448 0.3010970 0.4924438 1.1169497
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.394349 1.520491 1.149237
## [2,] 1.520491 1.924432 1.444585
## [3,] 1.149237 1.444585 1.265237
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.394349 1.520491 1.149237
## [2,] 1.520491 1.924432 1.444585
## [3,] 1.149237 1.444585 1.265237
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.3985166 0.72630132 0.576697018
## [2,] 0.4125606 0.79928970 0.640401923
## [3,] 0.3709616 0.04991031 0.044682543
## [4,] 0.2123101 0.34893020 0.003455029
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.398516579 0.726301319 0.576697018
## [2,] 0.412560567 0.799289704 0.640401923
## [3,] 0.370961644 0.049910311 0.044682543
## [4,] 0.212310132 0.348930196 0.003455029
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.59441855 0.1214410 0.01124643 9.260590e-02 0.0003815692
## [2,] 0.61405225 0.3564371 0.04751208 6.724167e-01 0.4928142175
## [3,] 0.03235659 0.1823435 0.82785098 5.696007e-01 0.1229149511
## [4,] 0.09714979 0.1773327 0.24107069 1.855293e-05 0.9159710236
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0004103228 0.11920487 0.54669856 0.9860412 0.084644749
## [2,] 0.0980212012 0.41046599 0.76162789 0.1963621 0.005938454
## [3,] 0.0266978194 0.02589523 0.96043312 0.7116500 0.255024880
## [4,] 0.0265012320 0.04224415 0.04545395 0.3621318 0.018079462
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.2861522 0.61145833 0.887650969 0.32837925 0.098953615
## [2,] 0.4809296 0.61140675 0.004296776 0.97611435 0.066729354
## [3,] 0.2902618 0.08597748 0.215870941 0.06804561 0.008023269
## [4,] 0.6589941 0.62666790 0.014572344 0.13029341 0.182899171
DelayedTensor::einsum('ijk,ijk->jki', darrE, darrE)
## <4 x 5 x 3> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 5.944186e-01 1.214410e-01 1.124643e-02 9.260590e-02 3.815692e-04
## [2,] 6.140523e-01 3.564371e-01 4.751208e-02 6.724167e-01 4.928142e-01
## [3,] 3.235659e-02 1.823435e-01 8.278510e-01 5.696007e-01 1.229150e-01
## [4,] 9.714979e-02 1.773327e-01 2.410707e-01 1.855293e-05 9.159710e-01
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0004103228 0.1192048736 0.5466985647 0.9860411517 0.0846447491
## [2,] 0.0980212012 0.4104659872 0.7616278898 0.1963620949 0.0059384539
## [3,] 0.0266978194 0.0258952348 0.9604331211 0.7116500476 0.2550248800
## [4,] 0.0265012320 0.0422441510 0.0454539539 0.3621318357 0.0180794618
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.286152210 0.611458333 0.887650969 0.328379251 0.098953615
## [2,] 0.480929648 0.611406750 0.004296776 0.976114346 0.066729354
## [3,] 0.290261818 0.085977482 0.215870941 0.068045610 0.008023269
## [4,] 0.658994068 0.626667899 0.014572344 0.130293414 0.182899171
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 2.046168 0.6595265 2.578967
## [2,] 1.793633 1.3523906 2.648726
## [3,] 1.724875 2.8053198 1.593037
## [4,] 1.883349 2.8814911 2.182846
## [5,] 2.029197 1.0074588 1.090129
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 2.0461683 0.6595265 2.5789669
## [2,] 1.7936332 1.3523906 2.6487258
## [3,] 1.7248745 2.8053198 1.5930372
## [4,] 1.8833486 2.8814911 2.1828462
## [5,] 2.0291971 1.0074588 1.0901290
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.21715451 0.04436513 0.004108575 3.383102e-02 0.0001393958
## [2,] 0.23223258 0.13480336 0.017968914 2.543058e-01 0.1863807497
## [3,] 0.01100327 0.06200823 0.281521318 1.937000e-01 0.0417988016
## [4,] 0.01890787 0.03451356 0.046918621 3.610883e-06 0.1782717654
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.000257252 0.074735545 0.34275289 0.61819891 0.053068061
## [2,] 0.067630189 0.283202939 0.52548875 0.13548095 0.004097264
## [3,] 0.001150224 0.001115646 0.04137841 0.03066007 0.010987255
## [4,] 0.007982170 0.012723937 0.01369073 0.10907410 0.005445533
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0258469453 0.0552305017 8.017784e-02 2.966114e-02 8.938071e-03
## [2,] 0.0482390329 0.0613263716 4.309826e-04 9.790790e-02 6.693202e-03
## [3,] 0.0020313848 0.0006017097 1.510763e-03 4.762143e-04 5.615050e-05
## [4,] 0.0003566134 0.0003391202 7.885797e-06 7.050804e-05 9.897554e-05
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,] 2.171545e-01 4.436513e-02 4.108575e-03 3.383102e-02 1.393958e-04
## [2,] 2.322326e-01 1.348034e-01 1.796891e-02 2.543058e-01 1.863807e-01
## [3,] 1.100327e-02 6.200823e-02 2.815213e-01 1.937000e-01 4.179880e-02
## [4,] 1.890787e-02 3.451356e-02 4.691862e-02 3.610883e-06 1.782718e-01
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.000257252 0.074735545 0.342752893 0.618198911 0.053068061
## [2,] 0.067630189 0.283202939 0.525488747 0.135480951 0.004097264
## [3,] 0.001150224 0.001115646 0.041378407 0.030660069 0.010987255
## [4,] 0.007982170 0.012723937 0.013690729 0.109074096 0.005445533
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.584695e-02 5.523050e-02 8.017784e-02 2.966114e-02 8.938071e-03
## [2,] 4.823903e-02 6.132637e-02 4.309826e-04 9.790790e-02 6.693202e-03
## [3,] 2.031385e-03 6.017097e-04 1.510763e-03 4.762143e-04 5.615050e-05
## [4,] 3.566134e-04 3.391202e-04 7.885797e-06 7.050804e-05 9.897554e-05
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: 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.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.2 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