DelayedTensor 0.99.12
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2021-06-30 14:57:51
Compiled: Tue Oct 19 20:43:48 2021
einsum
einsum
is an easy and intuitive way to write tensor operations.
It was originally introduced by
Numpy
1 https://numpy.org/doc/stable/reference/generated/numpy.einsum.html
package of Python but similar tools have been implemented in other languages
(e.g. R, Julia) inspired by Numpy
.
In this vignette, we will use CRAN einsum package first.
einsum
is named after
Einstein summation2 https://en.wikipedia.org/wiki/Einstein_notation
introduced by Albert Einstein,
which is a notational convention that implies summation over
a set of indexed terms in a formula.
Here, we consider a simple example of einsum
; matrix multiplication.
If we naively implement the matrix multiplication,
the calculation would look like the following in a for loop.
A <- matrix(runif(3*4), nrow=3, ncol=4)
B <- matrix(runif(4*5), nrow=4, ncol=5)
C <- matrix(0, nrow=3, ncol=5)
I <- nrow(A)
J <- ncol(A)
K <- ncol(B)
for(i in 1:I){
for(j in 1:J){
for(k in 1:K){
C[i,k] = C[i,k] + A[i,j] * B[j,k]
}
}
}
Therefore, any programming language can implement this. However, when analyzing tensor data, such operations tend to be more complicated and increase the possibility of causing bugs because the order of tensors is larger or more tensors are handled simultaneously. In addition, several programming languages, especially R, are known to significantly slow down the speed of computation if the code is written in for loop.
Obviously, in the case of the R language, it should be executed using the built-in matrix multiplication function (%*%) prepared by the R, as shown below.
C <- A %*% B
However, more complex operations than matrix multiplication are not always provided by programming languages as standard.
einsum
is a function that solves such a problem.
To put it simply, einsum
is a wrapper for the for loop above.
Like the Einstein summation, it omits many notations such as for,
array size (e.g. I, J, and K), brackets (e.g. {}, (), and []),
and even addition operator (+) and
extracts the array subscripts (e.g. i, j, and k)
to concisely express the tensor operation as follows.
suppressPackageStartupMessages(library("einsum"))
C <- einsum('ij,jk->ik', A, B)
DelayedTensor
CRAN einsum is easy to use because the syntax is almost
the same as that of Numpy
‘s einsum
,
except that it prohibits the implicit modes that do not use’->’.
It is extremely fast because the internal calculation
is actually performed by C++.
When the input tensor is huge, however,
it is not scalable because it assumes that the input is R’s standard array.
Using einsum
of DelayedTensor,
we can augment the CRAN einsum
’s functionality;
in DelayedTensor,
the input DelayedArray objects are divided into
multiple block tensors and the CRAN einsum
is incremently applied in the block processing.
A surprisingly large number of tensor operations can be handled
uniformly in einsum
.
In more detail, einsum
is capable of performing any tensor operation
that can be described by a combination of the following
three operations3 https://ajcr.net/Basic-guide-to-einsum/.
Some typical operations are introduced below. Here we use the arrays and DelayedArray objects below.
suppressPackageStartupMessages(library("DelayedTensor"))
suppressPackageStartupMessages(library("DelayedArray"))
arrA <- array(runif(3), dim=c(3))
arrB <- array(runif(3*3), dim=c(3,3))
arrC <- array(runif(3*4), dim=c(3,4))
arrD <- array(runif(3*3*3), dim=c(3,3,3))
arrE <- array(runif(3*4*5), dim=c(3,4,5))
darrA <- DelayedArray(arrA)
darrB <- DelayedArray(arrB)
darrC <- DelayedArray(arrC)
darrD <- DelayedArray(arrD)
darrE <- DelayedArray(arrE)
If the same subscript is written on both sides of ->,
einsum
will simply output the object without any calculation.
einsum::einsum('i->i', arrA)
## [1] 0.8665767 0.3812867 0.7701046
DelayedTensor::einsum('i->i', darrA)
## <3> array of class DelayedArray and type "double":
## [1] [2] [3]
## 0.8665767 0.3812867 0.7701046
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.6944804 0.7774247 0.4601289 0.8619327
## [2,] 0.7104757 0.5617919 0.6101603 0.5303033
## [3,] 0.6662101 0.6470660 0.6650823 0.4353817
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.6944804 0.7774247 0.4601289 0.8619327
## [2,] 0.7104757 0.5617919 0.6101603 0.5303033
## [3,] 0.6662101 0.6470660 0.6650823 0.4353817
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2776417 0.3436685 0.2126393 0.2922871
## [2,] 0.6185796 0.5006168 0.1613489 0.3382598
## [3,] 0.6729805 0.9347015 0.7797031 0.5153848
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5342950 0.2833045 0.3387200 0.34643214
## [2,] 0.7269092 0.6268344 0.5496172 0.48381986
## [3,] 0.2166285 0.5843675 0.3426692 0.07755386
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7890304 0.4505259 0.3793656 0.05881733
## [2,] 0.3032854 0.1632046 0.2526108 0.19390098
## [3,] 0.1844713 0.6444075 0.8711722 0.99704044
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8950785 0.98455315 0.2346453 0.8743521
## [2,] 0.2271223 0.89293259 0.4914470 0.6554425
## [3,] 0.8825461 0.09538529 0.1858893 0.8872440
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8378515 0.3734326 0.2590844 0.07201653
## [2,] 0.2361258 0.1492547 0.2055513 0.48913971
## [3,] 0.8960497 0.9393634 0.9933287 0.34567313
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.2776417 0.3436685 0.2126393 0.2922871
## [2,] 0.6185796 0.5006168 0.1613489 0.3382598
## [3,] 0.6729805 0.9347015 0.7797031 0.5153848
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.53429498 0.28330453 0.33872002 0.34643214
## [2,] 0.72690917 0.62683437 0.54961722 0.48381986
## [3,] 0.21662851 0.58436746 0.34266916 0.07755386
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.78903038 0.45052588 0.37936564 0.05881733
## [2,] 0.30328538 0.16320462 0.25261080 0.19390098
## [3,] 0.18447130 0.64440748 0.87117217 0.99704044
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.89507852 0.98455315 0.23464533 0.87435212
## [2,] 0.22712227 0.89293259 0.49144705 0.65544252
## [3,] 0.88254607 0.09538529 0.18588931 0.88724397
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.83785147 0.37343263 0.25908436 0.07201653
## [2,] 0.23612576 0.14925473 0.20555132 0.48913971
## [3,] 0.89604965 0.93936339 0.99332873 0.34567313
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.97304543 0.04073512 0.07035760
DelayedTensor::einsum('ii->i', darrB)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.97304543 0.04073512 0.07035760
einsum::einsum('iii->i', arrD)
## [1] 0.86862021 0.09527509 0.78304200
DelayedTensor::einsum('iii->i', darrD)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.86862021 0.09527509 0.78304200
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.7509551 0.1453795 0.5930611
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 0.7509551 0.1453795 0.5930611
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.4823030 0.6043891 0.2117186 0.7429280
## [2,] 0.5047758 0.3156101 0.3722956 0.2812216
## [3,] 0.4438359 0.4186944 0.4423345 0.1895572
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.4823030 0.6043891 0.2117186 0.7429280
## [2,] 0.5047758 0.3156101 0.3722956 0.2812216
## [3,] 0.4438359 0.4186944 0.4423345 0.1895572
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07708493 0.1181081 0.04521546 0.08543177
## [2,] 0.38264069 0.2506172 0.02603348 0.11441967
## [3,] 0.45290270 0.8736668 0.60793688 0.26562147
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.28547113 0.08026145 0.1147313 0.1200152
## [2,] 0.52839694 0.39292133 0.3020791 0.2340817
## [3,] 0.04692791 0.34148533 0.1174222 0.0060146
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.62256895 0.20297357 0.14391829 0.003459478
## [2,] 0.09198202 0.02663575 0.06381221 0.037597591
## [3,] 0.03402966 0.41526100 0.75894094 0.994089639
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.80116555 0.969344903 0.05505843 0.7644916
## [2,] 0.05158453 0.797328609 0.24152020 0.4296049
## [3,] 0.77888756 0.009098353 0.03455484 0.7872019
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.70199509 0.13945193 0.06712471 0.00518638
## [2,] 0.05575537 0.02227697 0.04225135 0.23925765
## [3,] 0.80290498 0.88240358 0.98670196 0.11948991
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> array of class HDF5Array and type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.07708493 0.11810805 0.04521546 0.08543177
## [2,] 0.38264069 0.25061716 0.02603348 0.11441967
## [3,] 0.45290270 0.87366683 0.60793688 0.26562147
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.28547113 0.08026145 0.11473125 0.12001523
## [2,] 0.52839694 0.39292133 0.30207909 0.23408166
## [3,] 0.04692791 0.34148533 0.11742215 0.00601460
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.622568948 0.202973567 0.143918290 0.003459478
## [2,] 0.091982023 0.026635749 0.063812215 0.037597591
## [3,] 0.034029662 0.415260998 0.758940941 0.994089639
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.801165550 0.969344903 0.055058429 0.764491622
## [2,] 0.051584526 0.797328609 0.241520203 0.429604896
## [3,] 0.778887561 0.009098353 0.034554836 0.787201855
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.70199509 0.13945193 0.06712471 0.00518638
## [2,] 0.05575537 0.02227697 0.04225135 0.23925765
## [3,] 0.80290498 0.88240358 0.98670196 0.11948991
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.7509551 0.3304142 0.6673547
## [2,] 0.3304142 0.1453795 0.2936306
## [3,] 0.6673547 0.2936306 0.5930611
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.7509551 0.3304142 0.6673547
## [2,] 0.3304142 0.1453795 0.2936306
## [3,] 0.6673547 0.2936306 0.5930611
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1928167 0.2158455 0.1277510 0.2393085
## [2,] 0.1972577 0.1559769 0.1694060 0.1472343
## [3,] 0.1849677 0.1796525 0.1846546 0.1208801
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4295914 0.4808990 0.2846264 0.5331740
## [2,] 0.4394858 0.3475130 0.3774327 0.3280348
## [3,] 0.4121040 0.4002618 0.4114063 0.2693182
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4673717 0.5231916 0.3096578 0.5800639
## [2,] 0.4781363 0.3780750 0.4106260 0.3568838
## [3,] 0.4483464 0.4354628 0.4475874 0.2930034
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2386710 0.2671764 0.1581318 0.2962191
## [2,] 0.2441681 0.1930702 0.2096929 0.1822486
## [3,] 0.2289554 0.2223762 0.2285678 0.1496270
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3476685 0.3891918 0.2303483 0.4314980
## [2,] 0.3556761 0.2812424 0.3054565 0.2654787
## [3,] 0.3335160 0.3239321 0.3329514 0.2179594
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6491318 0.7266600 0.4300832 0.8056498
## [2,] 0.6640827 0.5251077 0.5703178 0.4956753
## [3,] 0.6227076 0.6048135 0.6216534 0.4069519
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1476738 0.1653110 0.09784148 0.18328074
## [2,] 0.1510750 0.1194590 0.12974405 0.11276331
## [3,] 0.1416624 0.1375916 0.14142261 0.09257924
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1120537 0.12543664 0.07424132 0.13907193
## [2,] 0.1146345 0.09064452 0.09844872 0.08556388
## [3,] 0.1074923 0.10440341 0.10731032 0.07024837
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5414885 0.6061604 0.3587640 0.6720516
## [2,] 0.5539601 0.4380309 0.4757439 0.4134791
## [3,] 0.5194461 0.5045193 0.5185667 0.3394684
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2029877 0.2272312 0.1344898 0.2519318
## [2,] 0.2076629 0.1642045 0.1783420 0.1550008
## [3,] 0.1947246 0.1891291 0.1943950 0.1272565
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2349148 0.2629715 0.1556431 0.2915572
## [2,] 0.2403254 0.1900316 0.2063927 0.1793803
## [3,] 0.2253521 0.2188764 0.2249706 0.1472721
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3579246 0.4006728 0.2371435 0.4442270
## [2,] 0.3661684 0.2895390 0.3144674 0.2733103
## [3,] 0.3433546 0.3334880 0.3427733 0.2243891
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3710574 0.4153741 0.2458446 0.4605263
## [2,] 0.3796036 0.3001626 0.3260056 0.2833384
## [3,] 0.3559527 0.3457241 0.3553501 0.2326222
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5048241 0.5651171 0.3344720 0.6265468
## [2,] 0.5164513 0.4083717 0.4435311 0.3854823
## [3,] 0.4842742 0.4703582 0.4834544 0.3164829
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1504442 0.1684123 0.09967705 0.18671920
## [2,] 0.1539093 0.1217001 0.13217812 0.11487882
## [3,] 0.1443201 0.1401729 0.14407579 0.09431608
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1967494 0.2202479 0.1303566 0.2441894
## [2,] 0.2012810 0.1591582 0.1728612 0.1502373
## [3,] 0.1887403 0.1833167 0.1884208 0.1233456
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4353242 0.4873165 0.2884246 0.5402891
## [2,] 0.4453506 0.3521505 0.3824695 0.3324124
## [3,] 0.4176034 0.4056032 0.4168964 0.2729122
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4058317 0.4543017 0.2688844 0.5036854
## [2,] 0.4151789 0.3282929 0.3565578 0.3098920
## [3,] 0.3893115 0.3781243 0.3886525 0.2544229
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2352344 0.2633293 0.1558549 0.2919539
## [2,] 0.2406524 0.1902902 0.2066735 0.1796244
## [3,] 0.2256587 0.2191742 0.2252767 0.1474725
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3816984 0.4272860 0.2528948 0.4737331
## [2,] 0.3904897 0.3087705 0.3353546 0.2914638
## [3,] 0.3661606 0.3556386 0.3655407 0.2392933
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2379770 0.2663995 0.1576720 0.2953578
## [2,] 0.2434581 0.1925088 0.2090831 0.1817186
## [3,] 0.2282897 0.2217296 0.2279032 0.1491919
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2405903 0.2693249 0.1594035 0.2986012
## [2,] 0.2461316 0.1946228 0.2113792 0.1837141
## [3,] 0.2307966 0.2241644 0.2304059 0.1508302
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3360034 0.3761335 0.2226195 0.4170202
## [2,] 0.3437423 0.2718061 0.2952077 0.2565713
## [3,] 0.3223257 0.3130634 0.3217800 0.2106463
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05385963 0.06029228 0.03568477 0.06684621
## [2,] 0.05510013 0.04356913 0.04732029 0.04112707
## [3,] 0.05166716 0.05018246 0.05157970 0.03376553
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5479661 0.6134117 0.3630557 0.6800911
## [2,] 0.5605869 0.4432709 0.4814350 0.4184254
## [3,] 0.5256600 0.5105547 0.5247701 0.3435294
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2106257 0.2357815 0.1395504 0.2614116
## [2,] 0.2154769 0.1703833 0.1850527 0.1608332
## [3,] 0.2020518 0.1962457 0.2017097 0.1320449
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1281117 0.1434125 0.08488059 0.15900185
## [2,] 0.1310624 0.1036345 0.11255707 0.09782575
## [3,] 0.1228966 0.1193651 0.12268860 0.08031542
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3128814 0.3502499 0.2073000 0.3883230
## [2,] 0.3200877 0.2531018 0.2748930 0.2389154
## [3,] 0.3001449 0.2915200 0.2996368 0.1961507
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1133424 0.12687930 0.07509517 0.14067141
## [2,] 0.1159529 0.09168703 0.09958099 0.08654795
## [3,] 0.1087286 0.10560416 0.10854451 0.07105630
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4475283 0.5009783 0.2965105 0.5554359
## [2,] 0.4578359 0.3620229 0.3931919 0.3417314
## [3,] 0.4293108 0.4169742 0.4285840 0.2805632
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2634620 0.2949282 0.1745571 0.3269877
## [2,] 0.2695301 0.2131245 0.2314739 0.2011789
## [3,] 0.2527372 0.2454746 0.2523094 0.1651688
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1754332 0.1963859 0.1162335 0.2177335
## [2,] 0.1794738 0.1419147 0.1541331 0.1339603
## [3,] 0.1682919 0.1634559 0.1680070 0.1099821
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6050120 0.6772707 0.4008515 0.7508918
## [2,] 0.6189467 0.4894175 0.5315547 0.4619855
## [3,] 0.5803837 0.5637059 0.5794012 0.3792924
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04084748 0.04572604 0.02706355 0.05069658
## [2,] 0.04178828 0.03304310 0.03588800 0.03119102
## [3,] 0.03918470 0.03805869 0.03911836 0.02560799
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1346604 0.1507434 0.08921946 0.16712960
## [2,] 0.1377619 0.1089320 0.11831069 0.10282634
## [3,] 0.1291788 0.1254667 0.12896011 0.08442093
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6924250 0.7751238 0.4587672 0.8593818
## [2,] 0.7083730 0.5601292 0.6083545 0.5287339
## [3,] 0.6642384 0.6451509 0.6631139 0.4340931
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6216144 0.6958561 0.4118515 0.7714975
## [2,] 0.6359316 0.5028478 0.5461414 0.4746631
## [3,] 0.5963104 0.5791749 0.5953009 0.3897008
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1577320 0.1765705 0.1045055 0.19576412
## [2,] 0.1613649 0.1275954 0.1385810 0.12044370
## [3,] 0.1513112 0.1469631 0.1510550 0.09888487
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6129109 0.6861131 0.4060850 0.7606953
## [2,] 0.6270276 0.4958072 0.5384946 0.4680171
## [3,] 0.5879611 0.5710655 0.5869658 0.3842444
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6837528 0.7654159 0.4530214 0.8486186
## [2,] 0.6995011 0.5531140 0.6007353 0.5221118
## [3,] 0.6559193 0.6370708 0.6548089 0.4286564
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6201241 0.6941878 0.4108641 0.7696478
## [2,] 0.6344069 0.5016423 0.5448321 0.4735251
## [3,] 0.5948807 0.5777863 0.5938737 0.3887665
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06624321 0.07415488 0.04388953 0.08221570
## [2,] 0.06776893 0.05358668 0.05820032 0.05058314
## [3,] 0.06354664 0.06172057 0.06343907 0.04152901
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1629566 0.1824191 0.1079671 0.2022485
## [2,] 0.1667098 0.1318218 0.1431713 0.1244332
## [3,] 0.1563231 0.1518310 0.1560585 0.1021603
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3413003 0.3820631 0.2261290 0.4235943
## [2,] 0.3491612 0.2760910 0.2998615 0.2606160
## [3,] 0.3274070 0.3179987 0.3268527 0.2139670
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1290965 0.1445149 0.08553305 0.16022408
## [2,] 0.1320698 0.1044311 0.11342228 0.09857772
## [3,] 0.1238413 0.1202826 0.12363169 0.08093280
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6072204 0.6797429 0.4023147 0.7536327
## [2,] 0.6212060 0.4912039 0.5334950 0.4636718
## [3,] 0.5825022 0.5657635 0.5815161 0.3806769
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4551920 0.5095572 0.3015881 0.5649474
## [2,] 0.4656760 0.3682223 0.3999250 0.3475833
## [3,] 0.4366624 0.4241146 0.4359232 0.2853677
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6161735 0.6897653 0.4082466 0.7647446
## [2,] 0.6303653 0.4984465 0.5413611 0.4705084
## [3,] 0.5910909 0.5741054 0.5900903 0.3862898
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5818714 0.6513664 0.3855197 0.7221716
## [2,] 0.5952731 0.4706982 0.5112237 0.4443154
## [3,] 0.5581851 0.5421452 0.5572402 0.3647852
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1639847 0.1835700 0.1086483 0.2035245
## [2,] 0.1677616 0.1326535 0.1440746 0.1252183
## [3,] 0.1573094 0.1527889 0.1570431 0.1028048
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6222889 0.6966111 0.4122984 0.7723345
## [2,] 0.6366215 0.5033934 0.5467340 0.4751781
## [3,] 0.5969573 0.5798032 0.5959468 0.3901236
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2593416 0.2903157 0.1718272 0.3218738
## [2,] 0.2653148 0.2097914 0.2278538 0.1980326
## [3,] 0.2487846 0.2416355 0.2483634 0.1625857
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10365447 0.11603430 0.06867642 0.12864753
## [2,] 0.10604186 0.08385009 0.09106931 0.07915028
## [3,] 0.09943501 0.09657765 0.09926668 0.06498277
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6523694 0.7302843 0.4322283 0.8096681
## [2,] 0.6673949 0.5277267 0.5731623 0.4981475
## [3,] 0.6258134 0.6078301 0.6247540 0.4089816
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1799290 0.2014186 0.1192122 0.2233133
## [2,] 0.1840732 0.1455515 0.1580830 0.1373933
## [3,] 0.1726046 0.1676447 0.1723124 0.1128006
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1427514 0.1598007 0.09458011 0.17717141
## [2,] 0.1460392 0.1154771 0.12541927 0.10900455
## [3,] 0.1369404 0.1330053 0.13670855 0.08949328
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6898473 0.7722382 0.4570593 0.8561825
## [2,] 0.7057360 0.5580440 0.6060898 0.5267655
## [3,] 0.6617657 0.6427492 0.6606454 0.4324771
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05001406 0.05598743 0.03313689 0.06207340
## [2,] 0.05116600 0.04045830 0.04394163 0.03819060
## [3,] 0.04797814 0.04659945 0.04789692 0.03135468
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3396979 0.3802693 0.2250673 0.4216055
## [2,] 0.3475219 0.2747947 0.2984537 0.2593924
## [3,] 0.3258698 0.3165057 0.3253182 0.2129625
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2400632 0.2687348 0.1590542 0.2979470
## [2,] 0.2455924 0.1941964 0.2109160 0.1833116
## [3,] 0.2302909 0.2236733 0.2299011 0.1504997
DelayedTensor::einsum('ij,klm->ijklm', darrC, darrE)
## <3 x 4 x 3 x 4 x 5> array of class HDF5Array and type "double":
## ,,1,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.1928167 0.2158455 0.1277510 0.2393085
## [2,] 0.1972577 0.1559769 0.1694060 0.1472343
## [3,] 0.1849677 0.1796525 0.1846546 0.1208801
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.4295914 0.4808990 0.2846264 0.5331740
## [2,] 0.4394858 0.3475130 0.3774327 0.3280348
## [3,] 0.4121040 0.4002618 0.4114063 0.2693182
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.4673717 0.5231916 0.3096578 0.5800639
## [2,] 0.4781363 0.3780750 0.4106260 0.3568838
## [3,] 0.4483464 0.4354628 0.4475874 0.2930034
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.05001406 0.05598743 0.03313689 0.06207340
## [2,] 0.05116600 0.04045830 0.04394163 0.03819060
## [3,] 0.04797814 0.04659945 0.04789692 0.03135468
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.3396979 0.3802693 0.2250673 0.4216055
## [2,] 0.3475219 0.2747947 0.2984537 0.2593924
## [3,] 0.3258698 0.3165057 0.3253182 0.2129625
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.2400632 0.2687348 0.1590542 0.2979470
## [2,] 0.2455924 0.1941964 0.2109160 0.1833116
## [3,] 0.2302909 0.2236733 0.2299011 0.1504997
If there is a vanishing subscript on the left or right side of ->, the summation is done for that subscript.
einsum::einsum('i->', arrA)
## [1] 2.017968
DelayedTensor::einsum('i->', darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 2.017968
einsum::einsum('ij->', arrC)
## [1] 7.620438
DelayedTensor::einsum('ij->', darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 7.620438
einsum::einsum('ijk->', arrE)
## [1] 29.1503
DelayedTensor::einsum('ijk->', darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 29.1503
einsum::einsum('ij->i', arrC)
## [1] 2.793967 2.412731 2.413740
DelayedTensor::einsum('ij->i', darrC)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 2.793967 2.412731 2.413740
einsum::einsum('ij->j', arrC)
## [1] 2.071166 1.986283 1.735372 1.827618
DelayedTensor::einsum('ij->j', darrC)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 2.071166 1.986283 1.735372 1.827618
einsum::einsum('ijk->i', arrE)
## [1] 8.837742 8.266003 12.046560
DelayedTensor::einsum('ijk->i', darrE)
## <3> array of class HDF5Array and type "double":
## [1] [2] [3]
## 8.837742 8.266003 12.046560
einsum::einsum('ijk->j', arrE)
## [1] 8.298595 7.966553 6.257792 6.627364
DelayedTensor::einsum('ijk->j', darrE)
## <4> array of class HDF5Array and type "double":
## [1] [2] [3] [4]
## 8.298595 7.966553 6.257792 6.627364
einsum::einsum('ijk->k', arrE)
## [1] 5.647811 5.111151 5.287832 7.306638 5.796871
DelayedTensor::einsum('ijk->k', darrE)
## <5> array of class HDF5Array and type "double":
## [1] [2] [3] [4] [5]
## 5.647811 5.111151 5.287832 7.306638 5.796871
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 3.333897 2.435485 1.424455 1.643905
## [2,] 2.112022 2.332843 1.660575 2.160563
## [3,] 2.852676 3.198225 3.172762 2.822896
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4]
## [1,] 3.333897 2.435485 1.424455 1.643905
## [2,] 2.112022 2.332843 1.660575 2.160563
## [3,] 2.852676 3.198225 3.172762 2.822896
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.569202 1.4778327 1.276787 2.0047469 1.9700269
## [2,] 1.778987 1.4945064 1.258138 1.9728710 1.4620508
## [3,] 1.153691 1.2310064 1.503149 0.9119817 1.4579644
## [4,] 1.145932 0.9078059 1.249759 2.4170386 0.9068294
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.5692018 1.4778327 1.2767871 2.0047469 1.9700269
## [2,] 1.7789868 1.4945064 1.2581380 1.9728710 1.4620508
## [3,] 1.1536913 1.2310064 1.5031486 0.9119817 1.4579644
## [4,] 1.1459317 0.9078059 1.2497588 2.4170386 0.9068294
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.569202 1.4778327 1.276787 2.0047469 1.9700269
## [2,] 1.778987 1.4945064 1.258138 1.9728710 1.4620508
## [3,] 1.153691 1.2310064 1.503149 0.9119817 1.4579644
## [4,] 1.145932 0.9078059 1.249759 2.4170386 0.9068294
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.5692018 1.4778327 1.2767871 2.0047469 1.9700269
## [2,] 1.7789868 1.4945064 1.2581380 1.9728710 1.4620508
## [3,] 1.1536913 1.2310064 1.5031486 0.9119817 1.4579644
## [4,] 1.1459317 0.9078059 1.2497588 2.4170386 0.9068294
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.084138
DelayedTensor::einsum('ii->', darrB)
## <1> array of class HDF5Array and type "double":
## [1]
## 1.084138
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.9730454 0.36546789 0.6978592
## [2,] 0.4521147 0.04073512 0.4330534
## [3,] 0.8491393 0.67209222 0.0703576
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> matrix of class DelayedArray and type "double":
## [,1] [,2] [,3]
## [1,] 0.97304543 0.36546789 0.69785922
## [2,] 0.45211468 0.04073512 0.43305337
## [3,] 0.84913926 0.67209222 0.07035760
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.8686202 0.92258730 0.4437798
## [2,] 0.2812429 0.92290392 0.8652479
## [3,] 0.9068190 0.05909214 0.5473617
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.4887164 0.17755825 0.8426398
## [2,] 0.1010495 0.09527509 0.5062799
## [3,] 0.2174616 0.14427442 0.6428554
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.48183546 0.4700926 0.2867172
## [2,] 0.02853322 0.8283994 0.3626023
## [3,] 0.87224790 0.3095202 0.7830420
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> array of class DelayedArray and type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.86862021 0.92258730 0.44377979
## [2,] 0.28124294 0.92290392 0.86524786
## [3,] 0.90681898 0.05909214 0.54736171
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.48871638 0.17755825 0.84263977
## [2,] 0.10104950 0.09527509 0.50627992
## [3,] 0.21746159 0.14427442 0.64285543
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.48183546 0.47009255 0.28671716
## [2,] 0.02853322 0.82839940 0.36260230
## [3,] 0.87224790 0.30952018 0.78304200
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.489396
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> array of class HDF5Array and type "double":
## [1]
## 1.489396
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 5.009664
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> array of class HDF5Array and type "double":
## [1]
## 5.009664
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 19.0494
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> array of class HDF5Array and type "double":
## [1]
## 19.0494
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.9126283 0.8607960 0.7485806 1.6316376 1.5606554
## [2,] 1.2423920 0.8146681 0.6448703 1.7757719 1.0441325
## [3,] 0.6791858 0.5342325 0.9666714 0.3311335 1.0960780
## [4,] 0.4654729 0.3601115 1.0351467 1.9812984 0.3639339
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9126283 0.8607960 0.7485806 1.6316376 1.5606554
## [2,] 1.2423920 0.8146681 0.6448703 1.7757719 1.0441325
## [3,] 0.6791858 0.5342325 0.9666714 0.3311335 1.0960780
## [4,] 0.4654729 0.3601115 1.0351467 1.9812984 0.3639339
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 2.041339 1.668001 1.647008
## [2,] 1.668001 1.473903 1.473534
## [3,] 1.647008 1.473534 1.494422
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 2.041339 1.668001 1.647008
## [2,] 1.668001 1.473903 1.473534
## [3,] 1.647008 1.473534 1.494422
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.4823030 0.5047758 0.4438359
## [2,] 0.6043891 0.3156101 0.4186944
## [3,] 0.2117186 0.3722956 0.4423345
## [4,] 0.7429280 0.2812216 0.1895572
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 0.4823030 0.5047758 0.4438359
## [2,] 0.6043891 0.3156101 0.4186944
## [3,] 0.2117186 0.3722956 0.4423345
## [4,] 0.7429280 0.2812216 0.1895572
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.07708493 0.28547113 0.622568948 0.80116555 0.70199509
## [2,] 0.11810805 0.08026145 0.202973567 0.96934490 0.13945193
## [3,] 0.04521546 0.11473125 0.143918290 0.05505843 0.06712471
## [4,] 0.08543177 0.12001523 0.003459478 0.76449162 0.00518638
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.38264069 0.5283969 0.09198202 0.05158453 0.05575537
## [2,] 0.25061716 0.3929213 0.02663575 0.79732861 0.02227697
## [3,] 0.02603348 0.3020791 0.06381221 0.24152020 0.04225135
## [4,] 0.11441967 0.2340817 0.03759759 0.42960490 0.23925765
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.4529027 0.04692791 0.03402966 0.778887561 0.8029050
## [2,] 0.8736668 0.34148533 0.41526100 0.009098353 0.8824036
## [3,] 0.6079369 0.11742215 0.75894094 0.034554836 0.9867020
## [4,] 0.2656215 0.00601460 0.99408964 0.787201855 0.1194899
DelayedTensor::einsum('ijk,ijk->jki', darrE, darrE)
## <4 x 5 x 3> array of class HDF5Array and type "double":
## ,,1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.077084934 0.285471126 0.622568948 0.801165550 0.701995092
## [2,] 0.118108053 0.080261454 0.202973567 0.969344903 0.139451932
## [3,] 0.045215457 0.114731251 0.143918290 0.055058429 0.067124706
## [4,] 0.085431766 0.120015228 0.003459478 0.764491622 0.005186380
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.38264069 0.52839694 0.09198202 0.05158453 0.05575537
## [2,] 0.25061716 0.39292133 0.02663575 0.79732861 0.02227697
## [3,] 0.02603348 0.30207909 0.06381221 0.24152020 0.04225135
## [4,] 0.11441967 0.23408166 0.03759759 0.42960490 0.23925765
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.452902703 0.046927910 0.034029662 0.778887561 0.802904979
## [2,] 0.873666829 0.341485328 0.415260998 0.009098353 0.882403584
## [3,] 0.607936885 0.117422152 0.758940941 0.034554836 0.986701961
## [4,] 0.265621474 0.006014600 0.994089639 0.787201855 0.119489912
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 1.126237 1.6188051 2.902770
## [2,] 1.502752 2.3871806 1.221219
## [3,] 1.677739 0.9130018 2.697091
## [4,] 2.988629 2.2669444 2.051065
## [5,] 1.542385 1.0800715 3.174415
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> matrix of class HDF5Matrix and type "double":
## [,1] [,2] [,3]
## [1,] 1.1262367 1.6188051 2.9027698
## [2,] 1.5027517 2.3871806 1.2212190
## [3,] 1.6777392 0.9130018 2.6970914
## [4,] 2.9886291 2.2669444 2.0510646
## [5,] 1.5423850 1.0800715 3.1744149
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.03221784 0.11931337 0.260204244 0.33484914 0.293400599
## [2,] 0.06185903 0.04203690 0.106307302 0.50769390 0.073037878
## [3,] 0.00829570 0.02104979 0.026404752 0.01010159 0.012315399
## [4,] 0.05500132 0.07726630 0.002227226 0.49218284 0.003339013
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.073644667 0.10169754 0.017703254 0.009928179 0.010730918
## [2,] 0.030158753 0.04728334 0.003205291 0.095948883 0.002680765
## [3,] 0.003695488 0.04288054 0.009058232 0.034284125 0.005997637
## [4,] 0.012268771 0.02509965 0.004031442 0.046064841 0.025654656
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.15480219 0.0160399641 0.01163134 0.266223841 0.2744330
## [2,] 0.28170379 0.1101080054 0.13389612 0.002933659 0.2845209
## [3,] 0.20708994 0.0399991292 0.25852853 0.011770891 0.3361139
## [4,] 0.03877512 0.0008780046 0.14511608 0.114914838 0.0174430
DelayedTensor::einsum('i,ij,ijk,ijk,ji->jki',
darrA, darrC, darrE, darrE, t(darrC))
## <4 x 5 x 3> array of class HDF5Array and type "double":
## ,,1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.032217840 0.119313369 0.260204244 0.334849139 0.293400599
## [2,] 0.061859032 0.042036896 0.106307302 0.507693900 0.073037878
## [3,] 0.008295700 0.021049793 0.026404752 0.010101594 0.012315399
## [4,] 0.055001321 0.077266296 0.002227226 0.492182840 0.003339013
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.073644667 0.101697539 0.017703254 0.009928179 0.010730918
## [2,] 0.030158753 0.047283344 0.003205291 0.095948883 0.002680765
## [3,] 0.003695488 0.042880543 0.009058232 0.034284125 0.005997637
## [4,] 0.012268771 0.025099655 0.004031442 0.046064841 0.025654656
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.1548021859 0.0160399641 0.0116313416 0.2662238407 0.2744329964
## [2,] 0.2817037922 0.1101080054 0.1338961193 0.0029336590 0.2845208582
## [3,] 0.2070899355 0.0399991292 0.2585285324 0.0117708908 0.3361139138
## [4,] 0.0387751227 0.0008780046 0.1451160826 0.1149148376 0.0174430024
einsum
By using einsum
and other DelayedTensor functions,
it is possible to implement your original tensor calculation functions.
It is intended to be applied to Delayed Arrays,
which can scale to large-scale data
since the calculation is performed internally by block processing.
For example, kronecker
can be easily implmented by eimsum
and other DelayedTensor functions4 https://stackoverflow.com/
questions/56067643/speeding-up-kronecker-products-numpy
(the kronecker
function inside DelayedTensor
has a more efficient implementation though).
darr1 <- DelayedArray(array(1:6, dim=c(2,3)))
darr2 <- DelayedArray(array(20:1, dim=c(4,5)))
mykronecker <- function(darr1, darr2){
stopifnot((length(dim(darr1)) == 2) && (length(dim(darr2)) == 2))
# Outer Product
tmpdarr <- DelayedTensor::einsum('ij,kl->ikjl', darr1, darr2)
# Reshape
DelayedTensor::unfold(tmpdarr, row_idx=c(2,1), col_idx=c(4,3))
}
identical(as.array(DelayedTensor::kronecker(darr1, darr2)),
as.array(mykronecker(darr1, darr2)))
## [1] TRUE
## R version 4.1.1 (2021-08-10)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows Server x64 (build 17763)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=C
## [2] LC_CTYPE=English_United States.1252
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.1252
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] einsum_0.1.0 DelayedRandomArray_1.1.0 HDF5Array_1.21.0
## [4] rhdf5_2.37.4 DelayedArray_0.19.4 IRanges_2.27.2
## [7] S4Vectors_0.31.5 MatrixGenerics_1.5.4 matrixStats_0.61.0
## [10] BiocGenerics_0.39.2 Matrix_1.3-4 DelayedTensor_0.99.12
## [13] BiocStyle_2.21.4
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.7 rTensor_1.4.8 bslib_0.3.1
## [4] compiler_4.1.1 BiocManager_1.30.16 jquerylib_0.1.4
## [7] rhdf5filters_1.5.0 tools_4.1.1 digest_0.6.28
## [10] jsonlite_1.7.2 evaluate_0.14 lattice_0.20-45
## [13] rlang_0.4.12 parallel_4.1.1 yaml_2.2.1
## [16] xfun_0.27 fastmap_1.1.0 stringr_1.4.0
## [19] knitr_1.36 sass_0.4.0 grid_4.1.1
## [22] R6_2.5.1 BiocParallel_1.27.17 rmarkdown_2.11
## [25] bookdown_0.24 irlba_2.3.3 Rhdf5lib_1.15.2
## [28] magrittr_2.0.1 BiocSingular_1.9.1 htmltools_0.5.2
## [31] rsvd_1.0.5 beachmat_2.9.1 dqrng_0.3.0
## [34] ScaledMatrix_1.1.0 stringi_1.7.5