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
impl SizedContraction
Sourcepub fn subset(
&self,
new_operand_indices: &[Vec<char>],
new_output_indices: &[char],
) -> Result<Self, &'static str>
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]);
fn from_contraction_and_shapes( contraction: &Contraction, operand_shapes: &[Vec<usize>], ) -> Result<Self, &'static str>
Sourcepub fn from_contraction_and_operands<A>(
contraction: &Contraction,
operands: &[&dyn ArrayLike<A>],
) -> Result<Self, &'static str>
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);
Sourcepub fn from_string_and_shapes(
input_string: &str,
operand_shapes: &[Vec<usize>],
) -> Result<Self, &'static str>
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);
Sourcepub fn new<A>(
input_string: &str,
operands: &[&dyn ArrayLike<A>],
) -> Result<Self, &'static str>
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);
Sourcepub fn contract_operands<A: Clone + LinalgScalar>(
&self,
operands: &[&dyn ArrayLike<A>],
) -> ArrayD<A>
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);
Sourcepub fn as_einsum_string(&self) -> String
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
impl Clone for SizedContraction
Source§fn clone(&self) -> SizedContraction
fn clone(&self) -> SizedContraction
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more