pub struct TTCache<T: TTScalar> { /* private fields */ }Expand description
Cached tensor train evaluator.
Wraps a tensor train and caches left and right partial contractions so
that repeated evaluations sharing common prefixes or suffixes reuse
previously computed results. This is particularly effective for
batch evaluation via evaluate_many.
§Examples
use tensor4all_simplett::{TensorTrain, AbstractTensorTrain, TTCache};
let tt = TensorTrain::<f64>::constant(&[2, 3, 4], 5.0);
let mut cache = TTCache::new(&tt);
// Single evaluation (caches intermediate contractions)
let val = cache.evaluate(&[1, 2, 3]).unwrap();
assert!((val - 5.0).abs() < 1e-12);
// Batch evaluation reuses cached partial contractions
let indices = vec![vec![0, 0, 0], vec![1, 2, 3], vec![0, 1, 2]];
let vals = cache.evaluate_many(&indices, None).unwrap();
assert!(vals.iter().all(|&v| (v - 5.0).abs() < 1e-12));Implementations§
Source§impl<T: TTScalar + EinsumScalar> TTCache<T>
impl<T: TTScalar + EinsumScalar> TTCache<T>
Sourcepub fn new<TT: AbstractTensorTrain<T>>(tt: &TT) -> Self
pub fn new<TT: AbstractTensorTrain<T>>(tt: &TT) -> Self
Create a new TTCache from a tensor train
Sourcepub fn with_site_dims<TT: AbstractTensorTrain<T>>(
tt: &TT,
site_dims: Vec<Vec<usize>>,
) -> Result<Self>
pub fn with_site_dims<TT: AbstractTensorTrain<T>>( tt: &TT, site_dims: Vec<Vec<usize>>, ) -> Result<Self>
Create a new TTCache with custom site dimensions
This allows treating a single tensor site as multiple logical indices.
Sourcepub fn link_dim(&self, i: usize) -> usize
pub fn link_dim(&self, i: usize) -> usize
Get link dimension at position i (between site i and i+1)
Sourcepub fn clear_cache(&mut self)
pub fn clear_cache(&mut self)
Clear all cached values
Sourcepub fn evaluate_left(&mut self, indices: &[LocalIndex]) -> Vec<T>
pub fn evaluate_left(&mut self, indices: &[LocalIndex]) -> Vec<T>
Evaluate from the left up to (but not including) site end
Returns a vector of size link_dim(end-1) (or 1 if end == 0)
Sourcepub fn evaluate_right(&mut self, indices: &[LocalIndex]) -> Vec<T>
pub fn evaluate_right(&mut self, indices: &[LocalIndex]) -> Vec<T>
Evaluate from the right starting at site start
indices contains indices for sites start to n-1
Returns a vector of size link_dim(start-1) (or 1 if start == n)
Sourcepub fn evaluate(&mut self, indices: &[LocalIndex]) -> Result<T>
pub fn evaluate(&mut self, indices: &[LocalIndex]) -> Result<T>
Evaluate the tensor train at a given index set using cache
Sourcepub fn evaluate_many(
&mut self,
indices: &[MultiIndex],
split: Option<usize>,
) -> Result<Vec<T>>
pub fn evaluate_many( &mut self, indices: &[MultiIndex], split: Option<usize>, ) -> Result<Vec<T>>
Batch evaluate at multiple index sets
This method efficiently evaluates the tensor train at multiple indices by splitting at a given position and computing unique left/right environments.
§Arguments
indices- The indices to evaluatesplit- Optional split position. IfNone, uses a simple heuristic (checks 1/4, 1/2, 3/4 positions and picks the best). If you know the optimal split position (e.g., from TCI), passSome(split)to avoid the search overhead.
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for TTCache<T>
impl<T> RefUnwindSafe for TTCache<T>where
T: RefUnwindSafe,
impl<T> Send for TTCache<T>
impl<T> Sync for TTCache<T>
impl<T> Unpin for TTCache<T>where
T: Unpin,
impl<T> UnsafeUnpin for TTCache<T>
impl<T> UnwindSafe for TTCache<T>where
T: UnwindSafe,
Blanket Implementations§
§impl<U> As for U
impl<U> As for U
§fn as_<T>(self) -> Twhere
T: CastFrom<U>,
fn as_<T>(self) -> Twhere
T: CastFrom<U>,
self to type T. The semantics of numeric casting with the as operator are followed, so <T as As>::as_::<U> can be used in the same way as T as U for numeric conversions. Read moreSource§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> Twhere
Self: Distribution<T>,
§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> Twhere
Self: Distribution<T>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more