Skip to main content

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.

§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

use tensor4all_core::{direct_sum, DynIndex, TensorDynLen};

let j = DynIndex::new_dyn(2);
let k = DynIndex::new_dyn(3);

let a = TensorDynLen::from_dense(vec![j.clone()], vec![1.0, 2.0])?;
let b = TensorDynLen::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]);