Skip to main content

unfold_split

Function unfold_split 

Source
pub fn unfold_split(
    t: &TensorDynLen,
    left_inds: &[DynIndex],
) -> Result<(Tensor, usize, usize, usize, Vec<DynIndex>, Vec<DynIndex>)>
Expand description

Unfold a tensor into a matrix by splitting indices into left and right groups.

This function validates the split, permutes the tensor so that left indices come first, and returns a rank-2 native tenferro tensor along with metadata.

§Arguments

  • t - Input tensor
  • left_inds - Indices to place on the left (row) side of the matrix

§Returns

A tuple (matrix_tensor, left_len, m, n, left_indices, right_indices) where:

  • matrix_tensor is a rank-2 tenferro::Tensor with shape [m, n]
  • left_len is the number of left indices
  • m is the product of left index dimensions
  • n is the product of right index dimensions
  • left_indices is the vector of left indices (cloned)
  • right_indices is the vector of right indices (cloned)

§Errors

Returns an error if:

  • The tensor rank is < 2
  • left_inds is empty or contains all indices
  • left_inds contains indices not in the tensor or duplicates
  • Native reshape fails

§Examples

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

let i = DynIndex::new_dyn(2);
let j = DynIndex::new_dyn(3);
// 2x3 dense tensor with data [1..6]
let t = TensorDynLen::from_dense(
    vec![i.clone(), j.clone()],
    vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
).unwrap();

let (matrix, left_len, m, n, left_indices, right_indices) =
    unfold_split(&t, &[i]).unwrap();
assert_eq!(left_len, 1);
assert_eq!(m, 2);
assert_eq!(n, 3);
assert_eq!(left_indices.len(), 1);
assert_eq!(right_indices.len(), 1);