Skip to main content

PreparedContraction

Struct PreparedContraction 

Source
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

Source

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]);
Source

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

Source§

fn clone(&self) -> PreparedContraction

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PreparedContraction

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<U> As for U

§

fn as_<T>(self) -> T
where T: CastFrom<U>, U: Sized,

Casts 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 more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> ByRef<T> for T

§

fn by_ref(&self) -> &T

§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> MaybeSend for T
where T: Send,

§

impl<T> MaybeSendSync for T
where T: Send + Sync,

§

impl<T> MaybeSync for T
where T: Sync,

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V