pub trait TensorIndex:
Sized
+ Clone
+ Debug
+ Send
+ Sync {
type Index: IndexLike;
type Error: Error + Send + Sync + 'static + From<Error>;
// Required methods
fn external_indices(&self) -> Vec<Self::Index>;
fn replaceind(
&self,
old_index: &Self::Index,
new_index: &Self::Index,
) -> Result<Self, Self::Error>;
fn replace_indices(
&self,
old_indices: &[Self::Index],
new_indices: &[Self::Index],
) -> Result<Self, Self::Error>;
// Provided methods
fn num_external_indices(&self) -> usize { ... }
fn replace_indices_pairs(
&self,
pairs: &[(Self::Index, Self::Index)],
) -> Result<Self, Self::Error> { ... }
}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 (
IdxTensor) - 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 full index identity, 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, Self::Error>
fn replaceind( &self, old_index: &Self::Index, new_index: &Self::Index, ) -> Result<Self, Self::Error>
Replace an index in this object.
This replaces the index equal to old_index (including dimension,
prime level, tags, and other identity metadata) with new_index.
The storage data is not modified, only the index metadata is changed.
§Arguments
old_index- The full index identity to replacenew_index- The new index to use
§Returns
A new object with the index replaced.
§Errors
Returns Self::Error when old_index is not present (a
missing-index failure) or the replacement violates an index identity
invariant (an invalid replacement).
Sourcefn replace_indices(
&self,
old_indices: &[Self::Index],
new_indices: &[Self::Index],
) -> Result<Self, Self::Error>
fn replace_indices( &self, old_indices: &[Self::Index], new_indices: &[Self::Index], ) -> Result<Self, Self::Error>
Replace multiple indices in this object.
This replaces each full index identity in old_indices with the
corresponding index in new_indices. The storage data is not modified.
§Arguments
old_indices- The full index identities to replacenew_indices- The new indices to use
§Returns
A new object with the indices replaced.
§Errors
Returns Self::Error when an old_indices entry is not present
(a missing-index failure), when old_indices and new_indices differ
in length (a length mismatch), or when the replacement violates an
index identity invariant (an invalid replacement).
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.
Sourcefn replace_indices_pairs(
&self,
pairs: &[(Self::Index, Self::Index)],
) -> Result<Self, Self::Error>
fn replace_indices_pairs( &self, pairs: &[(Self::Index, Self::Index)], ) -> Result<Self, Self::Error>
Replace indices using pairs of (old, new).
This is a convenience method that wraps replace_indices.
§Arguments
pairs- Pairs of (old_index, new_index) to replace
§Returns
A new object with the indices replaced.
§Errors
Returns Self::Error when an old entry is not present (a
missing-index failure) or when the replacement violates an index
identity invariant (an invalid replacement); propagates failures from
Self::replace_indices.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".