Skip to main content

replace_indices

Function replace_indices 

Source
pub fn replace_indices<I: IndexLike>(
    indices: Vec<I>,
    replacements: &[(I, I)],
) -> Result<Vec<I>, ReplaceIndsError>
Expand description

Replace indices in a collection based on full-index matching.

This corresponds to ITensors.jl’s replace_indices function. It replaces indices in indices that equal any of the (old, new) pairs in replacements. The replacement index must have the same dimension as the original.

§Arguments

  • indices - Collection of indices to modify
  • replacements - Pairs of (old_index, new_index) where indices equal to old_index are replaced with new_index

§Returns

A new vector with replacements applied, or an error if any replacement has a shape mismatch.

§Errors

Returns ReplaceIndsError::SpaceMismatch if any replacement index has a different dimension than the original.

§Example

use tensor4all_core::index::{DefaultIndex as Index, DynId};
use tensor4all_core::index_ops::replace_indices;

let i = Index::new_dyn(2);
let j = Index::new_dyn(3);
let k = Index::new_dyn(4);
let new_j = Index::new_dyn(3);  // Same size as j

let indices = vec![i.clone(), j.clone(), k.clone()];
let replacements = vec![(j.clone(), new_j.clone())];

let replaced = replace_indices(indices, &replacements).unwrap();
assert_eq!(replaced.len(), 3);
assert_eq!(replaced[1].id, new_j.id);