4. Einsum operation by DelayedTensor

Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2025-11-11 13:55:52.017756
Compiled: Tue Nov 11 13:59:41 2025

What is einsum

einsum is an easy and intuitive way to write tensor operations.

It was originally introduced by Numpy1 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 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)

Einsum of 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.

Typical operations of einsum

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.

  1. Multiplication: the element values of the tensors on the left side of -> are multiplied by each other
  2. Summation: when comparing the left and right sides of ->, if there is a missing subscript on the right, the summation is done in the direction of the subscript
  3. Permutation: the subscripts to the right of -> can be rearranged in any order

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)

No Operation

diag

We can also extract the diagonal elements as follows.

einsum::einsum('ii->i', arrB)
## [1] 0.1181751 0.4370737 0.3452910
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.1181751 0.4370737 0.3452910
einsum::einsum('iii->i', arrD)
## [1] 0.6780809 0.5772520 0.7917708
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.6780809 0.5772520 0.7917708

Multiplication

By using multiple arrays or DelayedArray objects as input and writing “,” on the right side of ->, multiplication will be performed.

Hadamard Product

Hadamard Product can also be implemented in einsum, multiplying by the product of each element.

einsum::einsum('i,i->i', arrA, arrA)
## [1] 0.01244361 0.40117065 0.01763296
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
##        [1]        [2]        [3] 
## 0.01244361 0.40117065 0.01763296
einsum::einsum('ij,ij->ij', arrC, arrC)
##           [,1]       [,2]      [,3]        [,4]
## [1,] 0.1687937 0.12066136 0.8264505 0.626110635
## [2,] 0.8166054 0.34425095 0.8656792 0.247488497
## [3,] 0.1117221 0.02427646 0.2261673 0.000438107
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.168793658 0.120661361 0.826450550 0.626110635
## [2,] 0.816605359 0.344250951 0.865679175 0.247488497
## [3,] 0.111722089 0.024276463 0.226167310 0.000438107
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
## 
##             [,1]      [,2]       [,3]        [,4]
## [1,] 0.019795422 0.6067248 0.03198010 0.008435972
## [2,] 0.006535636 0.2399233 0.28415749 0.195054658
## [3,] 0.001121236 0.7124605 0.04871685 0.261140207
## 
## , , 2
## 
##           [,1]        [,2]      [,3]       [,4]
## [1,] 0.4919538 0.225019465 0.5667077 0.64993883
## [2,] 0.6422628 0.140389789 0.1080308 0.00748801
## [3,] 0.6091612 0.004389053 0.1169587 0.91168022
## 
## , , 3
## 
##           [,1]       [,2]      [,3]        [,4]
## [1,] 0.7335686 0.66208510 0.2497806 0.004105086
## [2,] 0.1357892 0.05455137 0.2929482 0.389431744
## [3,] 0.3997925 0.11512237 0.2509948 0.121125518
## 
## , , 4
## 
##              [,1]       [,2]       [,3]        [,4]
## [1,] 0.1919821195 0.15486157 0.99193562 0.003483023
## [2,] 0.0201223862 0.24451283 0.20112354 0.359185539
## [3,] 0.0007909717 0.05196382 0.04583377 0.083608874
## 
## , , 5
## 
##           [,1]       [,2]         [,3]      [,4]
## [1,] 0.2364347 0.38743025 0.0006870732 0.5589898
## [2,] 0.8396805 0.07251418 0.0730665026 0.3701855
## [3,] 0.4352668 0.84469384 0.1031976472 0.4144229
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.019795422 0.606724839 0.031980102 0.008435972
## [2,] 0.006535636 0.239923309 0.284157489 0.195054658
## [3,] 0.001121236 0.712460511 0.048716852 0.261140207
## 
## ,,2
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.491953829 0.225019465 0.566707733 0.649938830
## [2,] 0.642262814 0.140389789 0.108030817 0.007488010
## [3,] 0.609161173 0.004389053 0.116958674 0.911680222
## 
## ,,3
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.733568554 0.662085096 0.249780599 0.004105086
## [2,] 0.135789219 0.054551367 0.292948170 0.389431744
## [3,] 0.399792547 0.115122368 0.250994813 0.121125518
## 
## ,,4
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.1919821195 0.1548615662 0.9919356161 0.0034830227
## [2,] 0.0201223862 0.2445128346 0.2011235407 0.3591855386
## [3,] 0.0007909717 0.0519638173 0.0458337669 0.0836088742
## 
## ,,5
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.2364346810 0.3874302529 0.0006870732 0.5589898005
## [2,] 0.8396805240 0.0725141812 0.0730665026 0.3701854523
## [3,] 0.4352668295 0.8446938442 0.1031976472 0.4144228992

