pub struct PreparedContraction { /* private fields */ }Expand description
A caller-owned plan for repeated contractions with fixed index metadata.
Prepare this once when repeated operands keep the same ordered indices,
dimensions, and axis classes but their values, dtypes, or gradient state may
change. Planning reuse applies to N-ary or retained-index execution. A binary
call without retained indices preserves the faster pairwise path and does not
consume the stored N-ary labels; use contract_pair directly for that case.
Fresh index identities require a fresh plan.
§Examples
use tensor4all_core::{ContractionOptions, DynIndex, IdxTensor, PreparedContraction};
let i = DynIndex::new_dyn(2);
let j = DynIndex::new_dyn(2);
let k = DynIndex::new_dyn(2);
let a = IdxTensor::from_dense(vec![i.clone(), k.clone()], vec![1.0, 2.0, 3.0, 4.0])?;
let b = IdxTensor::from_dense(vec![k, j.clone()], vec![5.0, 6.0, 7.0, 8.0])?;
let c = IdxTensor::from_dense(vec![j], vec![1.0, 2.0])?;
let plan = PreparedContraction::new(&[&a, &b, &c], ContractionOptions::new())?;
let result = plan.execute(&[&a, &b, &c])?;
assert_eq!(result.indices(), &[i]);
assert_eq!(result.to_vec::<f64>()?, vec![85.0, 126.0]);Implementations§
Source§impl PreparedContraction
impl PreparedContraction
Sourcepub fn new(
tensors: &[&IdxTensor],
options: ContractionOptions<'_>,
) -> Result<Self, IdxTensorError>
pub fn new( tensors: &[&IdxTensor], options: ContractionOptions<'_>, ) -> Result<Self, IdxTensorError>
Prepare index matching, label assignment, and result ordering.
§Arguments
tensors- Representative operands whose ordered index metadata defines the execution contract.options- Retained indices to preserve in every execution result.
§Returns
An immutable caller-owned plan reusable with compatible operands.
§Errors
Returns IdxTensorError when no operands are supplied, retained indices
are absent, the index relationships do not form the requested connected
network, or the result would contain duplicate output indices.
§Examples
use tensor4all_core::{ContractionOptions, DynIndex, IdxTensor, PreparedContraction};
let left = DynIndex::new_dyn(2);
let right = DynIndex::new_dyn(2);
let a = IdxTensor::from_dense(vec![left.clone()], vec![1.0, 2.0])?;
let b = IdxTensor::from_dense(
vec![left, right.clone()],
vec![3.0, 0.0, 0.0, 4.0],
)?;
let c = IdxTensor::from_dense(vec![right], vec![5.0, 6.0])?;
let plan = PreparedContraction::new(&[&a, &b, &c], ContractionOptions::new())?;
assert_eq!(plan.execute(&[&a, &b, &c])?.to_vec::<f64>()?, vec![63.0]);Sourcepub fn execute(
&self,
tensors: &[&IdxTensor],
) -> Result<IdxTensor, IdxTensorError>
pub fn execute( &self, tensors: &[&IdxTensor], ) -> Result<IdxTensor, IdxTensorError>
Execute this plan with compatible operands.
Operand values, dtypes, and gradient state may differ from preparation; ordered full indices, dimensions, and axis classes must match exactly.
§Returns
The contracted tensor in the result-index order fixed at preparation.
§Errors
Returns IdxTensorError::ShapeMismatch before backend execution when
operand count, indices, dimensions, or axis classes differ. Storage,
dtype-promotion, AD, or backend execution failures retain their ordinary
IdxTensorError diagnostics.
§Examples
use tensor4all_core::{ContractionOptions, DynIndex, IdxTensor, PreparedContraction};
let left = DynIndex::new_dyn(2);
let right = DynIndex::new_dyn(2);
let a = IdxTensor::from_dense(vec![left.clone()], vec![1.0, 2.0])?;
let b = IdxTensor::from_dense(
vec![left, right.clone()],
vec![3.0, 0.0, 0.0, 4.0],
)?;
let c = IdxTensor::from_dense(vec![right.clone()], vec![5.0, 6.0])?;
let plan = PreparedContraction::new(&[&a, &b, &c], ContractionOptions::new())?;
let updated = IdxTensor::from_dense(vec![right], vec![1.0, 1.0])?;
assert_eq!(plan.execute(&[&a, &b, &updated])?.to_vec::<f64>()?, vec![11.0]);Trait Implementations§
Source§impl Clone for PreparedContraction
impl Clone for PreparedContraction
Source§fn clone(&self) -> PreparedContraction
fn clone(&self) -> PreparedContraction
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for PreparedContraction
impl RefUnwindSafe for PreparedContraction
impl Send for PreparedContraction
impl Sync for PreparedContraction
impl Unpin for PreparedContraction
impl UnsafeUnpin for PreparedContraction
impl UnwindSafe for PreparedContraction
Blanket Implementations§
§impl<U> As for U
impl<U> As for U
§fn as_<T>(self) -> Twhere
T: CastFrom<U>,
U: Sized,
fn as_<T>(self) -> Twhere
T: CastFrom<U>,
U: Sized,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T, U> Imply<T> for U
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