pub enum NestedEinsum<L: Label> {
Leaf {
tensor_index: usize,
},
Node {
args: Vec<NestedEinsum<L>>,
eins: EinCode<L>,
},
}Expand description
A binary tree representing the contraction order for an einsum.
Each internal node represents a contraction of its two children, and each leaf represents an input tensor.
§Example
use omeco::{EinCode, NestedEinsum};
// Create leaves for input tensors
let leaf0 = NestedEinsum::<char>::leaf(0);
let leaf1 = NestedEinsum::<char>::leaf(1);
// Contract tensors 0 and 1
let contraction = EinCode::new(
vec![vec!['i', 'j'], vec!['j', 'k']],
vec!['i', 'k']
);
let tree = NestedEinsum::node(vec![leaf0, leaf1], contraction);
assert!(tree.is_binary());Variants§
Leaf
A leaf node referencing an input tensor by index.
Node
An internal node representing a contraction.
Fields
args: Vec<NestedEinsum<L>>Child nodes to contract
Implementations§
Source§impl<L: Label> NestedEinsum<L>
impl<L: Label> NestedEinsum<L>
Sourcepub fn node(args: Vec<NestedEinsum<L>>, eins: EinCode<L>) -> Self
pub fn node(args: Vec<NestedEinsum<L>>, eins: EinCode<L>) -> Self
Create an internal node for a contraction.
Sourcepub fn tensor_index(&self) -> Option<usize>
pub fn tensor_index(&self) -> Option<usize>
Get the tensor index if this is a leaf node.
Sourcepub fn is_binary(&self) -> bool
pub fn is_binary(&self) -> bool
Check if the tree is strictly binary (each internal node has exactly 2 children).
Sourcepub fn node_count(&self) -> usize
pub fn node_count(&self) -> usize
Count the total number of nodes in the tree.
Sourcepub fn leaf_count(&self) -> usize
pub fn leaf_count(&self) -> usize
Count the number of leaf nodes (input tensors).
Sourcepub fn leaf_indices(&self) -> Vec<usize>
pub fn leaf_indices(&self) -> Vec<usize>
Get all leaf tensor indices in depth-first order.
Sourcepub fn output_labels(&self, input_labels: &[Vec<L>]) -> Vec<L>
pub fn output_labels(&self, input_labels: &[Vec<L>]) -> Vec<L>
Get the output labels for this subtree.
Requires the original input tensor labels to compute leaf outputs.
Sourcepub fn to_eincode(&self, input_labels: &[Vec<L>]) -> EinCode<L>
pub fn to_eincode(&self, input_labels: &[Vec<L>]) -> EinCode<L>
Convert a NestedEinsum back to a flat EinCode.
This “flattens” the contraction tree by collecting all the input tensors in order and using the final output labels from the root.
§Arguments
input_labels- The original labels for each input tensor, indexed by tensor_index
§Returns
A flat EinCode representing the same computation
§Example
use omeco::{EinCode, NestedEinsum};
let original_ixs = vec![vec!['i', 'j'], vec!['j', 'k']];
let original_output = vec!['i', 'k'];
let code = EinCode::new(original_ixs.clone(), original_output.clone());
// After optimization
let nested = omeco::optimize_code(&code, &omeco::uniform_size_dict(&code, 2), &omeco::GreedyMethod::default()).unwrap();
// Convert back to flat EinCode
let flat = nested.to_eincode(&original_ixs);
assert_eq!(flat.ixs, original_ixs);
assert_eq!(flat.iy, original_output);Sourcepub fn is_path_decomposition(&self) -> bool
pub fn is_path_decomposition(&self) -> bool
Check if this tree forms a valid path decomposition.
A path decomposition is a binary tree where each internal node has at most one internal child (the other must be a leaf). This forms a linear “path” structure that guarantees bounded treewidth.
§Returns
true if the tree is a valid path decomposition, false otherwise.
§Example
use omeco::{EinCode, NestedEinsum};
// Valid path decomposition: ((leaf0, leaf1), leaf2)
let leaf0 = NestedEinsum::<char>::leaf(0);
let leaf1 = NestedEinsum::<char>::leaf(1);
let leaf2 = NestedEinsum::<char>::leaf(2);
let eins1 = EinCode::new(vec![vec!['i', 'j'], vec!['j', 'k']], vec!['i', 'k']);
let node1 = NestedEinsum::node(vec![leaf0, leaf1], eins1);
let eins2 = EinCode::new(vec![vec!['i', 'k'], vec!['k', 'l']], vec!['i', 'l']);
let path_tree = NestedEinsum::node(vec![node1, leaf2], eins2);
assert!(path_tree.is_path_decomposition());Trait Implementations§
Source§impl<L: Clone + Label> Clone for NestedEinsum<L>
impl<L: Clone + Label> Clone for NestedEinsum<L>
Source§fn clone(&self) -> NestedEinsum<L>
fn clone(&self) -> NestedEinsum<L>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<'de, L> Deserialize<'de> for NestedEinsum<L>where
L: Deserialize<'de> + Label,
impl<'de, L> Deserialize<'de> for NestedEinsum<L>where
L: Deserialize<'de> + Label,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<L> Serialize for NestedEinsum<L>
impl<L> Serialize for NestedEinsum<L>
impl<L: Eq + Label> Eq for NestedEinsum<L>
impl<L: Label> StructuralPartialEq for NestedEinsum<L>
Auto Trait Implementations§
impl<L> Freeze for NestedEinsum<L>
impl<L> RefUnwindSafe for NestedEinsum<L>where
L: RefUnwindSafe,
impl<L> Send for NestedEinsum<L>
impl<L> Sync for NestedEinsum<L>
impl<L> Unpin for NestedEinsum<L>where
L: Unpin,
impl<L> UnwindSafe for NestedEinsum<L>where
L: UnwindSafe,
Blanket Implementations§
Source§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,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.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