DelayedTensor 1.13.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2024-10-23 23:57:56.972841
Compiled: Tue Mar 25 18:37:40 2025
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.3085233 0.4164991 0.2443954
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.3085233 0.4164991 0.2443954
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.7616324 0.6518101 0.8213295 0.2944553
## [2,] 0.9778903 0.7823836 0.4005236 0.8550405
## [3,] 0.4937298 0.3988139 0.2382483 0.6972323
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.7616324 0.6518101 0.8213295 0.2944553
## [2,] 0.9778903 0.7823836 0.4005236 0.8550405
## [3,] 0.4937298 0.3988139 0.2382483 0.6972323
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1418374 0.8754345 0.2099754 0.8642629
## [2,] 0.3427501 0.8723879 0.8898313 0.8875893
## [3,] 0.4940102 0.6928967 0.8283540 0.9034283
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.883959677 0.5804780 0.95877022 0.9515198
## [2,] 0.007946567 0.8121993 0.70353472 0.9416167
## [3,] 0.828841161 0.5663200 0.05428478 0.5492731
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7514968 0.7529246 0.7477714 0.78082356
## [2,] 0.9554800 0.7270957 0.4012727 0.55908049
## [3,] 0.5305489 0.3198219 0.1377269 0.09058866
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5562354 0.9096894 0.3160785 0.3520404
## [2,] 0.5558451 0.8996205 0.1329182 0.4922695
## [3,] 0.9238278 0.3360771 0.3601853 0.7996746
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.005039524 0.30132673 0.6933755 0.7958445
## [2,] 0.326277955 0.09277142 0.4333548 0.9490494
## [3,] 0.193328713 0.10357921 0.6769028 0.8904571
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.1418374 0.8754345 0.2099754 0.8642629
## [2,] 0.3427501 0.8723879 0.8898313 0.8875893
## [3,] 0.4940102 0.6928967 0.8283540 0.9034283
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.883959677 0.580477979 0.958770215 0.951519753
## [2,] 0.007946567 0.812199342 0.703534725 0.941616728
## [3,] 0.828841161 0.566320017 0.054284783 0.549273139
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.75149675 0.75292463 0.74777137 0.78082356
## [2,] 0.95548004 0.72709572 0.40127268 0.55908049
## [3,] 0.53054887 0.31982190 0.13772685 0.09058866
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.5562354 0.9096894 0.3160785 0.3520404
## [2,] 0.5558451 0.8996205 0.1329182 0.4922695
## [3,] 0.9238278 0.3360771 0.3601853 0.7996746
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.005039524 0.301326734 0.693375494 0.795844475
## [2,] 0.326277955 0.092771419 0.433354821 0.949049430
## [3,] 0.193328713 0.103579212 0.676902766 0.890457121
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.4693892 0.1432994 0.4142779
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.4693892 0.1432994 0.4142779
einsum::einsum('iii->i', arrD)
## [1] 0.63904635 0.62109737 0.00403083
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.63904635 0.62109737 0.00403083
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.09518664 0.17347150 0.05972912
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.09518664 0.17347150 0.05972912
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.5800839 0.4248564 0.67458207 0.0867039
## [2,] 0.9562695 0.6121241 0.16041914 0.7310943
## [3,] 0.2437691 0.1590525 0.05676223 0.4861329
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.58008388 0.42485643 0.67458207 0.08670390
## [2,] 0.95626947 0.61212414 0.16041914 0.73109429
## [3,] 0.24376909 0.15905254 0.05676223 0.48613291
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02011785 0.7663856 0.04408967 0.7469504
## [2,] 0.11747760 0.7610607 0.79179981 0.7878149
## [3,] 0.24404607 0.4801058 0.68617041 0.8161827
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 7.813847e-01 0.3369547 0.919240326 0.9053898
## [2,] 6.314793e-05 0.6596678 0.494961109 0.8866421
## [3,] 6.869777e-01 0.3207184 0.002946838 0.3017010
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5647474 0.5668955 0.55916203 0.609685431
## [2,] 0.9129421 0.5286682 0.16101976 0.312570996
## [3,] 0.2814821 0.1022860 0.01896869 0.008206305
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3093978 0.8275347 0.09990564 0.1239324
## [2,] 0.3089637 0.8093171 0.01766726 0.2423292
## [3,] 0.8534578 0.1129478 0.12973347 0.6394795
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0000253968 0.090797801 0.4807696 0.6333684
## [2,] 0.1064573039 0.008606536 0.1877964 0.9006948
## [3,] 0.0373759914 0.010728653 0.4581974 0.7929139
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.02011785 0.76638563 0.04408967 0.74695038
## [2,] 0.11747760 0.76106072 0.79179981 0.78781485
## [3,] 0.24404607 0.48010577 0.68617041 0.81618269
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 7.813847e-01 3.369547e-01 9.192403e-01 9.053898e-01
## [2,] 6.314793e-05 6.596678e-01 4.949611e-01 8.866421e-01
## [3,] 6.869777e-01 3.207184e-01 2.946838e-03 3.017010e-01
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.564747371 0.566895494 0.559162028 0.609685431
## [2,] 0.912942110 0.528668179 0.161019763 0.312570996
## [3,] 0.281482103 0.102286047 0.018968686 0.008206305
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.30939779 0.82753474 0.09990564 0.12393243
## [2,] 0.30896374 0.80931711 0.01766726 0.24232923
## [3,] 0.85345780 0.11294779 0.12973347 0.63947952
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.0000253968 0.0907978009 0.4807695755 0.6333684288
## [2,] 0.1064573039 0.0086065362 0.1877964011 0.9006948196
## [3,] 0.0373759914 0.0107286532 0.4581973548 0.7929138843
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.09518664 0.1284997 0.07540168
## [2,] 0.12849968 0.1734715 0.10179047
## [3,] 0.07540168 0.1017905 0.05972912
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.09518664 0.12849968 0.07540168
## [2,] 0.12849968 0.17347150 0.10179047
## [3,] 0.07540168 0.10179047 0.05972912
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10802797 0.09245106 0.11649525 0.04176477
## [2,] 0.13870144 0.11097127 0.05680923 0.12127674
## [3,] 0.07002936 0.05656674 0.03379252 0.09889363
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2610495 0.2234080 0.28151072 0.1009246
## [2,] 0.3351720 0.2681620 0.13727948 0.2930652
## [3,] 0.1692259 0.1366935 0.08165961 0.2389764
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3762542 0.3220008 0.4057451 0.1454639
## [2,] 0.4830878 0.3865055 0.1978627 0.4223987
## [3,] 0.2439075 0.1970181 0.1176971 0.3444399
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6667593 0.5706171 0.7190202 0.2577763
## [2,] 0.8560790 0.6849256 0.3506322 0.7485320
## [3,] 0.4322281 0.3491355 0.2085708 0.6103813
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6644389 0.5686313 0.7165179 0.2568792
## [2,] 0.8530997 0.6825420 0.3494119 0.7459270
## [3,] 0.4307239 0.3479205 0.2078449 0.6082571
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5277325 0.4516370 0.5690964 0.2040271
## [2,] 0.6775769 0.5421110 0.2775214 0.5924547
## [3,] 0.3421037 0.2763368 0.1650814 0.4831099
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1599241 0.13686409 0.17245898 0.06182836
## [2,] 0.2053329 0.16428132 0.08410010 0.17953748
## [3,] 0.1036711 0.08374111 0.05002627 0.14640164
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6777244 0.5800011 0.7308447 0.2620155
## [2,] 0.8701575 0.6961895 0.3563984 0.7608419
## [3,] 0.4393362 0.3548771 0.2120008 0.6204192
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6309013 0.5399295 0.6803516 0.2439132
## [2,] 0.8100394 0.6480906 0.3317753 0.7082763
## [3,] 0.4089831 0.3303591 0.1973539 0.5775552
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6582506 0.5633353 0.7098446 0.2544868
## [2,] 0.8451543 0.6761852 0.3461577 0.7389798
## [3,] 0.4267123 0.3446801 0.2059091 0.6025920
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6760168 0.5785397 0.7290033 0.2613553
## [2,] 0.8679650 0.6944354 0.3555005 0.7589249
## [3,] 0.4382293 0.3539830 0.2114666 0.6188560
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6880802 0.5888637 0.7420123 0.2660192
## [2,] 0.8834538 0.7068275 0.3618443 0.7724678
## [3,] 0.4460495 0.3602998 0.2152402 0.6298994
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6732523 0.5761739 0.7260221 0.2602866
## [2,] 0.8644156 0.6915956 0.3540467 0.7558213
## [3,] 0.4364372 0.3525354 0.2106019 0.6163253
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.006052363 0.005179653 0.006526749 0.002339908
## [2,] 0.007770871 0.006217264 0.003182787 0.006794637
## [3,] 0.003923457 0.003169202 0.001893256 0.005540603
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6312723 0.5402471 0.6807517 0.2440566
## [2,] 0.8105157 0.6484718 0.3319704 0.7086928
## [3,] 0.4092236 0.3305534 0.1974700 0.5778948
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4421108 0.3783614 0.4767637 0.1709248
## [2,] 0.5676438 0.4541565 0.2324951 0.4963322
## [3,] 0.2865993 0.2315027 0.1382979 0.4047280
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6185973 0.5293997 0.6670832 0.2391564
## [2,] 0.7942419 0.6354515 0.3253050 0.6944633
## [3,] 0.4010070 0.3239164 0.1935051 0.5662916
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4313277 0.3691331 0.4651353 0.1667559
## [2,] 0.5537989 0.4430795 0.2268245 0.4842266
## [3,] 0.2796091 0.2258563 0.1349248 0.3948566
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7302304 0.6249361 0.7874662 0.2823149
## [2,] 0.9375721 0.7501261 0.3840101 0.8197874
## [3,] 0.4733734 0.3823709 0.2284253 0.6684856
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5358348 0.4585710 0.5778338 0.2071595
## [2,] 0.6879798 0.5504340 0.2817822 0.6015507
## [3,] 0.3473560 0.2805794 0.1676159 0.4905271
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04134505 0.03538337 0.04458569 0.01598444
## [2,] 0.05308456 0.04247153 0.02174234 0.04641569
## [3,] 0.02680201 0.02164953 0.01293326 0.03784911
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7247083 0.6202102 0.7815112 0.2801800
## [2,] 0.9304820 0.7444535 0.3811061 0.8135879
## [3,] 0.4697936 0.3794793 0.2266979 0.6634303
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7171658 0.6137553 0.7733776 0.2772640
## [2,] 0.9207979 0.7367055 0.3771397 0.8051205
## [3,] 0.4649042 0.3755299 0.2243385 0.6565256
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4183442 0.3580218 0.4511342 0.1617364
## [2,] 0.5371289 0.4297423 0.2199968 0.4696508
## [3,] 0.2711925 0.2190578 0.1308634 0.3829710
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5723643 0.4898332 0.6172264 0.2212822
## [2,] 0.7348814 0.5879588 0.3009922 0.6425602
## [3,] 0.3710363 0.2997074 0.1790428 0.5239678
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7277245 0.6227916 0.7847639 0.2813461
## [2,] 0.9343547 0.7475519 0.3826923 0.8169742
## [3,] 0.4717489 0.3810587 0.2276415 0.6661916
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4040832 0.3458171 0.4357554 0.1562229
## [2,] 0.5188186 0.4150927 0.2124973 0.4536408
## [3,] 0.2619478 0.2115903 0.1264023 0.3699158
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5734518 0.4907639 0.6183992 0.2217026
## [2,] 0.7362777 0.5890759 0.3015641 0.6437811
## [3,] 0.3717413 0.3002768 0.1793830 0.5249634
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5537796 0.4739283 0.5971851 0.2140972
## [2,] 0.7110199 0.5688678 0.2912190 0.6216963
## [3,] 0.3589888 0.2899759 0.1732293 0.5069546
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2435867 0.2084631 0.26267915 0.09417324
## [2,] 0.3127507 0.2502234 0.12809621 0.27346068
## [3,] 0.1579056 0.1275494 0.07619701 0.22299016
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5695269 0.4874049 0.6141667 0.2201852
## [2,] 0.7312384 0.5850441 0.2995001 0.6393748
## [3,] 0.3691970 0.2982216 0.1781552 0.5213704
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3056223 0.2615536 0.32957707 0.1181568
## [2,] 0.3924007 0.3139492 0.16071917 0.3431044
## [3,] 0.1981203 0.1600331 0.09560252 0.2797803
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10489723 0.08977176 0.11311912 0.04055439
## [2,] 0.13468176 0.10775523 0.05516285 0.11776204
## [3,] 0.06799985 0.05492739 0.03281318 0.09602761
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5947005 0.5089487 0.6413134 0.2299176
## [2,] 0.7635598 0.6109036 0.3127382 0.6676358
## [3,] 0.3855158 0.3114033 0.1860299 0.5444154
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4258138 0.3644143 0.4591893 0.1646242
## [2,] 0.5467194 0.4374154 0.2239249 0.4780365
## [3,] 0.2760347 0.2229691 0.1332000 0.3898090
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06899525 0.05904660 0.07440313 0.02667431
## [2,] 0.08858577 0.07087508 0.03628289 0.07745697
## [3,] 0.04472632 0.03612802 0.02158259 0.06316134
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4236469 0.3625598 0.4568525 0.1637864
## [2,] 0.5439372 0.4351894 0.2227854 0.4756038
## [3,] 0.2746300 0.2218344 0.1325221 0.3878253
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4233496 0.3623054 0.4565319 0.1636715
## [2,] 0.5435555 0.4348841 0.2226291 0.4752701
## [3,] 0.2744373 0.2216788 0.1324291 0.3875531
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7036172 0.6021603 0.7587670 0.2720259
## [2,] 0.9034023 0.7227877 0.3700148 0.7899102
## [3,] 0.4561213 0.3684354 0.2201004 0.6441226
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6928489 0.5929447 0.7471547 0.2678628
## [2,] 0.8895764 0.7117261 0.3643520 0.7778213
## [3,] 0.4491407 0.3627968 0.2167319 0.6342648
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6851801 0.5863818 0.7388848 0.2648980
## [2,] 0.8797302 0.7038484 0.3603192 0.7692120
## [3,] 0.4441694 0.3587812 0.2143330 0.6272445
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2559672 0.2190584 0.27602999 0.09895966
## [2,] 0.3286465 0.2629412 0.13460679 0.28735950
## [3,] 0.1659313 0.1340322 0.08006978 0.23432379
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2407357 0.2060232 0.25960462 0.09307099
## [2,] 0.3090901 0.2472947 0.12659691 0.27025996
## [3,] 0.1560574 0.1260565 0.07530516 0.22038017
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10123483 0.08663745 0.10916966 0.03913847
## [2,] 0.12997945 0.10399305 0.05323689 0.11365048
## [3,] 0.06562569 0.05300964 0.03166754 0.09267489
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2743288 0.2347724 0.29583082 0.1060585
## [2,] 0.3522217 0.2818031 0.14426272 0.3079730
## [3,] 0.1778342 0.1436469 0.08581353 0.2511329
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2681254 0.2294635 0.28914114 0.1036601
## [2,] 0.3442569 0.2754306 0.14100048 0.3010088
## [3,] 0.1738128 0.1403986 0.08387301 0.2454539
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3749284 0.3208662 0.4043154 0.1449513
## [2,] 0.4813855 0.3851436 0.1971655 0.4209103
## [3,] 0.2430481 0.1963239 0.1172823 0.3432262
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6090581 0.5212360 0.6567963 0.2354684
## [2,] 0.7819941 0.6256523 0.3202885 0.6837542
## [3,] 0.3948232 0.3189214 0.1905211 0.5575590
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.003838265 0.003284813 0.004139109 0.001483914
## [2,] 0.004928102 0.003942841 0.002018448 0.004308997
## [3,] 0.002488163 0.002009832 0.001200658 0.003513719
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2485039 0.2126713 0.26798169 0.09607426
## [2,] 0.3190641 0.2552745 0.13068201 0.27898087
## [3,] 0.1610931 0.1301242 0.07773516 0.22749154
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.14724541 0.12601361 0.15878657 0.05692665
## [2,] 0.18905428 0.15125722 0.07743271 0.16530388
## [3,] 0.09545214 0.07710218 0.04606023 0.13479503
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2295002 0.1964078 0.24748852 0.08872724
## [2,] 0.2946645 0.2357531 0.12068846 0.25764657
## [3,] 0.1487740 0.1201733 0.07179057 0.21009474
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07065772 0.06046935 0.07619590 0.02731703
## [2,] 0.09072027 0.07258284 0.03715714 0.07932332
## [3,] 0.04580401 0.03699853 0.02210263 0.06468323
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07888928 0.06751398 0.08507266 0.03049944
## [2,] 0.10128911 0.08103868 0.04148592 0.08856442
## [3,] 0.05114014 0.04130883 0.02467757 0.07221877
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5280972 0.4519492 0.5694897 0.2041681
## [2,] 0.6780452 0.5424856 0.2777132 0.5928641
## [3,] 0.3423401 0.2765278 0.1651955 0.4834438
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3300571 0.2824651 0.3559271 0.1276036
## [2,] 0.4237735 0.3390497 0.1735688 0.3705359
## [3,] 0.2139602 0.1728279 0.1032460 0.3021490
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5155511 0.4412121 0.5559602 0.1993176
## [2,] 0.6619367 0.5295976 0.2711155 0.5787793
## [3,] 0.3342071 0.2699582 0.1612709 0.4719585
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6061409 0.5187395 0.6536505 0.2343406
## [2,] 0.7782486 0.6226557 0.3187545 0.6804793
## [3,] 0.3929321 0.3173939 0.1896086 0.5548885
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7228268 0.6186000 0.7794822 0.2794526
## [2,] 0.9280662 0.7425207 0.3801167 0.8114757
## [3,] 0.4685740 0.3784941 0.2261094 0.6617079
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6782010 0.5804090 0.7313587 0.2621998
## [2,] 0.8707694 0.6966791 0.3566491 0.7613769
## [3,] 0.4396452 0.3551267 0.2121499 0.6208555
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.10802797 0.09245106 0.11649525 0.04176477
## [2,] 0.13870144 0.11097127 0.05680923 0.12127674
## [3,] 0.07002936 0.05656674 0.03379252 0.09889363
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.26104954 0.22340796 0.28151072 0.10092456
## [2,] 0.33517197 0.26816204 0.13727948 0.29306519
## [3,] 0.16922591 0.13669350 0.08165961 0.23897642
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.3762542 0.3220008 0.4057451 0.1454639
## [2,] 0.4830878 0.3865055 0.1978627 0.4223987
## [3,] 0.2439075 0.1970181 0.1176971 0.3444399
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.6061409 0.5187395 0.6536505 0.2343406
## [2,] 0.7782486 0.6226557 0.3187545 0.6804793
## [3,] 0.3929321 0.3173939 0.1896086 0.5548885
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.7228268 0.6186000 0.7794822 0.2794526
## [2,] 0.9280662 0.7425207 0.3801167 0.8114757
## [3,] 0.4685740 0.3784941 0.2261094 0.6617079
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.6782010 0.5804090 0.7313587 0.2621998
## [2,] 0.8707694 0.6966791 0.3566491 0.7613769
## [3,] 0.4396452 0.3551267 0.2121499 0.6208555
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.9694178
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 0.9694178
einsum::einsum('ij->', arrC)
## [1] 7.37309
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 7.37309
einsum::einsum('ijk->', arrE)
## [1] 34.6919
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 34.6919
einsum::einsum('ij->i', arrC)
## [1] 2.529227 3.015838 1.828024
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 2.529227 3.015838 1.828024
einsum::einsum('ij->j', arrC)
## [1] 2.233252 1.833008 1.460101 1.846728
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 2.233252 1.833008 1.460101 1.846728
einsum::einsum('ijk->i', arrE)
## [1] 12.42888 11.98289 10.28013
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 12.42888 11.98289 10.28013
einsum::einsum('ijk->j', arrE)
## [1] 7.497425 8.842623 7.544337 10.807518
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 7.497425 8.842623 7.544337 10.807518
einsum::einsum('ijk->k', arrE)
## [1] 8.002758 7.838744 6.754632 6.634462 5.461308
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 8.002758 7.838744 6.754632 6.634462 5.461308
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 2.338569 3.419853 2.925971 3.744491
## [2,] 2.188300 3.404075 2.560912 3.829605
## [3,] 2.970557 2.018695 2.057454 3.233422
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 2.338569 3.419853 2.925971 3.744491
## [2,] 2.188300 3.404075 2.560912 3.829605
## [3,] 2.970557 2.018695 2.057454 3.233422
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9785977 1.720747 2.237526 2.0359082 0.5246462
## [2,] 2.4407191 1.958997 1.799842 2.1453870 0.4976774
## [3,] 1.9281608 1.716590 1.286771 0.8091821 1.8036331
## [4,] 2.6552806 2.442410 1.430493 1.6439845 2.6353510
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9785977 1.7207474 2.2375257 2.0359082 0.5246462
## [2,] 2.4407191 1.9589973 1.7998422 2.1453870 0.4976774
## [3,] 1.9281608 1.7165897 1.2867709 0.8091821 1.8036331
## [4,] 2.6552806 2.4424096 1.4304927 1.6439845 2.6353510
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9785977 1.720747 2.237526 2.0359082 0.5246462
## [2,] 2.4407191 1.958997 1.799842 2.1453870 0.4976774
## [3,] 1.9281608 1.716590 1.286771 0.8091821 1.8036331
## [4,] 2.6552806 2.442410 1.430493 1.6439845 2.6353510
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9785977 1.7207474 2.2375257 2.0359082 0.5246462
## [2,] 2.4407191 1.9589973 1.7998422 2.1453870 0.4976774
## [3,] 1.9281608 1.7165897 1.2867709 0.8091821 1.8036331
## [4,] 2.6552806 2.4424096 1.4304927 1.6439845 2.6353510
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.026967
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 1.026967
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.4693892 0.2118158 0.7335155
## [2,] 0.3430617 0.1432994 0.2342094
## [3,] 0.9242616 0.3352425 0.4142779
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.4693892 0.2118158 0.7335155
## [2,] 0.3430617 0.1432994 0.2342094
## [3,] 0.9242616 0.3352425 0.4142779
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.6390463 0.2500874 0.98735857
## [2,] 0.8897520 0.0911920 0.85130824
## [3,] 0.2849080 0.5417269 0.04900848
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.3356785 0.2026746 0.6485664
## [2,] 0.4234698 0.6210974 0.2549093
## [3,] 0.4618686 0.4495679 0.2699456
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.4483141 0.8046339 0.20173547
## [2,] 0.5439669 0.7720983 0.18001195
## [3,] 0.9003115 0.2458243 0.00403083
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.63904635 0.25008744 0.98735857
## [2,] 0.88975199 0.09119200 0.85130824
## [3,] 0.28490799 0.54172687 0.04900848
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.3356785 0.2026746 0.6485664
## [2,] 0.4234698 0.6210974 0.2549093
## [3,] 0.4618686 0.4495679 0.2699456
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.44831413 0.80463390 0.20173547
## [2,] 0.54396692 0.77209834 0.18001195
## [3,] 0.90031153 0.24582429 0.00403083
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.3283873
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 0.3283873
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 5.17185
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 5.17185
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 25.36788
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 25.36788
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.3816415 1.468426 1.7591716 1.4718193 0.1438587
## [2,] 2.0075521 1.317341 1.1978497 1.7497996 0.1101330
## [3,] 1.5220599 1.417148 0.7391505 0.2473064 1.1267633
## [4,] 2.3509479 2.093733 0.9304627 1.0057412 2.3269771
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.3816415 1.4684255 1.7591716 1.4718193 0.1438587
## [2,] 2.0075521 1.3173408 1.1978497 1.7497996 0.1101330
## [3,] 1.5220599 1.4171483 0.7391505 0.2473064 1.1267633
## [4,] 2.3509479 2.0937329 0.9304627 1.0057412 2.3269771
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.766226 1.835491 1.0369756
## [2,] 1.835491 2.459907 1.4864250
## [3,] 1.036976 1.486425 0.9457168
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.7662263 1.8354915 1.0369756
## [2,] 1.8354915 2.4599070 1.4864250
## [3,] 1.0369756 1.4864250 0.9457168
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.5800839 0.9562695 0.24376909
## [2,] 0.4248564 0.6121241 0.15905254
## [3,] 0.6745821 0.1604191 0.05676223
## [4,] 0.0867039 0.7310943 0.48613291
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.58008388 0.95626947 0.24376909
## [2,] 0.42485643 0.61212414 0.15905254
## [3,] 0.67458207 0.16041914 0.05676223
## [4,] 0.08670390 0.73109429 0.48613291
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.02011785 0.7813847 0.5647474 0.30939779 0.0000253968
## [2,] 0.76638563 0.3369547 0.5668955 0.82753474 0.0907978009
## [3,] 0.04408967 0.9192403 0.5591620 0.09990564 0.4807695755
## [4,] 0.74695038 0.9053898 0.6096854 0.12393243 0.6333684288
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.1174776 6.314793e-05 0.9129421 0.30896374 0.106457304
## [2,] 0.7610607 6.596678e-01 0.5286682 0.80931711 0.008606536
## [3,] 0.7917998 4.949611e-01 0.1610198 0.01766726 0.187796401
## [4,] 0.7878149 8.866421e-01 0.3125710 0.24232923 0.900694820
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.2440461 0.686977669 0.281482103 0.8534578 0.03737599
## [2,] 0.4801058 0.320718361 0.102286047 0.1129478 0.01072865
## [3,] 0.6861704 0.002946838 0.018968686 0.1297335 0.45819735
## [4,] 0.8161827 0.301700982 0.008206305 0.6394795 0.79291388
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.0201178537 0.7813847111 0.5647473715 0.3093977933 0.0000253968
## [2,] 0.7663856338 0.3369546836 0.5668954942 0.8275347403 0.0907978009
## [3,] 0.0440896689 0.9192403257 0.5591620280 0.0999056443 0.4807695755
## [4,] 0.7469503830 0.9053898405 0.6096854311 0.1239324349 0.6333684288
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.174776e-01 6.314793e-05 9.129421e-01 3.089637e-01 1.064573e-01
## [2,] 7.610607e-01 6.596678e-01 5.286682e-01 8.093171e-01 8.606536e-03
## [3,] 7.917998e-01 4.949611e-01 1.610198e-01 1.766726e-02 1.877964e-01
## [4,] 7.878149e-01 8.866421e-01 3.125710e-01 2.423292e-01 9.006948e-01
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.244046070 0.686977669 0.281482103 0.853457796 0.037375991
## [2,] 0.480105772 0.320718361 0.102286047 0.112947790 0.010728653
## [3,] 0.686170413 0.002946838 0.018968686 0.129733470 0.458197355
## [4,] 0.816182693 0.301700982 0.008206305 0.639479520 0.792913884
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 2.091510 2.992559 2.918689
## [2,] 3.374728 2.465297 1.998719
## [3,] 3.033016 2.642929 1.078686
## [4,] 2.134044 2.080653 2.419765
## [5,] 1.795586 1.801454 1.864268
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 2.091510 2.992559 2.918689
## [2,] 3.374728 2.465297 1.998719
## [3,] 3.033016 2.642929 1.078686
## [4,] 2.134044 2.080653 2.419765
## [5,] 1.795586 1.801454 1.864268
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.003600480 0.13984395 0.10107250 0.055372738 4.545250e-06
## [2,] 0.100456383 0.04416738 0.07430759 0.108471692 1.190160e-02
## [3,] 0.009176131 0.19131625 0.11637520 0.020792792 1.000598e-01
## [4,] 0.019981052 0.02421934 0.01630919 0.003315214 1.694272e-02
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.04678961 2.515089e-05 0.36361149 0.123055738 0.042400387
## [2,] 0.19403178 1.681817e-01 0.13478350 0.206334707 0.002194229
## [3,] 0.05290365 3.307054e-02 0.01075844 0.001180428 0.012547508
## [4,] 0.23988971 2.699826e-01 0.09517790 0.073789277 0.274261673
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.014539301 0.0409274156 0.0167695917 0.050845644 0.0022267139
## [2,] 0.018662534 0.0124668721 0.0039760338 0.004390474 0.0004170411
## [3,] 0.009518851 0.0000408798 0.0002631418 0.001799718 0.0063563107
## [4,] 0.096969568 0.0358446878 0.0009749800 0.075975702 0.0942050319
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,] 3.600480e-03 1.398440e-01 1.010725e-01 5.537274e-02 4.545250e-06
## [2,] 1.004564e-01 4.416738e-02 7.430759e-02 1.084717e-01 1.190160e-02
## [3,] 9.176131e-03 1.913162e-01 1.163752e-01 2.079279e-02 1.000598e-01
## [4,] 1.998105e-02 2.421934e-02 1.630919e-02 3.315214e-03 1.694272e-02
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 4.678961e-02 2.515089e-05 3.636115e-01 1.230557e-01 4.240039e-02
## [2,] 1.940318e-01 1.681817e-01 1.347835e-01 2.063347e-01 2.194229e-03
## [3,] 5.290365e-02 3.307054e-02 1.075844e-02 1.180428e-03 1.254751e-02
## [4,] 2.398897e-01 2.699826e-01 9.517790e-02 7.378928e-02 2.742617e-01
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0145393007 0.0409274156 0.0167695917 0.0508456438 0.0022267139
## [2,] 0.0186625337 0.0124668721 0.0039760338 0.0043904741 0.0004170411
## [3,] 0.0095188509 0.0000408798 0.0002631418 0.0017997185 0.0063563107
## [4,] 0.0969695678 0.0358446878 0.0009749800 0.0759757015 0.0942050319
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) (2025-03-01 r87860 ucrt)
## Platform: x86_64-w64-mingw32/x64
## Running under: Windows Server 2022 x64 (build 20348)
##
## Matrix products: default
## LAPACK version 3.12.0
##
## 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.16 h5mread_0.99.4
## [5] rhdf5_2.51.2 DelayedArray_0.33.6
## [7] SparseArray_1.7.7 S4Arrays_1.7.3
## [9] abind_1.4-8 IRanges_2.41.3
## [11] S4Vectors_0.45.4 MatrixGenerics_1.19.1
## [13] matrixStats_1.5.0 BiocGenerics_0.53.6
## [15] generics_0.1.3 Matrix_1.7-3
## [17] DelayedTensor_1.13.0 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.3 grid_4.5.0
## [7] bookdown_0.42 fastmap_1.2.0 jsonlite_1.9.1
## [10] BiocManager_1.30.25 codetools_0.2-20 jquerylib_0.1.4
## [13] cli_3.6.4 rlang_1.1.5 crayon_1.5.3
## [16] XVector_0.47.2 cachem_1.1.0 yaml_2.3.10
## [19] tools_4.5.0 beachmat_2.23.7 parallel_4.5.0
## [22] BiocParallel_1.41.2 Rhdf5lib_1.29.2 rsvd_1.0.5
## [25] R6_2.6.1 lifecycle_1.0.4 BiocSingular_1.23.0
## [28] irlba_2.3.5.1 ScaledMatrix_1.15.0 rTensor_1.4.8
## [31] bslib_0.9.0 Rcpp_1.0.14 xfun_0.51
## [34] knitr_1.50 rhdf5filters_1.19.2 htmltools_0.5.8.1
## [37] rmarkdown_2.29 compiler_4.5.0