DelayedTensor 1.16.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2025-10-29 20:12:10.36772
Compiled: Wed Oct 29 23:35:34 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.5172736 0.9744528 0.6652107
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.5172736 0.9744528 0.6652107
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.5906119 0.1535408 0.7640054 0.5096071
## [2,] 0.8582684 0.4352356 0.6269506 0.3525871
## [3,] 0.6390596 0.1360211 0.6262532 0.1707573
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.5906119 0.1535408 0.7640054 0.5096071
## [2,] 0.8582684 0.4352356 0.6269506 0.3525871
## [3,] 0.6390596 0.1360211 0.6262532 0.1707573
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.36805239 0.8136199 0.43373601 0.72231141
## [2,] 0.02654479 0.4696985 0.04016921 0.12952560
## [3,] 0.98317333 0.7374409 0.21429429 0.01593689
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1733752 0.7668326 0.8213798 0.6663194
## [2,] 0.1164418 0.3516137 0.8045129 0.3993360
## [3,] 0.5650845 0.7578191 0.3634950 0.4653050
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3739350 0.461487421 0.80329373 0.06672814
## [2,] 0.7759773 0.009529161 0.01629255 0.01735822
## [3,] 0.1811103 0.948629274 0.74605876 0.80037430
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02100904 0.1743366 0.6103823 0.4766318
## [2,] 0.33419914 0.9076262 0.4420992 0.2668230
## [3,] 0.13323694 0.4735707 0.1047771 0.8408271
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.16789590 0.8416282 0.4436238 0.08465500
## [2,] 0.01820825 0.4557965 0.5451249 0.53637852
## [3,] 0.69643710 0.8897261 0.7170277 0.08231698
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.36805239 0.81361986 0.43373601 0.72231141
## [2,] 0.02654479 0.46969854 0.04016921 0.12952560
## [3,] 0.98317333 0.73744094 0.21429429 0.01593689
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.1733752 0.7668326 0.8213798 0.6663194
## [2,] 0.1164418 0.3516137 0.8045129 0.3993360
## [3,] 0.5650845 0.7578191 0.3634950 0.4653050
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.373935046 0.461487421 0.803293727 0.066728143
## [2,] 0.775977263 0.009529161 0.016292547 0.017358218
## [3,] 0.181110251 0.948629274 0.746058755 0.800374296
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.02100904 0.17433657 0.61038230 0.47663184
## [2,] 0.33419914 0.90762616 0.44209923 0.26682300
## [3,] 0.13323694 0.47357068 0.10477705 0.84082706
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.16789590 0.84162824 0.44362379 0.08465500
## [2,] 0.01820825 0.45579649 0.54512494 0.53637852
## [3,] 0.69643710 0.88972607 0.71702771 0.08231698
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.8138845 0.5089873 0.3984533
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.8138845 0.5089873 0.3984533
einsum::einsum('iii->i', arrD)
## [1] 0.2392709 0.3768051 0.4874895
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.2392709 0.3768051 0.4874895
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.2675719 0.9495583 0.4425053
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.2675719 0.9495583 0.4425053
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.3488224 0.02357476 0.5837042 0.25969944
## [2,] 0.7366247 0.18943005 0.3930671 0.12431767
## [3,] 0.4083971 0.01850173 0.3921930 0.02915805
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.34882240 0.02357476 0.58370422 0.25969944
## [2,] 0.73662471 0.18943005 0.39306711 0.12431767
## [3,] 0.40839713 0.01850173 0.39219304 0.02915805
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.135462563 0.6619773 0.188126929 0.5217337799
## [2,] 0.000704626 0.2206167 0.001613565 0.0167768820
## [3,] 0.966629802 0.5438191 0.045922045 0.0002539844
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03005897 0.5880323 0.6746648 0.4439815
## [2,] 0.01355869 0.1236322 0.6472410 0.1594693
## [3,] 0.31932044 0.5742898 0.1321286 0.2165088
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13982742 2.129706e-01 0.6452808116 0.0044526451
## [2,] 0.60214071 9.080491e-05 0.0002654471 0.0003013077
## [3,] 0.03280092 8.998975e-01 0.5566036660 0.6405990137
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0004413796 0.03039324 0.37256655 0.22717791
## [2,] 0.1116890648 0.82378524 0.19545173 0.07119452
## [3,] 0.0177520834 0.22426919 0.01097823 0.70699014
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0281890342 0.7083381 0.1968021 0.007166468
## [2,] 0.0003315404 0.2077504 0.2971612 0.287701916
## [3,] 0.4850246283 0.7916125 0.5141287 0.006776086
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.1354625633 0.6619772762 0.1881269290 0.5217337799
## [2,] 0.0007046260 0.2206167164 0.0016135651 0.0167768820
## [3,] 0.9666298025 0.5438191451 0.0459220448 0.0002539844
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.03005897 0.58803228 0.67466480 0.44398148
## [2,] 0.01355869 0.12363221 0.64724100 0.15946925
## [3,] 0.31932044 0.57428975 0.13212858 0.21650878
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 1.398274e-01 2.129706e-01 6.452808e-01 4.452645e-03
## [2,] 6.021407e-01 9.080491e-05 2.654471e-04 3.013077e-04
## [3,] 3.280092e-02 8.998975e-01 5.566037e-01 6.405990e-01
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.0004413796 0.0303932389 0.3725665463 0.2271779131
## [2,] 0.1116890648 0.8237852407 0.1954517272 0.0711945155
## [3,] 0.0177520834 0.2242691910 0.0109782304 0.7069901392
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.0281890342 0.7083380998 0.1968020640 0.0071664683
## [2,] 0.0003315404 0.2077504375 0.2971611983 0.2877019158
## [3,] 0.4850246283 0.7916124730 0.5141287316 0.0067760856
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.2675719 0.5040587 0.3440959
## [2,] 0.5040587 0.9495583 0.6482164
## [3,] 0.3440959 0.6482164 0.4425053
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.2675719 0.5040587 0.3440959
## [2,] 0.5040587 0.9495583 0.6482164
## [3,] 0.3440959 0.6482164 0.4425053
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2173761 0.05651104 0.2811940 0.18756213
## [2,] 0.3158878 0.16018951 0.2307507 0.12977053
## [3,] 0.2352074 0.05006287 0.2304940 0.06284763
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01567767 0.004075707 0.02028036 0.013527416
## [2,] 0.02278256 0.011553240 0.01664227 0.009359352
## [3,] 0.01696370 0.003610651 0.01662376 0.004532717
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5806739 0.1509572 0.7511497 0.5010322
## [2,] 0.8438266 0.4279121 0.6164012 0.3466542
## [3,] 0.6283063 0.1337323 0.6157154 0.1678840
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4805336 0.1249238 0.6216100 0.4146265
## [2,] 0.6983042 0.3541164 0.5100995 0.2868719
## [3,] 0.5199516 0.1106694 0.5095320 0.1389315
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2774095 0.07211787 0.3588522 0.23936173
## [2,] 0.4031274 0.20442954 0.2944778 0.16560965
## [3,] 0.3001653 0.06388889 0.2941502 0.08020445
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4355414 0.1132272 0.5634088 0.3758052
## [2,] 0.6329223 0.3209606 0.4623391 0.2600122
## [3,] 0.4712687 0.1003075 0.4618247 0.1259234
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2561696 0.06659615 0.3313766 0.22103497
## [2,] 0.3722619 0.18877737 0.2719311 0.15292973
## [3,] 0.2771831 0.05899723 0.2716286 0.07406359
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02372441 0.006167610 0.03068949 0.020470514
## [2,] 0.03447596 0.017483070 0.02518411 0.014163144
## [3,] 0.02567052 0.005463858 0.02515609 0.006859185
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1265648 0.03290291 0.1637220 0.10920590
## [2,] 0.1839220 0.09326851 0.1343519 0.07555741
## [3,] 0.1369468 0.02914854 0.1342025 0.03659231
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4266057 0.11090424 0.5518498 0.3680951
## [2,] 0.6199371 0.31437566 0.4528536 0.2546777
## [3,] 0.4616000 0.09824956 0.4523498 0.1233399
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07649936 0.01988746 0.09895826 0.06600717
## [2,] 0.11116774 0.05637416 0.08120616 0.04566906
## [3,] 0.08277458 0.01761821 0.08111582 0.02211744
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.009412516 0.002446962 0.012175868 0.008121552
## [2,] 0.013678128 0.006936302 0.009991642 0.005619141
## [3,] 0.010184621 0.002167752 0.009980527 0.002721340
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1023975 0.02662016 0.1324596 0.08835325
## [2,] 0.1488025 0.07545908 0.1086977 0.06112987
## [3,] 0.1107971 0.02358268 0.1085768 0.02960508
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06877189 0.01787856 0.08896214 0.05933956
## [2,] 0.09993830 0.05067961 0.07300324 0.04105587
## [3,] 0.07441323 0.01583853 0.07292203 0.01988328
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3337456 0.08676349 0.4317276 0.28797107
## [2,] 0.4849942 0.24594489 0.3542801 0.19924149
## [3,] 0.3611226 0.07686338 0.3538859 0.09649229
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4529005 0.1177401 0.5858643 0.3907834
## [2,] 0.6581482 0.3337529 0.4807662 0.2703753
## [3,] 0.4900517 0.1043054 0.4802314 0.1309423
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2076672 0.05398704 0.2686348 0.17918487
## [2,] 0.3017790 0.15303482 0.2204445 0.12397447
## [3,] 0.2247021 0.04782687 0.2201992 0.06004061
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4475770 0.1163561 0.5789779 0.3861900
## [2,] 0.6504122 0.3298299 0.4751152 0.2671972
## [3,] 0.4842915 0.1030793 0.4745866 0.1294031
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4851167 0.1261153 0.6275386 0.4185810
## [2,] 0.7049644 0.3574938 0.5149646 0.2896079
## [3,] 0.5249106 0.1117249 0.5143917 0.1402566
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4751549 0.1235255 0.6146522 0.4099855
## [2,] 0.6904880 0.3501527 0.5043899 0.2836609
## [3,] 0.5141317 0.1094307 0.5038288 0.1373764
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2146844 0.05581129 0.2777121 0.18523963
## [2,] 0.3119762 0.15820596 0.2278934 0.12816364
## [3,] 0.2322949 0.04944297 0.2276399 0.06206941
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3935361 0.10230718 0.5090716 0.3395611
## [2,] 0.5718809 0.29000592 0.4177493 0.2349356
## [3,] 0.4258178 0.09063346 0.4172846 0.1137789
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2358526 0.06131435 0.3050949 0.20350449
## [2,] 0.3427375 0.17380526 0.2503640 0.14080073
## [3,] 0.2551995 0.05431811 0.2500854 0.06818954
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2748147 0.07144329 0.3554956 0.23712277
## [2,] 0.3993566 0.20251733 0.2917233 0.16406056
## [3,] 0.2973576 0.06329128 0.2913988 0.07945423
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2208505 0.05741427 0.2856884 0.19055997
## [2,] 0.3209366 0.16274986 0.2344388 0.13184468
## [3,] 0.2389668 0.05086304 0.2341780 0.06385213
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4583014 0.1191441 0.5928508 0.3954436
## [2,] 0.6659968 0.3377330 0.4864994 0.2735996
## [3,] 0.4958957 0.1055492 0.4859582 0.1325038
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1069659 0.02780780 0.1383692 0.09229508
## [2,] 0.1554412 0.07882563 0.1135472 0.06385714
## [3,] 0.1157402 0.02463481 0.1134209 0.03092590
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2725600 0.07085713 0.3525789 0.23517729
## [2,] 0.3960801 0.20085577 0.2893298 0.16271452
## [3,] 0.2949180 0.06277200 0.2890080 0.07880234
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.005628036 0.001463115 0.007280330 0.004856129
## [2,] 0.008178578 0.004147431 0.005974314 0.003359859
## [3,] 0.006089702 0.001296167 0.005967667 0.001627174
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5602717 0.1456533 0.7247579 0.4834283
## [2,] 0.8141786 0.4128773 0.5947437 0.3344745
## [3,] 0.6062306 0.1290336 0.5940821 0.1619854
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4744348 0.1233383 0.6137207 0.4093642
## [2,] 0.6894417 0.3496221 0.5036255 0.2832310
## [3,] 0.5133525 0.1092649 0.5030652 0.1371683
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.009622572 0.002501570 0.01244759 0.008302798
## [2,] 0.013983379 0.007091097 0.01021462 0.005744542
## [3,] 0.010411908 0.002216129 0.01020326 0.002782071
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4406312 0.1145504 0.5699929 0.3801969
## [2,] 0.6403187 0.3247114 0.4677420 0.2630507
## [3,] 0.4767760 0.1014797 0.4672217 0.1273950
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03941043 0.010245489 0.05098066 0.03400514
## [2,] 0.05727066 0.029042465 0.04183525 0.02352748
## [3,] 0.04264326 0.009076432 0.04178871 0.01139432
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01025197 0.002665194 0.01326177 0.008845872
## [2,] 0.01489801 0.007554915 0.01088275 0.006120284
## [3,] 0.01109294 0.002361083 0.01087064 0.002964042
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4727106 0.1228901 0.6114903 0.4078765
## [2,] 0.6869360 0.3483514 0.5017952 0.2822017
## [3,] 0.5114868 0.1088678 0.5012369 0.1366697
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01240819 0.003225743 0.01605102 0.010706355
## [2,] 0.01803139 0.009143881 0.01317163 0.007407515
## [3,] 0.01342603 0.002857671 0.01315698 0.003587446
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1973820 0.05131319 0.2553299 0.17031027
## [2,] 0.2868326 0.14545537 0.2095264 0.11783431
## [3,] 0.2135732 0.04545812 0.2092933 0.05706694
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07869132 0.02045730 0.10179374 0.06789850
## [2,] 0.11435306 0.05798947 0.08353299 0.04697763
## [3,] 0.08514634 0.01812303 0.08344006 0.02275118
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1029652 0.02676777 0.1331941 0.08884316
## [2,] 0.1496276 0.07587749 0.1093004 0.06146883
## [3,] 0.1114115 0.02371344 0.1091788 0.02976924
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5360548 0.1393576 0.6934313 0.4625328
## [2,] 0.7789869 0.3950312 0.5690368 0.3200173
## [3,] 0.5800272 0.1234563 0.5684038 0.1549838
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2796965 0.07271240 0.3618106 0.24133500
## [2,] 0.4064508 0.20611483 0.2969054 0.16697492
## [3,] 0.3026399 0.06441558 0.2965751 0.08086565
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3604990 0.09371856 0.4663354 0.3110552
## [2,] 0.5238719 0.26566012 0.3826796 0.2152129
## [3,] 0.3900706 0.08302484 0.3822539 0.1042272
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2611091 0.06788025 0.3377662 0.22529693
## [2,] 0.3794398 0.19241734 0.2771744 0.15587849
## [3,] 0.2825277 0.06013480 0.2768660 0.07549167
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06188257 0.01608755 0.08005023 0.05339513
## [2,] 0.08992684 0.04560271 0.06569004 0.03694304
## [3,] 0.06695878 0.01425188 0.06561696 0.01789145
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2815044 0.07318241 0.3641493 0.24289499
## [2,] 0.4090781 0.20744716 0.2988246 0.16805424
## [3,] 0.3045961 0.06483197 0.2984922 0.08138836
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1575888 0.04096821 0.2038542 0.13597491
## [2,] 0.2290058 0.11613088 0.1672849 0.09407835
## [3,] 0.1705158 0.03629355 0.1670988 0.04556197
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4966025 0.1291012 0.6423964 0.4284915
## [2,] 0.7216553 0.3659579 0.5271571 0.2964648
## [3,] 0.5373386 0.1143702 0.5265706 0.1435773
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09916132 0.02577886 0.1282734 0.08556095
## [2,] 0.14409975 0.07307428 0.1052624 0.05919793
## [3,] 0.10729548 0.02283738 0.1051453 0.02866945
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01075401 0.002795708 0.01391120 0.009279054
## [2,] 0.01562757 0.007924879 0.01141567 0.006419994
## [3,] 0.01163616 0.002476705 0.01140297 0.003109191
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4113240 0.10693148 0.5320817 0.3549093
## [2,] 0.5977300 0.30311424 0.4366317 0.2455547
## [3,] 0.4450648 0.09473011 0.4361459 0.1189217
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4970756 0.1292242 0.6430085 0.4288998
## [2,] 0.7223430 0.3663066 0.5276594 0.2967473
## [3,] 0.5378506 0.1144792 0.5270724 0.1437142
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2691988 0.06998334 0.3482310 0.23227715
## [2,] 0.3911957 0.19837887 0.2857619 0.16070797
## [3,] 0.2912811 0.06199792 0.2854440 0.07783057
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5254828 0.1366092 0.6797555 0.4534108
## [2,] 0.7636238 0.3872405 0.5578143 0.3137059
## [3,] 0.5685880 0.1210215 0.5571938 0.1519272
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2620095 0.06811433 0.3389310 0.22607385
## [2,] 0.3807483 0.19308088 0.2781302 0.15641603
## [3,] 0.2835020 0.06034217 0.2778208 0.07575199
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3219573 0.08369889 0.4164784 0.27779956
## [2,] 0.4678635 0.23725780 0.3417664 0.19220403
## [3,] 0.3483673 0.07414847 0.3413862 0.09308406
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4234851 0.11009297 0.5478130 0.3654024
## [2,] 0.6154023 0.31207601 0.4495410 0.2528147
## [3,] 0.4582234 0.09753086 0.4490409 0.1224377
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04999825 0.01299799 0.06467687 0.04314079
## [2,] 0.07265671 0.03684487 0.05307450 0.02984826
## [3,] 0.05409958 0.01151486 0.05301546 0.01445546
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3167915 0.08235596 0.4097961 0.27334233
## [2,] 0.4603568 0.23345104 0.3362829 0.18912015
## [3,] 0.3427778 0.07295877 0.3359088 0.09159054
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04861739 0.01263901 0.06289062 0.04194932
## [2,] 0.07065007 0.03582728 0.05160869 0.02902391
## [3,] 0.05260546 0.01119684 0.05155127 0.01405622
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.21737612 0.05651104 0.28119401 0.18756213
## [2,] 0.31588775 0.16018951 0.23075068 0.12977053
## [3,] 0.23520740 0.05006287 0.23049398 0.06284763
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.015677670 0.004075707 0.020280364 0.013527416
## [2,] 0.022782558 0.011553240 0.016642275 0.009359352
## [3,] 0.016963704 0.003610651 0.016623761 0.004532717
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.5806739 0.1509572 0.7511497 0.5010322
## [2,] 0.8438266 0.4279121 0.6164012 0.3466542
## [3,] 0.6283063 0.1337323 0.6157154 0.1678840
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.04999825 0.01299799 0.06467687 0.04314079
## [2,] 0.07265671 0.03684487 0.05307450 0.02984826
## [3,] 0.05409958 0.01151486 0.05301546 0.01445546
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.31679153 0.08235596 0.40979608 0.27334233
## [2,] 0.46035675 0.23345104 0.33628286 0.18912015
## [3,] 0.34277782 0.07295877 0.33590875 0.09159054
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.04861739 0.01263901 0.06289062 0.04194932
## [2,] 0.07065007 0.03582728 0.05160869 0.02902391
## [3,] 0.05260546 0.01119684 0.05155127 0.01405622
If there is a vanishing subscript on the left or right side of ->, the summation is done for that subscript.
einsum::einsum('i->', arrA)
## [1] 2.156937
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 2.156937
einsum::einsum('ij->', arrC)
## [1] 5.862898
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 5.862898
einsum::einsum('ijk->', arrE)
## [1] 26.67113
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 26.67113
einsum::einsum('ij->i', arrC)
## [1] 2.017765 2.273042 1.572091
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 2.017765 2.273042 1.572091
einsum::einsum('ij->j', arrC)
## [1] 2.0879399 0.7247974 2.0172092 1.0329515
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 2.0879399 0.7247974 2.0172092 1.0329515
einsum::einsum('ijk->i', arrE)
## [1] 9.291234 6.663255 10.716641
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 9.291234 6.663255 10.716641
einsum::einsum('ijk->j', arrE)
## [1] 4.934681 9.059355 7.106267 5.570827
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 4.934681 9.059355 7.106267 5.570827
einsum::einsum('ijk->k', arrE)
## [1] 4.954503 6.251515 5.200774 4.785519 5.478819
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 4.954503 6.251515 5.200774 4.785519 5.478819
These are the same as what the modeSum function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 1.104268 3.057905 3.112416 2.016646
## [2,] 1.271371 2.194264 1.848199 1.349421
## [3,] 2.559042 3.807186 2.145653 2.204760
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 1.104268 3.057905 3.112416 2.016646
## [2,] 1.271371 2.194264 1.848199 1.349421
## [3,] 2.559042 3.807186 2.145653 2.204760
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.3777705 0.8549014 1.3310226 0.4884451 0.8825412
## [2,] 2.0207593 1.8762654 1.4196459 1.5555334 2.1871508
## [3,] 0.6881995 1.9893877 1.5656450 1.1572586 1.7057764
## [4,] 0.8677739 1.5309604 0.8844607 1.5842819 0.7033505
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.3777705 0.8549014 1.3310226 0.4884451 0.8825412
## [2,] 2.0207593 1.8762654 1.4196459 1.5555334 2.1871508
## [3,] 0.6881995 1.9893877 1.5656450 1.1572586 1.7057764
## [4,] 0.8677739 1.5309604 0.8844607 1.5842819 0.7033505
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.3777705 0.8549014 1.3310226 0.4884451 0.8825412
## [2,] 2.0207593 1.8762654 1.4196459 1.5555334 2.1871508
## [3,] 0.6881995 1.9893877 1.5656450 1.1572586 1.7057764
## [4,] 0.8677739 1.5309604 0.8844607 1.5842819 0.7033505
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.3777705 0.8549014 1.3310226 0.4884451 0.8825412
## [2,] 2.0207593 1.8762654 1.4196459 1.5555334 2.1871508
## [3,] 0.6881995 1.9893877 1.5656450 1.1572586 1.7057764
## [4,] 0.8677739 1.5309604 0.8844607 1.5842819 0.7033505
If we take the diagonal elements of a matrix
and add them together, we get trace.
einsum::einsum('ii->', arrB)
## [1] 1.721325
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 1.721325
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.81388454 0.4836104 0.6212086
## [2,] 0.90870213 0.5089873 0.8909507
## [3,] 0.06335515 0.1447504 0.3984533
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.81388454 0.48361041 0.62120863
## [2,] 0.90870213 0.50898735 0.89095074
## [3,] 0.06335515 0.14475039 0.39845328
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.2392709 0.13866634 0.6437537
## [2,] 0.3607552 0.71582529 0.2195930
## [3,] 0.5032380 0.02078558 0.5532102
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.57349399 0.8285790 0.3751757
## [2,] 0.22390765 0.3768051 0.8924989
## [3,] 0.06784517 0.8044561 0.7888727
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.8219880 0.1963314 0.9264409
## [2,] 0.8419245 0.6248912 0.3883659
## [3,] 0.5209261 0.1798157 0.4874895
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.23927088 0.13866634 0.64375374
## [2,] 0.36075520 0.71582529 0.21959299
## [3,] 0.50323796 0.02078558 0.55321018
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.57349399 0.82857897 0.37517570
## [2,] 0.22390765 0.37680509 0.89249886
## [3,] 0.06784517 0.80445611 0.78887274
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.8219880 0.1963314 0.9264409
## [2,] 0.8419245 0.6248912 0.3883659
## [3,] 0.5209261 0.1798157 0.4874895
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.659635
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.659635
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 3.50749
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 3.50749
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 17.28543
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 17.28543
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,] 1.1027970 0.3629381 0.7747691 0.1298825 0.5135452
## [2,] 1.4264131 1.2859542 1.1129589 1.0784477 1.7077010
## [3,] 0.2356625 1.4540344 1.2021499 0.5789965 1.0080920
## [4,] 0.5387646 0.8199595 0.6453530 1.0053626 0.3016445
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.1027970 0.3629381 0.7747691 0.1298825 0.5135452
## [2,] 1.4264131 1.2859542 1.1129589 1.0784477 1.7077010
## [3,] 0.2356625 1.4540344 1.2021499 0.5789965 1.0080920
## [4,] 0.5387646 0.8199595 0.6453530 1.0053626 0.3016445
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.2158008 1.232405 0.9638009
## [2,] 1.2324045 1.443440 1.0605225
## [3,] 0.9638009 1.060523 0.8482499
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.2158008 1.2324045 0.9638009
## [2,] 1.2324045 1.4434395 1.0605225
## [3,] 0.9638009 1.0605225 0.8482499
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.34882240 0.7366247 0.40839713
## [2,] 0.02357476 0.1894301 0.01850173
## [3,] 0.58370422 0.3930671 0.39219304
## [4,] 0.25969944 0.1243177 0.02915805
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.34882240 0.73662471 0.40839713
## [2,] 0.02357476 0.18943005 0.01850173
## [3,] 0.58370422 0.39306711 0.39219304
## [4,] 0.25969944 0.12431767 0.02915805
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.1354626 0.03005897 0.139827418 0.0004413796 0.028189034
## [2,] 0.6619773 0.58803228 0.212970640 0.0303932389 0.708338100
## [3,] 0.1881269 0.67466480 0.645280812 0.3725665463 0.196802064
## [4,] 0.5217338 0.44398148 0.004452645 0.2271779131 0.007166468
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.000704626 0.01355869 6.021407e-01 0.11168906 0.0003315404
## [2,] 0.220616716 0.12363221 9.080491e-05 0.82378524 0.2077504375
## [3,] 0.001613565 0.64724100 2.654471e-04 0.19545173 0.2971611983
## [4,] 0.016776882 0.15946925 3.013077e-04 0.07119452 0.2877019158
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9666298025 0.3193204 0.03280092 0.01775208 0.485024628
## [2,] 0.5438191451 0.5742898 0.89989750 0.22426919 0.791612473
## [3,] 0.0459220448 0.1321286 0.55660367 0.01097823 0.514128732
## [4,] 0.0002539844 0.2165088 0.64059901 0.70699014 0.006776086
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.1354625633 0.0300589690 0.1398274184 0.0004413796 0.0281890342
## [2,] 0.6619772762 0.5880322844 0.2129706396 0.0303932389 0.7083380998
## [3,] 0.1881269290 0.6746648009 0.6452808116 0.3725665463 0.1968020640
## [4,] 0.5217337799 0.4439814790 0.0044526451 0.2271779131 0.0071664683
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 7.046260e-04 1.355869e-02 6.021407e-01 1.116891e-01 3.315404e-04
## [2,] 2.206167e-01 1.236322e-01 9.080491e-05 8.237852e-01 2.077504e-01
## [3,] 1.613565e-03 6.472410e-01 2.654471e-04 1.954517e-01 2.971612e-01
## [4,] 1.677688e-02 1.594693e-01 3.013077e-04 7.119452e-02 2.877019e-01
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9666298025 0.3193204393 0.0328009229 0.0177520834 0.4850246283
## [2,] 0.5438191451 0.5742897517 0.8998974991 0.2242691910 0.7916124730
## [3,] 0.0459220448 0.1321285827 0.5566036660 0.0109782304 0.5141287316
## [4,] 0.0002539844 0.2165087782 0.6405990137 0.7069901392 0.0067760856
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 2.337720 0.6659381 1.950845
## [2,] 2.427907 1.6719044 2.151704
## [3,] 1.705444 0.8191572 2.676173
## [4,] 1.282360 1.9507475 1.552412
## [5,] 1.537803 1.5555082 2.385508
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 2.3377197 0.6659381 1.9508455
## [2,] 2.4279070 1.6719044 2.1517035
## [3,] 1.7054443 0.8191572 2.6761726
## [4,] 1.2823597 1.9507475 1.5524117
## [5,] 1.5378029 1.5555082 2.3855079
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.024442405 0.005423738 0.025229984 7.964103e-05 0.0050863335
## [2,] 0.008072549 0.007170819 0.002597092 3.706334e-04 0.0086379009
## [3,] 0.056802059 0.203704755 0.194832707 1.124908e-01 0.0594213841
## [4,] 0.070087449 0.059642543 0.000598149 3.051809e-02 0.0009627122
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0005057848 0.009732506 4.322202e-01 0.080171082 0.0002379817
## [2,] 0.0407237822 0.022821350 1.676174e-05 0.152063050 0.0383487874
## [3,] 0.0006180363 0.247909707 1.016730e-04 0.074862965 0.1138202704
## [4,] 0.0020323800 0.019318376 3.650093e-05 0.008624625 0.0348526993
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.626045e-01 0.086749829 0.008911031 0.004822711 0.1317667091
## [2,] 6.693080e-03 0.007068098 0.011075531 0.002760204 0.0097428075
## [3,] 1.198065e-02 0.034471159 0.145212891 0.002864122 0.1341315626
## [4,] 4.926345e-06 0.004199458 0.012425217 0.013712956 0.0001314306
DelayedTensor::einsum('i,ij,ijk,ijk,ji->jki',
darrA, darrC, darrE, darrE, t(darrC))
## <4 x 5 x 3> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.444240e-02 5.423738e-03 2.522998e-02 7.964103e-05 5.086334e-03
## [2,] 8.072549e-03 7.170819e-03 2.597092e-03 3.706334e-04 8.637901e-03
## [3,] 5.680206e-02 2.037048e-01 1.948327e-01 1.124908e-01 5.942138e-02
## [4,] 7.008745e-02 5.964254e-02 5.981490e-04 3.051809e-02 9.627122e-04
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 5.057848e-04 9.732506e-03 4.322202e-01 8.017108e-02 2.379817e-04
## [2,] 4.072378e-02 2.282135e-02 1.676174e-05 1.520630e-01 3.834879e-02
## [3,] 6.180363e-04 2.479097e-01 1.016730e-04 7.486297e-02 1.138203e-01
## [4,] 2.032380e-03 1.931838e-02 3.650093e-05 8.624625e-03 3.485270e-02
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.626045e-01 8.674983e-02 8.911031e-03 4.822711e-03 1.317667e-01
## [2,] 6.693080e-03 7.068098e-03 1.107553e-02 2.760204e-03 9.742808e-03
## [3,] 1.198065e-02 3.447116e-02 1.452129e-01 2.864122e-03 1.341316e-01
## [4,] 4.926345e-06 4.199458e-03 1.242522e-02 1.371296e-02 1.314306e-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 version 4.5.1 Patched (2025-08-23 r88802)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.3 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.22-bioc/R/lib/libRblas.so
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0 LAPACK version 3.12.0
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_GB LC_COLLATE=C
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## time zone: America/New_York
## tzcode source: system (glibc)
##
## 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