pub struct Subscripts {
    pub inputs: Vec<Subscript>,
    pub output: Subscript,
}
Expand description

Einsum subscripts with tensor names, e.g. ij,jk->ik | arg0 arg1 -> out

Fields§

§inputs: Vec<Subscript>

Input subscript, ij and jk

§output: Subscript

Output subscript.

Implementations§

Returns $\alpha$ if this subscripts requires $O(N^\alpha)$ floating point operation

Returns $\beta$ if this subscripts requires $O(N^\beta)$ memory

Normalize subscripts into “explicit mode”

numpy.einsum has “explicit mode” including ->, e.g. ij,jk->ik and “implicit mode” e.g. ij,jk. The output subscript is determined from input subscripts in implicit mode:

In implicit mode, the chosen subscripts are important since the axes of the output are reordered alphabetically. This means that np.einsum('ij', a) doesn’t affect a 2D array, while np.einsum('ji', a) takes its transpose. Additionally, np.einsum('ij,jk', a, b) returns a matrix multiplication, while, np.einsum('ij,jh', a, b) returns the transpose of the multiplication since subscript ‘h’ precedes subscript ‘i’.

use std::str::FromStr;
use einsum_codegen::{*, parser::*};

let mut names = Namespace::init();

// Infer output subscripts for implicit mode
let raw = RawSubscripts::from_str("ij,jk").unwrap();
let subscripts = Subscripts::from_raw(&mut names, raw);
assert_eq!(subscripts.output.raw(), &['i', 'k']);

// Reordered alphabetically
let raw = RawSubscripts::from_str("ji").unwrap();
let subscripts = Subscripts::from_raw(&mut names, raw);
assert_eq!(subscripts.output.raw(), &['i', 'j']);

Indices to be contracted

use std::str::FromStr;
use maplit::btreeset;
use einsum_codegen::*;

let mut names = Namespace::init();

// Matrix multiplication AB
let subscripts = Subscripts::from_raw_indices(&mut names, "ij,jk->ik").unwrap();
assert_eq!(subscripts.contraction_indices(), btreeset!{'j'});

// Reduce all Tr(AB)
let subscripts = Subscripts::from_raw_indices(&mut names, "ij,ji->").unwrap();
assert_eq!(subscripts.contraction_indices(), btreeset!{'i', 'j'});

// Take diagonal elements
let subscripts = Subscripts::from_raw_indices(&mut names, "ii->i").unwrap();
assert_eq!(subscripts.contraction_indices(), btreeset!{});

Factorize subscripts

ij,jk,kl->il | arg0 arg1 arg2 -> out0

will be factorized with (arg0, arg1) into

ij,jk->ik | arg0 arg1 -> out1
ik,kl->il | out1 arg2 -> out0
use einsum_codegen::{*, parser::RawSubscript};
use std::str::FromStr;
use maplit::btreeset;

let mut names = Namespace::init();
let base = Subscripts::from_raw_indices(&mut names, "ij,jk,kl->il").unwrap();

let (ijjk, ikkl) = base.factorize(&mut names,
  btreeset!{ Position::Arg(0), Position::Arg(1) }
).unwrap();

Escaped subscript for identifier

This is not injective, e.g. i...,j->ij and i,...j->ij returns a same result i____j__ij.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
Write self to the given TokenStream. Read more
Convert self directly into a TokenStream object. Read more
Convert self directly into a TokenStream object. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Returns a Span covering the complete contents of this syntax tree node, or Span::call_site() if this node is empty. Read more
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.