DelayedTensor 1.8.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2023-10-24 14:41:21.735014
Compiled: Tue Oct 24 19:36:56 2023
einsum
einsum
is an easy and intuitive way to write tensor operations.
It was originally introduced by
Numpy
1 https://numpy.org/doc/stable/reference/generated/numpy.einsum.html
package of Python but similar tools have been implemented in other languages
(e.g. R, Julia) inspired by Numpy
.
In this vignette, we will use CRAN einsum package first.
einsum
is named after
Einstein summation2 https://en.wikipedia.org/wiki/Einstein_notation
introduced by Albert Einstein,
which is a notational convention that implies summation over
a set of indexed terms in a formula.
Here, we consider a simple example of einsum
; matrix multiplication.
If we naively implement the matrix multiplication,
the calculation would look like the following in a for loop.
A <- matrix(runif(3*4), nrow=3, ncol=4)
B <- matrix(runif(4*5), nrow=4, ncol=5)
C <- matrix(0, nrow=3, ncol=5)
I <- nrow(A)
J <- ncol(A)
K <- ncol(B)
for(i in 1:I){
for(j in 1:J){
for(k in 1:K){
C[i,k] = C[i,k] + A[i,j] * B[j,k]
}
}
}
Therefore, any programming language can implement this. However, when analyzing tensor data, such operations tend to be more complicated and increase the possibility of causing bugs because the order of tensors is larger or more tensors are handled simultaneously. In addition, several programming languages, especially R, are known to significantly slow down the speed of computation if the code is written in for loop.
Obviously, in the case of the R language, it should be executed using the built-in matrix multiplication function (%*%) prepared by the R, as shown below.
C <- A %*% B
However, more complex operations than matrix multiplication are not always provided by programming languages as standard.
einsum
is a function that solves such a problem.
To put it simply, einsum
is a wrapper for the for loop above.
Like the Einstein summation, it omits many notations such as for,
array size (e.g. I, J, and K), brackets (e.g. {}, (), and []),
and even addition operator (+) and
extracts the array subscripts (e.g. i, j, and k)
to concisely express the tensor operation as follows.
suppressPackageStartupMessages(library("einsum"))
C <- einsum('ij,jk->ik', A, B)
DelayedTensor
CRAN einsum is easy to use because the syntax is almost
the same as that of Numpy
‘s einsum
,
except that it prohibits the implicit modes that do not use’->’.
It is extremely fast because the internal calculation
is actually performed by C++.
When the input tensor is huge, however,
it is not scalable because it assumes that the input is R’s standard array.
Using einsum
of DelayedTensor,
we can augment the CRAN einsum
’s functionality;
in DelayedTensor,
the input DelayedArray objects are divided into
multiple block tensors and the CRAN einsum
is incremently applied in the block processing.
A surprisingly large number of tensor operations can be handled
uniformly in einsum
.
In more detail, einsum
is capable of performing any tensor operation
that can be described by a combination of the following
three operations3 https://ajcr.net/Basic-guide-to-einsum/.
Some typical operations are introduced below. Here we use the arrays and DelayedArray objects below.
suppressPackageStartupMessages(library("DelayedTensor"))
suppressPackageStartupMessages(library("DelayedArray"))
arrA <- array(runif(3), dim=c(3))
arrB <- array(runif(3*3), dim=c(3,3))
arrC <- array(runif(3*4), dim=c(3,4))
arrD <- array(runif(3*3*3), dim=c(3,3,3))
arrE <- array(runif(3*4*5), dim=c(3,4,5))
darrA <- DelayedArray(arrA)
darrB <- DelayedArray(arrB)
darrC <- DelayedArray(arrC)
darrD <- DelayedArray(arrD)
darrE <- DelayedArray(arrE)
If the same subscript is written on both sides of ->,
einsum
will simply output the object without any calculation.
einsum::einsum('i->i', arrA)
## [1] 0.5172846 0.8708766 0.7105915
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.5172846 0.8708766 0.7105915
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.2346040 0.6284997 0.7199236 0.4279983
## [2,] 0.1845706 0.8836400 0.7700790 0.8397927
## [3,] 0.4298575 0.8970225 0.7231585 0.3507740
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.2346040 0.6284997 0.7199236 0.4279983
## [2,] 0.1845706 0.8836400 0.7700790 0.8397927
## [3,] 0.4298575 0.8970225 0.7231585 0.3507740
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.476325132 0.9510722 0.1071784 0.8049508
## [2,] 0.009257058 0.2747214 0.4206767 0.3676130
## [3,] 0.728687260 0.3742485 0.8894915 0.9849512
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1741179 0.8948609 0.1729172 0.9510966
## [2,] 0.6968433 0.6881038 0.9256547 0.7714190
## [3,] 0.1354967 0.1176650 0.9231015 0.5052018
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5968173 0.4059654 0.9347186 0.8167805
## [2,] 0.9165321 0.7975244 0.5751779 0.4439014
## [3,] 0.1699540 0.8750953 0.9286727 0.1864744
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3499032 0.5691934 0.5852980 0.07379478
## [2,] 0.9624966 0.6438895 0.3952540 0.56711487
## [3,] 0.4462390 0.8171192 0.4371054 0.62035828
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4494057 0.4625389 0.4271757 0.12606150
## [2,] 0.4842795 0.3970323 0.8930560 0.42155015
## [3,] 0.8117854 0.1528657 0.6756063 0.05787811
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.476325132 0.951072197 0.107178433 0.804950770
## [2,] 0.009257058 0.274721418 0.420676673 0.367613001
## [3,] 0.728687260 0.374248528 0.889491498 0.984951175
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.1741179 0.8948609 0.1729172 0.9510966
## [2,] 0.6968433 0.6881038 0.9256547 0.7714190
## [3,] 0.1354967 0.1176650 0.9231015 0.5052018
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.5968173 0.4059654 0.9347186 0.8167805
## [2,] 0.9165321 0.7975244 0.5751779 0.4439014
## [3,] 0.1699540 0.8750953 0.9286727 0.1864744
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.34990316 0.56919338 0.58529795 0.07379478
## [2,] 0.96249659 0.64388947 0.39525403 0.56711487
## [3,] 0.44623904 0.81711916 0.43710545 0.62035828
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.44940570 0.46253886 0.42717575 0.12606150
## [2,] 0.48427949 0.39703231 0.89305603 0.42155015
## [3,] 0.81178538 0.15286568 0.67560628 0.05787811
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.5555358 0.7705289 0.6453627
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.5555358 0.7705289 0.6453627
einsum::einsum('iii->i', arrD)
## [1] 0.1968715 0.2683786 0.0478911
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.1968715 0.2683786 0.0478911
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.2675834 0.7584261 0.5049402
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.2675834 0.7584261 0.5049402
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.05503902 0.3950118 0.5182900 0.1831825
## [2,] 0.03406629 0.7808197 0.5930217 0.7052518
## [3,] 0.18477748 0.8046493 0.5229582 0.1230424
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.05503902 0.39501183 0.51828995 0.18318253
## [2,] 0.03406629 0.78081973 0.59302173 0.70525183
## [3,] 0.18477748 0.80464931 0.52295824 0.12304239
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 2.268856e-01 0.90453832 0.01148722 0.6479457
## [2,] 8.569313e-05 0.07547186 0.17696886 0.1351393
## [3,] 5.309851e-01 0.14006196 0.79119512 0.9701288
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03031706 0.80077599 0.02990036 0.9045848
## [2,] 0.48559064 0.47348691 0.85683655 0.5950872
## [3,] 0.01835935 0.01384506 0.85211633 0.2552288
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.35619086 0.1648079 0.8736989 0.6671303
## [2,] 0.84003114 0.6360452 0.3308296 0.1970485
## [3,] 0.02888436 0.7657918 0.8624329 0.0347727
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1224322 0.3239811 0.3425737 0.005445669
## [2,] 0.9263997 0.4145937 0.1562257 0.321619274
## [3,] 0.1991293 0.6676837 0.1910612 0.384844401
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2019655 0.21394220 0.1824791 0.015891502
## [2,] 0.2345266 0.15763466 0.7975491 0.177704528
## [3,] 0.6589955 0.02336792 0.4564439 0.003349875
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 2.268856e-01 9.045383e-01 1.148722e-02 6.479457e-01
## [2,] 8.569313e-05 7.547186e-02 1.769689e-01 1.351393e-01
## [3,] 5.309851e-01 1.400620e-01 7.911951e-01 9.701288e-01
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.03031706 0.80077599 0.02990036 0.90458483
## [2,] 0.48559064 0.47348691 0.85683655 0.59508724
## [3,] 0.01835935 0.01384506 0.85211633 0.25522885
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.35619086 0.16480789 0.87369890 0.66713033
## [2,] 0.84003114 0.63604524 0.33082965 0.19704849
## [3,] 0.02888436 0.76579176 0.86243293 0.03477270
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.122432220 0.323981108 0.342573694 0.005445669
## [2,] 0.926399685 0.414593653 0.156225749 0.321619274
## [3,] 0.199129277 0.667683720 0.191061172 0.384844401
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.201965481 0.213942198 0.182479119 0.015891502
## [2,] 0.234526626 0.157634655 0.797549072 0.177704528
## [3,] 0.658995501 0.023367916 0.456443851 0.003349875
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.2675834 0.4504911 0.3675780
## [2,] 0.4504911 0.7584261 0.6188375
## [3,] 0.3675780 0.6188375 0.5049402
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.2675834 0.4504911 0.3675780
## [2,] 0.4504911 0.7584261 0.6188375
## [3,] 0.3675780 0.6188375 0.5049402
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11174776 0.2993702 0.3429177 0.2038663
## [2,] 0.08791559 0.4209000 0.3668080 0.4000144
## [3,] 0.20475193 0.4272743 0.3444586 0.1670825
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.002171743 0.005818058 0.006664374 0.003962005
## [2,] 0.001708580 0.008179907 0.007128666 0.007774010
## [3,] 0.003979216 0.008303789 0.006694320 0.003247135
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1709529 0.4579797 0.5245991 0.3118769
## [2,] 0.1344942 0.6438972 0.5611468 0.6119463
## [3,] 0.3132317 0.6536488 0.5269564 0.2556045
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2231253 0.5977486 0.6846993 0.4070573
## [2,] 0.1755399 0.8404055 0.7324008 0.7987035
## [3,] 0.4088255 0.8531331 0.6877760 0.3336114
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06445073 0.1726623 0.1977784 0.11758030
## [2,] 0.05070548 0.2427548 0.2115572 0.23070905
## [3,] 0.11809106 0.2464313 0.1986671 0.09636513
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08780019 0.2352151 0.2694303 0.1601777
## [2,] 0.06907526 0.3307010 0.2882009 0.3142912
## [3,] 0.16087354 0.3357093 0.2706410 0.1312767
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02514449 0.06736161 0.07716028 0.04587219
## [2,] 0.01978198 0.09470716 0.08253587 0.09000767
## [3,] 0.04607145 0.09614146 0.07750700 0.03759541
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09869242 0.2643951 0.3028551 0.1800489
## [2,] 0.07764453 0.3717268 0.3239543 0.3532812
## [3,] 0.18083103 0.3773564 0.3042159 0.1475624
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2086782 0.5590451 0.6403659 0.3807008
## [2,] 0.1641739 0.7859903 0.6849788 0.7469885
## [3,] 0.3823546 0.7978939 0.6432433 0.3120105
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1888446 0.5059113 0.5795030 0.3445175
## [2,] 0.1485702 0.7112867 0.6198757 0.6759918
## [3,] 0.3460141 0.7220589 0.5821070 0.2823558
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08624347 0.2310446 0.2646533 0.1573377
## [2,] 0.06785054 0.3248376 0.2830911 0.3087187
## [3,] 0.15802121 0.3297571 0.2658425 0.1289491
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2310734 0.6190415 0.7090896 0.4215574
## [2,] 0.1817930 0.8703423 0.7584903 0.8271548
## [3,] 0.4233887 0.8835233 0.7122758 0.3454953
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04084876 0.1094331 0.1253516 0.07452218
## [2,] 0.03213704 0.1538576 0.1340846 0.14622298
## [3,] 0.07484590 0.1561877 0.1259149 0.06107604
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1634822 0.4379658 0.5016739 0.2982478
## [2,] 0.1286168 0.6157587 0.5366245 0.5852040
## [3,] 0.2995433 0.6250841 0.5039282 0.2444345
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03178806 0.08515963 0.09754727 0.05799235
## [2,] 0.02500870 0.11973031 0.10434317 0.11378914
## [3,] 0.05824427 0.12154358 0.09798559 0.04752872
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2099379 0.5624198 0.6442314 0.3829989
## [2,] 0.1651650 0.7907349 0.6891136 0.7514977
## [3,] 0.3846627 0.8027103 0.6471263 0.3138939
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1614319 0.4324730 0.4953822 0.2945073
## [2,] 0.1270037 0.6080361 0.5298944 0.5778646
## [3,] 0.2957866 0.6172446 0.4976082 0.2413689
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02760468 0.07395242 0.08470982 0.05036042
## [2,] 0.02171750 0.10397352 0.09061136 0.09881422
## [3,] 0.05057919 0.10554816 0.08509046 0.04127383
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04056706 0.1086784 0.1244872 0.07400827
## [2,] 0.03191543 0.1527966 0.1331599 0.14521462
## [3,] 0.07432976 0.1551106 0.1250466 0.06065486
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2171623 0.5817736 0.6664006 0.3961786
## [2,] 0.1708486 0.8179455 0.7128273 0.7773581
## [3,] 0.3978996 0.8303330 0.6693950 0.3246956
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2165633 0.5801690 0.6645625 0.3950858
## [2,] 0.1703774 0.8156894 0.7108611 0.7752139
## [3,] 0.3968021 0.8280428 0.6675487 0.3238000
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2231310 0.5977639 0.6847169 0.4070677
## [2,] 0.1755444 0.8404271 0.7324196 0.7987241
## [3,] 0.4088360 0.8531551 0.6877936 0.3336200
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1809779 0.4848366 0.5553627 0.3301660
## [2,] 0.1423812 0.6816567 0.5940536 0.6478320
## [3,] 0.3316002 0.6919802 0.5578582 0.2705937
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11852234 0.3175192 0.3637067 0.2162255
## [2,] 0.09324537 0.4464165 0.3890453 0.4242648
## [3,] 0.21716478 0.4531774 0.3653410 0.1772116
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1400157 0.3750995 0.4296628 0.2554368
## [2,] 0.1101549 0.5273716 0.4595965 0.5012028
## [3,] 0.2565464 0.5353585 0.4315935 0.2093480
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2150221 0.5760401 0.6598331 0.3922742
## [2,] 0.1691648 0.8098845 0.7058022 0.7696970
## [3,] 0.3939782 0.8221499 0.6627980 0.3214956
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03987188 0.1068160 0.1223539 0.07274001
## [2,] 0.03136850 0.1501781 0.1308780 0.14272612
## [3,] 0.07305599 0.1524525 0.1229037 0.05961544
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09524109 0.2551491 0.2922640 0.1737525
## [2,] 0.07492925 0.3587273 0.3126254 0.3409268
## [3,] 0.17450726 0.3641601 0.2935773 0.1424021
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1871024 0.5012438 0.5741566 0.3413391
## [2,] 0.1471995 0.7047245 0.6141569 0.6697552
## [3,] 0.3428219 0.7153973 0.5767366 0.2797508
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2053008 0.5499971 0.6300017 0.3745393
## [2,] 0.1615168 0.7732692 0.6738925 0.7348987
## [3,] 0.3761663 0.7849801 0.6328326 0.3069607
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2192887 0.5874703 0.6729260 0.4000580
## [2,] 0.1725215 0.8259548 0.7198072 0.7849699
## [3,] 0.4017958 0.8384636 0.6759497 0.3278750
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1349390 0.3614991 0.4140841 0.2461752
## [2,] 0.1061609 0.5082503 0.4429325 0.4830302
## [3,] 0.2472446 0.5159475 0.4159448 0.2017575
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2178703 0.5836705 0.6685733 0.3974703
## [2,] 0.1714056 0.8206124 0.7151514 0.7798926
## [3,] 0.3991969 0.8330403 0.6715775 0.3257542
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1916199 0.5133462 0.5880195 0.3495806
## [2,] 0.1507536 0.7217399 0.6289855 0.6859263
## [3,] 0.3510992 0.7326704 0.5906617 0.2865053
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10414104 0.2789919 0.3195751 0.1899891
## [2,] 0.08193113 0.3922491 0.3418392 0.3727852
## [3,] 0.19081437 0.3981896 0.3210111 0.1557091
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04374763 0.1171991 0.1342473 0.07981072
## [2,] 0.03441768 0.1647762 0.1436000 0.15659984
## [3,] 0.08015742 0.1672717 0.1348505 0.06541037
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08208867 0.2199140 0.2519035 0.1497580
## [2,] 0.06458182 0.3091884 0.2694531 0.2938461
## [3,] 0.15040850 0.3138710 0.2530354 0.1227369
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2258055 0.6049288 0.6929240 0.4119469
## [2,] 0.1776485 0.8505005 0.7411985 0.8082976
## [3,] 0.4137364 0.8633811 0.6960376 0.3376188
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10468945 0.2804611 0.3212580 0.1909895
## [2,] 0.08236259 0.3943147 0.3436393 0.3747483
## [3,] 0.19181920 0.4002864 0.3227016 0.1565290
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1335350 0.3577379 0.4097757 0.2436138
## [2,] 0.1050563 0.5029621 0.4383239 0.4780045
## [3,] 0.2446721 0.5105793 0.4116170 0.1996582
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1510590 0.4046843 0.4635512 0.2755836
## [2,] 0.1188430 0.5689665 0.4958458 0.5407337
## [3,] 0.2767807 0.5775833 0.4656342 0.2258597
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1916994 0.5135591 0.5882633 0.3497256
## [2,] 0.1508161 0.7220392 0.6292463 0.6862107
## [3,] 0.3512448 0.7329742 0.5909067 0.2866241
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1373132 0.3678596 0.4213698 0.2505065
## [2,] 0.1080288 0.5171927 0.4507257 0.4915290
## [3,] 0.2515947 0.5250254 0.4232632 0.2053073
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09272816 0.2484170 0.2845527 0.1691680
## [2,] 0.07295226 0.3492623 0.3043768 0.3319315
## [3,] 0.16990291 0.3545517 0.2858313 0.1386448
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10254667 0.2747206 0.3146825 0.1870804
## [2,] 0.08067679 0.3862439 0.3366057 0.3670780
## [3,] 0.18789306 0.3920934 0.3160965 0.1533252
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01731255 0.04637999 0.05312660 0.03158404
## [2,] 0.01362034 0.06520802 0.05682781 0.06197232
## [3,] 0.03172124 0.06619557 0.05336532 0.02588529
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1330474 0.3564315 0.4082794 0.2427242
## [2,] 0.1046727 0.5011254 0.4367233 0.4762589
## [3,] 0.2437786 0.5087148 0.4101139 0.1989291
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1455385 0.3898950 0.4466106 0.2655123
## [2,] 0.1144999 0.5481734 0.4777249 0.5209724
## [3,] 0.2666657 0.5564753 0.4486174 0.2176056
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10543236 0.2824513 0.3235378 0.1923449
## [2,] 0.08294706 0.3971129 0.3460779 0.3774076
## [3,] 0.19318041 0.4031270 0.3249916 0.1576398
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.11361389 0.3043695 0.3486442 0.2072708
## [2,] 0.08938373 0.4279288 0.3729335 0.4066944
## [3,] 0.20817118 0.4344096 0.3502108 0.1698727
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1904481 0.5102068 0.5844234 0.3474428
## [2,] 0.1498317 0.7173261 0.6251389 0.6817315
## [3,] 0.3489520 0.7281897 0.5870495 0.2847532
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10851345 0.2907055 0.3329926 0.1979658
## [2,] 0.08537105 0.4087179 0.3561915 0.3884368
## [3,] 0.19882580 0.4149078 0.3344889 0.1622466
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09314535 0.2495347 0.2858329 0.1699291
## [2,] 0.07328047 0.3508336 0.3057463 0.3334248
## [3,] 0.17066732 0.3561469 0.2871173 0.1392686
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03586289 0.09607603 0.1100516 0.06542625
## [2,] 0.02821450 0.13507824 0.1177187 0.12837549
## [3,] 0.06571046 0.13712395 0.1105461 0.05362130
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10021712 0.2684798 0.3075339 0.1828305
## [2,] 0.07884406 0.3774696 0.3289591 0.3587391
## [3,] 0.18362470 0.3831862 0.3089158 0.1498421
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2095145 0.5612854 0.6429321 0.3822264
## [2,] 0.1648318 0.7891401 0.6877237 0.7499820
## [3,] 0.3838868 0.8010913 0.6458211 0.3132608
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1584999 0.4246183 0.4863849 0.2891583
## [2,] 0.1246970 0.5969928 0.5202702 0.5673692
## [3,] 0.2904144 0.6060340 0.4885704 0.2369851
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02957453 0.07922961 0.09075465 0.05395411
## [2,] 0.02326724 0.11139299 0.09707732 0.10586553
## [3,] 0.05418848 0.11308000 0.09116245 0.04421910
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09889734 0.2649441 0.3034839 0.1804227
## [2,] 0.07780574 0.3724986 0.3246269 0.3540148
## [3,] 0.18120650 0.3781400 0.3048476 0.1478688
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01357843 0.03637637 0.04166781 0.02477173
## [2,] 0.01068259 0.05114341 0.04457072 0.04860561
## [3,] 0.02487934 0.05191796 0.04185504 0.02030213
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.11174776 0.29937019 0.34291769 0.20386634
## [2,] 0.08791559 0.42089996 0.36680800 0.40001438
## [3,] 0.20475193 0.42727435 0.34445858 0.16708247
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.002171743 0.005818058 0.006664374 0.003962005
## [2,] 0.001708580 0.008179907 0.007128666 0.007774010
## [3,] 0.003979216 0.008303789 0.006694320 0.003247135
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.1709529 0.4579797 0.5245991 0.3118769
## [2,] 0.1344942 0.6438972 0.5611468 0.6119463
## [3,] 0.3132317 0.6536488 0.5269564 0.2556045
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.02957453 0.07922961 0.09075465 0.05395411
## [2,] 0.02326724 0.11139299 0.09707732 0.10586553
## [3,] 0.05418848 0.11308000 0.09116245 0.04421910
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.09889734 0.26494413 0.30348389 0.18042274
## [2,] 0.07780574 0.37249859 0.32462694 0.35401475
## [3,] 0.18120650 0.37813996 0.30484758 0.14786883
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.01357843 0.03637637 0.04166781 0.02477173
## [2,] 0.01068259 0.05114341 0.04457072 0.04860561
## [3,] 0.02487934 0.05191796 0.04185504 0.02030213
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.098753
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 2.098753
einsum::einsum('ij->', arrC)
## [1] 7.08992
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 7.08992
einsum::einsum('ijk->', arrE)
## [1] 32.82027
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 32.82027
einsum::einsum('ij->i', arrC)
## [1] 2.011025 2.678082 2.400812
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 2.011025 2.678082 2.400812
einsum::einsum('ij->j', arrC)
## [1] 0.849032 2.409162 2.213161 1.618565
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 0.849032 2.409162 2.213161 1.618565
einsum::einsum('ijk->i', arrE)
## [1] 10.33017 11.65210 10.83800
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 10.33017 11.65210 10.83800
einsum::einsum('ijk->j', arrE)
## [1] 7.408140 8.421896 9.291085 7.699146
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 7.408140 8.421896 9.291085 7.699146
einsum::einsum('ijk->k', arrE)
## [1] 6.389173 6.956478 7.647614 6.467766 5.359235
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 6.389173 6.956478 7.647614 6.467766 5.359235
These are the same as what the modeSum
function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 2.046569 3.283631 2.227288 2.772684
## [2,] 3.069409 2.801271 3.209819 2.571598
## [3,] 2.292162 2.336994 3.853977 2.354864
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 2.046569 3.283631 2.227288 2.772684
## [2,] 3.069409 2.801271 3.209819 2.571598
## [3,] 2.292162 2.336994 3.853977 2.354864
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.214269 1.006458 1.683303 1.758639 1.7454706
## [2,] 1.600042 1.700630 2.078585 2.030202 1.0124369
## [3,] 1.417347 2.021673 2.438569 1.417657 1.9958381
## [4,] 2.157515 2.227717 1.447156 1.261268 0.6054898
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.2142695 1.0064580 1.6833034 1.7586388 1.7454706
## [2,] 1.6000421 1.7006297 2.0785851 2.0302020 1.0124369
## [3,] 1.4173466 2.0216733 2.4385692 1.4176574 1.9958381
## [4,] 2.1575149 2.2277174 1.4471563 1.2612679 0.6054898
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.214269 1.006458 1.683303 1.758639 1.7454706
## [2,] 1.600042 1.700630 2.078585 2.030202 1.0124369
## [3,] 1.417347 2.021673 2.438569 1.417657 1.9958381
## [4,] 2.157515 2.227717 1.447156 1.261268 0.6054898
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.2142695 1.0064580 1.6833034 1.7586388 1.7454706
## [2,] 1.6000421 1.7006297 2.0785851 2.0302020 1.0124369
## [3,] 1.4173466 2.0216733 2.4385692 1.4176574 1.9958381
## [4,] 2.1575149 2.2277174 1.4471563 1.2612679 0.6054898
If we take the diagonal elements of a matrix
and add them together, we get trace
.
einsum::einsum('ii->', arrB)
## [1] 1.971427
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 1.971427
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.5555358 0.8452816 0.2726626
## [2,] 0.5077829 0.7705289 0.1201889
## [3,] 0.1483034 0.3459323 0.6453627
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.5555358 0.8452816 0.2726626
## [2,] 0.5077829 0.7705289 0.1201889
## [3,] 0.1483034 0.3459323 0.6453627
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.1968715 0.1025258 0.4598943
## [2,] 0.7471046 0.4581973 0.3631994
## [3,] 0.5145352 0.4794278 0.2917470
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.8190260 0.2256953 0.2589368
## [2,] 0.2696186 0.2683786 0.5384408
## [3,] 0.9014761 0.3196657 0.5135222
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.2873969 0.8546601 0.02686813
## [2,] 0.5606644 0.8580917 0.45093985
## [3,] 0.3947956 0.5691829 0.04789110
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.1968715 0.1025258 0.4598943
## [2,] 0.7471046 0.4581973 0.3631994
## [3,] 0.5145352 0.4794278 0.2917470
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.8190260 0.2256953 0.2589368
## [2,] 0.2696186 0.2683786 0.5384408
## [3,] 0.9014761 0.3196657 0.5135222
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.28739686 0.85466012 0.02686813
## [2,] 0.56066438 0.85809172 0.45093985
## [3,] 0.39479558 0.56918294 0.04789110
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.53095
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.53095
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 4.90011
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 4.90011
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 22.86453
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 22.86453
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.7579564 0.5342671 1.2251064 1.2479612 1.0954876
## [2,] 1.1200721 1.2881080 1.5666449 1.4062585 0.3949448
## [3,] 0.9796512 1.7388532 2.0669615 0.6898606 1.4364720
## [4,] 1.7532139 1.7549009 0.8989515 0.7119093 0.1969459
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.7579564 0.5342671 1.2251064 1.2479612 1.0954876
## [2,] 1.1200721 1.2881080 1.5666449 1.4062585 0.3949448
## [3,] 0.9796512 1.7388532 2.0669615 0.6898606 1.4364720
## [4,] 1.7532139 1.7549009 0.8989515 0.7119093 0.1969459
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.151523 1.512496 1.335374
## [2,] 1.512496 2.113160 1.723451
## [3,] 1.335374 1.723451 1.635427
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.151523 1.512496 1.335374
## [2,] 1.512496 2.113160 1.723451
## [3,] 1.335374 1.723451 1.635427
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.05503902 0.03406629 0.1847775
## [2,] 0.39501183 0.78081973 0.8046493
## [3,] 0.51828995 0.59302173 0.5229582
## [4,] 0.18318253 0.70525183 0.1230424
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.05503902 0.03406629 0.18477748
## [2,] 0.39501183 0.78081973 0.80464931
## [3,] 0.51828995 0.59302173 0.52295824
## [4,] 0.18318253 0.70525183 0.12304239
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.22688563 0.03031706 0.3561909 0.122432220 0.2019655
## [2,] 0.90453832 0.80077599 0.1648079 0.323981108 0.2139422
## [3,] 0.01148722 0.02990036 0.8736989 0.342573694 0.1824791
## [4,] 0.64794574 0.90458483 0.6671303 0.005445669 0.0158915
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 8.569313e-05 0.4855906 0.8400311 0.9263997 0.2345266
## [2,] 7.547186e-02 0.4734869 0.6360452 0.4145937 0.1576347
## [3,] 1.769689e-01 0.8568366 0.3308296 0.1562257 0.7975491
## [4,] 1.351393e-01 0.5950872 0.1970485 0.3216193 0.1777045
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.5309851 0.01835935 0.02888436 0.1991293 0.658995501
## [2,] 0.1400620 0.01384506 0.76579176 0.6676837 0.023367916
## [3,] 0.7911951 0.85211633 0.86243293 0.1910612 0.456443851
## [4,] 0.9701288 0.25522885 0.03477270 0.3848444 0.003349875
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.226885632 0.030317056 0.356190860 0.122432220 0.201965481
## [2,] 0.904538325 0.800775989 0.164807885 0.323981108 0.213942198
## [3,] 0.011487217 0.029900362 0.873698900 0.342573694 0.182479119
## [4,] 0.647945742 0.904584833 0.667130331 0.005445669 0.015891502
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 8.569313e-05 4.855906e-01 8.400311e-01 9.263997e-01 2.345266e-01
## [2,] 7.547186e-02 4.734869e-01 6.360452e-01 4.145937e-01 1.576347e-01
## [3,] 1.769689e-01 8.568366e-01 3.308296e-01 1.562257e-01 7.975491e-01
## [4,] 1.351393e-01 5.950872e-01 1.970485e-01 3.216193e-01 1.777045e-01
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.530985123 0.018359355 0.028884355 0.199129277 0.658995501
## [2,] 0.140061961 0.013845055 0.765791761 0.667683720 0.023367916
## [3,] 0.791195125 0.852116329 0.862432931 0.191061172 0.456443851
## [4,] 0.970128816 0.255228846 0.034772700 0.384844401 0.003349875
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 2.339527 1.072268 2.977378
## [2,] 2.192993 3.082021 1.681465
## [3,] 2.754282 2.733136 2.160196
## [4,] 1.578189 2.568755 2.320822
## [5,] 1.465182 2.195918 1.698135
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 2.339527 1.072268 2.977378
## [2,] 2.192993 3.082021 1.681465
## [3,] 2.754282 2.733136 2.160196
## [4,] 1.578189 2.568755 2.320822
## [5,] 1.465182 2.195918 1.698135
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.006459624 0.000863152 0.01014105 0.003485748 0.005750127
## [2,] 0.184827522 0.163625396 0.03367578 0.066200208 0.043715568
## [3,] 0.003079762 0.008016389 0.23424166 0.091845180 0.048923276
## [4,] 0.061397723 0.085716204 0.06321561 0.000516018 0.001505839
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.542304e-06 0.01440628 0.02492165 0.0274840 0.006957827
## [2,] 5.132069e-02 0.32196999 0.43250927 0.2819227 0.107191197
## [3,] 9.139535e-02 0.44251218 0.17085657 0.0806826 0.411893237
## [4,] 8.300086e-02 0.36549505 0.12102469 0.1975345 0.109143872
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.06971904 0.002410607 0.003792553 0.02614593 0.0865269668
## [2,] 0.08008420 0.007916283 0.437862071 0.38176616 0.0133612357
## [3,] 0.29401576 0.316654664 0.320488413 0.07100018 0.1696189469
## [4,] 0.08482115 0.022315392 0.003040277 0.03364805 0.0002928892
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.006459624 0.000863152 0.010141053 0.003485748 0.005750127
## [2,] 0.184827522 0.163625396 0.033675779 0.066200208 0.043715568
## [3,] 0.003079762 0.008016389 0.234241665 0.091845180 0.048923276
## [4,] 0.061397723 0.085716204 0.063215607 0.000516018 0.001505839
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.542304e-06 1.440628e-02 2.492165e-02 2.748400e-02 6.957827e-03
## [2,] 5.132069e-02 3.219700e-01 4.325093e-01 2.819227e-01 1.071912e-01
## [3,] 9.139535e-02 4.425122e-01 1.708566e-01 8.068260e-02 4.118932e-01
## [4,] 8.300086e-02 3.654950e-01 1.210247e-01 1.975345e-01 1.091439e-01
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0697190375 0.0024106071 0.0037925535 0.0261459331 0.0865269668
## [2,] 0.0800841997 0.0079162835 0.4378620710 0.3817661554 0.0133612357
## [3,] 0.2940157556 0.3166546640 0.3204884132 0.0710001781 0.1696189469
## [4,] 0.0848211522 0.0223153920 0.0030402772 0.0336480527 0.0002928892
einsum
By using einsum
and other DelayedTensor functions,
it is possible to implement your original tensor calculation functions.
It is intended to be applied to Delayed Arrays,
which can scale to large-scale data
since the calculation is performed internally by block processing.
For example, kronecker
can be easily implmented by eimsum
and other DelayedTensor functions4 https://stackoverflow.com/
questions/56067643/speeding-up-kronecker-products-numpy
(the kronecker
function inside DelayedTensor
has a more efficient implementation though).
darr1 <- DelayedArray(array(1:6, dim=c(2,3)))
darr2 <- DelayedArray(array(20:1, dim=c(4,5)))
mykronecker <- function(darr1, darr2){
stopifnot((length(dim(darr1)) == 2) && (length(dim(darr2)) == 2))
# Outer Product
tmpdarr <- DelayedTensor::einsum('ij,kl->ikjl', darr1, darr2)
# Reshape
DelayedTensor::unfold(tmpdarr, row_idx=c(2,1), col_idx=c(4,3))
}
identical(as.array(DelayedTensor::kronecker(darr1, darr2)),
as.array(mykronecker(darr1, darr2)))
## [1] TRUE
## R version 4.3.1 (2023-06-16 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## 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.10.0
## [3] HDF5Array_1.30.0 rhdf5_2.46.0
## [5] DelayedArray_0.28.0 SparseArray_1.2.0
## [7] S4Arrays_1.2.0 abind_1.4-5
## [9] IRanges_2.36.0 S4Vectors_0.40.0
## [11] MatrixGenerics_1.14.0 matrixStats_1.0.0
## [13] BiocGenerics_0.48.0 Matrix_1.6-1.1
## [15] DelayedTensor_1.8.0 BiocStyle_2.30.0
##
## loaded via a namespace (and not attached):
## [1] jsonlite_1.8.7 compiler_4.3.1 BiocManager_1.30.22
## [4] crayon_1.5.2 rsvd_1.0.5 Rcpp_1.0.11
## [7] rhdf5filters_1.14.0 parallel_4.3.1 jquerylib_0.1.4
## [10] BiocParallel_1.36.0 yaml_2.3.7 fastmap_1.1.1
## [13] lattice_0.22-5 R6_2.5.1 XVector_0.42.0
## [16] ScaledMatrix_1.10.0 knitr_1.44 bookdown_0.36
## [19] bslib_0.5.1 rlang_1.1.1 cachem_1.0.8
## [22] xfun_0.40 sass_0.4.7 cli_3.6.1
## [25] Rhdf5lib_1.24.0 BiocSingular_1.18.0 zlibbioc_1.48.0
## [28] digest_0.6.33 grid_4.3.1 irlba_2.3.5.1
## [31] rTensor_1.4.8 dqrng_0.3.1 evaluate_0.22
## [34] codetools_0.2-19 beachmat_2.18.0 rmarkdown_2.25
## [37] tools_4.3.1 htmltools_0.5.6.1