4. Einsum operation by DelayedTensor

Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2025-01-28 06:55:06.175188
Compiled: Tue Jan 28 06:59:12 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.4615998 0.3144546 0.7472242
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.4615998 0.3144546 0.7472242
einsum::einsum('iii->i', arrD)
## [1] 0.9274726 0.4752893 0.1097551
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.9274726 0.4752893 0.1097551

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.972295103 0.002269572 0.062274167
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
##         [1]         [2]         [3] 
## 0.972295103 0.002269572 0.062274167
einsum::einsum('ij,ij->ij', arrC, arrC)
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.4149197 0.7481466 0.03079062 0.0491062
## [2,] 0.9659166 0.6292829 0.72591430 0.1322741
## [3,] 0.5247631 0.3603045 0.18467003 0.8994551
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.41491966 0.74814658 0.03079062 0.04910620
## [2,] 0.96591659 0.62928294 0.72591430 0.13227409
## [3,] 0.52476307 0.36030451 0.18467003 0.89945508
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
## 
##            [,1]       [,2]       [,3]      [,4]
## [1,] 0.01677605 0.71605798 0.01156123 0.8334554
## [2,] 0.01019461 0.79341906 0.19823885 0.3992617
## [3,] 0.21212178 0.02308378 0.56206412 0.2846645
## 
## , , 2
## 
##           [,1]         [,2]      [,3]       [,4]
## [1,] 0.4179661 0.0681950866 0.4010674 0.64124743
## [2,] 0.8972031 0.0730654516 0.2523817 0.07192112
## [3,] 0.7288865 0.0008793743 0.8625321 0.76543822
## 
## , , 3
## 
##            [,1]      [,2]       [,3]         [,4]
## [1,] 0.64829945 0.4141621 0.07852213 0.2401076829
## [2,] 0.01428771 0.7738564 0.39603425 0.0421963576
## [3,] 0.02170685 0.4541742 0.77734091 0.0007157633
## 
## , , 4
## 
##           [,1]       [,2]        [,3]       [,4]
## [1,] 0.4213464 0.06388082 0.167774911 0.04112558
## [2,] 0.4645855 0.03674445 0.001502846 0.01352253
## [3,] 0.2623712 0.95008780 0.297746086 0.45041634
## 
## , , 5
## 
##              [,1]       [,2]       [,3]      [,4]
## [1,] 0.4076489261 0.79221681 0.35594894 0.7787897
## [2,] 0.0005613624 0.08819822 0.09631747 0.1012754
## [3,] 0.6927909573 0.26560910 0.78209300 0.9830088
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.01677605 0.71605798 0.01156123 0.83345542
## [2,] 0.01019461 0.79341906 0.19823885 0.39926170
## [3,] 0.21212178 0.02308378 0.56206412 0.28466450
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.4179661387 0.0681950866 0.4010674194 0.6412474348
## [2,] 0.8972031384 0.0730654516 0.2523817066 0.0719211154
## [3,] 0.7288864903 0.0008793743 0.8625320869 0.7654382204
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.6482994489 0.4141621353 0.0785221316 0.2401076829
## [2,] 0.0142877116 0.7738563613 0.3960342530 0.0421963576
## [3,] 0.0217068461 0.4541742297 0.7773409092 0.0007157633
## 
## ,,4
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.421346450 0.063880818 0.167774911 0.041125583
## [2,] 0.464585504 0.036744449 0.001502846 0.013522532
## [3,] 0.262371215 0.950087803 0.297746086 0.450416336
## 
## ,,5
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.4076489261 0.7922168066 0.3559489353 0.7787896583
## [2,] 0.0005613624 0.0881982225 0.0963174706 0.1012754035
## [3,] 0.6927909573 0.2656091035 0.7820929959 0.9830087618

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.97229510 0.046975464 0.24606680
## [2,] 0.04697546 0.002269572 0.01188847
## [3,] 0.24606680 0.011888471 0.06227417
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
##             [,1]        [,2]        [,3]
## [1,] 0.972295103 0.046975464 0.246066795
## [2,] 0.046975464 0.002269572 0.011888471
## [3,] 0.246066795 0.011888471 0.062274167
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.08343088 0.11203098 0.02272762 0.02870205
## [2,] 0.12729596 0.10274668 0.11035385 0.04710665
## [3,] 0.09382670 0.07774629 0.05565998 0.12283851
## 
## , , 2, 1, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.06503802 0.08733306 0.01771718 0.02237451
## [2,] 0.09923277 0.08009553 0.08602565 0.03672169
## [3,] 0.07314202 0.06060663 0.04338939 0.09575800
## 
## , , 3, 1, 1
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.2966707 0.3983694 0.08081684 0.1020612
## [2,] 0.4526499 0.3653555 0.39240570 0.1675059
## [3,] 0.3336370 0.2764569 0.19792052 0.4367997
## 
## , , 1, 2, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5450748 0.7319264 0.1484853 0.1875177
## [2,] 0.8316563 0.6712697 0.7209693 0.3077595
## [3,] 0.6129933 0.5079359 0.3636405 0.8025347
## 
## , , 2, 2, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5737640 0.7704504 0.1563006 0.1973874
## [2,] 0.8754294 0.7066011 0.7589165 0.3239580
## [3,] 0.6452573 0.5346704 0.3827802 0.8447750
## 
## , , 3, 2, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.09786681 0.13141556 0.02666016 0.03366833
## [2,] 0.14932181 0.12052480 0.12944823 0.05525745
## [3,] 0.11006141 0.09119862 0.06529075 0.14409309
## 
## , , 1, 3, 1
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.06926025 0.09300267 0.01886737 0.02382705
## [2,] 0.10567491 0.08529529 0.09161039 0.03910565
## [3,] 0.07789036 0.06454118 0.04620620 0.10197455
## 
## , , 2, 3, 1
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.2867982 0.3851126 0.07812745 0.09866486
## [2,] 0.4375868 0.3531973 0.37934735 0.16193166
## [3,] 0.3225344 0.2672571 0.19133419 0.42226406
## 
## , , 3, 3, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4829197 0.6484646 0.1315534 0.1661350
## [2,] 0.7368223 0.5947246 0.6387569 0.2726656
## [3,] 0.5430934 0.4500158 0.3221745 0.7110214
## 
## , , 1, 4, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5880621 0.7896498 0.1601955 0.2023063
## [2,] 0.8972449 0.7242094 0.7778285 0.3320310
## [3,] 0.6613370 0.5479943 0.3923190 0.8658266
## 
## , , 2, 4, 1
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4070154 0.5465403 0.1108761 0.1400222
## [2,] 0.6210101 0.5012470 0.5383584 0.2298086
## [3,] 0.4577311 0.3792833 0.2715358 0.5992645
## 
## , , 3, 4, 1
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.3436756 0.4614876 0.09362157 0.1182319
## [2,] 0.5243683 0.4232429 0.45457896 0.1940457
## [3,] 0.3864989 0.3202591 0.22927931 0.5060068
## 
## , , 1, 1, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4164401 0.5591958 0.1134435 0.1432645
## [2,] 0.6353900 0.5128537 0.5508245 0.2351299
## [3,] 0.4683302 0.3880658 0.2778234 0.6131409
## 
## , , 2, 1, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.6101371 0.8192920 0.1662090 0.2099005
## [2,] 0.9309261 0.7513951 0.8070270 0.3444949
## [3,] 0.6861626 0.5685652 0.4070461 0.8983284
## 
## , , 3, 1, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5499358 0.7384537 0.1498094 0.1891900
## [2,] 0.8390730 0.6772561 0.7273989 0.3105041
## [3,] 0.6184600 0.5124657 0.3668835 0.8096917
## 
## , , 1, 2, 2
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.1682126 0.2258759 0.04582324 0.05786883
## [2,] 0.2566530 0.2071570 0.22249447 0.09497601
## [3,] 0.1891726 0.1567514 0.11222116 0.24766594
## 
## , , 2, 2, 2
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.1741157 0.2338026 0.04743133 0.05989964
## [2,] 0.2656598 0.2144268 0.23030253 0.09830904
## [3,] 0.1958113 0.1622523 0.11615937 0.25635735
## 
## , , 3, 2, 2
## 
##            [,1]       [,2]        [,3]        [,4]
## [1,] 0.01910156 0.02564958 0.005203507 0.006571357
## [2,] 0.02914451 0.02352393 0.025265597 0.010785103
## [3,] 0.02148169 0.01780007 0.012743394 0.028123970
## 
## , , 1, 3, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4079347 0.5477748 0.1111266 0.1403385
## [2,] 0.6224128 0.5023792 0.5395744 0.2303277
## [3,] 0.4587650 0.3801400 0.2721491 0.6006181
## 
## , , 2, 3, 2
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.3236018 0.4345325 0.08815322 0.1113261
## [2,] 0.4937405 0.3985216 0.42802744 0.1827117
## [3,] 0.3639239 0.3015531 0.21588732 0.4764515
## 
## , , 3, 3, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5982320 0.8033059 0.1629660 0.2058049
## [2,] 0.9127618 0.7367338 0.7912802 0.3377731
## [3,] 0.6727741 0.5574713 0.3991038 0.8808001
## 
## , , 1, 4, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5158160 0.6926378 0.1405148 0.1774520
## [2,] 0.7870143 0.6352370 0.6822688 0.2912395
## [3,] 0.5800888 0.4806707 0.3441209 0.7594559
## 
## , , 2, 4, 2
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.1727469 0.2319645 0.04705843 0.05942872
## [2,] 0.2635712 0.2127410 0.22849194 0.09753615
## [3,] 0.1942718 0.1609767 0.11524615 0.25434192
## 
## , , 3, 4, 2
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5635560 0.7567430 0.1535198 0.1938756
## [2,] 0.8598543 0.6940297 0.7454143 0.3181944
## [3,] 0.6337773 0.5251579 0.3759701 0.8297453
## 
## , , 1, 1, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5186446 0.6964359 0.1412853 0.1784251
## [2,] 0.7913300 0.6387204 0.6860101 0.2928365
## [3,] 0.5832698 0.4833065 0.3460079 0.7636205
## 
## , , 2, 1, 3
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.07699515 0.10338908 0.02097445 0.02648802
## [2,] 0.11747654 0.09482095 0.10184132 0.04347291
## [3,] 0.08658905 0.07174906 0.05136645 0.11336293
## 
## , , 3, 1, 3
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.09490309 0.12743588 0.02585280 0.03264875
## [2,] 0.14479987 0.11687492 0.12552812 0.05358408
## [3,] 0.10672840 0.08843684 0.06331354 0.13972950
## 
## , , 1, 2, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4145407 0.5566453 0.1129261 0.1426111
## [2,] 0.6324920 0.5105146 0.5483122 0.2340575
## [3,] 0.4661942 0.3862959 0.2765562 0.6103444
## 
## , , 2, 2, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5666465 0.7608929 0.1543617 0.1949388
## [2,] 0.8645697 0.6978357 0.7495021 0.3199393
## [3,] 0.6372529 0.5280378 0.3780318 0.8342955
## 
## , , 3, 2, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4341035 0.5829141 0.1182553 0.1493411
## [2,] 0.6623401 0.5346065 0.5741877 0.2451030
## [3,] 0.4881945 0.4045257 0.2896073 0.6391473
## 
## , , 1, 3, 3
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.1805003 0.2423759 0.04917057 0.06209608
## [2,] 0.2754012 0.2222895 0.23874744 0.10191390
## [3,] 0.2029914 0.1682019 0.12041879 0.26575765
## 
## , , 2, 3, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4053670 0.5443268 0.1104271 0.1394551
## [2,] 0.6184950 0.4992170 0.5361781 0.2288779
## [3,] 0.4558773 0.3777472 0.2704360 0.5968375
## 
## , , 3, 3, 3
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5679208 0.7626041 0.1547088 0.1953772
## [2,] 0.8665140 0.6994050 0.7511876 0.3206588
## [3,] 0.6386860 0.5292253 0.3788820 0.8361718
## 
## , , 1, 4, 3
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.3156349 0.4238346 0.08598294 0.1085853
## [2,] 0.4815849 0.3887103 0.41748964 0.1782134
## [3,] 0.3549643 0.2941290 0.21057230 0.4647215
## 
## , , 2, 4, 3
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.1323182 0.1776768 0.03604514 0.04552035
## [2,] 0.2018865 0.1629523 0.17501697 0.07470934
## [3,] 0.1488055 0.1233026 0.08827459 0.19481717
## 
## , , 3, 4, 3
## 
##            [,1]       [,2]       [,3]        [,4]
## [1,] 0.01723323 0.02314078 0.00469455 0.005928610
## [2,] 0.02629387 0.02122305 0.02279436 0.009730208
## [3,] 0.01938056 0.01605904 0.01149696 0.025373154
## 
## , , 1, 1, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4181207 0.5614525 0.1139014 0.1438427
## [2,] 0.6379542 0.5149234 0.5530474 0.2360788
## [3,] 0.4702202 0.3896319 0.2789445 0.6156153
## 
## , , 2, 1, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4390509 0.5895575 0.1196030 0.1510431
## [2,] 0.6698887 0.5406993 0.5807317 0.2478964
## [3,] 0.4937584 0.4091360 0.2929079 0.6464316
## 
## , , 3, 1, 4
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.3299439 0.4430487 0.08988088 0.1135079
## [2,] 0.5034170 0.4063320 0.43641610 0.1862926
## [3,] 0.3710562 0.3074631 0.22011837 0.4857892
## 
## , , 1, 2, 4
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.1628048 0.2186143 0.04435009 0.05600843
## [2,] 0.2484020 0.2004972 0.21534159 0.09192267
## [3,] 0.1830909 0.1517121 0.10861341 0.23970383
## 
## , , 2, 2, 4
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.1234747 0.1658018 0.03363606 0.04247800
## [2,] 0.1883934 0.1520614 0.16331969 0.06971613
## [3,] 0.1388601 0.1150617 0.08237474 0.18179654
## 
## , , 3, 2, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.6278615 0.8430925 0.1710374 0.2159981
## [2,] 0.9579695 0.7732232 0.8304711 0.3545025
## [3,] 0.7060956 0.5850820 0.4188708 0.9244248
## 
## , , 1, 3, 4
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.2638430 0.3542883 0.07187416 0.09076777
## [2,] 0.4025625 0.3249275 0.34898454 0.14897072
## [3,] 0.2967189 0.2458659 0.17601988 0.38846621
## 
## , , 2, 3, 4
## 
##            [,1]       [,2]        [,3]        [,4]
## [1,] 0.02497119 0.03353131 0.006802467 0.008590637
## [2,] 0.03810018 0.03075248 0.033029339 0.014099204
## [3,] 0.02808270 0.02326977 0.016659249 0.036766048
## 
## , , 3, 3, 4
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.3514836 0.4719722 0.09574857 0.1209181
## [2,] 0.5362815 0.4328586 0.46490659 0.1984543
## [3,] 0.3952798 0.3275351 0.23448833 0.5175029
## 
## , , 1, 4, 4
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.1306285 0.1754080 0.03558486 0.04493908
## [2,] 0.1993085 0.1608715 0.17278208 0.07375533
## [3,] 0.1469054 0.1217281 0.08714736 0.19232944
## 
## , , 2, 4, 4
## 
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.07490504 0.10058248 0.02040508 0.02576898
## [2,] 0.11428752 0.09224695 0.09907674 0.04229280
## [3,] 0.08423850 0.06980136 0.04997206 0.11028559
## 
## , , 3, 4, 4
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4323038 0.5804976 0.1177650 0.1487220
## [2,] 0.6595943 0.5323902 0.5718074 0.2440869
## [3,] 0.4861706 0.4028487 0.2884067 0.6364977
## 
## , , 1, 1, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.4112682 0.5522510 0.1120347 0.1414853
## [2,] 0.6274989 0.5064845 0.5439836 0.2322098
## [3,] 0.4625139 0.3832463 0.2743730 0.6055261
## 
## , , 2, 1, 5
## 
##            [,1]       [,2]        [,3]        [,4]
## [1,] 0.01526173 0.02049345 0.004157487 0.005250369
## [2,] 0.02328582 0.01879510 0.020186654 0.008617059
## [3,] 0.01716340 0.01422186 0.010181690 0.022470431
## 
## , , 3, 1, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5361461 0.7199369 0.1460530 0.1844460
## [2,] 0.8180332 0.6602738 0.7091593 0.3027182
## [3,] 0.6029520 0.4996156 0.3576838 0.7893886
## 
## , , 1, 2, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5733292 0.7698664 0.1561821 0.1972378
## [2,] 0.8747659 0.7060655 0.7583413 0.3237125
## [3,] 0.6447683 0.5342652 0.3824901 0.8441347
## 
## , , 2, 2, 5
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.1912987 0.2568758 0.05211217 0.06581094
## [2,] 0.2918769 0.2355879 0.25303034 0.10801083
## [3,] 0.2151352 0.1782645 0.12762276 0.28165642
## 
## , , 3, 2, 5
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.3319736 0.4457741 0.09043379 0.1142062
## [2,] 0.5065138 0.4088316 0.43910072 0.1874385
## [3,] 0.3733388 0.3093544 0.22147244 0.4887775
## 
## , , 1, 3, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.3843048 0.5160445 0.1046895 0.1322093
## [2,] 0.5863591 0.4732786 0.5083192 0.2169858
## [3,] 0.4321908 0.3581201 0.2563847 0.5658269
## 
## , , 2, 3, 5
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.1999100 0.2684392 0.05445801 0.06877343
## [2,] 0.3050158 0.2461929 0.26442055 0.11287297
## [3,] 0.2248196 0.1862891 0.13336772 0.29433525
## 
## , , 3, 3, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5696541 0.7649315 0.1551810 0.1959735
## [2,] 0.8691586 0.7015396 0.7534803 0.3216374
## [3,] 0.6406352 0.5308405 0.3800383 0.8387237
## 
## , , 1, 4, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5684498 0.7633144 0.1548529 0.1955592
## [2,] 0.8673211 0.7000565 0.7518873 0.3209575
## [3,] 0.6392809 0.5297183 0.3792349 0.8369506
## 
## , , 2, 4, 5
## 
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.2049906 0.2752614 0.05584204 0.07052127
## [2,] 0.3127676 0.2524498 0.27114067 0.11574158
## [3,] 0.2305333 0.1910235 0.13675720 0.30181563
## 
## , , 3, 4, 5
## 
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.6386467 0.8575749 0.1739754 0.2197085
## [2,] 0.9744252 0.7865053 0.8447367 0.3605920
## [3,] 0.7182247 0.5951323 0.4260660 0.9403043
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.08343088 0.11203098 0.02272762 0.02870205
## [2,] 0.12729596 0.10274668 0.11035385 0.04710665
## [3,] 0.09382670 0.07774629 0.05565998 0.12283851
## 
## ,,2,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.06503802 0.08733306 0.01771718 0.02237451
## [2,] 0.09923277 0.08009553 0.08602565 0.03672169
## [3,] 0.07314202 0.06060663 0.04338939 0.09575800
## 
## ,,3,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.29667069 0.39836940 0.08081684 0.10206122
## [2,] 0.45264991 0.36535547 0.39240570 0.16750587
## [3,] 0.33363704 0.27645693 0.19792052 0.43679974
## 
## ...
## 
## ,,1,4,5
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5684498 0.7633144 0.1548529 0.1955592
## [2,] 0.8673211 0.7000565 0.7518873 0.3209575
## [3,] 0.6392809 0.5297183 0.3792349 0.8369506
## 
## ,,2,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.20499062 0.27526141 0.05584204 0.07052127
## [2,] 0.31276763 0.25244976 0.27114067 0.11574158
## [3,] 0.23053328 0.19102352 0.13675720 0.30181563
## 
## ,,3,4,5
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.6386467 0.8575749 0.1739754 0.2197085
## [2,] 0.9744252 0.7865053 0.8447367 0.3605920
## [3,] 0.7182247 0.5951323 0.4260660 0.9403043

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

