pub struct EinsumExpr { /* private fields */ }Expand description
Parsed einsum expression with N inputs (indices as ASCII codes)
Supports 1 to N input tensors. Output indices can be explicit (->out)
or implicitly inferred (free indices sorted alphabetically).
§Examples
use ariadnetor_core::EinsumExpr;
// Matrix multiplication
let expr = EinsumExpr::parse("ij,jk->ik").unwrap();
assert_eq!(expr.num_inputs(), 2);
assert!(expr.is_matrix_multiply());
assert_eq!(expr.infer_output_shape(&[&[10, 20], &[20, 30]]).unwrap(), vec![10, 30]);
// Higher-dimensional contraction (not a plain matmul)
let expr = EinsumExpr::parse("ijk,jkl->il").unwrap();
assert_eq!(expr.out_indices(), &[b'i', b'l']);
assert_eq!(expr.contracted_indices(), vec![b'j', b'k']);
assert!(!expr.is_matrix_multiply());
// Element-wise: every index appears in the output, nothing is contracted
let expr = EinsumExpr::parse("ij,ij->ij").unwrap();
assert!(expr.contracted_indices().is_empty());
// Implicit output inference
let expr = EinsumExpr::parse("ij,jk").unwrap();
assert_eq!(expr.out_indices(), &[b'i', b'k']);
// Single tensor trace
let expr = EinsumExpr::parse("ii->").unwrap();
assert_eq!(expr.num_inputs(), 1);
// Errors: an output index absent from every input, or a non-alphabetic index
assert!(EinsumExpr::parse("ij,jk->im").is_err());
assert!(EinsumExpr::parse("i1,jk->ik").is_err());Implementations§
Source§impl EinsumExpr
impl EinsumExpr
Sourcepub fn parse(notation: &str) -> Result<EinsumExpr, String>
pub fn parse(notation: &str) -> Result<EinsumExpr, String>
Parse an einsum expression from string notation.
When -> is present, output indices are explicit.
When -> is omitted, output is inferred as free indices (appearing
exactly once across all inputs) sorted alphabetically.
Sourcepub fn validate(&self) -> Result<(), String>
pub fn validate(&self) -> Result<(), String>
Validate the einsum expression.
Checks that all output indices appear in at least one input tensor.
Sourcepub fn out_indices(&self) -> &[u8] ⓘ
pub fn out_indices(&self) -> &[u8] ⓘ
Get output indices
Sourcepub fn num_inputs(&self) -> usize
pub fn num_inputs(&self) -> usize
Number of input tensors
Sourcepub fn lhs_indices(&self) -> &[u8] ⓘ
pub fn lhs_indices(&self) -> &[u8] ⓘ
Sourcepub fn rhs_indices(&self) -> &[u8] ⓘ
pub fn rhs_indices(&self) -> &[u8] ⓘ
Convenience accessor for the second input’s indices.
§Panics
Panics if the expression has fewer than 2 inputs.
Sourcepub fn all_indices(&self) -> HashSet<u8>
pub fn all_indices(&self) -> HashSet<u8>
Get all unique indices across inputs and output
Sourcepub fn contracted_indices(&self) -> Vec<u8> ⓘ
pub fn contracted_indices(&self) -> Vec<u8> ⓘ
Get contracted indices (appear in inputs but not in output), preserving the order of first appearance across inputs.
§Examples
let expr = EinsumExpr::parse("ijk,jkl->il").unwrap();
assert_eq!(expr.contracted_indices(), vec![b'j', b'k']);Sourcepub fn is_matrix_multiply(&self) -> bool
pub fn is_matrix_multiply(&self) -> bool
Check if this is a matrix multiplication pattern: 2 inputs, 3 unique indices, each input has 2 indices, output has 2 indices, exactly 1 contracted index.
§Examples
assert!(EinsumExpr::parse("ij,jk->ik").unwrap().is_matrix_multiply());
assert!(!EinsumExpr::parse("ijk,jkl->il").unwrap().is_matrix_multiply());Sourcepub fn infer_output_shape(
&self,
shapes: &[&[usize]],
) -> Result<Vec<usize>, String>
pub fn infer_output_shape( &self, shapes: &[&[usize]], ) -> Result<Vec<usize>, String>
Infer the output tensor shape from input shapes.
The number of shapes must match num_inputs(), and each shape’s rank
must match its corresponding input index count. Shared indices must
have matching dimensions.
§Examples
let expr = EinsumExpr::parse("ij,jk->ik").unwrap();
assert_eq!(expr.infer_output_shape(&[&[10, 20], &[20, 30]]).unwrap(), vec![10, 30]);Trait Implementations§
Source§impl Clone for EinsumExpr
impl Clone for EinsumExpr
Source§fn clone(&self) -> EinsumExpr
fn clone(&self) -> EinsumExpr
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for EinsumExpr
impl Debug for EinsumExpr
impl Eq for EinsumExpr
Source§impl PartialEq for EinsumExpr
impl PartialEq for EinsumExpr
Source§fn eq(&self, other: &EinsumExpr) -> bool
fn eq(&self, other: &EinsumExpr) -> bool
self and other values to be equal, and is used by ==.impl StructuralPartialEq for EinsumExpr
Auto Trait Implementations§
impl Freeze for EinsumExpr
impl RefUnwindSafe for EinsumExpr
impl Send for EinsumExpr
impl Sync for EinsumExpr
impl Unpin for EinsumExpr
impl UnsafeUnpin for EinsumExpr
impl UnwindSafe for EinsumExpr
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<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
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