DelayedTensor 1.2.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2022-04-26 14:34:11
Compiled: Tue Apr 26 16:29:49 2022
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.3439338 0.1327868 0.1841393
DelayedTensor::einsum('i->i', darrA)
## <3> array of class DelayedArray and type "double":
## [1] [2] [3]
## 0.3439338 0.1327868 0.1841393
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.7395031 0.09469429 0.7118025 0.3014190
## [2,] 0.5469468 0.97729942 0.1966828 0.8247099
## [3,] 0.3595283 0.16778860 0.1635929 0.6862923
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.73950305 0.09469429 0.71180246 0.30141903
## [2,] 0.54694685 0.97729942 0.19668285 0.82470988
## [3,] 0.35952827 0.16778860 0.16359288 0.68629234
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9945582 0.92750945 0.63928385 0.58138859
## [2,] 0.8016844 0.02893851 0.29174918 0.09936193
## [3,] 0.2004097 0.16992099 0.04627957 0.97137015
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7396856 0.37319973 0.8366278 0.5792793
## [2,] 0.3224259 0.05169714 0.6311730 0.2345900
## [3,] 0.1305053 0.90643915 0.9548810 0.3476399
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3465467 0.186302 0.2189995 0.007094816
## [2,] 0.4731529 0.734652 0.2403506 0.293607944
## [3,] 0.8221319 0.213192 0.5738681 0.736730504
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7442052 0.1700390 0.21706057 0.1123424
## [2,] 0.9990560 0.8140010 0.26416810 0.3856158
## [3,] 0.3986464 0.1438258 0.02736673 0.8689806
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05541274 0.6702479 0.7803815 0.2820069
## [2,] 0.29570447 0.4448090 0.5827925 0.4753475
## [3,] 0.91662679 0.2609817 0.8226270 0.3592090
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.99455815 0.92750945 0.63928385 0.58138859
## [2,] 0.80168442 0.02893851 0.29174918 0.09936193
## [3,] 0.20040970 0.16992099 0.04627957 0.97137015
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.73968561 0.37319973 0.83662778 0.57927935
## [2,] 0.32242591 0.05169714 0.63117298 0.23459003
## [3,] 0.13050530 0.90643915 0.95488095 0.34763993
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.346546692 0.186302045 0.218999533 0.007094816
## [2,] 0.473152888 0.734651983 0.240350576 0.293607944
## [3,] 0.822131905 0.213192038 0.573868146 0.736730504
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.74420517 0.17003900 0.21706057 0.11234237
## [2,] 0.99905600 0.81400096 0.26416810 0.38561583
## [3,] 0.39864640 0.14382578 0.02736673 0.86898062
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.05541274 0.67024794 0.78038155 0.28200694
## [2,] 0.29570447 0.44480902 0.58279250 0.47534755
## [3,] 0.91662679 0.26098167 0.82262698 0.35920897
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.8646593 0.4662422 0.9516241
DelayedTensor::einsum('ii->i', darrB)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.8646593 0.4662422 0.9516241
einsum::einsum('iii->i', arrD)
## [1] 0.3054856 0.1080123 0.5187663
DelayedTensor::einsum('iii->i', darrD)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.3054856 0.1080123 0.5187663
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.11829047 0.01763233 0.03390728
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.11829047 0.01763233 0.03390728
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.5468648 0.008967008 0.50666274 0.09085343
## [2,] 0.2991509 0.955114160 0.03868414 0.68014639
## [3,] 0.1292606 0.028153015 0.02676263 0.47099717
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.546864765 0.008967008 0.506662739 0.090853429
## [2,] 0.299150854 0.955114160 0.038684143 0.680146386
## [3,] 0.129260577 0.028153015 0.026762631 0.470997174
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.98914592 0.8602737792 0.408683843 0.338012690
## [2,] 0.64269790 0.0008374373 0.085117583 0.009872792
## [3,] 0.04016405 0.0288731423 0.002141798 0.943559964
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.54713480 0.139278040 0.6999460 0.33556456
## [2,] 0.10395847 0.002672595 0.3983793 0.05503248
## [3,] 0.01703163 0.821631925 0.9117976 0.12085352
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1200946 0.03470845 0.0479608 5.033642e-05
## [2,] 0.2238737 0.53971354 0.0577684 8.620562e-02
## [3,] 0.6759009 0.04545085 0.3293246 5.427718e-01
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5538413 0.02891326 0.0471152920 0.01262081
## [2,] 0.9981129 0.66259756 0.0697847828 0.14869957
## [3,] 0.1589190 0.02068585 0.0007489379 0.75512732
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.003070571 0.44923231 0.6089954 0.07952791
## [2,] 0.087441133 0.19785507 0.3396471 0.22595529
## [3,] 0.840204669 0.06811143 0.6767151 0.12903108
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.9891459234 0.8602737792 0.4086838434 0.3380126895
## [2,] 0.6426979046 0.0008374373 0.0851175830 0.0098727923
## [3,] 0.0401640485 0.0288731423 0.0021417983 0.9435599640
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.547134796 0.139278040 0.699946044 0.335564562
## [2,] 0.103958468 0.002672595 0.398379329 0.055032482
## [3,] 0.017031632 0.821631925 0.911797637 0.120853522
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 1.200946e-01 3.470845e-02 4.796080e-02 5.033642e-05
## [2,] 2.238737e-01 5.397135e-01 5.776840e-02 8.620562e-02
## [3,] 6.759009e-01 4.545085e-02 3.293246e-01 5.427718e-01
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.5538413300 0.0289132607 0.0471152920 0.0126208071
## [2,] 0.9981128903 0.6625975560 0.0697847828 0.1486995668
## [3,] 0.1589189521 0.0206858542 0.0007489379 0.7551273225
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.003070571 0.449232305 0.608995363 0.079527914
## [2,] 0.087441133 0.197855068 0.339647100 0.225955291
## [3,] 0.840204669 0.068111433 0.676715147 0.129031083
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.11829047 0.04566987 0.06333173
## [2,] 0.04566987 0.01763233 0.02445127
## [3,] 0.06333173 0.02445127 0.03390728
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.11829047 0.04566987 0.06333173
## [2,] 0.04566987 0.01763233 0.02445127
## [3,] 0.06333173 0.02445127 0.03390728
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7354788 0.09417898 0.7079289 0.2997788
## [2,] 0.5439704 0.97198111 0.1956125 0.8202219
## [3,] 0.3575718 0.16687552 0.1627026 0.6825576
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5928481 0.07591493 0.5706409 0.2416429
## [2,] 0.4384788 0.78348572 0.1576776 0.6611571
## [3,] 0.2882282 0.13451351 0.1311499 0.5501899
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.14820359 0.01897765 0.14265212 0.0604073
## [2,] 0.10961345 0.19586029 0.03941715 0.1652799
## [3,] 0.07205295 0.03362646 0.03278560 0.1375396
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6858961 0.08782985 0.6602035 0.2795690
## [2,] 0.5072984 0.90645445 0.1824252 0.7649262
## [3,] 0.3334659 0.15562552 0.1517339 0.6365426
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02140012 0.002740311 0.020598502 0.008722617
## [2,] 0.01582783 0.028281588 0.005691708 0.023865874
## [3,] 0.01040421 0.004855552 0.004734134 0.019860277
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12565709 0.01609055 0.12095018 0.05121742
## [2,] 0.09293775 0.16606368 0.03342054 0.14013552
## [3,] 0.06109140 0.02851081 0.02779786 0.11661547
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4727524 0.06053653 0.4550438 0.1926923
## [2,] 0.3496543 0.62477174 0.1257362 0.5272237
## [3,] 0.2298406 0.10726454 0.1045823 0.4387356
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2157494 0.02762698 0.20766778 0.08793875
## [2,] 0.1595713 0.28512630 0.05738206 0.24060843
## [3,] 0.1048921 0.04895219 0.04772809 0.20022523
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03422388 0.004382411 0.032941909 0.01394954
## [2,] 0.02531246 0.045228994 0.009102397 0.03816722
## [3,] 0.01663881 0.007765184 0.007571008 0.03176131
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4299386 0.05505418 0.41383383 0.1752416
## [2,] 0.3179887 0.56819073 0.11434916 0.4794769
## [3,] 0.2090256 0.09755038 0.09511103 0.3990025
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07347845 0.009409007 0.07072606 0.02994957
## [2,] 0.05434569 0.097106352 0.01954279 0.08194476
## [3,] 0.03572342 0.016671799 0.01625490 0.06819133
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7183312 0.0919832 0.6914237 0.2927894
## [2,] 0.5312878 0.9493195 0.1910518 0.8010986
## [3,] 0.3492350 0.1629848 0.1589092 0.6666439
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5469998 0.0700440 0.5265100 0.2229553
## [2,] 0.4045687 0.7228943 0.1454835 0.6100260
## [3,] 0.2659379 0.1241108 0.1210073 0.5076406
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2384349 0.03053189 0.22950356 0.0971853
## [2,] 0.1763498 0.31510666 0.06341565 0.2659078
## [3,] 0.1159212 0.05409939 0.05274658 0.2212784
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09650906 0.01235811 0.09289399 0.03933678
## [2,] 0.07137946 0.12754275 0.02566815 0.10762901
## [3,] 0.04692034 0.02189730 0.02134974 0.08956478
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2759823 0.03533988 0.26564449 0.1124895
## [2,] 0.2041204 0.36472788 0.07340199 0.3077815
## [3,] 0.1341759 0.06261866 0.06105282 0.2561241
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03823019 0.004895424 0.036798153 0.01558250
## [2,] 0.02827559 0.050523587 0.010167941 0.04263514
## [3,] 0.01858658 0.008674191 0.008457284 0.03547935
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6703145 0.08583461 0.6452056 0.2732180
## [2,] 0.4957740 0.88586245 0.1782810 0.7475493
## [3,] 0.3258905 0.15209016 0.1482870 0.6220822
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6186888 0.07922387 0.5955137 0.2521755
## [2,] 0.4575909 0.81763585 0.1645503 0.6899752
## [3,] 0.3007913 0.14037661 0.1368663 0.5741712
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4667543 0.05976848 0.4492705 0.1902475
## [2,] 0.3452181 0.61684499 0.1241409 0.5205346
## [3,] 0.2269245 0.10590363 0.1032554 0.4331692
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7061374 0.09042177 0.6796866 0.2878193
## [2,] 0.5222691 0.93320460 0.1878087 0.7874998
## [3,] 0.3433067 0.16021814 0.1562117 0.6553275
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4283788 0.05485444 0.41233246 0.1746058
## [2,] 0.3168350 0.56612937 0.11393431 0.4777374
## [3,] 0.2082673 0.09719647 0.09476598 0.3975550
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.17348004 0.02221434 0.16698176 0.0707099
## [2,] 0.12830828 0.22926470 0.04613984 0.1934687
## [3,] 0.08434175 0.03936153 0.03837726 0.1609973
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2570808 0.03291952 0.24745096 0.1047853
## [2,] 0.1901406 0.33974830 0.06837481 0.2867021
## [3,] 0.1249864 0.05833002 0.05687142 0.2385826
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2562723 0.03281599 0.24667279 0.1044558
## [2,] 0.1895426 0.33867988 0.06815979 0.2858005
## [3,] 0.1245933 0.05814659 0.05669257 0.2378323
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3498980 0.04480488 0.33679139 0.1426173
## [2,] 0.2587895 0.46241204 0.09306106 0.3902139
## [3,] 0.1701118 0.07938966 0.07740444 0.3247212
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6079691 0.07785119 0.5851955 0.2478062
## [2,] 0.4496625 0.80346904 0.1616992 0.6780203
## [3,] 0.2955797 0.13794436 0.1344949 0.5642228
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13777093 0.01764174 0.13261025 0.05615498
## [2,] 0.10189732 0.18207288 0.03664242 0.15364514
## [3,] 0.06698085 0.03125936 0.03047769 0.12785767
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5432774 0.06956735 0.5229271 0.2214381
## [2,] 0.4018156 0.71797496 0.1444934 0.6058747
## [3,] 0.2641282 0.12326623 0.1201838 0.5041860
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.15765616 0.02018807 0.15175062 0.06426014
## [2,] 0.11660471 0.20835246 0.04193122 0.17582158
## [3,] 0.07664856 0.03577119 0.03487670 0.14631206
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.16195082 0.02073800 0.15588441 0.06601063
## [2,] 0.11978110 0.21402812 0.04307345 0.18061108
## [3,] 0.07873652 0.03674563 0.03582676 0.15029770
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.17773998 0.02275983 0.17108213 0.07244624
## [2,] 0.13145899 0.23489448 0.04727284 0.19821949
## [3,] 0.08641283 0.04032809 0.03931964 0.16495076
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4243772 0.05434204 0.40848076 0.1729748
## [2,] 0.3138754 0.56084101 0.11287002 0.4732747
## [3,] 0.2063218 0.09628853 0.09388074 0.3938413
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.005246638 0.0006718386 0.005050108 0.002138513
## [2,] 0.003880487 0.0069337596 0.001395429 0.005851165
## [3,] 0.002550787 0.0011904293 0.001160661 0.004869118
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2171240 0.02780299 0.20899086 0.08849902
## [2,] 0.1605879 0.28694287 0.05774765 0.24214137
## [3,] 0.1055604 0.04926407 0.04803217 0.20150088
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5448145 0.06976417 0.5244066 0.2220646
## [2,] 0.4029524 0.72000630 0.1449023 0.6075889
## [3,] 0.2648754 0.12361498 0.1205239 0.5056125
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5503420 0.07047198 0.5297271 0.2243176
## [2,] 0.4070407 0.72731128 0.1463724 0.6137534
## [3,] 0.2675628 0.12486915 0.1217467 0.5107423
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7388050 0.0946049 0.7111305 0.3011345
## [2,] 0.5464305 0.9763769 0.1964972 0.8239314
## [3,] 0.3591889 0.1676302 0.1634384 0.6856445
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2948002 0.03774954 0.28375749 0.1201596
## [2,] 0.2180384 0.38959690 0.07840691 0.3287676
## [3,] 0.1433247 0.06688832 0.06521571 0.2735880
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12574436 0.01610172 0.12103418 0.05125299
## [2,] 0.09300229 0.16617901 0.03344375 0.14023284
## [3,] 0.06113383 0.02853061 0.02781717 0.11669646
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6019562 0.07708124 0.5794079 0.2453554
## [2,] 0.4452153 0.79552266 0.1601000 0.6713146
## [3,] 0.2926564 0.13658008 0.1331648 0.5586426
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10635960 0.01361948 0.10237554 0.04335183
## [2,] 0.07866506 0.14056085 0.02828806 0.11861454
## [3,] 0.05170943 0.02413233 0.02352887 0.09870653
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.16051696 0.02055440 0.15450425 0.06542619
## [2,] 0.11872060 0.21213317 0.04269209 0.17901200
## [3,] 0.07803941 0.03642029 0.03550956 0.14896701
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1953531 0.02501521 0.18803550 0.07962529
## [2,] 0.1444859 0.25817133 0.05195733 0.21786204
## [3,] 0.0949759 0.04432440 0.04321602 0.18129654
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.020237780 0.002591473 0.019479705 0.008248853
## [2,] 0.014968147 0.026745489 0.005382566 0.022569612
## [3,] 0.009839113 0.004591825 0.004477002 0.018781577
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08307752 0.01063818 0.07996557 0.03386213
## [2,] 0.06144530 0.10979213 0.02209582 0.09264986
## [3,] 0.04039026 0.01884977 0.01837841 0.07709970
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2851641 0.03651562 0.27448229 0.1162319
## [2,] 0.2109114 0.37686213 0.07584402 0.3180212
## [3,] 0.1386398 0.06470194 0.06308400 0.2646452
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6426138 0.0822875 0.6185425 0.2619273
## [2,] 0.4752862 0.8492543 0.1709136 0.7166569
## [3,] 0.3124231 0.1458050 0.1421590 0.5963747
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04097789 0.005247270 0.039442921 0.01670245
## [2,] 0.03030782 0.054154834 0.010898735 0.04569943
## [3,] 0.01992244 0.009297626 0.009065129 0.03802934
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2186744 0.02800152 0.21048317 0.08913095
## [2,] 0.1617346 0.28899181 0.05816000 0.24387040
## [3,] 0.1063141 0.04961584 0.04837515 0.20293971
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6778483 0.08679932 0.6524572 0.2762888
## [2,] 0.5013461 0.89581883 0.1802848 0.7559512
## [3,] 0.3295532 0.15379953 0.1499536 0.6290739
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4956504 0.06346865 0.4770841 0.2020255
## [2,] 0.3665900 0.65503293 0.1318263 0.5527601
## [3,] 0.2409731 0.11245997 0.1096478 0.4599860
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3289376 0.04212087 0.31661616 0.1340739
## [2,] 0.2432869 0.43471160 0.08748631 0.3668384
## [3,] 0.1599214 0.07463389 0.07276759 0.3052690
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.19299674 0.02471347 0.18576740 0.07866484
## [2,] 0.14274310 0.25505724 0.05133062 0.21523416
## [3,] 0.09383029 0.04378975 0.04269474 0.17910972
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5770945 0.07389767 0.5554775 0.2352218
## [2,] 0.4268272 0.76266644 0.1534877 0.6435884
## [3,] 0.2805692 0.13093913 0.1276649 0.5355699
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4309768 0.05518712 0.4148331 0.1756647
## [2,] 0.3187565 0.56956277 0.1146253 0.4806347
## [3,] 0.2095304 0.09778594 0.0953407 0.3999660
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6083352 0.07789808 0.5855479 0.2479554
## [2,] 0.4499332 0.80395287 0.1617966 0.6784286
## [3,] 0.2957577 0.13802743 0.1345759 0.5645626
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2085450 0.02670445 0.20073323 0.08500226
## [2,] 0.1542428 0.27560522 0.05546593 0.23257391
## [3,] 0.1013895 0.04731755 0.04613433 0.19353920
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3515210 0.0450127 0.33835355 0.1432788
## [2,] 0.2599898 0.4645569 0.09349271 0.3920238
## [3,] 0.1709009 0.0797579 0.07776347 0.3262274
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2656361 0.03401504 0.25568583 0.1082724
## [2,] 0.1964682 0.35105472 0.07065024 0.2962432
## [3,] 0.1291458 0.06027117 0.05876403 0.2465224
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.73547879 0.09417898 0.70792894 0.29977875
## [2,] 0.54397045 0.97198111 0.19561253 0.82022194
## [3,] 0.35757177 0.16687552 0.16270263 0.68255764
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.59284807 0.07591493 0.57064094 0.24164294
## [2,] 0.43847876 0.78348572 0.15767758 0.66115706
## [3,] 0.28822821 0.13451351 0.13114986 0.55018987
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.14820359 0.01897765 0.14265212 0.06040730
## [2,] 0.10961345 0.19586029 0.03941715 0.16527986
## [3,] 0.07205295 0.03362646 0.03278560 0.13753964
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.20854499 0.02670445 0.20073323 0.08500226
## [2,] 0.15424281 0.27560522 0.05546593 0.23257391
## [3,] 0.10138947 0.04731755 0.04613433 0.19353920
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.35152096 0.04501270 0.33835355 0.14327879
## [2,] 0.25998984 0.46455688 0.09349271 0.39202382
## [3,] 0.17090088 0.07975790 0.07776347 0.32622738
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.26563613 0.03401504 0.25568583 0.10827242
## [2,] 0.19646821 0.35105472 0.07065024 0.29624319
## [3,] 0.12914578 0.06027117 0.05876403 0.24652236
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] 0.6608599
DelayedTensor::einsum('i->', darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 0.6608599
einsum::einsum('ij->', arrC)
## [1] 5.77026
DelayedTensor::einsum('ij->', darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 5.77026
einsum::einsum('ijk->', arrE)
## [1] 27.79868
DelayedTensor::einsum('ijk->', darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 27.79868
einsum::einsum('ij->i', arrC)
## [1] 1.847419 2.545639 1.377202
DelayedTensor::einsum('ij->i', darrC)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 1.847419 2.545639 1.377202
einsum::einsum('ij->j', arrC)
## [1] 1.645978 1.239782 1.072078 1.812421
DelayedTensor::einsum('ij->j', darrC)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 1.645978 1.239782 1.072078 1.812421
einsum::einsum('ijk->i', arrE)
## [1] 9.462172 8.464878 9.871632
DelayedTensor::einsum('ijk->i', darrE)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 9.462172 8.464878 9.871632
einsum::einsum('ijk->j', arrE)
## [1] 8.240752 6.095755 7.127609 6.334566
DelayedTensor::einsum('ijk->j', darrE)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 8.240752 6.095755 7.127609 6.334566
einsum::einsum('ijk->k', arrE)
## [1] 5.752454 6.108144 4.846629 5.145308 5.946147
DelayedTensor::einsum('ijk->k', darrE)
## <5> array of class HDF5Array and type "double":
## [1] [2] [3] [4] [5]
## 5.752454 6.108144 4.846629 5.145308 5.946147
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 2.880408 2.327298 2.692353 1.562112
## [2,] 2.892024 2.074098 2.010233 1.488523
## [3,] 2.468320 1.694360 2.425022 3.283930
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 2.880408 2.327298 2.692353 1.562112
## [2,] 2.892024 2.074098 2.010233 1.488523
## [3,] 2.468320 1.694360 2.425022 3.283930
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.9966523 1.192617 1.641831 2.1419076 1.267744
## [2,] 1.1263689 1.331336 1.134146 1.1278657 1.376039
## [3,] 0.9773126 2.422682 1.033218 0.5085954 2.185801
## [4,] 1.6521207 1.161509 1.037433 1.3669388 1.116563
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.9966523 1.1926168 1.6418315 2.1419076 1.2677440
## [2,] 1.1263689 1.3313360 1.1341461 1.1278657 1.3760386
## [3,] 0.9773126 2.4226817 1.0332183 0.5085954 2.1858010
## [4,] 1.6521207 1.1615093 1.0374333 1.3669388 1.1165635
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.9966523 1.192617 1.641831 2.1419076 1.267744
## [2,] 1.1263689 1.331336 1.134146 1.1278657 1.376039
## [3,] 0.9773126 2.422682 1.033218 0.5085954 2.185801
## [4,] 1.6521207 1.161509 1.037433 1.3669388 1.116563
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.9966523 1.1926168 1.6418315 2.1419076 1.2677440
## [2,] 1.1263689 1.3313360 1.1341461 1.1278657 1.3760386
## [3,] 0.9773126 2.4226817 1.0332183 0.5085954 2.1858010
## [4,] 1.6521207 1.1615093 1.0374333 1.3669388 1.1165635
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 2.282526
DelayedTensor::einsum('ii->', darrB)
## <1> array of class HDF5Array and type "double":
## [1]
## 2.282526
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.8646593 0.7595464 0.3612860
## [2,] 0.1553289 0.4662422 0.9059080
## [3,] 0.7532351 0.8110918 0.9516241
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3]
## [1,] 0.8646593 0.7595464 0.3612860
## [2,] 0.1553289 0.4662422 0.9059080
## [3,] 0.7532351 0.8110918 0.9516241
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.30548565 0.6024460 0.71962048
## [2,] 0.07257162 0.2141188 0.02365518
## [3,] 0.16995124 0.4419451 0.73177391
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.954571417 0.3003874 0.9513093
## [2,] 0.006203289 0.1080123 0.5997033
## [3,] 0.838557994 0.5577925 0.1649271
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.8797507 0.7053575 0.36524230
## [2,] 0.4422525 0.6840923 0.08233647
## [3,] 0.8646246 0.4886988 0.51876627
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.30548565 0.60244597 0.71962048
## [2,] 0.07257162 0.21411882 0.02365518
## [3,] 0.16995124 0.44194510 0.73177391
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.954571417 0.300387441 0.951309252
## [2,] 0.006203289 0.108012284 0.599703346
## [3,] 0.838557994 0.557792491 0.164927106
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.87975066 0.70535748 0.36524230
## [2,] 0.44225249 0.68409230 0.08233647
## [3,] 0.86462461 0.48869878 0.51876627
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.1698301
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 0.1698301
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 3.781617
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 3.781617
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 18.36944
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 18.36944
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.6720079 0.6681249 1.0198691 1.7108732 0.9307164
## [2,] 0.8899844 0.9635826 0.6198728 0.7121967 0.7151988
## [3,] 0.4959432 2.0101230 0.4350538 0.1176490 1.6253576
## [4,] 1.2914454 0.5114506 0.6290278 0.9164477 0.4345143
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.6720079 0.6681249 1.0198691 1.7108732 0.9307164
## [2,] 0.8899844 0.9635826 0.6198728 0.7121967 0.7151988
## [3,] 0.4959432 2.0101230 0.4350538 0.1176490 1.6253576
## [4,] 1.2914454 0.5114506 0.6290278 0.9164477 0.4345143
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.1533479 0.8855961 0.6050683
## [2,] 0.8855961 1.9730955 0.9587905
## [3,] 0.6050683 0.9587905 0.6551734
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 1.1533479 0.8855961 0.6050683
## [2,] 0.8855961 1.9730955 0.9587905
## [3,] 0.6050683 0.9587905 0.6551734
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.546864765 0.29915085 0.12926058
## [2,] 0.008967008 0.95511416 0.02815302
## [3,] 0.506662739 0.03868414 0.02676263
## [4,] 0.090853429 0.68014639 0.47099717
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.546864765 0.299150854 0.129260577
## [2,] 0.008967008 0.955114160 0.028153015
## [3,] 0.506662739 0.038684143 0.026762631
## [4,] 0.090853429 0.680146386 0.470997174
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9891459 0.5471348 1.200946e-01 0.55384133 0.003070571
## [2,] 0.8602738 0.1392780 3.470845e-02 0.02891326 0.449232305
## [3,] 0.4086838 0.6999460 4.796080e-02 0.04711529 0.608995363
## [4,] 0.3380127 0.3355646 5.033642e-05 0.01262081 0.079527914
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.6426979046 0.103958468 0.22387366 0.99811289 0.08744113
## [2,] 0.0008374373 0.002672595 0.53971354 0.66259756 0.19785507
## [3,] 0.0851175830 0.398379329 0.05776840 0.06978478 0.33964710
## [4,] 0.0098727923 0.055032482 0.08620562 0.14869957 0.22595529
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.040164048 0.01703163 0.67590087 0.1589189521 0.84020467
## [2,] 0.028873142 0.82163193 0.04545085 0.0206858542 0.06811143
## [3,] 0.002141798 0.91179764 0.32932465 0.0007489379 0.67671515
## [4,] 0.943559964 0.12085352 0.54277184 0.7551273225 0.12903108
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,] 9.891459e-01 5.471348e-01 1.200946e-01 5.538413e-01 3.070571e-03
## [2,] 8.602738e-01 1.392780e-01 3.470845e-02 2.891326e-02 4.492323e-01
## [3,] 4.086838e-01 6.999460e-01 4.796080e-02 4.711529e-02 6.089954e-01
## [4,] 3.380127e-01 3.355646e-01 5.033642e-05 1.262081e-02 7.952791e-02
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.6426979046 0.1039584678 0.2238736559 0.9981128903 0.0874411334
## [2,] 0.0008374373 0.0026725945 0.5397135358 0.6625975560 0.1978550684
## [3,] 0.0851175830 0.3983793291 0.0577683993 0.0697847828 0.3396470997
## [4,] 0.0098727923 0.0550324818 0.0862056246 0.1486995668 0.2259552914
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0401640485 0.0170316320 0.6759008692 0.1589189521 0.8402046689
## [2,] 0.0288731423 0.8216319255 0.0454508451 0.0206858542 0.0681114331
## [3,] 0.0021417983 0.9117976372 0.3293246486 0.0007489379 0.6767151471
## [4,] 0.9435599640 0.1208535216 0.5427718353 0.7551273225 0.1290310832
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 3.1427400 1.221734 1.387980
## [2,] 2.5287925 1.239886 2.339465
## [3,] 0.7589431 1.741763 2.345923
## [4,] 1.2436471 2.462841 1.438820
## [5,] 1.7880492 1.798654 2.359444
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 3.1427400 1.2217340 1.3879804
## [2,] 2.5287925 1.2398861 2.3394653
## [3,] 0.7589431 1.7417634 2.3459226
## [4,] 1.2436471 2.4628409 1.4388195
## [5,] 1.7880492 1.7986535 2.3594444
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.186043798 0.1029080067 2.258803e-02 1.041694e-01 0.0005775293
## [2,] 0.002653134 0.0004295415 1.070429e-04 8.917015e-05 0.0013854582
## [3,] 0.071216614 0.1219715149 8.357574e-03 8.210238e-03 0.1061225900
## [4,] 0.010562074 0.0104855762 1.572891e-06 3.943695e-04 0.0024850539
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0255300715 0.0041295718 0.0088929969 0.0396483220 0.003473449
## [2,] 0.0001062093 0.0003389559 0.0684500067 0.0840349632 0.025093276
## [3,] 0.0004372272 0.0020463724 0.0002967415 0.0003584665 0.001744680
## [4,] 0.0008916559 0.0049702289 0.0077856145 0.0134297213 0.020407030
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 9.559827e-04 0.0004053861 0.0160877592 3.782581e-03 0.0199985101
## [2,] 1.496806e-04 0.0042594025 0.0002356206 1.072370e-04 0.0003530949
## [3,] 1.055489e-05 0.0044933860 0.0016229289 3.690805e-06 0.0033348873
## [4,] 8.183409e-02 0.0104815153 0.0470741043 6.549150e-02 0.0111907477
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,] 1.860438e-01 1.029080e-01 2.258803e-02 1.041694e-01 5.775293e-04
## [2,] 2.653134e-03 4.295415e-04 1.070429e-04 8.917015e-05 1.385458e-03
## [3,] 7.121661e-02 1.219715e-01 8.357574e-03 8.210238e-03 1.061226e-01
## [4,] 1.056207e-02 1.048558e-02 1.572891e-06 3.943695e-04 2.485054e-03
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0255300715 0.0041295718 0.0088929969 0.0396483220 0.0034734490
## [2,] 0.0001062093 0.0003389559 0.0684500067 0.0840349632 0.0250932761
## [3,] 0.0004372272 0.0020463724 0.0002967415 0.0003584665 0.0017446800
## [4,] 0.0008916559 0.0049702289 0.0077856145 0.0134297213 0.0204070304
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 9.559827e-04 4.053861e-04 1.608776e-02 3.782581e-03 1.999851e-02
## [2,] 1.496806e-04 4.259403e-03 2.356206e-04 1.072370e-04 3.530949e-04
## [3,] 1.055489e-05 4.493386e-03 1.622929e-03 3.690805e-06 3.334887e-03
## [4,] 8.183409e-02 1.048152e-02 4.707410e-02 6.549150e-02 1.119075e-02
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.2.0 RC (2022-04-19 r82224)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.4 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.15-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.15-bioc/R/lib/libRlapack.so
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_GB LC_COLLATE=C
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] einsum_0.1.0 DelayedRandomArray_1.4.0 HDF5Array_1.24.0
## [4] rhdf5_2.40.0 DelayedArray_0.22.0 IRanges_2.30.0
## [7] S4Vectors_0.34.0 MatrixGenerics_1.8.0 matrixStats_0.62.0
## [10] BiocGenerics_0.42.0 Matrix_1.4-1 DelayedTensor_1.2.0
## [13] BiocStyle_2.24.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.8.3 rTensor_1.4.8 bslib_0.3.1
## [4] compiler_4.2.0 BiocManager_1.30.17 jquerylib_0.1.4
## [7] rhdf5filters_1.8.0 tools_4.2.0 digest_0.6.29
## [10] jsonlite_1.8.0 evaluate_0.15 lattice_0.20-45
## [13] rlang_1.0.2 cli_3.3.0 parallel_4.2.0
## [16] yaml_2.3.5 xfun_0.30 fastmap_1.1.0
## [19] stringr_1.4.0 knitr_1.38 sass_0.4.1
## [22] grid_4.2.0 R6_2.5.1 BiocParallel_1.30.0
## [25] rmarkdown_2.14 bookdown_0.26 irlba_2.3.5
## [28] Rhdf5lib_1.18.0 magrittr_2.0.3 BiocSingular_1.12.0
## [31] htmltools_0.5.2 rsvd_1.0.5 beachmat_2.12.0
## [34] dqrng_0.3.0 ScaledMatrix_1.4.0 stringi_1.7.6