pub fn einsum_native_subscripts(
subscripts: &str,
operands: &[&Tensor],
) -> Result<Tensor>Expand description
Execute a cached einsum over borrowed native tensors using string subscripts.
This is the shared string-notation entry: it parses subscripts into the
numeric labels that einsum_native_tensors consumes, checks that the
equation describes exactly the supplied operands, and then delegates the
contraction. Use it instead of re-implementing the parsing plus label
mapping in each crate; use einsum_native_tensors directly when the
labels are already numeric.
§Arguments
subscripts- NumPy/PyTorch-style einsum notation such as"ij,jk->ik"or"ii->"for a trace. Parenthesized contraction order is rejected; see [tenferro_einsum::Subscripts::parse].operands- One native tensor per comma-separated input term, in term order. Ranks must match the term lengths and repeated labels must have equal extents.
§Returns
The contracted native tensor in the promoted common dtype of the operands.
§Errors
Returns an error when the notation parser rejects subscripts as invalid
notation, when the operand count does not match the number of comma-separated
input terms, when an input term’s label count does not match its operand
rank, when repeated or contracted labels have incompatible shapes, or when
dtype promotion or the backend contraction fails.
§Compatibility frontend
Like einsum_native_tensors, this runs on the retained process-global
(compatibility) frontend of
tensor4all-rs#859:
it selects the default execution context and pays one graph dispatch per
call. It is meant for cold or per-stage paths, not per-element hot loops;
use CpuExecutionContext sessions for those.
§Examples
use tensor4all_tensorbackend::einsum_native_subscripts;
use tenferro::Tensor as NativeTensor;
// Column-major A = [[1, 2], [3, 4]] and B = [[5, 6], [7, 8]].
let lhs = NativeTensor::from_vec_col_major(vec![2, 2], vec![1.0_f64, 3.0, 2.0, 4.0])?;
let rhs = NativeTensor::from_vec_col_major(vec![2, 2], vec![5.0_f64, 7.0, 6.0, 8.0])?;
let product = einsum_native_subscripts("ij,jk->ik", &[&lhs, &rhs])?;
assert_eq!(product.shape(), &[2, 2]);
assert_eq!(product.as_slice::<f64>()?, &[19.0, 43.0, 22.0, 50.0]);
let trace = einsum_native_subscripts("ii->", &[&lhs])?;
assert_eq!(trace.as_slice::<f64>()?, &[5.0]);