DelayedTensor 1.7.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2023-04-20 00:28:48.691296
Compiled: Thu May 4 19:41:48 2023
einsumeinsum is an easy and intuitive way to write tensor operations.
It was originally introduced by
Numpy1 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)
DelayedTensorCRAN 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.7629037 0.8052313 0.6102073
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.7629037 0.8052313 0.6102073
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.5407705 0.38475734 0.7660807 0.1596132
## [2,] 0.3663000 0.48239144 0.2501214 0.6163203
## [3,] 0.2799111 0.04327178 0.5936759 0.1476310
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.54077046 0.38475734 0.76608072 0.15961321
## [2,] 0.36630003 0.48239144 0.25012145 0.61632034
## [3,] 0.27991108 0.04327178 0.59367586 0.14763097
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.23866648 0.8956061 0.8916271 0.9263637
## [2,] 0.01639718 0.1200780 0.3193298 0.1470335
## [3,] 0.64283765 0.5536339 0.7254743 0.3531191
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.741077701 0.87576193 0.6735187 0.02562401
## [2,] 0.003276308 0.67861995 0.1323738 0.96190742
## [3,] 0.638283605 0.02261132 0.5729640 0.76031435
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4143817 0.5797431 0.4555152 0.8273029
## [2,] 0.6706370 0.8666476 0.6804327 0.3932990
## [3,] 0.7620352 0.2978296 0.3294040 0.3756991
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1757708 0.5863138 0.8957017 0.6885077
## [2,] 0.6324456 0.1339694 0.6420712 0.5852667
## [3,] 0.3152854 0.5431674 0.8758200 0.6065266
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.85914161 0.4999471 0.06013815 0.8033230
## [2,] 0.17343594 0.5342321 0.06013114 0.6949637
## [3,] 0.08396835 0.1351800 0.29305887 0.9089445
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.23866648 0.89560606 0.89162713 0.92636371
## [2,] 0.01639718 0.12007800 0.31932984 0.14703352
## [3,] 0.64283765 0.55363385 0.72547427 0.35311906
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.741077701 0.875761934 0.673518745 0.025624012
## [2,] 0.003276308 0.678619948 0.132373850 0.961907419
## [3,] 0.638283605 0.022611323 0.572964045 0.760314351
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.4143817 0.5797431 0.4555152 0.8273029
## [2,] 0.6706370 0.8666476 0.6804327 0.3932990
## [3,] 0.7620352 0.2978296 0.3294040 0.3756991
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.1757708 0.5863138 0.8957017 0.6885077
## [2,] 0.6324456 0.1339694 0.6420712 0.5852667
## [3,] 0.3152854 0.5431674 0.8758200 0.6065266
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.85914161 0.49994708 0.06013815 0.80332297
## [2,] 0.17343594 0.53423208 0.06013114 0.69496372
## [3,] 0.08396835 0.13518001 0.29305887 0.90894451
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.5020048 0.1422812 0.7374094
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.5020048 0.1422812 0.7374094
einsum::einsum('iii->i', arrD)
## [1] 0.07921062 0.80739563 0.07530616
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.07921062 0.80739563 0.07530616
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.5820220 0.6483975 0.3723530
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.5820220 0.6483975 0.3723530
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.29243269 0.148038210 0.58687967 0.02547638
## [2,] 0.13417571 0.232701506 0.06256074 0.37985077
## [3,] 0.07835021 0.001872447 0.35245103 0.02179490
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.292432688 0.148038210 0.586879671 0.025476377
## [2,] 0.134175708 0.232701506 0.062560738 0.379850766
## [3,] 0.078350212 0.001872447 0.352451027 0.021794904
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0569616884 0.80211021 0.7949989 0.85814972
## [2,] 0.0002688675 0.01441873 0.1019715 0.02161886
## [3,] 0.4132402414 0.30651044 0.5263129 0.12469307
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5491961594 0.7669589644 0.45362750 0.00065659
## [2,] 0.0000107342 0.4605250341 0.01752284 0.92526588
## [3,] 0.4074059610 0.0005112719 0.32828780 0.57807791
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1717122 0.33610204 0.2074941 0.6844301
## [2,] 0.4497540 0.75107808 0.4629887 0.1546841
## [3,] 0.5806976 0.08870248 0.1085070 0.1411498
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03089536 0.34376385 0.8022815 0.4740428
## [2,] 0.39998745 0.01794779 0.4122554 0.3425372
## [3,] 0.09940488 0.29503078 0.7670607 0.3678746
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.738124308 0.24994708 0.003616597 0.6453278
## [2,] 0.030080025 0.28540392 0.003615754 0.4829746
## [3,] 0.007050684 0.01827363 0.085883504 0.8261801
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.0569616884 0.8021102135 0.7949989379 0.8581497246
## [2,] 0.0002688675 0.0144187251 0.1019715446 0.0216188551
## [3,] 0.4132402414 0.3065104446 0.5263129098 0.1246930731
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.5491961594 0.7669589644 0.4536274995 0.0006565900
## [2,] 0.0000107342 0.4605250341 0.0175228361 0.9252658830
## [3,] 0.4074059610 0.0005112719 0.3282877968 0.5780779119
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.17171219 0.33610204 0.20749409 0.68443005
## [2,] 0.44975400 0.75107808 0.46298867 0.15468408
## [3,] 0.58069764 0.08870248 0.10850702 0.14114983
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.03089536 0.34376385 0.80228150 0.47404285
## [2,] 0.39998745 0.01794779 0.41225541 0.34253716
## [3,] 0.09940488 0.29503078 0.76706069 0.36787457
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.738124308 0.249947084 0.003616597 0.645327791
## [2,] 0.030080025 0.285403920 0.003615754 0.482974575
## [3,] 0.007050684 0.018273635 0.085883504 0.826180127
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.5820220 0.6143139 0.4655294
## [2,] 0.6143139 0.6483975 0.4913581
## [3,] 0.4655294 0.4913581 0.3723530
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.5820220 0.6143139 0.4655294
## [2,] 0.6143139 0.6483975 0.4913581
## [3,] 0.4655294 0.4913581 0.3723530
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12906378 0.09182868 0.1828378 0.03809432
## [2,] 0.08742354 0.11513067 0.0596956 0.14709501
## [3,] 0.06680539 0.01032752 0.1416905 0.03523456
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.008867111 0.0063089354 0.012561564 0.002617207
## [2,] 0.006006287 0.0079098594 0.004101286 0.010105916
## [3,] 0.004589752 0.0007095352 0.009734610 0.002420732
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3476276 0.24733650 0.4924655 0.10260538
## [2,] 0.2354714 0.31009938 0.1607875 0.39619392
## [3,] 0.1799374 0.02781673 0.3816372 0.09490275
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4843173 0.34459100 0.6861065 0.1429506
## [2,] 0.3280605 0.43203270 0.2240103 0.5519802
## [3,] 0.2506901 0.03875447 0.5316997 0.1322192
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06493463 0.046200890 0.09198944 0.01916603
## [2,] 0.04398457 0.057924598 0.03003408 0.07400651
## [3,] 0.03361116 0.005195989 0.07128741 0.01772723
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2993888 0.21301469 0.4241282 0.08836728
## [2,] 0.2027961 0.26706823 0.1384757 0.34121581
## [3,] 0.1549682 0.02395672 0.3286791 0.08173350
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4821656 0.34306008 0.6830584 0.1423155
## [2,] 0.3266030 0.43011330 0.2230151 0.5495279
## [3,] 0.2495763 0.03858229 0.5293375 0.1316318
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.17268414 0.12286450 0.24463243 0.05096926
## [2,] 0.11697053 0.15404198 0.07987124 0.19680947
## [3,] 0.08938396 0.01381797 0.18957842 0.04714297
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3923151 0.27913155 0.5557718 0.1157953
## [2,] 0.2657412 0.34996258 0.1814567 0.4471245
## [3,] 0.2030683 0.03139256 0.4306966 0.1071025
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5009501 0.35642524 0.7096694 0.1478599
## [2,] 0.3393271 0.44686993 0.2317034 0.5709368
## [3,] 0.2592995 0.04008541 0.5499598 0.1367600
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07951138 0.056572225 0.11263954 0.02346849
## [2,] 0.05385838 0.070927711 0.03677624 0.09061975
## [3,] 0.04115631 0.006362402 0.08729025 0.02170670
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.19095636 0.13586515 0.27051771 0.05636247
## [2,] 0.12934752 0.17034162 0.08832265 0.21763446
## [3,] 0.09884194 0.01528009 0.20963826 0.05213131
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4007529 0.28513508 0.5677253 0.1182858
## [2,] 0.2714568 0.35748954 0.1853594 0.4567413
## [3,] 0.2074359 0.03206775 0.4399599 0.1094060
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.001771731 0.0012605837 0.002509917 0.0005229421
## [2,] 0.001200112 0.0015804631 0.000819475 0.0020192555
## [3,] 0.000917075 0.0001417717 0.001945065 0.0004836846
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3451649 0.24558430 0.4889768 0.10187850
## [2,] 0.2338033 0.30790255 0.1596484 0.39338717
## [3,] 0.1786627 0.02761967 0.3789336 0.09423043
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4735862 0.33695583 0.6709043 0.1397832
## [2,] 0.3207916 0.42246006 0.2190468 0.5397499
## [3,] 0.2451355 0.03789578 0.5199187 0.1292896
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3669776 0.26110401 0.5198777 0.1083167
## [2,] 0.2485785 0.32736046 0.1697374 0.4182473
## [3,] 0.1899532 0.02936509 0.4028803 0.1001853
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.012227535 0.0086998723 0.017322098 0.003609066
## [2,] 0.008282528 0.0109075086 0.005655577 0.013935818
## [3,] 0.006329160 0.0009784322 0.013423796 0.003338132
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3642190 0.25914128 0.5159697 0.10750249
## [2,] 0.2467099 0.32489968 0.1684615 0.41510330
## [3,] 0.1885254 0.02914436 0.3998518 0.09943223
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07158387 0.050931810 0.10140905 0.02112862
## [2,] 0.04848854 0.063856013 0.03310954 0.08158470
## [3,] 0.03705291 0.005728052 0.07858716 0.01954248
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3098420 0.22045212 0.4389367 0.09145263
## [2,] 0.2098767 0.27639295 0.1433106 0.35312940
## [3,] 0.1603790 0.02479317 0.3401549 0.08458724
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.013856709 0.009859027 0.019630062 0.004089931
## [2,] 0.009386076 0.012360804 0.006409115 0.015792600
## [3,] 0.007172445 0.001108797 0.015212358 0.003782898
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5201711 0.37010094 0.7368987 0.1535331
## [2,] 0.3523467 0.46401591 0.2405937 0.5928431
## [3,] 0.2692485 0.04162345 0.5710612 0.1420073
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4111555 0.29253653 0.5824622 0.1213562
## [2,] 0.2785032 0.36676914 0.1901709 0.4685972
## [3,] 0.2128204 0.03290016 0.4513803 0.1122459
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2240854 0.15943640 0.3174498 0.06614079
## [2,] 0.1517880 0.19989419 0.1036457 0.25539187
## [3,] 0.1159900 0.01793103 0.2460084 0.06117557
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3626607 0.25803251 0.5137621 0.10704253
## [2,] 0.2456544 0.32350956 0.1677407 0.41332724
## [3,] 0.1877187 0.02901966 0.3981410 0.09900679
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4120861 0.29319864 0.5837805 0.1216309
## [2,] 0.2791335 0.36759926 0.1906013 0.4696578
## [3,] 0.2133021 0.03297462 0.4524019 0.1125000
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3135079 0.22306041 0.4441300 0.09253465
## [2,] 0.2123599 0.27966310 0.1450062 0.35730745
## [3,] 0.1622765 0.02508652 0.3441795 0.08558803
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4686574 0.33344903 0.6639220 0.1383284
## [2,] 0.3174530 0.41806339 0.2167672 0.5341326
## [3,] 0.2425843 0.03750139 0.5145078 0.1279440
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.16105746 0.11459213 0.22816153 0.04753754
## [2,] 0.10909500 0.14367046 0.07449357 0.18355845
## [3,] 0.08336581 0.01288762 0.17681425 0.04396888
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2463292 0.17526282 0.3489614 0.07270624
## [2,] 0.1668552 0.21973663 0.1139341 0.28074328
## [3,] 0.1275037 0.01971095 0.2704284 0.06724815
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3679579 0.26180148 0.5212664 0.1086060
## [2,] 0.2492425 0.32823492 0.1701908 0.4193645
## [3,] 0.1904607 0.02944354 0.4039565 0.1004529
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.17813198 0.1267406 0.25235009 0.05257724
## [2,] 0.12066071 0.1589017 0.08239102 0.20301841
## [3,] 0.09220384 0.0142539 0.19555923 0.04863024
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4473810 0.31831085 0.6337808 0.1320485
## [2,] 0.3030411 0.39908383 0.2069262 0.5098836
## [3,] 0.2315712 0.03579887 0.4911497 0.1221355
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2126845 0.15132467 0.30129876 0.06277571
## [2,] 0.1440654 0.18972406 0.09837251 0.24239816
## [3,] 0.1100887 0.01701875 0.23349211 0.05806311
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2031670 0.14455300 0.28781586 0.05996654
## [2,] 0.1376186 0.18123404 0.09397041 0.23155102
## [3,] 0.1051623 0.01625717 0.22304350 0.05546483
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09505163 0.067629087 0.13465458 0.02805533
## [2,] 0.06438483 0.084790307 0.04396403 0.10833109
## [3,] 0.04920018 0.007605913 0.10435085 0.02594921
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3420079 0.24333809 0.4845044 0.10094667
## [2,] 0.2316648 0.30508635 0.1581882 0.38978910
## [3,] 0.1770285 0.02736705 0.3754677 0.09336856
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.17049703 0.12130837 0.24153406 0.05032371
## [2,] 0.11548905 0.15209098 0.07885964 0.19431680
## [3,] 0.08825187 0.01364296 0.18717733 0.04654589
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3170612 0.22558853 0.4491637 0.09358343
## [2,] 0.2147668 0.28283275 0.1466497 0.36135711
## [3,] 0.1641157 0.02537084 0.3480803 0.08655807
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07244667 0.051545694 0.10263134 0.02138328
## [2,] 0.04907298 0.064625672 0.03350861 0.08256804
## [3,] 0.03749951 0.005797093 0.07953437 0.01977803
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2937289 0.20898763 0.4161100 0.08669669
## [2,] 0.1989622 0.26201929 0.1358578 0.33476509
## [3,] 0.1520386 0.02350382 0.3224653 0.08018833
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4843690 0.34462780 0.6861798 0.1429658
## [2,] 0.3280955 0.43207883 0.2240342 0.5520392
## [3,] 0.2507168 0.03875861 0.5317565 0.1322333
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3472131 0.24704160 0.4918784 0.10248304
## [2,] 0.2351907 0.30972965 0.1605958 0.39572154
## [3,] 0.1797228 0.02778356 0.3811822 0.09478959
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4736176 0.33697818 0.6709488 0.1397924
## [2,] 0.3208129 0.42248808 0.2190614 0.5397857
## [3,] 0.2451517 0.03789829 0.5199532 0.1292982
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3723246 0.26490839 0.5274525 0.1098949
## [2,] 0.2522004 0.33213022 0.1722105 0.4243413
## [3,] 0.1927209 0.02979295 0.4087504 0.1016451
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3164950 0.22518567 0.4483616 0.0934163
## [2,] 0.2143832 0.28232767 0.1463878 0.3607118
## [3,] 0.1638226 0.02532553 0.3474587 0.0864035
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3279917 0.23336558 0.4646484 0.09680966
## [2,] 0.2221707 0.29258326 0.1517053 0.37381471
## [3,] 0.1697735 0.02624549 0.3600802 0.08954212
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4645984 0.33056104 0.6581718 0.1371304
## [2,] 0.3147036 0.41444256 0.2148897 0.5295065
## [3,] 0.2404833 0.03717659 0.5100516 0.1268359
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09378903 0.066730751 0.13286593 0.02768267
## [2,] 0.06352959 0.083664013 0.04338005 0.10689210
## [3,] 0.04854664 0.007504882 0.10296473 0.02560452
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04540760 0.03230744 0.06432653 0.01340246
## [2,] 0.03075761 0.04050561 0.02100229 0.05175140
## [3,] 0.02350367 0.00363346 0.04984998 0.01239633
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2703566 0.1923583 0.3829998 0.07979816
## [2,] 0.1831306 0.2411702 0.1250475 0.30812756
## [3,] 0.1399407 0.0216336 0.2968065 0.07380767
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2888969 0.20554972 0.4092649 0.0852705
## [2,] 0.1956892 0.25770899 0.1336229 0.3292581
## [3,] 0.1495375 0.02311717 0.3171607 0.0788692
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07310136 0.05201150 0.10355880 0.02157652
## [2,] 0.04951644 0.06520968 0.03381142 0.08331419
## [3,] 0.03783838 0.00584948 0.08025311 0.01995676
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03252093 0.023138594 0.04607068 0.009598843
## [2,] 0.02202861 0.029010129 0.01504184 0.037064365
## [3,] 0.01683333 0.002602285 0.03570257 0.008878253
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03251714 0.023135898 0.04606531 0.009597724
## [2,] 0.02202604 0.029006748 0.01504009 0.037060046
## [3,] 0.01683137 0.002601982 0.03569841 0.008877219
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.15847758 0.11275655 0.22450675 0.04677607
## [2,] 0.10734747 0.14136909 0.07330031 0.18061815
## [3,] 0.08203043 0.01268118 0.17398198 0.04326457
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4344133 0.30908441 0.6154102 0.1282210
## [2,] 0.2942572 0.38751613 0.2009283 0.4951043
## [3,] 0.2248590 0.03476122 0.4769135 0.1185954
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3758159 0.26739239 0.5323983 0.1109254
## [2,] 0.2545652 0.33524455 0.1738253 0.4283203
## [3,] 0.1945280 0.03007232 0.4125832 0.1025982
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4915303 0.34972307 0.6963249 0.1450796
## [2,] 0.3329464 0.43846706 0.2273465 0.5602010
## [3,] 0.2544236 0.03933165 0.5396184 0.1341884
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.12906378 0.09182868 0.18283779 0.03809432
## [2,] 0.08742354 0.11513067 0.05969560 0.14709501
## [3,] 0.06680539 0.01032752 0.14169053 0.03523456
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.0088671106 0.0063089354 0.0125615635 0.0026172065
## [2,] 0.0060062875 0.0079098594 0.0041012864 0.0101059156
## [3,] 0.0045897523 0.0007095352 0.0097346100 0.0024207316
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.34762761 0.24733650 0.49246553 0.10260538
## [2,] 0.23547145 0.31009938 0.16078748 0.39619392
## [3,] 0.17993738 0.02781673 0.38163719 0.09490275
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.43441333 0.30908441 0.61541024 0.12822096
## [2,] 0.29425722 0.38751613 0.20092830 0.49510429
## [3,] 0.22485900 0.03476122 0.47691345 0.11859535
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.37581585 0.26739239 0.53239831 0.11092539
## [2,] 0.25456523 0.33524455 0.17382533 0.42832028
## [3,] 0.19452804 0.03007232 0.41258319 0.10259817
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.49153034 0.34972307 0.69632487 0.14507955
## [2,] 0.33294640 0.43846706 0.22734652 0.56020099
## [3,] 0.25442364 0.03933165 0.53961842 0.13418836
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] 2.178342
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 2.178342
einsum::einsum('ij->', arrC)
## [1] 4.630845
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 4.630845
einsum::einsum('ijk->', arrE)
## [1] 30.35674
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 30.35674
einsum::einsum('ij->i', arrC)
## [1] 1.851222 1.715133 1.064490
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 1.851222 1.715133 1.064490
einsum::einsum('ij->j', arrC)
## [1] 1.1869816 0.9104206 1.6098780 0.9235645
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 1.1869816 0.9104206 1.6098780 0.9235645
einsum::einsum('ijk->i', arrE)
## [1] 12.114032 8.446548 9.796157
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 12.114032 8.446548 9.796157
einsum::einsum('ijk->j', arrE)
## [1] 6.367640 7.323341 7.607561 9.058195
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 6.367640 7.323341 7.607561 9.058195
einsum::einsum('ijk->k', arrE)
## [1] 5.830167 6.086333 6.652927 6.680846 5.106464
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 5.830167 6.086333 6.652927 6.680846 5.106464
These are the same as what the modeSum function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 2.429038 3.437372 2.976501 3.271121
## [2,] 1.496192 2.333547 1.834339 2.782470
## [3,] 2.442410 1.552422 2.796721 3.004604
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 2.429038 3.437372 2.976501 3.271121
## [2,] 1.496192 2.333547 1.834339 2.782470
## [3,] 2.442410 1.552422 2.796721 3.004604
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.8979013 1.382638 1.847054 1.123502 1.1165459
## [2,] 1.5693179 1.576993 1.744220 1.263450 1.1693592
## [3,] 1.9364312 1.378857 1.465352 2.413593 0.4133282
## [4,] 1.4265163 1.747846 1.596301 1.880301 2.4072312
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.8979013 1.3826376 1.8470539 1.1235018 1.1165459
## [2,] 1.5693179 1.5769932 1.7442203 1.2634505 1.1693592
## [3,] 1.9364312 1.3788566 1.4653519 2.4135929 0.4133282
## [4,] 1.4265163 1.7478458 1.5963010 1.8803011 2.4072312
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.8979013 1.382638 1.847054 1.123502 1.1165459
## [2,] 1.5693179 1.576993 1.744220 1.263450 1.1693592
## [3,] 1.9364312 1.378857 1.465352 2.413593 0.4133282
## [4,] 1.4265163 1.747846 1.596301 1.880301 2.4072312
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.8979013 1.3826376 1.8470539 1.1235018 1.1165459
## [2,] 1.5693179 1.5769932 1.7442203 1.2634505 1.1693592
## [3,] 1.9364312 1.3788566 1.4653519 2.4135929 0.4133282
## [4,] 1.4265163 1.7478458 1.5963010 1.8803011 2.4072312
If we take the diagonal elements of a matrix
and add them together, we get trace.
einsum::einsum('ii->', arrB)
## [1] 1.381695
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 1.381695
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.502004813 0.9504958 0.6201391
## [2,] 0.002824075 0.1422812 0.6877132
## [3,] 0.884779935 0.2372263 0.7374094
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.502004813 0.950495794 0.620139112
## [2,] 0.002824075 0.142281183 0.687713250
## [3,] 0.884779935 0.237226274 0.737409377
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.07921062 0.62375173 0.4239442
## [2,] 0.09256001 0.38567775 0.7231836
## [3,] 0.80729981 0.04616608 0.9649225
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.4146287 0.0138454 0.3117027
## [2,] 0.1453022 0.8073956 0.7988075
## [3,] 0.5848245 0.9191537 0.7864594
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.5951349 0.2007140 0.09649433
## [2,] 0.5535448 0.3602362 0.61691690
## [3,] 0.3062137 0.3102061 0.07530616
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.07921062 0.62375173 0.42394424
## [2,] 0.09256001 0.38567775 0.72318357
## [3,] 0.80729981 0.04616608 0.96492248
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.4146287 0.0138454 0.3117027
## [2,] 0.1453022 0.8073956 0.7988075
## [3,] 0.5848245 0.9191537 0.7864594
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.59513493 0.20071396 0.09649433
## [2,] 0.55354482 0.36023622 0.61691690
## [3,] 0.30621370 0.31020609 0.07530616
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.602773
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.602773
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 2.316584
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 2.316584
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 20.37616
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 20.37616
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.4704708 0.9566129 1.2021638 0.5302877 0.77525502
## [2,] 1.1230394 1.2279953 1.1758826 0.6567424 0.55362464
## [3,] 1.4232834 0.7994381 0.7789898 1.9815976 0.09311586
## [4,] 1.0044617 1.5040004 0.9802640 1.1844546 1.95448249
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.47047080 0.95661285 1.20216384 0.53028769 0.77525502
## [2,] 1.12303938 1.22799527 1.17588260 0.65674242 0.55362464
## [3,] 1.42328339 0.79943813 0.77898979 1.98159761 0.09311586
## [4,] 1.00446165 1.50400038 0.98026397 1.18445457 1.95448249
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.0528269 0.6736740 0.6463843
## [2,] 0.6736740 0.8092887 0.3628844
## [3,] 0.6463843 0.3628844 0.4544686
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.0528269 0.6736740 0.6463843
## [2,] 0.6736740 0.8092887 0.3628844
## [3,] 0.6463843 0.3628844 0.4544686
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.29243269 0.13417571 0.078350212
## [2,] 0.14803821 0.23270151 0.001872447
## [3,] 0.58687967 0.06256074 0.352451027
## [4,] 0.02547638 0.37985077 0.021794904
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.292432688 0.134175708 0.078350212
## [2,] 0.148038210 0.232701506 0.001872447
## [3,] 0.586879671 0.062560738 0.352451027
## [4,] 0.025476377 0.379850766 0.021794904
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.05696169 0.54919616 0.1717122 0.03089536 0.738124308
## [2,] 0.80211021 0.76695896 0.3361020 0.34376385 0.249947084
## [3,] 0.79499894 0.45362750 0.2074941 0.80228150 0.003616597
## [4,] 0.85814972 0.00065659 0.6844301 0.47404285 0.645327791
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0002688675 0.0000107342 0.4497540 0.39998745 0.030080025
## [2,] 0.0144187251 0.4605250341 0.7510781 0.01794779 0.285403920
## [3,] 0.1019715446 0.0175228361 0.4629887 0.41225541 0.003615754
## [4,] 0.0216188551 0.9252658830 0.1546841 0.34253716 0.482974575
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.4132402 0.4074059610 0.58069764 0.09940488 0.007050684
## [2,] 0.3065104 0.0005112719 0.08870248 0.29503078 0.018273635
## [3,] 0.5263129 0.3282877968 0.10850702 0.76706069 0.085883504
## [4,] 0.1246931 0.5780779119 0.14114983 0.36787457 0.826180127
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.056961688 0.549196159 0.171712192 0.030895357 0.738124308
## [2,] 0.802110214 0.766958964 0.336102039 0.343763853 0.249947084
## [3,] 0.794998938 0.453627500 0.207494094 0.802281501 0.003616597
## [4,] 0.858149725 0.000656590 0.684430051 0.474042847 0.645327791
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0002688675 0.0000107342 0.4497540047 0.3999874507 0.0300800250
## [2,] 0.0144187251 0.4605250341 0.7510780827 0.0179477889 0.2854039203
## [3,] 0.1019715446 0.0175228361 0.4629886676 0.4122554143 0.0036157541
## [4,] 0.0216188551 0.9252658830 0.1546840815 0.3425371573 0.4829745746
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.4132402414 0.4074059610 0.5806976416 0.0994048804 0.0070506839
## [2,] 0.3065104446 0.0005112719 0.0887024812 0.2950307783 0.0182736347
## [3,] 0.5263129098 0.3282877968 0.1085070239 0.7670606915 0.0858835041
## [4,] 0.1246930731 0.5780779119 0.1411498344 0.3678745687 0.8261801268
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 2.952263 0.6028385 2.275065
## [2,] 2.315982 1.7761775 1.994173
## [3,] 2.276943 2.6110163 1.764968
## [4,] 2.346294 1.9937529 2.340799
## [5,] 2.222550 1.4627629 1.421152
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 2.9522634 0.6028385 2.2750648
## [2,] 2.3159824 1.7761775 1.9941733
## [3,] 2.2769429 2.6110163 1.7649680
## [4,] 2.3462939 1.9937529 2.3407994
## [5,] 2.2225498 1.4627629 1.4211517
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.01270804 0.1225245525 0.03830864 0.006892692 0.164674040
## [2,] 0.09058944 0.0866194998 0.03795899 0.038824311 0.028228748
## [3,] 0.35594700 0.2031038507 0.09290188 0.359207637 0.001619269
## [4,] 0.01667902 0.0000127615 0.01330260 0.009213507 0.012542605
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.904911e-05 1.159749e-06 0.04859254 0.04321564 0.0032499206
## [2,] 2.701760e-03 8.629251e-02 0.14073591 0.00336303 0.0534785699
## [3,] 5.136905e-03 8.827280e-04 0.02332345 0.02076772 0.0001821467
## [4,] 6.612510e-03 2.830090e-01 0.04731287 0.10477106 0.1477263382
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.019756964 1.947803e-02 0.0277630809 0.0047525348 3.370923e-04
## [2,] 0.000350213 5.841695e-07 0.0001013498 0.0003370965 2.087911e-05
## [3,] 0.113193171 7.060427e-02 0.0233364105 0.1649703632 1.847081e-02
## [4,] 0.001658344 7.688095e-03 0.0018772095 0.0048925148 1.098771e-02
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.0127080375 0.1225245525 0.0383086428 0.0068926917 0.1646740404
## [2,] 0.0905894431 0.0866194998 0.0379589937 0.0388243106 0.0282287481
## [3,] 0.3559469956 0.2031038507 0.0929018845 0.3592076369 0.0016192686
## [4,] 0.0166790169 0.0000127615 0.0133025975 0.0092135072 0.0125426051
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.904911e-05 1.159749e-06 4.859254e-02 4.321564e-02 3.249921e-03
## [2,] 2.701760e-03 8.629251e-02 1.407359e-01 3.363030e-03 5.347857e-02
## [3,] 5.136905e-03 8.827280e-04 2.332345e-02 2.076772e-02 1.821467e-04
## [4,] 6.612510e-03 2.830090e-01 4.731287e-02 1.047711e-01 1.477263e-01
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.975696e-02 1.947803e-02 2.776308e-02 4.752535e-03 3.370923e-04
## [2,] 3.502130e-04 5.841695e-07 1.013498e-04 3.370965e-04 2.087911e-05
## [3,] 1.131932e-01 7.060427e-02 2.333641e-02 1.649704e-01 1.847081e-02
## [4,] 1.658344e-03 7.688095e-03 1.877210e-03 4.892515e-03 1.098771e-02
einsumBy 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.3.0 RC (2023-04-18 r84287 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows Server 2022 x64 (build 20348)
##
## Matrix products: default
##
##
## locale:
## [1] LC_COLLATE=C
## [2] LC_CTYPE=English_United States.utf8
## [3] LC_MONETARY=English_United States.utf8
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.utf8
##
## time zone: America/New_York
## tzcode source: internal
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] einsum_0.1.0 DelayedRandomArray_1.9.1 HDF5Array_1.29.2
## [4] rhdf5_2.45.0 DelayedArray_0.27.2 SparseArray_1.1.2
## [7] S4Arrays_1.1.2 IRanges_2.35.1 S4Vectors_0.39.1
## [10] MatrixGenerics_1.13.0 matrixStats_0.63.0 BiocGenerics_0.47.0
## [13] Matrix_1.5-4 DelayedTensor_1.7.0 BiocStyle_2.29.0
##
## loaded via a namespace (and not attached):
## [1] jsonlite_1.8.4 compiler_4.3.0 BiocManager_1.30.20
## [4] crayon_1.5.2 rsvd_1.0.5 Rcpp_1.0.10
## [7] rhdf5filters_1.13.2 parallel_4.3.0 jquerylib_0.1.4
## [10] BiocParallel_1.35.0 yaml_2.3.7 fastmap_1.1.1
## [13] lattice_0.21-8 R6_2.5.1 XVector_0.41.1
## [16] ScaledMatrix_1.9.1 knitr_1.42 bookdown_0.33
## [19] bslib_0.4.2 rlang_1.1.1 cachem_1.0.8
## [22] xfun_0.39 sass_0.4.6 cli_3.6.1
## [25] Rhdf5lib_1.23.0 BiocSingular_1.17.0 zlibbioc_1.47.0
## [28] digest_0.6.31 grid_4.3.0 irlba_2.3.5.1
## [31] rTensor_1.4.8 dqrng_0.3.0 evaluate_0.20
## [34] codetools_0.2-19 beachmat_2.17.1 rmarkdown_2.21
## [37] tools_4.3.0 htmltools_0.5.5