Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2025-10-29 20:12:10
Compiled: Wed Oct 29 23:28:23 2025

1 What is einsum

einsum 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)

2 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.

3 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 https://ajcr.net/Basic-guide-to-einsum/.

  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)

3.1 No Operation

3.1.2 diag

We can also extract the diagonal elements as follows.

einsum::einsum('ii->i', arrB)
## [1] 0.5918220 0.9588781 0.4800987
DelayedTensor::einsum('ii->i', darrB)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.5918220 0.9588781 0.4800987
einsum::einsum('iii->i', arrD)
## [1] 0.2493323 0.1578544 0.5764090
DelayedTensor::einsum('iii->i', darrD)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 0.2493323 0.1578544 0.5764090

3.2 Multiplication

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

3.2.1 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.50048771 0.36687035 0.05907041
DelayedTensor::einsum('i,i->i', darrA, darrA)
## <3> HDF5Array object of type "double":
##        [1]        [2]        [3] 
## 0.50048771 0.36687035 0.05907041
einsum::einsum('ij,ij->ij', arrC, arrC)
##           [,1]        [,2]        [,3]      [,4]
## [1,] 0.6197434 0.006407316 0.584879160 0.2559719
## [2,] 0.8504382 0.302717719 0.001014908 0.4467360
## [3,] 0.6911256 0.761120910 0.966880137 0.5079740
DelayedTensor::einsum('ij,ij->ij', darrC, darrC)
## <3 x 4> HDF5Matrix object of type "double":
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.619743446 0.006407316 0.584879160 0.255971905
## [2,] 0.850438193 0.302717719 0.001014908 0.446735966
## [3,] 0.691125619 0.761120910 0.966880137 0.507973999
einsum::einsum('ijk,ijk->ijk', arrE, arrE)
## , , 1
## 
##              [,1]        [,2]       [,3]       [,4]
## [1,] 0.1325990451 0.086803497 0.00528532 0.34610492
## [2,] 0.0004614392 0.001894087 0.31205156 0.61266986
## [3,] 0.3586953670 0.292455375 0.03999078 0.01241495
## 
## , , 2
## 
##           [,1]        [,2]      [,3]         [,4]
## [1,] 0.4822358 0.873288277 0.2671087 0.2806724726
## [2,] 0.5494391 0.313444227 0.1986245 0.9780623864
## [3,] 0.6276151 0.004683494 0.1686702 0.0003422766
## 
## , , 3
## 
##           [,1]       [,2]      [,3]         [,4]
## [1,] 0.7839067 0.02278322 0.6875588 0.0003327065
## [2,] 0.5602012 0.70917208 0.5250269 0.0293474174
## [3,] 0.2055558 0.08149727 0.5694897 0.2396271951
## 
## , , 4
## 
##           [,1]      [,2]         [,3]       [,4]
## [1,] 0.1684820 0.8531403 1.326808e-05 0.63943614
## [2,] 0.4362619 0.2911744 2.994258e-01 0.04354019
## [3,] 0.7844897 0.2154536 1.433522e-01 0.73040501
## 
## , , 5
## 
##           [,1]      [,2]        [,3]       [,4]
## [1,] 0.4813377 0.8261061 0.240218125 0.01636903
## [2,] 0.1558870 0.9270001 0.558495016 0.22365814
## [3,] 0.5359284 0.8287563 0.004274978 0.56190603
DelayedTensor::einsum('ijk,ijk->ijk', darrE, darrE)
## <3 x 4 x 5> HDF5Array object of type "double":
## ,,1
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.1325990451 0.0868034967 0.0052853198 0.3461049227
## [2,] 0.0004614392 0.0018940869 0.3120515558 0.6126698567
## [3,] 0.3586953670 0.2924553753 0.0399907768 0.0124149469
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.4822358307 0.8732882769 0.2671087079 0.2806724726
## [2,] 0.5494390518 0.3134442266 0.1986244568 0.9780623864
## [3,] 0.6276151376 0.0046834944 0.1686701985 0.0003422766
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.7839066550 0.0227832194 0.6875587655 0.0003327065
## [2,] 0.5602011715 0.7091720835 0.5250268719 0.0293474174
## [3,] 0.2055557848 0.0814972688 0.5694896839 0.2396271951
## 
## ,,4
##              [,1]         [,2]         [,3]         [,4]
## [1,] 1.684820e-01 8.531403e-01 1.326808e-05 6.394361e-01
## [2,] 4.362619e-01 2.911744e-01 2.994258e-01 4.354019e-02
## [3,] 7.844897e-01 2.154536e-01 1.433522e-01 7.304050e-01
## 
## ,,5
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.481337743 0.826106090 0.240218125 0.016369032
## [2,] 0.155886978 0.927000098 0.558495016 0.223658141
## [3,] 0.535928425 0.828756265 0.004274978 0.561906035

