einsum | R Documentation |
Einstein summation is a convenient and concise notation for operations on n-dimensional arrays.
NOTE: Sparse mode of einsum is not available for now.
einsum(subscripts, ...)
subscripts |
a string in Einstein notation where arrays
are separated by ',' and the result is separated by '->'. For
example |
... |
the DelayedArrays that are combined. |
This function is an extension of the einsum
by DelayedArray.
The einsum
function returns an array with one dimension for each index
in the result of the subscripts
.
For example "ij,jk->ik"
produces a 2-dimensional array,
"abc,cd,de->abe"
produces a 3-dimensional array.
library("DelayedArray")
library("DelayedRandomArray")
darr1 <- RandomUnifArray(c(4,8))
darr2 <- RandomUnifArray(c(8,3))
# Matrix Multiply
darr1 %*% darr2
DelayedTensor::einsum("ij,jk -> ik", darr1, darr2)
# Diag
mat_sq <- RandomUnifArray(c(4,4))
DelayedTensor::diag(mat_sq)
einsum("ii->i", mat_sq)
# Trace
sum(DelayedTensor::diag(mat_sq))
einsum("ii->", mat_sq)
# Scalar product
darr3 <- RandomUnifArray(c(4,8))
darr3 * darr1
einsum("ij,ij->ij", darr3, darr1)
# Transpose
t(darr1)
einsum("ij->ji", darr1)
# Batched L2 norm
arr1 <- as.array(darr1)
arr3 <- as.array(darr3)
darr4 <- DelayedArray(array(c(arr1, arr3), dim = c(dim(arr1), 2)))
c(sum(darr1^2), sum(darr3^2))
einsum("ijb,ijb->b", darr4, darr4)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.