module#

Classes

Module([r, s])

Sparse formal linear combination over a basis.

class Module(r=None, s=None)#

Bases: Generic[CoefT, BasisT]

Sparse formal linear combination over a basis.

Stores terms as [(coefficient, basis_element), ...] and automatically combines like basis elements.

Example

>>> m = Module(2, "c") + Module(15, "a") - Module(3, "x")
>>> m["x"]
-3
>>> m["c"]
2
>>> m["a"]
15
>>> m["c"] = Module(4, "b") + Module(-3, "z")  # substitution
>>> m.to_tuple()
[(15, 'a'), (4, 'b'), (-3, 'z'), (-3, 'x')]
Parameters:
  • r (CoefT | None)

  • s (BasisT | None)

copy()#

Return a deep copy.

Return type:

Module[CoefT, BasisT]

index(s)#

Return index of basis element s or None if not present.

Parameters:

s (BasisT)

Return type:

int | None

append(r, s=None)#

Append a term, combining with an existing basis element if present.

Parameters:
  • r (CoefT | tuple[CoefT, BasisT]) – Coefficient or a (coefficient, basis) pair.

  • s (BasisT | None) – Basis element if r is just a coefficient.

Return type:

None

extend(module_element)#

Extend by an iterable of (coefficient, basis) pairs.

Parameters:

module_element (Iterable[tuple[CoefT, BasisT]])

Return type:

None

filter(filter_function)#

Return list of basis elements s for which filter_function(s) is True.

Return type:

list[BasisT]

sort()#

Sort terms by basis element (lexicographic).

Return type:

None

to_tuple()#

Return a sorted, zero-coefficient-pruned term list.

Return type:

list[tuple[CoefT, BasisT]]

static from_tuples(list_of_tuples)#

Construct a Module from an iterable of (coefficient, basis) pairs, combining duplicates.

Parameters:

list_of_tuples (Iterable[tuple[CoefT, BasisT]])

Return type:

Module[CoefT, BasisT]