3.2.2 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.5004877 0.4285022 0.17194190
## [2,] 0.4285022 0.3668703 0.14721136
## [3,] 0.1719419 0.1472114 0.05907041
DelayedTensor::einsum('i,j->ij', darrA, darrA)
## <3 x 3> HDF5Matrix object of type "double":
##            [,1]       [,2]       [,3]
## [1,] 0.50048771 0.42850216 0.17194190
## [2,] 0.42850216 0.36687035 0.14721136
## [3,] 0.17194190 0.14721136 0.05907041
einsum::einsum('ij,klm->ijklm', arrC, arrE)
## , , 1, 1, 1
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.2866660 0.02914797 0.27848594 0.1842325
## [2,] 0.3358084 0.20034990 0.01160068 0.2433860
## [3,] 0.3027253 0.31768523 0.35806059 0.2595320
## 
## , , 2, 1, 1
## 
##            [,1]        [,2]         [,3]       [,4]
## [1,] 0.01691076 0.001719473 0.0164282134 0.01086809
## [2,] 0.01980973 0.011818876 0.0006843378 0.01435763
## [3,] 0.01785812 0.018740626 0.0211224154 0.01531010
## 
## , , 3, 1, 1
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.4714861 0.04794032 0.4580321 0.3030114
## [2,] 0.5523117 0.32952002 0.0190799 0.4003025
## [3,] 0.4978991 0.52250411 0.5889104 0.4268582
## 
## , , 1, 2, 1
## 
##           [,1]       [,2]        [,3]      [,4]
## [1,] 0.2319394 0.02358342 0.225321007 0.1490613
## [2,] 0.2717002 0.16210169 0.009386029 0.1969219
## [3,] 0.2449329 0.25703688 0.289704292 0.2099855
## 
## , , 2, 2, 1
## 
##            [,1]        [,2]        [,3]       [,4]
## [1,] 0.03426146 0.003483678 0.033283809 0.02201892
## [2,] 0.04013482 0.023945222 0.001386479 0.02908877
## [3,] 0.03618082 0.037968792 0.042794334 0.03101849
## 
## , , 3, 2, 1
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.4257315 0.04328804 0.41358319 0.2736062
## [2,] 0.4987136 0.29754231 0.01722833 0.3614559
## [3,] 0.4495814 0.47179858 0.53176056 0.3854345
## 
## , , 1, 3, 1
## 
##            [,1]       [,2]        [,3]       [,4]
## [1,] 0.05723235 0.00581934 0.055599221 0.03678170
## [2,] 0.06704355 0.03999950 0.002316055 0.04859159
## [3,] 0.06043856 0.06342529 0.071486157 0.05181510
## 
## , , 2, 3, 1
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.4397635 0.0447148 0.42721476 0.2826242
## [2,] 0.5151510 0.3073492 0.01779617 0.3733693
## [3,] 0.4643994 0.4873489 0.54928722 0.3981383
## 
## , , 3, 3, 1
## 
##           [,1]      [,2]        [,3]      [,4]
## [1,] 0.1574294 0.0160073 0.152937150 0.1011757
## [2,] 0.1844171 0.1100269 0.006370789 0.1336612
## [3,] 0.1662488 0.1744644 0.196637453 0.1425282
## 
## , , 1, 4, 1
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.4631374 0.04709144 0.44992172 0.2976460
## [2,] 0.5425319 0.32368518 0.01874205 0.3932143
## [3,] 0.4890828 0.51325208 0.57848248 0.4192998
## 
## , , 2, 4, 1
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.6161965 0.06265437 0.59861326 0.3960130
## [2,] 0.7218295 0.43065766 0.02493599 0.5231650
## [3,] 0.6507164 0.68287322 0.76966117 0.5578713
## 
## , , 3, 4, 1
## 
##            [,1]        [,2]        [,3]       [,4]
## [1,] 0.08771592 0.008918884 0.085212932 0.05637267
## [2,] 0.10275283 0.061304359 0.003549651 0.07447284
## [3,] 0.09262984 0.097207385 0.109561698 0.07941329
## 
## , , 1, 1, 2
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.5466832 0.05558631 0.53108350 0.3513386
## [2,] 0.6403997 0.38207503 0.02212295 0.4641466
## [3,] 0.5773089 0.60583808 0.68283545 0.4949376
## 
## , , 2, 1, 2
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.5835334 0.05933321 0.56688222 0.3750213
## [2,] 0.6835671 0.40782954 0.02361419 0.4954333
## [3,] 0.6162235 0.64667577 0.72886330 0.5282999
## 
## , , 3, 1, 2
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.6236669 0.06341395 0.60587046 0.4008140
## [2,] 0.7305805 0.43587868 0.02523829 0.5295076
## [3,] 0.6586053 0.69115194 0.77899205 0.5646345
## 
## , , 1, 2, 2
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.7356729 0.07480264 0.71468043 0.4727973
## [2,] 0.8617875 0.51415935 0.02977091 0.6246033
## [3,] 0.7768860 0.81527785 0.91889340 0.6660388
## 
## , , 2, 2, 2
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.4407437 0.04481447 0.42816702 0.2832542
## [2,] 0.5162993 0.30803429 0.01783583 0.3742016
## [3,] 0.4654346 0.48843521 0.55051158 0.3990257
## 
## , , 3, 2, 2
## 
##            [,1]        [,2]       [,3]       [,4]
## [1,] 0.05387546 0.005478013 0.05233812 0.03462431
## [2,] 0.06311119 0.037653376 0.00218021 0.04574151
## [3,] 0.05689361 0.059705155 0.06729322 0.04877595
## 
## , , 1, 3, 2
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.4068647 0.04136967 0.39525475 0.2614810
## [2,] 0.4766125 0.28435636 0.01646483 0.3454375
## [3,] 0.4296576 0.45089026 0.50819495 0.3683535
## 
## , , 2, 3, 2
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.3508507 0.03567422 0.34083912 0.2254823
## [2,] 0.4109961 0.24520837 0.01419808 0.2978803
## [3,] 0.3705057 0.38881516 0.43823058 0.3176414
## 
## , , 3, 3, 2
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.3233145 0.03287436 0.31408866 0.2077855
## [2,] 0.3787395 0.22596340 0.01308376 0.2745014
## [3,] 0.3414269 0.35829934 0.40383643 0.2927116
## 
## , , 1, 4, 2
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.4170671 0.04240704 0.4051660 0.2680378
## [2,] 0.4885638 0.29148676 0.0168777 0.3540995
## [3,] 0.4404315 0.46219659 0.5209382 0.3775901
## 
## , , 2, 4, 2
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.7785549 0.07916284 0.75633875 0.5003564
## [2,] 0.9120206 0.54412941 0.03150624 0.6610111
## [3,] 0.8221703 0.86279994 0.97245519 0.7048619
## 
## , , 3, 4, 2
## 
##            [,1]        [,2]         [,3]        [,4]
## [1,] 0.01456447 0.001480903 0.0141488665 0.009360191
## [2,] 0.01706121 0.010179056 0.0005893888 0.012365567
## [3,] 0.01538038 0.016140441 0.0181917673 0.013185886
## 
## , , 1, 1, 3
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.6970086 0.07087128 0.67711939 0.4479487
## [2,] 0.8164950 0.48713698 0.02820626 0.5917764
## [3,] 0.7360557 0.77242977 0.87059966 0.6310342
## 
## , , 2, 1, 3
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.5892207 0.05991149 0.57240719 0.3786763
## [2,] 0.6902293 0.41180435 0.02384434 0.5002619
## [3,] 0.6222294 0.65297843 0.73596697 0.5334488
## 
## , , 3, 1, 3
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.3569199 0.03629133 0.34673519 0.2293829
## [2,] 0.4181058 0.24945015 0.01444369 0.3030333
## [3,] 0.3769149 0.39554116 0.44581140 0.3231362
## 
## , , 1, 2, 3
## 
##           [,1]       [,2]        [,3]       [,4]
## [1,] 0.1188266 0.01208219 0.115435827 0.07636664
## [2,] 0.1391967 0.08304748 0.004808624 0.10088649
## [3,] 0.1254833 0.13168441 0.148420491 0.10757919
## 
## , , 2, 2, 3
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.6629515 0.06740838 0.64403414 0.4260612
## [2,] 0.7765997 0.46333460 0.02682805 0.5628612
## [3,] 0.7000907 0.73468749 0.82806063 0.6002008
## 
## , , 3, 2, 3
## 
##           [,1]       [,2]        [,3]      [,4]
## [1,] 0.2247385 0.02285123 0.218325569 0.1444334
## [2,] 0.2632649 0.15706899 0.009094625 0.1908082
## [3,] 0.2373286 0.24905677 0.280709976 0.2034662
## 
## , , 1, 3, 3
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.6527710 0.06637324 0.63414414 0.4195184
## [2,] 0.7646739 0.45621949 0.02641607 0.5542177
## [3,] 0.6893399 0.72340539 0.81534466 0.5909839
## 
## , , 2, 3, 3
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.5704226 0.05800011 0.55414554 0.3665953
## [2,] 0.6682087 0.39866645 0.02308363 0.4843020
## [3,] 0.6023782 0.63214629 0.71248723 0.5164301
## 
## , , 3, 3, 3
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.5940854 0.06040613 0.5771331 0.3818028
## [2,] 0.6959280 0.41520431 0.0240412 0.5043922
## [3,] 0.6273666 0.65836958 0.7420433 0.5378531
## 
## , , 1, 4, 3
## 
##            [,1]        [,2]         [,3]        [,4]
## [1,] 0.01435941 0.001460053 0.0139496626 0.009228408
## [2,] 0.01682101 0.010035743 0.0005810907 0.012191470
## [3,] 0.01516384 0.015913198 0.0179356429 0.013000240
## 
## , , 2, 4, 3
## 
##           [,1]       [,2]        [,3]       [,4]
## [1,] 0.1348624 0.01371270 0.131014094 0.08667245
## [2,] 0.1579815 0.09425488 0.005457556 0.11450130
## [3,] 0.1424175 0.14945546 0.168450096 0.12209719
## 
## , , 3, 4, 3
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.3853666 0.03918376 0.37437007 0.2476648
## [2,] 0.4514290 0.26933139 0.01559485 0.3271851
## [3,] 0.4069551 0.42706588 0.48134268 0.3488902
## 
## , , 1, 1, 4
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.3231340 0.03285601 0.31391335 0.2076696
## [2,] 0.3785281 0.22583728 0.01307645 0.2743482
## [3,] 0.3412363 0.35809936 0.40361104 0.2925482
## 
## , , 2, 1, 4
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.5199716 0.05287029 0.50513411 0.3341718
## [2,] 0.6091090 0.36340638 0.02104199 0.4414679
## [3,] 0.5491009 0.57623608 0.64947127 0.4707544
## 
## , , 3, 1, 4
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.6972678 0.07089763 0.67737115 0.4481153
## [2,] 0.8167986 0.48731810 0.02821674 0.5919964
## [3,] 0.7363294 0.77271697 0.87092337 0.6312689
## 
## , , 1, 2, 4
## 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.7271369 0.0739347 0.70638800 0.4673114
## [2,] 0.8517882 0.5081936 0.02942548 0.6173560
## [3,] 0.7678718 0.8058182 0.90823149 0.6583108
## 
## , , 2, 2, 4
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.4247981 0.04319313 0.41267641 0.2730063
## [2,] 0.4976201 0.29688995 0.01719055 0.3606634
## [3,] 0.4485957 0.47076416 0.53059468 0.3845894
## 
## , , 3, 2, 4
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.3654121 0.03715481 0.35498500 0.2348405
## [2,] 0.4280537 0.25538527 0.01478735 0.3102433
## [3,] 0.3858828 0.40495219 0.45641851 0.3308245
## 
## , , 1, 3, 4
## 
##             [,1]         [,2]         [,3]        [,4]
## [1,] 0.002867543 0.0002915695 0.0027857175 0.001842893
## [2,] 0.003359119 0.0020041163 0.0001160426 0.002434610
## [3,] 0.003028186 0.0031778312 0.0035817091 0.002596120
## 
## , , 2, 3, 4
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.4307751 0.04380086 0.41848286 0.2768476
## [2,] 0.5046218 0.30106725 0.01743243 0.3657380
## [3,] 0.4549075 0.47738792 0.53806026 0.3900007
## 
## , , 3, 3, 4
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.2980631 0.03030681 0.28955778 0.1915571
## [2,] 0.3491592 0.20831526 0.01206189 0.2530624
## [3,] 0.3147608 0.33031553 0.37229610 0.2698503
## 
## , , 1, 4, 4
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.6295128 0.06400836 0.61154957 0.4045710
## [2,] 0.7374286 0.43996437 0.02547486 0.5344709
## [3,] 0.6647787 0.69763043 0.78629391 0.5699271
## 
## , , 2, 4, 4
## 
##           [,1]       [,2]        [,3]      [,4]
## [1,] 0.1642673 0.01670257 0.159579918 0.1055702
## [2,] 0.1924272 0.11480587 0.006647502 0.1394667
## [3,] 0.1734697 0.18204216 0.205178327 0.1487188
## 
## , , 3, 4, 4
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.6728029 0.06841006 0.65360437 0.4323924
## [2,] 0.7881398 0.47021967 0.02722671 0.5712252
## [3,] 0.7104939 0.74560480 0.84036545 0.6091197
## 
## , , 1, 1, 5
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.5461739 0.05553452 0.53058874 0.3510113
## [2,] 0.6398031 0.38171909 0.02210234 0.4637142
## [3,] 0.5767711 0.60527367 0.68219931 0.4944765
## 
## , , 2, 1, 5
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.3108214 0.03160407 0.30195206 0.1997566
## [2,] 0.3641047 0.21723202 0.01257819 0.2638945
## [3,] 0.3282339 0.34445441 0.38823192 0.2814010
## 
## , , 3, 1, 5
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.5763143 0.05859917 0.55986906 0.3703817
## [2,] 0.6751104 0.40278410 0.02332205 0.4893041
## [3,] 0.6085999 0.63867545 0.71984620 0.5217640
## 
## , , 1, 2, 5
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.7155235 0.07275385 0.69510592 0.4598477
## [2,] 0.8381839 0.50007694 0.02895551 0.6074959
## [3,] 0.7556078 0.79294806 0.89372567 0.6477966
## 
## , , 2, 2, 5
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.7579593 0.07706869 0.73633079 0.4871201
## [2,] 0.8878943 0.52973517 0.03067278 0.6435249
## [3,] 0.8004208 0.83997569 0.94673015 0.6862157
## 
## , , 3, 2, 5
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.7166703 0.07287046 0.69621999 0.4605848
## [2,] 0.8395272 0.50087843 0.02900192 0.6084696
## [3,] 0.7568188 0.79421894 0.89515807 0.6488348
## 
## , , 1, 3, 5
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.3858414 0.03923205 0.37483140 0.2479699
## [2,] 0.4519853 0.26966328 0.01561407 0.3275883
## [3,] 0.4074566 0.42759214 0.48193582 0.3493201
## 
## , , 2, 3, 5
## 
##           [,1]       [,2]      [,3]      [,4]
## [1,] 0.5883227 0.05982018 0.5715349 0.3780992
## [2,] 0.6891774 0.41117677 0.0238080 0.4994996
## [3,] 0.6212811 0.65198331 0.7348454 0.5326358
## 
## , , 3, 3, 5
## 
##            [,1]        [,2]        [,3]       [,4]
## [1,] 0.05147222 0.005233654 0.050003457 0.03307982
## [2,] 0.06029598 0.035973763 0.002082957 0.04370110
## [3,] 0.05435574 0.057041873 0.064291458 0.04660019
## 
## , , 1, 4, 5
## 
##           [,1]       [,2]        [,3]       [,4]
## [1,] 0.1007204 0.01024117 0.097846338 0.06473030
## [2,] 0.1179867 0.07039315 0.004075912 0.08551395
## [3,] 0.1063629 0.11161905 0.125804976 0.09118686
## 
## , , 2, 4, 5
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.3723045 0.03785563 0.36168078 0.2392701
## [2,] 0.4361278 0.26020239 0.01506627 0.3160951
## [3,] 0.3931614 0.41259046 0.46502754 0.3370646
## 
## , , 3, 4, 5
## 
##           [,1]       [,2]       [,3]      [,4]
## [1,] 0.5901166 0.06000258 0.57327753 0.3792521
## [2,] 0.6912788 0.41243049 0.02388059 0.5010226
## [3,] 0.6231755 0.65397128 0.73708601 0.5342599
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.28666599 0.02914797 0.27848594 0.18423254
## [2,] 0.33580842 0.20034990 0.01160068 0.24338604
## [3,] 0.30272528 0.31768523 0.35806059 0.25953202
## 
## ,,2,1,1
##              [,1]         [,2]         [,3]         [,4]
## [1,] 0.0169107641 0.0017194729 0.0164282134 0.0108680946
## [2,] 0.0198097338 0.0118188761 0.0006843378 0.0143576288
## [3,] 0.0178581208 0.0187406257 0.0211224154 0.0153100991
## 
## ,,3,1,1
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.47148606 0.04794032 0.45803214 0.30301145
## [2,] 0.55231172 0.32952002 0.01907990 0.40030254
## [3,] 0.49789914 0.52250411 0.58891037 0.42685820
## 
## ...
## 
## ,,1,4,5
##             [,1]        [,2]        [,3]        [,4]
## [1,] 0.100720407 0.010241170 0.097846338 0.064730305
## [2,] 0.117986652 0.070393154 0.004075912 0.085513949
## [3,] 0.106362858 0.111619052 0.125804976 0.091186856
## 
## ,,2,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.37230454 0.03785563 0.36168078 0.23927014
## [2,] 0.43612776 0.26020239 0.01506627 0.31609514
## [3,] 0.39316138 0.41259046 0.46502754 0.33706456
## 
## ,,3,4,5
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.59011658 0.06000258 0.57327753 0.37925210
## [2,] 0.69127878 0.41243049 0.02388059 0.50102259
## [3,] 0.62317546 0.65397128 0.73708601 0.53425991

