DelayedTensor 1.9.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2023-10-20 14:48:21.036968
Compiled: Sun Apr 21 18:32:41 2024
einsum
einsum
is an easy and intuitive way to write tensor operations.
It was originally introduced by
Numpy
1 https://numpy.org/doc/stable/reference/generated/numpy.einsum.html
package of Python but similar tools have been implemented in other languages
(e.g. R, Julia) inspired by Numpy
.
In this vignette, we will use CRAN einsum package first.
einsum
is named after
Einstein summation2 https://en.wikipedia.org/wiki/Einstein_notation
introduced by Albert Einstein,
which is a notational convention that implies summation over
a set of indexed terms in a formula.
Here, we consider a simple example of einsum
; matrix multiplication.
If we naively implement the matrix multiplication,
the calculation would look like the following in a for loop.
A <- matrix(runif(3*4), nrow=3, ncol=4)
B <- matrix(runif(4*5), nrow=4, ncol=5)
C <- matrix(0, nrow=3, ncol=5)
I <- nrow(A)
J <- ncol(A)
K <- ncol(B)
for(i in 1:I){
for(j in 1:J){
for(k in 1:K){
C[i,k] = C[i,k] + A[i,j] * B[j,k]
}
}
}
Therefore, any programming language can implement this. However, when analyzing tensor data, such operations tend to be more complicated and increase the possibility of causing bugs because the order of tensors is larger or more tensors are handled simultaneously. In addition, several programming languages, especially R, are known to significantly slow down the speed of computation if the code is written in for loop.
Obviously, in the case of the R language, it should be executed using the built-in matrix multiplication function (%*%) prepared by the R, as shown below.
C <- A %*% B
However, more complex operations than matrix multiplication are not always provided by programming languages as standard.
einsum
is a function that solves such a problem.
To put it simply, einsum
is a wrapper for the for loop above.
Like the Einstein summation, it omits many notations such as for,
array size (e.g. I, J, and K), brackets (e.g. {}, (), and []),
and even addition operator (+) and
extracts the array subscripts (e.g. i, j, and k)
to concisely express the tensor operation as follows.
suppressPackageStartupMessages(library("einsum"))
C <- einsum('ij,jk->ik', A, B)
DelayedTensor
CRAN einsum is easy to use because the syntax is almost
the same as that of Numpy
‘s einsum
,
except that it prohibits the implicit modes that do not use’->’.
It is extremely fast because the internal calculation
is actually performed by C++.
When the input tensor is huge, however,
it is not scalable because it assumes that the input is R’s standard array.
Using einsum
of DelayedTensor,
we can augment the CRAN einsum
’s functionality;
in DelayedTensor,
the input DelayedArray objects are divided into
multiple block tensors and the CRAN einsum
is incremently applied in the block processing.
A surprisingly large number of tensor operations can be handled
uniformly in einsum
.
In more detail, einsum
is capable of performing any tensor operation
that can be described by a combination of the following
three operations3 https://ajcr.net/Basic-guide-to-einsum/.
Some typical operations are introduced below. Here we use the arrays and DelayedArray objects below.
suppressPackageStartupMessages(library("DelayedTensor"))
suppressPackageStartupMessages(library("DelayedArray"))
arrA <- array(runif(3), dim=c(3))
arrB <- array(runif(3*3), dim=c(3,3))
arrC <- array(runif(3*4), dim=c(3,4))
arrD <- array(runif(3*3*3), dim=c(3,3,3))
arrE <- array(runif(3*4*5), dim=c(3,4,5))
darrA <- DelayedArray(arrA)
darrB <- DelayedArray(arrB)
darrC <- DelayedArray(arrC)
darrD <- DelayedArray(arrD)
darrE <- DelayedArray(arrE)
If the same subscript is written on both sides of ->,
einsum
will simply output the object without any calculation.
einsum::einsum('i->i', arrA)
## [1] 0.6394085 0.9262418 0.7607977
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.6394085 0.9262418 0.7607977
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.6430409 0.519048773 0.2359743 0.48357783
## [2,] 0.8809862 0.005757153 0.6994503 0.77848179
## [3,] 0.4997926 0.670941666 0.6160042 0.08827749
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.643040911 0.519048773 0.235974267 0.483577833
## [2,] 0.880986226 0.005757153 0.699450286 0.778481789
## [3,] 0.499792615 0.670941666 0.616004247 0.088277491
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4108867 0.35381944 0.8095215 0.5359701
## [2,] 0.1472493 0.07238012 0.3650979 0.2183037
## [3,] 0.1237199 0.11245590 0.6656307 0.5601964
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4464066 0.15502420 0.3292357 0.8637368
## [2,] 0.1537636 0.07328258 0.5553028 0.2209091
## [3,] 0.3522670 0.64632858 0.3062765 0.6570930
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8921419 0.9056840 0.4768032 0.7248723
## [2,] 0.6731026 0.7595508 0.1962999 0.2119469
## [3,] 0.8784443 0.4270215 0.3488327 0.5862515
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3455586 0.6998678 0.1799125 0.74470591
## [2,] 0.7620987 0.7202451 0.3386742 0.45782686
## [3,] 0.1004458 0.2061962 0.6096622 0.08953362
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1117204 0.3591447 0.8959606 0.6535566
## [2,] 0.9185780 0.7161588 0.5650194 0.1780223
## [3,] 0.3638667 0.7962281 0.2058537 0.1020685
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.41088673 0.35381944 0.80952151 0.53597011
## [2,] 0.14724928 0.07238012 0.36509787 0.21830369
## [3,] 0.12371990 0.11245590 0.66563065 0.56019640
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.44640662 0.15502420 0.32923568 0.86373675
## [2,] 0.15376360 0.07328258 0.55530278 0.22090907
## [3,] 0.35226701 0.64632858 0.30627646 0.65709296
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.8921419 0.9056840 0.4768032 0.7248723
## [2,] 0.6731026 0.7595508 0.1962999 0.2119469
## [3,] 0.8784443 0.4270215 0.3488327 0.5862515
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.34555858 0.69986783 0.17991255 0.74470591
## [2,] 0.76209873 0.72024513 0.33867424 0.45782686
## [3,] 0.10044585 0.20619616 0.60966220 0.08953362
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.1117204 0.3591447 0.8959606 0.6535566
## [2,] 0.9185780 0.7161588 0.5650194 0.1780223
## [3,] 0.3638667 0.7962281 0.2058537 0.1020685
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.7082281 0.8254049 0.7865241
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.7082281 0.8254049 0.7865241
einsum::einsum('iii->i', arrD)
## [1] 0.2451412 0.1771011 0.5747884
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.2451412 0.1771011 0.5747884
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.4088433 0.8579238 0.5788132
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.4088433 0.8579238 0.5788132
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.4135016 2.694116e-01 0.05568385 0.233847520
## [2,] 0.7761367 3.314481e-05 0.48923070 0.606033895
## [3,] 0.2497927 4.501627e-01 0.37946123 0.007792915
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 4.135016e-01 2.694116e-01 5.568385e-02 2.338475e-01
## [2,] 7.761367e-01 3.314481e-05 4.892307e-01 6.060339e-01
## [3,] 2.497927e-01 4.501627e-01 3.794612e-01 7.792915e-03
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.16882791 0.125188198 0.6553251 0.2872640
## [2,] 0.02168235 0.005238882 0.1332965 0.0476565
## [3,] 0.01530661 0.012646330 0.4430642 0.3138200
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.19927887 0.024032502 0.10839613 0.74604118
## [2,] 0.02364324 0.005370337 0.30836118 0.04880082
## [3,] 0.12409205 0.417740639 0.09380527 0.43177115
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7959172 0.8202636 0.22734126 0.52543982
## [2,] 0.4530671 0.5769175 0.03853367 0.04492151
## [3,] 0.7716643 0.1823473 0.12168423 0.34369085
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11941073 0.48981497 0.03236852 0.554586891
## [2,] 0.58079447 0.51875305 0.11470024 0.209605435
## [3,] 0.01008937 0.04251686 0.37168800 0.008016269
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01248144 0.1289849 0.80274544 0.42713627
## [2,] 0.84378554 0.5128835 0.31924693 0.03169194
## [3,] 0.13239896 0.6339792 0.04237575 0.01041797
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.168827907 0.125188198 0.655325079 0.287263962
## [2,] 0.021682351 0.005238882 0.133296454 0.047656501
## [3,] 0.015306614 0.012646330 0.443064164 0.313820001
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.199278867 0.024032502 0.108396134 0.746041177
## [2,] 0.023643245 0.005370337 0.308361179 0.048800818
## [3,] 0.124092048 0.417740639 0.093805267 0.431771155
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.79591721 0.82026357 0.22734126 0.52543982
## [2,] 0.45306712 0.57691746 0.03853367 0.04492151
## [3,] 0.77166430 0.18234733 0.12168423 0.34369085
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.119410735 0.489814973 0.032368525 0.554586891
## [2,] 0.580794474 0.518753053 0.114700243 0.209605435
## [3,] 0.010089368 0.042516857 0.371687996 0.008016269
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.01248144 0.12898493 0.80274544 0.42713627
## [2,] 0.84378554 0.51288347 0.31924693 0.03169194
## [3,] 0.13239896 0.63397924 0.04237575 0.01041797
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.4088433 0.5922469 0.4864606
## [2,] 0.5922469 0.8579238 0.7046826
## [3,] 0.4864606 0.7046826 0.5788132
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.4088433 0.5922469 0.4864606
## [2,] 0.5922469 0.8579238 0.7046826
## [3,] 0.4864606 0.7046826 0.5788132
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2642170 0.213270255 0.0969587 0.19869572
## [2,] 0.3619856 0.002365538 0.2873948 0.31986784
## [3,] 0.2053582 0.275681029 0.2531080 0.03627205
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09468731 0.0764295590 0.03474704 0.07120649
## [2,] 0.12972459 0.0008477366 0.10299355 0.11463088
## [3,] 0.07359410 0.0987956783 0.09070618 0.01299880
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07955696 0.0642166623 0.02919471 0.05982820
## [2,] 0.10899553 0.0007122744 0.08653592 0.09631369
## [3,] 0.06183429 0.0830088357 0.07621198 0.01092168
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2275204 0.183649548 0.08349228 0.17109924
## [2,] 0.3117101 0.002036993 0.24747911 0.27544199
## [3,] 0.1768363 0.237392206 0.21795428 0.03123429
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04654338 0.0375688138 0.01707985 0.035001423
## [2,] 0.06376589 0.0004167034 0.05062630 0.056346607
## [3,] 0.03617505 0.0485628400 0.04458646 0.006389536
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07231375 0.0583700984 0.02653670 0.054381182
## [2,] 0.09907210 0.0006474258 0.07865731 0.087544872
## [3,] 0.05620463 0.0754513508 0.06927331 0.009927325
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5205555 0.420181148 0.1910262 0.39146666
## [2,] 0.7131773 0.004660539 0.5662201 0.63019775
## [3,] 0.4045929 0.543141712 0.4986687 0.07146253
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2347729 0.189503601 0.0861537 0.17655324
## [2,] 0.3216462 0.002101924 0.2553678 0.28422204
## [3,] 0.1824732 0.244959372 0.2249018 0.03222992
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4280277 0.345494773 0.1570717 0.3218842
## [2,] 0.5864114 0.003832137 0.4655755 0.5181813
## [3,] 0.3326773 0.446599338 0.4100313 0.0587602
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3446507 0.278194630 0.1264752 0.2591833
## [2,] 0.4721823 0.003085662 0.3748844 0.4172430
## [3,] 0.2678739 0.359604680 0.3301599 0.0473141
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1403782 0.113310263 0.05151405 0.1055668
## [2,] 0.1923225 0.001256808 0.15269258 0.1699454
## [3,] 0.1091066 0.146469042 0.13447600 0.0192713
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3602292 0.290769252 0.1321919 0.27089856
## [2,] 0.4935253 0.003225136 0.3918295 0.43610269
## [3,] 0.2799820 0.375859102 0.3450834 0.04945273
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2870577 0.231706807 0.1053405 0.21587234
## [2,] 0.3932781 0.002570031 0.3122392 0.34751942
## [3,] 0.2231107 0.299512799 0.2749884 0.03940766
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09887629 0.0798108082 0.03628425 0.07435667
## [2,] 0.13546361 0.0008852405 0.10754999 0.11970216
## [3,] 0.07684991 0.1031664062 0.09471903 0.01357386
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2265221 0.182843761 0.08312595 0.17034852
## [2,] 0.3103424 0.002028055 0.24639326 0.27423345
## [3,] 0.1760605 0.236350616 0.21699798 0.03109725
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09968690 0.080465120 0.03658172 0.07496627
## [2,] 0.13657418 0.000892498 0.10843172 0.12068352
## [3,] 0.07747995 0.104012194 0.09549556 0.01368515
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04712370 0.038037236 0.01729280 0.035437833
## [2,] 0.06456095 0.000421899 0.05125752 0.057049157
## [3,] 0.03662609 0.049168339 0.04514238 0.006469203
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4156157 0.335476059 0.1525169 0.31255018
## [2,] 0.5694066 0.003721012 0.4520747 0.50315503
## [3,] 0.3230303 0.433648777 0.3981412 0.05705627
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2117120 0.17088938 0.07769115 0.1592111
## [2,] 0.2900521 0.00189546 0.23028399 0.2563040
## [3,] 0.1645496 0.22089794 0.20281058 0.0290641
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3570824 0.288229228 0.1310372 0.26853212
## [2,] 0.4892141 0.003196963 0.3884067 0.43229310
## [3,] 0.2775362 0.372575773 0.3420689 0.04902074
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1969483 0.15897242 0.07227336 0.14810850
## [2,] 0.2698253 0.00176328 0.21422515 0.23843064
## [3,] 0.1530747 0.20549364 0.18866760 0.02703732
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5554181 0.448321502 0.2038196 0.41768395
## [2,] 0.7609402 0.004972664 0.6041409 0.67240333
## [3,] 0.4316893 0.579516975 0.5320655 0.07624851
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1420536 0.114662583 0.05212886 0.1068267
## [2,] 0.1946178 0.001271807 0.15451491 0.1719737
## [3,] 0.1104087 0.148217101 0.13608093 0.0195013
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4225377 0.341063294 0.1550570 0.31775559
## [2,] 0.5788898 0.003782985 0.4596039 0.51153490
## [3,] 0.3284102 0.440871043 0.4047721 0.05800652
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5736838 0.463065172 0.2105225 0.43142006
## [2,] 0.7859647 0.005136197 0.6240089 0.69451624
## [3,] 0.4458859 0.598575189 0.5495632 0.07875605
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4328325 0.349373084 0.1588349 0.32549750
## [2,] 0.5929941 0.003875155 0.4708018 0.52399812
## [3,] 0.3364117 0.451612586 0.4146341 0.05941981
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5648756 0.455955411 0.2072902 0.42479617
## [2,] 0.7738973 0.005057338 0.6144281 0.68385285
## [3,] 0.4390399 0.589384849 0.5411254 0.07754685
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5823919 0.470094187 0.2137181 0.43796872
## [2,] 0.7978952 0.005214161 0.6334810 0.70505853
## [3,] 0.4526542 0.607661155 0.5579052 0.07995151
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4884223 0.39424393 0.1792345 0.36730194
## [2,] 0.6691538 0.00437285 0.5312680 0.59129649
## [3,] 0.3796179 0.50961430 0.4678865 0.06705124
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2745923 0.221644966 0.1007661 0.20649811
## [2,] 0.3762000 0.002458428 0.2986803 0.33242843
## [3,] 0.2134222 0.286506491 0.2630470 0.03769638
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3066039 0.247484097 0.1125133 0.23057144
## [2,] 0.4200570 0.002745029 0.3335001 0.37118258
## [3,] 0.2383027 0.319907108 0.2937128 0.04209099
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12622889 0.101889242 0.04632173 0.09492630
## [2,] 0.17293754 0.001130129 0.13730205 0.15281593
## [3,] 0.09810926 0.131705807 0.12092159 0.01732887
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2243137 0.181061170 0.08231553 0.16868775
## [2,] 0.3073168 0.002008283 0.24399111 0.27155988
## [3,] 0.1743440 0.234046373 0.21488241 0.03079407
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4661225 0.3762441 0.1710512 0.35053217
## [2,] 0.6386025 0.0041732 0.5070121 0.56429987
## [3,] 0.3622858 0.4863470 0.4465244 0.06398991
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1362906 0.110010801 0.05001402 0.10249284
## [2,] 0.1867223 0.001220211 0.14824635 0.16499684
## [3,] 0.1059295 0.142204035 0.13056022 0.01871014
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3769837 0.30429314 0.1383403 0.28349824
## [2,] 0.5164795 0.00337514 0.4100538 0.45638614
## [3,] 0.2930042 0.39334058 0.3611334 0.05175281
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2222083 0.179361759 0.08154293 0.16710447
## [2,] 0.3044324 0.001989434 0.24170105 0.26901106
## [3,] 0.1727076 0.231849652 0.21286556 0.03050504
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4900607 0.395566411 0.1798357 0.36853405
## [2,] 0.6713985 0.004387519 0.5330502 0.59327998
## [3,] 0.3808913 0.511323791 0.4694561 0.06727616
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06459079 0.0521362931 0.02370263 0.048573384
## [2,] 0.08849141 0.0005782821 0.07025688 0.078195262
## [3,] 0.05020209 0.0673933031 0.06187507 0.008867107
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4500436 0.363265536 0.1651508 0.33844057
## [2,] 0.6165739 0.004029246 0.4895228 0.54483436
## [3,] 0.3497888 0.469570484 0.4311216 0.06178258
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4631471 0.373842353 0.1699593 0.34829458
## [2,] 0.6345260 0.004146561 0.5037757 0.56069772
## [3,] 0.3599732 0.483242470 0.4436741 0.06358143
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1325926 0.107025865 0.04865699 0.09971189
## [2,] 0.1816560 0.001187103 0.14422396 0.16051996
## [3,] 0.1030553 0.138345597 0.12701771 0.01820248
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11569113 0.093383387 0.04245473 0.08700172
## [2,] 0.15850048 0.001035784 0.12583988 0.14005864
## [3,] 0.08991896 0.120710824 0.11082689 0.01588223
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2177814 0.175788451 0.07991841 0.16377536
## [2,] 0.2983673 0.001949799 0.23688580 0.26365173
## [3,] 0.1692669 0.227230661 0.20862477 0.02989731
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3920377 0.316444416 0.1438646 0.29481912
## [2,] 0.5371040 0.003509918 0.4264284 0.47461092
## [3,] 0.3047047 0.409047771 0.3755545 0.05381945
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4788764 0.386538689 0.1757314 0.36012327
## [2,] 0.6560756 0.004287386 0.5208848 0.57973999
## [3,] 0.3721985 0.499654223 0.4587420 0.06574077
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2944014 0.237634471 0.1080354 0.22139492
## [2,] 0.4033392 0.002635779 0.3202271 0.35640987
## [3,] 0.2288185 0.307175117 0.2820233 0.04041581
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05757378 0.0464723143 0.02112763 0.043296473
## [2,] 0.07887788 0.0005154587 0.06262431 0.069700291
## [3,] 0.04474824 0.0600718345 0.05515309 0.007903803
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07184076 0.0579883154 0.02636313 0.054025489
## [2,] 0.09842410 0.0006431912 0.07814284 0.086972265
## [3,] 0.05583701 0.0749578439 0.06882022 0.009862393
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5906832 0.476786783 0.2167608 0.44420396
## [2,] 0.8092546 0.005288394 0.6424996 0.71509624
## [3,] 0.4590985 0.616312252 0.5658479 0.08108976
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2339812 0.188864557 0.08586317 0.17595786
## [2,] 0.3205615 0.002094836 0.25450666 0.28326359
## [3,] 0.1818579 0.244133320 0.22414342 0.03212124
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2309448 0.186413629 0.08474891 0.1736744
## [2,] 0.3164016 0.002067651 0.25120388 0.2795876
## [3,] 0.1794979 0.240965160 0.22123468 0.0317044
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4605194 0.371721362 0.1689951 0.3463185
## [2,] 0.6309261 0.004123036 0.5009175 0.5575166
## [3,] 0.3579309 0.480500798 0.4411569 0.0632207
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5120073 0.413281235 0.1878893 0.38503827
## [2,] 0.7014660 0.004584007 0.5569220 0.61984910
## [3,] 0.3979489 0.534222629 0.4904799 0.07028902
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5761393 0.465047262 0.2114237 0.43326670
## [2,] 0.7893290 0.005158182 0.6266799 0.69748903
## [3,] 0.4477945 0.601137312 0.5519155 0.07909316
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3633306 0.293272631 0.1333300 0.2732309
## [2,] 0.4977743 0.003252903 0.3952030 0.4398573
## [3,] 0.2823925 0.379095063 0.3480544 0.0498785
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1323724 0.106848121 0.04857618 0.09954630
## [2,] 0.1813543 0.001185131 0.14398444 0.16025337
## [3,] 0.1028842 0.138115838 0.12680677 0.01817225
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4202637 0.339227768 0.1542225 0.31604550
## [2,] 0.5757744 0.003762625 0.4571304 0.50878194
## [3,] 0.3266428 0.438498376 0.4025937 0.05769434
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11447563 0.092402262 0.04200868 0.08608764
## [2,] 0.15683520 0.001024902 0.12451776 0.13858713
## [3,] 0.08897424 0.119442586 0.10966250 0.01571536
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06563421 0.0529785182 0.02408553 0.049358053
## [2,] 0.08992092 0.0005876238 0.07139183 0.079458451
## [3,] 0.05101307 0.0684819945 0.06287462 0.009010349
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.264216979 0.213270255 0.096958696 0.198695716
## [2,] 0.361985552 0.002365538 0.287394843 0.319867839
## [3,] 0.205358155 0.275681029 0.253107973 0.036272050
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.0946873122 0.0764295590 0.0347470413 0.0712064885
## [2,] 0.1297245890 0.0008477366 0.1029935522 0.1146308842
## [3,] 0.0735941036 0.0987956783 0.0907061829 0.0129987971
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.0795569571 0.0642166623 0.0291947127 0.0598282011
## [2,] 0.1089955277 0.0007122744 0.0865359194 0.0963136890
## [3,] 0.0618342924 0.0830088357 0.0762119838 0.0109216823
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.420263652 0.339227768 0.154222547 0.316045500
## [2,] 0.575774391 0.003762625 0.457130374 0.508781936
## [3,] 0.326642779 0.438498376 0.402593661 0.057694339
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.114475629 0.092402262 0.042008684 0.086087644
## [2,] 0.156835204 0.001024902 0.124517757 0.138587128
## [3,] 0.088974237 0.119442586 0.109662500 0.015715363
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.0656342069 0.0529785182 0.0240855342 0.0493580533
## [2,] 0.0899209231 0.0005876238 0.0713918260 0.0794584511
## [3,] 0.0510130715 0.0684819945 0.0628746158 0.0090103491
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.326448
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 2.326448
einsum::einsum('ij->', arrC)
## [1] 6.121333
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 6.121333
einsum::einsum('ijk->', arrE)
## [1] 27.33672
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 27.33672
einsum::einsum('ij->i', arrC)
## [1] 1.881642 2.364675 1.875016
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 1.881642 2.364675 1.875016
einsum::einsum('ij->j', arrC)
## [1] 2.023820 1.195748 1.551429 1.350337
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 2.023820 1.195748 1.551429 1.350337
einsum::einsum('ijk->i', arrE)
## [1] 10.894530 8.303813 8.138373
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 10.894530 8.303813 8.138373
einsum::einsum('ijk->j', arrE)
## [1] 6.680250 7.003388 6.848083 6.804994
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 6.680250 7.003388 6.848083 6.804994
einsum::einsum('ijk->k', arrE)
## [1] 4.375232 4.759626 7.080952 5.254728 5.866178
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 4.375232 4.759626 7.080952 5.254728 5.866178
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 2.206714 2.473540 2.691434 3.522842
## [2,] 2.654792 2.341617 2.020394 1.287009
## [3,] 1.818744 2.188230 2.136256 1.995143
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 2.206714 2.473540 2.691434 3.522842
## [2,] 2.654792 2.341617 2.020394 1.287009
## [3,] 1.818744 2.188230 2.136256 1.995143
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.6818559 0.9524372 2.443689 1.208103 1.3941650
## [2,] 0.5386555 0.8746354 2.092256 1.626309 1.8715317
## [3,] 1.8402500 1.1908149 1.021936 1.128249 1.6668338
## [4,] 1.3144702 1.7417388 1.523071 1.292066 0.9336474
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.6818559 0.9524372 2.4436888 1.2081032 1.3941650
## [2,] 0.5386555 0.8746354 2.0922563 1.6263091 1.8715317
## [3,] 1.8402500 1.1908149 1.0219358 1.1282490 1.6668338
## [4,] 1.3144702 1.7417388 1.5230707 1.2920664 0.9336474
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.6818559 0.9524372 2.443689 1.208103 1.3941650
## [2,] 0.5386555 0.8746354 2.092256 1.626309 1.8715317
## [3,] 1.8402500 1.1908149 1.021936 1.128249 1.6668338
## [4,] 1.3144702 1.7417388 1.523071 1.292066 0.9336474
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.6818559 0.9524372 2.4436888 1.2081032 1.3941650
## [2,] 0.5386555 0.8746354 2.0922563 1.6263091 1.8715317
## [3,] 1.8402500 1.1908149 1.0219358 1.1282490 1.6668338
## [4,] 1.3144702 1.7417388 1.5230707 1.2920664 0.9336474
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 2.320157
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 2.320157
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.7082281 0.8420889 0.3209009
## [2,] 0.9424336 0.8254049 0.8591519
## [3,] 0.6056071 0.7377374 0.7865241
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.7082281 0.8420889 0.3209009
## [2,] 0.9424336 0.8254049 0.8591519
## [3,] 0.6056071 0.7377374 0.7865241
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.2451412 0.006251923 0.41560796
## [2,] 0.9107019 0.986779925 0.05569849
## [3,] 0.5314574 0.858682551 0.30557225
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.2182638 0.9853703 0.7668089
## [2,] 0.2232491 0.1771011 0.1173196
## [3,] 0.4697268 0.7793093 0.6107162
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.35706139 0.82623414 0.3758665
## [2,] 0.07623507 0.08999767 0.6996640
## [3,] 0.90370824 0.51599357 0.5747884
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.245141188 0.006251923 0.415607961
## [2,] 0.910701892 0.986779925 0.055698487
## [3,] 0.531457429 0.858682551 0.305572253
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.2182638 0.9853703 0.7668089
## [2,] 0.2232491 0.1771011 0.1173196
## [3,] 0.4697268 0.7793093 0.6107162
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.35706139 0.82623414 0.37586653
## [2,] 0.07623507 0.08999767 0.69966403
## [3,] 0.90370824 0.51599357 0.57478843
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.84558
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.84558
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 3.931089
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 3.931089
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 16.61291
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 16.61291
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.2058169 0.3470142 2.0206486 0.7102946 0.9886659
## [2,] 0.1430734 0.4471435 1.5795284 1.0510849 1.2758476
## [3,] 1.2316857 0.5105626 0.3875592 0.5187568 1.1643681
## [4,] 0.6487405 1.2266131 0.9140522 0.7722086 0.4692462
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.2058169 0.3470142 2.0206486 0.7102946 0.9886659
## [2,] 0.1430734 0.4471435 1.5795284 1.0510849 1.2758476
## [3,] 1.2316857 0.5105626 0.3875592 0.5187568 1.1643681
## [4,] 0.6487405 1.2266131 0.9140522 0.7722086 0.4692462
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 0.9724446 1.1110072 0.8576887
## [2,] 1.1110072 1.8714345 0.9437599
## [3,] 0.8576887 0.9437599 1.0872095
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.9724446 1.1110072 0.8576887
## [2,] 1.1110072 1.8714345 0.9437599
## [3,] 0.8576887 0.9437599 1.0872095
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.41350161 7.761367e-01 0.249792658
## [2,] 0.26941163 3.314481e-05 0.450162719
## [3,] 0.05568385 4.892307e-01 0.379461233
## [4,] 0.23384752 6.060339e-01 0.007792915
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 4.135016e-01 7.761367e-01 2.497927e-01
## [2,] 2.694116e-01 3.314481e-05 4.501627e-01
## [3,] 5.568385e-02 4.892307e-01 3.794612e-01
## [4,] 2.338475e-01 6.060339e-01 7.792915e-03
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.1688279 0.1992789 0.7959172 0.11941073 0.01248144
## [2,] 0.1251882 0.0240325 0.8202636 0.48981497 0.12898493
## [3,] 0.6553251 0.1083961 0.2273413 0.03236852 0.80274544
## [4,] 0.2872640 0.7460412 0.5254398 0.55458689 0.42713627
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.021682351 0.023643245 0.45306712 0.5807945 0.84378554
## [2,] 0.005238882 0.005370337 0.57691746 0.5187531 0.51288347
## [3,] 0.133296454 0.308361179 0.03853367 0.1147002 0.31924693
## [4,] 0.047656501 0.048800818 0.04492151 0.2096054 0.03169194
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.01530661 0.12409205 0.7716643 0.010089368 0.13239896
## [2,] 0.01264633 0.41774064 0.1823473 0.042516857 0.63397924
## [3,] 0.44306416 0.09380527 0.1216842 0.371687996 0.04237575
## [4,] 0.31382000 0.43177115 0.3436909 0.008016269 0.01041797
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.16882791 0.19927887 0.79591721 0.11941073 0.01248144
## [2,] 0.12518820 0.02403250 0.82026357 0.48981497 0.12898493
## [3,] 0.65532508 0.10839613 0.22734126 0.03236852 0.80274544
## [4,] 0.28726396 0.74604118 0.52543982 0.55458689 0.42713627
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.021682351 0.023643245 0.453067124 0.580794474 0.843785537
## [2,] 0.005238882 0.005370337 0.576917461 0.518753053 0.512883468
## [3,] 0.133296454 0.308361179 0.038533665 0.114700243 0.319246932
## [4,] 0.047656501 0.048800818 0.044921507 0.209605435 0.031691943
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.015306614 0.124092048 0.771664302 0.010089368 0.132398964
## [2,] 0.012646330 0.417740639 0.182347329 0.042516857 0.633979237
## [3,] 0.443064164 0.093805267 0.121684232 0.371687996 0.042375754
## [4,] 0.313820001 0.431771155 0.343690853 0.008016269 0.010417974
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 2.110198 0.803031 1.462003
## [2,] 1.794403 1.003258 1.961965
## [3,] 2.999501 1.840900 2.240550
## [4,] 1.970045 2.278845 1.005838
## [5,] 2.020382 2.377779 1.468017
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 2.110198 0.803031 1.462003
## [2,] 1.794403 1.003258 1.961965
## [3,] 2.999501 1.840900 2.240550
## [4,] 1.970045 2.278845 1.005838
## [5,] 2.020382 2.377779 1.468017
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.04463750 0.052688628 0.210437697 0.031571776 0.003300048
## [2,] 0.02156543 0.004139937 0.141301964 0.084377534 0.022219473
## [3,] 0.02333267 0.003859415 0.008094425 0.001152473 0.028581537
## [4,] 0.04295289 0.111551138 0.078565918 0.082924107 0.063867168
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.558723e-02 1.699690e-02 3.257055e-01 4.175275e-01 6.065892e-01
## [2,] 1.608342e-07 1.648699e-07 1.771143e-05 1.592577e-05 1.574558e-05
## [3,] 6.040274e-02 1.397326e-01 1.746137e-02 5.197595e-02 1.446654e-01
## [4,] 2.675121e-02 2.739355e-02 2.521596e-02 1.176586e-01 1.778976e-02
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.002908895 0.023582662 0.146648384 1.917400e-03 2.516132e-02
## [2,] 0.004331150 0.143068971 0.062450819 1.456129e-02 2.171270e-01
## [3,] 0.127909629 0.027080947 0.035129415 1.073038e-01 1.223359e-02
## [4,] 0.001860586 0.002559899 0.002037685 4.752711e-05 6.176642e-05
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.044637502 0.052688628 0.210437697 0.031571776 0.003300048
## [2,] 0.021565432 0.004139937 0.141301964 0.084377534 0.022219473
## [3,] 0.023332674 0.003859415 0.008094425 0.001152473 0.028581537
## [4,] 0.042952886 0.111551138 0.078565918 0.082924107 0.063867168
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.558723e-02 1.699690e-02 3.257055e-01 4.175275e-01 6.065892e-01
## [2,] 1.608342e-07 1.648699e-07 1.771143e-05 1.592577e-05 1.574558e-05
## [3,] 6.040274e-02 1.397326e-01 1.746137e-02 5.197595e-02 1.446654e-01
## [4,] 2.675121e-02 2.739355e-02 2.521596e-02 1.176586e-01 1.778976e-02
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.908895e-03 2.358266e-02 1.466484e-01 1.917400e-03 2.516132e-02
## [2,] 4.331150e-03 1.430690e-01 6.245082e-02 1.456129e-02 2.171270e-01
## [3,] 1.279096e-01 2.708095e-02 3.512942e-02 1.073038e-01 1.223359e-02
## [4,] 1.860586e-03 2.559899e-03 2.037685e-03 4.752711e-05 6.176642e-05
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.4.0 beta (2024-04-15 r86425 ucrt)
## Platform: x86_64-w64-mingw32/x64
## Running under: Windows Server 2022 x64 (build 20348)
##
## Matrix products: default
##
##
## locale:
## [1] LC_COLLATE=C
## [2] LC_CTYPE=English_United States.utf8
## [3] LC_MONETARY=English_United States.utf8
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.utf8
##
## time zone: America/New_York
## tzcode source: internal
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] einsum_0.1.2 DelayedRandomArray_1.11.0
## [3] HDF5Array_1.31.6 rhdf5_2.47.7
## [5] DelayedArray_0.29.9 SparseArray_1.3.5
## [7] S4Arrays_1.3.7 abind_1.4-5
## [9] IRanges_2.37.1 S4Vectors_0.41.6
## [11] MatrixGenerics_1.15.1 matrixStats_1.3.0
## [13] BiocGenerics_0.49.1 Matrix_1.7-0
## [15] DelayedTensor_1.9.0 BiocStyle_2.31.0
##
## loaded via a namespace (and not attached):
## [1] jsonlite_1.8.8 compiler_4.4.0 BiocManager_1.30.22
## [4] crayon_1.5.2 rsvd_1.0.5 Rcpp_1.0.12
## [7] rhdf5filters_1.15.5 parallel_4.4.0 jquerylib_0.1.4
## [10] BiocParallel_1.37.1 yaml_2.3.8 fastmap_1.1.1
## [13] lattice_0.22-6 R6_2.5.1 XVector_0.43.1
## [16] ScaledMatrix_1.11.1 knitr_1.46 bookdown_0.39
## [19] bslib_0.7.0 rlang_1.1.3 cachem_1.0.8
## [22] xfun_0.43 sass_0.4.9 cli_3.6.2
## [25] Rhdf5lib_1.25.3 BiocSingular_1.19.0 zlibbioc_1.49.3
## [28] digest_0.6.35 grid_4.4.0 irlba_2.3.5.1
## [31] rTensor_1.4.8 dqrng_0.3.2 lifecycle_1.0.4
## [34] evaluate_0.23 codetools_0.2-20 beachmat_2.19.4
## [37] rmarkdown_2.26 tools_4.4.0 htmltools_0.5.8.1