DelayedTensor 1.17.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2025-10-21 16:26:44
Compiled: Tue Nov 18 16:47:59 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.09018427 0.01862510 0.25990973
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.09018427 0.01862510 0.25990973
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.01588493 0.3667823 0.7771100 0.52536003
## [2,] 0.02058628 0.2731170 0.7975628 0.97361588
## [3,] 0.43390034 0.6736031 0.2044738 0.04393389
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.01588493 0.36678231 0.77710999 0.52536003
## [2,] 0.02058628 0.27311699 0.79756278 0.97361588
## [3,] 0.43390034 0.67360309 0.20447376 0.04393389
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05330167 0.6313608 0.7876177 0.1088648
## [2,] 0.72285945 0.7464287 0.5212870 0.7425585
## [3,] 0.65895627 0.1992981 0.4088421 0.5352749
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8152929 0.9646294 0.3656295 0.7424876
## [2,] 0.9868345 0.7548374 0.4201127 0.9027194
## [3,] 0.5779794 0.6908142 0.2786573 0.7593469
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.29894900 0.7558637 0.1189096 0.03623409
## [2,] 0.04182249 0.5293032 0.6095269 0.93853357
## [3,] 0.38477244 0.7170783 0.7262260 0.10149778
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8662883 0.7148524 0.4154630 0.7478182
## [2,] 0.7157664 0.6151560 0.4421555 0.3452952
## [3,] 0.1779746 0.2536332 0.6078257 0.2793496
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4325311 0.7548230 0.6779122 0.8310093
## [2,] 0.9548953 0.7835654 0.4786259 0.1531693
## [3,] 0.6615089 0.5773530 0.3942809 0.8421172
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.05330167 0.63136079 0.78761771 0.10886477
## [2,] 0.72285945 0.74642872 0.52128695 0.74255850
## [3,] 0.65895627 0.19929810 0.40884209 0.53527492
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.8152929 0.9646294 0.3656295 0.7424876
## [2,] 0.9868345 0.7548374 0.4201127 0.9027194
## [3,] 0.5779794 0.6908142 0.2786573 0.7593469
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.29894900 0.75586366 0.11890958 0.03623409
## [2,] 0.04182249 0.52930324 0.60952695 0.93853357
## [3,] 0.38477244 0.71707827 0.72622600 0.10149778
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.8662883 0.7148524 0.4154630 0.7478182
## [2,] 0.7157664 0.6151560 0.4421555 0.3452952
## [3,] 0.1779746 0.2536332 0.6078257 0.2793496
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.4325311 0.7548230 0.6779122 0.8310093
## [2,] 0.9548953 0.7835654 0.4786259 0.1531693
## [3,] 0.6615089 0.5773530 0.3942809 0.8421172
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.7650943 0.6534926 0.4386890
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.7650943 0.6534926 0.4386890
einsum::einsum('iii->i', arrD)
## [1] 0.03535619 0.93097593 0.80908237
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.03535619 0.93097593 0.80908237
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.0081332032 0.0003468945 0.0675530677
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.0081332032 0.0003468945 0.0675530677
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.0002523310 0.13452926 0.60389994 0.276003158
## [2,] 0.0004237949 0.07459289 0.63610639 0.947927873
## [3,] 0.1882695014 0.45374112 0.04180952 0.001930187
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.0002523310 0.1345292631 0.6038999433 0.2760031576
## [2,] 0.0004237949 0.0745928915 0.6361063863 0.9479278734
## [3,] 0.1882695014 0.4537411213 0.0418095184 0.0019301869
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.002841068 0.39861644 0.6203417 0.01185154
## [2,] 0.522525788 0.55715584 0.2717401 0.55139313
## [3,] 0.434223372 0.03971973 0.1671519 0.28651924
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6647025 0.9305099 0.1336849 0.5512878
## [2,] 0.9738424 0.5697796 0.1764947 0.8149024
## [3,] 0.3340602 0.4772242 0.0776499 0.5766077
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.089370505 0.5713299 0.01413949 0.001312909
## [2,] 0.001749121 0.2801619 0.37152310 0.880845253
## [3,] 0.148049827 0.5142012 0.52740420 0.010301799
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.75045533 0.5110140 0.1726095 0.5592320
## [2,] 0.51232160 0.3784169 0.1955015 0.1192288
## [3,] 0.03167496 0.0643298 0.3694521 0.0780362
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1870832 0.5697578 0.4595650 0.69057648
## [2,] 0.9118251 0.6139748 0.2290828 0.02346082
## [3,] 0.4375941 0.3333365 0.1554574 0.70916133
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.002841068 0.398616444 0.620341658 0.011851539
## [2,] 0.522525788 0.557155838 0.271740089 0.551393128
## [3,] 0.434223372 0.039719733 0.167151856 0.286519237
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.6647025 0.9305099 0.1336849 0.5512878
## [2,] 0.9738424 0.5697796 0.1764947 0.8149024
## [3,] 0.3340602 0.4772242 0.0776499 0.5766077
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.089370505 0.571329867 0.014139488 0.001312909
## [2,] 0.001749121 0.280161915 0.371523103 0.880845253
## [3,] 0.148049827 0.514201241 0.527404202 0.010301799
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.75045533 0.51101398 0.17260951 0.55923202
## [2,] 0.51232160 0.37841694 0.19550146 0.11922876
## [3,] 0.03167496 0.06432980 0.36945209 0.07803620
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.18708316 0.56975782 0.45956500 0.69057648
## [2,] 0.91182513 0.61397479 0.22908277 0.02346082
## [3,] 0.43759406 0.33333650 0.15545740 0.70916133
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.008133203 0.0016796915 0.023439770
## [2,] 0.001679692 0.0003468945 0.004840846
## [3,] 0.023439770 0.0048408459 0.067553068
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.0081332032 0.0016796915 0.0234397702
## [2,] 0.0016796915 0.0003468945 0.0048408459
## [3,] 0.0234397702 0.0048408459 0.0675530677
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0008466934 0.01955011 0.04142126 0.02800257
## [2,] 0.0010972832 0.01455759 0.04251143 0.05189536
## [3,] 0.0231276140 0.03590417 0.01089879 0.00234175
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01148257 0.2651321 0.5617413 0.37976146
## [2,] 0.01488099 0.1974252 0.5765258 0.70378744
## [3,] 0.31364896 0.4869204 0.1478058 0.03175803
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01046748 0.2416935 0.5120815 0.34618929
## [2,] 0.01356546 0.1799722 0.5255590 0.64157029
## [3,] 0.28592135 0.4438750 0.1347393 0.02895051
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01002912 0.2315720 0.4906368 0.33169172
## [2,] 0.01299737 0.1724354 0.5035499 0.61470289
## [3,] 0.27394766 0.4252866 0.1290967 0.02773814
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01185697 0.2737769 0.5800572 0.39214381
## [2,] 0.01536619 0.2038624 0.5953238 0.72673485
## [3,] 0.32387567 0.5027967 0.1526251 0.03279352
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.003165837 0.07309902 0.15487655 0.104703256
## [2,] 0.004102807 0.05443170 0.15895275 0.194039795
## [3,] 0.086475513 0.13424782 0.04075123 0.008755941
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01251125 0.2888842 0.6120656 0.41378286
## [2,] 0.01621412 0.2151118 0.6281746 0.76683711
## [3,] 0.34174759 0.5305417 0.1610472 0.03460311
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.008280608 0.1911988 0.4050973 0.27386333
## [2,] 0.010731359 0.1423723 0.4157591 0.50753325
## [3,] 0.226186585 0.3511405 0.1065895 0.02290216
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.006494429 0.1499560 0.31771528 0.21478929
## [2,] 0.008416538 0.1116617 0.32607723 0.39805515
## [3,] 0.177396721 0.2753973 0.08359748 0.01796202
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.001729309 0.03992967 0.08459990 0.057193201
## [2,] 0.002241121 0.02973282 0.08682649 0.105992472
## [3,] 0.047236462 0.07333165 0.02225999 0.004782853
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01179549 0.2723573 0.5770496 0.39011055
## [2,] 0.01528652 0.2028053 0.5922370 0.72296675
## [3,] 0.32219638 0.5001897 0.1518337 0.03262348
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.008502805 0.1963294 0.4159675 0.28121204
## [2,] 0.011019319 0.1461927 0.4269154 0.52115216
## [3,] 0.232255966 0.3605628 0.1094497 0.02351671
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01295087 0.2990350 0.6335723 0.42832230
## [2,] 0.01678385 0.2226703 0.6502473 0.79378211
## [3,] 0.35375586 0.5491838 0.1667060 0.03581899
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01567580 0.3619534 0.7668790 0.51844341
## [2,] 0.02031525 0.2695213 0.7870625 0.96079776
## [3,] 0.42818783 0.6647348 0.2017818 0.04335548
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.009181163 0.2119926 0.4491535 0.30364726
## [2,] 0.011898445 0.1578560 0.4609748 0.56272989
## [3,] 0.250785443 0.3893287 0.1181816 0.02539288
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01532307 0.3538090 0.7496231 0.50677772
## [2,] 0.01985813 0.2634567 0.7693525 0.93917849
## [3,] 0.41855302 0.6497773 0.1972414 0.04237992
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01199054 0.2768610 0.5865917 0.39656142
## [2,] 0.01553930 0.2061589 0.6020303 0.73492172
## [3,] 0.32752422 0.5084608 0.1543445 0.03316295
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01097354 0.2533784 0.5368386 0.36292615
## [2,] 0.01422129 0.1886731 0.5509677 0.67258764
## [3,] 0.29974450 0.4653346 0.1412534 0.03035015
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.005807999 0.13410642 0.28413432 0.19208711
## [2,] 0.007526951 0.09985962 0.29161246 0.35598266
## [3,] 0.158646751 0.24628914 0.07476163 0.01606353
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.006673462 0.1540899 0.32647379 0.22071043
## [2,] 0.008648558 0.1147399 0.33506627 0.40902841
## [3,] 0.182287049 0.2829892 0.08590203 0.01845719
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.004426452 0.10220658 0.21654739 0.1463954
## [2,] 0.005736518 0.07610605 0.22224671 0.2713052
## [3,] 0.120909507 0.18770443 0.05697811 0.0122425
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01179436 0.2723313 0.5769945 0.39007329
## [2,] 0.01528506 0.2027860 0.5921805 0.72289769
## [3,] 0.32216561 0.5001419 0.1518192 0.03262037
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01433964 0.3311015 0.7015123 0.47425271
## [2,] 0.01858364 0.2465480 0.7199754 0.87890197
## [3,] 0.39169027 0.6080746 0.1845824 0.03965998
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01206217 0.2785150 0.5900961 0.39893050
## [2,] 0.01563213 0.2073905 0.6056268 0.73931218
## [3,] 0.32948087 0.5114984 0.1552665 0.03336106
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.004748784 0.10964920 0.23231626 0.15705585
## [2,] 0.006154248 0.08164805 0.23843060 0.29106149
## [3,] 0.129714071 0.20137297 0.06112723 0.01313399
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0006643474 0.01533975 0.032500674 0.021971864
## [2,] 0.0008609695 0.01142243 0.033356060 0.040719039
## [3,] 0.0181467920 0.02817176 0.008551602 0.001837425
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.006112084 0.1411277 0.29901051 0.20214406
## [2,] 0.007921033 0.1050879 0.30688017 0.37462055
## [3,] 0.166952889 0.2591839 0.07867587 0.01690455
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01200684 0.2772374 0.5873892 0.39710055
## [2,] 0.01556042 0.2064392 0.6028487 0.73592086
## [3,] 0.32796949 0.5091521 0.1545543 0.03320803
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.008407946 0.1941391 0.4113268 0.27807476
## [2,] 0.010896385 0.1445617 0.4221526 0.51533803
## [3,] 0.229664851 0.3565403 0.1082286 0.02325435
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01139074 0.2630116 0.5572487 0.37672426
## [2,] 0.01476197 0.1958463 0.5719149 0.69815878
## [3,] 0.31114050 0.4830261 0.1466237 0.03150404
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.001888871 0.04361393 0.09240582 0.062470340
## [2,] 0.002447906 0.03247623 0.09483785 0.115772254
## [3,] 0.051594906 0.08009786 0.02431389 0.005224161
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.009682294 0.2235637 0.4736695 0.32022109
## [2,] 0.012547893 0.1664722 0.4861360 0.59344512
## [3,] 0.264473948 0.4105792 0.1246323 0.02677889
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01153605 0.2663668 0.5643575 0.38153011
## [2,] 0.01495029 0.1983447 0.5792108 0.70706516
## [3,] 0.31510971 0.4891881 0.1484942 0.03190593
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0005755760 0.013290022 0.02815787 0.019035941
## [2,] 0.0007459251 0.009896145 0.02889896 0.035278082
## [3,] 0.0157219826 0.024407393 0.00740892 0.001591904
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01490854 0.3442375 0.7293438 0.49306802
## [2,] 0.01932091 0.2563295 0.7485394 0.91377118
## [3,] 0.40723003 0.6321991 0.1919055 0.04123343
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.001612285 0.03722759 0.07887494 0.053322874
## [2,] 0.002089462 0.02772077 0.08095085 0.098819846
## [3,] 0.044039919 0.06836922 0.02075363 0.004459192
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01376093 0.3177392 0.6732013 0.45511322
## [2,] 0.01783365 0.2365980 0.6909193 0.84343199
## [3,] 0.37588276 0.5835344 0.1771332 0.03805941
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01136990 0.2625305 0.5562293 0.37603508
## [2,] 0.01473497 0.1954880 0.5708687 0.69688157
## [3,] 0.31057130 0.4821425 0.1463555 0.03144641
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.002827114 0.06527794 0.13830584 0.093500742
## [2,] 0.003663835 0.04860789 0.14194592 0.173278899
## [3,] 0.077223240 0.11988424 0.03639114 0.007819117
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01135538 0.2621952 0.5555190 0.37555488
## [2,] 0.01471615 0.1952383 0.5701397 0.69599166
## [3,] 0.31017470 0.4815268 0.1461686 0.03140625
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.009771711 0.2256284 0.4780439 0.3231784
## [2,] 0.012663774 0.1680096 0.4906256 0.5989257
## [3,] 0.266916409 0.4143710 0.1257833 0.0270262
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.004028946 0.09302817 0.19710089 0.13324874
## [2,] 0.005221364 0.06927154 0.20228840 0.24694131
## [3,] 0.110051528 0.17084810 0.05186133 0.01114309
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.006599601 0.1523845 0.32286045 0.21826765
## [2,] 0.008552838 0.1134700 0.33135783 0.40450138
## [3,] 0.180269536 0.2798572 0.08495128 0.01825291
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.007023609 0.1621748 0.34360344 0.23229081
## [2,] 0.009102336 0.1207602 0.35264675 0.43048959
## [3,] 0.191851407 0.2978373 0.09040919 0.01942561
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.00965527 0.2229397 0.4723474 0.31932733
## [2,] 0.01251287 0.1660075 0.4847792 0.59178875
## [3,] 0.26373578 0.4094333 0.1242844 0.02670415
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01187904 0.2742865 0.5811370 0.39287377
## [2,] 0.01539479 0.2042418 0.5964319 0.72808764
## [3,] 0.32447856 0.5037326 0.1529092 0.03285456
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.005484990 0.12664816 0.2683323 0.18140428
## [2,] 0.007108343 0.09430598 0.2753946 0.33618487
## [3,] 0.149823694 0.23259190 0.0706038 0.01517016
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.004437449 0.10246050 0.21708537 0.14675912
## [2,] 0.005750769 0.07629513 0.22279885 0.27197922
## [3,] 0.121209890 0.18817076 0.05711967 0.01227292
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.006870727 0.1586448 0.33612425 0.22723456
## [2,] 0.008904207 0.1181316 0.34497072 0.42111916
## [3,] 0.187675396 0.2913543 0.08844126 0.01900278
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01516845 0.3502387 0.7420587 0.50166385
## [2,] 0.01965774 0.2607981 0.7615890 0.92970127
## [3,] 0.41432941 0.6432205 0.1952510 0.04195227
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01050802 0.2426298 0.5140652 0.34753035
## [2,] 0.01361801 0.1806693 0.5275949 0.64405559
## [3,] 0.28702894 0.4455945 0.1352612 0.02906266
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01199031 0.2768557 0.5865805 0.39655385
## [2,] 0.01553900 0.2061550 0.6020188 0.73490770
## [3,] 0.32751797 0.5084511 0.1543415 0.03316231
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01244688 0.2873979 0.6089165 0.41165396
## [2,] 0.01613070 0.2140050 0.6249426 0.76289175
## [3,] 0.33998931 0.5278121 0.1602186 0.03442508
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.009171213 0.2117629 0.4486668 0.30331820
## [2,] 0.011885551 0.1576849 0.4604753 0.56212006
## [3,] 0.250513667 0.3889068 0.1180535 0.02536536
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01076859 0.2486462 0.5268124 0.35614799
## [2,] 0.01395569 0.1851494 0.5406776 0.66002612
## [3,] 0.29414635 0.4566438 0.1386153 0.02978332
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.007602940 0.1755515 0.37194499 0.2514509
## [2,] 0.009853127 0.1307209 0.38173422 0.4659978
## [3,] 0.207675947 0.3224039 0.09786644 0.0210279
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.006263125 0.1446152 0.30639960 0.20713941
## [2,] 0.008116776 0.1076848 0.31446374 0.38387811
## [3,] 0.171078600 0.2655888 0.08062009 0.01732229
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01320053 0.3047995 0.6457856 0.43657907
## [2,] 0.01710739 0.2269628 0.6627821 0.80908386
## [3,] 0.36057522 0.5597704 0.1699196 0.03650947
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.002433083 0.05617977 0.11902936 0.080469003
## [2,] 0.003153185 0.04183313 0.12216210 0.149128017
## [3,] 0.066460191 0.10317528 0.03131909 0.006729321
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01337697 0.3088737 0.6544177 0.44241470
## [2,] 0.01733606 0.2299965 0.6716413 0.81989865
## [3,] 0.36539492 0.5672527 0.1721909 0.03699748
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.0008466934 0.0195501109 0.0414212631 0.0280025685
## [2,] 0.0010972832 0.0145575927 0.0425114307 0.0518953554
## [3,] 0.0231276140 0.0359041718 0.0108987936 0.0023417500
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.01148257 0.26513206 0.56174131 0.37976146
## [2,] 0.01488099 0.19742520 0.57652579 0.70378744
## [3,] 0.31364896 0.48692036 0.14780579 0.03175803
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.01046748 0.24169350 0.51208151 0.34618929
## [2,] 0.01356546 0.17997216 0.52555900 0.64157029
## [3,] 0.28592135 0.44387498 0.13473927 0.02895051
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.01320053 0.30479952 0.64578564 0.43657907
## [2,] 0.01710739 0.22696276 0.66278210 0.80908386
## [3,] 0.36057522 0.55977044 0.16991960 0.03650947
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.002433083 0.056179773 0.119029358 0.080469003
## [2,] 0.003153185 0.041833126 0.122162096 0.149128017
## [3,] 0.066460191 0.103175283 0.031319093 0.006729321
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.01337697 0.30887368 0.65441767 0.44241470
## [2,] 0.01733606 0.22999651 0.67164131 0.81989865
## [3,] 0.36539492 0.56725273 0.17219086 0.03699748
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] 0.3687191
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 0.3687191
einsum::einsum('ij->', arrC)
## [1] 5.10593
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 5.10593
einsum::einsum('ijk->', arrE)
## [1] 33.35808
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 33.35808
einsum::einsum('ij->i', arrC)
## [1] 1.685137 2.064882 1.355911
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 1.685137 2.064882 1.355911
einsum::einsum('ij->j', arrC)
## [1] 0.4703715 1.3135024 1.7791465 1.5429098
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 0.4703715 1.3135024 1.7791465 1.5429098
einsum::einsum('ijk->i', arrE)
## [1] 11.119838 12.405453 9.832787
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 11.119838 12.405453 9.832787
einsum::einsum('ijk->j', arrE)
## [1] 8.349733 9.688997 7.253072 8.066276
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 8.349733 9.688997 7.253072 8.066276
einsum::einsum('ijk->k', arrE)
## [1] 6.116650 8.259341 5.258717 6.181578 7.541792
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 6.116650 8.259341 5.258717 6.181578 7.541792
These are the same as what the modeSum function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 2.466363 3.821529 2.365532 2.466414
## [2,] 3.422178 3.429291 2.471708 3.082276
## [3,] 2.461192 2.438177 2.415832 2.517586
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 2.466363 3.821529 2.365532 2.466414
## [2,] 3.422178 3.429291 2.471708 3.082276
## [3,] 2.461192 2.438177 2.415832 2.517586
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.435117 2.380107 0.7255439 1.760029 2.048935
## [2,] 1.577088 2.410281 2.0022452 1.583642 2.115741
## [3,] 1.717747 1.064400 1.4546625 1.465444 1.550819
## [4,] 1.386698 2.404554 1.0762654 1.372463 1.826296
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.4351174 2.3801068 0.7255439 1.7600293 2.0489354
## [2,] 1.5770876 2.4102810 2.0022452 1.5836416 2.1157415
## [3,] 1.7177468 1.0643995 1.4546625 1.4654442 1.5508190
## [4,] 1.3866982 2.4045539 1.0762654 1.3724630 1.8262957
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.435117 2.380107 0.7255439 1.760029 2.048935
## [2,] 1.577088 2.410281 2.0022452 1.583642 2.115741
## [3,] 1.717747 1.064400 1.4546625 1.465444 1.550819
## [4,] 1.386698 2.404554 1.0762654 1.372463 1.826296
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.4351174 2.3801068 0.7255439 1.7600293 2.0489354
## [2,] 1.5770876 2.4102810 2.0022452 1.5836416 2.1157415
## [3,] 1.7177468 1.0643995 1.4546625 1.4654442 1.5508190
## [4,] 1.3866982 2.4045539 1.0762654 1.3724630 1.8262957
If we take the diagonal elements of a matrix
and add them together, we get trace.
einsum::einsum('ii->', arrB)
## [1] 1.857276
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 1.857276
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.7650943 0.76585753 0.4289232
## [2,] 0.3261765 0.65349255 0.9262357
## [3,] 0.4723433 0.02232459 0.4386890
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.76509435 0.76585753 0.42892319
## [2,] 0.32617654 0.65349255 0.92623570
## [3,] 0.47234327 0.02232459 0.43868897
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.03535619 0.06859311 0.3867646
## [2,] 0.44586412 0.55215856 0.7058060
## [3,] 0.66854777 0.37757201 0.1018059
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.2875449 0.1596614 0.33104866
## [2,] 0.8242382 0.9309759 0.78423455
## [3,] 0.7047102 0.2298747 0.03681235
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.7289269 0.5885487 0.8187040
## [2,] 0.2605273 0.7683551 0.1220108
## [3,] 0.7749966 0.1577923 0.8090824
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.03535619 0.06859311 0.38676457
## [2,] 0.44586412 0.55215856 0.70580600
## [3,] 0.66854777 0.37757201 0.10180588
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.28754489 0.15966144 0.33104866
## [2,] 0.82423818 0.93097593 0.78423455
## [3,] 0.70471021 0.22987466 0.03681235
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.7289269 0.5885487 0.8187040
## [2,] 0.2605273 0.7683551 0.1220108
## [3,] 0.7749966 0.1577923 0.8090824
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.07603317
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 0.07603317
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 3.359486
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 3.359486
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 22.61836
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 22.61836
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.9595902 1.9726050 0.2391695 1.2944519 1.5365023
## [2,] 0.9954920 1.9775137 1.3656930 0.9537607 1.5170691
## [3,] 1.0592336 0.3878295 0.9130668 0.7375631 0.8441052
## [4,] 0.8497639 1.9427979 0.8924600 0.7564970 1.4231986
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9595902 1.9726050 0.2391695 1.2944519 1.5365023
## [2,] 0.9954920 1.9775137 1.3656930 0.9537607 1.5170691
## [3,] 1.0592336 0.3878295 0.9130668 0.7375631 0.8441052
## [4,] 0.8497639 1.9427979 0.8924600 0.7564970 1.4231986
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.0146847 1.2317944 0.4359379
## [2,] 1.2317944 1.6590509 0.3987602
## [3,] 0.4359379 0.3987602 0.6857503
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.0146847 1.2317944 0.4359379
## [2,] 1.2317944 1.6590509 0.3987602
## [3,] 0.4359379 0.3987602 0.6857503
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.000252331 0.0004237949 0.188269501
## [2,] 0.134529263 0.0745928915 0.453741121
## [3,] 0.603899943 0.6361063863 0.041809518
## [4,] 0.276003158 0.9479278734 0.001930187
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.0002523310 0.0004237949 0.1882695014
## [2,] 0.1345292631 0.0745928915 0.4537411213
## [3,] 0.6038999433 0.6361063863 0.0418095184
## [4,] 0.2760031576 0.9479278734 0.0019301869
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.002841068 0.6647025 0.089370505 0.7504553 0.1870832
## [2,] 0.398616444 0.9305099 0.571329867 0.5110140 0.5697578
## [3,] 0.620341658 0.1336849 0.014139488 0.1726095 0.4595650
## [4,] 0.011851539 0.5512878 0.001312909 0.5592320 0.6905765
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.5225258 0.9738424 0.001749121 0.5123216 0.91182513
## [2,] 0.5571558 0.5697796 0.280161915 0.3784169 0.61397479
## [3,] 0.2717401 0.1764947 0.371523103 0.1955015 0.22908277
## [4,] 0.5513931 0.8149024 0.880845253 0.1192288 0.02346082
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.43422337 0.3340602 0.1480498 0.03167496 0.4375941
## [2,] 0.03971973 0.4772242 0.5142012 0.06432980 0.3333365
## [3,] 0.16715186 0.0776499 0.5274042 0.36945209 0.1554574
## [4,] 0.28651924 0.5766077 0.0103018 0.07803620 0.7091613
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.002841068 0.664702509 0.089370505 0.750455335 0.187083165
## [2,] 0.398616444 0.930509870 0.571329867 0.511013978 0.569757824
## [3,] 0.620341658 0.133684911 0.014139488 0.172609507 0.459565002
## [4,] 0.011851539 0.551287793 0.001312909 0.559232018 0.690576478
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.522525788 0.973842374 0.001749121 0.512321601 0.911825126
## [2,] 0.557155838 0.569779572 0.280161915 0.378416943 0.613974792
## [3,] 0.271740089 0.176494696 0.371523103 0.195501461 0.229082771
## [4,] 0.551393128 0.814902381 0.880845253 0.119228760 0.023460820
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.43422337 0.33406015 0.14804983 0.03167496 0.43759406
## [2,] 0.03971973 0.47722421 0.51420124 0.06432980 0.33333650
## [3,] 0.16715186 0.07764990 0.52740420 0.36945209 0.15545740
## [4,] 0.28651924 0.57660769 0.01030180 0.07803620 0.70916133
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 1.581145 2.733134 1.802371
## [2,] 2.888039 3.064504 2.306798
## [3,] 1.209956 2.119186 1.929574
## [4,] 2.744422 2.118373 1.318783
## [5,] 2.696276 2.370256 2.475260
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.581145 2.733134 1.802371
## [2,] 2.888039 3.064504 2.306798
## [3,] 1.209956 2.119186 1.929574
## [4,] 2.744422 2.118373 1.318783
## [5,] 2.696276 2.370256 2.475260
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,] 6.465218e-08 1.512616e-05 2.033741e-06 1.707758e-05 4.257319e-06
## [2,] 4.836184e-03 1.128934e-02 6.931616e-03 6.199838e-03 6.912543e-03
## [3,] 3.378522e-02 7.280785e-03 7.700687e-04 9.400707e-03 2.502896e-02
## [4,] 2.949984e-04 1.372218e-02 3.267981e-05 1.391992e-02 1.718924e-02
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 4.124414e-06 7.686757e-06 1.380620e-08 4.043870e-06 7.197241e-06
## [2,] 7.740568e-04 7.915950e-04 3.892291e-04 5.257348e-04 8.529954e-04
## [3,] 3.219454e-03 2.091029e-03 4.401638e-03 2.316213e-03 2.714069e-03
## [4,] 9.734987e-03 1.438731e-02 1.555155e-02 2.105014e-03 4.142068e-04
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0212478849 0.0163465905 7.244533e-03 1.549953e-03 0.021412823
## [2,] 0.0046842169 0.0562798779 6.064064e-02 7.586524e-03 0.039310951
## [3,] 0.0018163892 0.0008437983 5.731144e-03 4.014725e-03 0.001689309
## [4,] 0.0001437394 0.0002892693 5.168148e-06 3.914876e-05 0.000355768
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,] 6.465218e-08 1.512616e-05 2.033741e-06 1.707758e-05 4.257319e-06
## [2,] 4.836184e-03 1.128934e-02 6.931616e-03 6.199838e-03 6.912543e-03
## [3,] 3.378522e-02 7.280785e-03 7.700687e-04 9.400707e-03 2.502896e-02
## [4,] 2.949984e-04 1.372218e-02 3.267981e-05 1.391992e-02 1.718924e-02
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 4.124414e-06 7.686757e-06 1.380620e-08 4.043870e-06 7.197241e-06
## [2,] 7.740568e-04 7.915950e-04 3.892291e-04 5.257348e-04 8.529954e-04
## [3,] 3.219454e-03 2.091029e-03 4.401638e-03 2.316213e-03 2.714069e-03
## [4,] 9.734987e-03 1.438731e-02 1.555155e-02 2.105014e-03 4.142068e-04
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.124788e-02 1.634659e-02 7.244533e-03 1.549953e-03 2.141282e-02
## [2,] 4.684217e-03 5.627988e-02 6.064064e-02 7.586524e-03 3.931095e-02
## [3,] 1.816389e-03 8.437983e-04 5.731144e-03 4.014725e-03 1.689309e-03
## [4,] 1.437394e-04 2.892693e-04 5.168148e-06 3.914876e-05 3.557680e-04
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 Under development (unstable) (2025-10-21 r88958)
## Platform: x86_64-apple-darwin20
## Running under: macOS Ventura 13.7.8
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.6-x86_64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.6-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.19.0
## [3] HDF5Array_1.39.0 h5mread_1.3.0
## [5] rhdf5_2.55.11 DelayedArray_0.37.0
## [7] SparseArray_1.11.2 S4Arrays_1.11.0
## [9] abind_1.4-8 IRanges_2.45.0
## [11] S4Vectors_0.49.0 MatrixGenerics_1.23.0
## [13] matrixStats_1.5.0 BiocGenerics_0.57.0
## [15] generics_0.1.4 Matrix_1.7-4
## [17] DelayedTensor_1.17.0 BiocStyle_2.39.0
##
## loaded via a namespace (and not attached):
## [1] jsonlite_2.0.0 compiler_4.6.0 BiocManager_1.30.27
## [4] rsvd_1.0.5 Rcpp_1.1.0 rhdf5filters_1.23.1
## [7] parallel_4.6.0 jquerylib_0.1.4 BiocParallel_1.45.0
## [10] yaml_2.3.10 fastmap_1.2.0 lattice_0.22-7
## [13] R6_2.6.1 XVector_0.51.0 ScaledMatrix_1.19.0
## [16] knitr_1.50 bookdown_0.45 bslib_0.9.0
## [19] rlang_1.1.6 cachem_1.1.0 xfun_0.54
## [22] sass_0.4.10 cli_3.6.5 Rhdf5lib_1.33.0
## [25] BiocSingular_1.27.1 digest_0.6.38 grid_4.6.0
## [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.27.0 rmarkdown_2.30 tools_4.6.0
## [37] htmltools_0.5.8.1