3.3 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.556194
DelayedTensor::einsum('i->', darrA)
## <1> HDF5Array object of type "double":
##      [1] 
## 1.556194
einsum::einsum('ij->', arrC)
## [1] 7.71041
DelayedTensor::einsum('ij->', darrC)
## <1> HDF5Array object of type "double":
##     [1] 
## 7.71041
einsum::einsum('ijk->', arrE)
## [1] 31.16421
DelayedTensor::einsum('ijk->', darrE)
## <1> HDF5Array object of type "double":
##      [1] 
## 31.16421

3.3.1 Row-wise / Column-wise Summation

einsum::einsum('ij->i', arrC)
## [1] 2.137994 2.172631 3.399786
DelayedTensor::einsum('ij->i', darrC)
## <3> HDF5Array object of type "double":
##      [1]      [2]      [3] 
## 2.137994 2.172631 3.399786
einsum::einsum('ij->j', arrC)
## [1] 2.540770 1.502666 1.779932 1.887043
DelayedTensor::einsum('ij->j', darrC)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 2.540770 1.502666 1.779932 1.887043

3.3.2 Mode-wise Vectorization

einsum::einsum('ijk->i', arrE)
## [1] 10.237235 11.162436  9.764538
DelayedTensor::einsum('ijk->i', darrE)
## <3> HDF5Array object of type "double":
##       [1]       [2]       [3] 
## 10.237235 11.162436  9.764538
einsum::einsum('ijk->j', arrE)
## [1] 9.077026 8.429779 6.745199 6.912205
DelayedTensor::einsum('ijk->j', darrE)
## <4> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4] 
## 9.077026 8.429779 6.745199 6.912205
einsum::einsum('ijk->k', arrE)
## [1] 4.177226 6.701141 6.353268 6.676520 7.256054
DelayedTensor::einsum('ijk->k', darrE)
## <5> HDF5Array object of type "double":
##      [1]      [2]      [3]      [4]      [5] 
## 4.177226 6.701141 6.353268 6.676520 7.256054

