einsum_into

Function einsum_into 

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

Execute einsum using string notation, accumulating into an existing output.

Computes output = alpha * einsum(operands) + beta * output, writing the result into the provided output tensor. This avoids allocating a new output buffer on each call, which is critical for hot loops.

§Arguments

  • subscripts — Einstein summation notation (e.g., "ij,jk->ik")
  • operands — Input tensors
  • alpha — Scaling factor for the einsum result
  • beta — Scaling factor for the existing output contents
  • output — Pre-allocated output tensor (must have correct shape)

§Examples

use tenferro_einsum::einsum_into;
use tenferro_tensor::{Tensor, MemoryOrder};
use tenferro_device::LogicalMemorySpace;

let col = MemoryOrder::ColumnMajor;
let a = Tensor::<f64>::from_slice(&[1.0, 2.0, 3.0, 4.0], &[2, 2], col).unwrap();
let b = Tensor::<f64>::from_slice(&[5.0, 6.0, 7.0, 8.0], &[2, 2], col).unwrap();
let mut c = Tensor::<f64>::zeros(&[2, 2], LogicalMemorySpace::MainMemory, col);

// Overwrite: C = A @ B
einsum_into("ij,jk->ik", &[&a, &b], 1.0, 0.0, &mut c).unwrap();

// Accumulate: C += A @ B
einsum_into("ij,jk->ik", &[&a, &b], 1.0, 1.0, &mut c).unwrap();

§Errors

Returns an error if the notation is invalid, tensor shapes are incompatible, or the output shape does not match the expected result.