DelayedTensor 1.21.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2026-11-23 21:32:27.369412
Compiled: Tue Nov 24 01:01:41 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.70518244 0.09280465 0.45824145
DelayedTensor::einsum('i->i', darrA)
## <3> DelayedArray object of type "double":
## [1] [2] [3]
## 0.70518244 0.09280465 0.45824145
einsum::einsum('ij->ij', arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.2622628 0.2405097 0.2782464 0.4087036
## [2,] 0.4128781 0.1321867 0.8419315 0.4455776
## [3,] 0.9914084 0.7944685 0.7618723 0.1681583
DelayedTensor::einsum('ij->ij', darrC)
## <3 x 4> DelayedArray object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.2622628 0.2405097 0.2782464 0.4087036
## [2,] 0.4128781 0.1321867 0.8419315 0.4455776
## [3,] 0.9914084 0.7944685 0.7618723 0.1681583
einsum::einsum('ijk->ijk', arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07193745 0.03074787 0.9792123 0.97603377
## [2,] 0.02780228 0.26173125 0.5585735 0.20961719
## [3,] 0.27358135 0.50104844 0.9951623 0.06345733
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.7524100 0.9345414 0.8038662 0.7129298
## [2,] 0.9472147 0.5262150 0.9822984 0.2695026
## [3,] 0.7671001 0.9505310 0.6283175 0.2661427
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4284605 0.41157507 0.5575005 0.74173407
## [2,] 0.7088492 0.55249142 0.5026559 0.88332368
## [3,] 0.1423224 0.07333634 0.8127428 0.07621954
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.3218976 0.2434711 0.7541245 0.4511468
## [2,] 0.2976283 0.9508347 0.5171637 0.8226423
## [3,] 0.5402423 0.9056491 0.9968223 0.7208066
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.4121726 0.58012097 0.12520674 0.4744350
## [2,] 0.0164026 0.08110761 0.83637045 0.2416060
## [3,] 0.5559338 0.16522187 0.06107807 0.7236102
DelayedTensor::einsum('ijk->ijk', darrE)
## <3 x 4 x 5> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.07193745 0.03074787 0.97921225 0.97603377
## [2,] 0.02780228 0.26173125 0.55857354 0.20961719
## [3,] 0.27358135 0.50104844 0.99516235 0.06345733
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.7524100 0.9345414 0.8038662 0.7129298
## [2,] 0.9472147 0.5262150 0.9822984 0.2695026
## [3,] 0.7671001 0.9505310 0.6283175 0.2661427
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.42846049 0.41157507 0.55750050 0.74173407
## [2,] 0.70884916 0.55249142 0.50265595 0.88332368
## [3,] 0.14232238 0.07333634 0.81274276 0.07621954
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.3218976 0.2434711 0.7541245 0.4511468
## [2,] 0.2976283 0.9508347 0.5171637 0.8226423
## [3,] 0.5402423 0.9056491 0.9968223 0.7208066
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.41217256 0.58012097 0.12520674 0.47443502
## [2,] 0.01640260 0.08110761 0.83637045 0.24160596
## [3,] 0.55593378 0.16522187 0.06107807 0.72361017
We can also extract the diagonal elements as follows.
einsum::einsum('ii->i', arrB)
## [1] 0.9150066 0.6449287 0.7199535
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.9150066 0.6449287 0.7199535
einsum::einsum('iii->i', arrD)
## [1] 0.8778472 0.6654186 0.9656412
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.8778472 0.6654186 0.9656412
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.497282269 0.008612704 0.209985229
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 0.497282269 0.008612704 0.209985229
einsum::einsum('ij,ij->ij', arrC, arrC)
## [,1] [,2] [,3] [,4]
## [1,] 0.0687818 0.05784489 0.07742108 0.16703867
## [2,] 0.1704684 0.01747333 0.70884860 0.19853942
## [3,] 0.9828907 0.63118027 0.58044937 0.02827723
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.06878180 0.05784489 0.07742108 0.16703867
## [2,] 0.17046835 0.01747333 0.70884860 0.19853942
## [3,] 0.98289071 0.63118027 0.58044937 0.02827723
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.0051749972 0.0009454317 0.9588566 0.952641911
## [2,] 0.0007729668 0.0685032493 0.3120044 0.043939367
## [3,] 0.0748467546 0.2510495438 0.9903481 0.004026832
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.5661208 0.8733676 0.6462009 0.50826890
## [2,] 0.8972158 0.2769023 0.9649102 0.07263164
## [3,] 0.5884425 0.9035091 0.3947829 0.07083195
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.18357839 0.169394035 0.3108068 0.550169436
## [2,] 0.50246714 0.305246766 0.2526630 0.780260718
## [3,] 0.02025566 0.005378219 0.6605508 0.005809418
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1036181 0.05927816 0.5687038 0.2035334
## [2,] 0.0885826 0.90408672 0.2674583 0.6767404
## [3,] 0.2918617 0.82020030 0.9936548 0.5195622
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1698862220 0.336540335 0.01567673 0.22508859
## [2,] 0.0002690452 0.006578444 0.69951552 0.05837344
## [3,] 0.3090623714 0.027298267 0.00373053 0.52361167
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
## [,1] [,2] [,3] [,4]
## [1,] 0.0051749972 0.0009454317 0.9588566362 0.9526419111
## [2,] 0.0007729668 0.0685032493 0.3120043994 0.0439393668
## [3,] 0.0748467546 0.2510495438 0.9903481003 0.0040268325
##
## ,,2
## [,1] [,2] [,3] [,4]
## [1,] 0.56612075 0.87336757 0.64620093 0.50826890
## [2,] 0.89721575 0.27690226 0.96491018 0.07263164
## [3,] 0.58844249 0.90350915 0.39478290 0.07083195
##
## ,,3
## [,1] [,2] [,3] [,4]
## [1,] 0.183578390 0.169394035 0.310806809 0.550169436
## [2,] 0.502467138 0.305246766 0.252663001 0.780260718
## [3,] 0.020255658 0.005378219 0.660550800 0.005809418
##
## ,,4
## [,1] [,2] [,3] [,4]
## [1,] 0.10361809 0.05927816 0.56870381 0.20353340
## [2,] 0.08858260 0.90408672 0.26745828 0.67674035
## [3,] 0.29186172 0.82020030 0.99365476 0.51956217
##
## ,,5
## [,1] [,2] [,3] [,4]
## [1,] 0.1698862220 0.3365403353 0.0156767277 0.2250885906
## [2,] 0.0002690452 0.0065784438 0.6995155230 0.0583734399
## [3,] 0.3090623714 0.0272982671 0.0037305302 0.5236116733
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.49728227 0.065444213 0.32314382
## [2,] 0.06544421 0.008612704 0.04252694
## [3,] 0.32314382 0.042526940 0.20998523
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.497282269 0.065444213 0.323143824
## [2,] 0.065444213 0.008612704 0.042526940
## [3,] 0.323143824 0.042526940 0.209985229
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01886652 0.017301652 0.02001634 0.02940110
## [2,] 0.02970140 0.009509175 0.06056641 0.03205372
## [3,] 0.07131940 0.057152044 0.05480715 0.01209688
##
## , , 2, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.007291505 0.006686717 0.007735885 0.011362893
## [2,] 0.011478953 0.003675092 0.023407614 0.012388074
## [3,] 0.027563415 0.022088037 0.021181786 0.004675185
##
## , , 3, 1, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07175022 0.06579896 0.07612303 0.11181369
## [2,] 0.11295576 0.03616382 0.23033675 0.12190173
## [3,] 0.27123086 0.21735178 0.20843405 0.04600499
##
## , , 1, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.008064025 0.00739516 0.008555486 0.012566768
## [2,] 0.012695124 0.00406446 0.025887602 0.013700564
## [3,] 0.030483701 0.02442822 0.023425952 0.005170511
##
## , , 2, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06864238 0.06294889 0.07282579 0.10697052
## [2,] 0.10806311 0.03459739 0.22035978 0.11662159
## [3,] 0.25948258 0.20793725 0.19940579 0.04401229
##
## , , 3, 2, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1314064 0.12050699 0.1394149 0.20478033
## [2,] 0.2068719 0.06623195 0.4218485 0.22325597
## [3,] 0.4967437 0.39806723 0.3817349 0.08425548
##
## , , 1, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2568110 0.2355100 0.2724623 0.4002076
## [2,] 0.4042953 0.1294388 0.8244296 0.4363151
## [3,] 0.9707993 0.7779533 0.7460347 0.1646627
##
## , , 2, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1464931 0.1343423 0.1554211 0.2282910
## [2,] 0.2306228 0.0738360 0.4702806 0.2488879
## [3,] 0.5537745 0.4437691 0.4255617 0.0939288
##
## , , 3, 3, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2609941 0.2393462 0.2769004 0.4067265
## [2,] 0.4108808 0.1315472 0.8378585 0.4434221
## [3,] 0.9866124 0.7906252 0.7581866 0.1673449
##
## , , 1, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2559774 0.2347455 0.2715779 0.3989086
## [2,] 0.4029830 0.1290187 0.8217535 0.4348988
## [3,] 0.9676481 0.7754281 0.7436131 0.1641282
##
## , , 2, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.05497480 0.05041496 0.05832524 0.08567131
## [2,] 0.08654635 0.02770861 0.17648331 0.09340073
## [3,] 0.20781625 0.16653427 0.15970153 0.03524888
##
## , , 3, 4, 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01664250 0.015262100 0.01765678 0.02593524
## [2,] 0.02620014 0.008388215 0.05342672 0.02827517
## [3,] 0.06291213 0.050414851 0.04834638 0.01067088
##
## , , 1, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1973292 0.1809619 0.2093554 0.3075127
## [2,] 0.3106536 0.0994586 0.6334776 0.3352570
## [3,] 0.7459456 0.5977661 0.5732403 0.1265240
##
## , , 2, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2484192 0.2278143 0.2635591 0.3871301
## [2,] 0.3910843 0.1252092 0.7974899 0.4220577
## [3,] 0.9390767 0.7525323 0.7216566 0.1592821
##
## , , 3, 1, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2011818 0.1844950 0.2134429 0.3135166
## [2,] 0.3167188 0.1014004 0.6458457 0.3418026
## [3,] 0.7605095 0.6094369 0.5844323 0.1289943
##
## , , 1, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2450955 0.2247662 0.2600328 0.3819505
## [2,] 0.3858517 0.1235339 0.7868198 0.4164107
## [3,] 0.9265122 0.7424637 0.7120012 0.1571509
##
## , , 2, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1380067 0.12655980 0.1464175 0.21506600
## [2,] 0.2172627 0.06955863 0.4430370 0.23446964
## [3,] 0.5216940 0.41806129 0.4009086 0.08848745
##
## , , 3, 2, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2492890 0.2286119 0.2644819 0.3884855
## [2,] 0.3924535 0.1256476 0.8002819 0.4235353
## [3,] 0.9423644 0.7551670 0.7241832 0.1598397
##
## , , 1, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2108242 0.1933376 0.2236729 0.3285431
## [2,] 0.3318988 0.1062604 0.6768003 0.3581848
## [3,] 0.7969598 0.6386464 0.6124434 0.1351768
##
## , , 2, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2576204 0.2362523 0.2733210 0.4014689
## [2,] 0.4055695 0.1298468 0.8270280 0.4376902
## [3,] 0.9738589 0.7804052 0.7483859 0.1651817
##
## , , 3, 3, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1647843 0.15111643 0.1748271 0.2567957
## [2,] 0.2594186 0.08305523 0.5290003 0.2799642
## [3,] 0.6229193 0.49917850 0.4786977 0.1056568
##
## , , 1, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1869750 0.17146650 0.1983702 0.2913770
## [2,] 0.2943531 0.09423985 0.6002380 0.3176656
## [3,] 0.7068046 0.56640030 0.5431615 0.1198851
##
## , , 2, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07068051 0.06481797 0.07498813 0.11014669
## [2,] 0.11127172 0.03562466 0.22690270 0.12008432
## [3,] 0.26718713 0.21411132 0.20532654 0.04531911
##
## , , 3, 4, 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06979935 0.06400990 0.07405327 0.10877351
## [2,] 0.10988451 0.03518053 0.22407394 0.11858725
## [3,] 0.26385615 0.21144203 0.20276677 0.04475412
##
## , , 1, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1123693 0.10304889 0.1192176 0.17511336
## [2,] 0.1769020 0.05663678 0.3607344 0.19091240
## [3,] 0.4247793 0.34039838 0.3264322 0.07204921
##
## , , 2, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1859048 0.17048507 0.1972348 0.2897092
## [2,] 0.2926683 0.09370044 0.5968024 0.3158473
## [3,] 0.7027590 0.56315837 0.5400525 0.1191989
##
## , , 3, 1, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03732587 0.03422991 0.03960069 0.05816767
## [2,] 0.05876180 0.01881313 0.11982569 0.06341567
## [3,] 0.14109960 0.11307065 0.10843147 0.02393270
##
## , , 1, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1079408 0.09898778 0.1145193 0.16821223
## [2,] 0.1699303 0.05440475 0.3465180 0.18338864
## [3,] 0.4080390 0.32698344 0.3135676 0.06920978
##
## , , 2, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1448980 0.13287952 0.1537288 0.22580526
## [2,] 0.2281116 0.07303202 0.4651599 0.24617781
## [3,] 0.5477447 0.43893705 0.4209279 0.09290604
##
## , , 3, 2, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01923340 0.01763810 0.02040558 0.02997283
## [2,] 0.03027897 0.00969409 0.06174417 0.03267703
## [3,] 0.07270627 0.05826342 0.05587293 0.01233212
##
## , , 1, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1462117 0.13408425 0.1551225 0.22785249
## [2,] 0.2301798 0.07369416 0.4693772 0.24840975
## [3,] 0.5527107 0.44291661 0.4247442 0.09374836
##
## , , 2, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1318280 0.12089361 0.1398622 0.20543732
## [2,] 0.2075356 0.06644444 0.4232019 0.22397224
## [3,] 0.4983374 0.39934434 0.3829596 0.08452579
##
## , , 3, 3, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2131522 0.1954725 0.2261428 0.3321709
## [2,] 0.3355637 0.1074338 0.6842737 0.3621400
## [3,] 0.8057600 0.6456986 0.6192062 0.1366695
##
## , , 1, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1945293 0.17839421 0.2063849 0.3031494
## [2,] 0.3062458 0.09804739 0.6244893 0.3305001
## [3,] 0.7353614 0.58928439 0.5651066 0.1247288
##
## , , 2, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2316630 0.2124479 0.2457817 0.3610176
## [2,] 0.3647050 0.1167637 0.7436980 0.3935893
## [3,] 0.8757346 0.7017729 0.6729798 0.1485382
##
## , , 3, 4, 3
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01998955 0.01833153 0.02120781 0.03115120
## [2,] 0.03146938 0.01007521 0.06417163 0.03396172
## [3,] 0.07556469 0.06055403 0.05806955 0.01281695
##
## , , 1, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.08442179 0.07741949 0.08956687 0.13156074
## [2,] 0.13290449 0.04255059 0.27101575 0.14343038
## [3,] 0.31913203 0.25573755 0.24524488 0.05412977
##
## , , 2, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.07805684 0.07158248 0.08281401 0.12164177
## [2,] 0.12288422 0.03934251 0.25058263 0.13261651
## [3,] 0.29507121 0.23645632 0.22675475 0.05004868
##
## , , 3, 1, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1416855 0.12993349 0.1503205 0.22079899
## [2,] 0.2230542 0.07141285 0.4548470 0.24071987
## [3,] 0.5356008 0.42920550 0.4115956 0.09084625
##
## , , 1, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06385342 0.05855714 0.06774496 0.09950752
## [2,] 0.10052388 0.03218364 0.20498596 0.10848526
## [3,] 0.24137928 0.19343011 0.18549386 0.04094169
##
## , , 2, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2493686 0.2286849 0.2645664 0.3886096
## [2,] 0.3925789 0.1256877 0.8005377 0.4236707
## [3,] 0.9426656 0.7554083 0.7244146 0.1598908
##
## , , 3, 2, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2375181 0.2178174 0.2519936 0.3701421
## [2,] 0.3739227 0.1197148 0.7624945 0.4035370
## [3,] 0.8978682 0.7195097 0.6899889 0.1522925
##
## , , 1, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1977788 0.18137423 0.2098325 0.3082134
## [2,] 0.3113615 0.09968524 0.6349212 0.3360210
## [3,] 0.7476454 0.59912822 0.5745466 0.1268123
##
## , , 2, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1356328 0.12438286 0.1438990 0.21136669
## [2,] 0.2135256 0.06836217 0.4354164 0.23043657
## [3,] 0.5127204 0.41087029 0.3940127 0.08696539
##
## , , 3, 3, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2614295 0.2397454 0.2773623 0.4074049
## [2,] 0.4115661 0.1317667 0.8392561 0.4441617
## [3,] 0.9882581 0.7919440 0.7594513 0.1676240
##
## , , 1, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1183190 0.10850515 0.1255300 0.18438532
## [2,] 0.1862686 0.05963561 0.3798347 0.20102090
## [3,] 0.4472707 0.35842191 0.3437162 0.07586409
##
## , , 2, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2157485 0.1978534 0.2288973 0.3362169
## [2,] 0.3396510 0.1087424 0.6926084 0.3665510
## [3,] 0.8155745 0.6535634 0.6267484 0.1383342
##
## , , 3, 4, 4
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1890408 0.17336095 0.2005619 0.2945963
## [2,] 0.2976053 0.09528106 0.6068698 0.3211753
## [3,] 0.7146138 0.57265818 0.5491626 0.1212096
##
## , , 1, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1080976 0.09913148 0.1146855 0.16845643
## [2,] 0.1701770 0.05448374 0.3470211 0.18365487
## [3,] 0.4086314 0.32745814 0.3140229 0.06931026
##
## , , 2, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.004301792 0.003944983 0.004563964 0.006703801
## [2,] 0.006772273 0.002168205 0.013809862 0.007308630
## [3,] 0.016261673 0.013031347 0.012496683 0.002758233
##
## , , 3, 1, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1458008 0.13370744 0.1546866 0.22721216
## [2,] 0.2295329 0.07348706 0.4680581 0.24771165
## [3,] 0.5511574 0.44167191 0.4235505 0.09348491
##
## , , 1, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1521442 0.13952469 0.1614166 0.23709755
## [2,] 0.2395193 0.07668428 0.4884221 0.25848892
## [3,] 0.5751368 0.46088786 0.4419781 0.09755218
##
## , , 2, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.02127151 0.01950716 0.02256790 0.03314897
## [2,] 0.03348756 0.01072135 0.06828705 0.03613973
## [3,] 0.08041077 0.06443744 0.06179364 0.01363892
##
## , , 3, 2, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.04333156 0.03973746 0.0459724 0.06752678
## [2,] 0.06821650 0.02184014 0.1391055 0.07361917
## [3,] 0.16380236 0.13126358 0.1258780 0.02778344
##
## , , 1, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.03283708 0.03011343 0.03483833 0.05117245
## [2,] 0.05169512 0.01655067 0.10541549 0.05578932
## [3,] 0.12413102 0.09947282 0.09539154 0.02105456
##
## , , 2, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.2193489 0.2011552 0.2327171 0.3418277
## [2,] 0.3453191 0.1105571 0.7041666 0.3726680
## [3,] 0.8291847 0.6644700 0.6372075 0.1406427
##
## , , 3, 3, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.01601851 0.014689865 0.01699475 0.02496283
## [2,] 0.02521780 0.008073709 0.05142355 0.02721502
## [3,] 0.06055331 0.048524603 0.04653369 0.01027079
##
## , , 1, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1244267 0.11410620 0.1320099 0.19390332
## [2,] 0.1958838 0.06271401 0.3994418 0.21139763
## [3,] 0.4703589 0.37692370 0.3614589 0.07978021
##
## , , 2, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.06336427 0.05810857 0.0672260 0.09874524
## [2,] 0.09975382 0.03193710 0.2034157 0.10765421
## [3,] 0.23953019 0.19194834 0.1840729 0.04062806
##
## , , 3, 4, 5
##
## [,1] [,2] [,3] [,4]
## [1,] 0.1897761 0.17403523 0.2013419 0.2957421
## [2,] 0.2987628 0.09565165 0.6092302 0.3224245
## [3,] 0.7173932 0.57488552 0.5512985 0.1216811
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.018866521 0.017301652 0.020016340 0.029401100
## [2,] 0.029701402 0.009509175 0.060566406 0.032053719
## [3,] 0.071319399 0.057152044 0.054807152 0.012096883
##
## ,,2,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.007291505 0.006686717 0.007735885 0.011362893
## [2,] 0.011478953 0.003675092 0.023407614 0.012388074
## [3,] 0.027563415 0.022088037 0.021181786 0.004675185
##
## ,,3,1,1
## [,1] [,2] [,3] [,4]
## [1,] 0.07175022 0.06579896 0.07612303 0.11181369
## [2,] 0.11295576 0.03616382 0.23033675 0.12190173
## [3,] 0.27123086 0.21735178 0.20843405 0.04600499
##
## ...
##
## ,,1,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.12442668 0.11410620 0.13200985 0.19390332
## [2,] 0.19588385 0.06271401 0.39944178 0.21139763
## [3,] 0.47035889 0.37692370 0.36145889 0.07978021
##
## ,,2,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.06336427 0.05810857 0.06722600 0.09874524
## [2,] 0.09975382 0.03193710 0.20341566 0.10765421
## [3,] 0.23953019 0.19194834 0.18407288 0.04062806
##
## ,,3,4,5
## [,1] [,2] [,3] [,4]
## [1,] 0.18977606 0.17403523 0.20134195 0.29574211
## [2,] 0.29876281 0.09565165 0.60923017 0.32242450
## [3,] 0.71739323 0.57488552 0.55129853 0.12168109
If there is a vanishing subscript on the left or right side of ->, the summation is done for that subscript.
einsum::einsum('i->', arrA)
## [1] 1.256229
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
## [1]
## 1.256229
einsum::einsum('ij->', arrC)
## [1] 5.738204
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
## [1]
## 5.738204
einsum::einsum('ijk->', arrE)
## [1] 31.17688
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
## [1]
## 31.17688
einsum::einsum('ij->i', arrC)
## [1] 1.189723 1.832574 2.715908
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 1.189723 1.832574 2.715908
einsum::einsum('ij->j', arrC)
## [1] 1.666549 1.167165 1.882050 1.022440
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 1.666549 1.167165 1.882050 1.022440
einsum::einsum('ijk->i', arrE)
## [1] 10.76352 10.19403 10.21933
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
## [1] [2] [3]
## 10.76352 10.19403 10.21933
einsum::einsum('ijk->j', arrE)
## [1] 6.263955 7.168623 10.111095 7.633208
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
## [1] [2] [3] [4]
## 6.263955 7.168623 10.111095 7.633208
einsum::einsum('ijk->k', arrE)
## [1] 4.948905 8.541069 5.891211 7.522429 4.273266
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
## [1] [2] [3] [4] [5]
## 4.948905 8.541069 5.891211 7.522429 4.273266
These are the same as what the modeSum function does.
einsum::einsum('ijk->ij', arrE)
## [,1] [,2] [,3] [,4]
## [1,] 1.986878 2.200456 3.219910 3.356279
## [2,] 1.997897 2.372380 3.397062 2.426692
## [3,] 2.279180 2.595787 3.494123 1.850236
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 1.986878 2.200456 3.219910 3.356279
## [2,] 1.997897 2.372380 3.397062 2.426692
## [3,] 2.279180 2.595787 3.494123 1.850236
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.3733211 2.466725 1.279632 1.159768 0.9845089
## [2,] 0.7935276 2.411287 1.037403 2.099955 0.8264504
## [3,] 2.5329481 2.414482 1.872899 2.268111 1.0226553
## [4,] 1.2491083 1.248575 1.701277 1.994596 1.4396511
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.3733211 2.4667248 1.2796320 1.1597682 0.9845089
## [2,] 0.7935276 2.4112874 1.0374028 2.0999549 0.8264504
## [3,] 2.5329481 2.4144822 1.8728992 2.2681106 1.0226553
## [4,] 1.2491083 1.2485751 1.7012773 1.9945957 1.4396511
einsum::einsum('ijk->jk', arrE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.3733211 2.466725 1.279632 1.159768 0.9845089
## [2,] 0.7935276 2.411287 1.037403 2.099955 0.8264504
## [3,] 2.5329481 2.414482 1.872899 2.268111 1.0226553
## [4,] 1.2491083 1.248575 1.701277 1.994596 1.4396511
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.3733211 2.4667248 1.2796320 1.1597682 0.9845089
## [2,] 0.7935276 2.4112874 1.0374028 2.0999549 0.8264504
## [3,] 2.5329481 2.4144822 1.8728992 2.2681106 1.0226553
## [4,] 1.2491083 1.2485751 1.7012773 1.9945957 1.4396511
If we take the diagonal elements of a matrix
and add them together, we get trace.
einsum::einsum('ii->', arrB)
## [1] 2.279889
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
## [1]
## 2.279889
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.9150066 0.6700499 0.6791085
## [2,] 0.9105056 0.6449287 0.5462015
## [3,] 0.4376265 0.4891162 0.7199535
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
## [,1] [,2] [,3]
## [1,] 0.9150066 0.6700499 0.6791085
## [2,] 0.9105056 0.6449287 0.5462015
## [3,] 0.4376265 0.4891162 0.7199535
einsum::einsum('ijk->jki', arrD)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.8778472 0.8119172 0.6827852
## [2,] 0.6662891 0.3298705 0.6996659
## [3,] 0.9863259 0.3254564 0.9115450
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.7797762 0.9833952 0.007578615
## [2,] 0.3061916 0.6654186 0.387789414
## [3,] 0.5974054 0.9013811 0.586417729
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.8432630 0.4909662 0.5942337
## [2,] 0.4594075 0.9234197 0.5043779
## [3,] 0.7896912 0.7610115 0.9656412
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.8778472 0.8119172 0.6827852
## [2,] 0.6662891 0.3298705 0.6996659
## [3,] 0.9863259 0.3254564 0.9115450
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.779776166 0.983395246 0.007578615
## [2,] 0.306191589 0.665418556 0.387789414
## [3,] 0.597405425 0.901381127 0.586417729
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.8432630 0.4909662 0.5942337
## [2,] 0.4594075 0.9234197 0.5043779
## [3,] 0.7896912 0.7610115 0.9656412
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.7158802
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
## [1]
## 0.7158802
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 3.689214
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
## [1]
## 3.689214
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 22.04579
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
## [1]
## 22.04579
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.08079472 2.0517790 0.7063012 0.4840624 0.4792176
## [2,] 0.32049822 2.0537790 0.4800190 1.7835652 0.3704170
## [3,] 2.26120914 2.0058940 1.2240206 1.8298169 0.7189228
## [4,] 1.00060811 0.6517325 1.3362396 1.3998359 0.8070737
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.08079472 2.05177900 0.70630119 0.48406241 0.47921764
## [2,] 0.32049822 2.05377897 0.48001902 1.78356519 0.37041705
## [3,] 2.26120914 2.00589401 1.22402061 1.82981685 0.71892278
## [4,] 1.00060811 0.65173250 1.33623957 1.39983592 0.80707370
Matrix Multiplication is considered a contracted product.
einsum::einsum('ij,jk->ik', arrC, t(arrC))
## [,1] [,2] [,3]
## [1,] 0.3710864 0.5564484 0.7318021
## [2,] 0.5564484 1.0953297 1.2307209
## [3,] 0.7318021 1.2307209 2.2227976
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.3710864 0.5564484 0.7318021
## [2,] 0.5564484 1.0953297 1.2307209
## [3,] 0.7318021 1.2307209 2.2227976
Some examples of combining Multiplication and Permutation are shown below.
einsum::einsum('ij,ij->ji', arrC, arrC)
## [,1] [,2] [,3]
## [1,] 0.06878180 0.17046835 0.98289071
## [2,] 0.05784489 0.01747333 0.63118027
## [3,] 0.07742108 0.70884860 0.58044937
## [4,] 0.16703867 0.19853942 0.02827723
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 0.06878180 0.17046835 0.98289071
## [2,] 0.05784489 0.01747333 0.63118027
## [3,] 0.07742108 0.70884860 0.58044937
## [4,] 0.16703867 0.19853942 0.02827723
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0051749972 0.5661208 0.1835784 0.10361809 0.16988622
## [2,] 0.0009454317 0.8733676 0.1693940 0.05927816 0.33654034
## [3,] 0.9588566362 0.6462009 0.3108068 0.56870381 0.01567673
## [4,] 0.9526419111 0.5082689 0.5501694 0.20353340 0.22508859
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0007729668 0.89721575 0.5024671 0.0885826 0.0002690452
## [2,] 0.0685032493 0.27690226 0.3052468 0.9040867 0.0065784438
## [3,] 0.3120043994 0.96491018 0.2526630 0.2674583 0.6995155230
## [4,] 0.0439393668 0.07263164 0.7802607 0.6767404 0.0583734399
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.074846755 0.58844249 0.020255658 0.2918617 0.30906237
## [2,] 0.251049544 0.90350915 0.005378219 0.8202003 0.02729827
## [3,] 0.990348100 0.39478290 0.660550800 0.9936548 0.00373053
## [4,] 0.004026832 0.07083195 0.005809418 0.5195622 0.52361167
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.0051749972 0.5661207548 0.1835783903 0.1036180874 0.1698862220
## [2,] 0.0009454317 0.8733675679 0.1693940345 0.0592781644 0.3365403353
## [3,] 0.9588566362 0.6462009276 0.3108068091 0.5687038087 0.0156767277
## [4,] 0.9526419111 0.5082689033 0.5501694358 0.2035333970 0.2250885906
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0007729668 0.8972157533 0.5024671376 0.0885826030 0.0002690452
## [2,] 0.0685032493 0.2769022552 0.3052467660 0.9040867186 0.0065784438
## [3,] 0.3120043994 0.9649101833 0.2526630014 0.2674582815 0.6995155230
## [4,] 0.0439393668 0.0726316403 0.7802607177 0.6767403527 0.0583734399
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.074846755 0.588442494 0.020255658 0.291861720 0.309062371
## [2,] 0.251049544 0.903509145 0.005378219 0.820200302 0.027298267
## [3,] 0.990348100 0.394782898 0.660550800 0.993654762 0.003730530
## [4,] 0.004026832 0.070831954 0.005809418 0.519562171 0.523611673
Some examples of combining Summation and Permutation are shown below.
einsum::einsum('ijk->ki', arrE)
## [,1] [,2] [,3]
## [1,] 2.057931 1.057724 1.833249
## [2,] 3.203747 2.725231 2.612091
## [3,] 2.139270 2.647320 1.104621
## [4,] 1.770640 2.588269 3.163520
## [5,] 1.591935 1.175487 1.505844
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
## [,1] [,2] [,3]
## [1,] 2.057931 1.057724 1.833249
## [2,] 3.203747 2.725231 2.612091
## [3,] 2.139270 2.647320 1.104621
## [4,] 1.770640 2.588269 3.163520
## [5,] 1.591935 1.175487 1.505844
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.0002510066 0.02745896 0.008904235 0.005025863 0.0082401134
## [2,] 0.0000385653 0.03562571 0.006909787 0.002418028 0.0137278855
## [3,] 0.0523497216 0.03527998 0.016968804 0.031048944 0.0008558864
## [4,] 0.1122142976 0.05987038 0.064805963 0.023974756 0.0265138010
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.222853e-05 0.0141941834 0.007949159 0.001401400 4.256364e-06
## [2,] 1.110853e-04 0.0004490264 0.000494990 0.001466072 1.066764e-05
## [3,] 2.052504e-02 0.0634760854 0.016621296 0.017594596 4.601724e-02
## [4,] 8.095996e-04 0.0013382657 0.014376602 0.012469199 1.075553e-03
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 3.371107e-02 0.2650352437 9.123174e-03 0.131454888 0.1392020830
## [2,] 7.261180e-02 0.2613246292 1.555558e-03 0.237228965 0.0078955587
## [3,] 2.634187e-01 0.1050067090 1.756972e-01 0.264298218 0.0009922687
## [4,] 5.217888e-05 0.0009178262 7.527727e-05 0.006732382 0.0067848547
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.0002510066 0.0274589616 0.0089042345 0.0050258625 0.0082401134
## [2,] 0.0000385653 0.0356257146 0.0069097866 0.0024180277 0.0137278855
## [3,] 0.0523497216 0.0352799756 0.0169688036 0.0310489441 0.0008558864
## [4,] 0.1122142976 0.0598703850 0.0648059634 0.0239747558 0.0265138010
##
## ,,2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.222853e-05 1.419418e-02 7.949159e-03 1.401400e-03 4.256364e-06
## [2,] 1.110853e-04 4.490264e-04 4.949900e-04 1.466072e-03 1.066764e-05
## [3,] 2.052504e-02 6.347609e-02 1.662130e-02 1.759460e-02 4.601724e-02
## [4,] 8.095996e-04 1.338266e-03 1.437660e-02 1.246920e-02 1.075553e-03
##
## ,,3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 3.371107e-02 2.650352e-01 9.123174e-03 1.314549e-01 1.392021e-01
## [2,] 7.261180e-02 2.613246e-01 1.555558e-03 2.372290e-01 7.895559e-03
## [3,] 2.634187e-01 1.050067e-01 1.756972e-01 2.642982e-01 9.922687e-04
## [4,] 5.217888e-05 9.178262e-04 7.527727e-05 6.732382e-03 6.784855e-03
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 Under development (unstable) (2026-11-13 r90887)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 26.04.1 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.25-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.25-bioc/R/lib/libRlapack.so; LAPACK version 3.12.1
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_GB LC_COLLATE=C
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## time zone: America/New_York
## tzcode source: system (glibc)
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] einsum_0.2.0 DelayedRandomArray_1.23.0
## [3] HDF5Array_1.43.0 h5mread_1.7.0
## [5] rhdf5_2.59.0 DelayedArray_0.41.0
## [7] SparseArray_1.15.0 S4Arrays_1.15.0
## [9] abind_1.4-8 IRanges_2.49.0
## [11] S4Vectors_0.53.0 MatrixGenerics_1.27.0
## [13] matrixStats_1.5.0 BiocGenerics_0.61.0
## [15] generics_0.1.4 Matrix_1.7-6
## [17] DelayedTensor_1.21.0 BiocStyle_2.43.0
##
## loaded via a namespace (and not attached):
## [1] dqrng_0.4.1 sass_0.5.0 lattice_0.23-1
## [4] digest_0.6.39 evaluate_1.0.5 grid_4.7.0
## [7] bookdown_0.48 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.3.0 XVector_0.55.0
## [16] cachem_1.1.0 yaml_2.3.12 otel_0.3.0
## [19] tools_4.7.0 beachmat_2.31.0 parallel_4.7.0
## [22] BiocParallel_1.49.0 Rhdf5lib_2.3.0 rsvd_2.0.0
## [25] R6_2.6.1 lifecycle_1.0.5 BiocSingular_1.31.0
## [28] irlba_2.4.1 ScaledMatrix_1.23.0 rTensor_1.5.0
## [31] bslib_0.12.0 Rcpp_1.1.2 xfun_0.62
## [34] knitr_1.52 rhdf5filters_1.27.0 htmltools_0.5.9
## [37] rmarkdown_2.32 compiler_4.7.0