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§
Required Methods§
Sourcefn external_indices(&self) -> Vec<Self::Index>
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
idfield, or - Use insertion-ordered storage
This ensures consistent behavior for hashing, serialization, and comparison.
Sourcefn replaceind(
&self,
old_index: &Self::Index,
new_index: &Self::Index,
) -> Result<Self>
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.
Sourcefn replaceinds(
&self,
old_indices: &[Self::Index],
new_indices: &[Self::Index],
) -> Result<Self>
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§
Sourcefn num_external_indices(&self) -> usize
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.
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.