pub fn direct_sum(
a: &IdxTensor,
b: &IdxTensor,
pairs: &[(DynIndex, DynIndex)],
) -> Result<(IdxTensor, Vec<DynIndex>), IdxTensorError>Expand description
Compute the direct sum of two tensors along specified index pairs.
For tensors A and B with indices to be summed specified as pairs, creates a new tensor C where each paired index has dimension = dim_A + dim_B. Non-paired indices must match exactly between A and B.
§Arguments
-
a- First tensor -
b- Second tensor -
pairs- Pairs of (a_index, b_index) to be summed. Each pair createsa new index in the result with dimension = dim(a_index) + dim(b_index).
§Returns
A tuple of:
- The direct sum tensor
- The new indices created for the summed dimensions (one per pair)
§Errors
Returns an error when the operation fails (a shape or index mismatch, or /// a backend failure).
§Example
use tensor4all_core::{direct_sum, DynIndex, IdxTensor};
let j = DynIndex::new_dyn(2);
let k = DynIndex::new_dyn(3);
let a = IdxTensor::from_dense(vec![j.clone()], vec![1.0, 2.0])?;
let b = IdxTensor::from_dense(vec![k.clone()], vec![3.0, 4.0, 5.0])?;
let (result, new_indices) = direct_sum(&a, &b, &[(j.clone(), k.clone())])?;
assert_eq!(new_indices.len(), 1);
assert_eq!(result.to_vec::<f64>()?, vec![1.0, 2.0, 3.0, 4.0, 5.0]);