DelayedTensor 1.15.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2025-04-04 14:56:50.301149
Compiled: Wed Jun 4 18:40:14 2025
einsum
einsum
is an easy and intuitive way to write tensor operations.
It was originally introduced by
Numpy
1 https://numpy.org/doc/stable/reference/generated/numpy.einsum.html
package of Python but similar tools have been implemented in other languages
(e.g. R, Julia) inspired by Numpy
.
In this vignette, we will use CRAN einsum package first.
einsum
is named after
Einstein summation2 https://en.wikipedia.org/wiki/Einstein_notation
introduced by Albert Einstein,
which is a notational convention that implies summation over
a set of indexed terms in a formula.
Here, we consider a simple example of einsum
; matrix multiplication.
If we naively implement the matrix multiplication,
the calculation would look like the following in a for loop.
A <- matrix(runif(3*4), nrow=3, ncol=4)
B <- matrix(runif(4*5), nrow=4, ncol=5)
C <- matrix(0, nrow=3, ncol=5)
I <- nrow(A)
J <- ncol(A)
K <- ncol(B)
for(i in 1:I){
for(j in 1:J){
for(k in 1:K){
C[i,k] = C[i,k] + A[i,j] * B[j,k]
}
}
}
Therefore, any programming language can implement this. However, when analyzing tensor data, such operations tend to be more complicated and increase the possibility of causing bugs because the order of tensors is larger or more tensors are handled simultaneously. In addition, several programming languages, especially R, are known to significantly slow down the speed of computation if the code is written in for loop.
Obviously, in the case of the R language, it should be executed using the built-in matrix multiplication function (%*%) prepared by the R, as shown below.
C <- A %*% B
However, more complex operations than matrix multiplication are not always provided by programming languages as standard.
einsum
is a function that solves such a problem.
To put it simply, einsum
is a wrapper for the for loop above.
Like the Einstein summation, it omits many notations such as for,
array size (e.g. I, J, and K), brackets (e.g. {}, (), and []),
and even addition operator (+) and
extracts the array subscripts (e.g. i, j, and k)
to concisely express the tensor operation as follows.
suppressPackageStartupMessages(library("einsum"))
C <- einsum('ij,jk->ik', A, B)
DelayedTensor
CRAN einsum is easy to use because the syntax is almost
the same as that of Numpy
‘s einsum
,
except that it prohibits the implicit modes that do not use’->’.
It is extremely fast because the internal calculation
is actually performed by C++.
When the input tensor is huge, however,
it is not scalable because it assumes that the input is R’s standard array.
Using einsum
of DelayedTensor,
we can augment the CRAN einsum
’s functionality;
in DelayedTensor,
the input DelayedArray objects are divided into
multiple block tensors and the CRAN einsum
is incremently applied in the block processing.
A surprisingly large number of tensor operations can be handled
uniformly in einsum
.
In more detail, einsum
is capable of performing any tensor operation
that can be described by a combination of the following
three operations3 https://ajcr.net/Basic-guide-to-einsum/.
Some typical operations are introduced below. Here we use the arrays and DelayedArray objects below.
suppressPackageStartupMessages(library("DelayedTensor"))
suppressPackageStartupMessages(library("DelayedArray"))
arrA <- array(runif(3), dim=c(3))
arrB <- array(runif(3*3), dim=c(3,3))
arrC <- array(runif(3*4), dim=c(3,4))
arrD <- array(runif(3*3*3), dim=c(3,3,3))
arrE <- array(runif(3*4*5), dim=c(3,4,5))
darrA <- DelayedArray(arrA)
darrB <- DelayedArray(arrB)
darrC <- DelayedArray(arrC)
darrD <- DelayedArray(arrD)
darrE <- DelayedArray(arrE)
If the same subscript is written on both sides of ->,
einsum
will simply output the object without any calculation.
einsum::einsum('i->i', arrA)
## [1] 0.04095642 0.45128384 0.63312619
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.04095642 0.45128384 0.63312619
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.9554128 0.7286269 0.7021936 0.1608062
## [2,] 0.2828585 0.6873229 0.8127383 0.3826222
## [3,] 0.2696460 0.2548447 0.3836526 0.4601684
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.9554128 0.7286269 0.7021936 0.1608062
## [2,] 0.2828585 0.6873229 0.8127383 0.3826222
## [3,] 0.2696460 0.2548447 0.3836526 0.4601684
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4813638 0.5818852 0.5989449 0.4690833
## [2,] 0.6136502 0.6532618 0.1296770 0.7042959
## [3,] 0.3903627 0.0959344 0.2625992 0.3712458
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3717754 0.1881973 0.6397636 0.2368421
## [2,] 0.3159474 0.4417850 0.7633024 0.8831328
## [3,] 0.1405385 0.6363774 0.1824125 0.3534610
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.58838326 0.36456375 0.3624520 0.8810574
## [2,] 0.60453765 0.80294055 0.7960263 0.6810592
## [3,] 0.04080256 0.07441733 0.1169035 0.5513709
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.89882833 0.3170865 0.4772809 0.7229235
## [2,] 0.04247036 0.5083117 0.4678335 0.5802972
## [3,] 0.60799393 0.0329451 0.8874181 0.7365163
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3688396 0.4069875 0.7233839 0.810624661
## [2,] 0.9744274 0.1092326 0.4318608 0.155871340
## [3,] 0.9618179 0.5845417 0.2435051 0.002756249
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.4813638 0.5818852 0.5989449 0.4690833
## [2,] 0.6136502 0.6532618 0.1296770 0.7042959
## [3,] 0.3903627 0.0959344 0.2625992 0.3712458
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.3717754 0.1881973 0.6397636 0.2368421
## [2,] 0.3159474 0.4417850 0.7633024 0.8831328
## [3,] 0.1405385 0.6363774 0.1824125 0.3534610
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.58838326 0.36456375 0.36245199 0.88105740
## [2,] 0.60453765 0.80294055 0.79602631 0.68105917
## [3,] 0.04080256 0.07441733 0.11690354 0.55137092
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.89882833 0.31708651 0.47728087 0.72292349
## [2,] 0.04247036 0.50831170 0.46783351 0.58029717
## [3,] 0.60799393 0.03294510 0.88741814 0.73651628
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.368839643 0.406987533 0.723383900 0.810624661
## [2,] 0.974427359 0.109232637 0.431860769 0.155871340
## [3,] 0.961817943 0.584541704 0.243505139 0.002756249
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.77338895 0.07245039 0.96805672
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.77338895 0.07245039 0.96805672
einsum::einsum('iii->i', arrD)
## [1] 0.67532737 0.04521796 0.66611880
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.67532737 0.04521796 0.66611880
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.001677428 0.203657102 0.400848768
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.001677428 0.203657102 0.400848768
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.91281362 0.53089719 0.4930758 0.02585863
## [2,] 0.08000891 0.47241283 0.6605435 0.14639977
## [3,] 0.07270897 0.06494583 0.1471893 0.21175494
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.91281362 0.53089719 0.49307580 0.02585863
## [2,] 0.08000891 0.47241283 0.66054347 0.14639977
## [3,] 0.07270897 0.06494583 0.14718934 0.21175494
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2317111 0.338590361 0.35873501 0.2200392
## [2,] 0.3765665 0.426750944 0.01681613 0.4960327
## [3,] 0.1523830 0.009203409 0.06895833 0.1378234
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13821692 0.03541823 0.40929747 0.05609417
## [2,] 0.09982278 0.19517400 0.58263048 0.77992349
## [3,] 0.01975106 0.40497625 0.03327434 0.12493467
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.346194862 0.132906731 0.13137145 0.7762621
## [2,] 0.365465765 0.644713526 0.63365788 0.4638416
## [3,] 0.001664849 0.005537939 0.01366644 0.3040099
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.807892375 0.10054385 0.2277970 0.5226184
## [2,] 0.001803731 0.25838078 0.2188682 0.3367448
## [3,] 0.369656620 0.00108538 0.7875110 0.5424562
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1360427 0.16563885 0.52328427 6.571123e-01
## [2,] 0.9495087 0.01193177 0.18650372 2.429587e-02
## [3,] 0.9250938 0.34168900 0.05929475 7.596909e-06
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.231711103 0.338590361 0.358735014 0.220039164
## [2,] 0.376566535 0.426750944 0.016816133 0.496032737
## [3,] 0.152383011 0.009203409 0.068958330 0.137823421
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.13821692 0.03541823 0.40929747 0.05609417
## [2,] 0.09982278 0.19517400 0.58263048 0.77992349
## [3,] 0.01975106 0.40497625 0.03327434 0.12493467
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.346194862 0.132906731 0.131371446 0.776262145
## [2,] 0.365465765 0.644713526 0.633657881 0.463841599
## [3,] 0.001664849 0.005537939 0.013666437 0.304009888
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.807892375 0.100543852 0.227797030 0.522618379
## [2,] 0.001803731 0.258380784 0.218868197 0.336744803
## [3,] 0.369656620 0.001085380 0.787510958 0.542456227
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 1.360427e-01 1.656389e-01 5.232843e-01 6.571123e-01
## [2,] 9.495087e-01 1.193177e-02 1.865037e-01 2.429587e-02
## [3,] 9.250938e-01 3.416890e-01 5.929475e-02 7.596909e-06
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.001677428 0.01848297 0.02593058
## [2,] 0.018482970 0.20365710 0.28571961
## [3,] 0.025930582 0.28571961 0.40084877
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.001677428 0.018482970 0.025930582
## [2,] 0.018482970 0.203657102 0.285719615
## [3,] 0.025930582 0.285719615 0.400848768
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4599011 0.3507346 0.3380106 0.07740628
## [2,] 0.1361578 0.3308524 0.3912228 0.18418049
## [3,] 0.1297978 0.1226730 0.1846765 0.22150840
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5862892 0.4471220 0.4309012 0.09867875
## [2,] 0.1735761 0.4217758 0.4987370 0.23479620
## [3,] 0.1654683 0.1563855 0.2354285 0.28238241
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3729575 0.28442875 0.2741102 0.06277274
## [2,] 0.1104174 0.26830522 0.3172627 0.14936144
## [3,] 0.1052597 0.09948187 0.1497637 0.17963256
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5559405 0.4239772 0.4085960 0.09357075
## [2,] 0.1645911 0.3999430 0.4729203 0.22264221
## [3,] 0.1569030 0.1482904 0.2232418 0.26776516
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6241347 0.4759841 0.4587162 0.1050485
## [2,] 0.1847806 0.4490018 0.5309308 0.2499525
## [3,] 0.1761494 0.1664803 0.2506256 0.3006104
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09165695 0.06990039 0.06736452 0.01542685
## [2,] 0.02713586 0.06593791 0.07796956 0.03670663
## [3,] 0.02586833 0.02444838 0.03680549 0.04414598
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5722396 0.4364074 0.4205753 0.09631406
## [2,] 0.1694166 0.4116686 0.4867854 0.22916964
## [3,] 0.1615031 0.1526380 0.2297868 0.27561552
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.12389510 0.09448618 0.09105838 0.02085287
## [2,] 0.03668025 0.08913000 0.10539349 0.04961732
## [3,] 0.03496689 0.03304751 0.04975094 0.05967327
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.25089062 0.19133683 0.1843955 0.04222758
## [2,] 0.07427840 0.18049044 0.2134244 0.10047629
## [3,] 0.07080882 0.06692202 0.1007469 0.12083984
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4481682 0.3417867 0.3293873 0.07543151
## [2,] 0.1326842 0.3224117 0.3812420 0.17948171
## [3,] 0.1264864 0.1195434 0.1799651 0.21585732
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6728933 0.5131690 0.4945521 0.1132552
## [2,] 0.1992161 0.4840787 0.5724082 0.2694793
## [3,] 0.1899106 0.1794861 0.2702050 0.3240947
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3546930 0.27049966 0.2606864 0.05969862
## [2,] 0.1050100 0.25516574 0.3017256 0.14204689
## [3,] 0.1001049 0.09461003 0.1424294 0.17083557
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3551989 0.27088554 0.2610583 0.05978378
## [2,] 0.1051598 0.25552973 0.3021561 0.14224952
## [3,] 0.1002477 0.09474499 0.1426326 0.17107927
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.30186022 0.23020781 0.2218563 0.05080631
## [2,] 0.08936841 0.21715792 0.2567826 0.12088851
## [3,] 0.08519396 0.08051754 0.1212141 0.14538902
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.13427224 0.10240010 0.09868520 0.02259945
## [2,] 0.03975249 0.09659530 0.11422098 0.05377314
## [3,] 0.03789563 0.03581548 0.05391795 0.06467135
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.17980611 0.13712562 0.13215094 0.03026329
## [2,] 0.05323320 0.12935233 0.15295515 0.07200847
## [3,] 0.05074665 0.04796109 0.07220239 0.08660245
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4220871 0.3218965 0.3102186 0.07104177
## [2,] 0.1249626 0.3036490 0.3590556 0.16903677
## [3,] 0.1191256 0.1125866 0.1694920 0.20329550
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6080032 0.4636817 0.4468601 0.1023334
## [2,] 0.1800047 0.4373968 0.5172083 0.2434922
## [3,] 0.1715966 0.1621774 0.2441479 0.2928408
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6112383 0.4661490 0.4492379 0.1028780
## [2,] 0.1809626 0.4397242 0.5199604 0.2447878
## [3,] 0.1725097 0.1630404 0.2454470 0.2943990
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7292688 0.5561626 0.535986 0.1227438
## [2,] 0.2159065 0.5246352 0.620365 0.2920565
## [3,] 0.2058214 0.1945236 0.292843 0.3512476
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.17427928 0.13291069 0.12808892 0.02933307
## [2,] 0.05159693 0.12537633 0.14825366 0.06979510
## [3,] 0.04918681 0.04648688 0.06998305 0.08394049
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.22628195 0.17256951 0.16630898 0.03808567
## [2,] 0.06699278 0.16278699 0.19249061 0.09062104
## [3,] 0.06386352 0.06035795 0.09086508 0.10898723
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8437564 0.6434743 0.6201301 0.1420132
## [2,] 0.2498016 0.6069974 0.7177558 0.3379062
## [3,] 0.2381332 0.2250617 0.3388162 0.4063898
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.33770115 0.25754119 0.2481980 0.05683872
## [2,] 0.09997943 0.24294185 0.2872713 0.13524203
## [3,] 0.09530934 0.09007767 0.1356062 0.16265157
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5621489 0.4287119 0.4131589 0.09461568
## [2,] 0.1664292 0.4044093 0.4782016 0.22512852
## [3,] 0.1586552 0.1499464 0.2257348 0.27075538
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5775830 0.4404824 0.4245024 0.0972134
## [2,] 0.1709986 0.4155126 0.4913309 0.2313095
## [3,] 0.1630112 0.1540632 0.2319325 0.2781891
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03898329 0.02972984 0.02865129 0.006561304
## [2,] 0.01154135 0.02804453 0.03316180 0.015611966
## [3,] 0.01100225 0.01039832 0.01565401 0.018776048
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.34830888 0.26563097 0.2559943 0.05862411
## [2,] 0.10311994 0.25057303 0.2962949 0.13949020
## [3,] 0.09830316 0.09290715 0.1398658 0.16776071
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7671397 0.5850441 0.5638197 0.1291178
## [2,] 0.2271185 0.5518795 0.6525805 0.3072229
## [3,] 0.2165097 0.2046252 0.3080503 0.3694879
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07109927 0.05422247 0.05225537 0.01196677
## [2,] 0.02104957 0.05114874 0.06048181 0.02847373
## [3,] 0.02006634 0.01896486 0.02855041 0.03424450
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.34629127 0.26409228 0.2545115 0.05828453
## [2,] 0.10252261 0.24912157 0.2945786 0.13868219
## [3,] 0.09773373 0.09236898 0.1390557 0.16678895
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7605337 0.5800062 0.5589645 0.1280060
## [2,] 0.2251628 0.5471271 0.6469610 0.3045774
## [3,] 0.2146453 0.2028631 0.3053976 0.3663061
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11169114 0.08517906 0.08208891 0.01879881
## [2,] 0.03306716 0.08035048 0.09501198 0.04472989
## [3,] 0.03152257 0.02979225 0.04485035 0.05379531
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8417735 0.6419621 0.6186728 0.1416795
## [2,] 0.2492145 0.6055710 0.7160691 0.3371122
## [3,] 0.2375736 0.2245328 0.3380200 0.4054348
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6506927 0.4962381 0.4782354 0.1095185
## [2,] 0.1926434 0.4681076 0.5535228 0.2605884
## [3,] 0.1836449 0.1735643 0.2612901 0.3134019
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5267868 0.4017437 0.3871691 0.08866386
## [2,] 0.1559599 0.3789699 0.4481202 0.21096677
## [3,] 0.1486750 0.1405140 0.2115349 0.25372347
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8587521 0.6549105 0.6311515 0.1445372
## [2,] 0.2542412 0.6177853 0.7305122 0.3439117
## [3,] 0.2423655 0.2290617 0.3448379 0.4136124
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04057672 0.03094505 0.02982241 0.006829497
## [2,] 0.01201310 0.02919085 0.03451728 0.016250103
## [3,] 0.01145196 0.01082335 0.01629386 0.019543516
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5808852 0.4430007 0.4269294 0.09776919
## [2,] 0.1719762 0.4178882 0.4941399 0.23263200
## [3,] 0.1639431 0.1549440 0.2332585 0.27977959
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.30294851 0.23103776 0.2226561 0.05098948
## [2,] 0.08969060 0.21794083 0.2577083 0.12132435
## [3,] 0.08550111 0.08080782 0.1216511 0.14591319
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4856475 0.3703696 0.3569332 0.08173967
## [2,] 0.1437803 0.3493743 0.4131244 0.19449136
## [3,] 0.1370642 0.1295406 0.1950151 0.23390897
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.031476171 0.024004687 0.02313384 0.005297776
## [2,] 0.009318800 0.022643923 0.02677574 0.012605528
## [3,] 0.008883515 0.008395885 0.01263947 0.015160294
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4560003 0.3477597 0.3351436 0.07674972
## [2,] 0.1350029 0.3280461 0.3879044 0.18261827
## [3,] 0.1286969 0.1216325 0.1831101 0.21962957
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4469741 0.3408761 0.3285097 0.07523053
## [2,] 0.1323307 0.3215527 0.3802262 0.17900350
## [3,] 0.1261494 0.1192249 0.1794856 0.21528219
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.8478507 0.6465967 0.6231393 0.1427023
## [2,] 0.2510137 0.6099429 0.7212387 0.3395459
## [3,] 0.2392888 0.2261538 0.3404603 0.4083618
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6906904 0.5267415 0.5076322 0.1162506
## [2,] 0.2044850 0.4968819 0.5875476 0.2766066
## [3,] 0.1949334 0.1842332 0.2773515 0.3326665
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5544233 0.4228201 0.4074809 0.09331538
## [2,] 0.1641420 0.3988516 0.4716297 0.22203460
## [3,] 0.1564748 0.1478857 0.2226325 0.26703441
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7036771 0.5366456 0.5171770 0.1184364
## [2,] 0.2083299 0.5062245 0.5985950 0.2818075
## [3,] 0.1985987 0.1876973 0.2825664 0.3389215
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.35239412 0.26874649 0.2589968 0.0593117
## [2,] 0.10432942 0.25351195 0.2997701 0.1411262
## [3,] 0.09945614 0.09399684 0.1415063 0.1697283
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9309804 0.7099940 0.6842366 0.1566940
## [2,] 0.2756250 0.6697463 0.7919544 0.3728376
## [3,] 0.2627504 0.2483277 0.3738416 0.4484007
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.9189332 0.7008064 0.6753824 0.1546663
## [2,] 0.2720583 0.6610795 0.7817062 0.3680129
## [3,] 0.2593504 0.2451142 0.3690040 0.4425982
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3888411 0.2965421 0.2857840 0.06544612
## [2,] 0.1151199 0.2797319 0.3307743 0.15572248
## [3,] 0.1097426 0.1037186 0.1561418 0.18728280
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10436226 0.07958984 0.07670245 0.01756529
## [2,] 0.03089738 0.07507810 0.08877754 0.04179484
## [3,] 0.02945414 0.02783736 0.04190739 0.05026541
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5584786 0.4259128 0.4104614 0.09399793
## [2,] 0.1653426 0.4017689 0.4750794 0.22365865
## [3,] 0.1576193 0.1489674 0.2242610 0.26898761
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.6911302 0.5270770 0.5079555 0.1163246
## [2,] 0.2046153 0.4971984 0.5879218 0.2767828
## [3,] 0.1950576 0.1843506 0.2775281 0.3328784
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4126053 0.3146654 0.3032499 0.06944589
## [2,] 0.1221555 0.2968278 0.3509898 0.16523953
## [3,] 0.1164495 0.1100574 0.1656845 0.19872867
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.23264793 0.1774244 0.17098774 0.03915714
## [2,] 0.06887749 0.1673667 0.19790594 0.09317048
## [3,] 0.06566019 0.0620560 0.09342139 0.11205337
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7744812 0.5906430 0.5692154 0.1303535
## [2,] 0.2292920 0.5571609 0.6588257 0.3101630
## [3,] 0.2185817 0.2065834 0.3109983 0.3730238
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.14892147 0.11357205 0.10945185 0.02506508
## [2,] 0.04408953 0.10713395 0.12668260 0.05963984
## [3,] 0.04203008 0.03972299 0.05980045 0.07172706
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0026333557 0.0020082773 0.001935420 0.000443222
## [2,] 0.0007796284 0.0018944333 0.002240109 0.001054602
## [3,] 0.0007432116 0.0007024156 0.001057442 0.001268339
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.45990113 0.35073462 0.33801056 0.07740628
## [2,] 0.13615782 0.33085238 0.39122277 0.18418049
## [3,] 0.12979782 0.12267302 0.18467649 0.22150840
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.58628923 0.44712204 0.43090120 0.09867875
## [2,] 0.17357615 0.42177584 0.49873697 0.23479620
## [3,] 0.16546832 0.15638551 0.23542851 0.28238241
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.37295749 0.28442875 0.27411015 0.06277274
## [2,] 0.11041738 0.26830522 0.31726267 0.14936144
## [3,] 0.10525973 0.09948187 0.14976367 0.17963256
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.7744812 0.5906430 0.5692154 0.1303535
## [2,] 0.2292920 0.5571609 0.6588257 0.3101630
## [3,] 0.2185817 0.2065834 0.3109983 0.3730238
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.14892147 0.11357205 0.10945185 0.02506508
## [2,] 0.04408953 0.10713395 0.12668260 0.05963984
## [3,] 0.04203008 0.03972299 0.05980045 0.07172706
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.0026333557 0.0020082773 0.0019354204 0.0004432220
## [2,] 0.0007796284 0.0018944333 0.0022401091 0.0010546022
## [3,] 0.0007432116 0.0007024156 0.0010574423 0.0012683387
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.125366
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.125366
einsum::einsum('ij->', arrC)
## [1] 6.080893
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 6.080893
einsum::einsum('ijk->', arrE)
## [1] 28.42411
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 28.42411
einsum::einsum('ij->i', arrC)
## [1] 2.547039 2.165542 1.368312
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 2.547039 2.165542 1.368312
einsum::einsum('ij->j', arrC)
## [1] 1.507917 1.670795 1.898584 1.003597
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 1.507917 1.670795 1.898584 1.003597
einsum::einsum('ijk->i', arrE)
## [1] 10.49027 10.65992 7.27392
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 10.49027 10.65992 7.27392
einsum::einsum('ijk->j', arrE)
## [1] 7.401739 5.798468 7.083364 8.140538
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 7.401739 5.798468 7.083364 8.140538
einsum::einsum('ijk->k', arrE)
## [1] 5.352304 5.153535 5.864514 6.279905 5.773849
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 5.352304 5.153535 5.864514 6.279905 5.773849
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 2.709190 1.858720 2.801825 3.120531
## [2,] 2.551033 2.515532 2.588700 3.004656
## [3,] 2.141516 1.424216 1.692839 2.015350
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 2.709190 1.858720 2.801825 3.120531
## [2,] 2.551033 2.515532 2.588700 3.004656
## [3,] 2.141516 1.424216 1.692839 2.015350
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.4853766 0.8282612 1.233723 1.5492926 2.3050849
## [2,] 1.3310814 1.2663598 1.241922 0.8583433 1.1007619
## [3,] 0.9912211 1.5854785 1.275382 1.8325325 1.3987498
## [4,] 1.5446250 1.4734358 2.113487 2.0397369 0.9692522
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.4853766 0.8282612 1.2337235 1.5492926 2.3050849
## [2,] 1.3310814 1.2663598 1.2419216 0.8583433 1.1007619
## [3,] 0.9912211 1.5854785 1.2753818 1.8325325 1.3987498
## [4,] 1.5446250 1.4734358 2.1134875 2.0397369 0.9692522
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.4853766 0.8282612 1.233723 1.5492926 2.3050849
## [2,] 1.3310814 1.2663598 1.241922 0.8583433 1.1007619
## [3,] 0.9912211 1.5854785 1.275382 1.8325325 1.3987498
## [4,] 1.5446250 1.4734358 2.113487 2.0397369 0.9692522
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.4853766 0.8282612 1.2337235 1.5492926 2.3050849
## [2,] 1.3310814 1.2663598 1.2419216 0.8583433 1.1007619
## [3,] 0.9912211 1.5854785 1.2753818 1.8325325 1.3987498
## [4,] 1.5446250 1.4734358 2.1134875 2.0397369 0.9692522
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.813896
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 1.813896
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.7733889 0.34128291 0.6526370
## [2,] 0.3560000 0.07245039 0.3273156
## [3,] 0.6754582 0.08323022 0.9680567
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.77338895 0.34128291 0.65263699
## [2,] 0.35599999 0.07245039 0.32731562
## [3,] 0.67545815 0.08323022 0.96805672
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.6753274 0.2694926 0.7016426
## [2,] 0.7634756 0.5221606 0.2275195
## [3,] 0.4401177 0.2379135 0.4863149
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.6896294 0.61065377 0.9296792
## [2,] 0.7218854 0.04521796 0.9574130
## [3,] 0.6350458 0.42277285 0.9306509
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.2323972 0.9969306 0.8352462
## [2,] 0.3298069 0.9061734 0.4365546
## [3,] 0.8495909 0.5265210 0.6661188
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.6753274 0.2694926 0.7016426
## [2,] 0.7634756 0.5221606 0.2275195
## [3,] 0.4401177 0.2379135 0.4863149
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.68962943 0.61065377 0.92967924
## [2,] 0.72188545 0.04521796 0.95741301
## [3,] 0.63504578 0.42277285 0.93065089
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.2323972 0.9969306 0.8352462
## [2,] 0.3298069 0.9061734 0.4365546
## [3,] 0.8495909 0.5265210 0.6661188
Some examples of combining Multiplication and Summation are shown below.
Inner Product first calculate Hadamard Product and collapses it to 0D tensor (norm).
einsum::einsum('i,i->', arrA, arrA)
## [1] 0.6061833
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 0.6061833
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 3.818609
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 3.818609
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 17.68818
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 17.68818
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.7606606 0.2577908 0.7133255 1.179353 2.0106451
## [2,] 0.7745447 0.6355685 0.7831582 0.360010 0.5192596
## [3,] 0.4445095 1.0252023 0.7786958 1.234176 0.7690827
## [4,] 0.8538953 0.9609523 1.5441136 1.401819 0.6814158
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.7606606 0.2577908 0.7133255 1.1793527 2.0106451
## [2,] 0.7745447 0.6355685 0.7831582 0.3600100 0.5192596
## [3,] 0.4445095 1.0252023 0.7786958 1.2341762 0.7690827
## [4,] 0.8538953 0.9609523 1.5441136 1.4018194 0.6814158
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.9626452 1.4032762 0.7867063
## [2,] 1.4032762 1.3593650 0.7393121
## [3,] 0.7867063 0.7393121 0.4965991
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.9626452 1.4032762 0.7867063
## [2,] 1.4032762 1.3593650 0.7393121
## [3,] 0.7867063 0.7393121 0.4965991
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.91281362 0.08000891 0.07270897
## [2,] 0.53089719 0.47241283 0.06494583
## [3,] 0.49307580 0.66054347 0.14718934
## [4,] 0.02585863 0.14639977 0.21175494
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.91281362 0.08000891 0.07270897
## [2,] 0.53089719 0.47241283 0.06494583
## [3,] 0.49307580 0.66054347 0.14718934
## [4,] 0.02585863 0.14639977 0.21175494
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.2317111 0.13821692 0.3461949 0.8078924 0.1360427
## [2,] 0.3385904 0.03541823 0.1329067 0.1005439 0.1656389
## [3,] 0.3587350 0.40929747 0.1313714 0.2277970 0.5232843
## [4,] 0.2200392 0.05609417 0.7762621 0.5226184 0.6571123
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.37656653 0.09982278 0.3654658 0.001803731 0.94950868
## [2,] 0.42675094 0.19517400 0.6447135 0.258380784 0.01193177
## [3,] 0.01681613 0.58263048 0.6336579 0.218868197 0.18650372
## [4,] 0.49603274 0.77992349 0.4638416 0.336744803 0.02429587
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.152383011 0.01975106 0.001664849 0.36965662 9.250938e-01
## [2,] 0.009203409 0.40497625 0.005537939 0.00108538 3.416890e-01
## [3,] 0.068958330 0.03327434 0.013666437 0.78751096 5.929475e-02
## [4,] 0.137823421 0.12493467 0.304009888 0.54245623 7.596909e-06
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.23171110 0.13821692 0.34619486 0.80789238 0.13604268
## [2,] 0.33859036 0.03541823 0.13290673 0.10054385 0.16563885
## [3,] 0.35873501 0.40929747 0.13137145 0.22779703 0.52328427
## [4,] 0.22003916 0.05609417 0.77626214 0.52261838 0.65711234
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.376566535 0.099822782 0.365465765 0.001803731 0.949508678
## [2,] 0.426750944 0.195174001 0.644713526 0.258380784 0.011931769
## [3,] 0.016816133 0.582630482 0.633657881 0.218868197 0.186503724
## [4,] 0.496032737 0.779923486 0.463841599 0.336744803 0.024295875
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.523830e-01 1.975106e-02 1.664849e-03 3.696566e-01 9.250938e-01
## [2,] 9.203409e-03 4.049762e-01 5.537939e-03 1.085380e-03 3.416890e-01
## [3,] 6.895833e-02 3.327434e-02 1.366644e-02 7.875110e-01 5.929475e-02
## [4,] 1.378234e-01 1.249347e-01 3.040099e-01 5.424562e-01 7.596909e-06
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 2.131277 2.100885 1.1201420
## [2,] 1.436578 2.404168 1.3127894
## [3,] 2.196456 2.884564 0.7834943
## [4,] 2.416119 1.598913 2.2648735
## [5,] 2.309836 1.671392 1.7926210
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 2.1312772 2.1008849 1.1201420
## [2,] 1.4365783 2.4041676 1.3127894
## [3,] 2.1964564 2.8845637 0.7834943
## [4,] 2.4161192 1.5989127 2.2648735
## [5,] 2.3098357 1.6713921 1.7926210
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.0086626535 5.167319e-03 0.0129426950 0.0302035233 0.0050860343
## [2,] 0.0073621897 7.701214e-04 0.0028898772 0.0021861901 0.0036015929
## [3,] 0.0072445170 8.265607e-03 0.0026529963 0.0046002743 0.0105675266
## [4,] 0.0002330384 5.940805e-05 0.0008221215 0.0005534932 0.0006959326
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.013596586 0.003604274 0.01319577 6.512683e-05 0.034283652
## [2,] 0.090980004 0.041609589 0.13744794 5.508479e-02 0.002543761
## [3,] 0.005012765 0.173677832 0.18888872 6.524299e-02 0.055595379
## [4,] 0.032771817 0.051527869 0.03064502 2.224801e-02 0.001605176
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0070147921 0.0009092192 7.663957e-05 1.701675e-02 4.258572e-02
## [2,] 0.0003784341 0.0166521812 2.277140e-04 4.462963e-05 1.404988e-02
## [3,] 0.0064261873 0.0031008165 1.273567e-03 7.338770e-02 5.525644e-03
## [4,] 0.0184776553 0.0167496911 4.075788e-02 7.272580e-02 1.018499e-06
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,] 8.662653e-03 5.167319e-03 1.294269e-02 3.020352e-02 5.086034e-03
## [2,] 7.362190e-03 7.701214e-04 2.889877e-03 2.186190e-03 3.601593e-03
## [3,] 7.244517e-03 8.265607e-03 2.652996e-03 4.600274e-03 1.056753e-02
## [4,] 2.330384e-04 5.940805e-05 8.221215e-04 5.534932e-04 6.959326e-04
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.359659e-02 3.604274e-03 1.319577e-02 6.512683e-05 3.428365e-02
## [2,] 9.098000e-02 4.160959e-02 1.374479e-01 5.508479e-02 2.543761e-03
## [3,] 5.012765e-03 1.736778e-01 1.888887e-01 6.524299e-02 5.559538e-02
## [4,] 3.277182e-02 5.152787e-02 3.064502e-02 2.224801e-02 1.605176e-03
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 7.014792e-03 9.092192e-04 7.663957e-05 1.701675e-02 4.258572e-02
## [2,] 3.784341e-04 1.665218e-02 2.277140e-04 4.462963e-05 1.404988e-02
## [3,] 6.426187e-03 3.100816e-03 1.273567e-03 7.338770e-02 5.525644e-03
## [4,] 1.847766e-02 1.674969e-02 4.075788e-02 7.272580e-02 1.018499e-06
einsum
By using einsum
and other DelayedTensor functions,
it is possible to implement your original tensor calculation functions.
It is intended to be applied to Delayed Arrays,
which can scale to large-scale data
since the calculation is performed internally by block processing.
For example, kronecker
can be easily implmented by eimsum
and other DelayedTensor functions4 https://stackoverflow.com/
questions/56067643/speeding-up-kronecker-products-numpy
(the kronecker
function inside DelayedTensor
has a more efficient implementation though).
darr1 <- DelayedArray(array(1:6, dim=c(2,3)))
darr2 <- DelayedArray(array(20:1, dim=c(4,5)))
mykronecker <- function(darr1, darr2){
stopifnot((length(dim(darr1)) == 2) && (length(dim(darr2)) == 2))
# Outer Product
tmpdarr <- DelayedTensor::einsum('ij,kl->ikjl', darr1, darr2)
# Reshape
DelayedTensor::unfold(tmpdarr, row_idx=c(2,1), col_idx=c(4,3))
}
identical(as.array(DelayedTensor::kronecker(darr1, darr2)),
as.array(mykronecker(darr1, darr2)))
## [1] TRUE
## R version 4.5.0 (2025-04-11 ucrt)
## Platform: x86_64-w64-mingw32/x64
## Running under: Windows Server 2022 x64 (build 20348)
##
## Matrix products: default
## LAPACK version 3.12.1
##
## locale:
## [1] LC_COLLATE=C
## [2] LC_CTYPE=English_United States.utf8
## [3] LC_MONETARY=English_United States.utf8
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.utf8
##
## time zone: America/New_York
## tzcode source: internal
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] einsum_0.1.2 DelayedRandomArray_1.17.0
## [3] HDF5Array_1.37.0 h5mread_1.1.1
## [5] rhdf5_2.53.1 DelayedArray_0.35.1
## [7] SparseArray_1.9.0 S4Arrays_1.9.1
## [9] abind_1.4-8 IRanges_2.43.0
## [11] S4Vectors_0.47.0 MatrixGenerics_1.21.0
## [13] matrixStats_1.5.0 BiocGenerics_0.55.0
## [15] generics_0.1.4 Matrix_1.7-3
## [17] DelayedTensor_1.15.0 BiocStyle_2.37.0
##
## loaded via a namespace (and not attached):
## [1] dqrng_0.4.1 sass_0.4.10 lattice_0.22-7
## [4] digest_0.6.37 evaluate_1.0.3 grid_4.5.0
## [7] bookdown_0.43 fastmap_1.2.0 jsonlite_2.0.0
## [10] BiocManager_1.30.25 codetools_0.2-20 jquerylib_0.1.4
## [13] cli_3.6.5 rlang_1.1.6 crayon_1.5.3
## [16] XVector_0.49.0 cachem_1.1.0 yaml_2.3.10
## [19] tools_4.5.0 beachmat_2.25.1 parallel_4.5.0
## [22] BiocParallel_1.43.3 Rhdf5lib_1.31.0 rsvd_1.0.5
## [25] R6_2.6.1 lifecycle_1.0.4 BiocSingular_1.25.0
## [28] irlba_2.3.5.1 ScaledMatrix_1.17.0 rTensor_1.4.8
## [31] bslib_0.9.0 Rcpp_1.0.14 xfun_0.52
## [34] knitr_1.50 rhdf5filters_1.21.0 htmltools_0.5.8.1
## [37] rmarkdown_2.29 compiler_4.5.0