DelayedTensor 1.10.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2024-04-30 20:49:30
Compiled: Tue Apr 30 23:16:08 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.9669255 0.7022797 0.5593398
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.9669255 0.7022797 0.5593398
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.3503419 0.6702590 0.12081671 0.5043118
## [2,] 0.9647585 0.4730386 0.04729964 0.7077130
## [3,] 0.6323729 0.4022190 0.07752074 0.4073430
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.35034190 0.67025900 0.12081671 0.50431185
## [2,] 0.96475854 0.47303861 0.04729964 0.70771300
## [3,] 0.63237289 0.40221897 0.07752074 0.40734297
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4751091 0.4311949 0.87405512 0.7724306
## [2,] 0.7301156 0.3317740 0.02598719 0.1583268
## [3,] 0.5580925 0.2791218 0.87699871 0.5044421
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.001003983 0.8255707 0.9937457 0.1368056
## [2,] 0.687456288 0.9437586 0.9941294 0.5857445
## [3,] 0.474922070 0.0691706 0.4451007 0.7484058
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7017913 0.78204326 0.3208804 0.05258089
## [2,] 0.3658457 0.05303208 0.7022283 0.28156515
## [3,] 0.8237808 0.92994887 0.7326449 0.44546172
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06227422 0.8163680 0.5779551 0.1725249
## [2,] 0.07443035 0.0195554 0.7461451 0.7193353
## [3,] 0.63516707 0.2051511 0.1924066 0.4225584
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4100798 0.5122750 0.8707274 0.40900465
## [2,] 0.2231493 0.5778603 0.6197178 0.22540096
## [3,] 0.9148678 0.7537142 0.4337347 0.05840305
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.47510912 0.43119495 0.87405512 0.77243058
## [2,] 0.73011556 0.33177395 0.02598719 0.15832680
## [3,] 0.55809248 0.27912184 0.87699871 0.50444211
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.001003983 0.825570698 0.993745675 0.136805604
## [2,] 0.687456288 0.943758602 0.994129430 0.585744486
## [3,] 0.474922070 0.069170600 0.445100665 0.748405826
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.70179127 0.78204326 0.32088039 0.05258089
## [2,] 0.36584575 0.05303208 0.70222827 0.28156515
## [3,] 0.82378084 0.92994887 0.73264491 0.44546172
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.06227422 0.81636799 0.57795507 0.17252488
## [2,] 0.07443035 0.01955540 0.74614510 0.71933532
## [3,] 0.63516707 0.20515110 0.19240659 0.42255844
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.41007984 0.51227499 0.87072744 0.40900465
## [2,] 0.22314930 0.57786026 0.61971780 0.22540096
## [3,] 0.91486781 0.75371424 0.43373467 0.05840305
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.9563954 0.8994595 0.6619709
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.9563954 0.8994595 0.6619709
einsum::einsum('iii->i', arrD)
## [1] 0.1221588 0.2367871 0.9260831
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.1221588 0.2367871 0.9260831
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.9349449 0.4931967 0.3128610
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.9349449 0.4931967 0.3128610
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.1227394 0.4492471 0.014596676 0.2543304
## [2,] 0.9307590 0.2237655 0.002237256 0.5008577
## [3,] 0.3998955 0.1617801 0.006009466 0.1659283
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.122739449 0.449247134 0.014596676 0.254330440
## [2,] 0.930759039 0.223765529 0.002237256 0.500857697
## [3,] 0.399895469 0.161780097 0.006009466 0.165928294
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2257287 0.1859291 0.7639723596 0.59664900
## [2,] 0.5330687 0.1100740 0.0006753341 0.02506737
## [3,] 0.3114672 0.0779090 0.7691267393 0.25446184
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 1.007983e-06 0.681566978 0.9875305 0.01871577
## [2,] 4.725961e-01 0.890680299 0.9882933 0.34309660
## [3,] 2.255510e-01 0.004784572 0.1981146 0.56011128
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4925110 0.611591665 0.1029642 0.00276475
## [2,] 0.1338431 0.002812402 0.4931245 0.07927893
## [3,] 0.6786149 0.864804906 0.5367686 0.19843615
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.003878078 0.6664567010 0.3340321 0.02976484
## [2,] 0.005539877 0.0003824138 0.5567325 0.51744330
## [3,] 0.403437205 0.0420869732 0.0370203 0.17855564
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.16816548 0.2624257 0.7581663 0.167284806
## [2,] 0.04979561 0.3339225 0.3840501 0.050805592
## [3,] 0.83698311 0.5680852 0.1881258 0.003410917
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.2257286743 0.1859290832 0.7639723596 0.5966490011
## [2,] 0.5330687260 0.1100739556 0.0006753341 0.0250673747
## [3,] 0.3114672142 0.0779089993 0.7691267393 0.2544618411
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 1.007983e-06 6.815670e-01 9.875305e-01 1.871577e-02
## [2,] 4.725961e-01 8.906803e-01 9.882933e-01 3.430966e-01
## [3,] 2.255510e-01 4.784572e-03 1.981146e-01 5.601113e-01
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.492510987 0.611591665 0.102964224 0.002764750
## [2,] 0.133843111 0.002812402 0.493124546 0.079278935
## [3,] 0.678614866 0.864804906 0.536768558 0.198436148
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.0038780784 0.6664567010 0.3340320580 0.0297648355
## [2,] 0.0055398769 0.0003824138 0.5567325174 0.5174432961
## [3,] 0.4034372053 0.0420869732 0.0370202965 0.1785556363
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.168165476 0.262425668 0.758166281 0.167284806
## [2,] 0.049795608 0.333922482 0.384050147 0.050805592
## [3,] 0.836983113 0.568085162 0.188125762 0.003410917
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.9349449 0.6790521 0.5408399
## [2,] 0.6790521 0.4931967 0.3928130
## [3,] 0.5408399 0.3928130 0.3128610
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.9349449 0.6790521 0.5408399
## [2,] 0.6790521 0.4931967 0.3928130
## [3,] 0.5408399 0.3928130 0.3128610
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1664506 0.3184462 0.05740112 0.2396032
## [2,] 0.4583656 0.2247450 0.02247249 0.3362409
## [3,] 0.3004461 0.1910979 0.03683081 0.1935324
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2557901 0.4893665 0.08821016 0.3682059
## [2,] 0.7043852 0.3453728 0.03453421 0.5167123
## [3,] 0.4617053 0.2936663 0.05659910 0.2974074
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1955232 0.3740665 0.06742689 0.2814526
## [2,] 0.5384245 0.2639993 0.02639758 0.3949693
## [3,] 0.3529226 0.2244754 0.04326374 0.2273350
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1510657 0.2890123 0.05209555 0.2174567
## [2,] 0.4159990 0.2039719 0.02039537 0.3051623
## [3,] 0.2726760 0.1734348 0.03342655 0.1756442
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1162343 0.2223745 0.04008384 0.1673175
## [2,] 0.3200818 0.1569419 0.01569279 0.2348007
## [3,] 0.2098049 0.1334458 0.02571936 0.1351458
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09778807 0.1870839 0.03372258 0.1407644
## [2,] 0.26928517 0.1320354 0.01320236 0.1975382
## [3,] 0.17650908 0.1122681 0.02163773 0.1136983
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3062181 0.5858433 0.1056005 0.4407964
## [2,] 0.8432521 0.4134618 0.0413425 0.6185802
## [3,] 0.5527288 0.3515615 0.0677574 0.3560402
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.009104402 0.01741815 0.003139687 0.01310565
## [2,] 0.025071364 0.01229294 0.001229185 0.01839147
## [3,] 0.016433595 0.01045254 0.002014546 0.01058570
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3072494 0.5878163 0.10595609 0.4422808
## [2,] 0.8460920 0.4148543 0.04148173 0.6206634
## [3,] 0.5545902 0.3527455 0.06798559 0.3572393
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2706148 0.5177286 0.09332252 0.3895459
## [2,] 0.7452090 0.3653895 0.03653569 0.5466592
## [3,] 0.4884642 0.3106862 0.05987939 0.3146442
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05546851 0.10611996 0.019128522 0.07984608
## [2,] 0.15274713 0.07489469 0.007488801 0.11204993
## [3,] 0.10012157 0.06368204 0.012273611 0.06449331
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1767272 0.3381069 0.06094503 0.2543961
## [2,] 0.4866648 0.2386206 0.02385993 0.3570002
## [3,] 0.3189955 0.2028962 0.03910473 0.2054809
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0003517375 0.0006729289 1.212980e-04 0.0005063207
## [2,] 0.0009686016 0.0004749229 4.748806e-05 0.0007105321
## [3,] 0.0006348919 0.0004038212 7.782954e-05 0.0004089656
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2408447 0.4607738 0.08305620 0.3466924
## [2,] 0.6632293 0.3251934 0.03251644 0.4865218
## [3,] 0.4347287 0.2765080 0.05329212 0.2800305
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1663851 0.3183208 0.05737852 0.2395088
## [2,] 0.4581851 0.2246565 0.02246365 0.3361085
## [3,] 0.3003278 0.1910227 0.03681631 0.1934562
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2892320 0.5533462 0.09974273 0.4163451
## [2,] 0.7964764 0.3905268 0.03904920 0.5842671
## [3,] 0.5220685 0.3320602 0.06399885 0.3362904
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3306382 0.6325627 0.11402180 0.4759486
## [2,] 0.9104992 0.4464343 0.04463945 0.6679102
## [3,] 0.5968074 0.3795976 0.07316087 0.3844334
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02423336 0.04636222 0.008356964 0.03488355
## [2,] 0.06673293 0.03272036 0.003271745 0.04895293
## [3,] 0.04374161 0.02782173 0.005362156 0.02817616
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3481508 0.6660670 0.12006108 0.5011577
## [2,] 0.9587246 0.4700801 0.04700382 0.7032867
## [3,] 0.6284178 0.3997034 0.07703590 0.4047953
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3482852 0.6663242 0.12010744 0.5013512
## [2,] 0.9590949 0.4702616 0.04702197 0.7035583
## [3,] 0.6286605 0.3998577 0.07706565 0.4049516
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1559374 0.2983327 0.05377560 0.2244695
## [2,] 0.4294147 0.2105498 0.02105310 0.3150035
## [3,] 0.2814696 0.1790279 0.03450453 0.1813086
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04792874 0.09169519 0.016528402 0.06899269
## [2,] 0.13198437 0.06471433 0.006470856 0.09681910
## [3,] 0.08651215 0.05502581 0.010605272 0.05572680
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2052108 0.3926005 0.07076772 0.2953979
## [2,] 0.5651020 0.2770798 0.02770551 0.4145390
## [3,] 0.3704089 0.2355975 0.04540735 0.2385989
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2621979 0.5016257 0.09041993 0.3774299
## [2,] 0.7220309 0.3540249 0.03539933 0.5296565
## [3,] 0.4732716 0.3010230 0.05801698 0.3048579
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2458669 0.4703819 0.08478811 0.3539217
## [2,] 0.6770591 0.3319744 0.03319448 0.4966668
## [3,] 0.4437938 0.2822738 0.05440338 0.2858697
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1281711 0.2452114 0.04420028 0.1845003
## [2,] 0.3529528 0.1730592 0.01730437 0.2589138
## [3,] 0.2313509 0.1471501 0.02836063 0.1490247
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2886049 0.5521465 0.09952649 0.4154424
## [2,] 0.7947496 0.3896801 0.03896454 0.5830004
## [3,] 0.5209367 0.3313403 0.06386010 0.3355613
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2739825 0.5241715 0.09448389 0.3943937
## [2,] 0.7544829 0.3699367 0.03699037 0.5534622
## [3,] 0.4945430 0.3145526 0.06062458 0.3185598
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01857936 0.03554523 0.006407161 0.02674471
## [2,] 0.05116315 0.02508622 0.002508399 0.03753149
## [3,] 0.03353605 0.02133051 0.004111086 0.02160225
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3258001 0.6233066 0.11235336 0.4689842
## [2,] 0.8971761 0.4399017 0.04398625 0.6581369
## [3,] 0.5880745 0.3740431 0.07209033 0.3788081
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1124178 0.2150730 0.03876771 0.1618238
## [2,] 0.3095721 0.1517888 0.01517753 0.2270912
## [3,] 0.2029161 0.1290642 0.02487489 0.1307084
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2460200 0.4706748 0.08484091 0.3541420
## [2,] 0.6774807 0.3321811 0.03321515 0.4969761
## [3,] 0.4440701 0.2824495 0.05443726 0.2860477
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2566762 0.4910618 0.08851574 0.3694815
## [2,] 0.7068254 0.3465693 0.03465384 0.5185023
## [3,] 0.4633048 0.2946837 0.05679518 0.2984378
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01842129 0.03524281 0.006352650 0.02651716
## [2,] 0.05072786 0.02487279 0.002487057 0.03721218
## [3,] 0.03325073 0.02114903 0.004076109 0.02141845
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09864407 0.1887216 0.03401777 0.1419966
## [2,] 0.27164238 0.1331912 0.01331793 0.1992673
## [3,] 0.17805417 0.1132508 0.02182714 0.1146936
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1560639 0.2985747 0.05381922 0.2246516
## [2,] 0.4297630 0.2107206 0.02107018 0.3152591
## [3,] 0.2816979 0.1791732 0.03453252 0.1814557
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02181727 0.04173986 0.007523766 0.03140563
## [2,] 0.06007959 0.02945811 0.002945548 0.04407227
## [3,] 0.03938053 0.02504787 0.004827544 0.02536697
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02607607 0.04988761 0.008992430 0.03753611
## [2,] 0.07180731 0.03520843 0.003520529 0.05267533
## [3,] 0.04706773 0.02993730 0.005769896 0.03031868
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2225256 0.4257264 0.07673879 0.3203223
## [2,] 0.6127829 0.3004585 0.03004318 0.4495160
## [3,] 0.4016624 0.2554762 0.04923862 0.2587308
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2860079 0.5471780 0.09863089 0.4117041
## [2,] 0.7875980 0.3861736 0.03861392 0.5777542
## [3,] 0.5162490 0.3283587 0.06328545 0.3325418
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.006851077 0.013107185 0.0023626193 0.009862021
## [2,] 0.018866242 0.009250460 0.0009249636 0.013839613
## [3,] 0.012366306 0.007865554 0.0015159493 0.007965756
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07187303 0.13750437 0.024785680 0.10346013
## [2,] 0.19792127 0.09704439 0.009703574 0.14518810
## [3,] 0.12973199 0.08251566 0.015903466 0.08356686
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2024819 0.3873796 0.06982663 0.2914696
## [2,] 0.5575871 0.2733951 0.02733707 0.4090263
## [3,] 0.3654831 0.2324645 0.04480351 0.2354259
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2614059 0.5001105 0.09014679 0.3762898
## [2,] 0.7198499 0.3529554 0.03529240 0.5280566
## [3,] 0.4718419 0.3001137 0.05784172 0.3039370
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06740809 0.12896225 0.023245930 0.09703292
## [2,] 0.18562590 0.09101575 0.009100763 0.13616865
## [3,] 0.12167271 0.07738958 0.014915502 0.07837547
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0604427 0.11563636 0.020843888 0.08700634
## [2,] 0.1664449 0.08161093 0.008160366 0.12209810
## [3,] 0.1091001 0.06939278 0.013374257 0.07027680
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2520133 0.4821410 0.08690772 0.3627693
## [2,] 0.6939849 0.3402734 0.03402430 0.5090830
## [3,] 0.4548882 0.2893303 0.05576341 0.2930162
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1480399 0.2832236 0.05105212 0.2131012
## [2,] 0.4076669 0.1998865 0.01998686 0.2990501
## [3,] 0.2672145 0.1699610 0.03275704 0.1721262
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1436682 0.2748597 0.04954450 0.2068081
## [2,] 0.3956280 0.1939836 0.01939663 0.2902188
## [3,] 0.2593234 0.1649419 0.03178969 0.1670431
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07817855 0.14956783 0.02696016 0.1125368
## [2,] 0.21528519 0.10555823 0.01055488 0.1579257
## [3,] 0.14111356 0.08975488 0.01729870 0.0908983
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3205165 0.6131984 0.11053131 0.4613787
## [2,] 0.8826265 0.4327678 0.04327292 0.6474638
## [3,] 0.5785376 0.3679772 0.07092123 0.3726650
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1794714 0.3433569 0.06189138 0.2583463
## [2,] 0.4942217 0.2423259 0.02423043 0.3625437
## [3,] 0.3239488 0.2060467 0.03971194 0.2086716
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2024487 0.3873160 0.06981517 0.2914218
## [2,] 0.5574956 0.2733502 0.02733259 0.4089592
## [3,] 0.3654232 0.2324264 0.04479616 0.2353873
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2640577 0.5051838 0.09106127 0.3801070
## [2,] 0.7271523 0.3565359 0.03565042 0.5334134
## [3,] 0.4766285 0.3031582 0.05842849 0.3070202
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3050523 0.5836129 0.10519842 0.4391182
## [2,] 0.8400417 0.4118877 0.04118510 0.6162251
## [3,] 0.5506244 0.3502231 0.06749944 0.3546847
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2171131 0.4153714 0.07487226 0.3125310
## [2,] 0.5978780 0.2931504 0.02931243 0.4385823
## [3,] 0.3918927 0.2492623 0.04804098 0.2524377
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1519554 0.2907146 0.05240239 0.2187375
## [2,] 0.4184492 0.2051732 0.02051550 0.3069597
## [3,] 0.2742820 0.1744563 0.03362343 0.1766788
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1432915 0.2741391 0.04941459 0.2062659
## [2,] 0.3945907 0.1934750 0.01934577 0.2894579
## [3,] 0.2586435 0.1645094 0.03170634 0.1666052
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0789674 0.15107702 0.02723220 0.1136724
## [2,] 0.2174575 0.10662336 0.01066139 0.1595192
## [3,] 0.1425375 0.09066054 0.01747325 0.0918155
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02046104 0.03914517 0.007056064 0.02945335
## [2,] 0.05634484 0.02762690 0.002762444 0.04133260
## [3,] 0.03693251 0.02349082 0.004527448 0.02379007
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.16645063 0.31844616 0.05740112 0.23960316
## [2,] 0.45836558 0.22474496 0.02247249 0.33624090
## [3,] 0.30044612 0.19109790 0.03683081 0.19353236
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.25579007 0.48936653 0.08821016 0.36820593
## [2,] 0.70438522 0.34537285 0.03453421 0.51671227
## [3,] 0.46170528 0.29366632 0.05659910 0.29740744
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.19552318 0.37406651 0.06742689 0.28145265
## [2,] 0.53842448 0.26399929 0.02639758 0.39496930
## [3,] 0.35292255 0.22447538 0.04326374 0.22733505
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.14329147 0.27413905 0.04941459 0.20626589
## [2,] 0.39459073 0.19347499 0.01934577 0.28945791
## [3,] 0.25864345 0.16450943 0.03170634 0.16660517
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.07896740 0.15107702 0.02723220 0.11367237
## [2,] 0.21745750 0.10662336 0.01066139 0.15951919
## [3,] 0.14253745 0.09066054 0.01747325 0.09181550
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.020461036 0.039145172 0.007056064 0.029453351
## [2,] 0.056344844 0.027626899 0.002762444 0.041332600
## [3,] 0.036932507 0.023490815 0.004527448 0.023790073
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.228545
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 2.228545
einsum::einsum('ij->', arrC)
## [1] 5.357995
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 5.357995
einsum::einsum('ijk->', arrE)
## [1] 29.76807
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 29.76807
einsum::einsum('ij->i', arrC)
## [1] 1.645729 2.192810 1.519456
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 1.645729 2.192810 1.519456
einsum::einsum('ij->j', arrC)
## [1] 1.9474733 1.5455166 0.2456371 1.6193678
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 1.9474733 1.5455166 0.2456371 1.6193678
einsum::einsum('ijk->i', arrE)
## [1] 10.198421 9.065558 10.504094
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 10.198421 9.065558 10.504094
einsum::einsum('ijk->j', arrE)
## [1] 7.138086 7.530539 9.406457 5.692990
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 7.138086 7.530539 9.406457 5.692990
einsum::einsum('ijk->k', arrE)
## [1] 6.017648 6.905814 6.191803 4.643872 6.008935
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 6.017648 6.905814 6.191803 4.643872 6.008935
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 1.650258 3.367452 3.637364 1.543347
## [2,] 2.080997 1.925980 3.088208 1.970373
## [3,] 3.406830 2.237107 2.680886 2.179271
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 1.650258 3.367452 3.637364 1.543347
## [2,] 2.080997 1.925980 3.088208 1.970373
## [3,] 3.406830 2.237107 2.680886 2.179271
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.763317 1.163382 1.8914179 0.7718716 1.5480969
## [2,] 1.042091 1.838500 1.7650242 1.0410745 1.8438495
## [3,] 1.777041 2.432976 1.7557536 1.5165068 1.9241799
## [4,] 1.435199 1.470956 0.7796078 1.3144186 0.6928087
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.7633172 1.1633823 1.8914179 0.7718716 1.5480969
## [2,] 1.0420907 1.8384999 1.7650242 1.0410745 1.8438495
## [3,] 1.7770410 2.4329758 1.7557536 1.5165068 1.9241799
## [4,] 1.4351995 1.4709559 0.7796078 1.3144186 0.6928087
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.763317 1.163382 1.8914179 0.7718716 1.5480969
## [2,] 1.042091 1.838500 1.7650242 1.0410745 1.8438495
## [3,] 1.777041 2.432976 1.7557536 1.5165068 1.9241799
## [4,] 1.435199 1.470956 0.7796078 1.3144186 0.6928087
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.7633172 1.1633823 1.8914179 0.7718716 1.5480969
## [2,] 1.0420907 1.8384999 1.7650242 1.0410745 1.8438495
## [3,] 1.7770410 2.4329758 1.7557536 1.5165068 1.9241799
## [4,] 1.4351995 1.4709559 0.7796078 1.3144186 0.6928087
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 2.517826
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 2.517826
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.9563954 0.3209917 0.5636314
## [2,] 0.2077481 0.8994595 0.9475187
## [3,] 0.6529005 0.7037387 0.6619709
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.9563954 0.3209917 0.5636314
## [2,] 0.2077481 0.8994595 0.9475187
## [3,] 0.6529005 0.7037387 0.6619709
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.12215879 0.53499979 0.08931371
## [2,] 0.08780209 0.02250049 0.55022628
## [3,] 0.38805740 0.56035548 0.91918809
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.6401641 0.07870072 0.6575609
## [2,] 0.4256592 0.23678708 0.7442608
## [3,] 0.8246201 0.79871850 0.5625044
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.3467494 0.4951696 0.2828480
## [2,] 0.5855375 0.4879278 0.9071557
## [3,] 0.7431418 0.8852285 0.9260831
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.12215879 0.53499979 0.08931371
## [2,] 0.08780209 0.02250049 0.55022628
## [3,] 0.38805740 0.56035548 0.91918809
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.64016412 0.07870072 0.65756085
## [2,] 0.42565916 0.23678708 0.74426078
## [3,] 0.82462014 0.79871850 0.56250444
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.3467494 0.4951696 0.2828480
## [2,] 0.5855375 0.4879278 0.9071557
## [3,] 0.7431418 0.8852285 0.9260831
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.741003
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.741003
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 3.232147
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 3.232147
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 19.96924
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 19.96924
The inner product is an operation that eliminates all subscripts, while the outer product is an operation that leaves all subscripts intact. In the middle of the two, the operation that eliminates some subscripts while keeping others by summing them is called contracted product.
einsum::einsum('ijk,ijk->jk', arrE, arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.0702646 0.6981481 1.3049690 0.4128552 1.0549442
## [2,] 0.3739120 1.5770318 1.4792090 0.7089261 1.1644333
## [3,] 1.5337744 2.1739384 1.1328573 0.9277849 1.3303422
## [4,] 0.8761782 0.9219237 0.2804798 0.7257638 0.2215013
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.0702646 0.6981481 1.3049690 0.4128552 1.0549442
## [2,] 0.3739120 1.5770318 1.4792090 0.7089261 1.1644333
## [3,] 1.5337744 2.1739384 1.1328573 0.9277849 1.3303422
## [4,] 0.8761782 0.9219237 0.2804798 0.7257638 0.2215013
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 0.8409137 1.017676 0.7059313
## [2,] 1.0176764 1.657620 1.0923009
## [3,] 0.7059313 1.092301 0.7336133
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.8409137 1.0176764 0.7059313
## [2,] 1.0176764 1.6576195 1.0923009
## [3,] 0.7059313 1.0923009 0.7336133
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.12273945 0.930759039 0.399895469
## [2,] 0.44924713 0.223765529 0.161780097
## [3,] 0.01459668 0.002237256 0.006009466
## [4,] 0.25433044 0.500857697 0.165928294
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.122739449 0.930759039 0.399895469
## [2,] 0.449247134 0.223765529 0.161780097
## [3,] 0.014596676 0.002237256 0.006009466
## [4,] 0.254330440 0.500857697 0.165928294
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.2257287 1.007983e-06 0.49251099 0.003878078 0.1681655
## [2,] 0.1859291 6.815670e-01 0.61159167 0.666456701 0.2624257
## [3,] 0.7639724 9.875305e-01 0.10296422 0.334032058 0.7581663
## [4,] 0.5966490 1.871577e-02 0.00276475 0.029764835 0.1672848
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.5330687260 0.4725961 0.133843111 0.0055398769 0.04979561
## [2,] 0.1100739556 0.8906803 0.002812402 0.0003824138 0.33392248
## [3,] 0.0006753341 0.9882933 0.493124546 0.5567325174 0.38405015
## [4,] 0.0250673747 0.3430966 0.079278935 0.5174432961 0.05080559
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.3114672 0.225550972 0.6786149 0.40343721 0.836983113
## [2,] 0.0779090 0.004784572 0.8648049 0.04208697 0.568085162
## [3,] 0.7691267 0.198114602 0.5367686 0.03702030 0.188125762
## [4,] 0.2544618 0.560111280 0.1984361 0.17855564 0.003410917
DelayedTensor::einsum('ijk,ijk->jki', darrE, darrE)
## <4 x 5 x 3> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.257287e-01 1.007983e-06 4.925110e-01 3.878078e-03 1.681655e-01
## [2,] 1.859291e-01 6.815670e-01 6.115917e-01 6.664567e-01 2.624257e-01
## [3,] 7.639724e-01 9.875305e-01 1.029642e-01 3.340321e-01 7.581663e-01
## [4,] 5.966490e-01 1.871577e-02 2.764750e-03 2.976484e-02 1.672848e-01
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.5330687260 0.4725961475 0.1338431108 0.0055398769 0.0497956084
## [2,] 0.1100739556 0.8906802989 0.0028124018 0.0003824138 0.3339224819
## [3,] 0.0006753341 0.9882933232 0.4931245465 0.5567325174 0.3840501474
## [4,] 0.0250673747 0.3430966026 0.0792789346 0.5174432961 0.0508055919
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.311467214 0.225550972 0.678614866 0.403437205 0.836983113
## [2,] 0.077908999 0.004784572 0.864804906 0.042086973 0.568085162
## [3,] 0.769126739 0.198114602 0.536768558 0.037020296 0.188125762
## [4,] 0.254461841 0.560111280 0.198436148 0.178555636 0.003410917
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 2.552790 1.246203 2.218655
## [2,] 1.957126 3.211089 1.737599
## [3,] 1.857296 1.402671 2.931836
## [4,] 1.629122 1.559466 1.455283
## [5,] 2.202087 1.646128 2.160720
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 2.552790 1.246203 2.218655
## [2,] 1.957126 3.211089 1.737599
## [3,] 1.857296 1.402671 2.931836
## [4,] 1.629122 1.559466 1.455283
## [5,] 2.202087 1.646128 2.160720
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.02678946 1.196273e-07 0.0584511544 0.000460250 0.01995786
## [2,] 0.08076546 2.960649e-01 0.2656683843 0.289501125 0.11399469
## [3,] 0.01078263 1.393790e-02 0.0014532266 0.004714494 0.01070068
## [4,] 0.14672708 4.602556e-03 0.0006799033 0.007319726 0.04113844
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 3.484421e-01 0.308913965 0.0874869724 3.621158e-03 0.0325490568
## [2,] 1.729768e-02 0.139966832 0.0004419576 6.009479e-05 0.0524745770
## [3,] 1.061071e-06 0.001552786 0.0007747873 8.747268e-04 0.0006034118
## [4,] 8.817253e-03 0.120681548 0.0278857455 1.820066e-01 0.0178704698
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.069668193 0.0504506669 0.151790844 0.0902398063 0.1872142504
## [2,] 0.007049989 0.0004329561 0.078256237 0.0038084522 0.0514060535
## [3,] 0.002585291 0.0006659292 0.001804258 0.0001244376 0.0006323534
## [4,] 0.023616680 0.0519840955 0.018416918 0.0165718020 0.0003165682
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,] 2.678946e-02 1.196273e-07 5.845115e-02 4.602500e-04 1.995786e-02
## [2,] 8.076546e-02 2.960649e-01 2.656684e-01 2.895011e-01 1.139947e-01
## [3,] 1.078263e-02 1.393790e-02 1.453227e-03 4.714494e-03 1.070068e-02
## [4,] 1.467271e-01 4.602556e-03 6.799033e-04 7.319726e-03 4.113844e-02
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 3.484421e-01 3.089140e-01 8.748697e-02 3.621158e-03 3.254906e-02
## [2,] 1.729768e-02 1.399668e-01 4.419576e-04 6.009479e-05 5.247458e-02
## [3,] 1.061071e-06 1.552786e-03 7.747873e-04 8.747268e-04 6.034118e-04
## [4,] 8.817253e-03 1.206815e-01 2.788575e-02 1.820066e-01 1.787047e-02
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0696681930 0.0504506669 0.1517908444 0.0902398063 0.1872142504
## [2,] 0.0070499891 0.0004329561 0.0782562372 0.0038084522 0.0514060535
## [3,] 0.0025852914 0.0006659292 0.0018042580 0.0001244376 0.0006323534
## [4,] 0.0236166796 0.0519840955 0.0184169183 0.0165718020 0.0003165682
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-14 r86421)
## Platform: x86_64-apple-darwin20
## Running under: macOS Monterey 12.7.1
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## time zone: America/New_York
## tzcode source: internal
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] einsum_0.1.2 DelayedRandomArray_1.12.0
## [3] HDF5Array_1.32.0 rhdf5_2.48.0
## [5] DelayedArray_0.30.0 SparseArray_1.4.0
## [7] S4Arrays_1.4.0 abind_1.4-5
## [9] IRanges_2.38.0 S4Vectors_0.42.0
## [11] MatrixGenerics_1.16.0 matrixStats_1.3.0
## [13] BiocGenerics_0.50.0 Matrix_1.7-0
## [15] DelayedTensor_1.10.0 BiocStyle_2.32.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.16.0 parallel_4.4.0 jquerylib_0.1.4
## [10] BiocParallel_1.38.0 yaml_2.3.8 fastmap_1.1.1
## [13] lattice_0.22-6 R6_2.5.1 XVector_0.44.0
## [16] ScaledMatrix_1.12.0 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.26.0 BiocSingular_1.20.0 zlibbioc_1.50.0
## [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.20.0
## [37] rmarkdown_2.26 tools_4.4.0 htmltools_0.5.8.1