DelayedTensor 1.13.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2024-10-23 23:57:56.972841
Compiled: Wed Nov 6 18:43:16 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.9851323 0.1497661 0.9670422
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.9851323 0.1497661 0.9670422
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.0614672 0.6798181 0.7784841 0.02225511
## [2,] 0.1326773 0.4441795 0.3697049 0.31507386
## [3,] 0.7951754 0.1477860 0.8037269 0.80090120
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.06146720 0.67981811 0.77848413 0.02225511
## [2,] 0.13267725 0.44417954 0.36970493 0.31507386
## [3,] 0.79517536 0.14778600 0.80372692 0.80090120
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7100269 0.2144510 0.4030778 0.2158089
## [2,] 0.3354193 0.2037733 0.5299910 0.2064121
## [3,] 0.7104823 0.4015896 0.2276377 0.1828150
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5386648 0.5495145 0.08259149 0.4090459
## [2,] 0.5497737 0.3417914 0.65366228 0.6499776
## [3,] 0.6130117 0.7600684 0.82488232 0.4694288
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.18188086 0.8866689 0.1094820 0.8984752
## [2,] 0.92800980 0.9852258 0.1882847 0.6077945
## [3,] 0.09926882 0.5402041 0.2896238 0.5775135
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3660938 0.2860160 0.6900754 0.62443613
## [2,] 0.2211829 0.5913443 0.5889682 0.79933635
## [3,] 0.6355911 0.4297923 0.6635024 0.05181079
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5641839 0.7754928 0.6854470 0.1867309
## [2,] 0.1449283 0.5618209 0.3333982 0.2822650
## [3,] 0.6706336 0.8870397 0.3142768 0.5117592
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.7100269 0.2144510 0.4030778 0.2158089
## [2,] 0.3354193 0.2037733 0.5299910 0.2064121
## [3,] 0.7104823 0.4015896 0.2276377 0.1828150
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.53866478 0.54951449 0.08259149 0.40904588
## [2,] 0.54977371 0.34179142 0.65366228 0.64997756
## [3,] 0.61301170 0.76006836 0.82488232 0.46942877
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.18188086 0.88666891 0.10948203 0.89847520
## [2,] 0.92800980 0.98522576 0.18828470 0.60779448
## [3,] 0.09926882 0.54020408 0.28962378 0.57751352
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.36609375 0.28601599 0.69007537 0.62443613
## [2,] 0.22118295 0.59134434 0.58896817 0.79933635
## [3,] 0.63559112 0.42979227 0.66350236 0.05181079
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.5641839 0.7754928 0.6854470 0.1867309
## [2,] 0.1449283 0.5618209 0.3333982 0.2822650
## [3,] 0.6706336 0.8870397 0.3142768 0.5117592
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.7095499 0.4464874 0.2663946
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.7095499 0.4464874 0.2663946
einsum::einsum('iii->i', arrD)
## [1] 0.2821611 0.5126167 0.9120478
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.2821611 0.5126167 0.9120478
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.97048558 0.02242987 0.93517067
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.97048558 0.02242987 0.93517067
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.003778217 0.4621527 0.6060375 0.0004952898
## [2,] 0.017603254 0.1972955 0.1366817 0.0992715403
## [3,] 0.632303848 0.0218407 0.6459770 0.6414427388
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.0037782167 0.4621526577 0.6060375386 0.0004952898
## [2,] 0.0176032536 0.1972954677 0.1366817329 0.0992715403
## [3,] 0.6323038477 0.0218407017 0.6459769667 0.6414427388
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5041382 0.04598925 0.16247170 0.04657349
## [2,] 0.1125061 0.04152355 0.28089042 0.04260597
## [3,] 0.5047852 0.16127421 0.05181892 0.03342131
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2901597 0.3019662 0.006821354 0.1673185
## [2,] 0.3022511 0.1168214 0.427274379 0.4224708
## [3,] 0.3757833 0.5777039 0.680430834 0.2203634
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.033080647 0.7861818 0.01198631 0.8072577
## [2,] 0.861202187 0.9706698 0.03545113 0.3694141
## [3,] 0.009854298 0.2918204 0.08388194 0.3335219
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1340246 0.08180514 0.4762040 0.389920476
## [2,] 0.0489219 0.34968813 0.3468835 0.638938597
## [3,] 0.4039761 0.18472140 0.4402354 0.002684358
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.31830346 0.6013891 0.46983757 0.03486842
## [2,] 0.02100423 0.3156427 0.11115434 0.07967355
## [3,] 0.44974944 0.7868394 0.09876993 0.26189745
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.50413823 0.04598925 0.16247170 0.04657349
## [2,] 0.11250612 0.04152355 0.28089042 0.04260597
## [3,] 0.50478517 0.16127421 0.05181892 0.03342131
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.290159742 0.301966172 0.006821354 0.167318532
## [2,] 0.302251134 0.116821373 0.427274379 0.422470833
## [3,] 0.375783343 0.577703910 0.680430834 0.220363371
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.033080647 0.786181764 0.011986314 0.807257689
## [2,] 0.861202187 0.970669798 0.035451127 0.369414131
## [3,] 0.009854298 0.291820450 0.083881936 0.333521868
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.134024636 0.081805145 0.476204023 0.389920476
## [2,] 0.048921897 0.349688134 0.346883511 0.638938597
## [3,] 0.403976069 0.184721396 0.440235378 0.002684358
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.31830346 0.60138909 0.46983757 0.03486842
## [2,] 0.02100423 0.31564270 0.11115434 0.07967355
## [3,] 0.44974944 0.78683939 0.09876993 0.26189745
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.9704856 0.14753938 0.9526645
## [2,] 0.1475394 0.02242987 0.1448301
## [3,] 0.9526645 0.14483010 0.9351707
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.97048558 0.14753938 0.95266450
## [2,] 0.14753938 0.02242987 0.14483010
## [3,] 0.95266450 0.14483010 0.93517067
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04364337 0.4826892 0.5527447 0.01580173
## [2,] 0.09420442 0.3153794 0.2625005 0.22371093
## [3,] 0.56459591 0.1049320 0.5706678 0.56866142
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02061729 0.22802413 0.2611186 0.007464793
## [2,] 0.04450251 0.14898640 0.1240062 0.105681863
## [3,] 0.26671718 0.04957028 0.2695855 0.268637741
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04367136 0.4829988 0.5530992 0.01581186
## [2,] 0.09426485 0.3155817 0.2626688 0.22385442
## [3,] 0.56495805 0.1049993 0.5710338 0.56902617
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01318171 0.14578770 0.16694673 0.004772631
## [2,] 0.02845278 0.09525477 0.07928361 0.067567919
## [3,] 0.17052619 0.03169286 0.17236008 0.171754100
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01252537 0.13852876 0.15863426 0.004534996
## [2,] 0.02703608 0.09051192 0.07533598 0.064203631
## [3,] 0.16203548 0.03011484 0.16377806 0.163202257
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02468459 0.27300789 0.3126311 0.00893742
## [2,] 0.05328181 0.17837789 0.1484697 0.12653039
## [3,] 0.31933416 0.05934932 0.3227684 0.32163360
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02477606 0.27401958 0.3137897 0.00897054
## [2,] 0.05347925 0.17903891 0.1490198 0.12699928
## [3,] 0.32051752 0.05956925 0.3239645 0.32282548
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03257706 0.36029745 0.4125896 0.01179501
## [2,] 0.07031775 0.23541114 0.1959403 0.16698630
## [3,] 0.42143575 0.07832524 0.4259680 0.42447040
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01399225 0.15475222 0.17721232 0.005066101
## [2,] 0.03020234 0.10111200 0.08415877 0.071722685
## [3,] 0.18101188 0.03364166 0.18295854 0.182315296
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01326517 0.14671082 0.16800383 0.004802851
## [2,] 0.02863294 0.09585791 0.07978562 0.067995753
## [3,] 0.17160594 0.03189354 0.17345145 0.172841630
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01268758 0.14032271 0.16068858 0.004593725
## [2,] 0.02738620 0.09168405 0.07631159 0.065035072
## [3,] 0.16413385 0.03050483 0.16589900 0.165315735
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01123712 0.12428093 0.1423186 0.004068567
## [2,] 0.02425539 0.08120267 0.0675876 0.057600220
## [3,] 0.14536996 0.02701749 0.1469333 0.146416733
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03311022 0.36619407 0.4193420 0.01198804
## [2,] 0.07146856 0.23926388 0.1991470 0.16971919
## [3,] 0.42833296 0.07960711 0.4329394 0.43141727
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03379305 0.37374612 0.4279901 0.01223527
## [2,] 0.07294247 0.24419824 0.2032540 0.17321933
## [3,] 0.43716651 0.08124886 0.4418679 0.44031443
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03768011 0.41673645 0.4772199 0.01364264
## [2,] 0.08133271 0.27228726 0.2266334 0.19314397
## [3,] 0.48745180 0.09059455 0.4926940 0.49096181
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03377712 0.37356990 0.4277883 0.0122295
## [2,] 0.07290807 0.24408309 0.2031582 0.1731377
## [3,] 0.43696038 0.08121055 0.4416596 0.4401068
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02100896 0.23235599 0.2660792 0.007606605
## [2,] 0.04534795 0.15181676 0.1263620 0.107689543
## [3,] 0.27178411 0.05051199 0.2747070 0.273741158
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04671927 0.5167082 0.5917012 0.0169154
## [2,] 0.10084378 0.3376068 0.2810010 0.2394777
## [3,] 0.60438763 0.1123275 0.6108874 0.6087397
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.005076668 0.05614719 0.06429616 0.001838083
## [2,] 0.010958012 0.03668545 0.03053448 0.026022420
## [3,] 0.065674718 0.01220587 0.06638100 0.066147625
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04017879 0.44437145 0.5088657 0.01454732
## [2,] 0.08672612 0.29034341 0.2416622 0.20595190
## [3,] 0.51977614 0.09660213 0.5253660 0.52351891
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05070321 0.5607699 0.6421578 0.01835785
## [2,] 0.10944312 0.3663959 0.3049631 0.25989886
## [3,] 0.65592609 0.1219061 0.6629801 0.66064924
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02514291 0.27807680 0.3184357 0.00910336
## [2,] 0.05427108 0.18168981 0.1512263 0.12887967
## [3,] 0.32526320 0.06045125 0.3287612 0.32760534
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03995230 0.44186652 0.5059972 0.01446532
## [2,] 0.08623724 0.28870674 0.2402999 0.20479094
## [3,] 0.51684614 0.09605758 0.5224045 0.52056781
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02885447 0.3191262 0.3654428 0.01044719
## [2,] 0.06228252 0.2085107 0.1735501 0.14790474
## [3,] 0.37327819 0.0693750 0.3772925 0.37596607
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01117971 0.12364590 0.14159136 0.004047778
## [2,] 0.02413145 0.08078776 0.06724225 0.057305905
## [3,] 0.14462718 0.02687944 0.14618254 0.145668599
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05704216 0.6308779 0.7224409 0.02065296
## [2,] 0.12312579 0.4122030 0.3430898 0.29239163
## [3,] 0.73793052 0.1371469 0.7458665 0.74324417
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.006101776 0.06748474 0.07727920 0.002209238
## [2,] 0.013170714 0.04409318 0.03670017 0.031277010
## [3,] 0.078936118 0.01467054 0.07978502 0.079504516
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05450106 0.6027736 0.6902577 0.01973291
## [2,] 0.11764080 0.3938402 0.3278059 0.27936620
## [3,] 0.70505727 0.1310373 0.7126397 0.71013420
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06055907 0.6697743 0.7669826 0.02192631
## [2,] 0.13071705 0.4376171 0.3642428 0.31041889
## [3,] 0.78342725 0.1456026 0.7918525 0.78906850
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03320483 0.3672405 0.4205403 0.0120223
## [2,] 0.07167279 0.2399476 0.1997161 0.1702042
## [3,] 0.42955697 0.0798346 0.4341766 0.4326501
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.006729554 0.07442786 0.08523002 0.002436534
## [2,] 0.014525775 0.04862968 0.04047605 0.034494926
## [3,] 0.087057411 0.01617991 0.08799365 0.087684288
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01157333 0.12799935 0.14657665 0.004190296
## [2,] 0.02498110 0.08363221 0.06960978 0.059323587
## [3,] 0.14971935 0.02782584 0.15132948 0.150797440
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01780236 0.19689149 0.2254675 0.006445609
## [2,] 0.03842649 0.12864496 0.1070753 0.091252885
## [3,] 0.23030169 0.04280234 0.2327784 0.231960036
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05522676 0.6107997 0.6994487 0.01999566
## [2,] 0.11920722 0.3990843 0.3321707 0.28308605
## [3,] 0.71444534 0.1327821 0.7221287 0.71958987
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03735943 0.41318969 0.4731584 0.01352653
## [2,] 0.08064050 0.26996988 0.2247046 0.19150016
## [3,] 0.48330319 0.08982351 0.4885008 0.48678333
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03549814 0.39260415 0.4495851 0.01285263
## [2,] 0.07662291 0.25651969 0.2135096 0.18195942
## [3,] 0.45922452 0.08534841 0.4641632 0.46253128
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02250276 0.24887716 0.2849982 0.008147456
## [2,] 0.04857231 0.16261136 0.1353467 0.115346574
## [3,] 0.29110873 0.05410353 0.2942394 0.293204928
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01359550 0.15036417 0.17218741 0.00492245
## [2,] 0.02934595 0.09824494 0.08177243 0.06968897
## [3,] 0.17587923 0.03268774 0.17777069 0.17714569
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03906801 0.43208635 0.4947976 0.01414515
## [2,] 0.08432848 0.28231657 0.2349812 0.20025815
## [3,] 0.50540639 0.09393147 0.5108417 0.50904569
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01758060 0.19443885 0.2226589 0.006365317
## [2,] 0.03794782 0.12704245 0.1057415 0.090116162
## [3,] 0.22743286 0.04226916 0.2298787 0.229070548
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03634828 0.40200659 0.4603522 0.01316043
## [2,] 0.07845794 0.26266306 0.2186229 0.18631715
## [3,] 0.47022245 0.08739242 0.4752794 0.47360840
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02641813 0.29218057 0.3345865 0.009565073
## [2,] 0.05702366 0.19090493 0.1588963 0.135416312
## [3,] 0.34176022 0.06351728 0.3454356 0.344221147
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04241700 0.4691257 0.5372127 0.0153577
## [2,] 0.09155731 0.3065174 0.2551243 0.2174247
## [3,] 0.54873093 0.1019835 0.5546322 0.5526822
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03620222 0.40039123 0.4585024 0.01310755
## [2,] 0.07814268 0.26160762 0.2177444 0.18556848
## [3,] 0.46833298 0.08704125 0.4733696 0.47170532
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04078363 0.45106092 0.5165261 0.01476632
## [2,] 0.08803167 0.29471417 0.2453001 0.20905225
## [3,] 0.52760072 0.09805636 0.5332747 0.53139984
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03838234 0.42450299 0.4861136 0.01389689
## [2,] 0.08284847 0.27736175 0.2308571 0.19674350
## [3,] 0.49653622 0.09228292 0.5018761 0.50011165
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04913297 0.5434033 0.6222707 0.01778932
## [2,] 0.10605375 0.3550489 0.2955186 0.25184999
## [3,] 0.63561257 0.1181307 0.6424481 0.64018944
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.003184664 0.03522191 0.04033388 0.001153055
## [2,] 0.006874114 0.02301329 0.01915470 0.016324226
## [3,] 0.041198665 0.00765691 0.04164173 0.041495325
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03467880 0.38354242 0.4392082 0.01255597
## [2,] 0.07485437 0.25059894 0.2085816 0.17775960
## [3,] 0.44862512 0.08337848 0.4534498 0.45185556
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.00890834 0.09852492 0.11282442 0.003225396
## [2,] 0.01922870 0.06437421 0.05358072 0.045663135
## [3,] 0.11524345 0.02141838 0.11648282 0.116073289
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04122197 0.45590887 0.5220776 0.01492502
## [2,] 0.08897783 0.29788173 0.2479366 0.21129912
## [3,] 0.53327132 0.09911026 0.5390063 0.53711127
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04766737 0.527194 0.6037088 0.01725868
## [2,] 0.10289026 0.344458 0.2867035 0.24433752
## [3,] 0.61665277 0.114607 0.6232844 0.62109312
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03453356 0.38193600 0.4373686 0.01250338
## [2,] 0.07454085 0.24954934 0.2077079 0.17701507
## [3,] 0.44674612 0.08302926 0.4515506 0.44996302
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05452385 0.6030256 0.6905463 0.01974116
## [2,] 0.11768999 0.3940049 0.3279429 0.27948302
## [3,] 0.70535209 0.1310920 0.7129377 0.71043115
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04213251 0.4659793 0.5336096 0.0152547
## [2,] 0.09094322 0.3044615 0.2534131 0.2159664
## [3,] 0.54505055 0.1012995 0.5509122 0.5489753
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02049305 0.22665011 0.2595452 0.007419812
## [2,] 0.04423435 0.14808865 0.1232589 0.105045050
## [3,] 0.26511001 0.04927158 0.2679611 0.267018997
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01931772 0.21365108 0.2446595 0.006994265
## [2,] 0.04169739 0.13959534 0.1161897 0.099020418
## [3,] 0.24990520 0.04644572 0.2525928 0.251704698
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01147782 0.12694303 0.14536702 0.004155716
## [2,] 0.02477494 0.08294203 0.06903532 0.058834017
## [3,] 0.14848379 0.02759621 0.15008063 0.149552978
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01735004 0.19188888 0.2197388 0.006281839
## [2,] 0.03745015 0.12537635 0.1043548 0.088934333
## [3,] 0.22445019 0.04171482 0.2268640 0.226066401
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0314564 0.34790315 0.3983964 0.01138926
## [2,] 0.0678988 0.22731296 0.1891999 0.16124194
## [3,] 0.4069383 0.07563084 0.4113146 0.40986854
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.04364337 0.48268916 0.55274469 0.01580173
## [2,] 0.09420442 0.31537944 0.26250045 0.22371093
## [3,] 0.56459591 0.10493204 0.57066775 0.56866142
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.020617287 0.228024130 0.261118621 0.007464793
## [2,] 0.044502515 0.148986403 0.124006177 0.105681863
## [3,] 0.266717181 0.049570280 0.269585542 0.268637741
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.04367136 0.48299876 0.55309923 0.01581186
## [2,] 0.09426485 0.31558173 0.26266882 0.22385442
## [3,] 0.56495805 0.10499934 0.57103379 0.56902617
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.011477824 0.126943026 0.145367018 0.004155716
## [2,] 0.024774939 0.082942033 0.069035322 0.058834017
## [3,] 0.148483786 0.027596208 0.150080627 0.149552978
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.017350041 0.191888877 0.219738845 0.006281839
## [2,] 0.037450149 0.125376352 0.104354772 0.088934333
## [3,] 0.224450195 0.041714819 0.226864003 0.226066401
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.03145640 0.34790315 0.39839639 0.01138926
## [2,] 0.06789880 0.22731296 0.18919989 0.16124194
## [3,] 0.40693828 0.07563084 0.41131462 0.40986854
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.101941
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 2.101941
einsum::einsum('ij->', arrC)
## [1] 5.35125
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 5.35125
einsum::einsum('ijk->', arrE)
## [1] 28.94246
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 28.94246
einsum::einsum('ij->i', arrC)
## [1] 1.542025 1.261636 2.547589
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 1.542025 1.261636 2.547589
einsum::einsum('ij->j', arrC)
## [1] 0.9893198 1.2717837 1.9519160 1.1382302
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 0.9893198 1.2717837 1.9519160 1.1382302
einsum::einsum('ijk->i', arrE)
## [1] 9.378164 9.703360 9.860932
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 9.378164 9.703360 9.860932
einsum::einsum('ijk->j', arrE)
## [1] 7.269152 8.414793 6.584901 6.673610
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 7.269152 8.414793 6.584901 6.673610
einsum::einsum('ijk->k', arrE)
## [1] 4.341485 6.442413 6.292432 5.948150 5.917976
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 4.341485 6.442413 6.292432 5.948150 5.917976
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 2.360850 2.712143 1.970674 2.334497
## [2,] 2.179314 2.683956 2.294304 2.545786
## [3,] 2.728988 3.018694 2.319923 1.793327
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 2.360850 2.712143 1.970674 2.334497
## [2,] 2.179314 2.683956 2.294304 2.545786
## [3,] 2.728988 3.018694 2.319923 1.793327
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.7559286 1.701450 1.2091595 1.222868 1.3797458
## [2,] 0.8198139 1.651374 2.4120988 1.307153 2.2243534
## [3,] 1.1607064 1.561136 0.5873905 1.942546 1.3331220
## [4,] 0.6050360 1.528452 2.0837832 1.475583 0.9807551
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.7559286 1.7014502 1.2091595 1.2228678 1.3797458
## [2,] 0.8198139 1.6513743 2.4120988 1.3071526 2.2243534
## [3,] 1.1607064 1.5611361 0.5873905 1.9425459 1.3331220
## [4,] 0.6050360 1.5284522 2.0837832 1.4755833 0.9807551
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.7559286 1.701450 1.2091595 1.222868 1.3797458
## [2,] 0.8198139 1.651374 2.4120988 1.307153 2.2243534
## [3,] 1.1607064 1.561136 0.5873905 1.942546 1.3331220
## [4,] 0.6050360 1.528452 2.0837832 1.475583 0.9807551
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.7559286 1.7014502 1.2091595 1.2228678 1.3797458
## [2,] 0.8198139 1.6513743 2.4120988 1.3071526 2.2243534
## [3,] 1.1607064 1.5611361 0.5873905 1.9425459 1.3331220
## [4,] 0.6050360 1.5284522 2.0837832 1.4755833 0.9807551
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.422432
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 1.422432
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.7095499 0.4727958 0.4154021
## [2,] 0.3821141 0.4464874 0.9378907
## [3,] 0.7511664 0.5141717 0.2663946
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.7095499 0.4727958 0.4154021
## [2,] 0.3821141 0.4464874 0.9378907
## [3,] 0.7511664 0.5141717 0.2663946
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.28216114 0.7807293 0.3357125
## [2,] 0.25157811 0.4061634 0.5672971
## [3,] 0.02865098 0.7923412 0.5061237
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.29606792 0.5326338 0.2995126
## [2,] 0.05529904 0.5126167 0.4515257
## [3,] 0.06848292 0.6726771 0.8183801
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.25115577 0.019110472 0.4141820
## [2,] 0.24385317 0.402171827 0.8921622
## [3,] 0.03903611 0.001612106 0.9120478
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.28216114 0.78072930 0.33571245
## [2,] 0.25157811 0.40616341 0.56729709
## [3,] 0.02865098 0.79234122 0.50612368
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.29606792 0.53263380 0.29951262
## [2,] 0.05529904 0.51261674 0.45152574
## [3,] 0.06848292 0.67267714 0.81838008
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.251155774 0.019110472 0.414181953
## [2,] 0.243853167 0.402171827 0.892162239
## [3,] 0.039036110 0.001612106 0.912047814
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.928086
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.928086
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 3.46488
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 3.46488
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 17.51882
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 17.51882
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.1214295 0.9681942 0.9041371 0.5869226 0.7890571
## [2,] 0.2487870 0.9964915 2.0486720 0.6162147 1.7038712
## [3,] 0.4951810 1.1145266 0.1313194 1.2633229 0.6797618
## [4,] 0.1226008 0.8101527 1.5101937 1.0315434 0.3764394
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.1214295 0.9681942 0.9041371 0.5869226 0.7890571
## [2,] 0.2487870 0.9964915 2.0486720 0.6162147 1.7038712
## [3,] 0.4951810 1.1145266 0.1313194 1.2633229 0.6797618
## [4,] 0.1226008 0.8101527 1.5101937 1.0315434 0.3764394
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.0724637 0.604938 0.7928576
## [2,] 0.6049380 0.450852 0.7206300
## [3,] 0.7928576 0.720630 1.9415643
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.0724637 0.6049380 0.7928576
## [2,] 0.6049380 0.4508520 0.7206300
## [3,] 0.7928576 0.7206300 1.9415643
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.0037782167 0.01760325 0.6323038
## [2,] 0.4621526577 0.19729547 0.0218407
## [3,] 0.6060375386 0.13668173 0.6459770
## [4,] 0.0004952898 0.09927154 0.6414427
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.0037782167 0.0176032536 0.6323038477
## [2,] 0.4621526577 0.1972954677 0.0218407017
## [3,] 0.6060375386 0.1366817329 0.6459769667
## [4,] 0.0004952898 0.0992715403 0.6414427388
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.50413823 0.290159742 0.03308065 0.13402464 0.31830346
## [2,] 0.04598925 0.301966172 0.78618176 0.08180514 0.60138909
## [3,] 0.16247170 0.006821354 0.01198631 0.47620402 0.46983757
## [4,] 0.04657349 0.167318532 0.80725769 0.38992048 0.03486842
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.11250612 0.3022511 0.86120219 0.0489219 0.02100423
## [2,] 0.04152355 0.1168214 0.97066980 0.3496881 0.31564270
## [3,] 0.28089042 0.4272744 0.03545113 0.3468835 0.11115434
## [4,] 0.04260597 0.4224708 0.36941413 0.6389386 0.07967355
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.50478517 0.3757833 0.009854298 0.403976069 0.44974944
## [2,] 0.16127421 0.5777039 0.291820450 0.184721396 0.78683939
## [3,] 0.05181892 0.6804308 0.083881936 0.440235378 0.09876993
## [4,] 0.03342131 0.2203634 0.333521868 0.002684358 0.26189745
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.504138233 0.290159742 0.033080647 0.134024636 0.318303460
## [2,] 0.045989250 0.301966172 0.786181764 0.081805145 0.601389090
## [3,] 0.162471700 0.006821354 0.011986314 0.476204023 0.469837570
## [4,] 0.046573493 0.167318532 0.807257689 0.389920476 0.034868418
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.11250612 0.30225113 0.86120219 0.04892190 0.02100423
## [2,] 0.04152355 0.11682137 0.97066980 0.34968813 0.31564270
## [3,] 0.28089042 0.42727438 0.03545113 0.34688351 0.11115434
## [4,] 0.04260597 0.42247083 0.36941413 0.63893860 0.07967355
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.504785167 0.375783343 0.009854298 0.403976069 0.449749441
## [2,] 0.161274211 0.577703910 0.291820450 0.184721396 0.786839390
## [3,] 0.051818915 0.680430834 0.083881936 0.440235378 0.098769931
## [4,] 0.033421315 0.220363371 0.333521868 0.002684358 0.261897450
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 1.543365 1.275596 1.522525
## [2,] 1.579817 2.195205 2.667391
## [3,] 2.076507 2.709315 1.506610
## [4,] 1.966621 2.200832 1.780697
## [5,] 2.211855 1.322412 2.383709
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.543365 1.275596 1.522525
## [2,] 1.579817 2.195205 2.667391
## [3,] 2.076507 2.709315 1.506610
## [4,] 1.966621 2.200832 1.780697
## [5,] 2.211855 1.322412 2.383709
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,] 1.876424e-03 1.079987e-03 0.0001231276 0.0004988455 1.184739e-03
## [2,] 2.093805e-02 1.374796e-01 0.3579340081 0.0372443685 2.738013e-01
## [3,] 9.700001e-02 4.072534e-03 0.0071561550 0.2843067222 2.805058e-01
## [4,] 2.272442e-05 8.163906e-05 0.0003938820 0.0001902523 1.701321e-05
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0002966078 0.0007968458 0.0022704476 0.0001289762 5.537491e-05
## [2,] 0.0012269446 0.0034518572 0.0286815113 0.0103326427 9.326662e-03
## [3,] 0.0057499068 0.0087464282 0.0007256947 0.0071008042 2.275361e-03
## [4,] 0.0006334446 0.0062810883 0.0054922674 0.0094994244 1.184547e-03
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.308658221 0.22977818 0.006025554 0.247017034 0.27500583
## [2,] 0.003406253 0.01220162 0.006163505 0.003901479 0.01661874
## [3,] 0.032370603 0.42505630 0.052399955 0.275009320 0.06170029
## [4,] 0.020731316 0.13669189 0.206884354 0.001665113 0.16245557
DelayedTensor::einsum('i,ij,ijk,ijk,ji->jki',
darrA, darrC, darrE, darrE, t(darrC))
## <4 x 5 x 3> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.876424e-03 1.079987e-03 1.231276e-04 4.988455e-04 1.184739e-03
## [2,] 2.093805e-02 1.374796e-01 3.579340e-01 3.724437e-02 2.738013e-01
## [3,] 9.700001e-02 4.072534e-03 7.156155e-03 2.843067e-01 2.805058e-01
## [4,] 2.272442e-05 8.163906e-05 3.938820e-04 1.902523e-04 1.701321e-05
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.966078e-04 7.968458e-04 2.270448e-03 1.289762e-04 5.537491e-05
## [2,] 1.226945e-03 3.451857e-03 2.868151e-02 1.033264e-02 9.326662e-03
## [3,] 5.749907e-03 8.746428e-03 7.256947e-04 7.100804e-03 2.275361e-03
## [4,] 6.334446e-04 6.281088e-03 5.492267e-03 9.499424e-03 1.184547e-03
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.308658221 0.229778182 0.006025554 0.247017034 0.275005827
## [2,] 0.003406253 0.012201615 0.006163505 0.003901479 0.016618741
## [3,] 0.032370603 0.425056301 0.052399955 0.275009320 0.061700292
## [4,] 0.020731316 0.136691887 0.206884354 0.001665113 0.162455569
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 Under development (unstable) (2024-10-26 r87273 ucrt)
## Platform: x86_64-w64-mingw32/x64
## 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.2 DelayedRandomArray_1.15.0
## [3] HDF5Array_1.35.1 rhdf5_2.51.0
## [5] DelayedArray_0.33.1 SparseArray_1.7.1
## [7] S4Arrays_1.7.1 abind_1.4-8
## [9] IRanges_2.41.0 S4Vectors_0.45.0
## [11] MatrixGenerics_1.19.0 matrixStats_1.4.1
## [13] BiocGenerics_0.53.1 generics_0.1.3
## [15] Matrix_1.7-1 DelayedTensor_1.13.0
## [17] BiocStyle_2.35.0
##
## loaded via a namespace (and not attached):
## [1] dqrng_0.4.1 sass_0.4.9 lattice_0.22-6
## [4] digest_0.6.37 evaluate_1.0.1 grid_4.5.0
## [7] bookdown_0.41 fastmap_1.2.0 jsonlite_1.8.9
## [10] BiocManager_1.30.25 codetools_0.2-20 jquerylib_0.1.4
## [13] cli_3.6.3 rlang_1.1.4 crayon_1.5.3
## [16] XVector_0.47.0 cachem_1.1.0 yaml_2.3.10
## [19] tools_4.5.0 beachmat_2.23.0 parallel_4.5.0
## [22] BiocParallel_1.41.0 Rhdf5lib_1.29.0 rsvd_1.0.5
## [25] R6_2.5.1 lifecycle_1.0.4 zlibbioc_1.53.0
## [28] BiocSingular_1.23.0 irlba_2.3.5.1 ScaledMatrix_1.15.0
## [31] rTensor_1.4.8 bslib_0.8.0 Rcpp_1.0.13-1
## [34] xfun_0.49 knitr_1.48 rhdf5filters_1.19.0
## [37] htmltools_0.5.8.1 rmarkdown_2.29 compiler_4.5.0