DelayedTensor 1.16.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2025-10-29 20:12:10
Compiled: Wed Oct 29 22:33:22 2025
einsumeinsum is an easy and intuitive way to write tensor operations.
It was originally introduced by
Numpy1 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)
DelayedTensorCRAN 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.2449869 0.5758191 0.7991953
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.2449869 0.5758191 0.7991953
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.9196677 0.7531109 0.8209918 0.9445824
## [2,] 0.4992900 0.2287996 0.6908815 0.9956172
## [3,] 0.4945573 0.6139631 0.9928635 0.5267786
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.9196677 0.7531109 0.8209918 0.9445824
## [2,] 0.4992900 0.2287996 0.6908815 0.9956172
## [3,] 0.4945573 0.6139631 0.9928635 0.5267786
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5525016 0.2395960 0.2222092 0.5401952
## [2,] 0.2720023 0.3460189 0.1059514 0.4248728
## [3,] 0.2845848 0.1781017 0.6233330 0.6316585
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9534570 0.05754865 0.0597178 0.7415094
## [2,] 0.9966069 0.60857149 0.7856133 0.2686063
## [3,] 0.9919450 0.67774944 0.6989500 0.9397982
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1216440 0.7672628 0.8993846 0.5543180
## [2,] 0.1509298 0.2705719 0.9773582 0.2837724
## [3,] 0.1066362 0.3565906 0.4916960 0.6223488
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2464704 0.6139151 0.5567983 0.74507872
## [2,] 0.5447409 0.0714218 0.1497964 0.04437955
## [3,] 0.5198660 0.4894467 0.9103196 0.95637974
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5139360 0.70178577 0.08385882 0.5352206
## [2,] 0.7838556 0.27905130 0.24501396 0.2809812
## [3,] 0.8350772 0.02150786 0.20577269 0.7903014
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.5525016 0.2395960 0.2222092 0.5401952
## [2,] 0.2720023 0.3460189 0.1059514 0.4248728
## [3,] 0.2845848 0.1781017 0.6233330 0.6316585
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.95345698 0.05754865 0.05971780 0.74150935
## [2,] 0.99660689 0.60857149 0.78561332 0.26860627
## [3,] 0.99194503 0.67774944 0.69895004 0.93979816
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.1216440 0.7672628 0.8993846 0.5543180
## [2,] 0.1509298 0.2705719 0.9773582 0.2837724
## [3,] 0.1066362 0.3565906 0.4916960 0.6223488
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.24647037 0.61391509 0.55679826 0.74507872
## [2,] 0.54474089 0.07142180 0.14979645 0.04437955
## [3,] 0.51986595 0.48944669 0.91031963 0.95637974
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.51393597 0.70178577 0.08385882 0.53522058
## [2,] 0.78385559 0.27905130 0.24501396 0.28098117
## [3,] 0.83507723 0.02150786 0.20577269 0.79030143
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.6853476 0.9785260 0.8775855
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.6853476 0.9785260 0.8775855
einsum::einsum('iii->i', arrD)
## [1] 0.7580472 0.0881421 0.6643107
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.7580472 0.0881421 0.6643107
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.06001858 0.33156761 0.63871307
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.06001858 0.33156761 0.63871307
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.8457887 0.56717605 0.6740276 0.8922359
## [2,] 0.2492905 0.05234925 0.4773173 0.9912536
## [3,] 0.2445869 0.37695070 0.9857780 0.2774957
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.84578870 0.56717605 0.67402758 0.89223585
## [2,] 0.24929051 0.05234925 0.47731729 0.99125355
## [3,] 0.24458691 0.37695070 0.98577800 0.27749573
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.30525802 0.05740624 0.04937692 0.2918108
## [2,] 0.07398525 0.11972908 0.01122570 0.1805169
## [3,] 0.08098852 0.03172020 0.38854407 0.3989925
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9090802 0.003311847 0.003566216 0.54983612
## [2,] 0.9932253 0.370359258 0.617188291 0.07214933
## [3,] 0.9839549 0.459344297 0.488531159 0.88322059
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01479727 0.58869223 0.8088927 0.30726844
## [2,] 0.02277980 0.07320914 0.9552290 0.08052676
## [3,] 0.01137128 0.12715685 0.2417649 0.38731806
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06074764 0.376891740 0.31002431 0.555142300
## [2,] 0.29674263 0.005101074 0.02243898 0.001969545
## [3,] 0.27026061 0.239558063 0.82868183 0.914662213
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2641302 0.4925032634 0.007032302 0.28646107
## [2,] 0.6144296 0.0778696284 0.060031842 0.07895042
## [3,] 0.6973540 0.0004625879 0.042342399 0.62457635
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.30525802 0.05740624 0.04937692 0.29181084
## [2,] 0.07398525 0.11972908 0.01122570 0.18051686
## [3,] 0.08098852 0.03172020 0.38854407 0.39899246
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.909080212 0.003311847 0.003566216 0.549836122
## [2,] 0.993225288 0.370359258 0.617188291 0.072149328
## [3,] 0.983954940 0.459344297 0.488531159 0.883220591
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.01479727 0.58869223 0.80889273 0.30726844
## [2,] 0.02277980 0.07320914 0.95522905 0.08052676
## [3,] 0.01137128 0.12715685 0.24176494 0.38731806
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.060747643 0.376891740 0.310024306 0.555142300
## [2,] 0.296742633 0.005101074 0.022438975 0.001969545
## [3,] 0.270260611 0.239558063 0.828681828 0.914662213
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.2641301831 0.4925032634 0.0070323020 0.2864610662
## [2,] 0.6144295795 0.0778696284 0.0600318416 0.0789504165
## [3,] 0.6973539807 0.0004625879 0.0423423993 0.6245763540
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.06001858 0.1410681 0.1957924
## [2,] 0.14106813 0.3315676 0.4601919
## [3,] 0.19579237 0.4601919 0.6387131
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.06001858 0.14106813 0.19579237
## [2,] 0.14106813 0.33156761 0.46019188
## [3,] 0.19579237 0.46019188 0.63871307
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5081179 0.4160950 0.4535993 0.5218833
## [2,] 0.2758585 0.1264121 0.3817132 0.5500801
## [3,] 0.2732437 0.3392156 0.5485587 0.2910460
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2501517 0.20484790 0.2233117 0.2569286
## [2,] 0.1358080 0.06223402 0.1879214 0.2708102
## [3,] 0.1345207 0.16699938 0.2700612 0.1432850
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2617235 0.21432393 0.2336418 0.2688138
## [2,] 0.1420904 0.06511289 0.1966144 0.2833375
## [3,] 0.1407435 0.17472458 0.2825539 0.1499132
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2203487 0.18044236 0.1967064 0.2263182
## [2,] 0.1196279 0.05481947 0.1655324 0.2385459
## [3,] 0.1184939 0.14710310 0.2378861 0.1262141
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3182224 0.26059061 0.2840787 0.3268434
## [2,] 0.1727638 0.07916899 0.2390581 0.3445024
## [3,] 0.1711262 0.21244284 0.3435496 0.1822754
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.16379435 0.13413031 0.1462200 0.16823170
## [2,] 0.08892438 0.04074959 0.1230472 0.17732108
## [3,] 0.08808148 0.10934785 0.1768307 0.09382015
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2043586 0.16734816 0.1824319 0.2098949
## [2,] 0.1109468 0.05084137 0.1535202 0.2212353
## [3,] 0.1098952 0.13642824 0.2206234 0.1170551
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09744007 0.07979314 0.08698522 0.10007981
## [2,] 0.05290047 0.02424163 0.07319986 0.10548702
## [3,] 0.05239903 0.06505024 0.10519527 0.05581293
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5732593 0.4694389 0.5117513 0.5887894
## [2,] 0.3112240 0.1426183 0.4306493 0.6206011
## [3,] 0.3082739 0.3827035 0.6188846 0.3283585
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4968001 0.4068269 0.4434958 0.5102588
## [2,] 0.2697141 0.1235964 0.3732109 0.5378276
## [3,] 0.2671575 0.3316599 0.5363401 0.2845633
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3907418 0.31997631 0.3488171 0.4013273
## [2,] 0.2121347 0.09721072 0.2935367 0.4230106
## [3,] 0.2101239 0.26085620 0.4218407 0.2238139
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5809159 0.4757089 0.5185865 0.5966535
## [2,] 0.3153808 0.1445232 0.4364012 0.6288900
## [3,] 0.3123913 0.3878150 0.6271507 0.3327442
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8768636 0.7180589 0.7827804 0.9006187
## [2,] 0.4760515 0.2181506 0.6587258 0.9492781
## [3,] 0.4715391 0.5853874 0.9466527 0.5022608
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9165472 0.7505555 0.8182061 0.9413773
## [2,] 0.4975959 0.2280233 0.6885373 0.9922389
## [3,] 0.4928792 0.6118799 0.9894946 0.5249912
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9122598 0.7470446 0.8143788 0.9369738
## [2,] 0.4952682 0.2269566 0.6853165 0.9875975
## [3,] 0.4905736 0.6090177 0.9848660 0.5225355
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05292564 0.04334052 0.04724697 0.05435944
## [2,] 0.02873347 0.01316711 0.03975930 0.05729643
## [3,] 0.02846110 0.03533275 0.05713796 0.03031540
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5596835 0.4583218 0.4996322 0.5748459
## [2,] 0.3038537 0.1392409 0.4204508 0.6059042
## [3,] 0.3009735 0.3736404 0.6042284 0.3205825
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6233043 0.5104205 0.5564267 0.6401902
## [2,] 0.3383935 0.1550688 0.4682446 0.6747790
## [3,] 0.3351859 0.4161132 0.6729127 0.3570239
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05492053 0.04497413 0.04902783 0.05640838
## [2,] 0.02981650 0.01366341 0.04125793 0.05945607
## [3,] 0.02953387 0.03666453 0.05929163 0.03145806
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7225032 0.5916540 0.6449821 0.7420765
## [2,] 0.3922489 0.1797480 0.5427657 0.7821701
## [3,] 0.3885308 0.4823376 0.7800068 0.4138443
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6428018 0.5263869 0.5738323 0.6602159
## [2,] 0.3489788 0.1599195 0.4828917 0.6958867
## [3,] 0.3456708 0.4291295 0.6939620 0.3681920
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6819422 0.5584388 0.6087731 0.7004167
## [2,] 0.3702282 0.1696570 0.5122951 0.7382594
## [3,] 0.3667189 0.4552594 0.7362176 0.3906113
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2470285 0.20229031 0.2205236 0.2537207
## [2,] 0.1341124 0.06145701 0.1855751 0.2674290
## [3,] 0.1328412 0.16491434 0.2666894 0.1414960
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8643020 0.7077723 0.7715666 0.8877168
## [2,] 0.4692318 0.2150254 0.6492892 0.9356792
## [3,] 0.4647840 0.5770014 0.9330913 0.4950656
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11187210 0.09161145 0.09986876 0.11490282
## [2,] 0.06073565 0.02783211 0.08404162 0.12111090
## [3,] 0.06015995 0.07468495 0.12077593 0.06407948
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13880526 0.11366688 0.1239121 0.14256563
## [2,] 0.07535774 0.03453268 0.1042746 0.15026830
## [3,] 0.07464343 0.09266533 0.1498527 0.07950659
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09806986 0.08030887 0.08754744 0.10072666
## [2,] 0.05324238 0.02439832 0.07367297 0.10616882
## [3,] 0.05273770 0.06547068 0.10587518 0.05617366
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7056268 0.5778340 0.6299165 0.7247429
## [2,] 0.3830867 0.1755494 0.5300877 0.7639000
## [3,] 0.3794554 0.4710711 0.7617873 0.4041777
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2488362 0.20377064 0.2221373 0.2555774
## [2,] 0.1350938 0.06190674 0.1869331 0.2693860
## [3,] 0.1338133 0.16612116 0.2686410 0.1425315
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3279449 0.26855227 0.2927580 0.3368292
## [2,] 0.1780421 0.08158778 0.2463619 0.3550277
## [3,] 0.1763545 0.21893347 0.3540458 0.1878443
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8271350 0.6773364 0.7383874 0.8495429
## [2,] 0.4490538 0.2057788 0.6213682 0.8954428
## [3,] 0.4447972 0.5521890 0.8929662 0.4737766
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8988448 0.7360591 0.8024031 0.9231953
## [2,] 0.4879852 0.2236192 0.6752387 0.9730746
## [3,] 0.4833596 0.6000619 0.9703833 0.5148514
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4521969 0.3703016 0.4036784 0.4644474
## [2,] 0.2454989 0.1124998 0.3397037 0.4895410
## [3,] 0.2431718 0.3018832 0.4881870 0.2590149
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5097884 0.4174629 0.4550905 0.5235990
## [2,] 0.2767654 0.1268277 0.3829681 0.5518885
## [3,] 0.2741420 0.3403308 0.5503621 0.2920029
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2609763 0.21371208 0.2329748 0.2680464
## [2,] 0.1416847 0.06492701 0.1960531 0.2825287
## [3,] 0.1403417 0.17422577 0.2817472 0.1494852
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5723541 0.4686977 0.5109433 0.5878597
## [2,] 0.3107325 0.1423932 0.4299693 0.6196212
## [3,] 0.3077871 0.3820992 0.6179075 0.3278401
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2266708 0.18561952 0.2023502 0.2328116
## [2,] 0.1230602 0.05639232 0.1702818 0.2453901
## [3,] 0.1218937 0.15132371 0.2447114 0.1298353
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5009806 0.4102503 0.4472278 0.5145526
## [2,] 0.2719837 0.1246365 0.3763514 0.5423534
## [3,] 0.2694056 0.3344508 0.5408534 0.2869579
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4781039 0.3915167 0.4268057 0.4910562
## [2,] 0.2595639 0.1189451 0.3591658 0.5175875
## [3,] 0.2571035 0.3191785 0.5161560 0.2738543
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5645979 0.4623462 0.5040193 0.5798934
## [2,] 0.3065217 0.1404635 0.4241426 0.6112244
## [3,] 0.3036162 0.3769212 0.6095339 0.3233974
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06568433 0.05378854 0.05863672 0.06746378
## [2,] 0.03566019 0.01634128 0.04934400 0.07110877
## [3,] 0.03532217 0.04385035 0.07091210 0.03762348
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4501283 0.3686076 0.4018317 0.4623227
## [2,] 0.2443758 0.1119852 0.3381497 0.4873015
## [3,] 0.2420594 0.3005022 0.4859538 0.2578301
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5120694 0.4193308 0.4571268 0.5259418
## [2,] 0.2780038 0.1273952 0.3846816 0.5543579
## [3,] 0.2753686 0.3418536 0.5528247 0.2933094
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13776295 0.11281334 0.1229817 0.14149508
## [2,] 0.07479187 0.03427337 0.1034916 0.14913991
## [3,] 0.07408292 0.09196949 0.1487274 0.07890957
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8371916 0.6855716 0.7473650 0.8598719
## [2,] 0.4545135 0.2082808 0.6289230 0.9063299
## [3,] 0.4502052 0.5589027 0.9038232 0.4795369
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6852248 0.5611269 0.6117035 0.7037882
## [2,] 0.3720104 0.1704737 0.5147611 0.7418132
## [3,] 0.3684841 0.4574508 0.7397615 0.3924916
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04081444 0.03342273 0.03643525 0.04192014
## [2,] 0.02215827 0.01015402 0.03066101 0.04418505
## [3,] 0.02194823 0.02724741 0.04406284 0.02337820
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8795516 0.7202600 0.7851800 0.9033794
## [2,] 0.4775109 0.2188193 0.6607451 0.9521881
## [3,] 0.4729846 0.5871819 0.9495546 0.5038004
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4726503 0.3870508 0.4219372 0.4854549
## [2,] 0.2566031 0.1175883 0.3550689 0.5116835
## [3,] 0.2541708 0.3155377 0.5102683 0.2707305
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7208867 0.5903302 0.6435390 0.7404162
## [2,] 0.3913713 0.1793458 0.5415513 0.7804201
## [3,] 0.3876615 0.4812584 0.7782616 0.4129184
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7679936 0.6289058 0.6855916 0.7887992
## [2,] 0.4169457 0.1910653 0.5769394 0.8314172
## [3,] 0.4129935 0.5127066 0.8291177 0.4399008
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6454097 0.5285225 0.5761604 0.6628945
## [2,] 0.3503946 0.1605683 0.4848508 0.6987100
## [3,] 0.3470733 0.4308706 0.6967775 0.3696858
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2566345 0.21015658 0.2290988 0.2635869
## [2,] 0.1393275 0.06384682 0.1927914 0.2778283
## [3,] 0.1380069 0.17132720 0.2770599 0.1469983
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01978008 0.016197802 0.01765778 0.02031594
## [2,] 0.01073866 0.004920989 0.01485938 0.02141359
## [3,] 0.01063687 0.013205031 0.02135437 0.01132988
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07712225 0.06315499 0.06884741 0.07921156
## [2,] 0.04186987 0.01918686 0.05793651 0.08349128
## [3,] 0.04147299 0.05148622 0.08326037 0.04417504
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2253314 0.1845227 0.2011545 0.2314359
## [2,] 0.1223330 0.0560591 0.1692756 0.2439401
## [3,] 0.1211734 0.1504295 0.2432654 0.1290681
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1892425 0.15496966 0.1689377 0.1943693
## [2,] 0.1027402 0.04708071 0.1421645 0.2048708
## [3,] 0.1017664 0.12633684 0.2043042 0.1083967
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4922251 0.4030805 0.4394117 0.5055599
## [2,] 0.2672303 0.1224583 0.3697740 0.5328748
## [3,] 0.2646972 0.3286057 0.5314010 0.2819428
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2584093 0.21160998 0.2306832 0.2654099
## [2,] 0.1402911 0.06428838 0.1941247 0.2797497
## [3,] 0.1389613 0.17251207 0.2789760 0.1480149
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7268147 0.5951846 0.6488310 0.7465048
## [2,] 0.3945896 0.1808206 0.5460047 0.7868377
## [3,] 0.3908493 0.4852159 0.7846615 0.4163139
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.5081179 0.4160950 0.4535993 0.5218833
## [2,] 0.2758585 0.1264121 0.3817132 0.5500801
## [3,] 0.2732437 0.3392156 0.5485587 0.2910460
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.25015173 0.20484790 0.22331166 0.25692858
## [2,] 0.13580803 0.06223402 0.18792136 0.27081016
## [3,] 0.13452072 0.16699938 0.27006116 0.14328500
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.26172347 0.21432393 0.23364181 0.26881380
## [2,] 0.14209036 0.06511289 0.19661440 0.28333753
## [3,] 0.14074350 0.17472458 0.28255389 0.14991320
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.4922251 0.4030805 0.4394117 0.5055599
## [2,] 0.2672303 0.1224583 0.3697740 0.5328748
## [3,] 0.2646972 0.3286057 0.5314010 0.2819428
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.25840931 0.21160998 0.23068324 0.26540986
## [2,] 0.14029109 0.06428838 0.19412470 0.27974967
## [3,] 0.13896128 0.17251207 0.27897596 0.14801488
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.7268147 0.5951846 0.6488310 0.7465048
## [2,] 0.3945896 0.1808206 0.5460047 0.7868377
## [3,] 0.3908493 0.4852159 0.7846615 0.4163139
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.620001
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.620001
einsum::einsum('ij->', arrC)
## [1] 8.481104
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 8.481104
einsum::einsum('ijk->', arrE)
## [1] 28.92859
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 28.92859
einsum::einsum('ij->i', arrC)
## [1] 3.438353 2.414588 2.628163
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 3.438353 2.414588 2.628163
einsum::einsum('ij->j', arrC)
## [1] 1.913515 1.595874 2.504737 2.466978
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 1.913515 1.595874 2.504737 2.466978
einsum::einsum('ijk->i', arrE)
## [1] 9.706408 7.890116 11.332064
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 9.706408 7.890116 11.332064
einsum::einsum('ijk->j', arrE)
## [1] 7.874254 5.679140 7.015773 8.359421
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 7.874254 5.679140 7.015773 8.359421
einsum::einsum('ijk->k', arrE)
## [1] 4.421025 7.780073 5.602513 5.848613 5.276362
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 4.421025 7.780073 5.602513 5.848613 5.276362
These are the same as what the modeSum function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 2.388009 2.380108 1.821969 3.116322
## [2,] 2.748135 1.575635 2.263733 1.302612
## [3,] 2.738109 1.723396 2.930071 3.940487
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 2.388009 2.380108 1.821969 3.116322
## [2,] 2.748135 1.575635 2.263733 1.302612
## [3,] 2.738109 1.723396 2.930071 3.940487
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.1090887 2.942009 0.379210 1.311077 2.1328688
## [2,] 0.7637166 1.343870 1.394425 1.174784 1.0023449
## [3,] 0.9514936 1.544281 2.368439 1.616914 0.5346455
## [4,] 1.5967264 1.949914 1.460439 1.745838 1.6065032
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.1090887 2.9420089 0.3792100 1.3110772 2.1328688
## [2,] 0.7637166 1.3438696 1.3944253 1.1747836 1.0023449
## [3,] 0.9514936 1.5442812 2.3684388 1.6169143 0.5346455
## [4,] 1.5967264 1.9499138 1.4604392 1.7458380 1.6065032
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.1090887 2.942009 0.379210 1.311077 2.1328688
## [2,] 0.7637166 1.343870 1.394425 1.174784 1.0023449
## [3,] 0.9514936 1.544281 2.368439 1.616914 0.5346455
## [4,] 1.5967264 1.949914 1.460439 1.745838 1.6065032
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.1090887 2.9420089 0.3792100 1.3110772 2.1328688
## [2,] 0.7637166 1.3438696 1.3944253 1.1747836 1.0023449
## [3,] 0.9514936 1.5442812 2.3684388 1.6169143 0.5346455
## [4,] 1.5967264 1.9499138 1.4604392 1.7458380 1.6065032
If we take the diagonal elements of a matrix
and add them together, we get trace.
einsum::einsum('ii->', arrB)
## [1] 2.541459
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 2.541459
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.6853476 0.1569604 0.2779743
## [2,] 0.6886378 0.9785260 0.9984846
## [3,] 0.7227555 0.6454306 0.8775855
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.6853476 0.1569604 0.2779743
## [2,] 0.6886378 0.9785260 0.9984846
## [3,] 0.7227555 0.6454306 0.8775855
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.7580472 0.15162027 0.5964027
## [2,] 0.4661280 0.66322637 0.5373098
## [3,] 0.4999786 0.01103248 0.1195853
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.6578408 0.5053706 0.5375684
## [2,] 0.2493007 0.0881421 0.8676526
## [3,] 0.4707618 0.2423578 0.3336807
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.9197606 0.03490767 0.8447282
## [2,] 0.4965311 0.49295937 0.7986897
## [3,] 0.6322764 0.77569437 0.6643107
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.75804720 0.15162027 0.59640269
## [2,] 0.46612798 0.66322637 0.53730978
## [3,] 0.49997859 0.01103248 0.11958534
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.6578408 0.5053706 0.5375684
## [2,] 0.2493007 0.0881421 0.8676526
## [3,] 0.4707618 0.2423578 0.3336807
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.91976058 0.03490767 0.84472821
## [2,] 0.49653110 0.49295937 0.79868973
## [3,] 0.63227639 0.77569437 0.66431065
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.030299
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.030299
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 6.63425
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 6.63425
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 19.07069
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 19.07069
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.4602318 2.8862604 0.04894835 0.6277509 1.5759137
## [2,] 0.2088555 0.8330154 0.78905823 0.6215509 0.5708355
## [3,] 0.4491467 1.1092857 2.00588671 1.1611451 0.1094065
## [4,] 0.8713202 1.5052060 0.77511325 1.4717741 0.9899878
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.46023179 2.88626044 0.04894835 0.62775089 1.57591374
## [2,] 0.20885553 0.83301540 0.78905823 0.62155088 0.57083548
## [3,] 0.44914669 1.10928567 2.00588671 1.16114511 0.10940654
## [4,] 0.87132016 1.50520604 0.77511325 1.47177406 0.98998784
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 2.979228 2.139143 2.229929
## [2,] 2.139143 1.770211 1.597823
## [3,] 2.229929 1.597823 1.884811
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 2.979228 2.139143 2.229929
## [2,] 2.139143 1.770211 1.597823
## [3,] 2.229929 1.597823 1.884811
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.8457887 0.24929051 0.2445869
## [2,] 0.5671760 0.05234925 0.3769507
## [3,] 0.6740276 0.47731729 0.9857780
## [4,] 0.8922359 0.99125355 0.2774957
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.84578870 0.24929051 0.24458691
## [2,] 0.56717605 0.05234925 0.37695070
## [3,] 0.67402758 0.47731729 0.98577800
## [4,] 0.89223585 0.99125355 0.27749573
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.30525802 0.909080212 0.01479727 0.06074764 0.264130183
## [2,] 0.05740624 0.003311847 0.58869223 0.37689174 0.492503263
## [3,] 0.04937692 0.003566216 0.80889273 0.31002431 0.007032302
## [4,] 0.29181084 0.549836122 0.30726844 0.55514230 0.286461066
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.07398525 0.99322529 0.02277980 0.296742633 0.61442958
## [2,] 0.11972908 0.37035926 0.07320914 0.005101074 0.07786963
## [3,] 0.01122570 0.61718829 0.95522905 0.022438975 0.06003184
## [4,] 0.18051686 0.07214933 0.08052676 0.001969545 0.07895042
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.08098852 0.9839549 0.01137128 0.2702606 0.6973539807
## [2,] 0.03172020 0.4593443 0.12715685 0.2395581 0.0004625879
## [3,] 0.38854407 0.4885312 0.24176494 0.8286818 0.0423423993
## [4,] 0.39899246 0.8832206 0.38731806 0.9146622 0.6245763540
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.305258019 0.909080212 0.014797273 0.060747643 0.264130183
## [2,] 0.057406240 0.003311847 0.588692231 0.376891740 0.492503263
## [3,] 0.049376921 0.003566216 0.808892726 0.310024306 0.007032302
## [4,] 0.291810839 0.549836122 0.307268436 0.555142300 0.286461066
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.073985250 0.993225288 0.022779804 0.296742633 0.614429579
## [2,] 0.119729082 0.370359258 0.073209144 0.005101074 0.077869628
## [3,] 0.011225696 0.617188291 0.955229048 0.022438975 0.060031842
## [4,] 0.180516862 0.072149328 0.080526763 0.001969545 0.078950416
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0809885201 0.9839549401 0.0113712760 0.2702606112 0.6973539807
## [2,] 0.0317202041 0.4593442969 0.1271568515 0.2395580631 0.0004625879
## [3,] 0.3885440720 0.4885311590 0.2417649403 0.8286818276 0.0423423993
## [4,] 0.3989924583 0.8832205907 0.3873180553 0.9146622134 0.6245763540
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 1.554502 1.1488453 1.717678
## [2,] 1.812233 2.6593980 3.308443
## [3,] 2.342609 1.6826323 1.577272
## [4,] 2.162262 0.8103387 2.876012
## [5,] 1.834801 1.5889020 1.852659
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.5545020 1.1488453 1.7176780
## [2,] 1.8122328 2.6593980 3.3084427
## [3,] 2.3426095 1.6826323 1.5772716
## [4,] 2.1622624 0.8103387 2.8760120
## [5,] 1.8348011 1.5889020 1.8526592
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.063251645 0.1883679241 0.003066101 0.01258735 0.054729664
## [2,] 0.007976637 0.0004601835 0.081799200 0.05236937 0.068433675
## [3,] 0.008153509 0.0005888819 0.133570781 0.05119367 0.001161229
## [4,] 0.063785793 0.1201865329 0.067164609 0.12134639 0.062616407
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.010620304 0.14257375 0.003269955 0.0425962875 0.088199052
## [2,] 0.003609077 0.01116400 0.002206794 0.0001537652 0.002347279
## [3,] 0.003085365 0.16963321 0.262543176 0.0061673164 0.016499656
## [4,] 0.103035903 0.04118159 0.045963284 0.0011241822 0.045063532
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.01583104 0.1923363 0.002222774 0.05282857 0.136313664
## [2,] 0.00955594 0.1383808 0.038306919 0.07216860 0.000139358
## [3,] 0.30610633 0.3848791 0.190469457 0.65285967 0.033358575
## [4,] 0.08848586 0.1958747 0.085896794 0.20284764 0.138514345
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,] 0.0632516455 0.1883679241 0.0030661008 0.0125873462 0.0547296637
## [2,] 0.0079766375 0.0004601835 0.0817991997 0.0523693725 0.0684336750
## [3,] 0.0081535087 0.0005888819 0.1335707812 0.0511936718 0.0011612295
## [4,] 0.0637857930 0.1201865329 0.0671646089 0.1213463898 0.0626164070
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0106203040 0.1425737495 0.0032699550 0.0425962875 0.0881990521
## [2,] 0.0036090775 0.0111639981 0.0022067944 0.0001537652 0.0023472787
## [3,] 0.0030853646 0.1696332147 0.2625431761 0.0061673164 0.0164996556
## [4,] 0.1030359028 0.0411815887 0.0459632836 0.0011241822 0.0450635323
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.015831044 0.192336326 0.002222774 0.052828571 0.136313664
## [2,] 0.009555940 0.138380784 0.038306919 0.072168595 0.000139358
## [3,] 0.306106330 0.384879067 0.190469457 0.652859666 0.033358575
## [4,] 0.088485865 0.195874725 0.085896794 0.202847636 0.138514345
einsumBy 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.1 Patched (2025-09-10 r88807)
## Platform: aarch64-apple-darwin20
## Running under: macOS Ventura 13.7.7
##
## 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.18.0
## [3] HDF5Array_1.38.0 h5mread_1.2.0
## [5] rhdf5_2.54.0 DelayedArray_0.36.0
## [7] SparseArray_1.10.0 S4Arrays_1.10.0
## [9] abind_1.4-8 IRanges_2.44.0
## [11] S4Vectors_0.48.0 MatrixGenerics_1.22.0
## [13] matrixStats_1.5.0 BiocGenerics_0.56.0
## [15] generics_0.1.4 Matrix_1.7-4
## [17] DelayedTensor_1.16.0 BiocStyle_2.38.0
##
## loaded via a namespace (and not attached):
## [1] jsonlite_2.0.0 compiler_4.5.1 BiocManager_1.30.26
## [4] rsvd_1.0.5 Rcpp_1.1.0 rhdf5filters_1.22.0
## [7] parallel_4.5.1 jquerylib_0.1.4 BiocParallel_1.44.0
## [10] yaml_2.3.10 fastmap_1.2.0 lattice_0.22-7
## [13] R6_2.6.1 XVector_0.50.0 ScaledMatrix_1.18.0
## [16] knitr_1.50 bookdown_0.45 bslib_0.9.0
## [19] rlang_1.1.6 cachem_1.1.0 xfun_0.53
## [22] sass_0.4.10 cli_3.6.5 Rhdf5lib_1.32.0
## [25] BiocSingular_1.26.0 digest_0.6.37 grid_4.5.1
## [28] irlba_2.3.5.1 rTensor_1.4.9 dqrng_0.4.1
## [31] lifecycle_1.0.4 evaluate_1.0.5 codetools_0.2-20
## [34] beachmat_2.26.0 rmarkdown_2.30 tools_4.5.1
## [37] htmltools_0.5.8.1