Outer Product

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.01244361 0.07065417 0.01481276
## [2,] 0.07065417 0.40117065 0.08410604
## [3,] 0.01481276 0.08410604 0.01763296
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.01244361 0.07065417 0.01481276
## [2,] 0.07065417 0.40117065 0.08410604
## [3,] 0.01481276 0.08410604 0.01763296
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
## 
##            [,1]       [,2]       [,3]        [,4]
## [1,] 0.05780434 0.04887272 0.12790597 0.111328900
## [2,] 0.12714184 0.08255055 0.13090639 0.069993850
## [3,] 0.04702750 0.02192174 0.06691096 0.002944913
## 
## , , 2, 1, 1
## 
##            [,1]       [,2]       [,3]        [,4]
## [1,] 0.03321406 0.02808200 0.07349408 0.063968984
## [2,] 0.07305501 0.04743310 0.07521811 0.040218088
## [3,] 0.02702175 0.01259611 0.03844668 0.001692131
## 
## , , 3, 1, 1
## 
##            [,1]        [,2]       [,3]        [,4]
## [1,] 0.01375709 0.011631419 0.03044087 0.026495623
## [2,] 0.03025901 0.019646544 0.03115495 0.016658124
## [3,] 0.01119227 0.005217246 0.01592442 0.000700872
## 
## , , 1, 2, 1
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.3200177 0.2705702 0.7081159 0.61634152
## [2,] 0.7038855 0.4570182 0.7247269 0.38750151
## [3,] 0.2603547 0.1213636 0.3704340 0.01630369
## 
## , , 2, 2, 1
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.2012400 0.17014545 0.4452918 0.38758036
## [2,] 0.4426315 0.28739142 0.4557374 0.24367655
## [3,] 0.1637215 0.07631834 0.2329438 0.01025242
## 
## , , 3, 2, 1
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.3467835 0.2932004 0.7673418 0.66789154
## [2,] 0.7627575 0.4952426 0.7853421 0.41991163
## [3,] 0.2821304 0.1315143 0.4014166 0.01766731
## 
## , , 1, 3, 1
## 
##            [,1]       [,2]       [,3]        [,4]
## [1,] 0.07347134 0.06211894 0.16257298 0.141502939
## [2,] 0.16160174 0.10492464 0.16638662 0.088964641
## [3,] 0.05977360 0.02786330 0.08504619 0.003743088
## 
## , , 2, 3, 1
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.2190068 0.18516703 0.4846051 0.42179856
## [2,] 0.4817100 0.31276427 0.4959730 0.26518995
## [3,] 0.1781759 0.08305624 0.2535096 0.01115757
## 
## , , 3, 3, 1
## 
##            [,1]       [,2]      [,3]        [,4]
## [1,] 0.09068129 0.07666969 0.2006541 0.174648616
## [2,] 0.19945536 0.12950221 0.2053611 0.109803736
## [3,] 0.07377499 0.03439001 0.1049674 0.004619869
## 
## , , 1, 4, 1
## 
##            [,1]       [,2]       [,3]        [,4]
## [1,] 0.03773511 0.03190448 0.08349799 0.072676351
## [2,] 0.08299916 0.05388962 0.08545669 0.045692517
## [3,] 0.03069991 0.01431068 0.04367999 0.001922462
## 
## , , 2, 4, 1
## 
##           [,1]       [,2]      [,3]        [,4]
## [1,] 0.1814497 0.15341304 0.4015010 0.349465014
## [2,] 0.3991023 0.25912883 0.4109194 0.219712958
## [3,] 0.1476208 0.06881306 0.2100357 0.009244177
## 
## , , 3, 4, 1
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.2099495 0.17750925 0.4645637 0.40435462
## [2,] 0.4617884 0.29982956 0.4754615 0.25422273
## [3,] 0.1708073 0.07962136 0.2430255 0.01069614
## 
## , , 1, 1, 2
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.2881643 0.2436387 0.6376327 0.55499327
## [2,] 0.6338234 0.4115283 0.6525904 0.34893110
## [3,] 0.2344400 0.1092836 0.3335624 0.01468089
## 
## , , 2, 1, 2
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.3292566 0.2783816 0.7285592 0.63413530
## [2,] 0.7242066 0.4702123 0.7456497 0.39868867
## [3,] 0.2678711 0.1248674 0.3811284 0.01677438
## 
## , , 3, 1, 2
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.3206595 0.2711129 0.7095362 0.61757776
## [2,] 0.7052973 0.4579348 0.7261805 0.38827874
## [3,] 0.2608769 0.1216071 0.3711770 0.01633639
## 
## , , 1, 2, 2
## 
##           [,1]       [,2]      [,3]        [,4]
## [1,] 0.1948893 0.16477607 0.4312394 0.375349277
## [2,] 0.4286632 0.27832205 0.4413555 0.235986714
## [3,] 0.1585549 0.07390992 0.2255927 0.009928877
## 
## , , 2, 2, 2
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.1539380 0.13015231 0.3406247 0.29647857
## [2,] 0.3385898 0.21983930 0.3486151 0.18639973
## [3,] 0.1252383 0.05837951 0.1781897 0.00784256
## 
## , , 3, 2, 2
## 
##            [,1]       [,2]       [,3]        [,4]
## [1,] 0.02721846 0.02301280 0.06022736 0.052421684
## [2,] 0.05986756 0.03887076 0.06164018 0.032958158
## [3,] 0.02214394 0.01032234 0.03150651 0.001386678
## 
## , , 1, 3, 2
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.3092841 0.2614952 0.6843653 0.59566915
## [2,] 0.6802768 0.4416896 0.7004192 0.37450453
## [3,] 0.2516223 0.1172930 0.3580094 0.01575686
## 
## , , 2, 3, 2
## 
##           [,1]       [,2]      [,3]        [,4]
## [1,] 0.1350367 0.11417156 0.2988012 0.260075457
## [2,] 0.2970161 0.19284634 0.3058104 0.163512643
## [3,] 0.1098610 0.05121139 0.1563107 0.006879612
## 
## , , 3, 3, 2
## 
##           [,1]       [,2]      [,3]        [,4]
## [1,] 0.1405058 0.11879559 0.3109028 0.270608702
## [2,] 0.3090454 0.20065676 0.3181960 0.170135024
## [3,] 0.1143104 0.05328548 0.1626414 0.007158241
## 
## , , 1, 4, 2
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.3312183 0.2800402 0.7328999 0.63791348
## [2,] 0.7285215 0.4730138 0.7500923 0.40106406
## [3,] 0.2694671 0.1256114 0.3833992 0.01687432
## 
## , , 2, 4, 2
## 
##            [,1]       [,2]       [,3]        [,4]
## [1,] 0.03555177 0.03005850 0.07866683 0.068471327
## [2,] 0.07819686 0.05077159 0.08051220 0.043048767
## [3,] 0.02892363 0.01348267 0.04115268 0.001811229
## 
## , , 3, 4, 2
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.3922829 0.3316694 0.8680199 0.75552146
## [2,] 0.8628343 0.5602203 0.8883820 0.47500565
## [3,] 0.3191470 0.1487695 0.4540840 0.01998533
## 
## , , 1, 1, 3
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.3518831 0.2975120 0.7786258 0.67771312
## [2,] 0.7739742 0.5025253 0.7968908 0.42608659
## [3,] 0.2862793 0.1334483 0.4073196 0.01792712
## 
## , , 2, 1, 3
## 
##           [,1]      [,2]      [,3]        [,4]
## [1,] 0.1513947 0.1280020 0.3349971 0.291580304
## [2,] 0.3329958 0.2162072 0.3428555 0.183320129
## [3,] 0.1231692 0.0574150 0.1752458 0.007712989
## 
## , , 3, 1, 3
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.2597738 0.21963495 0.5748119 0.5003143
## [2,] 0.5713779 0.37098378 0.5882959 0.3145537
## [3,] 0.2113425 0.09851674 0.3006992 0.0132345
## 
## , , 1, 2, 3
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.3342989 0.2826448 0.7397166 0.64384666
## [2,] 0.7352974 0.4774133 0.7570689 0.40479432
## [3,] 0.2719734 0.1267797 0.3869651 0.01703127
## 
## , , 2, 2, 3
## 
##            [,1]       [,2]      [,3]        [,4]
## [1,] 0.09595793 0.08113102 0.2123299 0.184811231
## [2,] 0.21106146 0.13703781 0.2173108 0.116193097
## [3,] 0.07806787 0.03639113 0.1110754 0.004888695
## 
## , , 3, 2, 3
## 
##           [,1]       [,2]      [,3]        [,4]
## [1,] 0.1393984 0.11785933 0.3084525 0.268475956
## [2,] 0.3066098 0.19907532 0.3156882 0.168794140
## [3,] 0.1134095 0.05286553 0.1613596 0.007101825
## 
## , , 1, 3, 3
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.2053324 0.17360549 0.4543471 0.39546212
## [2,] 0.4516328 0.29323576 0.4650052 0.24863191
## [3,] 0.1670509 0.07787034 0.2376809 0.01046091
## 
## , , 2, 3, 3
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.2223686 0.18800937 0.4920439 0.42827324
## [2,] 0.4891043 0.31756525 0.5035863 0.26926066
## [3,] 0.1809110 0.08433116 0.2574010 0.01132884
## 
## , , 3, 3, 3
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.2058308 0.17402694 0.4554501 0.3964222
## [2,] 0.4527292 0.29394762 0.4661341 0.2492355
## [3,] 0.1674565 0.07805938 0.2382579 0.0104863
## 
## , , 1, 4, 3
## 
##            [,1]        [,2]       [,3]        [,4]
## [1,] 0.02632323 0.022255904 0.05824646 0.050697515
## [2,] 0.05789849 0.037592283 0.05961281 0.031874152
## [3,] 0.02141562 0.009982834 0.03047025 0.001341069
## 
## , , 2, 4, 3
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.2563857 0.21677030 0.5673148 0.49378878
## [2,] 0.5639256 0.36614512 0.5806229 0.31045109
## [3,] 0.2085860 0.09723181 0.2967772 0.01306188
## 
## , , 3, 4, 3
## 
##           [,1]       [,2]      [,3]        [,4]
## [1,] 0.1429868 0.12089322 0.3163926 0.275386956
## [2,] 0.3145024 0.20419984 0.3238145 0.173139171
## [3,] 0.1163288 0.05422637 0.1655132 0.007284637
## 
## , , 1, 1, 4
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.1800149 0.15219995 0.3983262 0.34670167
## [2,] 0.3959465 0.25707981 0.4076701 0.21797561
## [3,] 0.1464536 0.06826893 0.2083749 0.00917108
## 
## , , 2, 1, 4
## 
##            [,1]       [,2]       [,3]        [,4]
## [1,] 0.05827977 0.04927468 0.12895797 0.112244555
## [2,] 0.12818755 0.08322951 0.13198307 0.070569534
## [3,] 0.04741429 0.02210204 0.06746129 0.002969134
## 
## , , 3, 1, 4
## 
##            [,1]        [,2]       [,3]         [,4]
## [1,] 0.01155470 0.009769326 0.02556754 0.0222538944
## [2,] 0.02541479 0.016501296 0.02616730 0.0139912975
## [3,] 0.00940048 0.004382008 0.01337505 0.0005886682
## 
## , , 1, 2, 4
## 
##           [,1]       [,2]      [,3]        [,4]
## [1,] 0.1616776 0.13669604 0.3577505 0.311384768
## [2,] 0.3556133 0.23089227 0.3661426 0.195771439
## [3,] 0.1315350 0.06131469 0.1871487 0.008236864
## 
## , , 2, 2, 4
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.2031556 0.17176511 0.4495306 0.39126984
## [2,] 0.4468450 0.29012717 0.4600757 0.24599617
## [3,] 0.1652800 0.07704484 0.2351612 0.01035001
## 
## , , 3, 2, 4
## 
##            [,1]       [,2]      [,3]        [,4]
## [1,] 0.09365449 0.07918349 0.2072330 0.180374884
## [2,] 0.20599498 0.13374825 0.2120943 0.113403911
## [3,] 0.07619387 0.03551757 0.1084090 0.004771343
## 
## , , 1, 3, 4
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.4091851 0.3459600 0.9054202 0.78807451
## [2,] 0.9000111 0.5843584 0.9266596 0.49547215
## [3,] 0.3328981 0.1551795 0.4736490 0.02084644
## 
## , , 2, 3, 4
## 
##           [,1]       [,2]      [,3]        [,4]
## [1,] 0.1842509 0.15578139 0.4076992 0.354859955
## [2,] 0.4052636 0.26312919 0.4172631 0.223104825
## [3,] 0.1498998 0.06987538 0.2132782 0.009386886
## 
## , , 3, 3, 4
## 
##            [,1]       [,2]      [,3]        [,4]
## [1,] 0.08795709 0.07436642 0.1946262 0.169401915
## [2,] 0.19346343 0.12561177 0.1991917 0.106505071
## [3,] 0.07155868 0.03335688 0.1018140 0.004481082
## 
## , , 1, 4, 4
## 
##            [,1]        [,2]       [,3]        [,4]
## [1,] 0.02424690 0.020500396 0.05365208 0.046698582
## [2,] 0.05333156 0.034627068 0.05491066 0.029359974
## [3,] 0.01972639 0.009195405 0.02806681 0.001235288
## 
## , , 2, 4, 4
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.2462280 0.20818217 0.5448386 0.47422556
## [2,] 0.5415836 0.35163897 0.5576194 0.29815145
## [3,] 0.2003221 0.09337963 0.2850193 0.01254439
## 
## , , 3, 4, 4
## 
##            [,1]      [,2]      [,3]        [,4]
## [1,] 0.11879667 0.1004408 0.2628661 0.228797739
## [2,] 0.26129572 0.1696539 0.2690325 0.143847956
## [3,] 0.09664863 0.0450525 0.1375122 0.006052242
## 
## , , 1, 1, 5
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.1997716 0.16890391 0.4420425 0.38475222
## [2,] 0.4394017 0.28529435 0.4524120 0.24189846
## [3,] 0.1625268 0.07576145 0.2312440 0.01017761
## 
## , , 2, 1, 5
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.3764741 0.3183033 0.8330393 0.72507441
## [2,] 0.8280626 0.5376438 0.8525808 0.45586321
## [3,] 0.3062856 0.1427742 0.4357847 0.01917994
## 
## , , 3, 1, 5
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.2710540 0.2291722 0.5997720 0.52203945
## [2,] 0.5961889 0.3870930 0.6138415 0.32821263
## [3,] 0.2205197 0.1027946 0.3137565 0.01380918
## 
## , , 1, 2, 5
## 
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.2557260 0.21621254 0.5658551 0.49251822
## [2,] 0.5624746 0.36520300 0.5791289 0.30965227
## [3,] 0.2080493 0.09698163 0.2960136 0.01302827
## 
## , , 2, 2, 5
## 
##            [,1]       [,2]      [,3]        [,4]
## [1,] 0.11063423 0.09353962 0.2448048 0.213077216
## [2,] 0.24334229 0.15799708 0.2505474 0.133964270
## [3,] 0.09000798 0.04195698 0.1280638 0.005636397
## 
## , , 3, 2, 5
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.3775963 0.3192521 0.8355224 0.72723572
## [2,] 0.8305309 0.5392464 0.8551221 0.45722206
## [3,] 0.3071986 0.1431998 0.4370837 0.01923711
## 
## , , 1, 3, 5
## 
##            [,1]        [,2]       [,3]         [,4]
## [1,] 0.01076910 0.009105119 0.02382923 0.0207408733
## [2,] 0.02368687 0.015379389 0.02438821 0.0130400425
## [3,] 0.00876135 0.004084080 0.01246569 0.0005486452
## 
## , , 2, 3, 5
## 
##            [,1]       [,2]      [,3]        [,4]
## [1,] 0.11105477 0.09389517 0.2457353 0.213887153
## [2,] 0.24426727 0.15859765 0.2514998 0.134473488
## [3,] 0.09035011 0.04211646 0.1285506 0.005657822
## 
## , , 3, 3, 5
## 
##           [,1]       [,2]      [,3]        [,4]
## [1,] 0.1319815 0.11158839 0.2920407 0.254191157
## [2,] 0.2902960 0.18848312 0.2988914 0.159813111
## [3,] 0.1073753 0.05005271 0.1527741 0.006723958
## 
## , , 1, 4, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3071709 0.2597084 0.6796892 0.5915991
## [2,] 0.6756286 0.4386716 0.6956334 0.3719456
## [3,] 0.2499030 0.1164916 0.3555632 0.0156492
## 
## , , 2, 4, 5
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.2499699 0.2113459 0.5531184 0.48143229
## [2,] 0.5498140 0.3569828 0.5660935 0.30268241
## [3,] 0.2033664 0.0947987 0.2893507 0.01273502
## 
## , , 3, 4, 5
## 
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.2644843 0.2236176 0.5852350 0.50938648
## [2,] 0.5817387 0.3777108 0.5989635 0.32025755
## [3,] 0.2151748 0.1003032 0.3061518 0.01347448
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.057804339 0.048872717 0.127905969 0.111328900
## [2,] 0.127141839 0.082550546 0.130906395 0.069993850
## [3,] 0.047027501 0.021921743 0.066910965 0.002944913
## 
## ,,2,1,1
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.033214061 0.028081999 0.073494078 0.063968984
## [2,] 0.073055013 0.047433098 0.075218107 0.040218088
## [3,] 0.027021748 0.012596115 0.038446679 0.001692131
## 
## ,,3,1,1
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.013757092 0.011631419 0.030440868 0.026495623
## [2,] 0.030259009 0.019646544 0.031154951 0.016658124
## [3,] 0.011192268 0.005217246 0.015924416 0.000700872
## 
## ...
## 
## ,,1,4,5
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3071709 0.2597084 0.6796892 0.5915991
## [2,] 0.6756286 0.4386716 0.6956334 0.3719456
## [3,] 0.2499030 0.1164916 0.3555632 0.0156492
## 
## ,,2,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.24996991 0.21134588 0.55311841 0.48143229
## [2,] 0.54981399 0.35698276 0.56609349 0.30268241
## [3,] 0.20336640 0.09479870 0.28935074 0.01273502
## 
## ,,3,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.26448432 0.22361760 0.58523502 0.50938648
## [2,] 0.58173874 0.37771084 0.59896350 0.32025755
## [3,] 0.21517479 0.10030315 0.30615178 0.01347448

