TermSet¶
- class zixy.container.terms.TermSet(terms: Terms[ImplT, SpecT, CoeffT])[source]¶
Bases:
Generic[ImplT,SpecT,CoeffT]A collection of unique terms consisting of components and coefficients.
A set-like container of terms that may be used to store unique terms and perform set-like operations on them.
Note
Coefficients are mutable in-place, but components are the keys of a hashmap and therefore are not.
- __getitem__(value: SpecT | Cmpnt[ImplT, SpecT] | Term[ImplT, SpecT, CoeffT]) CoeffT[source]¶
Get the coefficient of the term with component specified by
value.- Parameters:
value – The component or term specifier.
- Returns:
The coefficient of the term with component specified by
value.- Raises:
KeyError – The component specified by
valuewas not found inself.
- __init__(terms: Terms[ImplT, SpecT, CoeffT])[source]¶
Initialize the term set.
- Parameters:
terms – Terms-derived object from which to construct the set of components and their coefficients.
Note
- Components which appear multiple times in
termswill appear only once in self.
- __setitem__(key: SpecT | Term[ImplT, SpecT, CoeffT], coeff: CoeffT) None[source]¶
Set the coefficient of the term with component specified by
key.
- contains(value: SpecT | Cmpnt[ImplT, SpecT] | Term[ImplT, SpecT, CoeffT]) bool[source]¶
Check whether the component specified by
valueis stored inself.- Parameters:
value – The component specifier.
- Returns:
Whether the lookup of
valuewas successful.
- filter_map(f: Callable[[Term[ImplT, SpecT, CoeffT]], bool]) Self[source]¶
Eagerly evaluate a filter-map operation over the terms of
self.- Parameters:
f – Function which may mutate copies of the terms of
self, returningTrueif those mutated copies are to be included in the generator. The function signature should take a singleTerminstance as an argument, and return a boolean.- Returns:
New instance containing the occurrences of selected (and possibly mutated) components of
selfaccording tof.
Note
The resulting generator enforces uniqueness of components by overwriting coefficients of duplicate components.
- classmethod from_iterable(iterable: Iterable[Term[ImplT, SpecT, CoeffT] | Cmpnt[ImplT, SpecT] | SpecT | tuple[SpecT | Cmpnt[ImplT, SpecT] | None, CoeffT | None] | None], *args: Any, **kwargs: Any) Self[source]¶
Create a new instance of
clsfrom an iterable.- Parameters:
iterable – Iterable returning specifiers of all the terms to be appended.
args – Positional arguments to forward to the constructor of
cls.kwargs – Keyword arguments to forward to the constructor of
cls.
- Returns:
New instance of
clscontaining the terms specified byiterable.
- classmethod from_terms(terms: Terms[ImplT, SpecT, CoeffT]) Self[source]¶
Create a new instance of
clsfromterms.- Parameters:
terms – Terms-derived object from which to construct the set of terms.
- Returns:
A new instance of
clscontaining the terms interms.
- insert(key: Term[ImplT, SpecT, CoeffT] | Cmpnt[ImplT, SpecT] | SpecT | tuple[SpecT | Cmpnt[ImplT, SpecT] | None, CoeffT | None] | None) int[source]¶
Try to insert the given term.
- Parameters:
key – The term specifier.
- Returns:
The index at which the term was inserted, or the index at which it already was stored if insertion is unsuccessful.
Note
This method operates in-place.
- insert_iterable(source: Iterable[Term[ImplT, SpecT, CoeffT] | Cmpnt[ImplT, SpecT] | SpecT | tuple[SpecT | Cmpnt[ImplT, SpecT] | None, CoeffT | None] | None] = ()) None[source]¶
Insert many terms from an iterable source.
- Parameters:
source – Iterable over any term specifiers.
Note
This method operates in-place.
- into(t: type[TermSet[ImplT, SpecT, OtherCoeffT]]) TermSet[ImplT, SpecT, OtherCoeffT][source]¶
Clone
selfinto a new related container of typet.- Parameters:
t – Type of the new container to create.
- Returns:
A new instance of
tcontaining the same data asself.
- iter_filter_map(f: Callable[[Term[ImplT, SpecT, CoeffT]], bool]) Iterator[Term[ImplT, SpecT, CoeffT]][source]¶
Lazily evaluate a filter-map operation over the terms of
self.- Parameters:
f – Function which may mutate copies of the terms of
self, returningTrueif those mutated copies are to be included in the generator. The function signature should take a singleTerminstance as an argument, and return a boolean.- Returns:
Iterator over the selected (and possibly mutated) components of
selfaccording tof.
Note
The resulting generator does not enforce uniqueness of components.
- iter_filter_nonzero() Iterator[Term[ImplT, SpecT, CoeffT]][source]¶
Iterate over the non-zero terms of
self.
- lookup(value: SpecT | Cmpnt[ImplT, SpecT] | Term[ImplT, SpecT, CoeffT]) tuple[int, CoeffT] | None[source]¶
Try to find the index and coefficient of the component specified by
value.- Parameters:
value – The component or term specifier.
- Returns:
The index at which the value was inserted, or
Noneif the value was not found.
- lookup_coeff(value: SpecT | Cmpnt[ImplT, SpecT] | Term[ImplT, SpecT, CoeffT]) CoeffT | None[source]¶
Try to find the coefficient of the term specified by
valueinself.- Parameters:
value – The component or term specifier.
- Returns:
The coefficient of the term specified by
valueif it is present inself, andNoneotherwise.
- lookup_index(value: SpecT | Cmpnt[ImplT, SpecT] | Term[ImplT, SpecT, CoeffT]) int | None[source]¶
Try to find the index of the component specified by
valueinself.- Parameters:
value – The component or term specifier.
- Returns:
The index of the component specified by
valueif it is present inself, andNoneotherwise.
- remove(value: SpecT | Cmpnt[ImplT, SpecT] | Term[ImplT, SpecT, CoeffT]) int[source]¶
Try to remove the component specified by
valuefromself.If the component is found, removal proceeds via swap-remove.
- Parameters:
value – The component specifier.
- Returns:
The index at which the component was removed.
- Raises:
KeyError – The component was not found.
- soft_insert(key: Term[ImplT, SpecT, CoeffT] | Cmpnt[ImplT, SpecT] | SpecT | tuple[SpecT | Cmpnt[ImplT, SpecT] | None, CoeffT | None] | None) tuple[int, bool][source]¶
Insertion method which does not overwrite coefficient values.
Operates similarly to
insert(), but does not overwrite coefficient values if the component specified bykeyis already present inself.- Parameters:
key – The term specifier.
- Returns:
Index at which the term was inserted or found, and a boolean indicating whether insertion was successful (i.e. whether the component specified by
keywas not already present inself).
Note
This method operates in-place.