DelayedTensor 1.12.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2024-10-29 16:15:12
Compiled: Tue Oct 29 19:53:57 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.05613553 0.41347991 0.72001345
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.05613553 0.41347991 0.72001345
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.5954589 0.2015422 0.2262354 0.58545181
## [2,] 0.9615254 0.9626533 0.3663882 0.97125626
## [3,] 0.2753793 0.9915938 0.8722627 0.03860487
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.59545886 0.20154224 0.22623544 0.58545181
## [2,] 0.96152542 0.96265329 0.36638816 0.97125626
## [3,] 0.27537926 0.99159383 0.87226273 0.03860487
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2874059 0.22184346 0.2106767 0.5031662
## [2,] 0.1121082 0.33561027 0.5526318 0.3889234
## [3,] 0.8363360 0.07115157 0.8300148 0.9500176
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8913759 0.02759543 0.95374438 0.2449398
## [2,] 0.1054925 0.29765747 0.85573268 0.1895445
## [3,] 0.5686719 0.72406377 0.04240805 0.9268938
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3153777 0.04982351 0.6052920 0.9058128
## [2,] 0.1478107 0.31588184 0.5790844 0.9979326
## [3,] 0.1110919 0.12220339 0.4188806 0.3146449
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9997312 0.85461291 0.50658189 0.90793886
## [2,] 0.8901341 0.06823316 0.05471163 0.53913704
## [3,] 0.7096131 0.45693411 0.27763052 0.06149956
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.66793075 0.3465657 0.27382861 0.7536349
## [2,] 0.78198824 0.8114325 0.45190130 0.2011282
## [3,] 0.01835322 0.5435205 0.05120172 0.1575961
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.28740590 0.22184346 0.21067672 0.50316618
## [2,] 0.11210820 0.33561027 0.55263180 0.38892339
## [3,] 0.83633600 0.07115157 0.83001475 0.95001764
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.89137594 0.02759543 0.95374438 0.24493980
## [2,] 0.10549255 0.29765747 0.85573268 0.18954446
## [3,] 0.56867190 0.72406377 0.04240805 0.92689377
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.31537773 0.04982351 0.60529199 0.90581275
## [2,] 0.14781066 0.31588184 0.57908441 0.99793263
## [3,] 0.11109193 0.12220339 0.41888056 0.31464492
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.99973120 0.85461291 0.50658189 0.90793886
## [2,] 0.89013406 0.06823316 0.05471163 0.53913704
## [3,] 0.70961309 0.45693411 0.27763052 0.06149956
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.66793075 0.34656571 0.27382861 0.75363491
## [2,] 0.78198824 0.81143248 0.45190130 0.20112821
## [3,] 0.01835322 0.54352047 0.05120172 0.15759608
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.6643861 0.1527595 0.8637067
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.6643861 0.1527595 0.8637067
einsum::einsum('iii->i', arrD)
## [1] 0.2714472 0.3225690 0.4493381
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.2714472 0.3225690 0.4493381
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.003151198 0.170965639 0.518419368
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.003151198 0.170965639 0.518419368
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.35457126 0.04061927 0.05118247 0.342753818
## [2,] 0.92453113 0.92670135 0.13424029 0.943338715
## [3,] 0.07583374 0.98325833 0.76084227 0.001490336
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.354571258 0.040619275 0.051182473 0.342753818
## [2,] 0.924531129 0.926701355 0.134240287 0.943338715
## [3,] 0.075833739 0.983258327 0.760842267 0.001490336
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08260215 0.049214520 0.04438468 0.2531762
## [2,] 0.01256825 0.112634251 0.30540191 0.1512614
## [3,] 0.69945790 0.005062546 0.68892449 0.9025335
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.79455107 0.0007615079 0.909628341 0.05999551
## [2,] 0.01112868 0.0885999721 0.732278417 0.03592710
## [3,] 0.32338773 0.5242683463 0.001798443 0.85913206
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09946311 0.002482382 0.3663784 0.82049675
## [2,] 0.02184799 0.099781335 0.3353388 0.99586954
## [3,] 0.01234142 0.014933669 0.1754609 0.09900142
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9994625 0.730363232 0.256625207 0.824352972
## [2,] 0.7923386 0.004655765 0.002993363 0.290668746
## [3,] 0.5035507 0.208788777 0.077078703 0.003782196
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4461314908 0.1201078 0.074982110 0.56796557
## [2,] 0.6115056054 0.6584227 0.204214786 0.04045256
## [3,] 0.0003368406 0.2954145 0.002621616 0.02483653
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.082602152 0.049214520 0.044384680 0.253176202
## [2,] 0.012568249 0.112634251 0.305401912 0.151261402
## [3,] 0.699457899 0.005062546 0.688924488 0.902533512
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.7945510723 0.0007615079 0.9096283407 0.0599955073
## [2,] 0.0111286776 0.0885999721 0.7322784169 0.0359271032
## [3,] 0.3233877313 0.5242683463 0.0017984430 0.8591320574
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.099463113 0.002482382 0.366378388 0.820496747
## [2,] 0.021847992 0.099781335 0.335338755 0.995869538
## [3,] 0.012341416 0.014933669 0.175460922 0.099001424
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.999462479 0.730363232 0.256625207 0.824352972
## [2,] 0.792338649 0.004655765 0.002993363 0.290668746
## [3,] 0.503550730 0.208788777 0.077078703 0.003782196
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.4461314908 0.1201077929 0.0749821104 0.5679655739
## [2,] 0.6115056054 0.6584226656 0.2042147857 0.0404525563
## [3,] 0.0003368406 0.2954145027 0.0026216163 0.0248365255
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.003151198 0.02321092 0.04041834
## [2,] 0.023210915 0.17096564 0.29771110
## [3,] 0.040418338 0.29771110 0.51841937
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.003151198 0.023210915 0.040418338
## [2,] 0.023210915 0.170965639 0.297711099
## [3,] 0.040418338 0.297711099 0.518419368
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.17113839 0.05792443 0.0650214 0.16826230
## [2,] 0.27634808 0.27667224 0.1053021 0.27914478
## [3,] 0.07914563 0.28498992 0.2506935 0.01109527
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06675582 0.02259454 0.02536285 0.065633950
## [2,] 0.10779489 0.10792133 0.04107512 0.108885794
## [3,] 0.03087227 0.11116580 0.09778781 0.004327923
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4980037 0.1685570 0.1892088 0.48963442
## [2,] 0.8041583 0.8051016 0.3064236 0.81229657
## [3,] 0.2303096 0.8293056 0.7295047 0.03228665
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13209865 0.04471083 0.05018885 0.129878654
## [2,] 0.21330813 0.21355834 0.08128082 0.215466848
## [3,] 0.06109109 0.21997861 0.19350578 0.008564239
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.19984211 0.06763965 0.07592694 0.19648364
## [2,] 0.32269780 0.32307633 0.12296363 0.32596357
## [3,] 0.09242011 0.33278907 0.29274033 0.01295619
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04236783 0.01434005 0.01609701 0.041655814
## [2,] 0.06841404 0.06849429 0.02606909 0.069106406
## [3,] 0.01959367 0.07055346 0.06206286 0.002746797
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1254493 0.04246026 0.04766254 0.123341066
## [2,] 0.2025710 0.20280864 0.07718946 0.204621082
## [3,] 0.0580160 0.20890574 0.18376545 0.008133148
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3290695 0.1113787 0.1250249 0.32353929
## [2,] 0.5313695 0.5319928 0.2024778 0.53674710
## [3,] 0.1521833 0.5479863 0.4820401 0.02133428
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4942396 0.1672830 0.1877788 0.48593364
## [2,] 0.7980803 0.7990164 0.3041076 0.80615702
## [3,] 0.2285689 0.8230375 0.7239909 0.03204262
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2996148 0.1014092 0.1138340 0.29457955
## [2,] 0.4838071 0.4843746 0.1843541 0.48870330
## [3,] 0.1385615 0.4989365 0.4388931 0.01942467
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2315879 0.07838449 0.08798825 0.22769590
## [2,] 0.3739597 0.37439838 0.14249693 0.37774427
## [3,] 0.1071014 0.38565403 0.33924338 0.01501434
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5656964 0.1914687 0.2149277 0.55618954
## [2,] 0.9134661 0.9145376 0.3480752 0.92271057
## [3,] 0.2616152 0.9420316 0.8286650 0.03667531
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5307777 0.1796499 0.2016608 0.52185766
## [2,] 0.8570806 0.8580860 0.3265896 0.86575446
## [3,] 0.2454665 0.8838829 0.7775140 0.03441146
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06281647 0.0212612 0.02386615 0.061760803
## [2,] 0.10143377 0.1015527 0.03865122 0.102460297
## [3,] 0.02905046 0.1046058 0.09201722 0.004072527
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3386207 0.1146114 0.1286537 0.33292999
## [2,] 0.5467925 0.5474339 0.2083547 0.55232614
## [3,] 0.1566004 0.5638915 0.4960313 0.02195351
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01643195 0.005561645 0.006243065 0.016155796
## [2,] 0.02653371 0.026564835 0.010110640 0.026802237
## [3,] 0.00759921 0.027363462 0.024070468 0.001065318
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1772428 0.05999055 0.06734067 0.17426411
## [2,] 0.2862052 0.28654095 0.10905818 0.28910168
## [3,] 0.0819687 0.29515532 0.25963552 0.01149103
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4311502 0.1459294 0.1638089 0.42390444
## [2,] 0.6962057 0.6970224 0.2652884 0.70325147
## [3,] 0.1993921 0.7179772 0.6315738 0.02795239
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5679155 0.1922198 0.2157708 0.55837137
## [2,] 0.9170495 0.9181252 0.3494407 0.92633020
## [3,] 0.2626414 0.9457270 0.8319157 0.03681918
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5095536 0.1724663 0.1935971 0.50099024
## [2,] 0.8228087 0.8237739 0.3135303 0.83113572
## [3,] 0.2356510 0.8485392 0.7464237 0.03303545
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02525225 0.008547014 0.009594204 0.024827871
## [2,] 0.04077642 0.040824252 0.015537809 0.041189087
## [3,] 0.01167830 0.042051564 0.036990964 0.001637158
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.14585158 0.04936572 0.05541406 0.14340045
## [2,] 0.23551585 0.23579211 0.08974304 0.23789932
## [3,] 0.06745134 0.24288080 0.21365186 0.00945587
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11286593 0.03820122 0.04288167 0.11096915
## [2,] 0.18225182 0.18246560 0.06944685 0.18409624
## [3,] 0.05219661 0.18795112 0.16533257 0.00731734
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5519271 0.1868082 0.2096962 0.54265163
## [2,] 0.8912319 0.8922773 0.3396029 0.90025137
## [3,] 0.2552473 0.9191021 0.8084949 0.03578262
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.18779446 0.06356193 0.07134962 0.18463846
## [2,] 0.30324370 0.30359941 0.11555067 0.30631259
## [3,] 0.08684849 0.31272661 0.27509224 0.01217512
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08801517 0.02979009 0.03344001 0.086536020
## [2,] 0.14212371 0.14229042 0.05415608 0.143562031
## [3,] 0.04070399 0.14656814 0.12892973 0.005706212
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06615067 0.02238972 0.02513293 0.06503897
## [2,] 0.10681771 0.10694301 0.04070277 0.10789873
## [3,] 0.03059241 0.11015807 0.09690135 0.00428869
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02966785 0.01004154 0.01127184 0.02916926
## [2,] 0.04790657 0.04796276 0.01825474 0.04839139
## [3,] 0.01372036 0.04940468 0.04345919 0.00192343
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.18809464 0.06366353 0.07146367 0.18493359
## [2,] 0.30372842 0.30408469 0.11573537 0.30680221
## [3,] 0.08698731 0.31322648 0.27553195 0.01219458
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07276709 0.02462915 0.02764674 0.071544197
## [2,] 0.11750167 0.11763950 0.04477388 0.118690810
## [3,] 0.03365228 0.12117613 0.10659346 0.004717647
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3604265 0.1219919 0.1369385 0.35436929
## [2,] 0.5820036 0.5826863 0.2217718 0.58789363
## [3,] 0.1666849 0.6002038 0.5279736 0.02336722
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3448209 0.1167100 0.1310094 0.33902601
## [2,] 0.5568044 0.5574575 0.2121697 0.56243936
## [3,] 0.1594678 0.5742165 0.5051137 0.02235548
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2494261 0.08442213 0.09476563 0.24523438
## [2,] 0.4027643 0.40323675 0.15347288 0.40684036
## [3,] 0.1153510 0.41535938 0.36537390 0.01617083
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5393742 0.1825595 0.2049269 0.53030971
## [2,] 0.8709620 0.8719836 0.3318791 0.87977631
## [3,] 0.2494420 0.8981983 0.7901067 0.03496879
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5942278 0.2011256 0.2257677 0.58424146
## [2,] 0.9595376 0.9606631 0.3656307 0.96924831
## [3,] 0.2748100 0.9895438 0.8704594 0.03852506
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.18735810 0.06341424 0.07118383 0.18420944
## [2,] 0.30253909 0.30289396 0.11528217 0.30560084
## [3,] 0.08664669 0.31199996 0.27445303 0.01214683
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5952988 0.2014881 0.2261746 0.5852944
## [2,] 0.9612670 0.9623945 0.3662897 0.9709952
## [3,] 0.2753052 0.9913273 0.8720283 0.0385945
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5300382 0.1793996 0.2013799 0.52113059
## [2,] 0.8558865 0.8568905 0.3261346 0.86454828
## [3,] 0.2451245 0.8826514 0.7764308 0.03436351
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4225454 0.1430170 0.1605396 0.41544426
## [2,] 0.6823110 0.6831114 0.2599938 0.68921615
## [3,] 0.1954127 0.7036480 0.6189690 0.02739452
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5088868 0.1722406 0.1933437 0.50033467
## [2,] 0.8217320 0.8226959 0.3131201 0.83004814
## [3,] 0.2353427 0.8474289 0.7454470 0.03299222
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04063004 0.01375186 0.01543676 0.039947229
## [2,] 0.06560792 0.06568488 0.02499982 0.066271888
## [3,] 0.01879000 0.06765958 0.05951725 0.002634133
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2720855 0.09209152 0.1033747 0.26751290
## [2,] 0.4393538 0.43986912 0.1674152 0.44380011
## [3,] 0.1258302 0.45309304 0.3985666 0.01763988
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3016487 0.1020976 0.1146068 0.29657928
## [2,] 0.4870914 0.4876627 0.1856056 0.49202083
## [3,] 0.1395021 0.5023235 0.4418725 0.01955653
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03257853 0.01102671 0.01237771 0.032031025
## [2,] 0.05260663 0.05266833 0.02004570 0.053139017
## [3,] 0.01506645 0.05425172 0.04772292 0.002112136
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.16531755 0.05595428 0.06280986 0.16253929
## [2,] 0.26694880 0.26726193 0.10172053 0.26965037
## [3,] 0.07645369 0.27529671 0.24216675 0.01071789
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5406402 0.1829880 0.2054079 0.53155445
## [2,] 0.8730063 0.8740303 0.3326581 0.88184130
## [3,] 0.2500275 0.9003066 0.7919612 0.03505087
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3210339 0.1086589 0.1219719 0.31563875
## [2,] 0.5183940 0.5190020 0.1975334 0.52364022
## [3,] 0.1484672 0.5346050 0.4702691 0.02081332
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03662046 0.01239476 0.01391338 0.036005030
## [2,] 0.05913339 0.05920276 0.02253271 0.059731835
## [3,] 0.01693570 0.06098259 0.05364378 0.002374183
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3977253 0.1346163 0.1511096 0.39104127
## [2,] 0.6422324 0.6429857 0.2447219 0.64873192
## [3,] 0.1839343 0.6623160 0.5826111 0.02578538
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4656418 0.1576037 0.1769135 0.45781643
## [2,] 0.7519016 0.7527835 0.2865112 0.75951097
## [3,] 0.2153433 0.7754147 0.6820992 0.03018856
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.010928587 0.003698949 0.004152148 0.0107449250
## [2,] 0.017647086 0.017667786 0.006724402 0.0178256785
## [3,] 0.005054096 0.018198938 0.016008829 0.0007085237
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.20636563 0.06984763 0.07840545 0.20289752
## [2,] 0.33323174 0.33362262 0.12697757 0.33660412
## [3,] 0.09543701 0.34365242 0.30229635 0.01337913
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4831747 0.1635379 0.1835748 0.47505461
## [2,] 0.7802130 0.7811281 0.2972993 0.78810887
## [3,] 0.2234517 0.8046114 0.7077823 0.03132525
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3236441 0.1095423 0.1229636 0.31820504
## [2,] 0.5226087 0.5232218 0.1991395 0.52789766
## [3,] 0.1496743 0.5389515 0.4740926 0.02098254
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.16305368 0.05518803 0.06194974 0.16031346
## [2,] 0.26329317 0.26360202 0.10032756 0.26595776
## [3,] 0.07540672 0.27152677 0.23885049 0.01057112
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2690886 0.0910772 0.1022361 0.26456643
## [2,] 0.4345146 0.4350243 0.1655713 0.43891197
## [3,] 0.1244442 0.4481025 0.3941767 0.01744559
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03048852 0.01031931 0.01158364 0.029976140
## [2,] 0.04923176 0.04928951 0.01875970 0.049729992
## [3,] 0.01409989 0.05077131 0.04466135 0.001976636
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4487586 0.1518893 0.1704989 0.44121692
## [2,] 0.7246391 0.7254891 0.2761229 0.73197262
## [3,] 0.2075354 0.7472997 0.6573676 0.02909398
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11976357 0.04053583 0.04550233 0.117750873
## [2,] 0.19338988 0.19361673 0.07369100 0.195347031
## [3,] 0.05538654 0.19943749 0.17543664 0.007764529
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09384198 0.03176227 0.03565382 0.092264912
## [2,] 0.15153264 0.15171039 0.05774134 0.153066182
## [3,] 0.04339869 0.15627130 0.13746519 0.006083977
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.17113839 0.05792443 0.06502140 0.16826230
## [2,] 0.27634808 0.27667224 0.10530212 0.27914478
## [3,] 0.07914563 0.28498992 0.25069346 0.01109527
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.066755824 0.022594539 0.025362849 0.065633950
## [2,] 0.107794887 0.107921331 0.041075119 0.108885794
## [3,] 0.030872275 0.111165803 0.097787808 0.004327923
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.49800368 0.16855703 0.18920884 0.48963442
## [2,] 0.80415832 0.80510160 0.30642361 0.81229657
## [3,] 0.23030959 0.82930562 0.72950472 0.03228665
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.44875859 0.15188927 0.17049892 0.44121692
## [2,] 0.72463912 0.72548912 0.27612291 0.73197262
## [3,] 0.20753543 0.74729973 0.65736764 0.02909398
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.119763575 0.040535830 0.045502328 0.117750873
## [2,] 0.193389885 0.193616731 0.073690995 0.195347031
## [3,] 0.055386538 0.199437491 0.175436640 0.007764529
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.093841985 0.031762268 0.035653819 0.092264912
## [2,] 0.151532640 0.151710388 0.057741340 0.153066182
## [3,] 0.043398693 0.156271304 0.137465190 0.006083977
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.189629
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.189629
einsum::einsum('ij->', arrC)
## [1] 7.048352
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 7.048352
einsum::einsum('ijk->', arrE)
## [1] 27.39768
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 27.39768
einsum::einsum('ij->i', arrC)
## [1] 1.608688 3.261823 2.177841
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 1.608688 3.261823 2.177841
einsum::einsum('ij->j', arrC)
## [1] 1.832364 2.155789 1.464886 1.595313
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 1.832364 2.155789 1.464886 1.595313
einsum::einsum('ijk->i', arrE)
## [1] 10.527879 8.677076 8.192727
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 10.527879 8.677076 8.192727
einsum::einsum('ijk->j', arrE)
## [1] 7.443421 5.247130 6.664321 8.042810
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 7.443421 5.247130 6.664321 8.042810
einsum::einsum('ijk->k', arrE)
## [1] 5.299886 5.828120 4.883836 6.326758 5.059082
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 5.299886 5.828120 4.883836 6.326758 5.059082
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 3.161822 1.500441 2.550124 3.315493
## [2,] 2.037534 1.828815 2.494062 2.316666
## [3,] 2.244066 1.917873 1.620136 2.410652
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 3.161822 1.500441 2.550124 3.315493
## [2,] 2.037534 1.828815 2.494062 2.316666
## [3,] 2.244066 1.917873 1.620136 2.410652
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.2358501 1.565540 0.5742803 2.599478 1.4682722
## [2,] 0.6286053 1.049317 0.4879087 1.379780 1.7015187
## [3,] 1.5933233 1.851885 1.6032570 0.838924 0.7769316
## [4,] 1.8421072 1.361378 2.2183903 1.508575 1.1123592
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.2358501 1.5655404 0.5742803 2.5994784 1.4682722
## [2,] 0.6286053 1.0493167 0.4879087 1.3797802 1.7015187
## [3,] 1.5933233 1.8518851 1.6032570 0.8389240 0.7769316
## [4,] 1.8421072 1.3613780 2.2183903 1.5085755 1.1123592
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.2358501 1.565540 0.5742803 2.599478 1.4682722
## [2,] 0.6286053 1.049317 0.4879087 1.379780 1.7015187
## [3,] 1.5933233 1.851885 1.6032570 0.838924 0.7769316
## [4,] 1.8421072 1.361378 2.2183903 1.508575 1.1123592
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.2358501 1.5655404 0.5742803 2.5994784 1.4682722
## [2,] 0.6286053 1.0493167 0.4879087 1.3797802 1.7015187
## [3,] 1.5933233 1.8518851 1.6032570 0.8389240 0.7769316
## [4,] 1.8421072 1.3613780 2.2183903 1.5085755 1.1123592
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.680852
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 1.680852
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.6643861 0.7602512 0.4707616
## [2,] 0.1196401 0.1527595 0.1956835
## [3,] 0.2713793 0.7026801 0.8637067
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.6643861 0.7602512 0.4707616
## [2,] 0.1196401 0.1527595 0.1956835
## [3,] 0.2713793 0.7026801 0.8637067
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.2714472 0.039308626 0.1689075
## [2,] 0.6079455 0.004129363 0.5498044
## [3,] 0.1855906 0.899994869 0.6027034
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.04456308 0.02432778 0.8712002
## [2,] 0.04514756 0.32256896 0.5502526
## [3,] 0.31734239 0.71199167 0.2049334
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.55573373 0.33691987 0.7517178
## [2,] 0.05527696 0.09784828 0.8946766
## [3,] 0.05808796 0.52534310 0.4493381
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.271447169 0.039308626 0.168907512
## [2,] 0.607945545 0.004129363 0.549804432
## [3,] 0.185590610 0.899994869 0.602703393
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.04456308 0.02432778 0.87120022
## [2,] 0.04514756 0.32256896 0.55025263
## [3,] 0.31734239 0.71199167 0.20493340
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.55573373 0.33691987 0.75171776
## [2,] 0.05527696 0.09784828 0.89467663
## [3,] 0.05808796 0.52534310 0.44933809
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.6925362
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 0.6925362
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 5.539363
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 5.539363
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 18.43373
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 18.43373
The inner product is an operation that eliminates all subscripts, while the outer product is an operation that leaves all subscripts intact. In the middle of the two, the operation that eliminates some subscripts while keeping others by summing them is called contracted product.
einsum::einsum('ijk,ijk->jk', arrE, arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.7946283 1.1290675 0.1336525 2.2953519 1.0579739
## [2,] 0.1669113 0.6136298 0.1171974 0.9438078 1.0739450
## [3,] 1.0387111 1.6437052 0.8771781 0.3366973 0.2818185
## [4,] 1.3069711 0.9550547 1.9153677 1.1188039 0.6332547
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.7946283 1.1290675 0.1336525 2.2953519 1.0579739
## [2,] 0.1669113 0.6136298 0.1171974 0.9438078 1.0739450
## [3,] 1.0387111 1.6437052 0.8771781 0.3366973 0.2818185
## [4,] 1.3069711 0.9550547 1.9153677 1.1188039 0.6332547
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 0.7891268 1.418078 0.5837631
## [2,] 1.4180779 2.928811 1.5764272
## [3,] 0.5837631 1.576427 1.8214247
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.7891268 1.4180779 0.5837631
## [2,] 1.4180779 2.9288115 1.5764272
## [3,] 0.5837631 1.5764272 1.8214247
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.35457126 0.9245311 0.075833739
## [2,] 0.04061927 0.9267014 0.983258327
## [3,] 0.05118247 0.1342403 0.760842267
## [4,] 0.34275382 0.9433387 0.001490336
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.354571258 0.924531129 0.075833739
## [2,] 0.040619275 0.926701355 0.983258327
## [3,] 0.051182473 0.134240287 0.760842267
## [4,] 0.342753818 0.943338715 0.001490336
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.08260215 0.7945510723 0.099463113 0.9994625 0.44613149
## [2,] 0.04921452 0.0007615079 0.002482382 0.7303632 0.12010779
## [3,] 0.04438468 0.9096283407 0.366378388 0.2566252 0.07498211
## [4,] 0.25317620 0.0599955073 0.820496747 0.8243530 0.56796557
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.01256825 0.01112868 0.02184799 0.792338649 0.61150561
## [2,] 0.11263425 0.08859997 0.09978133 0.004655765 0.65842267
## [3,] 0.30540191 0.73227842 0.33533875 0.002993363 0.20421479
## [4,] 0.15126140 0.03592710 0.99586954 0.290668746 0.04045256
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.699457899 0.323387731 0.01234142 0.503550730 0.0003368406
## [2,] 0.005062546 0.524268346 0.01493367 0.208788777 0.2954145027
## [3,] 0.688924488 0.001798443 0.17546092 0.077078703 0.0026216163
## [4,] 0.902533512 0.859132057 0.09900142 0.003782196 0.0248365255
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.0826021521 0.7945510723 0.0994631127 0.9994624789 0.4461314908
## [2,] 0.0492145205 0.0007615079 0.0024823817 0.7303632322 0.1201077929
## [3,] 0.0443846800 0.9096283407 0.3663783882 0.2566252072 0.0749821104
## [4,] 0.2531762025 0.0599955073 0.8204967467 0.8243529725 0.5679655739
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.012568249 0.011128678 0.021847992 0.792338649 0.611505605
## [2,] 0.112634251 0.088599972 0.099781335 0.004655765 0.658422666
## [3,] 0.305401912 0.732278417 0.335338755 0.002993363 0.204214786
## [4,] 0.151261402 0.035927103 0.995869538 0.290668746 0.040452556
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.6994578989 0.3233877313 0.0123414161 0.5035507304 0.0003368406
## [2,] 0.0050625457 0.5242683463 0.0149336692 0.2087887773 0.2954145027
## [3,] 0.6889244881 0.0017984430 0.1754609216 0.0770787030 0.0026216163
## [4,] 0.9025335121 0.8591320574 0.0990014239 0.0037821963 0.0248365255
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 1.223092 1.389274 2.6875200
## [2,] 2.117656 1.448427 2.2620375
## [3,] 1.876306 2.040710 0.9668208
## [4,] 3.268865 1.552216 1.5056773
## [5,] 2.041960 2.246450 0.7706715
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.2230923 1.3892737 2.6875200
## [2,] 2.1176556 1.4484272 2.2620375
## [3,] 1.8763060 2.0407095 0.9668208
## [4,] 3.2688649 1.5522159 1.5056773
## [5,] 2.0419600 2.2464502 0.7706715
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.0016441171 1.581478e-02 1.979718e-03 0.0198933475 0.0088798219
## [2,] 0.0001122182 1.736379e-06 5.660289e-06 0.0016653630 0.0002738679
## [3,] 0.0001275241 2.613504e-03 1.052662e-03 0.0007373241 0.0002154353
## [4,] 0.0048712793 1.154354e-03 1.578691e-02 0.0158611019 0.0109280371
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.004804528 0.004254216 0.008351942 0.3028912977 0.23376334
## [2,] 0.043158336 0.033949064 0.038233453 0.0017839605 0.25228939
## [3,] 0.016951535 0.040645598 0.018613200 0.0001661486 0.01133508
## [4,] 0.058999753 0.014013424 0.388440517 0.1133758125 0.01577858
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0381913190 0.0176573944 0.0006738575 2.749453e-02 1.839194e-05
## [2,] 0.0035840759 0.3711606098 0.0105724288 1.478139e-01 2.091414e-01
## [3,] 0.3774043160 0.0009852170 0.0961204170 4.222500e-02 1.436165e-03
## [4,] 0.0009684746 0.0009219021 0.0001062347 4.058532e-06 2.665114e-05
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,] 1.644117e-03 1.581478e-02 1.979718e-03 1.989335e-02 8.879822e-03
## [2,] 1.122182e-04 1.736379e-06 5.660289e-06 1.665363e-03 2.738679e-04
## [3,] 1.275241e-04 2.613504e-03 1.052662e-03 7.373241e-04 2.154353e-04
## [4,] 4.871279e-03 1.154354e-03 1.578691e-02 1.586110e-02 1.092804e-02
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0048045282 0.0042542158 0.0083519423 0.3028912977 0.2337633368
## [2,] 0.0431583358 0.0339490636 0.0382334531 0.0017839605 0.2522893904
## [3,] 0.0169515353 0.0406455984 0.0186131996 0.0001661486 0.0113350769
## [4,] 0.0589997534 0.0140134245 0.3884405171 0.1133758125 0.0157785847
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 3.819132e-02 1.765739e-02 6.738575e-04 2.749453e-02 1.839194e-05
## [2,] 3.584076e-03 3.711606e-01 1.057243e-02 1.478139e-01 2.091414e-01
## [3,] 3.774043e-01 9.852170e-04 9.612042e-02 4.222500e-02 1.436165e-03
## [4,] 9.684746e-04 9.219021e-04 1.062347e-04 4.058532e-06 2.665114e-05
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.1 (2024-06-14)
## Platform: x86_64-apple-darwin20
## Running under: macOS Monterey 12.7.6
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0
##
## locale:
## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## time zone: America/New_York
## tzcode source: internal
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] einsum_0.1.2 DelayedRandomArray_1.14.0
## [3] HDF5Array_1.34.0 rhdf5_2.50.0
## [5] DelayedArray_0.32.0 SparseArray_1.6.0
## [7] S4Arrays_1.6.0 abind_1.4-8
## [9] IRanges_2.40.0 S4Vectors_0.44.0
## [11] MatrixGenerics_1.18.0 matrixStats_1.4.1
## [13] BiocGenerics_0.52.0 Matrix_1.7-1
## [15] DelayedTensor_1.12.0 BiocStyle_2.34.0
##
## loaded via a namespace (and not attached):
## [1] jsonlite_1.8.9 compiler_4.4.1 BiocManager_1.30.25
## [4] crayon_1.5.3 rsvd_1.0.5 Rcpp_1.0.13
## [7] rhdf5filters_1.18.0 parallel_4.4.1 jquerylib_0.1.4
## [10] BiocParallel_1.40.0 yaml_2.3.10 fastmap_1.2.0
## [13] lattice_0.22-6 R6_2.5.1 XVector_0.46.0
## [16] ScaledMatrix_1.14.0 knitr_1.48 bookdown_0.41
## [19] bslib_0.8.0 rlang_1.1.4 cachem_1.1.0
## [22] xfun_0.48 sass_0.4.9 cli_3.6.3
## [25] Rhdf5lib_1.28.0 BiocSingular_1.22.0 zlibbioc_1.52.0
## [28] digest_0.6.37 grid_4.4.1 irlba_2.3.5.1
## [31] rTensor_1.4.8 dqrng_0.4.1 lifecycle_1.0.4
## [34] evaluate_1.0.1 codetools_0.2-20 beachmat_2.22.0
## [37] rmarkdown_2.28 tools_4.4.1 htmltools_0.5.8.1