Struct SizedContraction

Source
pub struct SizedContraction {
    pub contraction: Contraction,
    pub output_size: HashMap<char, usize>,
}
Expand description

A SizedContraction contains a Contraction as well as a HashMap<char, usize> specifying the axis lengths for each index in the contraction.

Note that output_size is a misnomer (to be changed); it contains all the axis lengths, including the ones that will be contracted (i.e. not just the ones in contraction.output_indices).

Fields§

§contraction: Contraction§output_size: HashMap<char, usize>

Implementations§

Source§

impl SizedContraction

Source

pub fn subset( &self, new_operand_indices: &[Vec<char>], new_output_indices: &[char], ) -> Result<Self, &'static str>

Creates a new SizedContraction based on a subset of the operand indices and/or output indices. Not intended for general use; used internally in the crate when compiling a multi-tensor contraction into a set of tensor simplifications (a.k.a. singleton contractions) and pairwise contractions.

let m1: Array3<f64> = Array::zeros((2, 2, 3));
let m2: Array2<f64> = Array::zeros((3, 4));
let sc = SizedContraction::new("iij,jk->ik", &[&m1, &m2]).unwrap();
let lhs_simplification = sc.subset(&[vec!['i','i','j']], &['i','j']).unwrap();
let diagonalized_m1 = lhs_simplification.contract_operands(&[&m1]);
assert_eq!(diagonalized_m1.shape(), &[2, 3]);
Source

fn from_contraction_and_shapes( contraction: &Contraction, operand_shapes: &[Vec<usize>], ) -> Result<Self, &'static str>

Source

pub fn from_contraction_and_operands<A>( contraction: &Contraction, operands: &[&dyn ArrayLike<A>], ) -> Result<Self, &'static str>

Create a SizedContraction from an already-created Contraction and a list of operands.

let m1: Array2<f64> = Array::zeros((2, 3));
let m2: Array2<f64> = Array::zeros((3, 4));
let c = Contraction::new("ij,jk->ik").unwrap();
let sc = SizedContraction::from_contraction_and_operands(&c, &[&m1, &m2]).unwrap();
assert_eq!(sc.output_size[&'i'], 2);
assert_eq!(sc.output_size[&'k'], 4);
assert_eq!(sc.output_size[&'j'], 3);
Source

pub fn from_string_and_shapes( input_string: &str, operand_shapes: &[Vec<usize>], ) -> Result<Self, &'static str>

Create a SizedContraction from an einsum-formatted input string and a slice of Vec<usize>s containing the shapes of each operand.

let sc = SizedContraction::from_string_and_shapes(
    "ij,jk->ik",
    &[vec![2, 3], vec![3, 4]]
).unwrap();
assert_eq!(sc.output_size[&'i'], 2);
assert_eq!(sc.output_size[&'k'], 4);
assert_eq!(sc.output_size[&'j'], 3);
Source

pub fn new<A>( input_string: &str, operands: &[&dyn ArrayLike<A>], ) -> Result<Self, &'static str>

Create a SizedContraction from an einsum-formatted input string and a list of operands.

let m1: Array2<f64> = Array::zeros((2, 3));
let m2: Array2<f64> = Array::zeros((3, 4));
let sc = SizedContraction::new("ij,jk->ik", &[&m1, &m2]).unwrap();
assert_eq!(sc.output_size[&'i'], 2);
assert_eq!(sc.output_size[&'k'], 4);
assert_eq!(sc.output_size[&'j'], 3);
Source

pub fn contract_operands<A: Clone + LinalgScalar>( &self, operands: &[&dyn ArrayLike<A>], ) -> ArrayD<A>

Perform the contraction on a set of operands.

let m1: Array2<f64> = Array::zeros((2, 3));
let m2: Array2<f64> = Array::zeros((3, 4));
let out: ArrayD<f64> = Array::zeros((2, 4)).into_dyn();
let sc = validate_and_size("ij,jk->ik", &[&m1, &m2]).unwrap();
assert_eq!(sc.contract_operands(&[&m1, &m2]), out);
Source

pub fn as_einsum_string(&self) -> String

Show as an einsum-formatted string.

let m1: Array2<f64> = Array::zeros((2, 3));
let m2: Array2<f64> = Array::zeros((3, 4));
let sc = validate_and_size("ij,jk", &[&m1, &m2]).unwrap();
assert_eq!(sc.as_einsum_string(), "ij,jk->ik");

Trait Implementations§

Source§

impl Clone for SizedContraction

Source§

fn clone(&self) -> SizedContraction

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for SizedContraction

Source§

fn fmt(&self, f: &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
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
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.

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> 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.