Row-wise / Column-wise Summation

einsum::einsum('ij->i', arrC)
## [1] 1.906169 2.991785 2.702788
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
##      [1]      [2]      [3] 
## 1.906169 2.991785 2.702788
einsum::einsum('ij->j', arrC)
## [1] 2.351358 2.258482 1.457211 1.533690
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 2.351358 2.258482 1.457211 1.533690

Mode-wise Vectorization

einsum::einsum('ijk->i', arrE)
## [1] 11.078760  7.868455 12.239731
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 11.078760  7.868455 12.239731
einsum::einsum('ijk->j', arrE)
## [1] 7.547998 7.768728 7.943430 7.926790
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 7.547998 7.768728 7.943430 7.926790
einsum::einsum('ijk->k', arrE)
## [1] 5.960756 7.016817 5.782583 5.246341 7.180450
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4]      [5] 
## 5.960756 7.016817 5.782583 5.246341 7.180450

Mode-wise Summation

These are the same as what the modeSum function does.

einsum::einsum('ijk->ij', arrE)
##          [,1]     [,2]     [,3]     [,4]
## [1,] 2.868782 2.893710 2.027258 3.289010
## [2,] 1.873006 2.529408 1.926046 1.539995
## [3,] 2.806210 2.345609 3.990126 3.097785
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]     [,4]
## [1,] 2.868782 2.893710 2.027258 3.289010
## [2,] 1.873006 2.529408 1.926046 1.539995
## [3,] 2.806210 2.345609 3.990126 3.097785
einsum::einsum('ijk->jk', arrE)
##           [,1]      [,2]      [,3]      [,4]     [,5]
## [1,] 0.6910575 2.4474596 1.0720341 1.8429390 1.494508
## [2,] 1.8888763 0.5611024 2.1971699 1.4191594 1.702420
## [3,] 1.3024727 2.0644010 1.7911999 0.9940311 1.791325
## [4,] 2.0783490 1.9438537 0.7221791 0.9902115 2.192197
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.6910575 2.4474596 1.0720341 1.8429390 1.4945076
## [2,] 1.8888763 0.5611024 2.1971699 1.4191594 1.7024203
## [3,] 1.3024727 2.0644010 1.7911999 0.9940311 1.7913253
## [4,] 2.0783490 1.9438537 0.7221791 0.9902115 2.1921965
einsum::einsum('ijk->jk', arrE)
##           [,1]      [,2]      [,3]      [,4]     [,5]
## [1,] 0.6910575 2.4474596 1.0720341 1.8429390 1.494508
## [2,] 1.8888763 0.5611024 2.1971699 1.4191594 1.702420
## [3,] 1.3024727 2.0644010 1.7911999 0.9940311 1.791325
## [4,] 2.0783490 1.9438537 0.7221791 0.9902115 2.192197
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.6910575 2.4474596 1.0720341 1.8429390 1.4945076
## [2,] 1.8888763 0.5611024 2.1971699 1.4191594 1.7024203
## [3,] 1.3024727 2.0644010 1.7911999 0.9940311 1.7913253
## [4,] 2.0783490 1.9438537 0.7221791 0.9902115 2.1921965

