einsum

Function einsum 

Source
pub fn einsum<T: Scalar + HasAlgebra>(
    subscripts: &str,
    operands: &[&Tensor<T>],
) -> Result<Tensor<T>>
Expand description

Execute einsum using string notation.

Parses the subscript string, optimizes the contraction order, and executes the contraction. The backend is selected automatically from the tensors’ memory space and compute device.

Parentheses in the subscript string specify contraction order explicitly (e.g., "ij,(jk,kl)->il" contracts B and C first). Without parentheses, the contraction order is optimized automatically.

§Arguments

  • subscripts — Einstein summation notation (e.g., "ij,jk->ik")
  • operands — Input tensors

§Examples

// Matrix multiplication
let c = einsum("ij,jk->ik", &[&a, &b]).unwrap();

// Trace
let tr = einsum("ii->", &[&a]).unwrap();

// Batch matrix multiplication
let c = einsum("bij,bjk->bik", &[&a, &b]).unwrap();

// Explicit contraction order: contract B*C first, then A
let d = einsum("ij,(jk,kl)->il", &[&a, &b, &c]).unwrap();

§Errors

Returns an error if the notation is invalid or tensor shapes are incompatible with the subscripts.