DelayedTensor 1.11.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2024-04-23 17:14:07.775682
Compiled: Wed May 1 18:07:39 2024
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.1939511 0.3040014 0.8496968
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.1939511 0.3040014 0.8496968
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.06724596 0.89981537 0.57724298 0.5121527
## [2,] 0.46081383 0.02530818 0.67137626 0.2145035
## [3,] 0.32718320 0.38851826 0.09942134 0.9596681
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.06724596 0.89981537 0.57724298 0.51215265
## [2,] 0.46081383 0.02530818 0.67137626 0.21450351
## [3,] 0.32718320 0.38851826 0.09942134 0.95966813
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9406780 0.7633561 0.1726829 0.3672317
## [2,] 0.4724877 0.5229375 0.9053476 0.4234348
## [3,] 0.5553747 0.3711458 0.8932192 0.9124440
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1957005 0.2322554 0.5906818 0.5693603
## [2,] 0.3498258 0.5942760 0.9094884 0.5470869
## [3,] 0.7847520 0.5980475 0.5832416 0.9101505
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4957947 0.5053586 0.5243941 0.1476627
## [2,] 0.8544610 0.1462342 0.2492705 0.8108682
## [3,] 0.5359447 0.2490246 0.8342446 0.5666664
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0553549 0.8360411 0.4388893 0.70703798
## [2,] 0.5791418 0.2872892 0.9312622 0.21209828
## [3,] 0.1616374 0.5459298 0.1024537 0.03264352
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2696134 0.3134760 0.9773786 0.1868592
## [2,] 0.1521051 0.4204165 0.2489786 0.2944066
## [3,] 0.2124038 0.4925636 0.2233157 0.9763229
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.9406780 0.7633561 0.1726829 0.3672317
## [2,] 0.4724877 0.5229375 0.9053476 0.4234348
## [3,] 0.5553747 0.3711458 0.8932192 0.9124440
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.1957005 0.2322554 0.5906818 0.5693603
## [2,] 0.3498258 0.5942760 0.9094884 0.5470869
## [3,] 0.7847520 0.5980475 0.5832416 0.9101505
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.4957947 0.5053586 0.5243941 0.1476627
## [2,] 0.8544610 0.1462342 0.2492705 0.8108682
## [3,] 0.5359447 0.2490246 0.8342446 0.5666664
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.05535490 0.83604108 0.43888933 0.70703798
## [2,] 0.57914177 0.28728915 0.93126221 0.21209828
## [3,] 0.16163737 0.54592983 0.10245366 0.03264352
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.2696134 0.3134760 0.9773786 0.1868592
## [2,] 0.1521051 0.4204165 0.2489786 0.2944066
## [3,] 0.2124038 0.4925636 0.2233157 0.9763229
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.8643482 0.6287139 0.2200951
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.8643482 0.6287139 0.2200951
einsum::einsum('iii->i', arrD)
## [1] 0.3983571 0.8364050 0.9105576
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.3983571 0.8364050 0.9105576
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.03761702 0.09241686 0.72198465
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.03761702 0.09241686 0.72198465
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.004522019 0.8096677045 0.333209454 0.26230034
## [2,] 0.212349385 0.0006405038 0.450746077 0.04601175
## [3,] 0.107048847 0.1509464391 0.009884603 0.92096293
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.0045220188 0.8096677045 0.3332094537 0.2623003390
## [2,] 0.2123493848 0.0006405038 0.4507460769 0.0460117544
## [3,] 0.1070488473 0.1509464391 0.0098846025 0.9209629254
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8848751 0.5827126 0.02981937 0.1348591
## [2,] 0.2232446 0.2734636 0.81965431 0.1792970
## [3,] 0.3084410 0.1377492 0.79784060 0.8325541
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0382987 0.05394258 0.3489050 0.3241711
## [2,] 0.1223781 0.35316399 0.8271691 0.2993041
## [3,] 0.6158358 0.35766085 0.3401707 0.8283740
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2458124 0.25538729 0.27498913 0.02180426
## [2,] 0.7301036 0.02138443 0.06213578 0.65750718
## [3,] 0.2872367 0.06201323 0.69596406 0.32111080
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.003064165 0.69896468 0.19262384 0.499902701
## [2,] 0.335405191 0.08253506 0.86724931 0.044985679
## [3,] 0.026126639 0.29803938 0.01049675 0.001065599
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07269137 0.09826718 0.95526887 0.03491635
## [2,] 0.02313597 0.17675000 0.06199035 0.08667525
## [3,] 0.04511537 0.24261890 0.04986989 0.95320643
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.88487510 0.58271256 0.02981937 0.13485913
## [2,] 0.22324465 0.27346362 0.81965431 0.17929701
## [3,] 0.30844103 0.13774918 0.79784060 0.83255408
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.03829870 0.05394258 0.34890498 0.32417114
## [2,] 0.12237809 0.35316399 0.82716908 0.29930411
## [3,] 0.61583577 0.35766085 0.34017074 0.82837397
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.24581237 0.25538729 0.27498913 0.02180426
## [2,] 0.73010363 0.02138443 0.06213578 0.65750718
## [3,] 0.28723675 0.06201323 0.69596406 0.32111080
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.003064165 0.698964683 0.192623843 0.499902701
## [2,] 0.335405191 0.082535057 0.867249309 0.044985679
## [3,] 0.026126639 0.298039377 0.010496753 0.001065599
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.07269137 0.09826718 0.95526887 0.03491635
## [2,] 0.02313597 0.17675000 0.06199035 0.08667525
## [3,] 0.04511537 0.24261890 0.04986989 0.95320643
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.03761702 0.05896140 0.1647996
## [2,] 0.05896140 0.09241686 0.2583090
## [3,] 0.16479961 0.25830902 0.7219846
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.03761702 0.05896140 0.16479961
## [2,] 0.05896140 0.09241686 0.25830902
## [3,] 0.16479961 0.25830902 0.72198465
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06325679 0.84643653 0.54299977 0.4817707
## [2,] 0.43347743 0.02380684 0.63154888 0.2017787
## [3,] 0.30777404 0.36547058 0.09352347 0.9027387
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03177289 0.4251517 0.27274022 0.2419858
## [2,] 0.21772888 0.0119578 0.31721704 0.1013503
## [3,] 0.15459005 0.1835701 0.04697536 0.4534314
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0373467 0.49973467 0.32058613 0.2844366
## [2,] 0.2559243 0.01405552 0.37286537 0.1191298
## [3,] 0.1817093 0.21577320 0.05521609 0.5329754
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05133261 0.68687957 0.44064195 0.3909549
## [2,] 0.35176505 0.01931915 0.51249917 0.1637426
## [3,] 0.24975730 0.29657779 0.07589389 0.7325685
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03516543 0.47054720 0.30186200 0.2678238
## [2,] 0.24097683 0.01323459 0.35108782 0.1121719
## [3,] 0.17109636 0.20317077 0.05199115 0.5018464
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02495805 0.333962670 0.21424129 0.19008329
## [2,] 0.17102910 0.009393023 0.24917846 0.07961207
## [3,] 0.12143266 0.144196909 0.03689981 0.35617677
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01161222 0.155382697 0.09967997 0.08843999
## [2,] 0.07957465 0.004370288 0.11593518 0.03704108
## [3,] 0.05649893 0.067090446 0.01716836 0.16571824
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06088097 0.8146457 0.52260556 0.4636762
## [2,] 0.41719670 0.0229127 0.60782890 0.1942002
## [3,] 0.29621453 0.3517441 0.09001087 0.8688333
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06006538 0.80373240 0.51560453 0.4574646
## [2,] 0.41160777 0.02260575 0.59968618 0.1915987
## [3,] 0.29224633 0.34703198 0.08880505 0.8571940
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02469485 0.330440737 0.21198192 0.18807869
## [2,] 0.16922545 0.009293965 0.24655065 0.07877249
## [3,] 0.12015205 0.142676225 0.03651067 0.35242057
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02847428 0.38101312 0.24442475 0.21686324
## [2,] 0.19512460 0.01071636 0.28428405 0.09082824
## [3,] 0.13854074 0.16451214 0.04209845 0.40635686
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06135817 0.82103115 0.52670190 0.4673106
## [2,] 0.42046682 0.02309229 0.61259325 0.1957224
## [3,] 0.29853635 0.35450116 0.09071641 0.8756434
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01316007 0.176094359 0.11296677 0.10022855
## [2,] 0.09018152 0.004952824 0.13138870 0.04197845
## [3,] 0.06402993 0.076033235 0.01945681 0.18780758
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02352437 0.314778628 0.20193448 0.17916421
## [2,] 0.16120456 0.008853453 0.23486473 0.07503886
## [3,] 0.11445712 0.135913710 0.03478015 0.33571667
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0527714 0.70613196 0.4529926 0.4019128
## [2,] 0.3616246 0.01986064 0.5268639 0.1683321
## [3,] 0.2567577 0.30489050 0.0780211 0.7531015
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01561824 0.208987005 0.13406781 0.1189502
## [2,] 0.10702651 0.005877961 0.15593078 0.0498196
## [3,] 0.07599007 0.090235475 0.02309115 0.2228881
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03996266 0.53473870 0.34304166 0.3043600
## [2,] 0.27385061 0.01504004 0.39898281 0.1274743
## [3,] 0.19443713 0.23088709 0.05908372 0.5703078
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04021628 0.53813236 0.34521874 0.3062916
## [2,] 0.27558857 0.01513549 0.40151491 0.1282833
## [3,] 0.19567111 0.23235239 0.05945869 0.5739272
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03972096 0.53150456 0.34096692 0.3025192
## [2,] 0.27219434 0.01494908 0.39656973 0.1267033
## [3,] 0.19326116 0.22949066 0.05872637 0.5668585
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06115942 0.81837161 0.52499577 0.4657969
## [2,] 0.41910481 0.02301749 0.61060889 0.1950884
## [3,] 0.29756931 0.35335284 0.09042255 0.8728070
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03922064 0.52480974 0.33667210 0.2987087
## [2,] 0.26876578 0.01476078 0.39157455 0.1251074
## [3,] 0.19082685 0.22660000 0.05798666 0.5597184
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03828718 0.51231914 0.32865923 0.2915994
## [2,] 0.26236909 0.01440947 0.38225498 0.1221298
## [3,] 0.18628512 0.22120687 0.05660656 0.5463969
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03678938 0.49227723 0.31580209 0.2801920
## [2,] 0.25210522 0.01384577 0.36730118 0.1173521
## [3,] 0.17899765 0.21255326 0.05439212 0.5250219
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06120394 0.81896743 0.52537800 0.4661360
## [2,] 0.41940995 0.02303425 0.61105345 0.1952305
## [3,] 0.29778596 0.35361010 0.09048838 0.8734425
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03334019 0.44612368 0.28619400 0.2539226
## [2,] 0.22846905 0.01254766 0.33286478 0.1063497
## [3,] 0.16221569 0.19262529 0.04929257 0.4757984
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05745905 0.76885716 0.49323162 0.4376145
## [2,] 0.39374745 0.02162485 0.57366484 0.1832849
## [3,] 0.27956529 0.33197371 0.08495166 0.8199990
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03604012 0.48225130 0.30937033 0.2744855
## [2,] 0.24697074 0.01356378 0.35982056 0.1149620
## [3,] 0.17535211 0.20822431 0.05328434 0.5143291
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03398332 0.4547294 0.29171469 0.2588207
## [2,] 0.23287622 0.0127897 0.33928575 0.1084012
## [3,] 0.16534484 0.1963410 0.05024343 0.4849765
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.009833656 0.13158374 0.08441264 0.07489421
## [2,] 0.067386720 0.00370092 0.09817814 0.03136774
## [3,] 0.047845358 0.05681464 0.01453880 0.14033626
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01674590 0.224076133 0.14374768 0.12753859
## [2,] 0.11475396 0.006302358 0.16718918 0.05341664
## [3,] 0.08147665 0.096750591 0.02475836 0.23898094
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03526338 0.47185783 0.30270279 0.2685698
## [2,] 0.24164803 0.01327146 0.35206572 0.1124844
## [3,] 0.17157293 0.20373667 0.05213596 0.5032443
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01676243 0.224297420 0.14388964 0.12766454
## [2,] 0.11486729 0.006308582 0.16735429 0.05346939
## [3,] 0.08155712 0.096846138 0.02478281 0.23921695
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05609958 0.75066612 0.48156184 0.4272606
## [2,] 0.38443145 0.02111321 0.56009202 0.1789484
## [3,] 0.27295082 0.32411926 0.08294172 0.8005980
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.009929717 0.132869138 0.08523724 0.07562583
## [2,] 0.068044999 0.003737073 0.09913721 0.03167416
## [3,] 0.048312744 0.057369643 0.01468082 0.14170716
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05452761 0.72963164 0.4680680 0.4152883
## [2,] 0.37365926 0.02052159 0.5443976 0.1739341
## [3,] 0.26530244 0.31503709 0.0806176 0.7781643
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03810602 0.50989513 0.32710419 0.2902197
## [2,] 0.26112771 0.01434129 0.38044636 0.1215519
## [3,] 0.18540372 0.22016024 0.05633873 0.5438117
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.003722393 0.049809187 0.031953225 0.02835016
## [2,] 0.025508302 0.001400931 0.037163963 0.01187382
## [3,] 0.018111192 0.021506388 0.005503458 0.05312233
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03894494 0.52112067 0.33430552 0.2966090
## [2,] 0.26687654 0.01465702 0.38882203 0.1242279
## [3,] 0.18948546 0.22500715 0.05757905 0.5557839
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01086946 0.145443789 0.09330404 0.08278301
## [2,] 0.07448473 0.004090747 0.10851949 0.03467178
## [3,] 0.05288503 0.062799069 0.01607020 0.15511823
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05622038 0.75228261 0.48259884 0.4281807
## [2,] 0.38525929 0.02115867 0.56129813 0.1793337
## [3,] 0.27353860 0.32481723 0.08312032 0.8023220
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01931903 0.258507196 0.16583565 0.14713590
## [2,] 0.13238681 0.007270764 0.19287912 0.06162453
## [3,] 0.09399618 0.111617082 0.02856267 0.27570224
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03671157 0.49123605 0.31513416 0.2795994
## [2,] 0.25157201 0.01381649 0.36652432 0.1171039
## [3,] 0.17861907 0.21210371 0.05427707 0.5239115
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02951353 0.39491936 0.25334578 0.2247783
## [2,] 0.20224627 0.01110749 0.29465987 0.0941433
## [3,] 0.14359722 0.17051652 0.04363496 0.4211881
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06262362 0.83796405 0.53756457 0.4769484
## [2,] 0.42913851 0.02356855 0.62522734 0.1997590
## [3,] 0.30469335 0.36181238 0.09258734 0.8937027
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.006889595 0.092189380 0.05914066 0.05247191
## [2,] 0.047212064 0.002592915 0.06878496 0.02197667
## [3,] 0.033521117 0.039805119 0.01018608 0.09832151
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04754545 0.63620364 0.40813271 0.3621114
## [2,] 0.32581288 0.01789384 0.47468851 0.1516621
## [3,] 0.23133095 0.27469717 0.07029466 0.6785218
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01426275 0.190849289 0.12243224 0.10862669
## [2,] 0.09773782 0.005367821 0.14239775 0.04549582
## [3,] 0.06939499 0.082404053 0.02108709 0.20354396
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.002195145 0.0293731390 0.018843241 0.016718464
## [2,] 0.015042584 0.0008261479 0.021916083 0.007002149
## [3,] 0.010680411 0.0126826027 0.003245462 0.031326944
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01813041 0.242602252 0.15563242 0.13808320
## [2,] 0.12424157 0.006823423 0.18101201 0.05783301
## [3,] 0.08821296 0.104749716 0.02680532 0.25873936
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01022846 0.136866543 0.08780162 0.07790105
## [2,] 0.07009215 0.003849504 0.10211978 0.03262709
## [3,] 0.04976625 0.059095624 0.01512250 0.14597046
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01428330 0.191124202 0.12260860 0.10878317
## [2,] 0.09787861 0.005375553 0.14260287 0.04556136
## [3,] 0.06949495 0.082522754 0.02111747 0.20383716
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02107999 0.282070486 0.1809518 0.16054754
## [2,] 0.14445406 0.007933505 0.2104603 0.06724169
## [3,] 0.10256407 0.121791134 0.0311662 0.30083289
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02827131 0.37829719 0.24268245 0.2153174
## [2,] 0.19373372 0.01063997 0.28225763 0.0901808
## [3,] 0.13755320 0.16333947 0.04179837 0.4034603
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03312291 0.44321630 0.28432888 0.2522678
## [2,] 0.22698012 0.01246589 0.33069550 0.1056566
## [3,] 0.16115853 0.19136995 0.04897133 0.4726976
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06572476 0.87946026 0.56418491 0.5005670
## [2,] 0.45038956 0.02473567 0.65618876 0.2096511
## [3,] 0.31978185 0.37972942 0.09717229 0.9379591
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01674281 0.224034789 0.14372116 0.12751506
## [2,] 0.11473279 0.006301195 0.16715833 0.05340679
## [3,] 0.08146162 0.096732740 0.02475379 0.23893685
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01501708 0.200942871 0.12890740 0.11437171
## [2,] 0.10290695 0.005651712 0.14992884 0.04790199
## [3,] 0.07306514 0.086762215 0.02220234 0.21430893
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01256552 0.168138766 0.10786315 0.09570043
## [2,] 0.08610730 0.004729065 0.12545282 0.04008195
## [3,] 0.06113719 0.072598205 0.01857779 0.17932280
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01979765 0.264911584 0.1699441 0.15078112
## [2,] 0.13566663 0.007450894 0.1976576 0.06315125
## [3,] 0.09632489 0.114382340 0.0292703 0.28253263
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06565377 0.87851037 0.56357554 0.5000264
## [2,] 0.44990310 0.02470895 0.65548002 0.2094247
## [3,] 0.31943646 0.37931928 0.09706733 0.9369460
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.06325679 0.84643653 0.54299977 0.48177073
## [2,] 0.43347743 0.02380684 0.63154888 0.20177873
## [3,] 0.30777404 0.36547058 0.09352347 0.90273870
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.03177289 0.42515171 0.27274022 0.24198584
## [2,] 0.21772888 0.01195780 0.31721704 0.10135027
## [3,] 0.15459005 0.18357011 0.04697536 0.45343141
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.03734670 0.49973467 0.32058613 0.28443661
## [2,] 0.25592433 0.01405552 0.37286537 0.11912981
## [3,] 0.18170926 0.21577320 0.05521609 0.53297537
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.012565525 0.168138766 0.107863151 0.095700426
## [2,] 0.086107296 0.004729065 0.125452819 0.040081950
## [3,] 0.061137186 0.072598205 0.018577790 0.179322804
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.019797654 0.264911584 0.169944142 0.150781121
## [2,] 0.135666632 0.007450894 0.197657601 0.063151248
## [3,] 0.096324894 0.114382340 0.029270298 0.282532632
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.06565377 0.87851037 0.56357554 0.50002637
## [2,] 0.44990310 0.02470895 0.65548002 0.20942469
## [3,] 0.31943646 0.37931928 0.09706733 0.93694599
If there is a vanishing subscript on the left or right side of ->, the summation is done for that subscript.
einsum::einsum('i->', arrA)
## [1] 1.347649
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.347649
einsum::einsum('ij->', arrC)
## [1] 5.20325
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 5.20325
einsum::einsum('ijk->', arrE)
## [1] 29.74275
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 29.74275
einsum::einsum('ij->i', arrC)
## [1] 2.056457 1.372002 1.774791
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 2.056457 1.372002 1.774791
einsum::einsum('ij->j', arrC)
## [1] 0.855243 1.313642 1.348041 1.686324
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 0.855243 1.313642 1.348041 1.686324
einsum::einsum('ijk->i', arrE)
## [1] 9.289807 9.911417 10.541526
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 9.289807 9.911417 10.541526
einsum::einsum('ijk->j', arrE)
## [1] 6.615276 6.878352 8.584849 7.664274
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 6.615276 6.878352 8.584849 7.664274
einsum::einsum('ijk->k', arrE)
## [1] 7.300340 6.864867 5.919924 4.889779 4.767840
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 7.300340 6.864867 5.919924 4.889779 4.767840
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 1.957141 2.650487 2.704027 1.978152
## [2,] 2.408021 1.971153 3.244347 2.287895
## [3,] 2.250113 2.256711 2.636475 3.398227
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 1.957141 2.650487 2.704027 1.978152
## [2,] 2.408021 1.971153 3.244347 2.287895
## [3,] 2.250113 2.256711 2.636475 3.398227
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.968540 1.330278 1.8862004 0.7961340 0.6341223
## [2,] 1.657439 1.424579 0.9006173 1.6692601 1.2264560
## [3,] 1.971250 2.083412 1.6079092 1.4726052 1.4496729
## [4,] 1.703110 2.026598 1.5251972 0.9517798 1.4575887
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.9685404 1.3302784 1.8862004 0.7961340 0.6341223
## [2,] 1.6574394 1.4245790 0.9006173 1.6692601 1.2264560
## [3,] 1.9712497 2.0834117 1.6079092 1.4726052 1.4496729
## [4,] 1.7031105 2.0265977 1.5251972 0.9517798 1.4575887
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.968540 1.330278 1.8862004 0.7961340 0.6341223
## [2,] 1.657439 1.424579 0.9006173 1.6692601 1.2264560
## [3,] 1.971250 2.083412 1.6079092 1.4726052 1.4496729
## [4,] 1.703110 2.026598 1.5251972 0.9517798 1.4575887
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.9685404 1.3302784 1.8862004 0.7961340 0.6341223
## [2,] 1.6574394 1.4245790 0.9006173 1.6692601 1.2264560
## [3,] 1.9712497 2.0834117 1.6079092 1.4726052 1.4496729
## [4,] 1.7031105 2.0265977 1.5251972 0.9517798 1.4575887
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.713157
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 1.713157
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.8643482 0.00512664 0.4846905
## [2,] 0.2439047 0.62871393 0.6885880
## [3,] 0.1513472 0.66506862 0.2200951
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.86434823 0.00512664 0.48469050
## [2,] 0.24390475 0.62871393 0.68858798
## [3,] 0.15134725 0.66506862 0.22009506
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.39835709 0.12866156 0.2114869
## [2,] 0.08424127 0.08837573 0.9859434
## [3,] 0.56717470 0.81326486 0.3783720
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.6600317 0.2972494 0.3507190
## [2,] 0.6663619 0.8364050 0.5599263
## [3,] 0.4697912 0.6683456 0.3620486
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.1072177 0.9495946 0.1671999
## [2,] 0.5563240 0.8594067 0.6179603
## [3,] 0.4270002 0.8409066 0.9105576
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.39835709 0.12866156 0.21148688
## [2,] 0.08424127 0.08837573 0.98594342
## [3,] 0.56717470 0.81326486 0.37837205
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.6600317 0.2972494 0.3507190
## [2,] 0.6663619 0.8364050 0.5599263
## [3,] 0.4697912 0.6683456 0.3620486
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.1072177 0.9495946 0.1671999
## [2,] 0.5563240 0.8594067 0.6179603
## [3,] 0.4270002 0.8409066 0.9105576
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.8520185
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 0.8520185
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 3.30829
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 3.30829
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 19.2103
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 19.2103
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.4165608 0.7765126 1.263153 0.364596 0.1409427
## [2,] 0.9939254 0.7647674 0.338785 1.079539 0.5176361
## [3,] 1.6473143 1.5162448 1.033089 1.070370 1.0671291
## [4,] 1.1467102 1.4518492 1.000422 0.545954 1.0747980
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.4165608 0.7765126 1.2631527 0.3645960 0.1409427
## [2,] 0.9939254 0.7647674 0.3387850 1.0795391 0.5176361
## [3,] 1.6473143 1.5162448 1.0330890 1.0703699 1.0671291
## [4,] 1.1467102 1.4518492 1.0004222 0.5459540 1.0747980
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.4096995 0.5511663 0.9204833
## [2,] 0.5511663 0.7097477 0.4332045
## [3,] 0.9204833 0.4332045 1.1888428
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.4096995 0.5511663 0.9204833
## [2,] 0.5511663 0.7097477 0.4332045
## [3,] 0.9204833 0.4332045 1.1888428
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.004522019 0.2123493848 0.107048847
## [2,] 0.809667704 0.0006405038 0.150946439
## [3,] 0.333209454 0.4507460769 0.009884603
## [4,] 0.262300339 0.0460117544 0.920962925
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.0045220188 0.2123493848 0.1070488473
## [2,] 0.8096677045 0.0006405038 0.1509464391
## [3,] 0.3332094537 0.4507460769 0.0098846025
## [4,] 0.2623003390 0.0460117544 0.9209629254
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.88487510 0.03829870 0.24581237 0.003064165 0.07269137
## [2,] 0.58271256 0.05394258 0.25538729 0.698964683 0.09826718
## [3,] 0.02981937 0.34890498 0.27498913 0.192623843 0.95526887
## [4,] 0.13485913 0.32417114 0.02180426 0.499902701 0.03491635
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.2232446 0.1223781 0.73010363 0.33540519 0.02313597
## [2,] 0.2734636 0.3531640 0.02138443 0.08253506 0.17675000
## [3,] 0.8196543 0.8271691 0.06213578 0.86724931 0.06199035
## [4,] 0.1792970 0.2993041 0.65750718 0.04498568 0.08667525
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.3084410 0.6158358 0.28723675 0.026126639 0.04511537
## [2,] 0.1377492 0.3576609 0.06201323 0.298039377 0.24261890
## [3,] 0.7978406 0.3401707 0.69596406 0.010496753 0.04986989
## [4,] 0.8325541 0.8283740 0.32111080 0.001065599 0.95320643
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.884875105 0.038298703 0.245812367 0.003064165 0.072691367
## [2,] 0.582712556 0.053942584 0.255387294 0.698964683 0.098267176
## [3,] 0.029819372 0.348904979 0.274989125 0.192623843 0.955268867
## [4,] 0.134859128 0.324171136 0.021804263 0.499902701 0.034916354
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.22324465 0.12237809 0.73010363 0.33540519 0.02313597
## [2,] 0.27346362 0.35316399 0.02138443 0.08253506 0.17675000
## [3,] 0.81965431 0.82716908 0.06213578 0.86724931 0.06199035
## [4,] 0.17929701 0.29930411 0.65750718 0.04498568 0.08667525
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.308441026 0.615835775 0.287236748 0.026126639 0.045115373
## [2,] 0.137749183 0.357660852 0.062013234 0.298039377 0.242618896
## [3,] 0.797840597 0.340170737 0.695964060 0.010496753 0.049869887
## [4,] 0.832554084 0.828373974 0.321110798 0.001065599 0.953206432
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 2.243949 2.324208 2.7321837
## [2,] 1.587998 2.400677 2.8761917
## [3,] 1.673210 2.060834 2.1858803
## [4,] 2.037323 2.009791 0.8426644
## [5,] 1.747327 1.115907 1.9046060
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 2.2439487 2.3242076 2.7321837
## [2,] 1.5879981 2.4006771 2.8761917
## [3,] 1.6732100 2.0608338 2.1858803
## [4,] 2.0373233 2.0097914 0.8426644
## [5,] 1.7473271 1.1159068 1.9046060
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.0007760801 3.358989e-05 0.0002155898 2.687427e-06 6.375399e-05
## [2,] 0.0915068028 8.470923e-03 0.0401049788 1.097626e-01 1.543148e-02
## [3,] 0.0019271166 2.254845e-02 0.0177715383 1.244857e-02 6.173552e-02
## [4,] 0.0068607467 1.649170e-02 0.0011092577 2.543177e-02 1.776315e-03
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.441145e-02 7.900058e-03 4.713148e-02 2.165192e-02 1.493531e-03
## [2,] 5.324721e-05 6.876599e-05 4.163849e-06 1.607074e-05 3.441571e-05
## [3,] 1.123151e-01 1.133449e-01 8.514307e-03 1.188370e-01 8.494379e-03
## [4,] 2.507942e-03 4.186558e-03 9.196972e-03 6.292434e-04 1.212382e-03
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.028055507 0.056015845 0.026126785 2.376455e-03 0.004103652
## [2,] 0.017667532 0.045873118 0.007953737 3.822614e-02 0.031117986
## [3,] 0.006700995 0.002857065 0.005845343 8.816134e-05 0.000418853
## [4,] 0.651506247 0.648235148 0.251281803 8.338732e-04 0.745921446
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,] 7.760801e-04 3.358989e-05 2.155898e-04 2.687427e-06 6.375399e-05
## [2,] 9.150680e-02 8.470923e-03 4.010498e-02 1.097626e-01 1.543148e-02
## [3,] 1.927117e-03 2.254845e-02 1.777154e-02 1.244857e-02 6.173552e-02
## [4,] 6.860747e-03 1.649170e-02 1.109258e-03 2.543177e-02 1.776315e-03
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.441145e-02 7.900058e-03 4.713148e-02 2.165192e-02 1.493531e-03
## [2,] 5.324721e-05 6.876599e-05 4.163849e-06 1.607074e-05 3.441571e-05
## [3,] 1.123151e-01 1.133449e-01 8.514307e-03 1.188370e-01 8.494379e-03
## [4,] 2.507942e-03 4.186558e-03 9.196972e-03 6.292434e-04 1.212382e-03
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.805551e-02 5.601584e-02 2.612679e-02 2.376455e-03 4.103652e-03
## [2,] 1.766753e-02 4.587312e-02 7.953737e-03 3.822614e-02 3.111799e-02
## [3,] 6.700995e-03 2.857065e-03 5.845343e-03 8.816134e-05 4.188530e-04
## [4,] 6.515062e-01 6.482351e-01 2.512818e-01 8.338732e-04 7.459214e-01
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.4.0 RC (2024-04-16 r86468 ucrt)
## Platform: x86_64-w64-mingw32/x64
## Running under: Windows Server 2022 x64 (build 20348)
##
## Matrix products: default
##
##
## locale:
## [1] LC_COLLATE=C
## [2] LC_CTYPE=English_United States.utf8
## [3] LC_MONETARY=English_United States.utf8
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.utf8
##
## 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.13.0
## [3] HDF5Array_1.33.0 rhdf5_2.49.0
## [5] DelayedArray_0.31.0 SparseArray_1.5.0
## [7] S4Arrays_1.5.0 abind_1.4-5
## [9] IRanges_2.39.0 S4Vectors_0.43.0
## [11] MatrixGenerics_1.17.0 matrixStats_1.3.0
## [13] BiocGenerics_0.51.0 Matrix_1.7-0
## [15] DelayedTensor_1.11.0 BiocStyle_2.33.0
##
## loaded via a namespace (and not attached):
## [1] jsonlite_1.8.8 compiler_4.4.0 BiocManager_1.30.22
## [4] crayon_1.5.2 rsvd_1.0.5 Rcpp_1.0.12
## [7] rhdf5filters_1.17.0 parallel_4.4.0 jquerylib_0.1.4
## [10] BiocParallel_1.39.0 yaml_2.3.8 fastmap_1.1.1
## [13] lattice_0.22-6 R6_2.5.1 XVector_0.45.0
## [16] ScaledMatrix_1.13.0 knitr_1.46 bookdown_0.39
## [19] bslib_0.7.0 rlang_1.1.3 cachem_1.0.8
## [22] xfun_0.43 sass_0.4.9 cli_3.6.2
## [25] Rhdf5lib_1.27.0 BiocSingular_1.21.0 zlibbioc_1.51.0
## [28] digest_0.6.35 grid_4.4.0 irlba_2.3.5.1
## [31] rTensor_1.4.8 dqrng_0.3.2 lifecycle_1.0.4
## [34] evaluate_0.23 codetools_0.2-20 beachmat_2.21.0
## [37] rmarkdown_2.26 tools_4.4.0 htmltools_0.5.8.1