Trace

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

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

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.4615998 0.01433942 0.5668152
## [2,] 0.9211690 0.31445457 0.9514725
## [3,] 0.5000174 0.90581565 0.7472242
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.46159983 0.01433942 0.56681523
## [2,] 0.92116905 0.31445457 0.95147253
## [3,] 0.50001736 0.90581565 0.74722424
einsum::einsum('ijk->jki', arrD)
## , , 1
## 
##           [,1]      [,2]         [,3]
## [1,] 0.9274726 0.4853761 0.2624130913
## [2,] 0.6025841 0.9448202 0.2747428440
## [3,] 0.3283763 0.4337656 0.0009992351
## 
## , , 2
## 
##           [,1]       [,2]      [,3]
## [1,] 0.4117461 0.04426783 0.5700048
## [2,] 0.2114417 0.47528926 0.2642661
## [3,] 0.4158258 0.31417236 0.9890670
## 
## , , 3
## 
##           [,1]       [,2]       [,3]
## [1,] 0.1552066 0.03341102 0.03402409
## [2,] 0.6960388 0.79539319 0.15250234
## [3,] 0.1557802 0.25458198 0.10975506
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
##              [,1]         [,2]         [,3]
## [1,] 0.9274725956 0.4853760747 0.2624130913
## [2,] 0.6025840670 0.9448201777 0.2747428440
## [3,] 0.3283763025 0.4337655918 0.0009992351
## 
## ,,2
##            [,1]       [,2]       [,3]
## [1,] 0.41174612 0.04426783 0.57000482
## [2,] 0.21144170 0.47528926 0.26426607
## [3,] 0.41582582 0.31417236 0.98906696
## 
## ,,3
##            [,1]       [,2]       [,3]
## [1,] 0.15520659 0.03341102 0.03402409
## [2,] 0.69603876 0.79539319 0.15250234
## [3,] 0.15578024 0.25458198 0.10975506

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

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.2390924 2.0440558 0.6842940 1.1483032 1.101001
## [2,] 1.5325608 0.1421399 1.6421927 1.0507131 1.146024
## [3,] 0.7718642 1.5159812 1.2518973 0.4670238 1.234359
## [4,] 1.5173816 1.4786068 0.2830198 0.5050645 1.863074
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.2390924 2.0440558 0.6842940 1.1483032 1.1010012
## [2,] 1.5325608 0.1421399 1.6421927 1.0507131 1.1460241
## [3,] 0.7718642 1.5159812 1.2518973 0.4670238 1.2343594
## [4,] 1.5173816 1.4786068 0.2830198 0.5050645 1.8630738