Summation

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.8777204
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
##       [1] 
## 0.8777204
einsum::einsum('ij->', arrC)
## [1] 6.363425
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
##      [1] 
## 6.363425
einsum::einsum('ijk->', arrE)
## [1] 27.45741
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
##      [1] 
## 27.45741

Row-wise / Column-wise Summation

einsum::einsum('ij->i', arrC)
## [1] 2.4585733 2.9182922 0.9865591
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 2.4585733 2.9182922 0.9865591
einsum::einsum('ij->j', arrC)
## [1] 1.648756 1.089902 2.315083 1.309685
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 1.648756 1.089902 2.315083 1.309685

Mode-wise Vectorization

einsum::einsum('ijk->i', arrE)
## [1] 9.928279 8.652527 8.876600
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
##      [1]      [2]      [3] 
## 9.928279 8.652527 8.876600
einsum::einsum('ijk->j', arrE)
## [1] 7.066062 7.341421 6.174387 6.875535
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 7.066062 7.341421 6.174387 6.875535
einsum::einsum('ijk->k', arrE)
## [1] 4.344972 6.469606 5.821987 4.330104 6.490736
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4]      [5] 
## 4.344972 6.469606 5.821987 4.330104 6.490736

Mode-wise Summation

These are the same as what the modeSum function does.

