DelayedTensor 1.14.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2025-04-15 15:16:31.230982
Compiled: Tue Apr 15 20:16:51 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.7000265 0.3063409 0.8865268
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.7000265 0.3063409 0.8865268
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.1722713 0.9316770 0.1648670 0.02340474
## [2,] 0.6082508 0.4752072 0.9381979 0.96048442
## [3,] 0.7538058 0.6784524 0.3517897 0.25389083
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.17227126 0.93167705 0.16486699 0.02340474
## [2,] 0.60825079 0.47520716 0.93819788 0.96048442
## [3,] 0.75380576 0.67845237 0.35178973 0.25389083
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9297689 0.3736735 0.6675812 0.35970213
## [2,] 0.5198990 0.2410921 0.2588948 0.09426775
## [3,] 0.5982098 0.1212930 0.1112052 0.76307358
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05884417 0.1218004 0.6415978 0.9291792
## [2,] 0.60626590 0.1031175 0.6766436 0.9334257
## [3,] 0.35035857 0.9214899 0.5019497 0.6592060
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1127712 0.7491061 0.3126280 0.1206069
## [2,] 0.5575035 0.5831368 0.9786325 0.3529743
## [3,] 0.6665795 0.8692642 0.3561776 0.6433665
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4225361 0.7399396 0.1438258 0.07372929
## [2,] 0.2829296 0.2895990 0.1451982 0.85618689
## [3,] 0.9296090 0.7914504 0.3859612 0.60520621
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5743971 0.4949474 0.95627117 0.8091491
## [2,] 0.6620919 0.4268673 0.27580273 0.3311678
## [3,] 0.6413120 0.8276866 0.07793336 0.3530467
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.92976889 0.37367349 0.66758115 0.35970213
## [2,] 0.51989904 0.24109209 0.25889481 0.09426775
## [3,] 0.59820984 0.12129300 0.11120519 0.76307358
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.05884417 0.12180037 0.64159777 0.92917924
## [2,] 0.60626590 0.10311753 0.67664360 0.93342570
## [3,] 0.35035857 0.92148992 0.50194965 0.65920595
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.1127712 0.7491061 0.3126280 0.1206069
## [2,] 0.5575035 0.5831368 0.9786325 0.3529743
## [3,] 0.6665795 0.8692642 0.3561776 0.6433665
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.42253614 0.73993958 0.14382577 0.07372929
## [2,] 0.28292957 0.28959904 0.14519817 0.85618689
## [3,] 0.92960901 0.79145044 0.38596117 0.60520621
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.57439715 0.49494741 0.95627117 0.80914913
## [2,] 0.66209194 0.42686726 0.27580273 0.33116785
## [3,] 0.64131204 0.82768661 0.07793336 0.35304672
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.74511534 0.07654687 0.05494365
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.74511534 0.07654687 0.05494365
einsum::einsum('iii->i', arrD)
## [1] 0.45739255 0.45837011 0.07799245
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.45739255 0.45837011 0.07799245
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.49003703 0.09384474 0.78592970
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.49003703 0.09384474 0.78592970
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.02967739 0.8680221 0.02718112 0.0005477817
## [2,] 0.36996902 0.2258218 0.88021527 0.9225303304
## [3,] 0.56822312 0.4602976 0.12375601 0.0644605544
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.0296773872 0.8680221185 0.0271811243 0.0005477817
## [2,] 0.3699690176 0.2258218449 0.8802152683 0.9225303304
## [3,] 0.5682231197 0.4602976202 0.1237560119 0.0644605544
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8644702 0.13963188 0.44566459 0.129385622
## [2,] 0.2702950 0.05812540 0.06702652 0.008886409
## [3,] 0.3578550 0.01471199 0.01236660 0.582281285
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.003462636 0.01483533 0.4116477 0.8633741
## [2,] 0.367558337 0.01063323 0.4578466 0.8712835
## [3,] 0.122751129 0.84914368 0.2519535 0.4345525
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01271735 0.5611600 0.09773628 0.01454603
## [2,] 0.31081012 0.3400486 0.95772148 0.12459088
## [3,] 0.44432821 0.7556202 0.12686251 0.41392043
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.17853679 0.5475106 0.02068585 0.005436008
## [2,] 0.08004914 0.0838676 0.02108251 0.733055988
## [3,] 0.86417291 0.6263938 0.14896602 0.366274562
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3299321 0.2449729 0.914454556 0.6547223
## [2,] 0.4383657 0.1822157 0.076067146 0.1096721
## [3,] 0.4112811 0.6850651 0.006073609 0.1246420
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.864470196 0.139631878 0.445664593 0.129385622
## [2,] 0.270295010 0.058125395 0.067026521 0.008886409
## [3,] 0.357855009 0.014711991 0.012366595 0.582281285
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.003462636 0.014835331 0.411647695 0.863374055
## [2,] 0.367558337 0.010633226 0.457846559 0.871283530
## [3,] 0.122751129 0.849143679 0.251953456 0.434552490
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.01271735 0.56115999 0.09773628 0.01454603
## [2,] 0.31081012 0.34004857 0.95772148 0.12459088
## [3,] 0.44432821 0.75562021 0.12686251 0.41392043
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.178536793 0.547510588 0.020685851 0.005436008
## [2,] 0.080049141 0.083867601 0.021082509 0.733055988
## [3,] 0.864172914 0.626393797 0.148966021 0.366274562
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.329932082 0.244972943 0.914454556 0.654722315
## [2,] 0.438365741 0.182215653 0.076067146 0.109672142
## [3,] 0.411281134 0.685065121 0.006073609 0.124641984
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.4900370 0.21444672 0.6205922
## [2,] 0.2144467 0.09384474 0.2715794
## [3,] 0.6205922 0.27157939 0.7859297
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.49003703 0.21444672 0.62059218
## [2,] 0.21444672 0.09384474 0.27157939
## [3,] 0.62059218 0.27157939 0.78592970
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1601725 0.8662443 0.1532882 0.0217610
## [2,] 0.5655327 0.4418328 0.8723072 0.8930285
## [3,] 0.7008651 0.6308039 0.3270831 0.2360598
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08956366 0.4843780 0.08571419 0.0121681
## [2,] 0.31622900 0.2470597 0.48776818 0.4993549
## [3,] 0.39190289 0.3527267 0.18289514 0.1319976
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1030544 0.5573384 0.09862506 0.01400094
## [2,] 0.3638616 0.2842736 0.56123920 0.57457123
## [3,] 0.4509340 0.4058569 0.21044408 0.15187999
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0643732 0.3481430 0.06160642 0.00874573
## [2,] 0.2272872 0.1775723 0.35057968 0.35890757
## [3,] 0.2816772 0.2535197 0.13145450 0.09487227
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04153324 0.2246200 0.03974813 0.005642697
## [2,] 0.14664445 0.1145687 0.22619209 0.231565196
## [3,] 0.18173660 0.1635695 0.08481372 0.061211071
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02089530 0.11300590 0.01999721 0.002838831
## [2,] 0.07377656 0.05763930 0.11379683 0.116500034
## [3,] 0.09143136 0.08229152 0.04266963 0.030795180
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1150050 0.6219700 0.1100621 0.01562456
## [2,] 0.4060568 0.3172393 0.6263232 0.64120130
## [3,] 0.5032265 0.4529220 0.2348482 0.16949273
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04460013 0.2412063 0.04268321 0.006059365
## [2,] 0.15747297 0.1230287 0.24289456 0.248664429
## [3,] 0.19515640 0.1756478 0.09107653 0.065731018
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01915746 0.10360733 0.01833407 0.002602728
## [2,] 0.06764065 0.05284550 0.10433248 0.106810857
## [3,] 0.08382712 0.07544743 0.03912084 0.028233979
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06196634 0.3351262 0.05930301 0.008418734
## [2,] 0.21878910 0.1709330 0.33747178 0.345488293
## [3,] 0.27114554 0.2440408 0.12653951 0.091325073
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01623962 0.08782710 0.01554164 0.002206312
## [2,] 0.05733843 0.04479671 0.08844180 0.090542707
## [3,] 0.07105957 0.06395618 0.03316243 0.023933718
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1314556 0.7109381 0.1258056 0.01785954
## [2,] 0.4641401 0.3626180 0.7159140 0.73292029
## [3,] 0.5752093 0.5177091 0.2684414 0.19373739
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01013716 0.05482376 0.009701461 0.001377232
## [2,] 0.03579201 0.02796317 0.055207475 0.056518908
## [3,] 0.04435707 0.03992297 0.020700774 0.014939995
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1044422 0.5648440 0.09995323 0.01418949
## [2,] 0.3687617 0.2881019 0.56879738 0.58230895
## [3,] 0.4570067 0.4113225 0.21327811 0.15392535
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06035671 0.3264210 0.05776256 0.00820005
## [2,] 0.21310588 0.1664929 0.32870567 0.33651395
## [3,] 0.26410231 0.2377016 0.12325255 0.08895283
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02098270 0.11347861 0.02008086 0.002850706
## [2,] 0.07408517 0.05788041 0.11427285 0.116987362
## [3,] 0.09181382 0.08263575 0.04284812 0.030923998
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01776419 0.09607224 0.01700068 0.002413439
## [2,] 0.06272132 0.04900219 0.09674465 0.099042786
## [3,] 0.07773059 0.06996034 0.03627569 0.026180597
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1587462 0.8585310 0.1519233 0.02156723
## [2,] 0.5604970 0.4378986 0.8645399 0.88507672
## [3,] 0.6946244 0.6251870 0.3241707 0.23395784
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1105289 0.5977619 0.1057783 0.01501643
## [2,] 0.3902523 0.3048919 0.6019457 0.61624466
## [3,] 0.4836401 0.4352935 0.2257075 0.16289579
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1165662 0.6304133 0.1115562 0.01583667
## [2,] 0.4115690 0.3215459 0.6348256 0.64990564
## [3,] 0.5100578 0.4590705 0.2380363 0.17179361
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0864715 0.4676550 0.08275493 0.0117480
## [2,] 0.3053113 0.2385301 0.47092810 0.4821148
## [3,] 0.3783725 0.3405489 0.17658073 0.1274404
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1600709 0.8656950 0.1531910 0.0217472
## [2,] 0.5651740 0.4415526 0.8717540 0.8924622
## [3,] 0.7004207 0.6304039 0.3268757 0.2359101
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1608024 0.8696513 0.1538911 0.02184658
## [2,] 0.5677569 0.4435706 0.8757380 0.89654084
## [3,] 0.7036217 0.6332849 0.3283696 0.23698823
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1135622 0.6141671 0.1086813 0.01542854
## [2,] 0.4009625 0.3132594 0.6184656 0.63315705
## [3,] 0.4969132 0.4472398 0.2319019 0.16736635
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01942724 0.10506636 0.01859225 0.002639381
## [2,] 0.06859318 0.05358969 0.10580172 0.108315002
## [3,] 0.08500760 0.07650990 0.03967176 0.028631579
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09604183 0.5194132 0.09191392 0.01304822
## [2,] 0.33910193 0.2649296 0.52304858 0.53547340
## [3,] 0.42024933 0.3782396 0.19612400 0.14154502
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1148325 0.6210368 0.1098970 0.01560112
## [2,] 0.4054475 0.3167633 0.6253835 0.64023921
## [3,] 0.5024715 0.4522424 0.2344958 0.16923842
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1290495 0.6979250 0.1235029 0.01753263
## [2,] 0.4556444 0.3559806 0.7028098 0.71950477
## [3,] 0.5646805 0.5082328 0.2635278 0.19019118
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1004577 0.5432952 0.09614002 0.01364816
## [2,] 0.3546934 0.2771108 0.54709775 0.56009385
## [3,] 0.4395719 0.3956306 0.20514155 0.14805310
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1497492 0.8098735 0.1433130 0.0203449
## [2,] 0.5287306 0.4130806 0.8155418 0.8349147
## [3,] 0.6552563 0.5897543 0.3057982 0.2206982
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05385682 0.2912684 0.05154204 0.007316977
## [2,] 0.19015624 0.1485631 0.29330695 0.300274346
## [3,] 0.23566080 0.2121032 0.10997933 0.079373389
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1685902 0.9117694 0.1613442 0.02290464
## [2,] 0.5952540 0.4650531 0.9181509 0.93996123
## [3,] 0.7376988 0.6639555 0.3442728 0.24846581
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06135917 0.3318425 0.05872193 0.008336244
## [2,] 0.21664533 0.1692582 0.33416511 0.342103074
## [3,] 0.26848875 0.2416496 0.12529963 0.090430237
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02077710 0.11236669 0.01988410 0.002822773
## [2,] 0.07335925 0.05731327 0.11315315 0.115841059
## [3,] 0.09091418 0.08182604 0.04242827 0.030620989
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06080733 0.3288581 0.05819381 0.008261271
## [2,] 0.21469691 0.1677359 0.33115977 0.339026343
## [3,] 0.26607408 0.2394763 0.12417274 0.089616945
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1108336 0.5994098 0.1060699 0.01505782
## [2,] 0.3913282 0.3057324 0.6036051 0.61794349
## [3,] 0.4849734 0.4364935 0.2263297 0.16334485
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07279083 0.3936672 0.06966226 0.009889348
## [2,] 0.25700794 0.2007922 0.39642252 0.405839385
## [3,] 0.31851018 0.2866706 0.14864387 0.107278053
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04874063 0.2635990 0.04664575 0.006621892
## [2,] 0.17209213 0.1344502 0.26544392 0.271749444
## [3,] 0.21327394 0.1919542 0.09953172 0.071833223
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1601449 0.8660954 0.1532618 0.02175725
## [2,] 0.5654354 0.4417569 0.8721572 0.89287498
## [3,] 0.7007446 0.6306954 0.3270269 0.23601921
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1274703 0.6893847 0.1219916 0.01731809
## [2,] 0.4500688 0.3516246 0.6942098 0.71070045
## [3,] 0.5577707 0.5020138 0.2603031 0.18786388
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04988959 0.2698128 0.04774532 0.006777989
## [2,] 0.17614884 0.1376195 0.27170120 0.278155363
## [3,] 0.21830142 0.1964792 0.10187797 0.073526540
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1363442 0.7373762 0.1304841 0.01852369
## [2,] 0.4814004 0.3761029 0.7425371 0.76017582
## [3,] 0.5965999 0.5369614 0.2784241 0.20094201
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02477705 0.13399916 0.02371212 0.003366204
## [2,] 0.08748213 0.06834703 0.13493703 0.138142407
## [3,] 0.10841669 0.09757893 0.05059643 0.036516043
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02501347 0.13527780 0.02393839 0.003398325
## [2,] 0.08831690 0.06899921 0.13622462 0.139460582
## [3,] 0.10945122 0.09851004 0.05107923 0.036864485
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06649002 0.3595912 0.06363226 0.00903332
## [2,] 0.23476118 0.1834115 0.36210795 0.37070969
## [3,] 0.29093975 0.2618563 0.13577717 0.09799200
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01270144 0.06869188 0.01215553 0.001725615
## [2,] 0.04484590 0.03503669 0.06917266 0.070815832
## [3,] 0.05557756 0.05002181 0.02593721 0.018719190
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1474964 0.7976897 0.1411570 0.02003883
## [2,] 0.5207763 0.4068661 0.8032727 0.82235417
## [3,] 0.6453986 0.5808820 0.3011978 0.21737800
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1042596 0.5638567 0.09977853 0.01416469
## [2,] 0.3681172 0.2875983 0.56780319 0.58129114
## [3,] 0.4562079 0.4106036 0.21290533 0.15365631
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09895212 0.5351526 0.09469913 0.01344361
## [2,] 0.34937752 0.2729576 0.53889819 0.55169951
## [3,] 0.43298388 0.3897011 0.20206702 0.14583417
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1140594 0.6168559 0.1091571 0.01549609
## [2,] 0.4027179 0.3146308 0.6211733 0.63592900
## [3,] 0.4990887 0.4491978 0.2329171 0.16809907
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1104796 0.5974957 0.1057312 0.01500974
## [2,] 0.3900786 0.3047561 0.6016776 0.61597023
## [3,] 0.4834247 0.4350997 0.2256070 0.16282325
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08526522 0.4611311 0.08160049 0.01158411
## [2,] 0.30105215 0.2352026 0.46435862 0.47538928
## [3,] 0.37309421 0.3357982 0.17411742 0.12566261
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07353696 0.3977024 0.07037632 0.009990716
## [2,] 0.25964234 0.2028504 0.40048596 0.409999350
## [3,] 0.32177499 0.2896091 0.15016752 0.108377682
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1425866 0.7711366 0.1364582 0.01937179
## [2,] 0.5034410 0.3933226 0.7765338 0.79498010
## [3,] 0.6239149 0.5615459 0.2911716 0.21014204
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1647380 0.8909359 0.1576575 0.02238128
## [2,] 0.5816527 0.4544269 0.8971716 0.91848357
## [3,] 0.7208427 0.6487844 0.3364064 0.24278848
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04751288 0.2569591 0.04547077 0.00645509
## [2,] 0.16775723 0.1310634 0.25875754 0.26490423
## [3,] 0.20790169 0.1871190 0.09702457 0.07002378
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01342568 0.07260873 0.01284864 0.00182401
## [2,] 0.04740303 0.03703449 0.07311692 0.07485378
## [3,] 0.05874662 0.05287408 0.02741616 0.01978657
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1393931 0.7538657 0.1334020 0.01893792
## [2,] 0.4921656 0.3845135 0.7591420 0.77717514
## [3,] 0.6099413 0.5489691 0.2846504 0.20543555
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0570507 0.3085415 0.05459865 0.007750896
## [2,] 0.2014331 0.1573733 0.31070097 0.318081558
## [3,] 0.2496362 0.2246816 0.11650145 0.084080480
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0608198 0.3289255 0.05820575 0.008262966
## [2,] 0.2147409 0.1677703 0.33122768 0.339095872
## [3,] 0.2661286 0.2395254 0.12419821 0.089635324
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.1601725 0.8662443 0.1532882 0.0217610
## [2,] 0.5655327 0.4418328 0.8723072 0.8930285
## [3,] 0.7008651 0.6308039 0.3270831 0.2360598
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.08956366 0.48437800 0.08571419 0.01216810
## [2,] 0.31622900 0.24705975 0.48776818 0.49935493
## [3,] 0.39190289 0.35272674 0.18289514 0.13199760
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.10305436 0.55733837 0.09862506 0.01400094
## [2,] 0.36386160 0.28427360 0.56123920 0.57457123
## [3,] 0.45093402 0.40585688 0.21044408 0.15187999
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.13939314 0.75386567 0.13340198 0.01893792
## [2,] 0.49216559 0.38451346 0.75914200 0.77717514
## [3,] 0.60994127 0.54896915 0.28465035 0.20543555
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.057050702 0.308541480 0.054598646 0.007750896
## [2,] 0.201433102 0.157373331 0.310700972 0.318081558
## [3,] 0.249636229 0.224681610 0.116501446 0.084080480
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.060819803 0.328925522 0.058205749 0.008262966
## [2,] 0.214740942 0.167770327 0.331227682 0.339095872
## [3,] 0.266128647 0.239525382 0.124198208 0.089635324
If there is a vanishing subscript on the left or right side of ->, the summation is done for that subscript.
einsum::einsum('i->', arrA)
## [1] 1.892894
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.892894
einsum::einsum('ij->', arrC)
## [1] 6.312299
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 6.312299
einsum::einsum('ijk->', arrE)
## [1] 29.94213
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 29.94213
einsum::einsum('ij->i', arrC)
## [1] 1.292220 2.982140 2.037939
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 1.292220 2.982140 2.037939
einsum::einsum('ij->j', arrC)
## [1] 1.534328 2.085337 1.454855 1.237780
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 1.534328 2.085337 1.454855 1.237780
einsum::einsum('ijk->i', arrE)
## [1] 9.592055 9.175697 11.174379
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 9.592055 9.175697 11.174379
einsum::einsum('ijk->j', arrE)
## [1] 7.913076 7.654464 6.490303 7.884288
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 7.913076 7.654464 6.490303 7.884288
einsum::einsum('ijk->k', arrE)
## [1] 5.038661 6.503878 6.302747 5.666171 6.430673
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 5.038661 6.503878 6.302747 5.666171 6.430673
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 2.098318 2.479467 2.721904 2.292367
## [2,] 2.628690 1.643813 2.335172 2.568023
## [3,] 3.186069 3.531184 1.433227 3.023899
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 2.098318 2.479467 2.721904 2.292367
## [2,] 2.628690 1.643813 2.335172 2.568023
## [3,] 3.186069 3.531184 1.433227 3.023899
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.0478778 1.015469 1.336854 1.6350747 1.877801
## [2,] 0.7360586 1.146408 2.201507 1.8209891 1.749501
## [3,] 1.0376812 1.820191 1.647438 0.6749851 1.310007
## [4,] 1.2170435 2.521811 1.116948 1.5351224 1.493364
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.0478778 1.0154686 1.3368542 1.6350747 1.8778011
## [2,] 0.7360586 1.1464078 2.2015071 1.8209891 1.7495013
## [3,] 1.0376812 1.8201910 1.6474381 0.6749851 1.3100073
## [4,] 1.2170435 2.5218109 1.1169477 1.5351224 1.4933637
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.0478778 1.015469 1.336854 1.6350747 1.877801
## [2,] 0.7360586 1.146408 2.201507 1.8209891 1.749501
## [3,] 1.0376812 1.820191 1.647438 0.6749851 1.310007
## [4,] 1.2170435 2.521811 1.116948 1.5351224 1.493364
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.0478778 1.0154686 1.3368542 1.6350747 1.8778011
## [2,] 0.7360586 1.1464078 2.2015071 1.8209891 1.7495013
## [3,] 1.0376812 1.8201910 1.6474381 0.6749851 1.3100073
## [4,] 1.2170435 2.5218109 1.1169477 1.5351224 1.4933637
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 0.8766059
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 0.8766059
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.7451153 0.59934093 0.97666186
## [2,] 0.4895017 0.07654687 0.18397788
## [3,] 0.8212056 0.79047355 0.05494365
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.74511534 0.59934093 0.97666186
## [2,] 0.48950173 0.07654687 0.18397788
## [3,] 0.82120561 0.79047355 0.05494365
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.4573925 0.04461771 0.05917342
## [2,] 0.7795232 0.61665870 0.19514209
## [3,] 0.9359094 0.34519856 0.49182815
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.1316491 0.8543659 0.001163944
## [2,] 0.7277067 0.4583701 0.296997315
## [3,] 0.9384553 0.9586015 0.052851541
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.33044857 0.3802437 0.33498475
## [2,] 0.62685219 0.4902621 0.22125107
## [3,] 0.02380987 0.4493388 0.07799245
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.45739255 0.04461771 0.05917342
## [2,] 0.77952316 0.61665870 0.19514209
## [3,] 0.93590941 0.34519856 0.49182815
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.131649144 0.854365934 0.001163944
## [2,] 0.727706695 0.458370112 0.296997315
## [3,] 0.938455312 0.958601456 0.052851541
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.33044857 0.38024371 0.33498475
## [2,] 0.62685219 0.49026210 0.22125107
## [3,] 0.02380987 0.44933884 0.07799245
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.369811
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.369811
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 4.540702
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 4.540702
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 19.6233
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 19.6233
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.4926202 0.4937721 0.7678557 1.1227588 1.1795790
## [2,] 0.2124693 0.8746122 1.6568288 1.2577720 1.1122537
## [3,] 0.5250577 1.1214477 1.1823203 0.1907344 0.9965953
## [4,] 0.7205533 2.1692101 0.5530573 1.1047666 0.8890364
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.4926202 0.4937721 0.7678557 1.1227588 1.1795790
## [2,] 0.2124693 0.8746122 1.6568288 1.2577720 1.1122537
## [3,] 0.5250577 1.1214477 1.1823203 0.1907344 0.9965953
## [4,] 0.7205533 2.1692101 0.5530573 1.1047666 0.8890364
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 0.9254284 0.7246815 0.8258983
## [2,] 0.7246815 2.3985365 1.3548149
## [3,] 0.8258983 1.3548149 1.2167373
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.9254284 0.7246815 0.8258983
## [2,] 0.7246815 2.3985365 1.3548149
## [3,] 0.8258983 1.3548149 1.2167373
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.0296773872 0.3699690 0.56822312
## [2,] 0.8680221185 0.2258218 0.46029762
## [3,] 0.0271811243 0.8802153 0.12375601
## [4,] 0.0005477817 0.9225303 0.06446055
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.0296773872 0.3699690176 0.5682231197
## [2,] 0.8680221185 0.2258218449 0.4602976202
## [3,] 0.0271811243 0.8802152683 0.1237560119
## [4,] 0.0005477817 0.9225303304 0.0644605544
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.8644702 0.003462636 0.01271735 0.178536793 0.3299321
## [2,] 0.1396319 0.014835331 0.56115999 0.547510588 0.2449729
## [3,] 0.4456646 0.411647695 0.09773628 0.020685851 0.9144546
## [4,] 0.1293856 0.863374055 0.01454603 0.005436008 0.6547223
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.270295010 0.36755834 0.3108101 0.08004914 0.43836574
## [2,] 0.058125395 0.01063323 0.3400486 0.08386760 0.18221565
## [3,] 0.067026521 0.45784656 0.9577215 0.02108251 0.07606715
## [4,] 0.008886409 0.87128353 0.1245909 0.73305599 0.10967214
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.35785501 0.1227511 0.4443282 0.8641729 0.411281134
## [2,] 0.01471199 0.8491437 0.7556202 0.6263938 0.685065121
## [3,] 0.01236660 0.2519535 0.1268625 0.1489660 0.006073609
## [4,] 0.58228128 0.4345525 0.4139204 0.3662746 0.124641984
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.864470196 0.003462636 0.012717348 0.178536793 0.329932082
## [2,] 0.139631878 0.014835331 0.561159986 0.547510588 0.244972943
## [3,] 0.445664593 0.411647695 0.097736280 0.020685851 0.914454556
## [4,] 0.129385622 0.863374055 0.014546027 0.005436008 0.654722315
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.270295010 0.367558337 0.310810124 0.080049141 0.438365741
## [2,] 0.058125395 0.010633226 0.340048573 0.083867601 0.182215653
## [3,] 0.067026521 0.457846559 0.957721479 0.021082509 0.076067146
## [4,] 0.008886409 0.871283530 0.124590875 0.733055988 0.109672142
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.357855009 0.122751129 0.444328210 0.864172914 0.411281134
## [2,] 0.014711991 0.849143679 0.755620208 0.626393797 0.685065121
## [3,] 0.012366595 0.251953456 0.126862510 0.148966021 0.006073609
## [4,] 0.582281285 0.434552490 0.413920435 0.366274562 0.124641984
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 2.330726 1.114154 1.593782
## [2,] 1.751422 2.319453 2.433004
## [3,] 1.295112 2.472247 2.535388
## [4,] 1.380031 1.573914 2.712227
## [5,] 2.834765 1.695930 1.899979
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 2.330726 1.114154 1.593782
## [2,] 1.751422 2.319453 2.433004
## [3,] 1.295112 2.472247 2.535388
## [4,] 1.380031 1.573914 2.712227
## [5,] 2.834765 1.695930 1.899979
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.795933e-02 7.193612e-05 2.642024e-04 3.709094e-03 0.0068543245
## [2,] 8.484570e-02 9.014518e-03 3.409824e-01 3.326885e-01 0.1488549784
## [3,] 8.479886e-03 7.832629e-03 1.859678e-03 3.936001e-04 0.0173997896
## [4,] 4.961443e-05 3.310709e-04 5.577844e-06 2.084501e-06 0.0002510609
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.030634327 0.0416578253 0.03522617 0.009072500 0.04968290
## [2,] 0.004021026 0.0007355902 0.02352404 0.005801832 0.01260540
## [3,] 0.018073428 0.1234564578 0.25824569 0.005684813 0.02051120
## [4,] 0.002511377 0.2462323547 0.03521047 0.207168041 0.03099431
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.180267672 0.06183527 0.22382812 0.43532279 0.207180815
## [2,] 0.006003466 0.34650680 0.30834304 0.25561011 0.279551892
## [3,] 0.001356776 0.02764257 0.01391847 0.01634351 0.000666354
## [4,] 0.033275050 0.02483294 0.02365390 0.02093113 0.007122792
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.795933e-02 7.193612e-05 2.642024e-04 3.709094e-03 6.854325e-03
## [2,] 8.484570e-02 9.014518e-03 3.409824e-01 3.326885e-01 1.488550e-01
## [3,] 8.479886e-03 7.832629e-03 1.859678e-03 3.936001e-04 1.739979e-02
## [4,] 4.961443e-05 3.310709e-04 5.577844e-06 2.084501e-06 2.510609e-04
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0306343271 0.0416578253 0.0352261738 0.0090725003 0.0496828982
## [2,] 0.0040210255 0.0007355902 0.0235240378 0.0058018318 0.0126053989
## [3,] 0.0180734281 0.1234564578 0.2582456920 0.0056848126 0.0205111958
## [4,] 0.0025113770 0.2462323547 0.0352104723 0.2071680410 0.0309943076
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.180267672 0.061835267 0.223828115 0.435322786 0.207180815
## [2,] 0.006003466 0.346506799 0.308343035 0.255610110 0.279551892
## [3,] 0.001356776 0.027642574 0.013918469 0.016343512 0.000666354
## [4,] 0.033275050 0.024832939 0.023653900 0.020931129 0.007122792
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.5.0 RC (2025-04-04 r88126 ucrt)
## Platform: x86_64-w64-mingw32/x64
## Running under: Windows Server 2022 x64 (build 20348)
##
## Matrix products: default
## LAPACK version 3.12.1
##
## 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.16.0
## [3] HDF5Array_1.36.0 h5mread_1.0.0
## [5] rhdf5_2.52.0 DelayedArray_0.34.0
## [7] SparseArray_1.8.0 S4Arrays_1.8.0
## [9] abind_1.4-8 IRanges_2.42.0
## [11] S4Vectors_0.46.0 MatrixGenerics_1.20.0
## [13] matrixStats_1.5.0 BiocGenerics_0.54.0
## [15] generics_0.1.3 Matrix_1.7-3
## [17] DelayedTensor_1.14.0 BiocStyle_2.36.0
##
## loaded via a namespace (and not attached):
## [1] dqrng_0.4.1 sass_0.4.10 lattice_0.22-7
## [4] digest_0.6.37 evaluate_1.0.3 grid_4.5.0
## [7] bookdown_0.43 fastmap_1.2.0 jsonlite_2.0.0
## [10] BiocManager_1.30.25 codetools_0.2-20 jquerylib_0.1.4
## [13] cli_3.6.4 rlang_1.1.6 crayon_1.5.3
## [16] XVector_0.48.0 cachem_1.1.0 yaml_2.3.10
## [19] tools_4.5.0 beachmat_2.24.0 parallel_4.5.0
## [22] BiocParallel_1.42.0 Rhdf5lib_1.30.0 rsvd_1.0.5
## [25] R6_2.6.1 lifecycle_1.0.4 BiocSingular_1.24.0
## [28] irlba_2.3.5.1 ScaledMatrix_1.16.0 rTensor_1.4.8
## [31] bslib_0.9.0 Rcpp_1.0.14 xfun_0.52
## [34] knitr_1.50 rhdf5filters_1.20.0 htmltools_0.5.8.1
## [37] rmarkdown_2.29 compiler_4.5.0