DelayedTensor 1.0.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2021-10-26 15:33:21
Compiled: Tue Oct 26 21:04:53 2021
einsum
einsum
is an easy and intuitive way to write tensor operations.
It was originally introduced by
Numpy
1 https://numpy.org/doc/stable/reference/generated/numpy.einsum.html
package of Python but similar tools have been implemented in other languages
(e.g. R, Julia) inspired by Numpy
.
In this vignette, we will use CRAN einsum package first.
einsum
is named after
Einstein summation2 https://en.wikipedia.org/wiki/Einstein_notation
introduced by Albert Einstein,
which is a notational convention that implies summation over
a set of indexed terms in a formula.
Here, we consider a simple example of einsum
; matrix multiplication.
If we naively implement the matrix multiplication,
the calculation would look like the following in a for loop.
A <- matrix(runif(3*4), nrow=3, ncol=4)
B <- matrix(runif(4*5), nrow=4, ncol=5)
C <- matrix(0, nrow=3, ncol=5)
I <- nrow(A)
J <- ncol(A)
K <- ncol(B)
for(i in 1:I){
for(j in 1:J){
for(k in 1:K){
C[i,k] = C[i,k] + A[i,j] * B[j,k]
}
}
}
Therefore, any programming language can implement this. However, when analyzing tensor data, such operations tend to be more complicated and increase the possibility of causing bugs because the order of tensors is larger or more tensors are handled simultaneously. In addition, several programming languages, especially R, are known to significantly slow down the speed of computation if the code is written in for loop.
Obviously, in the case of the R language, it should be executed using the built-in matrix multiplication function (%*%) prepared by the R, as shown below.
C <- A %*% B
However, more complex operations than matrix multiplication are not always provided by programming languages as standard.
einsum
is a function that solves such a problem.
To put it simply, einsum
is a wrapper for the for loop above.
Like the Einstein summation, it omits many notations such as for,
array size (e.g. I, J, and K), brackets (e.g. {}, (), and []),
and even addition operator (+) and
extracts the array subscripts (e.g. i, j, and k)
to concisely express the tensor operation as follows.
suppressPackageStartupMessages(library("einsum"))
C <- einsum('ij,jk->ik', A, B)
DelayedTensor
CRAN einsum is easy to use because the syntax is almost
the same as that of Numpy
‘s einsum
,
except that it prohibits the implicit modes that do not use’->’.
It is extremely fast because the internal calculation
is actually performed by C++.
When the input tensor is huge, however,
it is not scalable because it assumes that the input is R’s standard array.
Using einsum
of DelayedTensor,
we can augment the CRAN einsum
’s functionality;
in DelayedTensor,
the input DelayedArray objects are divided into
multiple block tensors and the CRAN einsum
is incremently applied in the block processing.
A surprisingly large number of tensor operations can be handled
uniformly in einsum
.
In more detail, einsum
is capable of performing any tensor operation
that can be described by a combination of the following
three operations3 https://ajcr.net/Basic-guide-to-einsum/.
Some typical operations are introduced below. Here we use the arrays and DelayedArray objects below.
suppressPackageStartupMessages(library("DelayedTensor"))
suppressPackageStartupMessages(library("DelayedArray"))
arrA <- array(runif(3), dim=c(3))
arrB <- array(runif(3*3), dim=c(3,3))
arrC <- array(runif(3*4), dim=c(3,4))
arrD <- array(runif(3*3*3), dim=c(3,3,3))
arrE <- array(runif(3*4*5), dim=c(3,4,5))
darrA <- DelayedArray(arrA)
darrB <- DelayedArray(arrB)
darrC <- DelayedArray(arrC)
darrD <- DelayedArray(arrD)
darrE <- DelayedArray(arrE)
If the same subscript is written on both sides of ->,
einsum
will simply output the object without any calculation.
einsum::einsum('i->i', arrA)
## [1] 0.20513818 0.18359601 0.06704225
DelayedTensor::einsum('i->i', darrA)
## <3> array of class DelayedArray and type "double":
## [1] [2] [3]
## 0.20513818 0.18359601 0.06704225
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.6424802 0.1659930 0.3426848 0.04362615
## [2,] 0.7659118 0.3413148 0.3470581 0.05540834
## [3,] 0.6549631 0.4985020 0.6988532 0.67553360
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.64248021 0.16599304 0.34268482 0.04362615
## [2,] 0.76591184 0.34131478 0.34705806 0.05540834
## [3,] 0.65496309 0.49850200 0.69885318 0.67553360
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.96383431 0.5913194 0.2997702 0.02105426
## [2,] 0.01346796 0.8412430 0.5012095 0.87234571
## [3,] 0.14253552 0.4697116 0.6049144 0.26144279
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4409698 0.1239300 0.4336190 0.04759670
## [2,] 0.5694136 0.2374810 0.8414630 0.02031879
## [3,] 0.8013025 0.5784137 0.6222156 0.54091227
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.009894075 0.6274095 0.1392038 0.2043052
## [2,] 0.109851157 0.3002029 0.5999186 0.7327796
## [3,] 0.572258263 0.7478009 0.1317702 0.4558522
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6827555 0.5524515 0.2875549 0.03586205
## [2,] 0.6427814 0.5383140 0.4368214 0.71389129
## [3,] 0.2810829 0.7171392 0.6085452 0.48127948
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.72216513 0.59261501 0.9505520 0.1780320
## [2,] 0.04324834 0.07579619 0.2464563 0.2781513
## [3,] 0.95739942 0.47972740 0.2761704 0.4632785
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.96383431 0.59131944 0.29977015 0.02105426
## [2,] 0.01346796 0.84124303 0.50120950 0.87234571
## [3,] 0.14253552 0.46971159 0.60491444 0.26144279
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.44096981 0.12393005 0.43361905 0.04759670
## [2,] 0.56941359 0.23748102 0.84146295 0.02031879
## [3,] 0.80130246 0.57841371 0.62221558 0.54091227
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.009894075 0.627409527 0.139203785 0.204305235
## [2,] 0.109851157 0.300202894 0.599918616 0.732779590
## [3,] 0.572258263 0.747800916 0.131770234 0.455852213
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.68275546 0.55245148 0.28755492 0.03586205
## [2,] 0.64278139 0.53831401 0.43682136 0.71389129
## [3,] 0.28108285 0.71713916 0.60854522 0.48127948
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.72216513 0.59261501 0.95055204 0.17803199
## [2,] 0.04324834 0.07579619 0.24645634 0.27815134
## [3,] 0.95739942 0.47972740 0.27617039 0.46327850
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.9951045 0.4453396 0.3386786
DelayedTensor::einsum('ii->i', darrB)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.9951045 0.4453396 0.3386786
einsum::einsum('iii->i', arrD)
## [1] 0.5050936 0.6728211 0.8142475
DelayedTensor::einsum('iii->i', darrD)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.5050936 0.6728211 0.8142475
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.042081673 0.033707494 0.004494664
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.042081673 0.033707494 0.004494664
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.4127808 0.02755369 0.1174329 0.001903241
## [2,] 0.5866209 0.11649578 0.1204493 0.003070084
## [3,] 0.4289766 0.24850425 0.4883958 0.456345646
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.412780824 0.027553689 0.117432884 0.001903241
## [2,] 0.586620945 0.116495777 0.120449296 0.003070084
## [3,] 0.428976648 0.248504246 0.488395774 0.456345646
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.928976572 0.3496587 0.08986214 0.0004432817
## [2,] 0.000181386 0.7076898 0.25121096 0.7609870421
## [3,] 0.020316373 0.2206290 0.36592148 0.0683523320
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1944544 0.01535866 0.1880255 0.0022654460
## [2,] 0.3242318 0.05639724 0.7080599 0.0004128533
## [3,] 0.6420856 0.33456242 0.3871522 0.2925860864
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 9.789272e-05 0.39364271 0.01937769 0.04174063
## [2,] 1.206728e-02 0.09012178 0.35990235 0.53696593
## [3,] 3.274795e-01 0.55920621 0.01736339 0.20780124
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.46615502 0.3052026 0.08268783 0.001286087
## [2,] 0.41316792 0.2897820 0.19081290 0.509640781
## [3,] 0.07900757 0.5142886 0.37032729 0.231629939
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.521522476 0.351192551 0.90354919 0.03169539
## [2,] 0.001870419 0.005745063 0.06074073 0.07736817
## [3,] 0.916613643 0.230138374 0.07627009 0.21462697
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.9289765717 0.3496586795 0.0898621444 0.0004432817
## [2,] 0.0001813860 0.7076898281 0.2512109638 0.7609870421
## [3,] 0.0203163735 0.2206289803 0.3659214787 0.0683523320
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.1944543710 0.0153586571 0.1880254804 0.0022654460
## [2,] 0.3242318376 0.0563972363 0.7080599009 0.0004128533
## [3,] 0.6420856354 0.3345624248 0.3871522253 0.2925860864
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 9.789272e-05 3.936427e-01 1.937769e-02 4.174063e-02
## [2,] 1.206728e-02 9.012178e-02 3.599023e-01 5.369659e-01
## [3,] 3.274795e-01 5.592062e-01 1.736339e-02 2.078012e-01
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.466155017 0.305202638 0.082687829 0.001286087
## [2,] 0.413167921 0.289781974 0.190812900 0.509640781
## [3,] 0.079007571 0.514288580 0.370327290 0.231629939
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.521522476 0.351192551 0.903549187 0.031695389
## [2,] 0.001870419 0.005745063 0.060740729 0.077368166
## [3,] 0.916613643 0.230138374 0.076270087 0.214626965
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.04208167 0.03766255 0.013752926
## [2,] 0.03766255 0.03370749 0.012308690
## [3,] 0.01375293 0.01230869 0.004494664
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.042081673 0.037662551 0.013752926
## [2,] 0.037662551 0.033707494 0.012308690
## [3,] 0.013752926 0.012308690 0.004494664
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6192445 0.1599898 0.3302914 0.04204838
## [2,] 0.7382121 0.3289709 0.3345065 0.05340445
## [3,] 0.6312759 0.4804733 0.6735787 0.65110246
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.008652899 0.002235588 0.004615266 0.0005875554
## [2,] 0.010315272 0.004596815 0.004674165 0.0007462374
## [3,] 0.008821018 0.006713806 0.009412128 0.0090980610
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09157625 0.02365990 0.04884476 0.006218276
## [2,] 0.10916964 0.04864948 0.04946810 0.007897656
## [3,] 0.09335550 0.07105424 0.09961140 0.096287531
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3799110 0.09815491 0.2026362 0.02579699
## [2,] 0.4528986 0.20182606 0.2052222 0.03276403
## [3,] 0.3872924 0.29477392 0.4132455 0.39945615
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5404820 0.1396405 0.2882812 0.03670020
## [2,] 0.6443180 0.2871287 0.2919602 0.04661188
## [3,] 0.5509831 0.4193613 0.5879054 0.56828793
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3017804 0.07796885 0.1609630 0.02049171
## [2,] 0.3597577 0.16031951 0.1630172 0.02602594
## [3,] 0.3076438 0.23415217 0.3282594 0.31730596
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1925964 0.04975976 0.1027267 0.01307782
## [2,] 0.2295975 0.10231598 0.1040376 0.01660977
## [3,] 0.1963384 0.14943602 0.2094953 0.20250481
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3220172 0.08319729 0.1717569 0.02186584
## [2,] 0.3838823 0.17107021 0.1739488 0.02777118
## [3,] 0.3282737 0.24985394 0.3502719 0.33858386
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3886456 0.1004116 0.2072950 0.02639009
## [2,] 0.4633111 0.2064662 0.2099404 0.03351730
## [3,] 0.3961966 0.3015511 0.4227464 0.40864003
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01352694 0.003494860 0.007214974 0.0009185161
## [2,] 0.01612570 0.007186129 0.007307049 0.0011665812
## [3,] 0.01378976 0.010495589 0.014713834 0.0142228571
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5604649 0.1448033 0.2989396 0.03805709
## [2,] 0.6681399 0.2977445 0.3027546 0.04833522
## [3,] 0.5713542 0.4348661 0.6096416 0.58929884
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1679718 0.04339768 0.08959247 0.01140574
## [2,] 0.2002421 0.08923429 0.09073583 0.01448611
## [3,] 0.1712354 0.13032975 0.18271013 0.17661339
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2833144 0.07319792 0.1511137 0.01923782
## [2,] 0.3377440 0.15050951 0.1530421 0.02443340
## [3,] 0.2888189 0.21982433 0.3081732 0.29788992
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3658370 0.09451869 0.1951294 0.02484132
## [2,] 0.4361206 0.19434927 0.1976196 0.03155026
## [3,] 0.3729449 0.28385382 0.3979365 0.38465801
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5148210 0.1330106 0.2745942 0.03495774
## [2,] 0.6137270 0.2734964 0.2780985 0.04439884
## [3,] 0.5248235 0.3994509 0.5599928 0.54130674
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07962260 0.02057153 0.04246895 0.005406591
## [2,] 0.09491949 0.04229916 0.04301092 0.006866758
## [3,] 0.08116961 0.06177938 0.08660891 0.083718912
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1525769 0.03942020 0.08138114 0.01036038
## [2,] 0.1818895 0.08105578 0.08241970 0.01315843
## [3,] 0.1555413 0.11838477 0.16596437 0.16042641
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3716194 0.09601265 0.1982136 0.02523396
## [2,] 0.4430139 0.19742115 0.2007431 0.03204894
## [3,] 0.3788396 0.28834039 0.4042263 0.39073790
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2785917 0.07197774 0.1485947 0.01891713
## [2,] 0.3321140 0.14800059 0.1504910 0.02402611
## [3,] 0.2840045 0.21615996 0.3030361 0.29292424
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5406233 0.1396770 0.2883566 0.03670979
## [2,] 0.6444864 0.2872037 0.2920365 0.04662406
## [3,] 0.5511272 0.4194710 0.5880591 0.56843650
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3997612 0.1032835 0.2132238 0.02714487
## [2,] 0.4765623 0.2123714 0.2159449 0.03447593
## [3,] 0.4075282 0.3101757 0.4348373 0.42032753
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03057994 0.007900721 0.01631067 0.002076461
## [2,] 0.03645488 0.016245458 0.01651882 0.002637254
## [3,] 0.03117408 0.023727051 0.03326311 0.032153171
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01305442 0.003372778 0.006962942 0.0008864307
## [2,] 0.01556240 0.006935104 0.007051801 0.0011258305
## [3,] 0.01330806 0.010128959 0.014199853 0.0137260273
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3475254 0.08978767 0.1853624 0.02359792
## [2,] 0.4142911 0.18462135 0.1877280 0.02997105
## [3,] 0.3542776 0.26964585 0.3780183 0.36540442
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.006356747 0.001642348 0.003390549 0.0004316404
## [2,] 0.007577989 0.003376994 0.003433818 0.0005482142
## [3,] 0.006480254 0.004932216 0.006914506 0.0066837800
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07057719 0.01823453 0.03764432 0.004792383
## [2,] 0.08413630 0.03749382 0.03812473 0.006086670
## [3,] 0.07194845 0.05476102 0.07676983 0.074208148
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3676646 0.09499089 0.1961042 0.02496543
## [2,] 0.4382994 0.19532020 0.1986068 0.03170788
## [3,] 0.3748080 0.28527189 0.3999245 0.38657968
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4030982 0.1041456 0.2150037 0.02737146
## [2,] 0.4805404 0.2141441 0.2177475 0.03476372
## [3,] 0.4109301 0.3127649 0.4384671 0.42383622
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1928744 0.04983159 0.1028750 0.01309670
## [2,] 0.2299290 0.10246368 0.1041878 0.01663374
## [3,] 0.1966218 0.14965174 0.2097977 0.20279714
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4804473 0.1241297 0.2562600 0.03262368
## [2,] 0.5727496 0.2552355 0.2595303 0.04143440
## [3,] 0.4897820 0.3727803 0.5226031 0.50516465
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08943568 0.02310686 0.04770302 0.006072925
## [2,] 0.10661783 0.04751231 0.04831180 0.007713050
## [3,] 0.09117334 0.06939337 0.09728301 0.094036834
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3854358 0.09958231 0.2055830 0.02617214
## [2,] 0.4594848 0.20476109 0.2082066 0.03324049
## [3,] 0.3929246 0.29906063 0.4192550 0.40526518
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08465977 0.02187294 0.04515566 0.005748628
## [2,] 0.10092438 0.04497513 0.04573192 0.007301169
## [3,] 0.08630464 0.06568773 0.09208805 0.089015220
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1312621 0.03391325 0.07001230 0.008913051
## [2,] 0.1564798 0.06973240 0.07090578 0.011320213
## [3,] 0.1338124 0.10184657 0.14277936 0.138015051
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4707964 0.1216363 0.2511124 0.03196835
## [2,] 0.5612446 0.2501085 0.2543171 0.04060210
## [3,] 0.4799436 0.3652921 0.5121054 0.49501724
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2928760 0.07566829 0.1562136 0.01988708
## [2,] 0.3491426 0.15558910 0.1582072 0.02525801
## [3,] 0.2985664 0.22724324 0.3185738 0.30794349
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4386569 0.1133327 0.2339699 0.02978599
## [2,] 0.5229305 0.2330345 0.2369558 0.03783034
## [3,] 0.4471796 0.3403550 0.4771458 0.46122425
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4129743 0.1066972 0.2202714 0.02804208
## [2,] 0.4923139 0.2193908 0.2230825 0.03561545
## [3,] 0.4209981 0.3204278 0.4492098 0.43422043
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1805902 0.04665780 0.09632283 0.01226256
## [2,] 0.2152847 0.09593773 0.09755207 0.01557433
## [3,] 0.1840989 0.14012037 0.19643565 0.18988091
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3549391 0.0917031 0.1893167 0.02410133
## [2,] 0.4231291 0.1885599 0.1917327 0.03061042
## [3,] 0.3618353 0.2753982 0.3860825 0.37319954
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3458561 0.08935638 0.1844720 0.02348457
## [2,] 0.4123011 0.18373453 0.1868262 0.02982708
## [3,] 0.3525758 0.26835061 0.3762025 0.36364920
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4607477 0.1190401 0.2457527 0.03128602
## [2,] 0.5492654 0.2447702 0.2488889 0.03973549
## [3,] 0.4696997 0.3574953 0.5011750 0.48445160
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1847483 0.04773211 0.09854070 0.01254491
## [2,] 0.2202417 0.09814674 0.09979825 0.01593294
## [3,] 0.1883379 0.14334670 0.20095867 0.19425301
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2806491 0.07250931 0.1496920 0.01905683
## [2,] 0.3345667 0.14909358 0.1516024 0.02420354
## [3,] 0.2861019 0.21775632 0.3052740 0.29508751
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3909783 0.1010143 0.2085392 0.02654849
## [2,] 0.4660920 0.2077055 0.2112005 0.03371848
## [3,] 0.3985747 0.3033610 0.4252838 0.41109275
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02304066 0.005952851 0.01228938 0.001564523
## [2,] 0.02746717 0.012240248 0.01244621 0.001987057
## [3,] 0.02348832 0.017877305 0.02506231 0.024226021
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4586610 0.1185010 0.2446397 0.03114433
## [2,] 0.5467778 0.2436616 0.2477617 0.03955553
## [3,] 0.4675724 0.3558762 0.4989052 0.48225756
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3092125 0.07988904 0.1649272 0.02099637
## [2,] 0.3686177 0.16426780 0.1670319 0.02666689
## [3,] 0.3152203 0.23991879 0.3363437 0.32512046
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4639768 0.1198744 0.2474750 0.03150529
## [2,] 0.5531148 0.2464856 0.2506332 0.04001397
## [3,] 0.4729915 0.3600008 0.5046874 0.48784681
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02778620 0.007178924 0.01482055 0.001886759
## [2,] 0.03312442 0.014761298 0.01500969 0.002396319
## [3,] 0.02832607 0.021559385 0.03022424 0.029215708
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6151102 0.1589216 0.3280862 0.04176765
## [2,] 0.7332835 0.3267746 0.3322732 0.05304791
## [3,] 0.6270613 0.4772655 0.6690816 0.64675548
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3807434 0.09836997 0.2030802 0.02585351
## [2,] 0.4538909 0.20226826 0.2056718 0.03283581
## [3,] 0.3881410 0.29541977 0.4141509 0.40033135
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04869755 0.01258164 0.02597420 0.003306696
## [2,] 0.05805320 0.02587036 0.02630568 0.004199741
## [3,] 0.04964371 0.03778455 0.05297041 0.051202875
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3082154 0.07963141 0.1643953 0.02092866
## [2,] 0.3674289 0.16373805 0.1664933 0.02658090
## [3,] 0.3142037 0.23914507 0.3352590 0.32407197
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6107109 0.1577850 0.3257398 0.04146893
## [2,] 0.7280391 0.3244375 0.3298967 0.05266851
## [3,] 0.6225765 0.4738521 0.6642963 0.64212984
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1583433 0.04091004 0.08445685 0.01075194
## [2,] 0.1887638 0.08411919 0.08553466 0.01365574
## [3,] 0.1614198 0.12285898 0.17223680 0.16648954
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1774340 0.04584236 0.09463940 0.01204825
## [2,] 0.2115222 0.09426104 0.09584716 0.01530214
## [3,] 0.1808814 0.13767149 0.19300256 0.18656238
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1143820 0.02955207 0.06100886 0.007766851
## [2,] 0.1363568 0.06076495 0.06178744 0.009864456
## [3,] 0.1166044 0.08874930 0.12441822 0.120266590
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1787067 0.04617119 0.09531824 0.01213467
## [2,] 0.2130394 0.09493716 0.09653466 0.01541190
## [3,] 0.1821789 0.13865900 0.19438695 0.18790057
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2976473 0.07690101 0.1587585 0.02021106
## [2,] 0.3548305 0.15812380 0.1607845 0.02566949
## [3,] 0.3034303 0.23094526 0.3237637 0.31296019
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.61924447 0.15998979 0.33029138 0.04204838
## [2,] 0.73821211 0.32897089 0.33450646 0.05340445
## [3,] 0.63127589 0.48047333 0.67357868 0.65110246
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.0086528992 0.0022355880 0.0046152662 0.0005875554
## [2,] 0.0103152717 0.0045968145 0.0046741648 0.0007462374
## [3,] 0.0088210181 0.0067138061 0.0094121282 0.0090980610
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.091576249 0.023659904 0.048844757 0.006218276
## [2,] 0.109169640 0.048649478 0.049468100 0.007897656
## [3,] 0.093355502 0.071054240 0.099611400 0.096287531
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.114382030 0.029552071 0.061008860 0.007766851
## [2,] 0.136356808 0.060764949 0.061787436 0.009864456
## [3,] 0.116604381 0.088749303 0.124418222 0.120266590
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.17870673 0.04617119 0.09531824 0.01213467
## [2,] 0.21303940 0.09493716 0.09653466 0.01541190
## [3,] 0.18217886 0.13865900 0.19438695 0.18790057
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.29764727 0.07690101 0.15875851 0.02021106
## [2,] 0.35483049 0.15812380 0.16078454 0.02566949
## [3,] 0.30343032 0.23094526 0.32376365 0.31296019
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.4557764
DelayedTensor::einsum('i->', darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 0.4557764
einsum::einsum('ij->', arrC)
## [1] 5.232329
DelayedTensor::einsum('ij->', darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 5.232329
einsum::einsum('ijk->', arrE)
## [1] 26.7138
DelayedTensor::einsum('ijk->', darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 26.7138
einsum::einsum('ij->i', arrC)
## [1] 1.194784 1.509693 2.527852
DelayedTensor::einsum('ij->i', darrC)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 1.194784 1.509693 2.527852
einsum::einsum('ij->j', arrC)
## [1] 2.0633551 1.0058098 1.3885961 0.7745681
DelayedTensor::einsum('ij->j', darrC)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 2.0633551 1.0058098 1.3885961 0.7745681
einsum::einsum('ijk->i', arrE)
## [1] 7.904894 8.615155 10.193752
DelayedTensor::einsum('ijk->i', darrE)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 7.904894 8.615155 10.193752
einsum::einsum('ijk->j', arrE)
## [1] 6.952960 7.473555 6.980185 5.307102
DelayedTensor::einsum('ijk->j', darrE)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 6.952960 7.473555 6.980185 5.307102
einsum::einsum('ijk->k', arrE)
## [1] 5.582849 5.257636 4.631247 5.978479 5.263592
DelayedTensor::einsum('ijk->k', darrE)
## <5> array of class HDF5Array and type "double":
## [1] [2] [3] [4] [5]
## 5.582849 5.257636 4.631247 5.978479 5.263592
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 2.819619 2.487726 2.110700 0.4868502
## [2,] 1.378762 1.993037 2.625869 2.6174867
## [3,] 2.754579 2.992793 2.243616 2.2027653
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 2.8196188 2.4877255 2.1106999 0.4868502
## [2,] 1.3787624 1.9930371 2.6258688 2.6174867
## [3,] 2.7545785 2.9927928 2.2436159 2.2027653
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.119838 1.8116859 0.6920035 1.606620 1.7228129
## [2,] 1.902274 0.9398248 1.6754133 1.807905 1.1481386
## [3,] 1.405894 1.8972976 0.8708926 1.332921 1.4731788
## [4,] 1.154843 0.6088278 1.3929370 1.231033 0.9194618
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.1198378 1.8116859 0.6920035 1.6066197 1.7228129
## [2,] 1.9022741 0.9398248 1.6754133 1.8079047 1.1481386
## [3,] 1.4058941 1.8972976 0.8708926 1.3329215 1.4731788
## [4,] 1.1548428 0.6088278 1.3929370 1.2310328 0.9194618
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.119838 1.8116859 0.6920035 1.606620 1.7228129
## [2,] 1.902274 0.9398248 1.6754133 1.807905 1.1481386
## [3,] 1.405894 1.8972976 0.8708926 1.332921 1.4731788
## [4,] 1.154843 0.6088278 1.3929370 1.231033 0.9194618
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.1198378 1.8116859 0.6920035 1.6066197 1.7228129
## [2,] 1.9022741 0.9398248 1.6754133 1.8079047 1.1481386
## [3,] 1.4058941 1.8972976 0.8708926 1.3329215 1.4731788
## [4,] 1.1548428 0.6088278 1.3929370 1.2310328 0.9194618
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.779123
DelayedTensor::einsum('ii->', darrB)
## <1> array of class HDF5Array and type "double":
## [1]
## 1.779123
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.9951045 0.8629505 0.5845919
## [2,] 0.2988388 0.4453396 0.9607559
## [3,] 0.2925196 0.6691233 0.3386786
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3]
## [1,] 0.9951045 0.8629505 0.5845919
## [2,] 0.2988388 0.4453396 0.9607559
## [3,] 0.2925196 0.6691233 0.3386786
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.505093641 0.2260388 0.9509883
## [2,] 0.786482470 0.3601535 0.2301907
## [3,] 0.001244808 0.2966478 0.4765422
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.2005470 0.5689846 0.6409881
## [2,] 0.7760866 0.6728211 0.2803181
## [3,] 0.1106220 0.8609187 0.1628593
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.1579984 0.7497019 0.1919791
## [2,] 0.5219565 0.4209664 0.8827434
## [3,] 0.9482240 0.3223366 0.8142475
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.505093641 0.226038781 0.950988327
## [2,] 0.786482470 0.360153487 0.230190700
## [3,] 0.001244808 0.296647762 0.476542179
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.2005470 0.5689846 0.6409881
## [2,] 0.7760866 0.6728211 0.2803181
## [3,] 0.1106220 0.8609187 0.1628593
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.1579984 0.7497019 0.1919791
## [2,] 0.5219565 0.4209664 0.8827434
## [3,] 0.9482240 0.3223366 0.8142475
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.08028383
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 0.08028383
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 3.008529
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 3.008529
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 16.32091
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 16.32091
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.9494743 1.1607718 0.3396447 0.9583305 1.4400065
## [2,] 1.2779775 0.4063183 1.0429707 1.1092732 0.5870760
## [3,] 0.7069946 1.2832376 0.3966434 0.6438280 1.0405600
## [4,] 0.8297827 0.2952644 0.7865078 0.7425568 0.3236905
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9494743 1.1607718 0.3396447 0.9583305 1.4400065
## [2,] 1.2779775 0.4063183 1.0429707 1.1092732 0.5870760
## [3,] 0.7069946 1.2832376 0.3966434 0.6438280 1.0405600
## [4,] 0.8297827 0.2952644 0.7865078 0.7425568 0.3236905
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 0.5596706 0.6700879 0.7725060
## [2,] 0.6700879 0.8266361 0.9517629
## [3,] 0.7725060 0.9517629 1.6222223
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.5596706 0.6700879 0.7725060
## [2,] 0.6700879 0.8266361 0.9517629
## [3,] 0.7725060 0.9517629 1.6222223
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.412780824 0.586620945 0.4289766
## [2,] 0.027553689 0.116495777 0.2485042
## [3,] 0.117432884 0.120449296 0.4883958
## [4,] 0.001903241 0.003070084 0.4563456
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.412780824 0.586620945 0.428976648
## [2,] 0.027553689 0.116495777 0.248504246
## [3,] 0.117432884 0.120449296 0.488395774
## [4,] 0.001903241 0.003070084 0.456345646
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9289765717 0.194454371 9.789272e-05 0.466155017 0.52152248
## [2,] 0.3496586795 0.015358657 3.936427e-01 0.305202638 0.35119255
## [3,] 0.0898621444 0.188025480 1.937769e-02 0.082687829 0.90354919
## [4,] 0.0004432817 0.002265446 4.174063e-02 0.001286087 0.03169539
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.000181386 0.3242318376 0.01206728 0.4131679 0.001870419
## [2,] 0.707689828 0.0563972363 0.09012178 0.2897820 0.005745063
## [3,] 0.251210964 0.7080599009 0.35990235 0.1908129 0.060740729
## [4,] 0.760987042 0.0004128533 0.53696593 0.5096408 0.077368166
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.02031637 0.6420856 0.32747952 0.07900757 0.91661364
## [2,] 0.22062898 0.3345624 0.55920621 0.51428858 0.23013837
## [3,] 0.36592148 0.3871522 0.01736339 0.37032729 0.07627009
## [4,] 0.06835233 0.2925861 0.20780124 0.23162994 0.21462697
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.289766e-01 1.944544e-01 9.789272e-05 4.661550e-01 5.215225e-01
## [2,] 3.496587e-01 1.535866e-02 3.936427e-01 3.052026e-01 3.511926e-01
## [3,] 8.986214e-02 1.880255e-01 1.937769e-02 8.268783e-02 9.035492e-01
## [4,] 4.432817e-04 2.265446e-03 4.174063e-02 1.286087e-03 3.169539e-02
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0001813860 0.3242318376 0.0120672768 0.4131679206 0.0018704191
## [2,] 0.7076898281 0.0563972363 0.0901217774 0.2897819739 0.0057450628
## [3,] 0.2512109638 0.7080599009 0.3599023464 0.1908129003 0.0607407289
## [4,] 0.7609870421 0.0004128533 0.5369659274 0.5096407808 0.0773681660
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.02031637 0.64208564 0.32747952 0.07900757 0.91661364
## [2,] 0.22062898 0.33456242 0.55920621 0.51428858 0.23013837
## [3,] 0.36592148 0.38715223 0.01736339 0.37032729 0.07627009
## [4,] 0.06835233 0.29258609 0.20780124 0.23162994 0.21462697
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 1.8759782 2.2282662 1.478604
## [2,] 1.0461156 1.6686764 2.542844
## [3,] 0.9808126 1.7427523 1.907682
## [4,] 1.5586239 2.3318081 2.088047
## [5,] 2.4433642 0.6436522 2.176576
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 1.8759782 2.2282662 1.4786043
## [2,] 1.0461156 1.6686764 2.5428440
## [3,] 0.9808126 1.7427523 1.9076816
## [4,] 1.5586239 2.3318081 2.0880467
## [5,] 2.4433642 0.6436522 2.1765757
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,] 7.866305e-02 1.646583e-02 8.289272e-06 3.947266e-02 4.416101e-02
## [2,] 1.976381e-03 8.681195e-05 2.224992e-03 1.725101e-03 1.985050e-03
## [3,] 2.164776e-03 4.529528e-03 4.668080e-04 1.991947e-03 2.176647e-02
## [4,] 1.730693e-07 8.844922e-07 1.629669e-05 5.021235e-07 1.237475e-05
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0000195355 3.492018e-02 0.0012996610 0.0444987071 2.014465e-04
## [2,] 0.0151361830 1.206233e-03 0.0019275390 0.0061979031 1.228763e-04
## [3,] 0.0055552817 1.565804e-02 0.0079588840 0.0042196384 1.343221e-03
## [4,] 0.0004289342 2.327069e-07 0.0003026636 0.0002872616 4.360896e-05
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.000584290 0.018466101 0.0094181673 0.002272223 0.026361406
## [2,] 0.003675742 0.005573905 0.0093165347 0.008568194 0.003834171
## [3,] 0.011981423 0.012676585 0.0005685323 0.012125683 0.002497323
## [4,] 0.002091201 0.008951508 0.0063575627 0.007086588 0.006566392
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,] 7.866305e-02 1.646583e-02 8.289272e-06 3.947266e-02 4.416101e-02
## [2,] 1.976381e-03 8.681195e-05 2.224992e-03 1.725101e-03 1.985050e-03
## [3,] 2.164776e-03 4.529528e-03 4.668080e-04 1.991947e-03 2.176647e-02
## [4,] 1.730693e-07 8.844922e-07 1.629669e-05 5.021235e-07 1.237475e-05
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.953550e-05 3.492018e-02 1.299661e-03 4.449871e-02 2.014465e-04
## [2,] 1.513618e-02 1.206233e-03 1.927539e-03 6.197903e-03 1.228763e-04
## [3,] 5.555282e-03 1.565804e-02 7.958884e-03 4.219638e-03 1.343221e-03
## [4,] 4.289342e-04 2.327069e-07 3.026636e-04 2.872616e-04 4.360896e-05
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0005842900 0.0184661012 0.0094181673 0.0022722231 0.0263614063
## [2,] 0.0036757416 0.0055739053 0.0093165347 0.0085681942 0.0038341709
## [3,] 0.0119814231 0.0126765847 0.0005685323 0.0121256833 0.0024973232
## [4,] 0.0020912014 0.0089515077 0.0063575627 0.0070865884 0.0065663920
einsum
By using einsum
and other DelayedTensor functions,
it is possible to implement your original tensor calculation functions.
It is intended to be applied to Delayed Arrays,
which can scale to large-scale data
since the calculation is performed internally by block processing.
For example, kronecker
can be easily implmented by eimsum
and other DelayedTensor functions4 https://stackoverflow.com/
questions/56067643/speeding-up-kronecker-products-numpy
(the kronecker
function inside DelayedTensor
has a more efficient implementation though).
darr1 <- DelayedArray(array(1:6, dim=c(2,3)))
darr2 <- DelayedArray(array(20:1, dim=c(4,5)))
mykronecker <- function(darr1, darr2){
stopifnot((length(dim(darr1)) == 2) && (length(dim(darr2)) == 2))
# Outer Product
tmpdarr <- DelayedTensor::einsum('ij,kl->ikjl', darr1, darr2)
# Reshape
DelayedTensor::unfold(tmpdarr, row_idx=c(2,1), col_idx=c(4,3))
}
identical(as.array(DelayedTensor::kronecker(darr1, darr2)),
as.array(mykronecker(darr1, darr2)))
## [1] TRUE
## R version 4.1.1 (2021-08-10)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows Server x64 (build 17763)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=C
## [2] LC_CTYPE=English_United States.1252
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.1252
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] einsum_0.1.0 DelayedRandomArray_1.2.0 HDF5Array_1.22.0
## [4] rhdf5_2.38.0 DelayedArray_0.20.0 IRanges_2.28.0
## [7] S4Vectors_0.32.0 MatrixGenerics_1.6.0 matrixStats_0.61.0
## [10] BiocGenerics_0.40.0 Matrix_1.3-4 DelayedTensor_1.0.0
## [13] BiocStyle_2.22.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.7 rTensor_1.4.8 bslib_0.3.1
## [4] compiler_4.1.1 BiocManager_1.30.16 jquerylib_0.1.4
## [7] rhdf5filters_1.6.0 tools_4.1.1 digest_0.6.28
## [10] jsonlite_1.7.2 evaluate_0.14 lattice_0.20-45
## [13] rlang_0.4.12 parallel_4.1.1 yaml_2.2.1
## [16] xfun_0.27 fastmap_1.1.0 stringr_1.4.0
## [19] knitr_1.36 sass_0.4.0 grid_4.1.1
## [22] R6_2.5.1 BiocParallel_1.28.0 rmarkdown_2.11
## [25] bookdown_0.24 irlba_2.3.3 Rhdf5lib_1.16.0
## [28] magrittr_2.0.1 BiocSingular_1.10.0 htmltools_0.5.2
## [31] rsvd_1.0.5 beachmat_2.10.0 dqrng_0.3.0
## [34] ScaledMatrix_1.2.0 stringi_1.7.5