3.3.3 Mode-wise Summation

These are the same as what the modeSum function does.

einsum::einsum('ijk->ij', arrE)
##          [,1]     [,2]     [,3]     [,4]
## [1,] 3.048209 3.212624 1.912481 2.063921
## [2,] 2.566515 2.947920 3.023400 2.624602
## [3,] 3.462302 2.269235 1.809319 2.223682
DelayedTensor::einsum('ijk->ij', darrE)
## <3 x 4> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]     [,4]
## [1,] 3.048209 3.212624 1.912481 2.063921
## [2,] 2.566515 2.947920 3.023400 2.624602
## [3,] 3.462302 2.269235 1.809319 2.223682
einsum::einsum('ijk->jk', arrE)
##           [,1]     [,2]      [,3]      [,4]     [,5]
## [1,] 0.9845345 2.227895 2.0872335 1.9566810 1.820682
## [2,] 0.8789369 1.562796 1.2785419 1.9274316 2.782073
## [3,] 0.8312929 1.373193 2.3084244 0.9294595 1.302829
## [4,] 1.4824615 1.537256 0.6790684 1.8629477 1.350471
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.9845345 2.2278952 2.0872335 1.9566810 1.8206817
## [2,] 0.8789369 1.5627959 1.2785419 1.9274316 2.7820727
## [3,] 0.8312929 1.3731934 2.3084244 0.9294595 1.3028291
## [4,] 1.4824615 1.5372564 0.6790684 1.8629477 1.3504705
einsum::einsum('ijk->jk', arrE)
##           [,1]     [,2]      [,3]      [,4]     [,5]
## [1,] 0.9845345 2.227895 2.0872335 1.9566810 1.820682
## [2,] 0.8789369 1.562796 1.2785419 1.9274316 2.782073
## [3,] 0.8312929 1.373193 2.3084244 0.9294595 1.302829
## [4,] 1.4824615 1.537256 0.6790684 1.8629477 1.350471
DelayedTensor::einsum('ijk->jk', darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.9845345 2.2278952 2.0872335 1.9566810 1.8206817
## [2,] 0.8789369 1.5627959 1.2785419 1.9274316 2.7820727
## [3,] 0.8312929 1.3731934 2.3084244 0.9294595 1.3028291
## [4,] 1.4824615 1.5372564 0.6790684 1.8629477 1.3504705

3.3.4 Trace

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

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

3.4 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.5918220 0.7901159 0.3890968
## [2,] 0.2980032 0.9588781 0.5950524
## [3,] 0.5894561 0.4444009 0.4800987
DelayedTensor::einsum('ij->ji', darrB)
## <3 x 3> DelayedArray object of type "double":
##           [,1]      [,2]      [,3]
## [1,] 0.5918220 0.7901159 0.3890968
## [2,] 0.2980032 0.9588781 0.5950524
## [3,] 0.5894561 0.4444009 0.4800987
einsum::einsum('ijk->jki', arrD)
## , , 1
## 
##           [,1]      [,2]      [,3]
## [1,] 0.2493323 0.6059907 0.8647994
## [2,] 0.8695296 0.3602530 0.9186661
## [3,] 0.4534160 0.1896793 0.3957930
## 
## , , 2
## 
##           [,1]      [,2]      [,3]
## [1,] 0.5917427 0.1768482 0.3158027
## [2,] 0.6846784 0.1578544 0.5161389
## [3,] 0.4854825 0.9759871 0.7872937
## 
## , , 3
## 
##             [,1]       [,2]      [,3]
## [1,] 0.630422669 0.95168468 0.5335488
## [2,] 0.008210031 0.06090615 0.1447439
## [3,] 0.858438100 0.66042234 0.5764090
DelayedTensor::einsum('ijk->jki', darrD)
## <3 x 3 x 3> DelayedArray object of type "double":
## ,,1
##           [,1]      [,2]      [,3]
## [1,] 0.2493323 0.6059907 0.8647994
## [2,] 0.8695296 0.3602530 0.9186661
## [3,] 0.4534160 0.1896793 0.3957930
## 
## ,,2
##           [,1]      [,2]      [,3]
## [1,] 0.5917427 0.1768482 0.3158027
## [2,] 0.6846784 0.1578544 0.5161389
## [3,] 0.4854825 0.9759871 0.7872937
## 
## ,,3
##             [,1]        [,2]        [,3]
## [1,] 0.630422669 0.951684682 0.533548773
## [2,] 0.008210031 0.060906152 0.144743859
## [3,] 0.858438100 0.660422341 0.576408965

3.5 Multiplication + Summation

Some examples of combining Multiplication and Summation are shown below.

3.5.1 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.9264285
DelayedTensor::einsum('i,i->', darrA, darrA)
## <1> HDF5Array object of type "double":
##       [1] 
## 0.9264285
einsum::einsum('ij,ij->', arrC, arrC)
## [1] 5.995009
DelayedTensor::einsum('ij,ij->', darrC, darrC)
## <1> HDF5Array object of type "double":
##      [1] 
## 5.995009
einsum::einsum('ijk,ijk->', arrE, arrE)
## [1] 21.32522
DelayedTensor::einsum('ijk,ijk->', darrE, darrE)
## <1> HDF5Array object of type "double":
##      [1] 
## 21.32522

3.5.2 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.4917559 1.6592900 1.5496636 1.3892335 1.1731531
## [2,] 0.3811530 1.1914160 0.8134526 1.3597683 2.5818625
## [3,] 0.3573277 0.6344034 1.7820753 0.4427912 0.8029881
## [4,] 0.9711897 1.2590771 0.2693073 1.4133813 0.8019332
DelayedTensor::einsum('ijk,ijk->jk', darrE, darrE)
## <4 x 5> HDF5Matrix object of type "double":
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.4917559 1.6592900 1.5496636 1.3892335 1.1731531
## [2,] 0.3811530 1.1914160 0.8134526 1.3597683 2.5818625
## [3,] 0.3573277 0.6344034 1.7820753 0.4427912 0.8029881
## [4,] 0.9711897 1.2590771 0.2693073 1.4133813 0.8019332

3.5.3 Matrix Multiplication

Matrix Multiplication is considered a contracted product.

einsum::einsum('ij,jk->ik', arrC, t(arrC))
##          [,1]     [,2]     [,3]
## [1,] 1.467002 1.132549 1.836891
## [2,] 1.132549 1.600907 1.754357
## [3,] 1.836891 1.754357 2.927101
DelayedTensor::einsum('ij,jk->ik', darrC, t(darrC))
## <3 x 3> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]
## [1,] 1.467002 1.132549 1.836891
## [2,] 1.132549 1.600907 1.754357
## [3,] 1.836891 1.754357 2.927101

3.6 Multiplication + Permutation

Some examples of combining Multiplication and Permutation are shown below.

einsum::einsum('ij,ij->ji', arrC, arrC)
##             [,1]        [,2]      [,3]
## [1,] 0.619743446 0.850438193 0.6911256
## [2,] 0.006407316 0.302717719 0.7611209
## [3,] 0.584879160 0.001014908 0.9668801
## [4,] 0.255971905 0.446735966 0.5079740
DelayedTensor::einsum('ij,ij->ji', darrC, darrC)
## <4 x 3> HDF5Matrix object of type "double":
##             [,1]        [,2]        [,3]
## [1,] 0.619743446 0.850438193 0.691125619
## [2,] 0.006407316 0.302717719 0.761120910
## [3,] 0.584879160 0.001014908 0.966880137
## [4,] 0.255971905 0.446735966 0.507973999
einsum::einsum('ijk,ijk->jki', arrE, arrE)
## , , 1
## 
##            [,1]      [,2]         [,3]         [,4]       [,5]
## [1,] 0.13259905 0.4822358 0.7839066550 1.684820e-01 0.48133774
## [2,] 0.08680350 0.8732883 0.0227832194 8.531403e-01 0.82610609
## [3,] 0.00528532 0.2671087 0.6875587655 1.326808e-05 0.24021813
## [4,] 0.34610492 0.2806725 0.0003327065 6.394361e-01 0.01636903
## 
## , , 2
## 
##              [,1]      [,2]       [,3]       [,4]      [,5]
## [1,] 0.0004614392 0.5494391 0.56020117 0.43626186 0.1558870
## [2,] 0.0018940869 0.3134442 0.70917208 0.29117437 0.9270001
## [3,] 0.3120515558 0.1986245 0.52502687 0.29942578 0.5584950
## [4,] 0.6126698567 0.9780624 0.02934742 0.04354019 0.2236581
## 
## , , 3
## 
##            [,1]         [,2]       [,3]      [,4]        [,5]
## [1,] 0.35869537 0.6276151376 0.20555578 0.7844897 0.535928425
## [2,] 0.29245538 0.0046834944 0.08149727 0.2154536 0.828756265
## [3,] 0.03999078 0.1686701985 0.56948968 0.1433522 0.004274978
## [4,] 0.01241495 0.0003422766 0.23962720 0.7304050 0.561906035
DelayedTensor::einsum('ijk,ijk->jki', darrE, darrE)
## <4 x 5 x 3> HDF5Array object of type "double":
## ,,1
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 1.325990e-01 4.822358e-01 7.839067e-01 1.684820e-01 4.813377e-01
## [2,] 8.680350e-02 8.732883e-01 2.278322e-02 8.531403e-01 8.261061e-01
## [3,] 5.285320e-03 2.671087e-01 6.875588e-01 1.326808e-05 2.402181e-01
## [4,] 3.461049e-01 2.806725e-01 3.327065e-04 6.394361e-01 1.636903e-02
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.0004614392 0.5494390518 0.5602011715 0.4362618620 0.1558869778
## [2,] 0.0018940869 0.3134442266 0.7091720835 0.2911743657 0.9270000984
## [3,] 0.3120515558 0.1986244568 0.5250268719 0.2994257833 0.5584950156
## [4,] 0.6126698567 0.9780623864 0.0293474174 0.0435401908 0.2236581412
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.3586953670 0.6276151376 0.2055557848 0.7844897016 0.5359284248
## [2,] 0.2924553753 0.0046834944 0.0814972688 0.2154536485 0.8287562645
## [3,] 0.0399907768 0.1686701985 0.5694896839 0.1433521926 0.0042749782
## [4,] 0.0124149469 0.0003422766 0.2396271951 0.7304050059 0.5619060347

3.7 Summation + Permutation

Some examples of combining Summation and Permutation are shown below.

einsum::einsum('ijk->ki', arrE)
##          [,1]     [,2]     [,3]
## [1,] 1.319773 1.406350 1.451103
## [2,] 2.675542 2.735746 1.289853
## [3,] 1.883758 2.486488 1.983023
## [4,] 2.137412 1.955968 2.583140
## [5,] 2.220751 2.577884 2.457419
DelayedTensor::einsum('ijk->ki', darrE)
## <5 x 3> HDF5Matrix object of type "double":
##          [,1]     [,2]     [,3]
## [1,] 1.319773 1.406350 1.451103
## [2,] 2.675542 2.735746 1.289853
## [3,] 1.883758 2.486488 1.983023
## [4,] 2.137412 1.955968 2.583140
## [5,] 2.220751 2.577884 2.457419

3.8 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.0581365222 0.211430739 3.436948e-01 7.386898e-02 0.211036983
## [2,] 0.0003934686 0.003958499 1.032733e-04 3.867171e-03 0.003744628
## [3,] 0.0021869262 0.110522552 2.844937e-01 5.489981e-06 0.099395936
## [4,] 0.0626753527 0.050826339 6.024906e-05 1.157940e-01 0.002964231
## 
## , , 2
## 
##              [,1]       [,2]         [,3]         [,4]         [,5]
## [1,] 0.0002376915 0.28302095 0.2885646154 0.2247223726 0.0802987714
## [2,] 0.0003472914 0.05747175 0.1300306639 0.0533884468 0.1699706475
## [3,] 0.0001918268 0.00012210 0.0003227486 0.0001840654 0.0003433224
## [4,] 0.1657806120 0.26465115 0.0079410351 0.0117814177 0.0605190269
## 
## , , 3
## 
##             [,1]         [,2]       [,3]       [,4]        [,5]
## [1,] 0.060251486 1.054230e-01 0.03452802 0.13177385 0.090022027
## [2,] 0.054100125 8.663805e-04 0.01507585 0.03985589 0.153308235
## [3,] 0.009397611 3.963656e-02 0.13382693 0.03368697 0.001004596
## [4,] 0.001532750 4.225748e-05 0.02958439 0.09017585 0.069372954
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,] 5.813652e-02 2.114307e-01 3.436948e-01 7.386898e-02 2.110370e-01
## [2,] 3.934686e-04 3.958499e-03 1.032733e-04 3.867171e-03 3.744628e-03
## [3,] 2.186926e-03 1.105226e-01 2.844937e-01 5.489981e-06 9.939594e-02
## [4,] 6.267535e-02 5.082634e-02 6.024906e-05 1.157940e-01 2.964231e-03
## 
## ,,2
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.0002376915 0.2830209516 0.2885646154 0.2247223726 0.0802987714
## [2,] 0.0003472914 0.0574717503 0.1300306639 0.0533884468 0.1699706475
## [3,] 0.0001918268 0.0001221000 0.0003227486 0.0001840654 0.0003433224
## [4,] 0.1657806120 0.2646511481 0.0079410351 0.0117814177 0.0605190269
## 
## ,,3
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 6.025149e-02 1.054230e-01 3.452802e-02 1.317739e-01 9.002203e-02
## [2,] 5.410012e-02 8.663805e-04 1.507585e-02 3.985589e-02 1.533082e-01
## [3,] 9.397611e-03 3.963656e-02 1.338269e-01 3.368697e-02 1.004596e-03
## [4,] 1.532750e-03 4.225748e-05 2.958439e-02 9.017585e-02 6.937295e-02

