DelayedTensor 1.11.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2024-04-25 16:06:28
Compiled: Sat May 4 17:06:44 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.5297692 0.8904230 0.4293254
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.5297692 0.8904230 0.4293254
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.08414873 0.3198219 0.2274903 0.6693240
## [2,] 0.16154138 0.4363578 0.3053775 0.4946069
## [3,] 0.86468692 0.5537739 0.4038625 0.2718853
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.08414873 0.31982190 0.22749033 0.66932396
## [2,] 0.16154138 0.43635776 0.30537749 0.49460695
## [3,] 0.86468692 0.55377395 0.40386247 0.27188529
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5879217 0.7746069 0.2677536 0.4786478
## [2,] 0.9493104 0.1502176 0.4885496 0.6560296
## [3,] 0.7993188 0.3502208 0.7285244 0.8119422
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4047395 0.3284064 0.8699507 0.7531236
## [2,] 0.9532732 0.7916772 0.9672360 0.0763183
## [3,] 0.9513452 0.1359534 0.1285412 0.1147220
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4740022 0.9723829 0.06501217 0.9380292
## [2,] 0.4222298 0.2794762 0.44199717 0.8610284
## [3,] 0.4994733 0.6340269 0.32861339 0.7376828
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9436546 0.8459363 0.7232319 0.1032492
## [2,] 0.1078875 0.3090479 0.4096868 0.2297486
## [3,] 0.1386634 0.9487913 0.1054500 0.9780631
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9689923 0.8318141 0.2075107 0.8675058
## [2,] 0.2023131 0.3984964 0.8390842 0.1096414
## [3,] 0.6483086 0.8971876 0.8464659 0.7506030
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.5879217 0.7746069 0.2677536 0.4786478
## [2,] 0.9493104 0.1502176 0.4885496 0.6560296
## [3,] 0.7993188 0.3502208 0.7285244 0.8119422
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.4047395 0.3284064 0.8699507 0.7531236
## [2,] 0.9532732 0.7916772 0.9672360 0.0763183
## [3,] 0.9513452 0.1359534 0.1285412 0.1147220
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.47400217 0.97238291 0.06501217 0.93802917
## [2,] 0.42222977 0.27947620 0.44199717 0.86102839
## [3,] 0.49947328 0.63402695 0.32861339 0.73768277
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.9436546 0.8459363 0.7232319 0.1032492
## [2,] 0.1078875 0.3090479 0.4096868 0.2297486
## [3,] 0.1386634 0.9487913 0.1054500 0.9780631
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.9689923 0.8318141 0.2075107 0.8675058
## [2,] 0.2023131 0.3984964 0.8390842 0.1096414
## [3,] 0.6483086 0.8971876 0.8464659 0.7506030
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.2903135 0.9344012 0.5834216
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.2903135 0.9344012 0.5834216
einsum::einsum('iii->i', arrD)
## [1] 0.68302287 0.01395281 0.15950817
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.68302287 0.01395281 0.15950817
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.2806554 0.7928532 0.1843203
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.2806554 0.7928532 0.1843203
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.007081008 0.1022860 0.05175185 0.44799456
## [2,] 0.026095617 0.1904081 0.09325541 0.24463603
## [3,] 0.747683462 0.3066656 0.16310489 0.07392161
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.007081008 0.102286047 0.051751851 0.447994559
## [2,] 0.026095617 0.190408093 0.093255410 0.244636035
## [3,] 0.747683462 0.306665584 0.163104895 0.073921609
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3456519 0.60001580 0.07169199 0.2291037
## [2,] 0.9011903 0.02256533 0.23868075 0.4303748
## [3,] 0.6389106 0.12265461 0.53074786 0.6592502
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1638141 0.10785077 0.75681421 0.567195165
## [2,] 0.9087299 0.62675280 0.93554555 0.005824483
## [3,] 0.9050577 0.01848331 0.01652284 0.013161139
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2246781 0.94552852 0.004226583 0.8798987
## [2,] 0.1782780 0.07810695 0.195361499 0.7413699
## [3,] 0.2494736 0.40199017 0.107986759 0.5441759
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.89048395 0.71560830 0.52306433 0.01066040
## [2,] 0.01163971 0.09551063 0.16784330 0.05278443
## [3,] 0.01922754 0.90020485 0.01111969 0.95660743
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.93894612 0.6919148 0.0430607 0.75256634
## [2,] 0.04093059 0.1587994 0.7040622 0.01202124
## [3,] 0.42030399 0.8049456 0.7165045 0.56340483
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.34565195 0.60001580 0.07169199 0.22910370
## [2,] 0.90119027 0.02256533 0.23868075 0.43037480
## [3,] 0.63891056 0.12265461 0.53074786 0.65925018
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.163814082 0.107850766 0.756814214 0.567195165
## [2,] 0.908729885 0.626752795 0.935545545 0.005824483
## [3,] 0.905057681 0.018483314 0.016522840 0.013161139
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.224678059 0.945528520 0.004226583 0.879898718
## [2,] 0.178277976 0.078106949 0.195361499 0.741369884
## [3,] 0.249473561 0.401990169 0.107986759 0.544175871
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.89048395 0.71560830 0.52306433 0.01066040
## [2,] 0.01163971 0.09551063 0.16784330 0.05278443
## [3,] 0.01922754 0.90020485 0.01111969 0.95660743
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.93894612 0.69191475 0.04306070 0.75256634
## [2,] 0.04093059 0.15879941 0.70406224 0.01202124
## [3,] 0.42030399 0.80494560 0.71650449 0.56340483
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.2806554 0.4717187 0.2274433
## [2,] 0.4717187 0.7928532 0.3822812
## [3,] 0.2274433 0.3822812 0.1843203
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.2806554 0.4717187 0.2274433
## [2,] 0.4717187 0.7928532 0.3822812
## [3,] 0.2274433 0.3822812 0.1843203
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04947286 0.1880302 0.1337465 0.3935101
## [2,] 0.09497369 0.2565442 0.1795381 0.2907902
## [3,] 0.50836822 0.3255757 0.2374395 0.1598473
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07988326 0.3036103 0.2159589 0.6353962
## [2,] 0.15335291 0.4142390 0.2898980 0.4695355
## [3,] 0.82085630 0.5257034 0.3833908 0.2581035
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06726166 0.2556397 0.1818373 0.5350032
## [2,] 0.12912306 0.3487890 0.2440940 0.3953486
## [3,] 0.69116052 0.4426419 0.3228149 0.2173230
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06518218 0.2477362 0.1762156 0.5184629
## [2,] 0.12513106 0.3380057 0.2365475 0.3831259
## [3,] 0.66979243 0.4289571 0.3128346 0.2106042
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01264062 0.04804288 0.03417305 0.10054424
## [2,] 0.02426636 0.06554862 0.04587307 0.07429867
## [3,] 0.12989119 0.08318659 0.06066725 0.04084196
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02947063 0.1120083 0.07967184 0.23441117
## [2,] 0.05657515 0.1528216 0.10694955 0.17322164
## [3,] 0.30283134 0.1939432 0.14144104 0.09521988
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02253112 0.08563346 0.06091135 0.17921390
## [2,] 0.04325329 0.11683636 0.08176592 0.13243279
## [3,] 0.23152303 0.14827497 0.10813563 0.07279826
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04111083 0.1562489 0.1111403 0.3269980
## [2,] 0.07892098 0.2131824 0.1491921 0.2416400
## [3,] 0.42244248 0.2705461 0.1973069 0.1328295
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0613044 0.2329981 0.1657323 0.4876189
## [2,] 0.1176868 0.3178973 0.2224750 0.3603333
## [3,] 0.6299456 0.4034379 0.2942237 0.1980751
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04027760 0.1530820 0.1088877 0.3203704
## [2,] 0.07732142 0.2088617 0.1461683 0.2367425
## [3,] 0.41388047 0.2650627 0.1933079 0.1301373
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05520405 0.2098126 0.1492404 0.4390963
## [2,] 0.10597592 0.2862636 0.2003367 0.3244768
## [3,] 0.56726019 0.3632921 0.2649457 0.1783648
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0683239 0.2596769 0.1847090 0.5434524
## [2,] 0.1311623 0.3542973 0.2479489 0.4015923
## [3,] 0.7020758 0.4496325 0.3279130 0.2207551
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03405832 0.1294446 0.09207433 0.2709019
## [2,] 0.06538218 0.1766112 0.12359834 0.2001870
## [3,] 0.34997297 0.2241342 0.16345910 0.1100427
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08021673 0.3048777 0.2168604 0.6380486
## [2,] 0.15399308 0.4159682 0.2911082 0.4714956
## [3,] 0.82428290 0.5278979 0.3849913 0.2591810
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08005449 0.3042610 0.2164218 0.6367581
## [2,] 0.15368161 0.4151269 0.2905194 0.4705419
## [3,] 0.82261574 0.5268302 0.3842126 0.2586568
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02763498 0.1050316 0.07470928 0.21981027
## [2,] 0.05305122 0.1433027 0.10028792 0.16243209
## [3,] 0.28396872 0.1818629 0.13263102 0.08928887
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06661863 0.2531957 0.1800989 0.5298885
## [2,] 0.12788863 0.3454545 0.2417604 0.3915690
## [3,] 0.68455292 0.4384102 0.3197287 0.2152454
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01144030 0.04348086 0.03092807 0.09099684
## [2,] 0.02196209 0.05932430 0.04151709 0.06724347
## [3,] 0.11755709 0.07528743 0.05490646 0.03696372
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07320524 0.2782293 0.1979054 0.5822788
## [2,] 0.14053304 0.3796097 0.2656634 0.4302837
## [3,] 0.75223498 0.4817560 0.3513404 0.2365268
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08139168 0.3093433 0.2200368 0.6473942
## [2,] 0.15624864 0.4220609 0.2953721 0.4784017
## [3,] 0.83635634 0.5356301 0.3906303 0.2629772
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01081658 0.04111029 0.02924188 0.08603570
## [2,] 0.02076472 0.05608995 0.03925359 0.06357737
## [3,] 0.11114789 0.07118277 0.05191297 0.03494846
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06337439 0.2408654 0.1713283 0.5040837
## [2,] 0.12166063 0.3286313 0.2299870 0.3725002
## [3,] 0.65121613 0.4170602 0.3041584 0.2047632
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.006422088 0.02440826 0.01736167 0.05108167
## [2,] 0.012328563 0.03330208 0.02330589 0.03774756
## [3,] 0.065991434 0.04226309 0.03082210 0.02074982
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.009653711 0.03669061 0.02609815 0.07678619
## [2,] 0.018532351 0.05005984 0.03503352 0.05674230
## [3,] 0.099198618 0.06353006 0.04633191 0.03119123
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03988668 0.1515963 0.1078309 0.3172610
## [2,] 0.07657096 0.2068345 0.1447496 0.2344448
## [3,] 0.40986348 0.2624901 0.1914317 0.1288742
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03553010 0.1350383 0.09605319 0.2826085
## [2,] 0.06820758 0.1842432 0.12893947 0.2088378
## [3,] 0.36509655 0.2338198 0.17052276 0.1147981
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04203004 0.1597425 0.1136253 0.3343094
## [2,] 0.08068560 0.2179490 0.1525279 0.2470430
## [3,] 0.43188801 0.2765953 0.2017185 0.1357994
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08182478 0.3109893 0.2212077 0.6508392
## [2,] 0.15708008 0.4243068 0.2969438 0.4809473
## [3,] 0.84080678 0.5384803 0.3927090 0.2643766
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02351757 0.08938261 0.06357813 0.18706012
## [2,] 0.04514697 0.12195161 0.08534574 0.13823087
## [3,] 0.24165942 0.15476664 0.11286995 0.07598547
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05335256 0.2027757 0.1442350 0.4243694
## [2,] 0.10242159 0.2766626 0.1936176 0.3135941
## [3,] 0.54823480 0.3511076 0.2560597 0.1723826
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.005470691 0.02079232 0.01478964 0.04351420
## [2,] 0.010502156 0.02836857 0.01985325 0.03215547
## [3,] 0.056215175 0.03600205 0.02625598 0.01767585
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03719350 0.1413604 0.1005501 0.2958393
## [2,] 0.07140083 0.1928689 0.1349760 0.2186149
## [3,] 0.38218917 0.2447665 0.1785061 0.1201725
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02765240 0.1050978 0.07475637 0.21994881
## [2,] 0.05308466 0.1433930 0.10035113 0.16253447
## [3,] 0.28414770 0.1819775 0.13271461 0.08934514
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07893396 0.3000023 0.2133926 0.6278454
## [2,] 0.15153053 0.4093163 0.2864530 0.4639557
## [3,] 0.81110155 0.5194561 0.3788348 0.2550363
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07245444 0.2753757 0.1958756 0.5763069
## [2,] 0.13909171 0.3757164 0.2629387 0.4258706
## [3,] 0.74451998 0.4768151 0.3477371 0.2341009
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06207507 0.2359271 0.1678157 0.4937488
## [2,] 0.11916629 0.3218936 0.2252717 0.3648630
## [3,] 0.63786464 0.4085095 0.2979224 0.2005651
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07940733 0.3018014 0.2146723 0.6316106
## [2,] 0.15243926 0.4117710 0.2881709 0.4667381
## [3,] 0.81596576 0.5225713 0.3811067 0.2565658
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.009078595 0.03450478 0.02454336 0.07221168
## [2,] 0.017428294 0.04707754 0.03294641 0.05336190
## [3,] 0.093288899 0.05974528 0.04357171 0.02933302
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01166835 0.04434759 0.03154458 0.09281073
## [2,] 0.02239988 0.06050685 0.04234468 0.06858388
## [3,] 0.11990042 0.07678818 0.05600094 0.03770054
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07118447 0.2705490 0.1924423 0.5662055
## [2,] 0.13665372 0.3691309 0.2583299 0.4184060
## [3,] 0.73147009 0.4684575 0.3416419 0.2299976
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02600599 0.0988403 0.07030542 0.20685319
## [2,] 0.04992403 0.1348555 0.09437628 0.15285726
## [3,] 0.26722971 0.1711427 0.12481286 0.08402559
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07983958 0.3034442 0.2158408 0.6350487
## [2,] 0.15326905 0.4140124 0.2897395 0.4692787
## [3,] 0.82040738 0.5254159 0.3831812 0.2579624
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06085904 0.2313054 0.1645283 0.4840764
## [2,] 0.11683187 0.3155878 0.2208587 0.3577155
## [3,] 0.62536913 0.4005070 0.2920862 0.1966361
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03447463 0.1310268 0.09319979 0.2742132
## [2,] 0.06618138 0.1787700 0.12510914 0.2026340
## [3,] 0.35425085 0.2268739 0.16545714 0.1113878
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.008873479 0.03372520 0.02398884 0.07058018
## [2,] 0.017034530 0.04601390 0.03220204 0.05215628
## [3,] 0.091181193 0.05839544 0.04258728 0.02867029
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.008688289 0.03302136 0.02348820 0.06910717
## [2,] 0.016679020 0.04505359 0.03152998 0.05106778
## [3,] 0.089278241 0.05717672 0.04169848 0.02807194
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01933305 0.07347864 0.05226559 0.15377626
## [2,] 0.03711391 0.10025260 0.07016006 0.11363527
## [3,] 0.19866063 0.12722881 0.09278685 0.06246527
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08230276 0.3128060 0.2224999 0.6546411
## [2,] 0.15799766 0.4267854 0.2986785 0.4837568
## [3,] 0.84571837 0.5416259 0.3950030 0.2659210
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08153947 0.3099050 0.2204364 0.6485698
## [2,] 0.15653236 0.4228273 0.2959084 0.4792703
## [3,] 0.83787498 0.5366027 0.3913396 0.2634548
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01702439 0.06470416 0.04602427 0.13541300
## [2,] 0.03268194 0.08828089 0.06178187 0.10006547
## [3,] 0.17493749 0.11203572 0.08170667 0.05500596
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05455434 0.2073433 0.1474839 0.4339285
## [2,] 0.10472866 0.2828945 0.1979788 0.3206579
## [3,] 0.56058393 0.3590164 0.2618275 0.1762656
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0699961 0.2660324 0.1892297 0.5567531
## [2,] 0.1343724 0.3629686 0.2540173 0.4114211
## [3,] 0.7192588 0.4606370 0.3359385 0.2261580
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03353297 0.1274479 0.09065409 0.2667232
## [2,] 0.06437366 0.1738870 0.12169184 0.1970991
## [3,] 0.34457465 0.2206769 0.16093775 0.1083453
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07549719 0.2869402 0.2041015 0.6005092
## [2,] 0.14493292 0.3914948 0.2739809 0.4437552
## [3,] 0.77578638 0.4968391 0.3623404 0.2439321
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01746176 0.06636648 0.04720668 0.13889190
## [2,] 0.03352157 0.09054892 0.06336911 0.10263625
## [3,] 0.17943181 0.11491404 0.08380580 0.05641911
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07060786 0.2683575 0.1908835 0.5616191
## [2,] 0.13554681 0.3661409 0.2562374 0.4150169
## [3,] 0.72554510 0.4646630 0.3388746 0.2281346
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07122903 0.2707183 0.1925628 0.5665599
## [2,] 0.13673927 0.3693620 0.2584916 0.4186679
## [3,] 0.73192797 0.4687508 0.3418558 0.2301416
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07299951 0.2774474 0.1973492 0.5806424
## [2,] 0.14013809 0.3785429 0.2649167 0.4290744
## [3,] 0.75012093 0.4804021 0.3503530 0.2358621
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.009226185 0.03506573 0.02494236 0.07338563
## [2,] 0.017711625 0.04784288 0.03348202 0.05422941
## [3,] 0.094805497 0.06071656 0.04428005 0.02980989
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06316228 0.2400593 0.1707549 0.5023966
## [2,] 0.12125344 0.3275314 0.2292173 0.3712534
## [3,] 0.64903657 0.4156644 0.3031404 0.2040779
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.04947286 0.18803024 0.13374651 0.39351009
## [2,] 0.09497369 0.25654420 0.17953806 0.29079017
## [3,] 0.50836822 0.32557573 0.23743952 0.15984727
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.07988326 0.30361026 0.21595894 0.63539620
## [2,] 0.15335291 0.41423896 0.28989803 0.46953553
## [3,] 0.82085630 0.52570338 0.38339085 0.25810353
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.06726166 0.25563966 0.18183730 0.53500323
## [2,] 0.12912306 0.34878896 0.24409397 0.39534864
## [3,] 0.69116052 0.44264193 0.32281487 0.21732302
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.07299951 0.27744736 0.19734918 0.58064242
## [2,] 0.14013809 0.37854289 0.26491675 0.42907440
## [3,] 0.75012093 0.48040212 0.35035304 0.23586207
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.009226185 0.035065725 0.024942362 0.073385625
## [2,] 0.017711625 0.047842882 0.033482020 0.054229406
## [3,] 0.094805497 0.060716559 0.044280052 0.029809887
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.06316228 0.24005927 0.17075492 0.50239656
## [2,] 0.12125344 0.32753143 0.22921725 0.37125345
## [3,] 0.64903657 0.41566437 0.30314037 0.20407791
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.849518
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.849518
einsum::einsum('ij->', arrC)
## [1] 4.792877
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 4.792877
einsum::einsum('ijk->', arrE)
## [1] 33.58362
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 33.58362
einsum::einsum('ij->i', arrC)
## [1] 1.300785 1.397884 2.094209
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 1.300785 1.397884 2.094209
einsum::einsum('ij->j', arrC)
## [1] 1.1103770 1.3099536 0.9367303 1.4358162
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 1.1103770 1.3099536 0.9367303 1.4358162
einsum::einsum('ijk->i', arrE)
## [1] 12.40647 9.64325 11.53390
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 12.40647 9.64325 11.53390
einsum::einsum('ijk->j', arrE)
## [1] 9.051434 8.648242 7.417608 8.466335
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 9.051434 8.648242 7.417608 8.466335
einsum::einsum('ijk->k', arrE)
## [1] 7.043043 6.475287 6.653954 5.843411 7.567923
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 7.043043 6.475287 6.653954 5.843411 7.567923
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 3.379310 3.753147 2.133459 3.140556
## [2,] 2.635014 1.928915 3.146554 1.932766
## [3,] 3.037109 2.966180 2.137595 3.393013
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 3.379310 3.753147 2.133459 3.140556
## [2,] 2.635014 1.928915 3.146554 1.932766
## [3,] 3.037109 2.966180 2.137595 3.393013
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.336551 2.3093580 1.3957052 1.190205 1.819614
## [2,] 1.275045 1.2560370 1.8858861 2.103776 2.127498
## [3,] 1.484828 1.9657279 0.8356227 1.238369 1.893061
## [4,] 1.946620 0.9441639 2.5367403 1.311061 1.727750
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.3365509 2.3093580 1.3957052 1.1902055 1.8196140
## [2,] 1.2750453 1.2560370 1.8858861 2.1037755 2.1274982
## [3,] 1.4848277 1.9657279 0.8356227 1.2383686 1.8930608
## [4,] 1.9466196 0.9441639 2.5367403 1.3110609 1.7277502
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.336551 2.3093580 1.3957052 1.190205 1.819614
## [2,] 1.275045 1.2560370 1.8858861 2.103776 2.127498
## [3,] 1.484828 1.9657279 0.8356227 1.238369 1.893061
## [4,] 1.946620 0.9441639 2.5367403 1.311061 1.727750
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.3365509 2.3093580 1.3957052 1.1902055 1.8196140
## [2,] 1.2750453 1.2560370 1.8858861 2.1037755 2.1274982
## [3,] 1.4848277 1.9657279 0.8356227 1.2383686 1.8930608
## [4,] 1.9466196 0.9441639 2.5367403 1.3110609 1.7277502
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.808136
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 1.808136
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.2903135 0.05294755 0.0469091
## [2,] 0.6403329 0.93440118 0.3607878
## [3,] 0.8294876 0.13990300 0.5834216
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.29031350 0.05294755 0.04690910
## [2,] 0.64033291 0.93440118 0.36078777
## [3,] 0.82948757 0.13990300 0.58342156
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.68302287 0.6556539 0.4518120
## [2,] 0.86103396 0.3226895 0.9910673
## [3,] 0.06131295 0.9523142 0.5801011
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.90131732 0.59349487 0.03689624
## [2,] 0.03249339 0.01395281 0.47570693
## [3,] 0.06919740 0.43676336 0.50904155
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.8506731 0.004105509 0.6190427
## [2,] 0.1904720 0.073721135 0.4175732
## [3,] 0.5869825 0.266135006 0.1595082
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.68302287 0.65565390 0.45181202
## [2,] 0.86103396 0.32268955 0.99106729
## [3,] 0.06131295 0.95231421 0.58010105
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.90131732 0.59349487 0.03689624
## [2,] 0.03249339 0.01395281 0.47570693
## [3,] 0.06919740 0.43676336 0.50904155
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.850673085 0.004105509 0.619042671
## [2,] 0.190472010 0.073721135 0.417573216
## [3,] 0.586982473 0.266135006 0.159508167
Some examples of combining Multiplication and Summation are shown below.
Inner Product first calculate Hadamard Product and collapses it to 0D tensor (norm).
einsum::einsum('i,i->', arrA, arrA)
## [1] 1.257829
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.257829
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 2.454884
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 2.454884
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 24.56988
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 24.56988
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.8857528 1.9776016 0.6524296 0.9213512 1.400181
## [2,] 0.7452357 0.7530869 1.4256256 1.7113238 1.655660
## [3,] 0.8411206 1.7088826 0.3075748 0.7020273 1.463627
## [4,] 1.3187287 0.5861808 2.1654445 1.0200523 1.327992
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.8857528 1.9776016 0.6524296 0.9213512 1.4001807
## [2,] 0.7452357 0.7530869 1.4256256 1.7113238 1.6556598
## [3,] 0.8411206 1.7088826 0.3075748 0.7020273 1.4636274
## [4,] 1.3187287 0.5861808 2.1654445 1.0200523 1.3279924
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 0.6091135 0.5536730 0.5237255
## [2,] 0.5536730 0.5543952 0.6391331
## [3,] 0.5237255 0.6391331 1.2913755
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.6091135 0.5536730 0.5237255
## [2,] 0.5536730 0.5543952 0.6391331
## [3,] 0.5237255 0.6391331 1.2913755
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.007081008 0.02609562 0.74768346
## [2,] 0.102286047 0.19040809 0.30666558
## [3,] 0.051751851 0.09325541 0.16310489
## [4,] 0.447994559 0.24463603 0.07392161
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.007081008 0.026095617 0.747683462
## [2,] 0.102286047 0.190408093 0.306665584
## [3,] 0.051751851 0.093255410 0.163104895
## [4,] 0.447994559 0.244636035 0.073921609
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.34565195 0.1638141 0.224678059 0.8904839 0.9389461
## [2,] 0.60001580 0.1078508 0.945528520 0.7156083 0.6919148
## [3,] 0.07169199 0.7568142 0.004226583 0.5230643 0.0430607
## [4,] 0.22910370 0.5671952 0.879898718 0.0106604 0.7525663
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.90119027 0.908729885 0.17827798 0.01163971 0.04093059
## [2,] 0.02256533 0.626752795 0.07810695 0.09551063 0.15879941
## [3,] 0.23868075 0.935545545 0.19536150 0.16784330 0.70406224
## [4,] 0.43037480 0.005824483 0.74136988 0.05278443 0.01202124
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.6389106 0.90505768 0.2494736 0.01922754 0.4203040
## [2,] 0.1226546 0.01848331 0.4019902 0.90020485 0.8049456
## [3,] 0.5307479 0.01652284 0.1079868 0.01111969 0.7165045
## [4,] 0.6592502 0.01316114 0.5441759 0.95660743 0.5634048
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.345651949 0.163814082 0.224678059 0.890483948 0.938946119
## [2,] 0.600015805 0.107850766 0.945528520 0.715608299 0.691914754
## [3,] 0.071691989 0.756814214 0.004226583 0.523064328 0.043060703
## [4,] 0.229103696 0.567195165 0.879898718 0.010660399 0.752566336
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.901190266 0.908729885 0.178277976 0.011639710 0.040930590
## [2,] 0.022565328 0.626752795 0.078106949 0.095510627 0.158799406
## [3,] 0.238680746 0.935545545 0.195361499 0.167843303 0.704062241
## [4,] 0.430374800 0.005824483 0.741369884 0.052784433 0.012021240
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.63891056 0.90505768 0.24947356 0.01922754 0.42030399
## [2,] 0.12265461 0.01848331 0.40199017 0.90020485 0.80494560
## [3,] 0.53074786 0.01652284 0.10798676 0.01111969 0.71650449
## [4,] 0.65925018 0.01316114 0.54417587 0.95660743 0.56340483
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 2.108930 2.244107 2.690006
## [2,] 2.356220 2.788505 1.330562
## [3,] 2.449426 2.004732 2.199796
## [4,] 2.616072 1.056371 2.170968
## [5,] 2.875823 1.549535 3.142565
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 2.108930 2.244107 2.690006
## [2,] 2.356220 2.788505 1.330562
## [3,] 2.449426 2.004732 2.199796
## [4,] 2.616072 1.056371 2.170968
## [5,] 2.875823 1.549535 3.142565
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.001296644 0.0006145157 0.0008428347 0.003340472 0.003522268
## [2,] 0.032513652 0.0058442165 0.0512362922 0.038777377 0.037493471
## [3,] 0.001965546 0.0207492227 0.0001158782 0.014340611 0.001180575
## [4,] 0.054374028 0.1346145262 0.2088296169 0.002530072 0.178609352
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.02094018 0.021115373 0.004142492 0.0002704619 0.0009510689
## [2,] 0.00382581 0.106262020 0.013242545 0.0161932299 0.0269234470
## [3,] 0.01981928 0.077684675 0.016222187 0.0139371649 0.0584630507
## [4,] 0.09374835 0.001268744 0.161492275 0.0114980098 0.0026185814
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.20508997 0.2905230554 0.080080886 0.0061720296 0.13491737
## [2,] 0.01614862 0.0024335006 0.052925752 0.1185203581 0.10597859
## [3,] 0.03716566 0.0011570131 0.007561781 0.0007786572 0.05017328
## [4,] 0.02092224 0.0004176875 0.017270196 0.0303592982 0.01788045
DelayedTensor::einsum('i,ij,ijk,ijk,ji->jki',
darrA, darrC, darrE, darrE, t(darrC))
## <4 x 5 x 3> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0012966440 0.0006145157 0.0008428347 0.0033404721 0.0035222683
## [2,] 0.0325136518 0.0058442165 0.0512362922 0.0387773770 0.0374934714
## [3,] 0.0019655458 0.0207492227 0.0001158782 0.0143406110 0.0011805752
## [4,] 0.0543740275 0.1346145262 0.2088296169 0.0025300720 0.1786093517
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0209401818 0.0211153735 0.0041424917 0.0002704619 0.0009510689
## [2,] 0.0038258103 0.1062620198 0.0132425450 0.0161932299 0.0269234470
## [3,] 0.0198192770 0.0776846754 0.0162221868 0.0139371649 0.0584630507
## [4,] 0.0937483530 0.0012687445 0.1614922752 0.0114980098 0.0026185814
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.2050899664 0.2905230554 0.0800808862 0.0061720296 0.1349173684
## [2,] 0.0161486221 0.0024335006 0.0529257524 0.1185203581 0.1059785909
## [3,] 0.0371656570 0.0011570131 0.0075617805 0.0007786572 0.0501732786
## [4,] 0.0209222427 0.0004176875 0.0172701958 0.0303592982 0.0178804542
einsum
By using einsum
and other DelayedTensor functions,
it is possible to implement your original tensor calculation functions.
It is intended to be applied to Delayed Arrays,
which can scale to large-scale data
since the calculation is performed internally by block processing.
For example, kronecker
can be easily implmented by eimsum
and other DelayedTensor functions4 https://stackoverflow.com/
questions/56067643/speeding-up-kronecker-products-numpy
(the kronecker
function inside DelayedTensor
has a more efficient implementation though).
darr1 <- DelayedArray(array(1:6, dim=c(2,3)))
darr2 <- DelayedArray(array(20:1, dim=c(4,5)))
mykronecker <- function(darr1, darr2){
stopifnot((length(dim(darr1)) == 2) && (length(dim(darr2)) == 2))
# Outer Product
tmpdarr <- DelayedTensor::einsum('ij,kl->ikjl', darr1, darr2)
# Reshape
DelayedTensor::unfold(tmpdarr, row_idx=c(2,1), col_idx=c(4,3))
}
identical(as.array(DelayedTensor::kronecker(darr1, darr2)),
as.array(mykronecker(darr1, darr2)))
## [1] TRUE
## R version 4.4.0 Patched (2024-04-24 r86482)
## Platform: aarch64-apple-darwin20
## Running under: macOS Ventura 13.6.6
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/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.13.0
## [3] HDF5Array_1.33.0 rhdf5_2.49.0
## [5] DelayedArray_0.31.0 SparseArray_1.5.0
## [7] S4Arrays_1.5.0 abind_1.4-5
## [9] IRanges_2.39.0 S4Vectors_0.43.0
## [11] MatrixGenerics_1.17.0 matrixStats_1.3.0
## [13] BiocGenerics_0.51.0 Matrix_1.7-0
## [15] DelayedTensor_1.11.0 BiocStyle_2.33.0
##
## loaded via a namespace (and not attached):
## [1] jsonlite_1.8.8 compiler_4.4.0 BiocManager_1.30.22
## [4] crayon_1.5.2 rsvd_1.0.5 Rcpp_1.0.12
## [7] rhdf5filters_1.17.0 parallel_4.4.0 jquerylib_0.1.4
## [10] BiocParallel_1.39.0 yaml_2.3.8 fastmap_1.1.1
## [13] lattice_0.22-6 R6_2.5.1 XVector_0.45.0
## [16] ScaledMatrix_1.13.0 knitr_1.46 bookdown_0.39
## [19] bslib_0.7.0 rlang_1.1.3 cachem_1.0.8
## [22] xfun_0.43 sass_0.4.9 cli_3.6.2
## [25] Rhdf5lib_1.27.0 BiocSingular_1.21.0 zlibbioc_1.51.0
## [28] digest_0.6.35 grid_4.4.0 irlba_2.3.5.1
## [31] rTensor_1.4.8 dqrng_0.3.2 lifecycle_1.0.4
## [34] evaluate_0.23 codetools_0.2-20 beachmat_2.21.0
## [37] rmarkdown_2.26 tools_4.4.0 htmltools_0.5.8.1