einsum::einsum('ijk->ij', arrE)
##          [,1]     [,2]     [,3]     [,4]
## [1,] 2.622980 3.082937 2.453582 1.768780
## [2,] 2.308946 1.861835 2.121768 2.359977
## [3,] 2.134136 2.396649 1.599037 2.746778
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]     [,4]
## [1,] 2.622980 3.082937 2.453582 1.768780
## [2,] 2.308946 1.861835 2.121768 2.359977
## [3,] 2.134136 2.396649 1.599037 2.746778
einsum::einsum('ijk->jk', arrE)
##           [,1]      [,2]     [,3]      [,4]      [,5]
## [1,] 0.2550243 2.2832950 1.857274 0.6081353 2.0623339
## [2,] 2.1128189 0.9152984 1.386545 1.1159627 1.8107958
## [3,] 0.9326130 1.4234726 1.542021 1.6585159 0.6177641
## [4,] 1.0445163 1.8475406 1.036147 0.9474900 1.9998417
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.2550243 2.2832950 1.8572736 0.6081353 2.0623339
## [2,] 2.1128189 0.9152984 1.3865453 1.1159627 1.8107958
## [3,] 0.9326130 1.4234726 1.5420212 1.6585159 0.6177641
## [4,] 1.0445163 1.8475406 1.0361465 0.9474900 1.9998417
einsum::einsum('ijk->jk', arrE)
##           [,1]      [,2]     [,3]      [,4]      [,5]
## [1,] 0.2550243 2.2832950 1.857274 0.6081353 2.0623339
## [2,] 2.1128189 0.9152984 1.386545 1.1159627 1.8107958
## [3,] 0.9326130 1.4234726 1.542021 1.6585159 0.6177641
## [4,] 1.0445163 1.8475406 1.036147 0.9474900 1.9998417
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.2550243 2.2832950 1.8572736 0.6081353 2.0623339
## [2,] 2.1128189 0.9152984 1.3865453 1.1159627 1.8107958
## [3,] 0.9326130 1.4234726 1.5420212 1.6585159 0.6177641
## [4,] 1.0445163 1.8475406 1.0361465 0.9474900 1.9998417