4 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 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

Session information

## R version 4.5.1 Patched (2025-09-10 r88807)
## Platform: x86_64-apple-darwin20
## Running under: macOS Monterey 12.7.6
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/lib/libRblas.0.dylib 
## LAPACK: /Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.1
## 
## locale:
## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## time zone: America/New_York
## tzcode source: internal
## 
## attached base packages:
## [1] stats4    stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
##  [1] einsum_0.1.2              DelayedRandomArray_1.18.0
##  [3] HDF5Array_1.38.0          h5mread_1.2.0            
##  [5] rhdf5_2.54.0              DelayedArray_0.36.0      
##  [7] SparseArray_1.10.0        S4Arrays_1.10.0          
##  [9] abind_1.4-8               IRanges_2.44.0           
## [11] S4Vectors_0.48.0          MatrixGenerics_1.22.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] jsonlite_2.0.0      compiler_4.5.1      BiocManager_1.30.26
##  [4] rsvd_1.0.5          Rcpp_1.1.0          rhdf5filters_1.22.0
##  [7] parallel_4.5.1      jquerylib_0.1.4     BiocParallel_1.44.0
## [10] yaml_2.3.10         fastmap_1.2.0       lattice_0.22-7     
## [13] R6_2.6.1            XVector_0.50.0      ScaledMatrix_1.18.0
## [16] knitr_1.50          bookdown_0.45       bslib_0.9.0        
## [19] rlang_1.1.6         cachem_1.1.0        xfun_0.53          
## [22] sass_0.4.10         cli_3.6.5           Rhdf5lib_1.32.0    
## [25] BiocSingular_1.26.0 digest_0.6.37       grid_4.5.1         
## [28] irlba_2.3.5.1       rTensor_1.4.9       dqrng_0.4.1        
## [31] lifecycle_1.0.4     evaluate_1.0.5      codetools_0.2-20   
## [34] beachmat_2.26.0     rmarkdown_2.30      tools_4.5.1        
## [37] htmltools_0.5.8.1