DelayedTensor 1.15.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2025-04-04 14:56:50
Compiled: Wed Apr 23 16:35:33 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.6531337 0.2454486 0.3876067
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.6531337 0.2454486 0.3876067
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.25304657 0.5994565 0.17565174 0.96350621
## [2,] 0.06318617 0.3355977 0.02717483 0.04598358
## [3,] 0.85141567 0.1974351 0.17863470 0.66764993
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.25304657 0.59945650 0.17565174 0.96350621
## [2,] 0.06318617 0.33559775 0.02717483 0.04598358
## [3,] 0.85141567 0.19743514 0.17863470 0.66764993
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.65800246 0.4544789 0.8031738 0.5661869
## [2,] 0.02433907 0.1053478 0.8843520 0.7974778
## [3,] 0.74244181 0.7675818 0.8979792 0.3713860
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7376521 0.09023564 0.3332295 0.3561628
## [2,] 0.6232941 0.72503061 0.2931499 0.8394594
## [3,] 0.6048114 0.21976758 0.2732905 0.1078593
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2070207 0.44663243 0.6735759 0.1248121
## [2,] 0.2164263 0.04182536 0.2044097 0.6950981
## [3,] 0.4211306 0.64869279 0.4272710 0.6107241
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1317285 0.9975331 0.8990813 0.0854495
## [2,] 0.9526852 0.8927713 0.9777225 0.5225216
## [3,] 0.9757759 0.1047497 0.7151858 0.7950940
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7321158 0.8278665 0.0159835 0.5424810
## [2,] 0.1520797 0.1781835 0.9449837 0.7756391
## [3,] 0.3575066 0.9797850 0.5501258 0.6725040
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.65800246 0.45447891 0.80317381 0.56618686
## [2,] 0.02433907 0.10534777 0.88435203 0.79747775
## [3,] 0.74244181 0.76758178 0.89797918 0.37138602
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.73765209 0.09023564 0.33322955 0.35616276
## [2,] 0.62329405 0.72503061 0.29314989 0.83945941
## [3,] 0.60481141 0.21976758 0.27329051 0.10785933
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.20702075 0.44663243 0.67357594 0.12481209
## [2,] 0.21642628 0.04182536 0.20440969 0.69509809
## [3,] 0.42113064 0.64869279 0.42727096 0.61072414
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.1317285 0.9975331 0.8990813 0.0854495
## [2,] 0.9526852 0.8927713 0.9777225 0.5225216
## [3,] 0.9757759 0.1047497 0.7151858 0.7950940
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.7321158 0.8278665 0.0159835 0.5424810
## [2,] 0.1520797 0.1781835 0.9449837 0.7756391
## [3,] 0.3575066 0.9797850 0.5501258 0.6725040
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.7862881 0.9817140 0.2898486
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.7862881 0.9817140 0.2898486
einsum::einsum('iii->i', arrD)
## [1] 0.6985954 0.3799349 0.5164721
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.6985954 0.3799349 0.5164721
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.42658360 0.06024503 0.15023893
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.42658360 0.06024503 0.15023893
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.064032567 0.35934810 0.0308535327 0.928344225
## [2,] 0.003992492 0.11262585 0.0007384713 0.002114489
## [3,] 0.724908649 0.03898063 0.0319103573 0.445756428
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.0640325665 0.3593480951 0.0308535327 0.9283442255
## [2,] 0.0039924920 0.1126258480 0.0007384713 0.0021144893
## [3,] 0.7249086495 0.0389806342 0.0319103573 0.4457564279
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4329672336 0.20655108 0.6450882 0.3205676
## [2,] 0.0005923902 0.01109815 0.7820785 0.6359708
## [3,] 0.5512198478 0.58918178 0.8063666 0.1379276
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5441306 0.008142471 0.11104193 0.12685191
## [2,] 0.3884955 0.525669386 0.08593686 0.70469211
## [3,] 0.3657968 0.048297789 0.07468770 0.01163364
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04285759 0.199480529 0.45370455 0.01557806
## [2,] 0.04684033 0.001749361 0.04178332 0.48316135
## [3,] 0.17735102 0.420802336 0.18256048 0.37298398
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01735239 0.99507231 0.8083472 0.007301617
## [2,] 0.90760911 0.79704062 0.9559414 0.273028783
## [3,] 0.95213865 0.01097249 0.5114907 0.632174458
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.53599361 0.68536287 0.0002554724 0.2942857
## [2,] 0.02312825 0.03174936 0.8929942374 0.6016160
## [3,] 0.12781096 0.95997858 0.3026384456 0.4522617
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.4329672336 0.2065510771 0.6450881698 0.3205675575
## [2,] 0.0005923902 0.0110981526 0.7820785171 0.6359707649
## [3,] 0.5512198478 0.5891817843 0.8063665999 0.1379275738
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.544130606 0.008142471 0.111041931 0.126851909
## [2,] 0.388495474 0.525669386 0.085936858 0.704692108
## [3,] 0.365796846 0.048297789 0.074687703 0.011633636
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.042857589 0.199480529 0.453704550 0.015578058
## [2,] 0.046840335 0.001749361 0.041783322 0.483161349
## [3,] 0.177351015 0.420802336 0.182560476 0.372983979
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.017352395 0.995072311 0.808347233 0.007301617
## [2,] 0.907609115 0.797040622 0.955941365 0.273028783
## [3,] 0.952138652 0.010972494 0.511490745 0.632174458
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.5359936057 0.6853628678 0.0002554724 0.2942856660
## [2,] 0.0231282464 0.0317493597 0.8929942374 0.6016160102
## [3,] 0.1278109597 0.9599785846 0.3026384456 0.4522616956
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.4265836 0.16031076 0.25315897
## [2,] 0.1603108 0.06024503 0.09513753
## [3,] 0.2531590 0.09513753 0.15023893
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.42658360 0.16031076 0.25315897
## [2,] 0.16031076 0.06024503 0.09513753
## [3,] 0.25315897 0.09513753 0.15023893
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.16650526 0.3944438 0.1155793 0.63398946
## [2,] 0.04157665 0.2208241 0.0178811 0.03025731
## [3,] 0.56023361 0.1299128 0.1175421 0.43931529
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.006158918 0.014590213 0.004275200 0.023450843
## [2,] 0.001537892 0.008168136 0.000661410 0.001119197
## [3,] 0.020722664 0.004805387 0.004347802 0.016249977
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.18787235 0.4450616 0.13041119 0.71534730
## [2,] 0.04691205 0.2491618 0.02017573 0.03414013
## [3,] 0.63212660 0.1465841 0.13262587 0.49569122
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11500433 0.27244033 0.07983001 0.43789325
## [2,] 0.02871678 0.15252210 0.01235039 0.02089857
## [3,] 0.38695046 0.08973011 0.08118570 0.30343281
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.026657892 0.06315141 0.018504519 0.101503231
## [2,] 0.006656522 0.03535447 0.002862807 0.004844267
## [3,] 0.089694742 0.02079935 0.018818768 0.070335431
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.19423394 0.4601319 0.1348271 0.73956981
## [2,] 0.04850055 0.2575987 0.0208589 0.03529616
## [3,] 0.65353116 0.1515476 0.1371167 0.51247592
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.20324038 0.4814678 0.14107887 0.7738630
## [2,] 0.05074948 0.2695433 0.02182611 0.0369328
## [3,] 0.68383477 0.1585747 0.14347472 0.5362389
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.22378225 0.5301306 0.15533797 0.85207868
## [2,] 0.05587882 0.2967865 0.02403211 0.04066567
## [3,] 0.75295118 0.1746022 0.15797596 0.59043757
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.22723055 0.5382995 0.15773160 0.86520852
## [2,] 0.05673986 0.3013598 0.02440243 0.04129229
## [3,] 0.76455354 0.1772926 0.16041024 0.59953573
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.14327164 0.3394044 0.09945170 0.5455246
## [2,] 0.03577518 0.1900110 0.01538603 0.0260353
## [3,] 0.48206036 0.1117852 0.10114062 0.3780146
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.20179901 0.4780532 0.14007835 0.76837477
## [2,] 0.05038956 0.2676317 0.02167132 0.03667088
## [3,] 0.67898506 0.1574501 0.14245720 0.53243596
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09397796 0.22262976 0.06523460 0.35783274
## [2,] 0.02346646 0.12463631 0.01009235 0.01707766
## [3,] 0.31620388 0.07332465 0.06634243 0.24795585
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.18666033 0.4421903 0.12956987 0.71073237
## [2,] 0.04660941 0.2475544 0.02004557 0.03391988
## [3,] 0.62804855 0.1456384 0.13177026 0.49249337
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.15772242 0.3736377 0.10948268 0.60054769
## [2,] 0.03938356 0.2091761 0.01693791 0.02866129
## [3,] 0.53068232 0.1230601 0.11134195 0.41614223
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.15304545 0.3625581 0.10623618 0.58273956
## [2,] 0.03821572 0.2029733 0.01643565 0.02781139
## [3,] 0.51494592 0.1194110 0.10804031 0.40380230
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.022833819 0.05409234 0.015850047 0.086942599
## [2,] 0.005701644 0.03028288 0.002452138 0.004149357
## [3,] 0.076828038 0.01781569 0.016119217 0.060245818
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.18346651 0.4346243 0.12735289 0.6985715
## [2,] 0.04581191 0.2433186 0.01970258 0.0333395
## [3,] 0.61730243 0.1431465 0.12951563 0.4840666
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05561143 0.13174110 0.038602557 0.2117474
## [2,] 0.01388627 0.07375350 0.005972146 0.0101057
## [3,] 0.18711356 0.04338984 0.039258116 0.1467278
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08432259 0.19975662 0.058532349 0.32106874
## [2,] 0.02105550 0.11183109 0.009055456 0.01532309
## [3,] 0.28371686 0.06579122 0.059526361 0.22248068
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07418057 0.17573061 0.051492287 0.28245174
## [2,] 0.01852302 0.09838044 0.007966298 0.01348008
## [3,] 0.24959241 0.05787809 0.052366744 0.19572150
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06915523 0.16382577 0.048003953 0.26331710
## [2,] 0.01726818 0.09171568 0.007426623 0.01256688
## [3,] 0.23268382 0.05395715 0.048819169 0.18246239
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09012576 0.21350408 0.062560607 0.34316503
## [2,] 0.02250456 0.11952742 0.009678662 0.01637764
## [3,] 0.30324255 0.07031904 0.063623028 0.23779204
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.21242233 0.5032194 0.14745250 0.80882436
## [2,] 0.05304222 0.2817207 0.02281216 0.03860135
## [3,] 0.71472890 0.1657388 0.14995658 0.56046502
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.027293434 0.06465698 0.018945679 0.103923137
## [2,] 0.006815218 0.03619735 0.002931059 0.004959758
## [3,] 0.091833126 0.02129522 0.019267420 0.072012275
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05238589 0.12409993 0.036363554 0.199465775
## [2,] 0.01308085 0.06947570 0.005625753 0.009519554
## [3,] 0.17626071 0.04087317 0.036981089 0.138217386
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05476593 0.12973814 0.038015652 0.208528066
## [2,] 0.01367515 0.07263217 0.005881347 0.009952054
## [3,] 0.18426873 0.04273015 0.038661244 0.144496991
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10656566 0.25244950 0.07397233 0.40576199
## [2,] 0.02660963 0.14133049 0.01144415 0.01936509
## [3,] 0.35855723 0.08314599 0.07522855 0.28116784
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11301880 0.26773671 0.07845176 0.43033312
## [2,] 0.02822099 0.14988884 0.01213716 0.02053776
## [3,] 0.38026985 0.08818094 0.07978405 0.29819411
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.010583765 0.025072486 0.007346698 0.04029900
## [2,] 0.002642784 0.014036498 0.001136597 0.00192328
## [3,] 0.035610770 0.008257796 0.007471461 0.02792470
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.16414949 0.3888631 0.11394402 0.62501953
## [2,] 0.04098841 0.2176998 0.01762811 0.02982921
## [3,] 0.55230721 0.1280748 0.11587904 0.43309970
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.17044608 0.4037795 0.11831478 0.64899461
## [2,] 0.04256068 0.2260506 0.01830431 0.03097343
## [3,] 0.57349311 0.1329876 0.12032404 0.44971293
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05172517 0.12253472 0.035904917 0.196950007
## [2,] 0.01291587 0.06859943 0.005554798 0.009399489
## [3,] 0.17403761 0.04035766 0.036514664 0.136474115
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10811945 0.2561304 0.07505089 0.41167823
## [2,] 0.02699762 0.1433912 0.01161101 0.01964745
## [3,] 0.36378519 0.0843583 0.07632542 0.28526743
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.031583271 0.07481942 0.021923460 0.120257224
## [2,] 0.007886398 0.04188666 0.003391747 0.005739306
## [3,] 0.106266970 0.02464229 0.022295771 0.083330783
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.17589219 0.4166811 0.12209519 0.6697313
## [2,] 0.04392059 0.2332734 0.01888917 0.0319631
## [3,] 0.59181740 0.1372368 0.12416864 0.4640822
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.15454165 0.3661026 0.10727476 0.58843651
## [2,] 0.03858932 0.2049576 0.01659632 0.02808328
## [3,] 0.51998011 0.1205784 0.10909653 0.40774993
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.033333442 0.07896550 0.023138338 0.126921217
## [2,] 0.008323419 0.04420778 0.003579699 0.006057347
## [3,] 0.112155700 0.02600783 0.023531279 0.087948516
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.24107373 0.5710933 0.16734081 0.91791812
## [2,] 0.06019653 0.3197190 0.02588906 0.04380787
## [3,] 0.81113112 0.1880935 0.17018264 0.63606021
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.24691675 0.5849352 0.17139674 0.94016617
## [2,] 0.06165554 0.3274682 0.02651654 0.04486967
## [3,] 0.83079091 0.1926525 0.17430744 0.65147673
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2524223 0.5979777 0.17521842 0.96112935
## [2,] 0.0630303 0.3347699 0.02710779 0.04587014
## [3,] 0.8493153 0.1969481 0.17819403 0.66600291
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2259127 0.5351776 0.15681683 0.86019071
## [2,] 0.0564108 0.2996120 0.02426091 0.04105282
## [3,] 0.7601195 0.1762644 0.15947994 0.59605871
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02650654 0.06279287 0.018399462 0.100926959
## [2,] 0.00661873 0.03515375 0.002846554 0.004816765
## [3,] 0.08918551 0.02068127 0.018711926 0.069936110
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.22750945 0.5389601 0.15792520 0.86627045
## [2,] 0.05680951 0.3017297 0.02443238 0.04134298
## [3,] 0.76549193 0.1775102 0.16060713 0.60027158
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.24740933 0.5861021 0.17173866 0.94204174
## [2,] 0.06177854 0.3281215 0.02656944 0.04495918
## [3,] 0.83244829 0.1930368 0.17465518 0.65277638
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.18097532 0.4287228 0.12562363 0.6890860
## [2,] 0.04518985 0.2400147 0.01943505 0.0328868
## [3,] 0.60892041 0.1412028 0.12775701 0.4774938
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.021622703 0.05122326 0.015009353 0.082331125
## [2,] 0.005399227 0.02867666 0.002322075 0.003929274
## [3,] 0.072753044 0.01687073 0.015264246 0.057050353
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13222229 0.3132289 0.09178182 0.50345277
## [2,] 0.03301614 0.1753571 0.01419943 0.02402741
## [3,] 0.44488305 0.1031641 0.09334048 0.34886148
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.20119581 0.4766243 0.13965964 0.76607800
## [2,] 0.05023894 0.2668318 0.02160654 0.03656127
## [3,] 0.67695549 0.1569795 0.14203138 0.53084445
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1852594 0.4388716 0.12859742 0.7053982
## [2,] 0.0462596 0.2456964 0.01989512 0.0336653
## [3,] 0.6233349 0.1445454 0.13078130 0.4887971
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.038483256 0.09116519 0.026713070 0.14652977
## [2,] 0.009609336 0.05103762 0.004132741 0.00699317
## [3,] 0.129483072 0.03002588 0.027166719 0.10153603
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09046582 0.21430965 0.06279665 0.34445982
## [2,] 0.02258947 0.11997841 0.00971518 0.01643943
## [3,] 0.30438671 0.07058436 0.06386308 0.23868925
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.20948877 0.4962699 0.14541618 0.79765447
## [2,] 0.05230971 0.2778301 0.02249713 0.03806826
## [3,] 0.70485848 0.1634499 0.14788568 0.55272498
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04508872 0.10681326 0.031298241 0.171680910
## [2,] 0.01125873 0.05979798 0.004842106 0.008193515
## [3,] 0.15170822 0.03517968 0.031829757 0.118964201
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.24793123 0.5873385 0.17210093 0.94402891
## [2,] 0.06190886 0.3288136 0.02662549 0.04505402
## [3,] 0.83420428 0.1934440 0.17502360 0.65415336
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.004044571 0.009581415 0.0028075300 0.0154002041
## [2,] 0.001009936 0.005364027 0.0004343489 0.0007349786
## [3,] 0.013608605 0.003155705 0.0028552082 0.0106713844
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2391249 0.5664766 0.16598803 0.91049769
## [2,] 0.0597099 0.3171344 0.02567977 0.04345373
## [3,] 0.8045740 0.1865730 0.16880689 0.63091832
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13920746 0.3297765 0.09663056 0.53004967
## [2,] 0.03476034 0.1846210 0.01494958 0.02529675
## [3,] 0.46838577 0.1086142 0.09827157 0.36729148
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1372730 0.3251938 0.09528773 0.52268384
## [2,] 0.0342773 0.1820554 0.01474183 0.02494522
## [3,] 0.4618769 0.1071048 0.09690594 0.36218742
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.19627281 0.4649619 0.13624235 0.74733309
## [2,] 0.04900966 0.2603027 0.02107786 0.03566666
## [3,] 0.66039129 0.1531384 0.13855606 0.51785539
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.17017484 0.4031369 0.11812650 0.64796183
## [2,] 0.04249295 0.2256908 0.01827518 0.03092414
## [3,] 0.57258049 0.1327759 0.12013256 0.44899728
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.16650526 0.39444385 0.11557927 0.63398946
## [2,] 0.04157665 0.22082414 0.01788110 0.03025731
## [3,] 0.56023361 0.12991281 0.11754207 0.43931529
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.006158918 0.014590213 0.004275200 0.023450843
## [2,] 0.001537892 0.008168136 0.000661410 0.001119197
## [3,] 0.020722664 0.004805387 0.004347802 0.016249977
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.18787235 0.44506157 0.13041119 0.71534730
## [2,] 0.04691205 0.24916180 0.02017573 0.03414013
## [3,] 0.63212660 0.14658410 0.13262587 0.49569122
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.13727296 0.32519378 0.09528773 0.52268384
## [2,] 0.03427730 0.18205541 0.01474183 0.02494522
## [3,] 0.46187685 0.10710482 0.09690594 0.36218742
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.19627281 0.46496190 0.13624235 0.74733309
## [2,] 0.04900966 0.26030273 0.02107786 0.03566666
## [3,] 0.66039129 0.15313841 0.13855606 0.51785539
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.17017484 0.40313692 0.11812650 0.64796183
## [2,] 0.04249295 0.22569084 0.01827518 0.03092414
## [3,] 0.57258049 0.13277593 0.12013256 0.44899728
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.286189
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.286189
einsum::einsum('ij->', arrC)
## [1] 4.358739
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 4.358739
einsum::einsum('ijk->', arrE)
## [1] 31.77386
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 31.77386
einsum::einsum('ij->i', arrC)
## [1] 1.9916610 0.4719423 1.8951354
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 1.9916610 0.4719423 1.8951354
einsum::einsum('ij->j', arrC)
## [1] 1.1676484 1.1324894 0.3814613 1.6771397
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 1.1676484 1.1324894 0.3814613 1.6771397
einsum::einsum('ijk->i', arrE)
## [1] 9.683403 10.846797 11.243663
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 9.683403 10.846797 11.243663
einsum::einsum('ijk->j', arrE)
## [1] 7.537010 7.480482 8.893514 7.862856
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 7.537010 7.480482 8.893514 7.862856
einsum::einsum('ijk->k', arrE)
## [1] 7.072747 5.203943 4.717619 8.050298 6.729254
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 7.072747 5.203943 4.717619 8.050298 6.729254
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 2.466520 2.816747 2.725044 1.675092
## [2,] 1.968824 1.943159 3.304618 3.630196
## [3,] 3.101666 2.720577 2.863852 2.557568
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 2.466520 2.816747 2.725044 1.675092
## [2,] 1.968824 1.943159 3.304618 3.630196
## [3,] 3.101666 2.720577 2.863852 2.557568
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.424783 1.9657576 0.8445777 2.060190 1.241702
## [2,] 1.327408 1.0350338 1.1371506 1.995054 1.985835
## [3,] 2.585505 0.8996699 1.3052566 2.591990 1.511093
## [4,] 1.735051 1.3034815 1.4306343 1.403065 1.990624
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.4247833 1.9657576 0.8445777 2.0601896 1.2417022
## [2,] 1.3274085 1.0350338 1.1371506 1.9950541 1.9858349
## [3,] 2.5855050 0.8996699 1.3052566 2.5919897 1.5110931
## [4,] 1.7350506 1.3034815 1.4306343 1.4030651 1.9906242
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.424783 1.9657576 0.8445777 2.060190 1.241702
## [2,] 1.327408 1.0350338 1.1371506 1.995054 1.985835
## [3,] 2.585505 0.8996699 1.3052566 2.591990 1.511093
## [4,] 1.735051 1.3034815 1.4306343 1.403065 1.990624
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.4247833 1.9657576 0.8445777 2.0601896 1.2417022
## [2,] 1.3274085 1.0350338 1.1371506 1.9950541 1.9858349
## [3,] 2.5855050 0.8996699 1.3052566 2.5919897 1.5110931
## [4,] 1.7350506 1.3034815 1.4306343 1.4030651 1.9906242
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 2.057851
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 2.057851
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.786288091 0.8791941 0.2069086
## [2,] 0.003096433 0.9817140 0.7600512
## [3,] 0.160037971 0.2706706 0.2898486
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.786288091 0.879194124 0.206908560
## [2,] 0.003096433 0.981713960 0.760051249
## [3,] 0.160037971 0.270670647 0.289848641
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.6985954 0.4877606 0.7522051
## [2,] 0.4177693 0.5657735 0.5745084
## [3,] 0.9565308 0.3693806 0.7130297
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.4753531 0.7151308 0.9798838
## [2,] 0.6603868 0.3799349 0.6923483
## [3,] 0.7938061 0.6615853 0.3318079
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.1552286 0.3569883 0.22528510
## [2,] 0.3520911 0.3492671 0.05320368
## [3,] 0.9550081 0.8920369 0.51647208
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.6985954 0.4877606 0.7522051
## [2,] 0.4177693 0.5657735 0.5745084
## [3,] 0.9565308 0.3693806 0.7130297
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.4753531 0.7151308 0.9798838
## [2,] 0.6603868 0.3799349 0.6923483
## [3,] 0.7938061 0.6615853 0.3318079
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.15522859 0.35698830 0.22528510
## [2,] 0.35209109 0.34926707 0.05320368
## [3,] 0.95500813 0.89203692 0.51647208
Some examples of combining Multiplication and Summation are shown below.
Inner Product first calculate Hadamard Product and collapses it to 0D tensor (norm).
einsum::einsum('i,i->', arrA, arrA)
## [1] 0.6370676
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 0.6370676
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 2.743606
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 2.743606
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 22.33038
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 22.33038
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.9847795 1.2984229 0.2670489 1.8771002 0.6869328
## [2,] 0.8068310 0.5821096 0.6220322 1.8030854 1.6770908
## [3,] 2.2335333 0.2716665 0.6780483 2.2757793 1.1958882
## [4,] 1.0944659 0.8431777 0.8717234 0.9125049 1.3481634
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9847795 1.2984229 0.2670489 1.8771002 0.6869328
## [2,] 0.8068310 0.5821096 0.6220322 1.8030854 1.6770908
## [3,] 2.2335333 0.2716665 0.6780483 2.2757793 1.1958882
## [4,] 1.0944659 0.8431777 0.8717234 0.9125049 1.3481634
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.3825784 0.2662441 1.0084639
## [2,] 0.2662441 0.1194713 0.1556118
## [3,] 1.0084639 0.1556118 1.2415561
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.3825784 0.2662441 1.0084639
## [2,] 0.2662441 0.1194713 0.1556118
## [3,] 1.0084639 0.1556118 1.2415561
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.06403257 0.0039924920 0.72490865
## [2,] 0.35934810 0.1126258480 0.03898063
## [3,] 0.03085353 0.0007384713 0.03191036
## [4,] 0.92834423 0.0021144893 0.44575643
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.0640325665 0.0039924920 0.7249086495
## [2,] 0.3593480951 0.1126258480 0.0389806342
## [3,] 0.0308535327 0.0007384713 0.0319103573
## [4,] 0.9283442255 0.0021144893 0.4457564279
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.4329672 0.544130606 0.04285759 0.017352395 0.5359936057
## [2,] 0.2065511 0.008142471 0.19948053 0.995072311 0.6853628678
## [3,] 0.6450882 0.111041931 0.45370455 0.808347233 0.0002554724
## [4,] 0.3205676 0.126851909 0.01557806 0.007301617 0.2942856660
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0005923902 0.38849547 0.046840335 0.9076091 0.02312825
## [2,] 0.0110981526 0.52566939 0.001749361 0.7970406 0.03174936
## [3,] 0.7820785171 0.08593686 0.041783322 0.9559414 0.89299424
## [4,] 0.6359707649 0.70469211 0.483161349 0.2730288 0.60161601
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.5512198 0.36579685 0.1773510 0.95213865 0.1278110
## [2,] 0.5891818 0.04829779 0.4208023 0.01097249 0.9599786
## [3,] 0.8063666 0.07468770 0.1825605 0.51149074 0.3026384
## [4,] 0.1379276 0.01163364 0.3729840 0.63217446 0.4522617
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.4329672336 0.5441306064 0.0428575891 0.0173523947 0.5359936057
## [2,] 0.2065510771 0.0081424706 0.1994805292 0.9950723111 0.6853628678
## [3,] 0.6450881698 0.1110419311 0.4537045496 0.8083472330 0.0002554724
## [4,] 0.3205675575 0.1268519092 0.0155780578 0.0073016172 0.2942856660
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0005923902 0.3884954743 0.0468403348 0.9076091150 0.0231282464
## [2,] 0.0110981526 0.5256693856 0.0017493610 0.7970406222 0.0317493597
## [3,] 0.7820785171 0.0859368581 0.0417833215 0.9559413648 0.8929942374
## [4,] 0.6359707649 0.7046921084 0.4831613486 0.2730287827 0.6016160102
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.55121985 0.36579685 0.17735102 0.95213865 0.12781096
## [2,] 0.58918178 0.04829779 0.42080234 0.01097249 0.95997858
## [3,] 0.80636660 0.07468770 0.18256048 0.51149074 0.30263845
## [4,] 0.13792757 0.01163364 0.37298398 0.63217446 0.45226170
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 2.481842 1.811517 2.779389
## [2,] 1.517280 2.480934 1.205729
## [3,] 1.452041 1.157759 2.107819
## [4,] 2.113792 3.345701 2.590805
## [5,] 2.118447 2.050886 2.559921
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 2.481842 1.811517 2.779389
## [2,] 1.517280 2.480934 1.205729
## [3,] 1.452041 1.157759 2.107819
## [4,] 2.113792 3.345701 2.590805
## [5,] 2.118447 2.050886 2.559921
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.01810748 0.022756535 0.001792383 0.0007257088 2.241623e-02
## [2,] 0.04847802 0.001911057 0.046818548 0.2335458036 1.608563e-01
## [3,] 0.01299948 0.002237659 0.009142819 0.0162893942 5.148146e-06
## [4,] 0.19437065 0.076914483 0.009445489 0.0044272106 1.784351e-01
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 5.805138e-07 3.807068e-04 4.590126e-05 0.0008894131 2.266456e-05
## [2,] 3.067958e-04 1.453153e-02 4.835909e-05 0.0220332791 8.776748e-04
## [3,] 1.417570e-04 1.557664e-05 7.573509e-06 0.0001732708 1.618612e-04
## [4,] 3.300679e-04 3.657341e-04 2.507600e-04 0.0001417015 3.122378e-04
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.154881438 0.1027813890 0.049831987 0.2675313745 0.035912250
## [2,] 0.008902038 0.0007297387 0.006357967 0.0001657851 0.014504464
## [3,] 0.009973680 0.0009237874 0.002258030 0.0063264589 0.003743234
## [4,] 0.023830873 0.0020100382 0.064443488 0.1092259430 0.078140946
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,] 1.810748e-02 2.275654e-02 1.792383e-03 7.257088e-04 2.241623e-02
## [2,] 4.847802e-02 1.911057e-03 4.681855e-02 2.335458e-01 1.608563e-01
## [3,] 1.299948e-02 2.237659e-03 9.142819e-03 1.628939e-02 5.148146e-06
## [4,] 1.943707e-01 7.691448e-02 9.445489e-03 4.427211e-03 1.784351e-01
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 5.805138e-07 3.807068e-04 4.590126e-05 8.894131e-04 2.266456e-05
## [2,] 3.067958e-04 1.453153e-02 4.835909e-05 2.203328e-02 8.776748e-04
## [3,] 1.417570e-04 1.557664e-05 7.573509e-06 1.732708e-04 1.618612e-04
## [4,] 3.300679e-04 3.657341e-04 2.507600e-04 1.417015e-04 3.122378e-04
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.1548814380 0.1027813890 0.0498319870 0.2675313745 0.0359122505
## [2,] 0.0089020382 0.0007297387 0.0063579672 0.0001657851 0.0145044642
## [3,] 0.0099736802 0.0009237874 0.0022580298 0.0063264589 0.0037432343
## [4,] 0.0238308731 0.0020100382 0.0644434876 0.1092259430 0.0781409460
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: 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.17.0
## [3] HDF5Array_1.37.0 h5mread_1.1.0
## [5] rhdf5_2.53.0 DelayedArray_0.35.1
## [7] SparseArray_1.9.0 S4Arrays_1.9.0
## [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.3 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.0 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