Trace

If we take the diagonal elements of a matrix and add them together, we get trace.

einsum::einsum('ii->', arrB)
## [1] 0.9005398
DelayedTensor::einsum('ii->', darrB)
## <1> HDF5Array object of type "double":
##       [1] 
## 0.9005398

Permutation

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.11817505 0.9727830 0.7768612
## [2,] 0.01345527 0.4370737 0.8521692
## [3,] 0.74069668 0.6539967 0.3452910
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.11817505 0.97278298 0.77686119
## [2,] 0.01345527 0.43707370 0.85216919
## [3,] 0.74069668 0.65399674 0.34529103
einsum::einsum('ijk->jki', arrD)
## , , 1
## 
##           [,1]      [,2]       [,3]
## [1,] 0.6780809 0.4745330 0.06959627
## [2,] 0.3071211 0.1561101 0.01304228
## [3,] 0.9716467 0.4113513 0.19542366
## 
## , , 2
## 
##           [,1]      [,2]       [,3]
## [1,] 0.2922804 0.1819037 0.71952200
## [2,] 0.9193625 0.5772520 0.30576277
## [3,] 0.6590923 0.2526422 0.03462885
## 
## , , 3
## 
##           [,1]      [,2]       [,3]
## [1,] 0.2352543 0.7295398 0.40445621
## [2,] 0.3657475 0.7493032 0.09568787
## [3,] 0.5346562 0.1627233 0.79177079
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
##            [,1]       [,2]       [,3]
## [1,] 0.67808092 0.47453299 0.06959627
## [2,] 0.30712109 0.15611007 0.01304228
## [3,] 0.97164670 0.41135129 0.19542366
## 
## ,,2
##            [,1]       [,2]       [,3]
## [1,] 0.29228036 0.18190375 0.71952200
## [2,] 0.91936250 0.57725204 0.30576277
## [3,] 0.65909233 0.25264215 0.03462885
## 
## ,,3
##            [,1]       [,2]       [,3]
## [1,] 0.23525432 0.72953978 0.40445621
## [2,] 0.36574748 0.74930319 0.09568787
## [3,] 0.53465618 0.16272327 0.79177079

