direct_sum

Function direct_sum 

Source
pub fn direct_sum(
    a: &TensorDynLen,
    b: &TensorDynLen,
    pairs: &[(DynIndex, DynIndex)],
) -> Result<(TensorDynLen, Vec<DynIndex>)>
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 (same ID).

§Arguments

  • a - First tensor
  • b - Second tensor
  • pairs - Pairs of (a_index, b_index) to be summed. Each pair creates a 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)

§Example

// A has indices [i, j] with dims [2, 3]
// B has indices [i, k] with dims [2, 4]
// If we pair (j, k), result has indices [i, m] with dims [2, 7]
// where m is a new index with dim = 3 + 4 = 7
let (result, new_indices) = direct_sum(&a, &b, &[(j, k)])?;