DelayedTensor 1.15.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2025-04-04 14:56:50.301149
Compiled: Tue Oct 7 17:20:23 2025
einsum
einsum
is an easy and intuitive way to write tensor operations.
It was originally introduced by
Numpy
1 https://numpy.org/doc/stable/reference/generated/numpy.einsum.html
package of Python but similar tools have been implemented in other languages
(e.g. R, Julia) inspired by Numpy
.
In this vignette, we will use CRAN einsum package first.
einsum
is named after
Einstein summation2 https://en.wikipedia.org/wiki/Einstein_notation
introduced by Albert Einstein,
which is a notational convention that implies summation over
a set of indexed terms in a formula.
Here, we consider a simple example of einsum
; matrix multiplication.
If we naively implement the matrix multiplication,
the calculation would look like the following in a for loop.
A <- matrix(runif(3*4), nrow=3, ncol=4)
B <- matrix(runif(4*5), nrow=4, ncol=5)
C <- matrix(0, nrow=3, ncol=5)
I <- nrow(A)
J <- ncol(A)
K <- ncol(B)
for(i in 1:I){
for(j in 1:J){
for(k in 1:K){
C[i,k] = C[i,k] + A[i,j] * B[j,k]
}
}
}
Therefore, any programming language can implement this. However, when analyzing tensor data, such operations tend to be more complicated and increase the possibility of causing bugs because the order of tensors is larger or more tensors are handled simultaneously. In addition, several programming languages, especially R, are known to significantly slow down the speed of computation if the code is written in for loop.
Obviously, in the case of the R language, it should be executed using the built-in matrix multiplication function (%*%) prepared by the R, as shown below.
C <- A %*% B
However, more complex operations than matrix multiplication are not always provided by programming languages as standard.
einsum
is a function that solves such a problem.
To put it simply, einsum
is a wrapper for the for loop above.
Like the Einstein summation, it omits many notations such as for,
array size (e.g. I, J, and K), brackets (e.g. {}, (), and []),
and even addition operator (+) and
extracts the array subscripts (e.g. i, j, and k)
to concisely express the tensor operation as follows.
suppressPackageStartupMessages(library("einsum"))
C <- einsum('ij,jk->ik', A, B)
DelayedTensor
CRAN einsum is easy to use because the syntax is almost
the same as that of Numpy
‘s einsum
,
except that it prohibits the implicit modes that do not use’->’.
It is extremely fast because the internal calculation
is actually performed by C++.
When the input tensor is huge, however,
it is not scalable because it assumes that the input is R’s standard array.
Using einsum
of DelayedTensor,
we can augment the CRAN einsum
’s functionality;
in DelayedTensor,
the input DelayedArray objects are divided into
multiple block tensors and the CRAN einsum
is incremently applied in the block processing.
A surprisingly large number of tensor operations can be handled
uniformly in einsum
.
In more detail, einsum
is capable of performing any tensor operation
that can be described by a combination of the following
three operations3 https://ajcr.net/Basic-guide-to-einsum/.
Some typical operations are introduced below. Here we use the arrays and DelayedArray objects below.
suppressPackageStartupMessages(library("DelayedTensor"))
suppressPackageStartupMessages(library("DelayedArray"))
arrA <- array(runif(3), dim=c(3))
arrB <- array(runif(3*3), dim=c(3,3))
arrC <- array(runif(3*4), dim=c(3,4))
arrD <- array(runif(3*3*3), dim=c(3,3,3))
arrE <- array(runif(3*4*5), dim=c(3,4,5))
darrA <- DelayedArray(arrA)
darrB <- DelayedArray(arrB)
darrC <- DelayedArray(arrC)
darrD <- DelayedArray(arrD)
darrE <- DelayedArray(arrE)
If the same subscript is written on both sides of ->,
einsum
will simply output the object without any calculation.
einsum::einsum('i->i', arrA)
## [1] 0.7464287 0.9937568 0.3488195
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.7464287 0.9937568 0.3488195
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.3938490 0.19050346 0.2354697 0.8340056
## [2,] 0.1841848 0.08083236 0.8199366 0.3067952
## [3,] 0.3532352 0.08737001 0.7662162 0.8896238
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.39384901 0.19050346 0.23546968 0.83400560
## [2,] 0.18418485 0.08083236 0.81993656 0.30679521
## [3,] 0.35323516 0.08737001 0.76621624 0.88962376
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8409000 0.8327254 0.6905697 0.3049990
## [2,] 0.8909192 0.6214691 0.6117555 0.9130678
## [3,] 0.7483175 0.1436356 0.8121864 0.3884238
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.46327872 0.5860348 0.7762928 0.2269158
## [2,] 0.18179112 0.5302921 0.7318139 0.6881397
## [3,] 0.09256609 0.8507183 0.3227320 0.8737924
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7279053 0.9597196 0.6930489 0.7306041
## [2,] 0.3171545 0.2025807 0.6143439 0.9454256
## [3,] 0.7525675 0.0326203 0.7016056 0.9475327
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5789996 0.548921241 0.5512479 0.88675762
## [2,] 0.8024132 0.840605920 0.7725821 0.25904095
## [3,] 0.2522553 0.007199195 0.1447312 0.01802865
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7705591 0.9820509 0.2883466 0.63035587
## [2,] 0.5802914 0.7022195 0.9699766 0.75485321
## [3,] 0.4695705 0.7371552 0.2203343 0.08687086
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.8409000 0.8327254 0.6905697 0.3049990
## [2,] 0.8909192 0.6214691 0.6117555 0.9130678
## [3,] 0.7483175 0.1436356 0.8121864 0.3884238
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.46327872 0.58603482 0.77629275 0.22691578
## [2,] 0.18179112 0.53029211 0.73181390 0.68813972
## [3,] 0.09256609 0.85071827 0.32273201 0.87379240
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.7279053 0.9597196 0.6930489 0.7306041
## [2,] 0.3171545 0.2025807 0.6143439 0.9454256
## [3,] 0.7525675 0.0326203 0.7016056 0.9475327
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.578999587 0.548921241 0.551247867 0.886757621
## [2,] 0.802413167 0.840605920 0.772582080 0.259040954
## [3,] 0.252255335 0.007199195 0.144731202 0.018028645
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.77055914 0.98205095 0.28834662 0.63035587
## [2,] 0.58029139 0.70221954 0.96997663 0.75485321
## [3,] 0.46957046 0.73715524 0.22033434 0.08687086
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.6471139 0.7334695 0.4702435
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.6471139 0.7334695 0.4702435
einsum::einsum('iii->i', arrD)
## [1] 0.9988134 0.1558760 0.5662890
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.9988134 0.1558760 0.5662890
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.5571558 0.9875526 0.1216750
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.5571558 0.9875526 0.1216750
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.15511704 0.036291567 0.05544597 0.6955653
## [2,] 0.03392406 0.006533870 0.67229596 0.0941233
## [3,] 0.12477508 0.007633518 0.58708732 0.7914304
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.155117039 0.036291567 0.055445970 0.695565348
## [2,] 0.033924057 0.006533870 0.672295959 0.094123298
## [3,] 0.124775078 0.007633518 0.587087320 0.791430441
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7071128 0.69343160 0.4768865 0.0930244
## [2,] 0.7937371 0.38622380 0.3742448 0.8336928
## [3,] 0.5599790 0.02063118 0.6596467 0.1508731
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.214627171 0.3434368 0.6026304 0.05149077
## [2,] 0.033048011 0.2812097 0.5355516 0.47353627
## [3,] 0.008568482 0.7237216 0.1041560 0.76351316
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5298462 0.921061711 0.4803167 0.5337824
## [2,] 0.1005870 0.041038928 0.3774184 0.8938295
## [3,] 0.5663578 0.001064084 0.4922504 0.8978182
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.33524052 3.013145e-01 0.30387421 0.7863390781
## [2,] 0.64386689 7.066183e-01 0.59688307 0.0671022158
## [3,] 0.06363275 5.182842e-05 0.02094712 0.0003250321
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5937614 0.9644241 0.08314377 0.397348529
## [2,] 0.3367381 0.4931123 0.94085465 0.569803365
## [3,] 0.2204964 0.5433979 0.04854722 0.007546546
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.70711278 0.69343160 0.47688653 0.09302440
## [2,] 0.79373709 0.38622380 0.37424476 0.83369285
## [3,] 0.55997904 0.02063118 0.65964673 0.15087308
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.214627171 0.343436812 0.602630441 0.051490770
## [2,] 0.033048011 0.281209719 0.535551579 0.473536271
## [3,] 0.008568482 0.723721569 0.104155951 0.763513158
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.529846186 0.921061711 0.480316733 0.533782365
## [2,] 0.100586959 0.041038928 0.377418381 0.893829529
## [3,] 0.566357828 0.001064084 0.492250368 0.897818153
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 3.352405e-01 3.013145e-01 3.038742e-01 7.863391e-01
## [2,] 6.438669e-01 7.066183e-01 5.968831e-01 6.710222e-02
## [3,] 6.363275e-02 5.182842e-05 2.094712e-02 3.250321e-04
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.593761382 0.964424061 0.083143774 0.397348529
## [2,] 0.336738095 0.493112288 0.940854654 0.569803365
## [3,] 0.220496412 0.543397855 0.048547223 0.007546546
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.5571558 0.7417686 0.2603689
## [2,] 0.7417686 0.9875526 0.3466417
## [3,] 0.2603689 0.3466417 0.1216750
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.5571558 0.7417686 0.2603689
## [2,] 0.7417686 0.9875526 0.3466417
## [3,] 0.2603689 0.3466417 0.1216750
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3311876 0.16019435 0.1980064 0.7013153
## [2,] 0.1548810 0.06797193 0.6894846 0.2579841
## [3,] 0.2970354 0.07346944 0.6443112 0.7480846
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3508877 0.16972319 0.2097845 0.7430316
## [2,] 0.1640938 0.07201510 0.7304973 0.2733298
## [3,] 0.3147040 0.07783962 0.6826368 0.7925829
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2947241 0.14255707 0.1762061 0.6241010
## [2,] 0.1378287 0.06048826 0.6135729 0.2295802
## [3,] 0.2643320 0.06538050 0.5733730 0.6657210
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3279681 0.15863707 0.1960816 0.6944977
## [2,] 0.1533754 0.06731116 0.6827820 0.2554762
## [3,] 0.2941479 0.07275523 0.6380477 0.7408123
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2447650 0.11839201 0.1463371 0.5183087
## [2,] 0.1144652 0.05023481 0.5095652 0.1906637
## [3,] 0.2195247 0.05429776 0.4761797 0.5528737
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05657074 0.02736308 0.03382183 0.11979289
## [2,] 0.02645550 0.01161040 0.11777208 0.04406671
## [3,] 0.05073714 0.01254944 0.11005593 0.12778164
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2719802 0.13155592 0.1626082 0.5759390
## [2,] 0.1271925 0.05582038 0.5662234 0.2118635
## [3,] 0.2439335 0.06033508 0.5291257 0.6143472
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2409393 0.11654153 0.1440499 0.5102075
## [2,] 0.1126761 0.04944964 0.5016007 0.1876836
## [3,] 0.2160935 0.05344908 0.4687370 0.5442322
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3198788 0.15472431 0.1912453 0.6773680
## [2,] 0.1495924 0.06565094 0.6659413 0.2491749
## [3,] 0.2868928 0.07096073 0.6223104 0.7225403
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1201236 0.05810337 0.07181802 0.25437088
## [2,] 0.0561762 0.02465379 0.25007984 0.09357223
## [3,] 0.1077364 0.02664777 0.23369519 0.27133437
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3596109 0.17394258 0.2149998 0.7615037
## [2,] 0.1681733 0.07380542 0.7486577 0.2801248
## [3,] 0.3225277 0.07977474 0.6996074 0.8122868
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.15298034 0.07399608 0.09146204 0.3239477
## [2,] 0.07154178 0.03139721 0.31848290 0.1191666
## [3,] 0.13720496 0.03393659 0.29761665 0.3455511
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.18246186 0.08825620 0.1090881 0.3863770
## [2,] 0.08532892 0.03744791 0.3798592 0.1421317
## [3,] 0.16364633 0.04047667 0.3549717 0.4121438
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07159825 0.03463184 0.0428063 0.15161481
## [2,] 0.03348317 0.01469460 0.1490572 0.05577264
## [3,] 0.06421501 0.01588309 0.1392913 0.16172570
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03645706 0.017634161 0.02179651 0.07720064
## [2,] 0.01704927 0.007482335 0.07589832 0.02839883
## [3,] 0.03269760 0.008087500 0.07092564 0.08234900
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2308092 0.11164166 0.1379934 0.4887563
## [2,] 0.1079387 0.04737057 0.4805114 0.1797927
## [3,] 0.2070081 0.05120187 0.4490294 0.5213505
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.20885502 0.10102248 0.1248677 0.4422666
## [2,] 0.09767177 0.04286476 0.4348059 0.1626911
## [3,] 0.18731782 0.04633163 0.4063184 0.4717605
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3350545 0.16206477 0.2003184 0.7095038
## [2,] 0.1566894 0.06876556 0.6975350 0.2609963
## [3,] 0.3005036 0.07432726 0.6518341 0.7568192
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3057421 0.14788645 0.1827934 0.6474325
## [2,] 0.1429814 0.06274957 0.6365108 0.2381629
## [3,] 0.2742139 0.06782470 0.5948081 0.6906085
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2882242 0.13941308 0.1723200 0.6103369
## [2,] 0.1347890 0.05915424 0.6000410 0.2245170
## [3,] 0.2585024 0.06393859 0.5607277 0.6510390
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12710768 0.06148156 0.0759936 0.26916031
## [2,] 0.05944235 0.02608719 0.2646198 0.09901263
## [3,] 0.11400029 0.02819710 0.2472825 0.28711007
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08937055 0.04322824 0.05343179 0.18924903
## [2,] 0.04179445 0.01834214 0.18605654 0.06961667
## [3,] 0.08015463 0.01982563 0.17386655 0.20186967
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2710231 0.13109299 0.1620360 0.5739124
## [2,] 0.1267449 0.05562395 0.5642309 0.2111180
## [3,] 0.2430751 0.06012277 0.5272638 0.6121854
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3441423 0.16646047 0.2057516 0.7287478
## [2,] 0.1609393 0.07063070 0.7164543 0.2680753
## [3,] 0.3086542 0.07634325 0.6695139 0.7773465
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2866848 0.1386685 0.1713996 0.6070771
## [2,] 0.1340691 0.0588383 0.5968362 0.2233179
## [3,] 0.2571218 0.0635971 0.5577329 0.6475619
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12491097 0.06041902 0.07468026 0.26450861
## [2,] 0.05841505 0.02563634 0.26004655 0.09730147
## [3,] 0.11203011 0.02770979 0.24300891 0.28214816
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2963980 0.14336671 0.1772068 0.6276455
## [2,] 0.1386115 0.06083180 0.6170576 0.2308841
## [3,] 0.2658333 0.06575183 0.5766294 0.6695019
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3779846 0.18282990 0.2259849 0.8004115
## [2,] 0.1767658 0.07757640 0.7869092 0.2944374
## [3,] 0.3390067 0.08385071 0.7353527 0.8537894
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07978620 0.03859232 0.04770161 0.16895341
## [2,] 0.03731229 0.01637507 0.16610330 0.06215078
## [3,] 0.07155862 0.01769947 0.15522060 0.18022058
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.012847472 0.006214280 0.007681091 0.02720551
## [2,] 0.006008165 0.002636776 0.026746576 0.01000775
## [3,] 0.011522636 0.002850036 0.024994203 0.02901979
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2729566 0.13202820 0.1631920 0.5780066
## [2,] 0.1276491 0.05602077 0.5682561 0.2126241
## [3,] 0.2448092 0.06055169 0.5310253 0.6165527
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2419587 0.11703463 0.1446594 0.5123662
## [2,] 0.1131528 0.04965886 0.5037230 0.1884778
## [3,] 0.2170079 0.05367523 0.4707202 0.5465349
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2763267 0.13365829 0.1652068 0.5851430
## [2,] 0.1292251 0.05671243 0.5752721 0.2152492
## [3,] 0.2478318 0.06129928 0.5375816 0.6241650
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2877477 0.13918261 0.1720351 0.6093279
## [2,] 0.1345662 0.05905645 0.5990490 0.2241458
## [3,] 0.2580751 0.06383289 0.5598007 0.6499628
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3723549 0.18010684 0.2226191 0.7884902
## [2,] 0.1741331 0.07642098 0.7751890 0.2900520
## [3,] 0.3339576 0.08260184 0.7244004 0.8410731
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3731848 0.18050825 0.2231152 0.7902476
## [2,] 0.1745212 0.07659130 0.7769167 0.2906985
## [3,] 0.3347019 0.08278594 0.7260149 0.8429476
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2280384 0.1103014 0.1363368 0.4828889
## [2,] 0.1066429 0.0468019 0.4747429 0.1776343
## [3,] 0.2045230 0.0505872 0.4436389 0.5150918
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3160296 0.15286248 0.1889440 0.6692171
## [2,] 0.1477923 0.06486095 0.6579279 0.2461765
## [3,] 0.2834405 0.07010685 0.6148220 0.7138458
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09935051 0.04805551 0.05939848 0.21038236
## [2,] 0.04646161 0.02039039 0.20683337 0.07739073
## [3,] 0.08910545 0.02203955 0.19328213 0.22441234
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2161921 0.10457139 0.1292543 0.4578034
## [2,] 0.1011030 0.04437060 0.4500806 0.1684064
## [3,] 0.1938983 0.04795925 0.4205924 0.4883334
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3310718 0.16013833 0.1979372 0.7010700
## [2,] 0.1548269 0.06794816 0.6892435 0.2578939
## [3,] 0.2969316 0.07344375 0.6440859 0.7478230
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.002835396 0.0013714716 0.001695192 0.006004169
## [2,] 0.001325983 0.0005819279 0.005902884 0.002208679
## [3,] 0.002543009 0.0006289938 0.005516140 0.006404575
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2171084 0.10501462 0.1298022 0.4597438
## [2,] 0.1015315 0.04455866 0.4519883 0.1691202
## [3,] 0.1947201 0.04816253 0.4223751 0.4904032
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3042807 0.14717956 0.1819197 0.6443378
## [2,] 0.1422979 0.06244963 0.6334683 0.2370245
## [3,] 0.2729032 0.06750050 0.5919649 0.6873074
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05700224 0.02757179 0.03407981 0.12070663
## [2,] 0.02665729 0.01169896 0.11867040 0.04440284
## [3,] 0.05112415 0.01264517 0.11089540 0.12875632
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3492486 0.16893039 0.2088045 0.7395608
## [2,] 0.1633273 0.07167871 0.7270850 0.2720530
## [3,] 0.3132340 0.07747602 0.6794481 0.7888807
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10202302 0.04934820 0.06099629 0.21604161
## [2,] 0.04771142 0.02093889 0.21239715 0.07947252
## [3,] 0.09150237 0.02263241 0.19848138 0.23044899
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.007100564 0.003434519 0.004245199 0.015035991
## [2,] 0.003320603 0.001457298 0.014782345 0.005531102
## [3,] 0.006368351 0.001575163 0.013813841 0.016038711
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3034839 0.14679418 0.1814433 0.6426506
## [2,] 0.1419253 0.06228611 0.6318096 0.2364038
## [3,] 0.2721886 0.06732376 0.5904149 0.6855077
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2285472 0.11054752 0.1366410 0.4839663
## [2,] 0.1068809 0.04690632 0.4758021 0.1780306
## [3,] 0.2049793 0.05070006 0.4446287 0.5162410
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.18493986 0.08945479 0.1105696 0.3916244
## [2,] 0.08648776 0.03795649 0.3850180 0.1440620
## [3,] 0.16586879 0.04102637 0.3597925 0.4177410
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3867798 0.18708410 0.2312432 0.8190360
## [2,] 0.1808789 0.07938149 0.8052195 0.3012885
## [3,] 0.3468949 0.08580180 0.7524634 0.8736559
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2765685 0.13377525 0.1653514 0.5856550
## [2,] 0.1293382 0.05676206 0.5757755 0.2154376
## [3,] 0.2480486 0.06135293 0.5380520 0.6247112
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2903279 0.14043062 0.1735777 0.6147916
## [2,] 0.1357728 0.05958599 0.6044205 0.2261557
## [3,] 0.2603892 0.06440526 0.5648203 0.6557908
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11356503 0.05493103 0.06789689 0.24048270
## [2,] 0.05310908 0.02330774 0.23642594 0.08846336
## [3,] 0.10185416 0.02519285 0.22093586 0.25652001
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3820243 0.18478390 0.2284001 0.8089659
## [2,] 0.1786550 0.07840550 0.7953193 0.2975842
## [3,] 0.3426298 0.08474687 0.7432118 0.8629143
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08677846 0.04197445 0.05188206 0.18376008
## [2,] 0.04058225 0.01781014 0.18066018 0.06759752
## [3,] 0.07782984 0.01925061 0.16882375 0.19601467
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2482650 0.12008497 0.1484297 0.5257203
## [2,] 0.1161020 0.05095315 0.5168518 0.1933902
## [3,] 0.2226639 0.05507420 0.4829889 0.5607796
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2972982 0.14380215 0.1777450 0.6295518
## [2,] 0.1390325 0.06101656 0.6189317 0.2315853
## [3,] 0.2666407 0.06595153 0.5783808 0.6715354
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03421400 0.016549199 0.02045545 0.07245078
## [2,] 0.01600030 0.007021976 0.07122859 0.02665156
## [3,] 0.03068584 0.007589908 0.06656186 0.07728238
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.33118762 0.16019435 0.19800645 0.70131530
## [2,] 0.15488103 0.06797193 0.68948464 0.25798408
## [3,] 0.29703544 0.07346944 0.64431122 0.74808461
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.35088766 0.16972319 0.20978447 0.74303164
## [2,] 0.16409382 0.07201510 0.73049725 0.27332975
## [3,] 0.31470400 0.07783962 0.68263678 0.79258292
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.29472409 0.14255707 0.17620608 0.62410097
## [2,] 0.13782874 0.06048826 0.61357285 0.22958021
## [3,] 0.26433204 0.06538050 0.57337300 0.66572101
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.24826503 0.12008497 0.14842970 0.52572033
## [2,] 0.11610200 0.05095315 0.51685183 0.19339016
## [3,] 0.22266386 0.05507420 0.48298891 0.56077957
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.29729819 0.14380215 0.17774504 0.62955181
## [2,] 0.13903252 0.06101656 0.61893174 0.23158535
## [3,] 0.26664069 0.06595153 0.57838078 0.67153535
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.034214001 0.016549199 0.020455453 0.072450782
## [2,] 0.016000296 0.007021976 0.071228592 0.026651563
## [3,] 0.030685841 0.007589908 0.066561862 0.077282380
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.089005
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 2.089005
einsum::einsum('ij->', arrC)
## [1] 5.142022
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 5.142022
einsum::einsum('ijk->', arrE)
## [1] 34.60381
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 34.60381
einsum::einsum('ij->i', arrC)
## [1] 1.653828 1.391749 2.096445
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 1.653828 1.391749 2.096445
einsum::einsum('ij->j', arrC)
## [1] 0.9312690 0.3587058 1.8216225 2.0304246
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 0.9312690 0.3587058 1.8216225 2.0304246
einsum::einsum('ijk->i', arrE)
## [1] 13.070233 12.930736 8.602843
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 13.070233 12.930736 8.602843
einsum::einsum('ijk->j', arrE)
## [1] 8.469489 8.577948 8.901567 8.654808
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 8.469489 8.577948 8.901567 8.654808
einsum::einsum('ijk->k', arrE)
## [1] 7.798969 6.324368 7.625109 5.662783 7.192584
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 7.798969 6.324368 7.625109 5.662783 7.192584
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 3.381643 3.909452 2.999506 2.779632
## [2,] 2.772569 2.897167 3.700472 3.560527
## [3,] 2.315277 1.771329 2.201590 2.314648
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 3.381643 3.909452 2.999506 2.779632
## [2,] 2.772569 2.897167 3.700472 3.560527
## [3,] 2.315277 1.771329 2.201590 2.314648
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.480137 0.7376359 1.797627 1.633668 1.820421
## [2,] 1.597830 1.9670452 1.194921 1.396726 2.421426
## [3,] 2.114512 1.8308387 2.008998 1.468561 1.478658
## [4,] 1.606491 1.7888479 2.623562 1.163827 1.472080
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.4801367 0.7376359 1.7976273 1.6336681 1.8204210
## [2,] 1.5978301 1.9670452 1.1949206 1.3967264 2.4214257
## [3,] 2.1145116 1.8308387 2.0089983 1.4685611 1.4786576
## [4,] 1.6064907 1.7888479 2.6235624 1.1638272 1.4720799
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.480137 0.7376359 1.797627 1.633668 1.820421
## [2,] 1.597830 1.9670452 1.194921 1.396726 2.421426
## [3,] 2.114512 1.8308387 2.008998 1.468561 1.478658
## [4,] 1.606491 1.7888479 2.623562 1.163827 1.472080
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.4801367 0.7376359 1.7976273 1.6336681 1.8204210
## [2,] 1.5978301 1.9670452 1.1949206 1.3967264 2.4214257
## [3,] 2.1145116 1.8308387 2.0089983 1.4685611 1.4786576
## [4,] 1.6064907 1.7888479 2.6235624 1.1638272 1.4720799
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.850827
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 1.850827
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.6471139 0.9343707 0.3324600
## [2,] 0.9060212 0.7334695 0.1572258
## [3,] 0.2545048 0.2618693 0.4702435
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.6471139 0.9343707 0.3324600
## [2,] 0.9060212 0.7334695 0.1572258
## [3,] 0.2545048 0.2618693 0.4702435
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.9988134 0.3072364 0.7541753
## [2,] 0.9328845 0.5040405 0.0388952
## [3,] 0.8448292 0.6187987 0.8880720
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.53919013 0.02131117 0.9197503
## [2,] 0.25579051 0.15587602 0.9701427
## [3,] 0.02920369 0.86020803 0.3514196
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.1199074 0.6308893 0.1380515
## [2,] 0.1138969 0.3886886 0.3001594
## [3,] 0.9043092 0.3671628 0.5662890
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.9988134 0.3072364 0.7541753
## [2,] 0.9328845 0.5040405 0.0388952
## [3,] 0.8448292 0.6187987 0.8880720
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.53919013 0.02131117 0.91975034
## [2,] 0.25579051 0.15587602 0.97014274
## [3,] 0.02920369 0.86020803 0.35141962
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.1199074 0.6308893 0.1380515
## [2,] 0.1138969 0.3886886 0.3001594
## [3,] 0.9043092 0.3671628 0.5662890
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.666383
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.666383
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 3.260223
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 3.260223
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 24.74571
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 24.74571
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,] 2.060829 0.2562437 1.1967910 1.0427402 1.1509959
## [2,] 1.100287 1.3483681 0.9631647 1.0079847 2.0009342
## [3,] 1.510778 1.2423380 1.3499855 0.9217044 1.0725457
## [4,] 1.077590 1.2885402 2.3254300 0.8537663 0.9746984
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.0608289 0.2562437 1.1967910 1.0427402 1.1509959
## [2,] 1.1002866 1.3483681 0.9631647 1.0079847 2.0009342
## [3,] 1.5107780 1.2423380 1.3499855 0.9217044 1.0725457
## [4,] 1.0775903 1.2885402 2.3254300 0.8537663 0.9746984
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 0.9424199 0.5368790 1.0781375
## [2,] 0.5368790 0.8068772 0.9733039
## [3,] 1.0781375 0.9733039 1.5109264
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.9424199 0.5368790 1.0781375
## [2,] 0.5368790 0.8068772 0.9733039
## [3,] 1.0781375 0.9733039 1.5109264
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.15511704 0.03392406 0.124775078
## [2,] 0.03629157 0.00653387 0.007633518
## [3,] 0.05544597 0.67229596 0.587087320
## [4,] 0.69556535 0.09412330 0.791430441
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.155117039 0.033924057 0.124775078
## [2,] 0.036291567 0.006533870 0.007633518
## [3,] 0.055445970 0.672295959 0.587087320
## [4,] 0.695565348 0.094123298 0.791430441
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.7071128 0.21462717 0.5298462 0.3352405 0.59376138
## [2,] 0.6934316 0.34343681 0.9210617 0.3013145 0.96442406
## [3,] 0.4768865 0.60263044 0.4803167 0.3038742 0.08314377
## [4,] 0.0930244 0.05149077 0.5337824 0.7863391 0.39734853
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.7937371 0.03304801 0.10058696 0.64386689 0.3367381
## [2,] 0.3862238 0.28120972 0.04103893 0.70661831 0.4931123
## [3,] 0.3742448 0.53555158 0.37741838 0.59688307 0.9408547
## [4,] 0.8336928 0.47353627 0.89382953 0.06710222 0.5698034
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.55997904 0.008568482 0.566357828 6.363275e-02 0.220496412
## [2,] 0.02063118 0.723721569 0.001064084 5.182842e-05 0.543397855
## [3,] 0.65964673 0.104155951 0.492250368 2.094712e-02 0.048547223
## [4,] 0.15087308 0.763513158 0.897818153 3.250321e-04 0.007546546
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.70711278 0.21462717 0.52984619 0.33524052 0.59376138
## [2,] 0.69343160 0.34343681 0.92106171 0.30131453 0.96442406
## [3,] 0.47688653 0.60263044 0.48031673 0.30387421 0.08314377
## [4,] 0.09302440 0.05149077 0.53378236 0.78633908 0.39734853
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.79373709 0.03304801 0.10058696 0.64386689 0.33673809
## [2,] 0.38622380 0.28120972 0.04103893 0.70661831 0.49311229
## [3,] 0.37424476 0.53555158 0.37741838 0.59688307 0.94085465
## [4,] 0.83369285 0.47353627 0.89382953 0.06710222 0.56980336
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 5.599790e-01 8.568482e-03 5.663578e-01 6.363275e-02 2.204964e-01
## [2,] 2.063118e-02 7.237216e-01 1.064084e-03 5.182842e-05 5.433979e-01
## [3,] 6.596467e-01 1.041560e-01 4.922504e-01 2.094712e-02 4.854722e-02
## [4,] 1.508731e-01 7.635132e-01 8.978182e-01 3.250321e-04 7.546546e-03
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 2.669194 3.037212 2.0925633
## [2,] 2.052522 2.132037 2.1398088
## [3,] 3.111278 2.079505 2.4343260
## [4,] 2.565926 2.674642 0.4222144
## [5,] 2.671313 3.007341 1.5139309
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 2.6691941 3.0372116 2.0925633
## [2,] 2.0525221 2.1320368 2.1398088
## [3,] 3.1112779 2.0795046 2.4343260
## [4,] 2.5659263 2.6746421 0.4222144
## [5,] 2.6713126 3.0073408 1.5139309
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.08187221 0.024850352 0.06134761 0.03881543 0.068747956
## [2,] 0.01878442 0.009303383 0.02495070 0.00816233 0.026125349
## [3,] 0.01973665 0.024940743 0.01987861 0.01257628 0.003441027
## [4,] 0.04829733 0.026733490 0.27713444 0.40825934 0.206299361
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.026758673 0.001114123 0.0033910140 0.021706210 0.011352203
## [2,] 0.002507781 0.001825916 0.0002664689 0.004588127 0.003201816
## [3,] 0.250032435 0.357801306 0.2521527244 0.398776794 0.628583757
## [4,] 0.077980018 0.044292532 0.0836049424 0.006276451 0.053296939
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.437252e-02 0.0003729344 2.465015e-02 2.769551e-03 0.009596881
## [2,] 5.493507e-05 0.0019270679 2.833358e-06 1.380046e-07 0.001446916
## [3,] 1.350874e-01 0.0213298370 1.008067e-01 4.289709e-03 0.009941865
## [4,] 4.165098e-02 0.2107803038 2.478574e-01 8.973042e-05 0.002083348
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.081872213 0.024850352 0.061347611 0.038815425 0.068747956
## [2,] 0.018784415 0.009303383 0.024950703 0.008162330 0.026125349
## [3,] 0.019736647 0.024940743 0.019878611 0.012576279 0.003441027
## [4,] 0.048297332 0.026733490 0.277134437 0.408259343 0.206299361
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0267586731 0.0011141232 0.0033910140 0.0217062098 0.0113522031
## [2,] 0.0025077810 0.0018259165 0.0002664689 0.0045881274 0.0032018163
## [3,] 0.2500324353 0.3578013055 0.2521527244 0.3987767944 0.6285837574
## [4,] 0.0779800177 0.0442925316 0.0836049424 0.0062764506 0.0532969385
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.437252e-02 3.729344e-04 2.465015e-02 2.769551e-03 9.596881e-03
## [2,] 5.493507e-05 1.927068e-03 2.833358e-06 1.380046e-07 1.446916e-03
## [3,] 1.350874e-01 2.132984e-02 1.008067e-01 4.289709e-03 9.941865e-03
## [4,] 4.165098e-02 2.107803e-01 2.478574e-01 8.973042e-05 2.083348e-03
einsum
By using einsum
and other DelayedTensor functions,
it is possible to implement your original tensor calculation functions.
It is intended to be applied to Delayed Arrays,
which can scale to large-scale data
since the calculation is performed internally by block processing.
For example, kronecker
can be easily implmented by eimsum
and other DelayedTensor functions4 https://stackoverflow.com/
questions/56067643/speeding-up-kronecker-products-numpy
(the kronecker
function inside DelayedTensor
has a more efficient implementation though).
darr1 <- DelayedArray(array(1:6, dim=c(2,3)))
darr2 <- DelayedArray(array(20:1, dim=c(4,5)))
mykronecker <- function(darr1, darr2){
stopifnot((length(dim(darr1)) == 2) && (length(dim(darr2)) == 2))
# Outer Product
tmpdarr <- DelayedTensor::einsum('ij,kl->ikjl', darr1, darr2)
# Reshape
DelayedTensor::unfold(tmpdarr, row_idx=c(2,1), col_idx=c(4,3))
}
identical(as.array(DelayedTensor::kronecker(darr1, darr2)),
as.array(mykronecker(darr1, darr2)))
## [1] TRUE
## R version 4.5.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.17.0
## [3] HDF5Array_1.37.0 h5mread_1.1.1
## [5] rhdf5_2.53.5 DelayedArray_0.35.3
## [7] SparseArray_1.9.1 S4Arrays_1.9.1
## [9] abind_1.4-8 IRanges_2.43.5
## [11] S4Vectors_0.47.4 MatrixGenerics_1.21.0
## [13] matrixStats_1.5.0 BiocGenerics_0.55.1
## [15] generics_0.1.4 Matrix_1.7-4
## [17] DelayedTensor_1.15.0 BiocStyle_2.37.1
##
## loaded via a namespace (and not attached):
## [1] dqrng_0.4.1 sass_0.4.10 lattice_0.22-7
## [4] digest_0.6.37 evaluate_1.0.5 grid_4.5.1
## [7] bookdown_0.45 fastmap_1.2.0 jsonlite_2.0.0
## [10] BiocManager_1.30.26 codetools_0.2-20 jquerylib_0.1.4
## [13] cli_3.6.5 rlang_1.1.6 crayon_1.5.3
## [16] XVector_0.49.1 cachem_1.1.0 yaml_2.3.10
## [19] tools_4.5.1 beachmat_2.25.5 parallel_4.5.1
## [22] BiocParallel_1.43.4 Rhdf5lib_1.31.0 rsvd_1.0.5
## [25] R6_2.6.1 lifecycle_1.0.4 BiocSingular_1.25.0
## [28] irlba_2.3.5.1 ScaledMatrix_1.17.0 rTensor_1.4.9
## [31] bslib_0.9.0 Rcpp_1.1.0 xfun_0.53
## [34] knitr_1.50 rhdf5filters_1.21.0 htmltools_0.5.8.1
## [37] rmarkdown_2.30 compiler_4.5.1