Multiplication + Summation

Some examples of combining Multiplication and Summation are shown below.

Inner Product (Squared Frobenius Norm)

Inner Product first calculate Hadamard Product and collapses it to 0D tensor (norm).

einsum::einsum('i,i->', arrA, arrA)
## [1] 0.4312472
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
##       [1] 
## 0.4312472
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 4.378644
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
##      [1] 
## 4.378644
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 16.9853
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
##     [1] 
## 16.9853

Contracted Product

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.02745229 1.7433778 1.2691503 0.2128955 1.5113820
## [2,] 1.55910866 0.3697983 0.8317588 0.4513382 1.3046383
## [3,] 0.36485444 0.7916972 0.7937236 1.2388929 0.1769512
## [4,] 0.46463084 1.5691071 0.5146623 0.4462774 1.3435982
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]       [,4]       [,5]
## [1,] 0.02745229 1.74337782 1.26915032 0.21289548 1.51138203
## [2,] 1.55910866 0.36979831 0.83175883 0.45133822 1.30463828
## [3,] 0.36485444 0.79169722 0.79372358 1.23889292 0.17695122
## [4,] 0.46463084 1.56910706 0.51466235 0.44627744 1.34359815

Matrix Multiplication

Matrix Multiplication is considered a contracted product.

einsum::einsum('ij,jk->ik', arrC, t(arrC))
##           [,1]     [,2]      [,3]
## [1,] 1.7420162 1.814554 0.6403468
## [2,] 1.8145543 2.274024 0.8463580
## [3,] 0.6403468 0.846358 0.3626040
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 1.7420162 1.8145543 0.6403468
## [2,] 1.8145543 2.2740240 0.8463580
## [3,] 0.6403468 0.8463580 0.3626040

