DelayedTensor 1.0.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2021-10-26 15:33:21
Compiled: Tue Oct 26 19:43:16 2021
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.5125436 0.5933638 0.5648008
DelayedTensor::einsum('i->i', darrA)
## <3> array of class DelayedArray and type "double":
## [1] [2] [3]
## 0.5125436 0.5933638 0.5648008
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.4522747 0.3403338 0.4672112 0.8154498
## [2,] 0.1229627 0.9709443 0.6529912 0.7255813
## [3,] 0.9147947 0.3187873 0.4033807 0.3808385
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.4522747 0.3403338 0.4672112 0.8154498
## [2,] 0.1229627 0.9709443 0.6529912 0.7255813
## [3,] 0.9147947 0.3187873 0.4033807 0.3808385
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4670900 0.5202673 0.6661886 0.37986317
## [2,] 0.4731134 0.8665448 0.5130598 0.46589098
## [3,] 0.9364552 0.9029409 0.6672771 0.03173292
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.33241509 0.03268191 0.3223502 0.4564067
## [2,] 0.02687331 0.88752531 0.4307244 0.1955236
## [3,] 0.26101826 0.10314046 0.9741988 0.3325117
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3205119 0.4377059 0.3509500 0.17861219
## [2,] 0.1334574 0.0555223 0.9180877 0.09612971
## [3,] 0.6085948 0.4783748 0.5551433 0.74066050
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9470280 0.3124668 0.3115245 0.7898103
## [2,] 0.3282719 0.2007891 0.3755132 0.8167340
## [3,] 0.5739498 0.3511186 0.2820179 0.6522912
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1675411 0.3170912 0.6616995 0.7645545
## [2,] 0.7840064 0.7226160 0.8461450 0.7507792
## [3,] 0.5406453 0.6541750 0.3839708 0.2932150
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.46708999 0.52026725 0.66618856 0.37986317
## [2,] 0.47311341 0.86654481 0.51305984 0.46589098
## [3,] 0.93645521 0.90294087 0.66727710 0.03173292
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.33241509 0.03268191 0.32235023 0.45640670
## [2,] 0.02687331 0.88752531 0.43072440 0.19552363
## [3,] 0.26101826 0.10314046 0.97419877 0.33251172
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.32051185 0.43770595 0.35094995 0.17861219
## [2,] 0.13345736 0.05552230 0.91808774 0.09612971
## [3,] 0.60859485 0.47837477 0.55514331 0.74066050
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.9470280 0.3124668 0.3115245 0.7898103
## [2,] 0.3282719 0.2007891 0.3755132 0.8167340
## [3,] 0.5739498 0.3511186 0.2820179 0.6522912
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.1675411 0.3170912 0.6616995 0.7645545
## [2,] 0.7840064 0.7226160 0.8461450 0.7507792
## [3,] 0.5406453 0.6541750 0.3839708 0.2932150
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.4922416 0.7546977 0.1986403
DelayedTensor::einsum('ii->i', darrB)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.4922416 0.7546977 0.1986403
einsum::einsum('iii->i', arrD)
## [1] 0.7963169 0.8330769 0.4389410
DelayedTensor::einsum('iii->i', darrD)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.7963169 0.8330769 0.4389410
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.2627009 0.3520806 0.3190000
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.2627009 0.3520806 0.3190000
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.20455243 0.1158271 0.2182863 0.6649583
## [2,] 0.01511983 0.9427328 0.4263976 0.5264682
## [3,] 0.83684936 0.1016253 0.1627160 0.1450380
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.20455243 0.11582711 0.21828634 0.66495833
## [2,] 0.01511983 0.94273283 0.42639756 0.52646815
## [3,] 0.83684936 0.10162533 0.16271599 0.14503799
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2181731 0.2706780 0.4438072 0.144296028
## [2,] 0.2238363 0.7508999 0.2632304 0.217054403
## [3,] 0.8769484 0.8153022 0.4452587 0.001006978
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1104997937 0.001068107 0.1039097 0.20830708
## [2,] 0.0007221746 0.787701175 0.1855235 0.03822949
## [3,] 0.0681305301 0.010637953 0.9490632 0.11056404
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10272785 0.191586495 0.1231659 0.031902314
## [2,] 0.01781087 0.003082726 0.8428851 0.009240922
## [3,] 0.37038769 0.228842416 0.3081841 0.548577980
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8968620 0.09763551 0.09704749 0.6238003
## [2,] 0.1077624 0.04031626 0.14101018 0.6670545
## [3,] 0.3294184 0.12328429 0.07953412 0.4254838
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02807001 0.1005468 0.4378462 0.58454361
## [2,] 0.61466606 0.5221739 0.7159614 0.56366946
## [3,] 0.29229738 0.4279450 0.1474336 0.08597504
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> array of class HDF5Array and type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.218173059 0.270678012 0.443807193 0.144296028
## [2,] 0.223836301 0.750899915 0.263230398 0.217054403
## [3,] 0.876948368 0.815302222 0.445258728 0.001006978
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.1104997937 0.0010681075 0.1039096740 0.2083070764
## [2,] 0.0007221746 0.7877011751 0.1855235102 0.0382294906
## [3,] 0.0681305301 0.0106379535 0.9490632426 0.1105640409
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.102727849 0.191586495 0.123165869 0.031902314
## [2,] 0.017810867 0.003082726 0.842885094 0.009240922
## [3,] 0.370387687 0.228842416 0.308184097 0.548577980
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.89686197 0.09763551 0.09704749 0.62380026
## [2,] 0.10776243 0.04031626 0.14101018 0.66705449
## [3,] 0.32941840 0.12328429 0.07953412 0.42548379
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.02807001 0.10054681 0.43784617 0.58454361
## [2,] 0.61466606 0.52217389 0.71596139 0.56366946
## [3,] 0.29229738 0.42794497 0.14743358 0.08597504
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.2627009 0.3041248 0.2894850
## [2,] 0.3041248 0.3520806 0.3351324
## [3,] 0.2894850 0.3351324 0.3190000
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.2627009 0.3041248 0.2894850
## [2,] 0.3041248 0.3520806 0.3351324
## [3,] 0.2894850 0.3351324 0.3190000
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.21125300 0.1589665 0.2182297 0.3808884
## [2,] 0.05743466 0.4535184 0.3050057 0.3389117
## [3,] 0.42729145 0.1489024 0.1884151 0.1778859
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.21397724 0.1610165 0.2210439 0.3858002
## [2,] 0.05817531 0.4593668 0.3089389 0.3432822
## [3,] 0.43280165 0.1508225 0.1908448 0.1801798
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4235350 0.3187074 0.4375224 0.7636322
## [2,] 0.1151491 0.9092459 0.6114971 0.6794743
## [3,] 0.8566643 0.2985300 0.3777480 0.3566382
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.23530373 0.1770645 0.2430747 0.4242518
## [2,] 0.06397348 0.5051505 0.3397300 0.3774962
## [3,] 0.47593773 0.1658546 0.2098658 0.1981378
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3919163 0.2949145 0.4048595 0.7066238
## [2,] 0.1065527 0.8413667 0.5658462 0.6287487
## [3,] 0.7927106 0.2762435 0.3495475 0.3300137
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4083773 0.3073013 0.4218641 0.7363029
## [2,] 0.1110281 0.8767053 0.5896125 0.6551570
## [3,] 0.8260055 0.2878461 0.3642289 0.3438747
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.30130025 0.2267265 0.3112508 0.5432433
## [2,] 0.08191636 0.6468320 0.4350153 0.4833739
## [3,] 0.60942577 0.2123724 0.2687276 0.2537103
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.23204400 0.1746116 0.2397073 0.4183745
## [2,] 0.06308723 0.4981525 0.3350236 0.3722666
## [3,] 0.46934443 0.1635570 0.2069584 0.1953930
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.30179257 0.2270970 0.3117594 0.5441310
## [2,] 0.08205021 0.6478889 0.4357261 0.4841638
## [3,] 0.61042156 0.2127195 0.2691667 0.2541248
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.17180251 0.1292803 0.1774763 0.3097593
## [2,] 0.04670901 0.3688260 0.2480473 0.2756216
## [3,] 0.34749682 0.1210955 0.1532295 0.1446665
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.21071072 0.1585585 0.2176695 0.3799107
## [2,] 0.05728722 0.4523542 0.3042227 0.3380418
## [3,] 0.42619460 0.1485201 0.1879314 0.1774292
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.014351998 0.01079979 0.01482598 0.02587660
## [2,] 0.003901966 0.03081090 0.02072132 0.02302481
## [3,] 0.029029108 0.01011605 0.01280045 0.01208512
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.15034295 0.1131321 0.1553081 0.2710678
## [2,] 0.04087466 0.3227565 0.2170641 0.2411942
## [3,] 0.30409157 0.1059697 0.1340898 0.1265965
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.012154117 0.009145895 0.01255551 0.02191383
## [2,] 0.003304415 0.026092484 0.01754803 0.01949877
## [3,] 0.024583558 0.008566868 0.01084017 0.01023439
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11805196 0.08883334 0.1219507 0.21284728
## [2,] 0.03209551 0.25343419 0.1704426 0.18938995
## [3,] 0.23877812 0.08320930 0.1052897 0.09940581
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.014781204 0.01112276 0.01526936 0.02665046
## [2,] 0.004018657 0.03173232 0.02134100 0.02371338
## [3,] 0.029897241 0.01041858 0.01318325 0.01244653
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4014053 0.3020549 0.4146618 0.7237323
## [2,] 0.1091325 0.8617376 0.5795463 0.6439717
## [3,] 0.8119035 0.2829318 0.3580106 0.3380038
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04664782 0.03510218 0.04818838 0.08410586
## [2,] 0.01268243 0.10014364 0.06734981 0.07483678
## [3,] 0.09435234 0.03287987 0.04160487 0.03927986
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.14579087 0.1097067 0.1506057 0.2628604
## [2,] 0.03963706 0.3129841 0.2104919 0.2338913
## [3,] 0.29488429 0.1027612 0.1300299 0.1227634
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.19480576 0.1465901 0.2012393 0.3512341
## [2,] 0.05296304 0.4182094 0.2812593 0.3125256
## [3,] 0.39402440 0.1373095 0.1737459 0.1640365
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4406055 0.3315528 0.4551566 0.7944102
## [2,] 0.1197901 0.9458927 0.6361433 0.7068604
## [3,] 0.8911919 0.3105622 0.3929730 0.3710124
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.20642122 0.1553306 0.2132383 0.3721767
## [2,] 0.05612101 0.4431455 0.2980296 0.3311601
## [3,] 0.41751843 0.1454967 0.1841057 0.1738173
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08843040 0.06654330 0.09135084 0.15943970
## [2,] 0.02404212 0.18984256 0.12767522 0.14186828
## [3,] 0.17886398 0.06233045 0.07887046 0.07446293
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.15038665 0.1131650 0.1553532 0.2711466
## [2,] 0.04088655 0.3228504 0.2171272 0.2412643
## [3,] 0.30417996 0.1060005 0.1341288 0.1266333
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.14495941 0.1090810 0.1497467 0.2613613
## [2,] 0.03941101 0.3111992 0.2092914 0.2325574
## [3,] 0.29320255 0.1021751 0.1292883 0.1220633
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06035939 0.04542005 0.06235278 0.10882777
## [2,] 0.01641028 0.12957966 0.08714649 0.09683416
## [3,] 0.12208609 0.04254451 0.05383412 0.05082571
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.27525207 0.2071254 0.2843424 0.4962785
## [2,] 0.07483448 0.5909117 0.3974071 0.4415850
## [3,] 0.55673934 0.1940123 0.2454954 0.2317764
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.19796334 0.1489661 0.2045011 0.3569272
## [2,] 0.05382151 0.4249881 0.2858181 0.3175912
## [3,] 0.40041108 0.1395351 0.1765621 0.1666953
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.025111334 0.01889612 0.02594064 0.04527565
## [2,] 0.006827173 0.05390906 0.03625558 0.04028594
## [3,] 0.050791509 0.01769980 0.02239663 0.02114503
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.21635682 0.1628071 0.2235021 0.3900906
## [2,] 0.05882226 0.4644753 0.3123745 0.3470998
## [3,] 0.43761470 0.1524998 0.1929671 0.1821835
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.15872579 0.1194401 0.1639678 0.2861821
## [2,] 0.04315376 0.3407529 0.2291672 0.2546427
## [3,] 0.32104716 0.1118784 0.1415664 0.1336553
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4152279 0.3124563 0.4289409 0.7486544
## [2,] 0.1128906 0.8914121 0.5995033 0.6661473
## [3,] 0.8398618 0.2926747 0.3703389 0.3496432
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.25107729 0.1889340 0.2593692 0.4526915
## [2,] 0.06826193 0.5390132 0.3625037 0.4028016
## [3,] 0.50784216 0.1769726 0.2239341 0.2114200
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08078178 0.06078777 0.08344962 0.1456493
## [2,] 0.02196264 0.17342249 0.11663219 0.1295977
## [3,] 0.16339348 0.05693929 0.07204871 0.0680224
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04347704 0.03271619 0.04491288 0.07838895
## [2,] 0.01182037 0.09333660 0.06277186 0.06974992
## [3,] 0.08793895 0.03064493 0.03877687 0.03660990
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.33498203 0.2520718 0.3460449 0.6039714
## [2,] 0.09107363 0.7191401 0.4836448 0.5374094
## [3,] 0.67755231 0.2361132 0.2987681 0.2820721
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4283168 0.3223056 0.4424621 0.7722537
## [2,] 0.1164491 0.9195114 0.6184010 0.6871457
## [3,] 0.8663362 0.3019005 0.3820128 0.3606647
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1484691 0.1117220 0.1533723 0.2676892
## [2,] 0.0403652 0.3187337 0.2143587 0.2381879
## [3,] 0.3003014 0.1046489 0.1324185 0.1250186
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.25958300 0.1953345 0.2681558 0.4680273
## [2,] 0.07057443 0.5572733 0.3747842 0.4164472
## [3,] 0.52504626 0.1829679 0.2315203 0.2185822
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.14132084 0.10634302 0.1459880 0.2548010
## [2,] 0.03842177 0.30338787 0.2040381 0.2267201
## [3,] 0.28584299 0.09961045 0.1260431 0.1189994
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09081183 0.06833532 0.09381092 0.16373342
## [2,] 0.02468957 0.19495503 0.13111352 0.14568880
## [3,] 0.18368080 0.06400901 0.08099445 0.07646823
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1588021 0.1194975 0.1640466 0.2863196
## [2,] 0.0431745 0.3409166 0.2292774 0.2547651
## [3,] 0.3212015 0.1119322 0.1416345 0.1337195
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1408946 0.10602231 0.1455477 0.2540326
## [2,] 0.0383059 0.30247290 0.2034227 0.2260363
## [3,] 0.2849809 0.09931004 0.1256630 0.1186405
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.16983514 0.1277999 0.1754440 0.3062122
## [2,] 0.04617413 0.3646024 0.2452068 0.2724654
## [3,] 0.34351751 0.1197088 0.1514748 0.1430099
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12754959 0.09598024 0.1317620 0.2299715
## [2,] 0.03467769 0.27382372 0.1841552 0.2046269
## [3,] 0.25798852 0.08990374 0.1137606 0.1074033
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.35721123 0.2687991 0.3690082 0.6440506
## [2,] 0.09711722 0.7668618 0.5157392 0.5730715
## [3,] 0.72251426 0.2517815 0.3185942 0.3007902
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3693882 0.2779622 0.3815873 0.6660056
## [2,] 0.1004278 0.7930033 0.5333202 0.5926069
## [3,] 0.7471440 0.2603644 0.3294547 0.3110438
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2950148 0.2219967 0.3047578 0.5319107
## [2,] 0.0802075 0.6333384 0.4259404 0.4732903
## [3,] 0.5967125 0.2079421 0.2631217 0.2484176
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07577459 0.05701989 0.07827707 0.13662132
## [2,] 0.02060130 0.16267304 0.10940285 0.12156465
## [3,] 0.15326568 0.05340996 0.06758283 0.06380609
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.35458629 0.2668239 0.3662966 0.6393179
## [2,] 0.09640356 0.7612266 0.5119493 0.5688604
## [3,] 0.71720492 0.2499313 0.3162531 0.2985799
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.24452022 0.1839999 0.2525956 0.4408691
## [2,] 0.06647922 0.5249365 0.3530367 0.3922821
## [3,] 0.49457949 0.1723509 0.2180859 0.2058986
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.14341233 0.1079169 0.1481486 0.2585719
## [2,] 0.03899039 0.3078779 0.2070578 0.2300754
## [3,] 0.29007333 0.1010846 0.1279085 0.1207605
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.32682096 0.2459307 0.3376143 0.5892571
## [2,] 0.08885483 0.7016199 0.4718619 0.5243166
## [3,] 0.66104530 0.2303608 0.2914893 0.2752000
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.29586684 0.2226379 0.3056379 0.5334469
## [2,] 0.08043914 0.6351675 0.4271706 0.4746571
## [3,] 0.59843586 0.2085427 0.2638816 0.2491351
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.29926994 0.2251987 0.3091534 0.5395827
## [2,] 0.08136436 0.6424733 0.4320839 0.4801167
## [3,] 0.60531916 0.2109414 0.2669168 0.2520007
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3826900 0.2879718 0.3953285 0.6899888
## [2,] 0.1040443 0.8215597 0.5525253 0.6139470
## [3,] 0.7740490 0.2697403 0.3413186 0.3222446
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.17366029 0.1306782 0.1793955 0.3131089
## [2,] 0.04721409 0.3728143 0.2507296 0.2786020
## [3,] 0.35125446 0.1224050 0.1548864 0.1462309
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3457887 0.2602038 0.3572085 0.6234558
## [2,] 0.0940117 0.7423399 0.4992474 0.5547464
## [3,] 0.6994104 0.2437303 0.3084065 0.2911718
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.33955847 0.2555156 0.3507725 0.6122228
## [2,] 0.09231786 0.7289648 0.4902523 0.5447513
## [3,] 0.68680887 0.2393389 0.3028498 0.2859257
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13261374 0.09979098 0.1369933 0.2391021
## [2,] 0.03605452 0.28469544 0.1914668 0.2127513
## [3,] 0.26823154 0.09347322 0.1182773 0.1116676
DelayedTensor::einsum('ij,klm->ijklm', darrC, darrE)
## <3 x 4 x 3 x 4 x 5> array of class HDF5Array and type "double":
## ,,1,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.21125300 0.15896652 0.21822969 0.38088843
## [2,] 0.05743466 0.45351836 0.30500567 0.33891174
## [3,] 0.42729145 0.14890235 0.18841509 0.17788587
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.21397724 0.16101649 0.22104390 0.38580022
## [2,] 0.05817531 0.45936677 0.30893891 0.34328222
## [3,] 0.43280165 0.15082254 0.19084482 0.18017982
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.4235350 0.3187074 0.4375224 0.7636322
## [2,] 0.1151491 0.9092459 0.6114971 0.6794743
## [3,] 0.8566643 0.2985300 0.3777480 0.3566382
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.3457887 0.2602038 0.3572085 0.6234558
## [2,] 0.0940117 0.7423399 0.4992474 0.5547464
## [3,] 0.6994104 0.2437303 0.3084065 0.2911718
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.33955847 0.25551556 0.35077249 0.61222276
## [2,] 0.09231786 0.72896482 0.49025226 0.54475134
## [3,] 0.68680887 0.23933887 0.30284985 0.28592566
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.13261374 0.09979098 0.13699335 0.23910211
## [2,] 0.03605452 0.28469544 0.19146683 0.21275131
## [3,] 0.26823154 0.09347322 0.11827727 0.11166758
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.670708
DelayedTensor::einsum('i->', darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 1.670708
einsum::einsum('ij->', arrC)
## [1] 6.56555
DelayedTensor::einsum('ij->', darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 6.56555
einsum::einsum('ijk->', arrE)
## [1] 28.9475
DelayedTensor::einsum('ijk->', darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 28.9475
einsum::einsum('ij->i', arrC)
## [1] 2.075270 2.472480 2.017801
DelayedTensor::einsum('ij->i', darrC)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 2.075270 2.472480 2.017801
einsum::einsum('ij->j', arrC)
## [1] 1.490032 1.630065 1.523583 1.921870
DelayedTensor::einsum('ij->j', darrC)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 1.490032 1.630065 1.523583 1.921870
einsum::einsum('ijk->i', arrE)
## [1] 8.736759 9.887308 10.323432
DelayedTensor::einsum('ijk->i', darrE)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 8.736759 9.887308 10.323432
einsum::einsum('ijk->j', arrE)
## [1] 6.900972 6.842960 8.258851 6.944716
DelayedTensor::einsum('ijk->j', darrE)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 6.900972 6.842960 8.258851 6.944716
einsum::einsum('ijk->k', arrE)
## [1] 6.890424 4.355370 4.873750 5.941515 6.886439
DelayedTensor::einsum('ijk->k', darrE)
## <5> array of class HDF5Array and type "double":
## [1] [2] [3] [4] [5]
## 6.890424 4.355370 4.873750 5.941515 6.886439
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 2.234586 1.620213 2.312713 2.569247
## [2,] 1.745722 2.732998 3.083530 2.325058
## [3,] 2.920663 2.489750 2.862608 2.050411
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 2.234586 1.620213 2.312713 2.569247
## [2,] 1.745722 2.732998 3.083530 2.325058
## [3,] 2.920663 2.489750 2.862608 2.050411
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.8766586 0.6203067 1.062564 1.8492497 1.492193
## [2,] 2.2897529 1.0233477 0.971603 0.8643745 1.693882
## [3,] 1.8465255 1.7272734 1.824181 0.9690556 1.891815
## [4,] 0.8774871 0.9844420 1.015402 2.2588355 1.808549
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.8766586 0.6203067 1.0625641 1.8492497 1.4921928
## [2,] 2.2897529 1.0233477 0.9716030 0.8643745 1.6938822
## [3,] 1.8465255 1.7272734 1.8241810 0.9690556 1.8918153
## [4,] 0.8774871 0.9844420 1.0154024 2.2588355 1.8085488
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.8766586 0.6203067 1.062564 1.8492497 1.492193
## [2,] 2.2897529 1.0233477 0.971603 0.8643745 1.693882
## [3,] 1.8465255 1.7272734 1.824181 0.9690556 1.891815
## [4,] 0.8774871 0.9844420 1.015402 2.2588355 1.808549
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.8766586 0.6203067 1.0625641 1.8492497 1.4921928
## [2,] 2.2897529 1.0233477 0.9716030 0.8643745 1.6938822
## [3,] 1.8465255 1.7272734 1.8241810 0.9690556 1.8918153
## [4,] 0.8774871 0.9844420 1.0154024 2.2588355 1.8085488
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.44558
DelayedTensor::einsum('ii->', darrB)
## <1> array of class HDF5Array and type "double":
## [1]
## 1.44558
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.4922416 0.1234500 0.9899984
## [2,] 0.5896906 0.7546977 0.6795194
## [3,] 0.4100624 0.2353131 0.1986403
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3]
## [1,] 0.4922416 0.1234500 0.9899984
## [2,] 0.5896906 0.7546977 0.6795194
## [3,] 0.4100624 0.2353131 0.1986403
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.79631689 0.4074875 0.4326715
## [2,] 0.72054264 0.8558413 0.4587450
## [3,] 0.09911812 0.3253185 0.9349435
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.70043286 0.1532741 0.7553059
## [2,] 0.10984722 0.8330769 0.6406363
## [3,] 0.01071574 0.8401856 0.5132513
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.7122118 0.8124682 0.2943609
## [2,] 0.9961464 0.2778485 0.5620574
## [3,] 0.3386994 0.4044345 0.4389410
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.79631689 0.40748752 0.43267154
## [2,] 0.72054264 0.85584128 0.45874496
## [3,] 0.09911812 0.32531849 0.93494349
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.70043286 0.15327411 0.75530588
## [2,] 0.10984722 0.83307693 0.64063632
## [3,] 0.01071574 0.84018556 0.51325130
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.7122118 0.8124682 0.2943609
## [2,] 0.9961464 0.2778485 0.5620574
## [3,] 0.3386994 0.4044345 0.4389410
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.9337815
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 0.9337815
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 4.360571
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 4.360571
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 18.17358
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 18.17358
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.3189577 0.1793525 0.4909264 1.3340428 0.9350335
## [2,] 1.8368801 0.7994072 0.4235116 0.2612361 1.0506657
## [3,] 1.1522963 1.2384964 1.2742351 0.3175918 1.3012411
## [4,] 0.3623574 0.3571006 0.5897212 1.7163385 1.2341881
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.3189577 0.1793525 0.4909264 1.3340428 0.9350335
## [2,] 1.8368801 0.7994072 0.4235116 0.2612361 1.0506657
## [3,] 1.1522963 1.2384964 1.2742351 0.3175918 1.3012411
## [4,] 0.3623574 0.3571006 0.5897212 1.7163385 1.2341881
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.203624 1.2828180 1.0212513
## [2,] 1.282818 1.9107184 0.9617437
## [3,] 1.021251 0.9617437 1.2462287
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 1.2036242 1.2828180 1.0212513
## [2,] 1.2828180 1.9107184 0.9617437
## [3,] 1.0212513 0.9617437 1.2462287
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.2045524 0.01511983 0.8368494
## [2,] 0.1158271 0.94273283 0.1016253
## [3,] 0.2182863 0.42639756 0.1627160
## [4,] 0.6649583 0.52646815 0.1450380
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.20455243 0.01511983 0.83684936
## [2,] 0.11582711 0.94273283 0.10162533
## [3,] 0.21828634 0.42639756 0.16271599
## [4,] 0.66495833 0.52646815 0.14503799
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.2181731 0.110499794 0.10272785 0.89686197 0.02807001
## [2,] 0.2706780 0.001068107 0.19158650 0.09763551 0.10054681
## [3,] 0.4438072 0.103909674 0.12316587 0.09704749 0.43784617
## [4,] 0.1442960 0.208307076 0.03190231 0.62380026 0.58454361
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.2238363 0.0007221746 0.017810867 0.10776243 0.6146661
## [2,] 0.7508999 0.7877011751 0.003082726 0.04031626 0.5221739
## [3,] 0.2632304 0.1855235102 0.842885094 0.14101018 0.7159614
## [4,] 0.2170544 0.0382294906 0.009240922 0.66705449 0.5636695
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.876948368 0.06813053 0.3703877 0.32941840 0.29229738
## [2,] 0.815302222 0.01063795 0.2288424 0.12328429 0.42794497
## [3,] 0.445258728 0.94906324 0.3081841 0.07953412 0.14743358
## [4,] 0.001006978 0.11056404 0.5485780 0.42548379 0.08597504
DelayedTensor::einsum('ijk,ijk->jki', darrE, darrE)
## <4 x 5 x 3> array of class HDF5Array and type "double":
## ,,1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.218173059 0.110499794 0.102727849 0.896861973 0.028070008
## [2,] 0.270678012 0.001068107 0.191586495 0.097635511 0.100546813
## [3,] 0.443807193 0.103909674 0.123165869 0.097047490 0.437846166
## [4,] 0.144296028 0.208307076 0.031902314 0.623800263 0.584543613
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.2238363006 0.0007221746 0.0178108670 0.1077624327 0.6146660638
## [2,] 0.7508999148 0.7877011751 0.0030827261 0.0403162611 0.5221738913
## [3,] 0.2632303976 0.1855235102 0.8428850940 0.1410101838 0.7159613868
## [4,] 0.2170544025 0.0382294906 0.0092409219 0.6670544935 0.5636694577
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.876948368 0.068130530 0.370387687 0.329418401 0.292297380
## [2,] 0.815302222 0.010637953 0.228842416 0.123284294 0.427944975
## [3,] 0.445258728 0.949063243 0.308184097 0.079534122 0.147433577
## [4,] 0.001006978 0.110564041 0.548577980 0.425483785 0.085975042
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 2.033409 2.318609 2.538406
## [2,] 1.143854 1.540647 1.670869
## [3,] 1.287780 1.203197 2.382773
## [4,] 2.360830 1.721308 1.859378
## [5,] 1.910886 3.103547 1.872006
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 2.033409 2.318609 2.538406
## [2,] 1.143854 1.540647 1.670869
## [3,] 1.287780 1.203197 2.382773
## [4,] 2.360830 1.721308 1.859378
## [5,] 1.910886 3.103547 1.872006
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.02287371 1.158502e-02 0.01077020 0.094028832 0.002942917
## [2,] 0.01606919 6.340974e-05 0.01137381 0.005796273 0.005969106
## [3,] 0.04965371 1.162555e-02 0.01377995 0.010857795 0.048986780
## [4,] 0.04917899 7.099524e-02 0.01087295 0.212603677 0.199224221
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.002008161 6.479033e-06 0.0001597913 0.0009667972 0.005514514
## [2,] 0.420041056 4.406271e-01 0.0017244263 0.0225522530 0.292095482
## [3,] 0.066599629 4.693910e-02 0.2132574153 0.0356768290 0.181144590
## [4,] 0.067805006 1.194240e-02 0.0028867453 0.2083792515 0.176083095
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 4.144924e-01 0.032202113 0.17506492 0.155700659 0.138155290
## [2,] 4.679677e-02 0.000610598 0.01313511 0.007076281 0.024563216
## [3,] 4.092022e-02 0.087220924 0.02832277 0.007309354 0.013549458
## [4,] 8.248921e-05 0.009057138 0.04493818 0.034854600 0.007042867
DelayedTensor::einsum('i,ij,ijk,ijk,ji->jki',
darrA, darrC, darrE, darrE, t(darrC))
## <4 x 5 x 3> array of class HDF5Array and type "double":
## ,,1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.287371e-02 1.158502e-02 1.077020e-02 9.402883e-02 2.942917e-03
## [2,] 1.606919e-02 6.340974e-05 1.137381e-02 5.796273e-03 5.969106e-03
## [3,] 4.965371e-02 1.162555e-02 1.377995e-02 1.085780e-02 4.898678e-02
## [4,] 4.917899e-02 7.099524e-02 1.087295e-02 2.126037e-01 1.992242e-01
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.008161e-03 6.479033e-06 1.597913e-04 9.667972e-04 5.514514e-03
## [2,] 4.200411e-01 4.406271e-01 1.724426e-03 2.255225e-02 2.920955e-01
## [3,] 6.659963e-02 4.693910e-02 2.132574e-01 3.567683e-02 1.811446e-01
## [4,] 6.780501e-02 1.194240e-02 2.886745e-03 2.083793e-01 1.760831e-01
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 4.144924e-01 3.220211e-02 1.750649e-01 1.557007e-01 1.381553e-01
## [2,] 4.679677e-02 6.105980e-04 1.313511e-02 7.076281e-03 2.456322e-02
## [3,] 4.092022e-02 8.722092e-02 2.832277e-02 7.309354e-03 1.354946e-02
## [4,] 8.248921e-05 9.057138e-03 4.493818e-02 3.485460e-02 7.042867e-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.1.1 Patched (2021-08-22 r80813)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Mojave 10.14.6
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib
##
## locale:
## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] einsum_0.1.0 DelayedRandomArray_1.2.0 HDF5Array_1.22.0
## [4] rhdf5_2.38.0 DelayedArray_0.20.0 IRanges_2.28.0
## [7] S4Vectors_0.32.0 MatrixGenerics_1.6.0 matrixStats_0.61.0
## [10] BiocGenerics_0.40.0 Matrix_1.3-4 DelayedTensor_1.0.0
## [13] BiocStyle_2.22.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.7 rTensor_1.4.8 bslib_0.3.1
## [4] compiler_4.1.1 BiocManager_1.30.16 jquerylib_0.1.4
## [7] rhdf5filters_1.6.0 tools_4.1.1 digest_0.6.28
## [10] jsonlite_1.7.2 evaluate_0.14 lattice_0.20-45
## [13] rlang_0.4.12 parallel_4.1.1 yaml_2.2.1
## [16] xfun_0.27 fastmap_1.1.0 stringr_1.4.0
## [19] knitr_1.36 sass_0.4.0 grid_4.1.1
## [22] R6_2.5.1 BiocParallel_1.28.0 rmarkdown_2.11
## [25] bookdown_0.24 irlba_2.3.3 Rhdf5lib_1.16.0
## [28] magrittr_2.0.1 BiocSingular_1.10.0 htmltools_0.5.2
## [31] rsvd_1.0.5 beachmat_2.10.0 dqrng_0.3.0
## [34] ScaledMatrix_1.2.0 stringi_1.7.5