DelayedTensor 1.18.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2026-04-28 14:31:28
Compiled: Tue Apr 28 17:56:29 2026
einsumeinsum is an easy and intuitive way to write tensor operations.
It was originally introduced by
Numpy1 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)
DelayedTensorCRAN 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.41205923 0.08283173 0.30650958
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.41205923 0.08283173 0.30650958
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.4199014 0.8892840 0.33684157 0.9239852
## [2,] 0.2761462 0.6809934 0.07693558 0.2213167
## [3,] 0.2310916 0.3313412 0.42910133 0.7207897
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.41990136 0.88928401 0.33684157 0.92398521
## [2,] 0.27614617 0.68099342 0.07693558 0.22131673
## [3,] 0.23109158 0.33134120 0.42910133 0.72078972
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1969544 0.8613597 0.3487553 0.7375829
## [2,] 0.2058898 0.6451516 0.6367337 0.4958927
## [3,] 0.1239491 0.7191518 0.1063021 0.1224581
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04729769 0.6451780 0.4677087 0.8338458
## [2,] 0.57743008 0.8691304 0.7161495 0.1800344
## [3,] 0.52039338 0.1583651 0.4140679 0.1424737
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1331147 0.8084967 0.4659798 0.7479492
## [2,] 0.6714090 0.5090942 0.1736603 0.5215606
## [3,] 0.5117298 0.8490083 0.5152887 0.3822948
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.84136939 0.01804796 0.7128630 0.8180497
## [2,] 0.07945046 0.74717928 0.8489940 0.2543922
## [3,] 0.92252906 0.92900915 0.7802755 0.8835008
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04663877 0.7729524 0.6591794 0.90993336
## [2,] 0.72988543 0.9055070 0.2213268 0.34580048
## [3,] 0.60044915 0.5970091 0.1810731 0.02368097
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.1969544 0.8613597 0.3487553 0.7375829
## [2,] 0.2058898 0.6451516 0.6367337 0.4958927
## [3,] 0.1239491 0.7191518 0.1063021 0.1224581
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.04729769 0.64517799 0.46770870 0.83384581
## [2,] 0.57743008 0.86913037 0.71614945 0.18003441
## [3,] 0.52039338 0.15836513 0.41406785 0.14247366
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.1331147 0.8084967 0.4659798 0.7479492
## [2,] 0.6714090 0.5090942 0.1736603 0.5215606
## [3,] 0.5117298 0.8490083 0.5152887 0.3822948
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.84136939 0.01804796 0.71286299 0.81804972
## [2,] 0.07945046 0.74717928 0.84899399 0.25439218
## [3,] 0.92252906 0.92900915 0.78027551 0.88350082
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.04663877 0.77295242 0.65917943 0.90993336
## [2,] 0.72988543 0.90550696 0.22132679 0.34580048
## [3,] 0.60044915 0.59700908 0.18107310 0.02368097
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.2780886 0.1485293 0.5423122
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.2780886 0.1485293 0.5423122
einsum::einsum('iii->i', arrD)
## [1] 0.33563440 0.01819514 0.61095558
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.33563440 0.01819514 0.61095558
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.169792805 0.006861095 0.093948123
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.169792805 0.006861095 0.093948123
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.17631715 0.790826 0.113462241 0.85374867
## [2,] 0.07625671 0.463752 0.005919083 0.04898109
## [3,] 0.05340332 0.109787 0.184127950 0.51953782
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.176317148 0.790826048 0.113462241 0.853748667
## [2,] 0.076256708 0.463752041 0.005919083 0.048981094
## [3,] 0.053403320 0.109786988 0.184127950 0.519537817
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03879105 0.7419405 0.12163027 0.54402854
## [2,] 0.04239062 0.4162206 0.40542976 0.24590960
## [3,] 0.01536339 0.5171793 0.01130013 0.01499599
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.002237072 0.41625464 0.2187514 0.69529883
## [2,] 0.333425501 0.75538760 0.5128700 0.03241239
## [3,] 0.270809265 0.02507951 0.1714522 0.02029874
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01771952 0.6536669 0.21713716 0.5594281
## [2,] 0.45079011 0.2591769 0.03015791 0.2720255
## [3,] 0.26186740 0.7208151 0.26552248 0.1461493
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.707902447 0.0003257289 0.5081736 0.66920534
## [2,] 0.006312376 0.5582768720 0.7207908 0.06471538
## [3,] 0.851059863 0.8630579978 0.6088299 0.78057370
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.002175175 0.5974555 0.43451752 0.8279787219
## [2,] 0.532732745 0.8199428 0.04898555 0.1195779717
## [3,] 0.360539178 0.3564198 0.03278747 0.0005607883
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.03879105 0.74194054 0.12163027 0.54402854
## [2,] 0.04239062 0.41622065 0.40542976 0.24590960
## [3,] 0.01536339 0.51717931 0.01130013 0.01499599
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.002237072 0.416254643 0.218751427 0.695298830
## [2,] 0.333425501 0.755387599 0.512870039 0.032412390
## [3,] 0.270809265 0.025079514 0.171452187 0.020298743
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.01771952 0.65366693 0.21713716 0.55942807
## [2,] 0.45079011 0.25917687 0.03015791 0.27202548
## [3,] 0.26186740 0.72081506 0.26552248 0.14614932
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.7079024469 0.0003257289 0.5081736443 0.6692053433
## [2,] 0.0063123760 0.5582768720 0.7207907913 0.0647153792
## [3,] 0.8510598630 0.8630579978 0.6088298718 0.7805737006
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.0021751746 0.5974554503 0.4345175228 0.8279787219
## [2,] 0.5327327448 0.8199428477 0.0489855462 0.1195779717
## [3,] 0.3605391778 0.3564198430 0.0327874693 0.0005607883
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.16979281 0.034131578 0.12630010
## [2,] 0.03413158 0.006861095 0.02538872
## [3,] 0.12630010 0.025388718 0.09394812
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.169792805 0.034131578 0.126300100
## [2,] 0.034131578 0.006861095 0.025388718
## [3,] 0.126300100 0.025388718 0.093948123
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08270143 0.17514843 0.06634244 0.18198298
## [2,] 0.05438821 0.13412467 0.01515280 0.04358931
## [3,] 0.04551451 0.06525912 0.08451341 0.14196273
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08645342 0.18309454 0.06935225 0.19023916
## [2,] 0.05685569 0.14020962 0.01584025 0.04556686
## [3,] 0.04757941 0.06821978 0.08834760 0.14840328
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05204641 0.11022598 0.041751218 0.11452716
## [2,] 0.03422808 0.08440854 0.009536098 0.02743201
## [3,] 0.02864360 0.04106945 0.053186734 0.08934126
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3616861 0.7659934 0.29014175 0.7958836
## [2,] 0.2378612 0.5865803 0.06626921 0.1906333
## [3,] 0.1990530 0.2854040 0.36961059 0.6208592
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2709001 0.5737230 0.21731389 0.5961106
## [2,] 0.1781562 0.4393440 0.04963511 0.1427829
## [3,] 0.1490891 0.2137653 0.27683543 0.4650187
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3019728 0.6395302 0.24224022 0.6644856
## [2,] 0.1985910 0.4897376 0.05532836 0.1591603
## [3,] 0.1661899 0.2382846 0.30858899 0.5183572
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.14644283 0.3101425 0.11747529 0.32224476
## [2,] 0.09630745 0.2375001 0.02683169 0.07718539
## [3,] 0.08059442 0.1155570 0.14965137 0.25137925
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2673653 0.5662371 0.21447837 0.5883325
## [2,] 0.1758316 0.4336114 0.04898747 0.1409198
## [3,] 0.1471438 0.2109761 0.27322326 0.4589511
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04463638 0.09453273 0.035806956 0.09822154
## [2,] 0.02935491 0.07239101 0.008178411 0.02352643
## [3,] 0.02456551 0.03522226 0.045614360 0.07662144
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3097121 0.6559207 0.24844858 0.6815157
## [2,] 0.2036807 0.5022891 0.05674637 0.1632394
## [3,] 0.1704492 0.2443916 0.31649780 0.5316422
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2082260 0.4409895 0.16703728 0.4581975
## [2,] 0.1369389 0.3376997 0.03815179 0.1097494
## [3,] 0.1145966 0.1643097 0.21278823 0.3574344
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05142033 0.10890005 0.041248986 0.11314949
## [2,] 0.03381634 0.08339318 0.009421386 0.02710203
## [3,] 0.02829904 0.04057542 0.052546943 0.08826656
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01986037 0.04206108 0.015931830 0.04370237
## [2,] 0.01306108 0.03220942 0.003638875 0.01046777
## [3,] 0.01093010 0.01567167 0.020295504 0.03409169
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2424637 0.5134993 0.19450245 0.5335369
## [2,] 0.1594551 0.3932261 0.04442492 0.1277949
## [3,] 0.1334392 0.1913264 0.24777602 0.4162057
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2185139 0.4627775 0.17529012 0.4808358
## [2,] 0.1437046 0.3543845 0.04003676 0.1151718
## [3,] 0.1202585 0.1724278 0.22330149 0.3750942
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2709111 0.5737465 0.21732277 0.5961349
## [2,] 0.1781634 0.4393620 0.04963714 0.1427887
## [3,] 0.1490952 0.2137740 0.27684673 0.4650377
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3649490 0.7729037 0.29275923 0.8030636
## [2,] 0.2400070 0.5918721 0.06686705 0.1923531
## [3,] 0.2008487 0.2879787 0.37294500 0.6264602
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06649773 0.14083158 0.05334396 0.14632704
## [2,] 0.04373192 0.10784561 0.01218391 0.03504885
## [3,] 0.03659685 0.05247289 0.06795469 0.11414796
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1963915 0.4159259 0.15754373 0.4321559
## [2,] 0.1291560 0.3185065 0.03598344 0.1035118
## [3,] 0.1080835 0.1549712 0.20069442 0.3371196
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3007121 0.6368603 0.24122890 0.6617115
## [2,] 0.1977619 0.4876931 0.05509737 0.1584959
## [3,] 0.1654961 0.2372898 0.30730068 0.5161932
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1738677 0.3682239 0.13947526 0.38259257
## [2,] 0.1143433 0.2819775 0.03185655 0.09164014
## [3,] 0.0956876 0.1371977 0.17767707 0.29845585
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3501330 0.7415257 0.28087393 0.7704612
## [2,] 0.2302633 0.5678435 0.06415241 0.1845440
## [3,] 0.1926947 0.2762875 0.35780434 0.6010275
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07559669 0.16010173 0.06064307 0.16634914
## [2,] 0.04971581 0.12260225 0.01385105 0.03984463
## [3,] 0.04160444 0.05965282 0.07725301 0.12976695
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05982488 0.12669955 0.04799105 0.1316436
## [2,] 0.03934356 0.09702362 0.01096129 0.0315318
## [3,] 0.03292446 0.04720739 0.06113564 0.1026935
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05589503 0.11837675 0.04483856 0.12299599
## [2,] 0.03675911 0.09065022 0.01024125 0.02946050
## [3,] 0.03076168 0.04410638 0.05711968 0.09594769
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2819256 0.5970733 0.22615848 0.6203720
## [2,] 0.1854070 0.4572251 0.05165524 0.1485941
## [3,] 0.1551570 0.2224655 0.28810251 0.4839447
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2148760 0.4550731 0.17237187 0.4728308
## [2,] 0.1413122 0.3484846 0.03937023 0.1132544
## [3,] 0.1182565 0.1695572 0.21958394 0.3688496
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3394889 0.7189832 0.27233530 0.7470390
## [2,] 0.2232633 0.5505809 0.06220216 0.1789338
## [3,] 0.1868368 0.2678883 0.34692701 0.5827561
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2137693 0.4527293 0.17148408 0.4703955
## [2,] 0.1405844 0.3466898 0.03916745 0.1126711
## [3,] 0.1176474 0.1686839 0.21845298 0.3669498
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3564997 0.7550095 0.28598128 0.7844711
## [2,] 0.2344504 0.5781691 0.06531894 0.1878997
## [3,] 0.1961987 0.2813114 0.36431058 0.6119564
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1956655 0.4143884 0.15696136 0.4305584
## [2,] 0.1286785 0.3173292 0.03585042 0.1031291
## [3,] 0.1076840 0.1543983 0.19995255 0.3358734
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07292020 0.15443335 0.05849601 0.16045957
## [2,] 0.04795563 0.11826154 0.01336066 0.03843393
## [3,] 0.04013144 0.05754082 0.07451787 0.12517257
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2163704 0.4582380 0.17357066 0.4761192
## [2,] 0.1422950 0.3509082 0.03964404 0.1140420
## [3,] 0.1190789 0.1707364 0.22111108 0.3714148
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3140649 0.6651393 0.25194039 0.6910940
## [2,] 0.2065433 0.5093485 0.05754391 0.1655337
## [3,] 0.1728448 0.2478264 0.32094601 0.5391141
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2190040 0.4638155 0.17568330 0.4819143
## [2,] 0.1440270 0.3551794 0.04012657 0.1154301
## [3,] 0.1205283 0.1728145 0.22380235 0.3759355
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.16052611 0.3399687 0.12877278 0.35323475
## [2,] 0.10556925 0.2603402 0.02941207 0.08460824
## [3,] 0.08834511 0.1266700 0.16404321 0.27555417
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3532921 0.7482163 0.28340818 0.7774129
## [2,] 0.2323409 0.5729670 0.06473124 0.1862091
## [3,] 0.1944334 0.2787803 0.36103272 0.6064504
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03336136 0.07065403 0.026762218 0.07341105
## [2,] 0.02193994 0.05410524 0.006112567 0.01758372
## [3,] 0.01836033 0.02632521 0.034092299 0.05726708
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3873712 0.8203903 0.31074613 0.8524032
## [2,] 0.2547529 0.6282362 0.07097531 0.2041711
## [3,] 0.2131887 0.3056719 0.39585844 0.6649495
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.007578363 0.016049762 0.006079303 0.016676048
## [2,] 0.004983875 0.012290542 0.001388530 0.003994316
## [3,] 0.004170732 0.005980033 0.007744404 0.013008784
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3137416 0.6644546 0.25168104 0.6903826
## [2,] 0.2063307 0.5088242 0.05748467 0.1653633
## [3,] 0.1726668 0.2475713 0.32061562 0.5385591
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3900922 0.8261530 0.31292890 0.8583907
## [2,] 0.2565423 0.6326491 0.07147386 0.2056053
## [3,] 0.2146862 0.3078190 0.39863906 0.6696202
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2993321 0.6339377 0.24012189 0.6586749
## [2,] 0.1968544 0.4854550 0.05484453 0.1577685
## [3,] 0.1647366 0.2362009 0.30589046 0.5138243
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3564937 0.7549968 0.28597646 0.7844579
## [2,] 0.2344464 0.5781593 0.06531784 0.1878966
## [3,] 0.1961954 0.2813067 0.36430445 0.6119461
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3276387 0.6938865 0.26282922 0.7209630
## [2,] 0.2154701 0.5313625 0.06003095 0.1726880
## [3,] 0.1803151 0.2585374 0.33481726 0.5624146
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3435002 0.7274785 0.27555315 0.7558658
## [2,] 0.2259013 0.5570865 0.06293713 0.1810481
## [3,] 0.1890444 0.2710536 0.35102622 0.5896418
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.10681962 0.22622689 0.08568986 0.23505461
## [2,] 0.07024943 0.17323940 0.01957181 0.05630124
## [3,] 0.05878789 0.08429061 0.10916002 0.18336326
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3709832 0.7856832 0.29759980 0.8163417
## [2,] 0.2439754 0.6016582 0.06797265 0.1955335
## [3,] 0.2041696 0.2927402 0.37911138 0.6368183
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01958368 0.04147511 0.01570988 0.04309353
## [2,] 0.01287912 0.03176069 0.00358818 0.01032194
## [3,] 0.01077783 0.01545334 0.02001276 0.03361674
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3064799 0.6490754 0.24585575 0.6744033
## [2,] 0.2015551 0.4970472 0.05615416 0.1615359
## [3,] 0.1686704 0.2418411 0.31319481 0.5260939
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2521294 0.5339698 0.2022562 0.5548061
## [2,] 0.1658117 0.4089019 0.0461959 0.1328894
## [3,] 0.1387587 0.1989535 0.2576535 0.4327976
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3245638 0.6873742 0.26036251 0.7141966
## [2,] 0.2134479 0.5263755 0.05946754 0.1710673
## [3,] 0.1786228 0.2561110 0.33167491 0.5571362
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3802236 0.8052529 0.3050124 0.8366750
## [2,] 0.2500523 0.6166443 0.0696657 0.2004038
## [3,] 0.2092550 0.3000318 0.3885542 0.6526801
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2506849 0.5309106 0.20109747 0.5516276
## [2,] 0.1648618 0.4065593 0.04593124 0.1321281
## [3,] 0.1379638 0.1978137 0.25617739 0.4303180
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2767903 0.5861977 0.22203903 0.6090720
## [2,] 0.1820299 0.4488969 0.05071435 0.1458874
## [3,] 0.1523308 0.2184133 0.28285477 0.4751298
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.09293542 0.19682237 0.07455206 0.20450268
## [2,] 0.06111854 0.15072209 0.01702790 0.04898332
## [3,] 0.05114676 0.07333468 0.09497162 0.15953007
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07603284 0.16102542 0.06099295 0.16730887
## [2,] 0.05000264 0.12330959 0.01393096 0.04007451
## [3,] 0.04184447 0.05999698 0.07769871 0.13051563
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3820823 0.8091892 0.30650338 0.8407650
## [2,] 0.2512746 0.6196586 0.07000625 0.2013835
## [3,] 0.2102779 0.3014984 0.39045361 0.6558706
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.14520209 0.3075148 0.11647998 0.31951453
## [2,] 0.09549148 0.2354879 0.02660436 0.07653143
## [3,] 0.07991158 0.1145779 0.14838345 0.24924943
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.009943671 0.02105911 0.007976735 0.021880865
## [2,] 0.006539409 0.01612658 0.001821909 0.005240994
## [3,] 0.005472472 0.00784648 0.010161535 0.017068999
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.08270143 0.17514843 0.06634244 0.18198298
## [2,] 0.05438821 0.13412467 0.01515280 0.04358931
## [3,] 0.04551451 0.06525912 0.08451341 0.14196273
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.08645342 0.18309454 0.06935225 0.19023916
## [2,] 0.05685569 0.14020962 0.01584025 0.04556686
## [3,] 0.04757941 0.06821978 0.08834760 0.14840328
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.052046406 0.110225975 0.041751218 0.114527159
## [2,] 0.034228076 0.084408539 0.009536098 0.027432015
## [3,] 0.028643600 0.041069452 0.053186734 0.089341255
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.38208225 0.80918919 0.30650338 0.84076497
## [2,] 0.25127461 0.61965863 0.07000625 0.20138347
## [3,] 0.21027794 0.30149841 0.39045361 0.65587061
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.14520209 0.30751484 0.11647998 0.31951453
## [2,] 0.09549148 0.23548785 0.02660436 0.07653143
## [3,] 0.07991158 0.11457794 0.14838345 0.24924943
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.009943671 0.021059107 0.007976735 0.021880865
## [2,] 0.006539409 0.016126584 0.001821909 0.005240994
## [3,] 0.005472472 0.007846480 0.010161535 0.017068999
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] 0.8014005
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 0.8014005
einsum::einsum('ij->', arrC)
## [1] 5.537728
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 5.537728
einsum::einsum('ijk->', arrE)
## [1] 30.89094
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 30.89094
einsum::einsum('ij->i', arrC)
## [1] 2.570012 1.255392 1.712324
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 2.570012 1.255392 1.712324
einsum::einsum('ij->j', arrC)
## [1] 0.9271391 1.9016186 0.8428785 1.8660917
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 0.9271391 1.9016186 0.8428785 1.8660917
einsum::einsum('ijk->i', arrE)
## [1] 11.07326 10.33467 9.48301
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 11.07326 10.33467 9.48301
einsum::einsum('ijk->j', arrE)
## [1] 6.208490 10.034641 7.248358 7.399450
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 6.208490 10.034641 7.248358 7.399450
einsum::einsum('ijk->k', arrE)
## [1] 5.200181 5.572075 6.289586 7.835660 5.993436
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 5.200181 5.572075 6.289586 7.835660 5.993436
These are the same as what the modeSum function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 1.265375 3.106035 2.654486 4.047361
## [2,] 2.264065 3.676062 2.596864 1.797680
## [3,] 2.679051 3.252543 1.997007 1.554408
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 1.265375 3.106035 2.654486 4.047361
## [2,] 2.264065 3.676062 2.596864 1.797680
## [3,] 2.679051 3.252543 1.997007 1.554408
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.5267934 1.145121 1.316254 1.843349 1.376973
## [2,] 2.2256631 1.672673 2.166599 1.694236 2.275468
## [3,] 1.0917911 1.597926 1.154929 2.342132 1.061579
## [4,] 1.3559338 1.156354 1.651805 1.955943 1.279415
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.5267934 1.1451212 1.3162535 1.8433489 1.3769733
## [2,] 2.2256631 1.6726735 2.1665992 1.6942364 2.2754685
## [3,] 1.0917911 1.5979260 1.1549288 2.3421325 1.0615793
## [4,] 1.3559338 1.1563539 1.6518047 1.9559427 1.2794148
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.5267934 1.145121 1.316254 1.843349 1.376973
## [2,] 2.2256631 1.672673 2.166599 1.694236 2.275468
## [3,] 1.0917911 1.597926 1.154929 2.342132 1.061579
## [4,] 1.3559338 1.156354 1.651805 1.955943 1.279415
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.5267934 1.1451212 1.3162535 1.8433489 1.3769733
## [2,] 2.2256631 1.6726735 2.1665992 1.6942364 2.2754685
## [3,] 1.0917911 1.5979260 1.1549288 2.3421325 1.0615793
## [4,] 1.3559338 1.1563539 1.6518047 1.9559427 1.2794148
If we take the diagonal elements of a matrix
and add them together, we get trace.
einsum::einsum('ii->', arrB)
## [1] 0.9689301
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 0.9689301
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.27808856 0.1077071 0.4830146
## [2,] 0.04833897 0.1485293 0.5019796
## [3,] 0.83675710 0.1014274 0.5423122
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.27808856 0.10770711 0.48301457
## [2,] 0.04833897 0.14852929 0.50197955
## [3,] 0.83675710 0.10142739 0.54231224
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.3356344 0.08375032 0.4629006
## [2,] 0.6936681 0.15556442 0.7184491
## [3,] 0.9405601 0.60232408 0.2775512
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.6492659 0.27024349 0.04682790
## [2,] 0.4133627 0.01819514 0.82669969
## [3,] 0.1617372 0.95757734 0.09802796
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.51231469 0.02371261 0.6957418
## [2,] 0.25941651 0.95815004 0.8409130
## [3,] 0.05062554 0.83452857 0.6109556
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.33563440 0.08375032 0.46290057
## [2,] 0.69366814 0.15556442 0.71844912
## [3,] 0.94056005 0.60232408 0.27755115
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.64926591 0.27024349 0.04682790
## [2,] 0.41336272 0.01819514 0.82669969
## [3,] 0.16173716 0.95757734 0.09802796
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.51231469 0.02371261 0.69574183
## [2,] 0.25941651 0.95815004 0.84091300
## [3,] 0.05062554 0.83452857 0.61095558
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.270602
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 0.270602
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 3.396119
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 3.396119
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 20.89681
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 20.89681
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.09654506 0.6064718 0.7303770 1.565275 0.8954471
## [2,] 1.67534050 1.1967218 1.6336589 1.421661 1.7738181
## [3,] 0.53836017 0.9030737 0.5128175 1.837794 0.5162905
## [4,] 0.80493413 0.7480100 0.9776029 1.514494 0.9481175
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.09654506 0.60647184 0.73037703 1.56527469 0.89544710
## [2,] 1.67534050 1.19672176 1.63365886 1.42166060 1.77381814
## [3,] 0.53836017 0.90307365 0.51281755 1.83779431 0.51629054
## [4,] 0.80493413 0.74800996 0.97760287 1.51449442 0.94811748
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 1.9343541 0.9519592 1.2022303
## [2,] 0.9519592 0.5949089 0.4819922
## [3,] 1.2022303 0.4819922 0.8668561
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 1.9343541 0.9519592 1.2022303
## [2,] 0.9519592 0.5949089 0.4819922
## [3,] 1.2022303 0.4819922 0.8668561
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.1763171 0.076256708 0.05340332
## [2,] 0.7908260 0.463752041 0.10978699
## [3,] 0.1134622 0.005919083 0.18412795
## [4,] 0.8537487 0.048981094 0.51953782
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.176317148 0.076256708 0.053403320
## [2,] 0.790826048 0.463752041 0.109786988
## [3,] 0.113462241 0.005919083 0.184127950
## [4,] 0.853748667 0.048981094 0.519537817
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.03879105 0.002237072 0.01771952 0.7079024469 0.002175175
## [2,] 0.74194054 0.416254643 0.65366693 0.0003257289 0.597455450
## [3,] 0.12163027 0.218751427 0.21713716 0.5081736443 0.434517523
## [4,] 0.54402854 0.695298830 0.55942807 0.6692053433 0.827978722
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.04239062 0.33342550 0.45079011 0.006312376 0.53273274
## [2,] 0.41622065 0.75538760 0.25917687 0.558276872 0.81994285
## [3,] 0.40542976 0.51287004 0.03015791 0.720790791 0.04898555
## [4,] 0.24590960 0.03241239 0.27202548 0.064715379 0.11957797
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.01536339 0.27080927 0.2618674 0.8510599 0.3605391778
## [2,] 0.51717931 0.02507951 0.7208151 0.8630580 0.3564198430
## [3,] 0.01130013 0.17145219 0.2655225 0.6088299 0.0327874693
## [4,] 0.01499599 0.02029874 0.1461493 0.7805737 0.0005607883
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.0387910493 0.0022370719 0.0177195163 0.7079024469 0.0021751746
## [2,] 0.7419405406 0.4162546432 0.6536669296 0.0003257289 0.5974554503
## [3,] 0.1216302734 0.2187514273 0.2171371628 0.5081736443 0.4345175228
## [4,] 0.5440285376 0.6952988303 0.5594280698 0.6692053433 0.8279787219
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.042390624 0.333425501 0.450790111 0.006312376 0.532732745
## [2,] 0.416220648 0.755387599 0.259176872 0.558276872 0.819942848
## [3,] 0.405429763 0.512870039 0.030157907 0.720790791 0.048985546
## [4,] 0.245909601 0.032412390 0.272025478 0.064715379 0.119577972
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0153633857 0.2708092651 0.2618674038 0.8510598630 0.3605391778
## [2,] 0.5171793075 0.0250795135 0.7208150628 0.8630579978 0.3564198430
## [3,] 0.0113001303 0.1714521865 0.2655224776 0.6088298718 0.0327874693
## [4,] 0.0149959920 0.0202987434 0.1461493188 0.7805737006 0.0005607883
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 2.144652 1.983668 1.071861
## [2,] 1.994030 2.342744 1.235300
## [3,] 2.155540 1.875724 2.258322
## [4,] 2.390330 1.930016 3.515315
## [5,] 2.388704 2.202520 1.402212
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 2.144652 1.983668 1.071861
## [2,] 1.994030 2.342744 1.235300
## [3,] 2.155540 1.875724 2.258322
## [4,] 2.390330 1.930016 3.515315
## [5,] 2.388704 2.202520 1.402212
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.00281829 0.0001625302 0.001287378 0.0514313127 0.0001580332
## [2,] 0.24177406 0.1356437220 0.213008592 0.0001061443 0.1946911160
## [3,] 0.00568660 0.0102273211 0.010151849 0.0237587252 0.0203150686
## [4,] 0.19138653 0.2446026620 0.196804006 0.2354231034 0.2912787864
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0002677593 0.0021060738 2.847404e-03 3.987196e-05 3.364993e-03
## [2,] 0.0159884431 0.0290169930 9.955860e-03 2.144530e-02 3.149678e-02
## [3,] 0.0001987773 0.0002514540 1.478606e-05 3.533950e-04 2.401702e-05
## [4,] 0.0009977016 0.0001315032 1.103659e-03 2.625625e-04 4.851504e-04
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0002514776 0.0044327764 0.00428641 0.01393068 5.901532e-03
## [2,] 0.0174034786 0.0008439448 0.02425598 0.02904256 1.199380e-02
## [3,] 0.0006377452 0.0096762437 0.01498529 0.03436052 1.850426e-03
## [4,] 0.0023880115 0.0032324392 0.02327330 0.12430115 8.930178e-05
DelayedTensor::einsum('i,ij,ijk,ijk,ji->jki',
darrA, darrC, darrE, darrE, t(darrC))
## <4 x 5 x 3> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0028182903 0.0001625302 0.0012873779 0.0514313127 0.0001580332
## [2,] 0.2417740632 0.1356437220 0.2130085915 0.0001061443 0.1946911160
## [3,] 0.0056866000 0.0102273211 0.0101518492 0.0237587252 0.0203150686
## [4,] 0.1913865272 0.2446026620 0.1968040059 0.2354231034 0.2912787864
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.677593e-04 2.106074e-03 2.847404e-03 3.987196e-05 3.364993e-03
## [2,] 1.598844e-02 2.901699e-02 9.955860e-03 2.144530e-02 3.149678e-02
## [3,] 1.987773e-04 2.514540e-04 1.478606e-05 3.533950e-04 2.401702e-05
## [4,] 9.977016e-04 1.315032e-04 1.103659e-03 2.625625e-04 4.851504e-04
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.514776e-04 4.432776e-03 4.286410e-03 1.393068e-02 5.901532e-03
## [2,] 1.740348e-02 8.439448e-04 2.425598e-02 2.904256e-02 1.199380e-02
## [3,] 6.377452e-04 9.676244e-03 1.498529e-02 3.436052e-02 1.850426e-03
## [4,] 2.388012e-03 3.232439e-03 2.327330e-02 1.243011e-01 8.930178e-05
einsumBy 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.6.0 Patched (2026-04-24 r89963)
## Platform: aarch64-apple-darwin23
## Running under: macOS Tahoe 26.3.1
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.6/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.6/Resources/lib/libRlapack.dylib; LAPACK version 3.12.1
##
## locale:
## [1] C/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.20.0
## [3] HDF5Array_1.40.0 h5mread_1.4.0
## [5] rhdf5_2.56.0 DelayedArray_0.38.0
## [7] SparseArray_1.12.0 S4Arrays_1.12.0
## [9] abind_1.4-8 IRanges_2.46.0
## [11] S4Vectors_0.50.0 MatrixGenerics_1.24.0
## [13] matrixStats_1.5.0 BiocGenerics_0.58.0
## [15] generics_0.1.4 Matrix_1.7-5
## [17] DelayedTensor_1.18.0 BiocStyle_2.40.0
##
## loaded via a namespace (and not attached):
## [1] dqrng_0.4.1 sass_0.4.10 lattice_0.22-9
## [4] digest_0.6.39 evaluate_1.0.5 grid_4.6.0
## [7] bookdown_0.46 fastmap_1.2.0 jsonlite_2.0.0
## [10] BiocManager_1.30.27 codetools_0.2-20 jquerylib_0.1.4
## [13] cli_3.6.6 rlang_1.2.0 XVector_0.52.0
## [16] cachem_1.1.0 yaml_2.3.12 otel_0.2.0
## [19] tools_4.6.0 beachmat_2.28.0 parallel_4.6.0
## [22] BiocParallel_1.46.0 Rhdf5lib_2.0.0 rsvd_1.0.5
## [25] R6_2.6.1 lifecycle_1.0.5 BiocSingular_1.28.0
## [28] irlba_2.3.7 ScaledMatrix_1.20.0 rTensor_1.4.9
## [31] bslib_0.10.0 Rcpp_1.1.1-1.1 xfun_0.57
## [34] knitr_1.51 rhdf5filters_1.24.0 htmltools_0.5.9
## [37] rmarkdown_2.31 compiler_4.6.0