Multiplication + Permutation

Some examples of combining Multiplication and Permutation are shown below.

einsum::einsum('ij,ij->ji', arrC, arrC)
##           [,1]      [,2]        [,3]
## [1,] 0.1687937 0.8166054 0.111722089
## [2,] 0.1206614 0.3442510 0.024276463
## [3,] 0.8264505 0.8656792 0.226167310
## [4,] 0.6261106 0.2474885 0.000438107
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
##             [,1]        [,2]        [,3]
## [1,] 0.168793658 0.816605359 0.111722089
## [2,] 0.120661361 0.344250951 0.024276463
## [3,] 0.826450550 0.865679175 0.226167310
## [4,] 0.626110635 0.247488497 0.000438107
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
## 
##             [,1]      [,2]        [,3]        [,4]         [,5]
## [1,] 0.019795422 0.4919538 0.733568554 0.191982119 0.2364346810
## [2,] 0.606724839 0.2250195 0.662085096 0.154861566 0.3874302529
## [3,] 0.031980102 0.5667077 0.249780599 0.991935616 0.0006870732
## [4,] 0.008435972 0.6499388 0.004105086 0.003483023 0.5589898005
## 
## , , 2
## 
##             [,1]       [,2]       [,3]       [,4]       [,5]
## [1,] 0.006535636 0.64226281 0.13578922 0.02012239 0.83968052
## [2,] 0.239923309 0.14038979 0.05455137 0.24451283 0.07251418
## [3,] 0.284157489 0.10803082 0.29294817 0.20112354 0.07306650
## [4,] 0.195054658 0.00748801 0.38943174 0.35918554 0.37018545
## 
## , , 3
## 
##             [,1]        [,2]      [,3]         [,4]      [,5]
## [1,] 0.001121236 0.609161173 0.3997925 0.0007909717 0.4352668
## [2,] 0.712460511 0.004389053 0.1151224 0.0519638173 0.8446938
## [3,] 0.048716852 0.116958674 0.2509948 0.0458337669 0.1031976
## [4,] 0.261140207 0.911680222 0.1211255 0.0836088742 0.4144229
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.0197954215 0.4919538291 0.7335685536 0.1919821195 0.2364346810
## [2,] 0.6067248386 0.2250194649 0.6620850956 0.1548615662 0.3874302529
## [3,] 0.0319801017 0.5667077329 0.2497805985 0.9919356161 0.0006870732
## [4,] 0.0084359723 0.6499388303 0.0041050860 0.0034830227 0.5589898005
## 
## ,,2
##             [,1]        [,2]        [,3]        [,4]        [,5]
## [1,] 0.006535636 0.642262814 0.135789219 0.020122386 0.839680524
## [2,] 0.239923309 0.140389789 0.054551367 0.244512835 0.072514181
## [3,] 0.284157489 0.108030817 0.292948170 0.201123541 0.073066503
## [4,] 0.195054658 0.007488010 0.389431744 0.359185539 0.370185452
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.0011212364 0.6091611729 0.3997925466 0.0007909717 0.4352668295
## [2,] 0.7124605109 0.0043890532 0.1151223678 0.0519638173 0.8446938442
## [3,] 0.0487168520 0.1169586735 0.2509948127 0.0458337669 0.1031976472
## [4,] 0.2611402067 0.9116802220 0.1211255185 0.0836088742 0.4144228992

Summation + Permutation

Some examples of combining Summation and Permutation are shown below.

einsum::einsum('ijk->ki', arrE)
##          [,1]     [,2]      [,3]
## [1,] 1.190299 1.545377 1.6092964
## [2,] 2.734744 1.591313 2.1435494
## [3,] 2.234024 1.767350 1.8206131
## [4,] 1.886659 1.684125 0.7593202
## [5,] 1.882553 2.064362 2.5438206
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 1.1902990 1.5453771 1.6092964
## [2,] 2.7347441 1.5913130 2.1435494
## [3,] 2.2340238 1.7673498 1.8206131
## [4,] 1.8866590 1.6841247 0.7593202
## [5,] 1.8825527 2.0643622 2.5438206

