einsum | R Documentation |
Einstein summation is a convenient and concise notation for operations on n-dimensional arrays.
einsum(equation_string, ...)
einsum_generator(equation_string, compile_function = TRUE)
equation_string |
a string in Einstein notation where arrays
are separated by ',' and the result is separated by '->'. For
example |
... |
the arrays that are combined. All arguments are converted
to arrays with |
compile_function |
boolean that decides if |
The following table show, how the Einstein notation abbreviates complex summation for arrays/matrices:
equation_string | Formula | |
------------------------ | -------------------------------------- | ---------------------------------- |
"ij,jk->ik" | \mjseqn Y_ik = \sum_jA_ij B_jk | Matrix multiplication |
"ij->ji" ` | \mjseqn Y = A^T | Transpose |
"ii->i" | \mjeqny = \textrmdiag(A)y = diag(A) | Diagonal |
"ii->ii" | \mjeqnY = \textrmdiag(A) IY = diag(A) I | Diagonal times Identity |
"ii->" | \mjeqny = \textrmtrace(A) = \sum_iA_ii y = trace(A) = Sum_i(A_ii) | Trace |
"ijk,mjj->i" | \mjeqn y_i = \sum_j\sum_k\sum_mA_ijkB_mjj y_i = Sum_j Sum_k Sum_m A_ijk B_mjj | Complex 3D operation |
The function and the conventions are inspired by the einsum()
function
in NumPy (documentation).
Unlike NumPy, 'einsum' only supports the explicit mode. The explicit mode is more flexible and
can avoid confusion. The common summary of the Einstein summation to
"sum over duplicated indices" however is not a good mental model. A better rule of thumb is
"sum over all indices not in the result".
Note: einsum()
internally uses C++ code to provide results quickly, the repeated
parsing of the equation_string
comes with some overhead. Thus,
if you need to do the same calculation over and over again it can be worth to use
einsum_generator()
and call the returned the function. einsum_generator()
generates efficient C++ code that can be one or two orders of magnitude faster than
einsum()
.
The einsum()
function returns an array with one dimension for each index in the result
of the equation_string
. For example "ij,jk->ik"
produces a 2-dimensional array,
"abc,cd,de->abe"
produces a 3-dimensional array.
The einsum_generator()
function returns a function that takes one array for each
comma-separated input in the equation_string
and returns the same result as einsum()
.
Or if compile_function = FALSE
, einsum_generator()
function returns a string with the
C++ code for such a function.
mat1 <- matrix(rnorm(n = 4 * 8), nrow = 4, ncol = 8)
mat2 <- matrix(rnorm(n = 8 * 3), nrow = 8, ncol = 3)
# Matrix Multiply
mat1 %*% mat2
einsum("ij,jk -> ik", mat1, mat2)
# einsum_generator() works just like einsum() but returns a performant function
mat_mult <- einsum_generator("ij,jk -> ik")
mat_mult(mat1, mat2)
# Diag
mat_sq <- matrix(rnorm(n = 4 * 4), nrow = 4, ncol = 4)
diag(mat_sq)
einsum("ii->i", mat_sq)
einsum("ii->ii", mat_sq)
# Trace
sum(diag(mat_sq))
einsum("ii->", mat_sq)
# Scalar product
mat3 <- matrix(rnorm(n = 4 * 8), nrow = 4, ncol = 8)
mat3 * mat1
einsum("ij,ij->ij", mat3, mat1)
# Transpose
t(mat1)
einsum("ij->ji", mat1)
# Batched L2 norm
arr1 <- array(c(mat1, mat3), dim = c(dim(mat1), 2))
c(sum(mat1^2), sum(mat3^2))
einsum("ijb,ijb->b", arr1, arr1)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.