pub fn contract_multi(
tensors: &[&TensorDynLen],
allowed: AllowedPairs<'_>,
) -> Result<TensorDynLen>Expand description
Contract multiple tensors into a single tensor, handling disconnected components.
This function automatically handles disconnected tensor graphs by:
- Finding connected components based on contractable indices
- Contracting each connected component separately
- Combining results using outer product
§Arguments
tensors- Slice of tensors to contractallowed- Specifies which tensor pairs can have their indices contracted
§Returns
The result of contracting all tensors over allowed contractable indices. If tensors form disconnected components, they are combined via outer product.
§Behavior by N
- N=0: Error
- N=1: Clone of input
- N>=2: Contract connected components, combine with outer product
§Errors
AllowedPairs::Specifiedcontains a pair with no contractable indices
§Examples
use tensor4all_core::{TensorDynLen, DynIndex, contract_multi, AllowedPairs};
// A[i, j] and B[j, k] share index j — contract to get C[i, 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_multi(&[&a, &b], AllowedPairs::All).unwrap();
assert_eq!(c.dims(), vec![2, 4]);