TensorIndex

Trait TensorIndex 

Source
pub trait TensorIndex:
    Sized
    + Clone
    + Debug
    + Send
    + Sync {
    type Index: IndexLike;

    // Required methods
    fn external_indices(&self) -> Vec<Self::Index>;
    fn replaceind(
        &self,
        old_index: &Self::Index,
        new_index: &Self::Index,
    ) -> Result<Self>;
    fn replaceinds(
        &self,
        old_indices: &[Self::Index],
        new_indices: &[Self::Index],
    ) -> Result<Self>;

    // Provided methods
    fn num_external_indices(&self) -> usize { ... }
    fn replaceinds_pairs(
        &self,
        pairs: &[(Self::Index, Self::Index)],
    ) -> Result<Self> { ... }
}
Expand description

Trait for objects that have external indices and support index operations.

This is a minimal trait that can be implemented by:

  • Dense tensors (TensorDynLen)
  • Tensor networks (TreeTN)
  • Any other structure that organizes tensors with indices

§Design

This trait is separate from TensorLike to allow tensor networks to implement index operations without needing to implement contraction/factorization operations.

Required Associated Types§

Source

type Index: IndexLike

The index type used by this object.

Required Methods§

Source

fn external_indices(&self) -> Vec<Self::Index>

Return flattened external indices for this object.

§Ordering

The ordering MUST be stable (deterministic). Implementations should:

  • Sort indices by their id field, or
  • Use insertion-ordered storage

This ensures consistent behavior for hashing, serialization, and comparison.

Source

fn replaceind( &self, old_index: &Self::Index, new_index: &Self::Index, ) -> Result<Self>

Replace an index in this object.

This replaces the index matching old_index by ID with new_index. The storage data is not modified, only the index metadata is changed.

§Arguments
  • old_index - The index to replace (matched by ID)
  • new_index - The new index to use
§Returns

A new object with the index replaced.

Source

fn replaceinds( &self, old_indices: &[Self::Index], new_indices: &[Self::Index], ) -> Result<Self>

Replace multiple indices in this object.

This replaces each index in old_indices (matched by ID) with the corresponding index in new_indices. The storage data is not modified.

§Arguments
  • old_indices - The indices to replace (matched by ID)
  • new_indices - The new indices to use
§Returns

A new object with the indices replaced.

Provided Methods§

Source

fn num_external_indices(&self) -> usize

Number of external indices.

Default implementation calls external_indices().len(), but implementations SHOULD override this for efficiency when the count can be computed without allocating the full index list.

Source

fn replaceinds_pairs( &self, pairs: &[(Self::Index, Self::Index)], ) -> Result<Self>

Replace indices using pairs of (old, new).

This is a convenience method that wraps replaceinds.

§Arguments
  • pairs - Pairs of (old_index, new_index) to replace
§Returns

A new object with the indices replaced.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§