TensorProductSum

class TensorProductSum(term_list: tuple | list | None, count: Counter = None, hash_: int = None)[source]

A linear combination of tensor products of forests. Works with both non-planar (CommutativeForest) and planar (NoncommutativeForest) forests.

Parameters:

term_list – A list of tuples representing terms in the sum. Tuples must be of the form (c, f1, f2), where c is an int or float and f1, f2 are Forests, representing the term \(c \cdot (f1 \otimes f2)\).

Example usage:

tp = Tree([]) @ Tree([[]]) - 2 * Tree([[],[]]) @ Tree(None)
kr.display(tp)
2
__add__(other: TensorProductSum) TensorProductSum[source]

Adds two tensor product sums, or adds a scalar (treated as scalar * (empty ⊗ empty)).

Parameters:

other (TensorProductSum | int | float) – Other tensor product sum or scalar

__eq__(other: TensorProductSum) bool[source]

Compares the tensor product sum with another tensor product sum and returns true if they represent the same sum, regardless of possible reorderings of trees within forests or reorderings of terms.

Parameters:

other – TensorProductSum

Return type:

bool

Example usage:

t1 = Tree([])
t2 = Tree([[]])
t3 = Tree([[[]],[]])
t4 = Tree([[],[[]]])
print(t1 @ t2 + t2 @ t3 == t2 @ t3 + t1 @ t2)
print(t1 @ (t2 * t3) == t1 @ (t3 * t2))
print(t1 @ t3 == t1 @ t4)
True
True
True
__mul__(other: int | float | TensorProductSum) TensorProductSum[source]

Multiplies a tensor product sum by a scalar or tensor product sum.

Parameters:

other (int | float | TensorProductSum) – Other

colors()[source]

Returns the number of colors/labels in the tensor product sum. Since the labels are indexed starting from 0, this is equivalent to one more than the maximum label.

Returns:

Number of colors

Return type:

int

Example usage:

print((Tree([[9],0]) @ Tree([3]) + Tree([2]) @ Tree([4])).colors())
10
simplify() TensorProductSum[source]

Simplify the tensor product sum by removing redundant empty trees and cancelling terms where applicable.

Returns:

Reduces tensor product sum

Return type:

TensorProductSum

Example usage:

tp1 = Tree([[],[[]]]) @ (Tree([]) * Tree(None)) + Tree([]) @ Tree([[]]) - Tree([]) @ Tree([[]])
tp2 = tp1.simplify() # Tree([[],[[]]]) @ Tree([])
singleton_reduced() TensorProductSum[source]

Removes redundant occurrences of the single-node tree in each forest of the tensor product sum. If the forest contains a tree with more than one node, removes all occurences of the single-node tree. Otherwise, replaces it with the single-node tree.

Returns:

Singleton-reduced tensor product sum

Return type:

TensorProductSum

Example usage:

s1 = (Tree([]) * Tree([[],[]])) @ (Tree([]) * Tree([]) * Tree([]))
kr.display(s1, '→', s1.singleton_reduced())