proarrow
Safe HaskellNone
LanguageGHC2024

Proarrow.Tools.Einsum

Description

Einstein summation with a numpy-style specification, as in einsum @"ij,jk->ik" a b, in any hypergraph category. A tensor is a state, Tensor xs, whose type lists the objects of its indices. Each letter of the specification is an index; the letters of the inputs are matched with the objects of the tensors given, so a letter used with two different objects does not compile, and the result's objects are those of the output letters. Without -> the output is, as in numpy, the letters used once, in alphabetical order. A letter may also occur more than once in the output, which copies it.

The specification is an open hypergraph (Proarrow.Category.Instance.OpenHypergraph): a node for each letter, the tensors as boxes, and the output letters as its boundary. It is already in normal form, and the result is its simplifyHypergraph: the tensors contracted in pairs, each letter summed out by a spider as soon as nothing still to come has it.

Synopsis

Documentation

type Tensor (xs :: [k]) = State xs Source Github #

A tensor with indices of the given objects: a state of their tensor, as a morphism of Strictified from '[].

einsum :: forall {k} (s :: Symbol) r. Einsum (Inputs s) (Output s) ('[] :: [([Char], [k])]) r => r Source Github #

Einstein summation: einsum @"ij,jk->ik" a b is the tensor with entries the sums over j of the products of the entries of a and b. The tensors are given after the specification, one for each input, and the result's type follows from theirs.

class Einsum (ins :: [[Char]]) (out :: [Char]) (ts :: [([Char], [k])]) r Source Github #

Collect the tensors of the inputs, then sum.

Minimal complete definition

collect

Instances

Instances details
(env ~ BindAll (Reverse ts) ('[] :: [(Char, k)]), Check ts out env, Hypergraph k, Sized k, os ~ Objs out env, r ~ Tensor os, SortList os, KnownChars out) => Einsum ('[] :: [[Char]]) out (ts :: [([Char], [k])]) r Source Github # 
Instance details

Defined in Proarrow.Tools.Einsum

Methods

collect :: [([Char], SomeArrow k)] -> r

(r ~ (Tensor xs -> r'), KnownChars ls, SortList xs, Einsum ins out ('(ls, xs) ': ts) r') => Einsum (ls ': ins) out (ts :: [([Char], [k])]) r Source Github # 
Instance details

Defined in Proarrow.Tools.Einsum

Methods

collect :: [([Char], SomeArrow k)] -> r

type EinsumType (s :: Symbol) (xss :: [[k]]) = Arrows (Inputs s) (Output s) (BindAll (Zip (Inputs s) xss) ('[] :: [(Char, k)])) Source Github #

The type of einsum @s applied to tensors with indices of the given objects. Each letter takes the object it is first given, so a letter given two objects is one type variable in every input, as in EinsumType "ij,jk" '[ '[a, b], '[c, d]] = Tensor '[a, b] -> Tensor '[b, d] -> Tensor '[a, d].

type Inputs (s :: Symbol) = (Fst :: ([[Char]], [Char]) +-> [[Char]]) @ Parse s Source Github #

The letters of each input.

type Output (s :: Symbol) = (Snd :: ([[Char]], [Char]) +-> [Char]) @ Parse s Source Github #

The letters of the output.