einsum: Einstein Summation of DelayedArray

View source: R/Einsum.R

einsumR Documentation

Einstein Summation of DelayedArray

Description

Einstein summation is a convenient and concise notation for operations on n-dimensional arrays.

NOTE: Sparse mode of einsum is not available for now.

Usage

einsum(subscripts, ...)

Arguments

subscripts

a string in Einstein notation where arrays are separated by ',' and the result is separated by '->'. For example "ij,jk->ik" corresponds to a standard matrix multiplication. Whitespace inside the subscripts is ignored. Unlike the equivalent functions in Python, einsum only supports the explicit mode. This means that the subscripts must contain '->'.

...

the DelayedArrays that are combined.

Details

This function is an extension of the einsum by DelayedArray.

Value

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.

Examples

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)

rikenbit/DelayedTensor documentation built on Sept. 28, 2024, 5:43 a.m.