Matrix Multiplication

Matrix Multiplication is considered a contracted product.

einsum::einsum('ij,jk->ik', arrC, t(arrC))
##          [,1]     [,2]     [,3]
## [1,] 1.242963 1.549314 1.271383
## [2,] 1.549314 2.453388 1.899180
## [3,] 1.271383 1.899180 1.969193
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]
## [1,] 1.242963 1.549314 1.271383
## [2,] 1.549314 2.453388 1.899180
## [3,] 1.271383 1.899180 1.969193

Multiplication + Permutation

Some examples of combining Multiplication and Permutation are shown below.

einsum::einsum('ij,ij->ji', arrC, arrC)
##            [,1]      [,2]      [,3]
## [1,] 0.41491966 0.9659166 0.5247631
## [2,] 0.74814658 0.6292829 0.3603045
## [3,] 0.03079062 0.7259143 0.1846700
## [4,] 0.04910620 0.1322741 0.8994551
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.41491966 0.96591659 0.52476307
## [2,] 0.74814658 0.62928294 0.36030451
## [3,] 0.03079062 0.72591430 0.18467003
## [4,] 0.04910620 0.13227409 0.89945508
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
## 
##            [,1]       [,2]       [,3]       [,4]      [,5]
## [1,] 0.01677605 0.41796614 0.64829945 0.42134645 0.4076489
## [2,] 0.71605798 0.06819509 0.41416214 0.06388082 0.7922168
## [3,] 0.01156123 0.40106742 0.07852213 0.16777491 0.3559489
## [4,] 0.83345542 0.64124743 0.24010768 0.04112558 0.7787897
## 
## , , 2
## 
##            [,1]       [,2]       [,3]        [,4]         [,5]
## [1,] 0.01019461 0.89720314 0.01428771 0.464585504 0.0005613624
## [2,] 0.79341906 0.07306545 0.77385636 0.036744449 0.0881982225
## [3,] 0.19823885 0.25238171 0.39603425 0.001502846 0.0963174706
## [4,] 0.39926170 0.07192112 0.04219636 0.013522532 0.1012754035
## 
## , , 3
## 
##            [,1]         [,2]         [,3]      [,4]      [,5]
## [1,] 0.21212178 0.7288864903 0.0217068461 0.2623712 0.6927910
## [2,] 0.02308378 0.0008793743 0.4541742297 0.9500878 0.2656091
## [3,] 0.56206412 0.8625320869 0.7773409092 0.2977461 0.7820930
## [4,] 0.28466450 0.7654382204 0.0007157633 0.4504163 0.9830088
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.01677605 0.41796614 0.64829945 0.42134645 0.40764893
## [2,] 0.71605798 0.06819509 0.41416214 0.06388082 0.79221681
## [3,] 0.01156123 0.40106742 0.07852213 0.16777491 0.35594894
## [4,] 0.83345542 0.64124743 0.24010768 0.04112558 0.77878966
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.0101946100 0.8972031384 0.0142877116 0.4645855037 0.0005613624
## [2,] 0.7934190636 0.0730654516 0.7738563613 0.0367444489 0.0881982225
## [3,] 0.1982388451 0.2523817066 0.3960342530 0.0015028458 0.0963174706
## [4,] 0.3992617050 0.0719211154 0.0421963576 0.0135225323 0.1012754035
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.2121217778 0.7288864903 0.0217068461 0.2623712145 0.6927909573
## [2,] 0.0230837762 0.0008793743 0.4541742297 0.9500878032 0.2656091035
## [3,] 0.5620641235 0.8625320869 0.7773409092 0.2977460858 0.7820929959
## [4,] 0.2846644993 0.7654382204 0.0007157633 0.4504163359 0.9830087618

