Skip to main content

EinsumExpr

Struct EinsumExpr 

Source
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

Source

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.

Source

pub fn validate(&self) -> Result<(), String>

Validate the einsum expression.

Checks that all output indices appear in at least one input tensor.

Source

pub fn inputs(&self) -> &[Vec<u8>]

Get all input index lists

Source

pub fn out_indices(&self) -> &[u8]

Get output indices

Source

pub fn num_inputs(&self) -> usize

Number of input tensors

Source

pub fn lhs_indices(&self) -> &[u8]

Convenience accessor for the first input’s indices.

§Panics

Panics if the expression has no inputs.

Source

pub fn rhs_indices(&self) -> &[u8]

Convenience accessor for the second input’s indices.

§Panics

Panics if the expression has fewer than 2 inputs.

Source

pub fn all_indices(&self) -> HashSet<u8>

Get all unique indices across inputs and output

Source

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

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

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

Source§

fn clone(&self) -> EinsumExpr

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 EinsumExpr

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Eq for EinsumExpr

Source§

impl PartialEq for EinsumExpr

Source§

fn eq(&self, other: &EinsumExpr) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for EinsumExpr

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> ByRef<T> for T

Source§

fn by_ref(&self) -> &T

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> DistributionExt for T
where T: ?Sized,

Source§

fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> T
where Self: Distribution<T>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.
Source§

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

Source§

fn vzip(self) -> V