pub fn contract_connected(
tensors: &[&TensorDynLen],
allowed: AllowedPairs<'_>,
) -> Result<TensorDynLen>Expand description
Contract multiple tensors that form a connected graph.
Uses hyperedge-aware einsum optimization via tensorbackend. This correctly handles Diag tensors by treating their diagonal axes as hyperedges.
§Arguments
tensors- Slice of tensors to contract (must form a connected graph)allowed- Specifies which tensor pairs can have their indices contracted
§Returns
The result of contracting all tensors over allowed contractable indices.
§Connectivity Requirement
All tensors must form a connected graph through contractable indices. Two tensors are connected if they share a contractable index (same ID, dual direction). If the tensors form disconnected components, this function returns an error.
Use contract_multi if you want automatic handling of disconnected components.
§Behavior by N
- N=0: Error
- N=1: Clone of input
- N>=2: Optimal order via hyperedge-aware greedy optimizer
§Examples
use tensor4all_core::{TensorDynLen, DynIndex, contract_connected, AllowedPairs};
// A[i, j] contracted with B[j, k]
let i = DynIndex::new_dyn(2);
let j = DynIndex::new_dyn(3);
let k = DynIndex::new_dyn(4);
let a = TensorDynLen::from_dense(
vec![i.clone(), j.clone()],
vec![1.0_f64; 6],
).unwrap();
let b = TensorDynLen::from_dense(
vec![j.clone(), k.clone()],
vec![1.0_f64; 12],
).unwrap();
let c = contract_connected(&[&a, &b], AllowedPairs::All).unwrap();
assert_eq!(c.dims(), vec![2, 4]);