DelayedTensor 1.8.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2023-10-24 14:41:21
Compiled: Tue Oct 24 16:57:14 2023
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.3880172 0.5278172 0.8955139
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.3880172 0.5278172 0.8955139
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.4860284 0.6518048 0.51442556 0.6337556
## [2,] 0.9028319 0.7814250 0.30941652 0.6534854
## [3,] 0.3537483 0.5793294 0.05806083 0.3733330
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.48602845 0.65180479 0.51442556 0.63375564
## [2,] 0.90283194 0.78142503 0.30941652 0.65348536
## [3,] 0.35374828 0.57932944 0.05806083 0.37333299
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.97441739 0.08059897 0.19255127 0.5218899
## [2,] 0.04868524 0.51432656 0.83034809 0.7761702
## [3,] 0.22297032 0.17100466 0.08137172 0.9719478
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9723747 0.03505762 0.8702701 0.1005181
## [2,] 0.2256747 0.31718923 0.3290716 0.6327068
## [3,] 0.3704025 0.50091662 0.9444762 0.2810320
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9912590 0.92940705 0.5991456 0.21937241
## [2,] 0.5605280 0.02157234 0.3950221 0.08276508
## [3,] 0.2477153 0.90201938 0.2311404 0.74504982
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04434657 0.19215173 0.6503045 0.1199316
## [2,] 0.41798267 0.60028156 0.6333511 0.9762120
## [3,] 0.62098810 0.07677616 0.9064887 0.1610940
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1277008 0.99237708 0.5926154 0.1228993
## [2,] 0.3818637 0.06693985 0.7659646 0.5661232
## [3,] 0.8244584 0.19343263 0.6158740 0.2798348
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.97441739 0.08059897 0.19255127 0.52188995
## [2,] 0.04868524 0.51432656 0.83034809 0.77617023
## [3,] 0.22297032 0.17100466 0.08137172 0.97194777
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.97237470 0.03505762 0.87027015 0.10051805
## [2,] 0.22567468 0.31718923 0.32907162 0.63270677
## [3,] 0.37040248 0.50091662 0.94447621 0.28103196
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.99125897 0.92940705 0.59914556 0.21937241
## [2,] 0.56052802 0.02157234 0.39502207 0.08276508
## [3,] 0.24771530 0.90201938 0.23114044 0.74504982
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.04434657 0.19215173 0.65030454 0.11993157
## [2,] 0.41798267 0.60028156 0.63335111 0.97621202
## [3,] 0.62098810 0.07677616 0.90648873 0.16109398
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.12770077 0.99237708 0.59261544 0.12289933
## [2,] 0.38186372 0.06693985 0.76596462 0.56612320
## [3,] 0.82445840 0.19343263 0.61587398 0.27983477
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.6391501 0.5771017 0.7006888
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.6391501 0.5771017 0.7006888
einsum::einsum('iii->i', arrD)
## [1] 0.8145132 0.2757450 0.7253453
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.8145132 0.2757450 0.7253453
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.1505573 0.2785910 0.8019451
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.1505573 0.2785910 0.8019451
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.2362237 0.4248495 0.26463365 0.4016462
## [2,] 0.8151055 0.6106251 0.09573859 0.4270431
## [3,] 0.1251378 0.3356226 0.00337106 0.1393775
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.23622365 0.42484948 0.26463365 0.40164622
## [2,] 0.81510552 0.61062508 0.09573859 0.42704311
## [3,] 0.12513784 0.33562260 0.00337106 0.13937752
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.949489252 0.006496194 0.037075991 0.2723691
## [2,] 0.002370253 0.264531810 0.689477956 0.6024402
## [3,] 0.049715762 0.029242593 0.006621356 0.9446825
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.94551256 0.001229037 0.7573701 0.01010388
## [2,] 0.05092906 0.100609005 0.1082881 0.40031786
## [3,] 0.13719799 0.250917458 0.8920353 0.07897896
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.98259434 0.8637974562 0.3589754 0.048124253
## [2,] 0.31419166 0.0004653658 0.1560424 0.006850059
## [3,] 0.06136287 0.8136389667 0.0534259 0.555099233
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.001966619 0.036922289 0.4228960 0.01438358
## [2,] 0.174709515 0.360337948 0.4011336 0.95298991
## [3,] 0.385626218 0.005894578 0.8217218 0.02595127
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01630749 0.984812275 0.3511931 0.01510425
## [2,] 0.14581990 0.004480944 0.5867018 0.32049548
## [3,] 0.67973166 0.037416182 0.3793008 0.07830750
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.949489252 0.006496194 0.037075991 0.272369115
## [2,] 0.002370253 0.264531810 0.689477956 0.602440226
## [3,] 0.049715762 0.029242593 0.006621356 0.944682473
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.945512561 0.001229037 0.757370134 0.010103879
## [2,] 0.050929062 0.100609005 0.108288131 0.400317856
## [3,] 0.137197994 0.250917458 0.892035316 0.078978964
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.9825943364 0.8637974562 0.3589754027 0.0481242529
## [2,] 0.3141916577 0.0004653658 0.1560424371 0.0068500589
## [3,] 0.0613628690 0.8136389667 0.0534259027 0.5550992330
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.001966619 0.036922289 0.422895991 0.014383581
## [2,] 0.174709515 0.360337948 0.401133625 0.952989909
## [3,] 0.385626218 0.005894578 0.821721813 0.025951270
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.016307487 0.984812275 0.351193060 0.015104246
## [2,] 0.145819904 0.004480944 0.586701802 0.320495479
## [3,] 0.679731659 0.037416182 0.379300762 0.078307496
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.1505573 0.2048021 0.3474748
## [2,] 0.2048021 0.2785910 0.4726677
## [3,] 0.3474748 0.4726677 0.8019451
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.1505573 0.2048021 0.3474748
## [2,] 0.2048021 0.2785910 0.4726677
## [3,] 0.3474748 0.4726677 0.8019451
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4735946 0.6351299 0.50126521 0.6175425
## [2,] 0.8797351 0.7614341 0.30150084 0.6367675
## [3,] 0.3446985 0.5645087 0.05657549 0.3637822
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02366241 0.03173327 0.025044932 0.03085455
## [2,] 0.04395459 0.03804387 0.015064018 0.03181509
## [3,] 0.01722232 0.02820479 0.002826706 0.01817581
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10836992 0.1453331 0.11470163 0.14130870
## [2,] 0.20130472 0.1742346 0.06899070 0.14570784
## [3,] 0.07887536 0.1291733 0.01294584 0.08324217
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03917339 0.05253479 0.041462170 0.05108005
## [2,] 0.07276732 0.06298205 0.024938653 0.05267025
## [3,] 0.02851175 0.04669336 0.004679643 0.03009025
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2499773 0.3352405 0.26458273 0.3259574
## [2,] 0.4643504 0.4019076 0.15914114 0.3361049
## [3,] 0.1819421 0.2979645 0.02986223 0.1920151
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08311313 0.11146166 0.087969167 0.10837517
## [2,] 0.15438847 0.13362732 0.052911667 0.11174904
## [3,] 0.06049260 0.09906803 0.009928673 0.06384168
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09358539 0.1255058 0.09905329 0.12203045
## [2,] 0.17384143 0.1504644 0.05957854 0.12582943
## [3,] 0.06811468 0.1115506 0.01117969 0.07188574
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4035728 0.5412249 0.4271523 0.5262378
## [2,] 0.7496648 0.6488548 0.2569234 0.5426203
## [3,] 0.2937342 0.4810451 0.0482107 0.3099963
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03954897 0.05303847 0.04185969 0.05156978
## [2,] 0.07346499 0.06358590 0.02517775 0.05317523
## [3,] 0.02878510 0.04714103 0.00472451 0.03037875
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2536534 0.3401704 0.26847353 0.3307507
## [2,] 0.4711789 0.4078179 0.16148137 0.3410474
## [3,] 0.1846177 0.3023462 0.03030137 0.1948387
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3772408 0.5059115 0.39928180 0.4919023
## [2,] 0.7007513 0.6065188 0.24015990 0.5072159
## [3,] 0.2745689 0.4496583 0.04506509 0.2897700
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4723943 0.6335202 0.4999948 0.6159774
## [2,] 0.8775055 0.7595043 0.3007367 0.6351536
## [3,] 0.3438248 0.5630780 0.0564321 0.3628602
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4726018 0.6337985 0.50021440 0.6162480
## [2,] 0.8778909 0.7598379 0.30086880 0.6354326
## [3,] 0.3439759 0.5633253 0.05645689 0.3630196
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10968432 0.1470958 0.11609282 0.1430226
## [2,] 0.20374631 0.1763478 0.06982748 0.1474751
## [3,] 0.07983203 0.1307400 0.01310286 0.0842518
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1800261 0.2414301 0.19054450 0.2347447
## [2,] 0.3344112 0.2894418 0.11460865 0.2420526
## [3,] 0.1310292 0.2145851 0.02150588 0.1382835
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01703900 0.02285073 0.018034538 0.02221797
## [2,] 0.03165114 0.02739491 0.010847408 0.02290964
## [3,] 0.01240157 0.02030991 0.002035475 0.01308817
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1541630 0.2067455 0.16317024 0.2010205
## [2,] 0.2863686 0.2478596 0.09814359 0.2072785
## [3,] 0.1122051 0.1837571 0.01841627 0.1184172
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2434597 0.3264999 0.25768431 0.3174587
## [2,] 0.4522435 0.3914288 0.15499188 0.3273417
## [3,] 0.1771984 0.2901957 0.02908364 0.1870087
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4229761 0.5672463 0.44768921 0.5515386
## [2,] 0.7857077 0.6800509 0.26927597 0.5687088
## [3,] 0.3078566 0.5041731 0.05052861 0.3249006
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1599382 0.2144905 0.16928285 0.2085510
## [2,] 0.2970964 0.2571448 0.10182020 0.2150435
## [3,] 0.1164085 0.1906409 0.01910617 0.1228533
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4590423 0.6156141 0.48586270 0.5985671
## [2,] 0.8527033 0.7380374 0.29223655 0.6172014
## [3,] 0.3341068 0.5471629 0.05483708 0.3526041
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04885463 0.06551815 0.051709056 0.06370388
## [2,] 0.09075091 0.07854732 0.031101947 0.06568708
## [3,] 0.03555809 0.05823307 0.005836162 0.03752671
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3075135 0.4124013 0.32548053 0.4009815
## [2,] 0.5712279 0.4944129 0.19576993 0.4134646
## [3,] 0.2238189 0.3665457 0.03673548 0.2362103
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13658953 0.1831780 0.14457002 0.1781056
## [2,] 0.25372463 0.2196054 0.08695593 0.1836503
## [3,] 0.09941457 0.1628101 0.01631695 0.1049185
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4817801 0.6461073 0.50992895 0.6282160
## [2,] 0.8949403 0.7745946 0.30671190 0.6477732
## [3,] 0.3506561 0.5742655 0.05755332 0.3700697
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2724326 0.3653548 0.28834994 0.3552378
## [2,] 0.5060626 0.4380106 0.17343663 0.3662969
## [3,] 0.1982858 0.3247304 0.03254472 0.2092636
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12039668 0.1614620 0.12743108 0.15699097
## [2,] 0.22364528 0.1935709 0.07664721 0.16187832
## [3,] 0.08762886 0.1435088 0.01438256 0.09248029
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4517183 0.6057920 0.47811074 0.5890170
## [2,] 0.8390984 0.7262619 0.28757390 0.6073539
## [3,] 0.3287761 0.5384329 0.05396215 0.3469783
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.010484771 0.01406095 0.011097363 0.013671592
## [2,] 0.019476197 0.01685717 0.006674838 0.014097208
## [3,] 0.007631178 0.01249749 0.001252508 0.008053666
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4384071 0.5879406 0.4640218 0.5716599
## [2,] 0.8143719 0.7048605 0.2790997 0.5894565
## [3,] 0.3190878 0.5225664 0.0523720 0.3367536
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2912018 0.3905259 0.30821579 0.3797119
## [2,] 0.5409278 0.4681873 0.18538554 0.3915329
## [3,] 0.2119467 0.3471027 0.03478689 0.2236808
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1919920 0.2574773 0.20320945 0.2503475
## [2,] 0.3566385 0.3086801 0.12222636 0.2581411
## [3,] 0.1397384 0.2288479 0.02293531 0.1474748
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11234083 0.1506584 0.11890455 0.14648656
## [2,] 0.20868097 0.1806189 0.07151867 0.15104689
## [3,] 0.08176553 0.1339065 0.01342021 0.08629235
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10662123 0.1429880 0.11285077 0.13902850
## [2,] 0.19805642 0.1714231 0.06787745 0.14335666
## [3,] 0.07760261 0.1270889 0.01273694 0.08189896
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04022618 0.05394668 0.04257647 0.05245284
## [2,] 0.07472296 0.06467471 0.02560888 0.05408577
## [3,] 0.02927801 0.04794825 0.00480541 0.03089894
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3621154 0.4856270 0.38327267 0.4721795
## [2,] 0.6726548 0.5822006 0.23053073 0.4868791
## [3,] 0.2635601 0.4316293 0.04325821 0.2781517
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02155370 0.02890531 0.022813011 0.02810489
## [2,] 0.04003750 0.03465352 0.013721563 0.02897984
## [3,] 0.01568752 0.02569128 0.002574799 0.01655604
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2031515 0.2724431 0.21502097 0.2648989
## [2,] 0.3773681 0.3266221 0.12933075 0.2731456
## [3,] 0.1478606 0.2421497 0.02426842 0.1560467
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3018179 0.4047630 0.31945215 0.3935547
## [2,] 0.5606479 0.4852556 0.19214398 0.4058066
## [3,] 0.2196735 0.3597567 0.03605509 0.2318353
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09339121 0.1252454 0.09884776 0.12177725
## [2,] 0.17348072 0.1501522 0.05945492 0.12556834
## [3,] 0.06797334 0.1113192 0.01115649 0.07173658
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2917539 0.3912664 0.30880017 0.3804318
## [2,] 0.5419534 0.4690750 0.18573703 0.3922752
## [3,] 0.2123486 0.3477608 0.03485285 0.2241049
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03731540 0.05004307 0.039495617 0.04865732
## [2,] 0.06931597 0.05999481 0.023755811 0.05017209
## [3,] 0.02715943 0.04447869 0.004457688 0.02866307
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3160665 0.4238716 0.33453327 0.4121342
## [2,] 0.5871157 0.5081642 0.20121497 0.4249645
## [3,] 0.2300441 0.3767406 0.03775722 0.2427801
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3078267 0.4128213 0.32581200 0.4013898
## [2,] 0.5718096 0.4949164 0.19596930 0.4138857
## [3,] 0.2240469 0.3669189 0.03677289 0.2364509
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4405793 0.5908537 0.46632097 0.5744923
## [2,] 0.8184070 0.7083530 0.28048259 0.5923771
## [3,] 0.3206688 0.5251556 0.05263149 0.3384221
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05829015 0.07817197 0.061695864 0.07600731
## [2,] 0.10827805 0.09371753 0.037108809 0.07837352
## [3,] 0.04242559 0.06947989 0.006963327 0.04477441
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4744668 0.6362997 0.50218841 0.6186799
## [2,] 0.8813554 0.7628365 0.30205613 0.6379403
## [3,] 0.3453333 0.5655484 0.05667968 0.3644522
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07829626 0.10500183 0.082870860 0.1020942
## [2,] 0.14544079 0.12588287 0.049845139 0.1052726
## [3,] 0.05698672 0.09332648 0.009353251 0.0601417
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06206621 0.08323597 0.065692541 0.08093108
## [2,] 0.11529234 0.09978858 0.039512729 0.08345058
## [3,] 0.04517393 0.07398082 0.007414413 0.04767491
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1855966 0.2489006 0.19644046 0.2420083
## [2,] 0.3447588 0.2983979 0.11815495 0.2495424
## [3,] 0.1350836 0.2212249 0.02217133 0.1425623
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4007102 0.5373859 0.42412247 0.5225052
## [2,] 0.7443474 0.6442524 0.25510105 0.5387715
## [3,] 0.2916507 0.4776330 0.04786874 0.3077975
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4823235 0.6468361 0.51050413 0.6289246
## [2,] 0.8959497 0.7754683 0.30705787 0.6485039
## [3,] 0.3510517 0.5749133 0.05761824 0.3704871
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03253467 0.04363172 0.034435570 0.04242351
## [2,] 0.06043544 0.05230847 0.020712296 0.04374421
## [3,] 0.02367986 0.03878023 0.003886584 0.02499085
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09401376 0.1260803 0.09950669 0.12258902
## [2,] 0.17463716 0.1511531 0.05985125 0.12640539
## [3,] 0.06842646 0.1120612 0.01123086 0.07221478
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2880280 0.3862696 0.30485653 0.3755734
## [2,] 0.5350321 0.4630845 0.18336501 0.3872655
## [3,] 0.2096367 0.3433196 0.03440775 0.2212429
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3722806 0.4992594 0.39403178 0.4854344
## [2,] 0.6915373 0.5985439 0.23700211 0.5005467
## [3,] 0.2709587 0.4437459 0.04447254 0.2859599
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2993323 0.4014296 0.31682132 0.3903136
## [2,] 0.5560307 0.4812593 0.19056159 0.4024646
## [3,] 0.2178644 0.3567939 0.03575816 0.2299261
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05973257 0.08010637 0.063222557 0.07788814
## [2,] 0.11095744 0.09603661 0.038027084 0.08031291
## [3,] 0.04347543 0.07119920 0.007135638 0.04588237
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2751520 0.3690018 0.29122824 0.3587838
## [2,] 0.5111141 0.4423828 0.17516787 0.3699532
## [3,] 0.2002651 0.3279718 0.03286959 0.2113525
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13600766 0.1823976 0.14395416 0.1773469
## [2,] 0.25264377 0.2186699 0.08658550 0.1828679
## [3,] 0.09899107 0.1621165 0.01624744 0.1044715
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.47359457 0.63512992 0.50126521 0.61754252
## [2,] 0.87973515 0.76143414 0.30150084 0.63676750
## [3,] 0.34469847 0.56450868 0.05657549 0.36378216
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.023662412 0.031733273 0.025044932 0.030854546
## [2,] 0.043954590 0.038043866 0.015064018 0.031815092
## [3,] 0.017222320 0.028204793 0.002826706 0.018175806
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.10836992 0.14533312 0.11470163 0.14130870
## [2,] 0.20130472 0.17423459 0.06899070 0.14570784
## [3,] 0.07887536 0.12917327 0.01294584 0.08324217
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.059732571 0.080106373 0.063222557 0.077888145
## [2,] 0.110957442 0.096036614 0.038027084 0.080312913
## [3,] 0.043475426 0.071199200 0.007135638 0.045882374
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.27515198 0.36900181 0.29122824 0.35878377
## [2,] 0.51111411 0.44238284 0.17516787 0.36995322
## [3,] 0.20026511 0.32797184 0.03286959 0.21135247
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.13600766 0.18239764 0.14395416 0.17734686
## [2,] 0.25264377 0.21866989 0.08658550 0.18286792
## [3,] 0.09899107 0.16211652 0.01624744 0.10447155
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.811348
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.811348
einsum::einsum('ij->', arrC)
## [1] 6.297645
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 6.297645
einsum::einsum('ijk->', arrE)
## [1] 27.82096
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 27.82096
einsum::einsum('ij->i', arrC)
## [1] 2.286014 2.647159 1.364472
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 2.286014 2.647159 1.364472
einsum::einsum('ij->j', arrC)
## [1] 1.7426087 2.0125593 0.8819029 1.6605740
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 1.7426087 2.0125593 0.8819029 1.6605740
einsum::einsum('ijk->i', arrE)
## [1] 9.329189 9.142779 9.348993
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 9.329189 9.142779 9.348993
einsum::einsum('ijk->j', arrE)
## [1] 7.031367 5.594051 8.637996 6.557547
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 7.031367 5.594051 8.637996 6.557547
einsum::einsum('ijk->k', arrE)
## [1] 5.386282 5.579690 5.924996 5.399909 5.530084
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 5.386282 5.579690 5.924996 5.399909 5.530084
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 3.110098 2.229592 2.904887 1.084611
## [2,] 1.634734 1.520310 2.953758 3.033977
## [3,] 2.286535 1.844149 2.779351 2.438958
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 3.110098 2.229592 2.904887 1.084611
## [2,] 1.634734 1.520310 2.953758 3.033977
## [3,] 2.286535 1.844149 2.779351 2.438958
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.2460729 1.5684519 1.799502 1.0833173 1.3340229
## [2,] 0.7659302 0.8531635 1.852999 0.8692094 1.2527496
## [3,] 1.1042711 2.1438180 1.225308 2.1901444 1.9744540
## [4,] 2.2700079 1.0142568 1.047187 1.2572376 0.9688573
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.2460729 1.5684519 1.7995023 1.0833173 1.3340229
## [2,] 0.7659302 0.8531635 1.8529988 0.8692094 1.2527496
## [3,] 1.1042711 2.1438180 1.2253081 2.1901444 1.9744540
## [4,] 2.2700079 1.0142568 1.0471873 1.2572376 0.9688573
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.2460729 1.5684519 1.799502 1.0833173 1.3340229
## [2,] 0.7659302 0.8531635 1.852999 0.8692094 1.2527496
## [3,] 1.1042711 2.1438180 1.225308 2.1901444 1.9744540
## [4,] 2.2700079 1.0142568 1.047187 1.2572376 0.9688573
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.2460729 1.5684519 1.7995023 1.0833173 1.3340229
## [2,] 0.7659302 0.8531635 1.8529988 0.8692094 1.2527496
## [3,] 1.1042711 2.1438180 1.2253081 2.1901444 1.9744540
## [4,] 2.2700079 1.0142568 1.0471873 1.2572376 0.9688573
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.916941
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 1.916941
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.6391501 0.9505729 0.5050457
## [2,] 0.1794800 0.5771017 0.6083638
## [3,] 0.7014562 0.6059903 0.7006888
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.6391501 0.9505729 0.5050457
## [2,] 0.1794800 0.5771017 0.6083638
## [3,] 0.7014562 0.6059903 0.7006888
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.8145132 0.79883648 0.8470683
## [2,] 0.1129696 0.41993257 0.2476367
## [3,] 0.4698582 0.06940866 0.7627255
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.6362116 0.1262297 0.9478238
## [2,] 0.1733944 0.2757450 0.6430187
## [3,] 0.8627013 0.9517530 0.9142414
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.4401515 0.1203929 0.31950012
## [2,] 0.5224841 0.4792808 0.02830174
## [3,] 0.9714391 0.1671433 0.72534532
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.81451323 0.79883648 0.84706825
## [2,] 0.11296960 0.41993257 0.24763668
## [3,] 0.46985819 0.06940866 0.76272554
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.6362116 0.1262297 0.9478238
## [2,] 0.1733944 0.2757450 0.6430187
## [3,] 0.8627013 0.9517530 0.9142414
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.44015154 0.12039286 0.31950012
## [2,] 0.52248405 0.47928076 0.02830174
## [3,] 0.97143911 0.16714326 0.72534532
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.231093
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.231093
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 3.879374
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 3.879374
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 19.00677
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 19.00677
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.0015753 1.1336396 1.3581489 0.5623024 0.8418590
## [2,] 0.3002706 0.3527555 1.6779018 0.4031548 1.0267094
## [3,] 0.7331753 1.7576936 0.5684437 1.6457514 1.3171956
## [4,] 1.8194918 0.4894007 0.6100735 0.9933248 0.4139072
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.0015753 1.1336396 1.3581489 0.5623024 0.8418590
## [2,] 0.3002706 0.3527555 1.6779018 0.4031548 1.0267094
## [3,] 0.7331753 1.7576936 0.5684437 1.6457514 1.3171956
## [4,] 1.8194918 0.4894007 0.6100735 0.9933248 0.4139072
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.3273530 1.521460 0.8160113
## [2,] 1.5214604 1.948512 1.0340104
## [3,] 0.8160113 1.034010 0.6035090
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.3273530 1.5214604 0.8160113
## [2,] 1.5214604 1.9485123 1.0340104
## [3,] 0.8160113 1.0340104 0.6035090
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.2362237 0.81510552 0.12513784
## [2,] 0.4248495 0.61062508 0.33562260
## [3,] 0.2646337 0.09573859 0.00337106
## [4,] 0.4016462 0.42704311 0.13937752
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.23622365 0.81510552 0.12513784
## [2,] 0.42484948 0.61062508 0.33562260
## [3,] 0.26463365 0.09573859 0.00337106
## [4,] 0.40164622 0.42704311 0.13937752
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.949489252 0.945512561 0.98259434 0.001966619 0.01630749
## [2,] 0.006496194 0.001229037 0.86379746 0.036922289 0.98481227
## [3,] 0.037075991 0.757370134 0.35897540 0.422895991 0.35119306
## [4,] 0.272369115 0.010103879 0.04812425 0.014383581 0.01510425
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.002370253 0.05092906 0.3141916577 0.1747095 0.145819904
## [2,] 0.264531810 0.10060901 0.0004653658 0.3603379 0.004480944
## [3,] 0.689477956 0.10828813 0.1560424371 0.4011336 0.586701802
## [4,] 0.602440226 0.40031786 0.0068500589 0.9529899 0.320495479
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.049715762 0.13719799 0.06136287 0.385626218 0.67973166
## [2,] 0.029242593 0.25091746 0.81363897 0.005894578 0.03741618
## [3,] 0.006621356 0.89203532 0.05342590 0.821721813 0.37930076
## [4,] 0.944682473 0.07897896 0.55509923 0.025951270 0.07830750
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.949489252 0.945512561 0.982594336 0.001966619 0.016307487
## [2,] 0.006496194 0.001229037 0.863797456 0.036922289 0.984812275
## [3,] 0.037075991 0.757370134 0.358975403 0.422895991 0.351193060
## [4,] 0.272369115 0.010103879 0.048124253 0.014383581 0.015104246
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0023702526 0.0509290617 0.3141916577 0.1747095146 0.1458199035
## [2,] 0.2645318104 0.1006090055 0.0004653658 0.3603379480 0.0044809436
## [3,] 0.6894779564 0.1082881313 0.1560424371 0.4011336252 0.5867018024
## [4,] 0.6024402259 0.4003178559 0.0068500589 0.9529899085 0.3204954788
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.049715762 0.137197994 0.061362869 0.385626218 0.679731659
## [2,] 0.029242593 0.250917458 0.813638967 0.005894578 0.037416182
## [3,] 0.006621356 0.892035316 0.053425903 0.821721813 0.379300762
## [4,] 0.944682473 0.078978964 0.555099233 0.025951270 0.078307496
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 1.769458 2.169530 1.447294
## [2,] 1.978221 1.504642 2.096827
## [3,] 2.739184 1.059888 2.125925
## [4,] 1.006734 2.627827 1.765347
## [5,] 1.835593 1.780891 1.913600
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.769458 2.169530 1.447294
## [2,] 1.978221 1.504642 2.096827
## [3,] 2.739184 1.059888 2.125925
## [4,] 1.006734 2.627827 1.765347
## [5,] 1.835593 1.780891 1.913600
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.087029075 0.0866645766 0.090063449 0.000180258 0.001494725
## [2,] 0.001070890 0.0002026054 0.142396053 0.006086598 0.162345211
## [3,] 0.003807052 0.0777685824 0.036860456 0.043423975 0.036061346
## [4,] 0.042447535 0.0015746453 0.007499954 0.002241618 0.002353931
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.001019746 0.021911044 0.1351736536 0.07516471 0.062735622
## [2,] 0.085258191 0.032426126 0.0001499867 0.11613636 0.001444201
## [3,] 0.034841028 0.005472067 0.0078852107 0.02027028 0.029647495
## [4,] 0.135790459 0.090231932 0.0015440082 0.21480461 0.072239911
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 5.571281e-03 0.015374774 0.0068764872 0.043214306 0.076172548
## [2,] 8.788999e-03 0.075414420 0.2445430117 0.001771643 0.011245609
## [3,] 1.998876e-05 0.002692904 0.0001612838 0.002480640 0.001145045
## [4,] 1.179101e-01 0.009857720 0.0692844354 0.003239095 0.009773911
DelayedTensor::einsum('i,ij,ijk,ijk,ji->jki',
darrA, darrC, darrE, darrE, t(darrC))
## <4 x 5 x 3> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0870290755 0.0866645766 0.0900634488 0.0001802580 0.0014947252
## [2,] 0.0010708904 0.0002026054 0.1423960526 0.0060865984 0.1623452113
## [3,] 0.0038070517 0.0777685824 0.0368604556 0.0434239750 0.0360613460
## [4,] 0.0424475350 0.0015746453 0.0074999543 0.0022416182 0.0023539306
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0010197461 0.0219110443 0.1351736536 0.0751647054 0.0627356222
## [2,] 0.0852581914 0.0324261261 0.0001499867 0.1161363607 0.0014442011
## [3,] 0.0348410283 0.0054720674 0.0078852107 0.0202702753 0.0296474948
## [4,] 0.1357904588 0.0902319317 0.0015440082 0.2148046085 0.0722399107
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 5.571281e-03 1.537477e-02 6.876487e-03 4.321431e-02 7.617255e-02
## [2,] 8.788999e-03 7.541442e-02 2.445430e-01 1.771643e-03 1.124561e-02
## [3,] 1.998876e-05 2.692904e-03 1.612838e-04 2.480640e-03 1.145045e-03
## [4,] 1.179101e-01 9.857720e-03 6.928444e-02 3.239095e-03 9.773911e-03
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.3.1 Patched (2023-06-17 r84564)
## Platform: x86_64-apple-darwin20 (64-bit)
## Running under: macOS Monterey 12.6.5
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0
##
## locale:
## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## 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.10.0
## [3] HDF5Array_1.30.0 rhdf5_2.46.0
## [5] DelayedArray_0.28.0 SparseArray_1.2.0
## [7] S4Arrays_1.2.0 abind_1.4-5
## [9] IRanges_2.36.0 S4Vectors_0.40.0
## [11] MatrixGenerics_1.14.0 matrixStats_1.0.0
## [13] BiocGenerics_0.48.0 Matrix_1.6-1.1
## [15] DelayedTensor_1.8.0 BiocStyle_2.30.0
##
## loaded via a namespace (and not attached):
## [1] jsonlite_1.8.7 compiler_4.3.1 BiocManager_1.30.22
## [4] crayon_1.5.2 rsvd_1.0.5 Rcpp_1.0.11
## [7] rhdf5filters_1.14.0 parallel_4.3.1 jquerylib_0.1.4
## [10] BiocParallel_1.36.0 yaml_2.3.7 fastmap_1.1.1
## [13] lattice_0.22-5 R6_2.5.1 XVector_0.42.0
## [16] ScaledMatrix_1.10.0 knitr_1.44 bookdown_0.36
## [19] bslib_0.5.1 rlang_1.1.1 cachem_1.0.8
## [22] xfun_0.40 sass_0.4.7 cli_3.6.1
## [25] Rhdf5lib_1.24.0 BiocSingular_1.18.0 zlibbioc_1.48.0
## [28] digest_0.6.33 grid_4.3.1 irlba_2.3.5.1
## [31] rTensor_1.4.8 dqrng_0.3.1 evaluate_0.22
## [34] codetools_0.2-19 beachmat_2.18.0 rmarkdown_2.25
## [37] tools_4.3.1 htmltools_0.5.6.1