Summation + Permutation

Some examples of combining Summation and Permutation are shown below.

einsum::einsum('ijk->ki', arrE)
##          [,1]      [,2]     [,3]
## [1,] 1.996185 2.0688209 1.895749
## [2,] 2.341723 1.9880715 2.687022
## [3,] 2.218951 1.8339523 1.729680
## [4,] 1.514256 1.0283465 2.703738
## [5,] 3.007645 0.9492635 3.223542
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 1.9961854 2.0688209 1.8957492
## [2,] 2.3417230 1.9880715 2.6870221
## [3,] 2.2189506 1.8339523 1.7296801
## [4,] 1.5142564 1.0283465 2.7037381
## [5,] 3.0076447 0.9492635 3.2235416

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.0068636112 0.17100317 0.265239813 0.172386162 0.16678207
## [2,] 0.5282432191 0.05030821 0.305531599 0.047125526 0.58442636
## [3,] 0.0003510118 0.01217685 0.002384019 0.005093831 0.01080700
## [4,] 0.0403568924 0.03104996 0.011626297 0.001991349 0.03770991
## 
## , , 2
## 
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.0004691182 0.0412859647 0.0006574676 2.137850e-02 2.583182e-05
## [2,] 0.0237859564 0.0021904334 0.0231994850 1.101564e-03 2.644100e-03
## [3,] 0.0068556106 0.0087280104 0.0136958859 5.197228e-05 3.330907e-03
## [4,] 0.0025159644 0.0004532139 0.0002659021 8.521280e-05 6.381912e-04
## 
## , , 3
## 
##             [,1]         [,2]         [,3]       [,4]       [,5]
## [1,] 0.027778097 0.0954502626 0.0028425882 0.03435844 0.09072342
## [2,] 0.002075537 0.0000790674 0.0408362777 0.08542548 0.02388178
## [3,] 0.025902175 0.0397489474 0.0358229953 0.01372134 0.03604199
## [4,] 0.063894982 0.1718080807 0.0001606582 0.10109917 0.22064334
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.0068636112 0.1710031695 0.2652398132 0.1723861617 0.1667820715
## [2,] 0.5282432191 0.0503082055 0.3055315988 0.0471255260 0.5844263559
## [3,] 0.0003510118 0.0121768491 0.0023840185 0.0050938313 0.0108070022
## [4,] 0.0403568924 0.0310499555 0.0116262966 0.0019913491 0.0377099118
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 4.691182e-04 4.128596e-02 6.574676e-04 2.137850e-02 2.583182e-05
## [2,] 2.378596e-02 2.190433e-03 2.319948e-02 1.101564e-03 2.644100e-03
## [3,] 6.855611e-03 8.728010e-03 1.369589e-02 5.197228e-05 3.330907e-03
## [4,] 2.515964e-03 4.532139e-04 2.659021e-04 8.521280e-05 6.381912e-04
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.0277780967 0.0954502626 0.0028425882 0.0343584380 0.0907234249
## [2,] 0.0020755372 0.0000790674 0.0408362777 0.0854254752 0.0238817758
## [3,] 0.0259021753 0.0397489474 0.0358229953 0.0137213371 0.0360419906
## [4,] 0.0638949819 0.1718080807 0.0001606582 0.1010991667 0.2206433442

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.4.2 (2024-10-31)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.1 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.15.0
##  [3] HDF5Array_1.35.8          h5mread_0.99.4           
##  [5] rhdf5_2.51.2              DelayedArray_0.33.4      
##  [7] SparseArray_1.7.4         S4Arrays_1.7.1           
##  [9] abind_1.4-8               IRanges_2.41.2           
## [11] S4Vectors_0.45.2          MatrixGenerics_1.19.1    
## [13] matrixStats_1.5.0         BiocGenerics_0.53.5      
## [15] generics_0.1.3            Matrix_1.7-2             
## [17] DelayedTensor_1.13.0      BiocStyle_2.35.0         
## 
## loaded via a namespace (and not attached):
##  [1] dqrng_0.4.1         sass_0.4.9          lattice_0.22-6     
##  [4] digest_0.6.37       evaluate_1.0.3      grid_4.4.2         
##  [7] fastmap_1.2.0       jsonlite_1.8.9      BiocManager_1.30.25
## [10] codetools_0.2-20    jquerylib_0.1.4     cli_3.6.3          
## [13] rlang_1.1.5         crayon_1.5.3        XVector_0.47.2     
## [16] cachem_1.1.0        yaml_2.3.10         tools_4.4.2        
## [19] beachmat_2.23.6     parallel_4.4.2      BiocParallel_1.41.0
## [22] Rhdf5lib_1.29.0     rsvd_1.0.5          buildtools_1.0.0   
## [25] R6_2.5.1            lifecycle_1.0.4     BiocSingular_1.23.0
## [28] irlba_2.3.5.1       ScaledMatrix_1.15.0 rTensor_1.4.8      
## [31] bslib_0.8.0         Rcpp_1.0.14         xfun_0.50          
## [34] sys_3.4.3           knitr_1.49          rhdf5filters_1.19.0
## [37] htmltools_0.5.8.1   rmarkdown_2.29      maketools_1.3.1    
## [40] compiler_4.4.2