Multiplication + Summation + Permutation

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.0003727298 0.009263044 0.0138124296 0.0036148489 0.0044518503
## [2,] 0.0081664487 0.003028737 0.0089115916 0.0020844194 0.0052147680
## [3,] 0.0029482884 0.052245544 0.0230276076 0.0914478717 0.0000633422
## [4,] 0.0005891956 0.045393828 0.0002867125 0.0002432656 0.0390416541
## 
## , , 2
## 
##             [,1]        [,2]       [,3]       [,4]       [,5]
## [1,] 0.003380373 0.332192315 0.07023314 0.01040774 0.43430105
## [2,] 0.052313306 0.030610840 0.01189448 0.05331401 0.01581112
## [3,] 0.155804736 0.059233747 0.16062470 0.11027688 0.04006267
## [4,] 0.030575666 0.001173778 0.06104512 0.05630389 0.05802818
## 
## , , 3
## 
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 1.663408e-05 9.037200e-03 5.931115e-03 1.173445e-05 6.457393e-03
## [2,] 2.296724e-03 1.414878e-05 3.711144e-04 1.675132e-04 2.722998e-03
## [3,] 1.463092e-03 3.512569e-03 7.538019e-03 1.376506e-03 3.099290e-03
## [4,] 1.519206e-05 5.303778e-05 7.046581e-06 4.864018e-06 2.410941e-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.0003727298 0.0092630438 0.0138124296 0.0036148489 0.0044518503
## [2,] 0.0081664487 0.0030287369 0.0089115916 0.0020844194 0.0052147680
## [3,] 0.0029482884 0.0522455442 0.0230276076 0.0914478717 0.0000633422
## [4,] 0.0005891956 0.0453938283 0.0002867125 0.0002432656 0.0390416541
## 
## ,,2
##             [,1]        [,2]        [,3]        [,4]        [,5]
## [1,] 0.003380373 0.332192315 0.070233141 0.010407736 0.434301054
## [2,] 0.052313306 0.030610840 0.011894477 0.053314015 0.015811121
## [3,] 0.155804736 0.059233747 0.160624703 0.110276876 0.040062668
## [4,] 0.030575666 0.001173778 0.061045119 0.056303895 0.058028179
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 1.663408e-05 9.037200e-03 5.931115e-03 1.173445e-05 6.457393e-03
## [2,] 2.296724e-03 1.414878e-05 3.711144e-04 1.675132e-04 2.722998e-03
## [3,] 1.463092e-03 3.512569e-03 7.538019e-03 1.376506e-03 3.099290e-03
## [4,] 1.519206e-05 5.303778e-05 7.046581e-06 4.864018e-06 2.410941e-05

Create your original function by 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 (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

Session information

## R version 4.5.2 (2025-10-31)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.3 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so;  LAPACK version 3.12.0
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        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: Etc/UTC
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats4    stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
##  [1] einsum_0.1.2              DelayedRandomArray_1.19.0
##  [3] HDF5Array_1.39.0          h5mread_1.3.0            
##  [5] rhdf5_2.55.7              DelayedArray_0.37.0      
##  [7] SparseArray_1.11.1        S4Arrays_1.11.0          
##  [9] abind_1.4-8               IRanges_2.45.0           
## [11] S4Vectors_0.49.0          MatrixGenerics_1.23.0    
## [13] matrixStats_1.5.0         BiocGenerics_0.56.0      
## [15] generics_0.1.4            Matrix_1.7-4             
## [17] DelayedTensor_1.16.0      BiocStyle_2.38.0         
## 
## loaded via a namespace (and not attached):
##  [1] dqrng_0.4.1         sass_0.4.10         lattice_0.22-7     
##  [4] digest_0.6.37       evaluate_1.0.5      grid_4.5.2         
##  [7] fastmap_1.2.0       jsonlite_2.0.0      BiocManager_1.30.26
## [10] codetools_0.2-20    jquerylib_0.1.4     cli_3.6.5          
## [13] rlang_1.1.6         XVector_0.51.0      cachem_1.1.0       
## [16] yaml_2.3.10         tools_4.5.2         beachmat_2.26.0    
## [19] parallel_4.5.2      BiocParallel_1.44.0 Rhdf5lib_1.33.0    
## [22] rsvd_1.0.5          buildtools_1.0.0    R6_2.6.1           
## [25] lifecycle_1.0.4     BiocSingular_1.26.0 irlba_2.3.5.1      
## [28] ScaledMatrix_1.19.0 rTensor_1.4.9       bslib_0.9.0        
## [31] Rcpp_1.1.0          xfun_0.54           sys_3.4.3          
## [34] knitr_1.50          rhdf5filters_1.23.0 htmltools_0.5.8.1  
## [37] rmarkdown_2.30      maketools_1.3.2     compiler_4.5.2